/* InsertStatement.c generated by valac 0.34.6, the Vala compiler
 * generated from InsertStatement.vala, do not modify */

/*  This file is part of corebird, a Gtk+ linux Twitter client.
 *  Copyright (C) 2013 Timm Bäder
 *
 *  corebird is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  corebird is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with corebird.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>


#define SQL_TYPE_INSERT_STATEMENT (sql_insert_statement_get_type ())
#define SQL_INSERT_STATEMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SQL_TYPE_INSERT_STATEMENT, SqlInsertStatement))
#define SQL_INSERT_STATEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SQL_TYPE_INSERT_STATEMENT, SqlInsertStatementClass))
#define SQL_IS_INSERT_STATEMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SQL_TYPE_INSERT_STATEMENT))
#define SQL_IS_INSERT_STATEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SQL_TYPE_INSERT_STATEMENT))
#define SQL_INSERT_STATEMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SQL_TYPE_INSERT_STATEMENT, SqlInsertStatementClass))

typedef struct _SqlInsertStatement SqlInsertStatement;
typedef struct _SqlInsertStatementClass SqlInsertStatementClass;
typedef struct _SqlInsertStatementPrivate SqlInsertStatementPrivate;
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
#define _g_ptr_array_unref0(var) ((var == NULL) ? NULL : (var = (g_ptr_array_unref (var), NULL)))
#define _sqlite3_finalize0(var) ((var == NULL) ? NULL : (var = (sqlite3_finalize (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

typedef gboolean (*SqlSelectCallback) (gchar** vals, int vals_length1, void* user_data);
struct _SqlInsertStatement {
	GObject parent_instance;
	SqlInsertStatementPrivate * priv;
	sqlite3* db;
};

struct _SqlInsertStatementClass {
	GObjectClass parent_class;
};

struct _SqlInsertStatementPrivate {
	GString* query_builder;
	GPtrArray* bindings;
	gboolean ran;
};


static gpointer sql_insert_statement_parent_class = NULL;

GType sql_insert_statement_get_type (void) G_GNUC_CONST;
#define SQL_INSERT_STATEMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SQL_TYPE_INSERT_STATEMENT, SqlInsertStatementPrivate))
enum  {
	SQL_INSERT_STATEMENT_DUMMY_PROPERTY
};
static void _g_free0_ (gpointer var);
SqlInsertStatement* sql_insert_statement_new (const gchar* table_name, gboolean replace);
SqlInsertStatement* sql_insert_statement_construct (GType object_type, const gchar* table_name, gboolean replace);
gint64 sql_insert_statement_run (SqlInsertStatement* self);
SqlInsertStatement* sql_insert_statement_val (SqlInsertStatement* self, const gchar* col_name, const gchar* col_value);
SqlInsertStatement* sql_insert_statement_vali (SqlInsertStatement* self, const gchar* col_name, gint col_value);
SqlInsertStatement* sql_insert_statement_vali64 (SqlInsertStatement* self, const gchar* col_name, gint64 col_value);
SqlInsertStatement* sql_insert_statement_valb (SqlInsertStatement* self, const gchar* col_name, gboolean col_value);
static void sql_insert_statement_finalize (GObject* obj);


static void _g_free0_ (gpointer var) {
	var = (g_free (var), NULL);
}


SqlInsertStatement* sql_insert_statement_construct (GType object_type, const gchar* table_name, gboolean replace) {
	SqlInsertStatement * self = NULL;
	gboolean _tmp0_ = FALSE;
	GString* _tmp3_ = NULL;
	const gchar* _tmp4_ = NULL;
	GString* _tmp5_ = NULL;
	g_return_val_if_fail (table_name != NULL, NULL);
	self = (SqlInsertStatement*) g_object_new (object_type, NULL);
	_tmp0_ = replace;
	if (_tmp0_) {
		GString* _tmp1_ = NULL;
		_tmp1_ = self->priv->query_builder;
		g_string_append (_tmp1_, "INSERT OR REPLACE INTO `");
	} else {
		GString* _tmp2_ = NULL;
		_tmp2_ = self->priv->query_builder;
		g_string_append (_tmp2_, "INSERT INTO `");
	}
	_tmp3_ = self->priv->query_builder;
	_tmp4_ = table_name;
	_tmp5_ = g_string_append (_tmp3_, _tmp4_);
	g_string_append (_tmp5_, "` (");
	return self;
}


SqlInsertStatement* sql_insert_statement_new (const gchar* table_name, gboolean replace) {
	return sql_insert_statement_construct (SQL_TYPE_INSERT_STATEMENT, table_name, replace);
}


static gint g_ptr_array_get_length (GPtrArray* self) {
	gint result;
	guint _tmp0_ = 0U;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->len;
	result = (gint) _tmp0_;
	return result;
}


static void g_ptr_array_set_length (GPtrArray* self, gint value) {
	gint _tmp0_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	g_ptr_array_set_size (self, _tmp0_);
}


gint64 sql_insert_statement_run (SqlInsertStatement* self) {
	gint64 result = 0LL;
	GString* _tmp0_ = NULL;
	GString* _tmp1_ = NULL;
	GString* _tmp9_ = NULL;
	sqlite3_stmt* stmt = NULL;
	gint ok = 0;
	sqlite3* _tmp10_ = NULL;
	GString* _tmp11_ = NULL;
	const gchar* _tmp12_ = NULL;
	sqlite3_stmt* _tmp13_ = NULL;
	gint _tmp14_ = 0;
	gint _tmp15_ = 0;
	sqlite3_stmt* _tmp31_ = NULL;
	gint _tmp32_ = 0;
	gint _tmp33_ = 0;
	sqlite3* _tmp54_ = NULL;
	gint64 _tmp55_ = 0LL;
	g_return_val_if_fail (SQL_IS_INSERT_STATEMENT (self), 0LL);
	_tmp0_ = self->priv->query_builder;
	g_string_append (_tmp0_, ") VALUES (");
	_tmp1_ = self->priv->query_builder;
	g_string_append (_tmp1_, "?");
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				gint _tmp4_ = 0;
				GPtrArray* _tmp5_ = NULL;
				gint _tmp6_ = 0;
				gint _tmp7_ = 0;
				GString* _tmp8_ = NULL;
				if (!_tmp2_) {
					gint _tmp3_ = 0;
					_tmp3_ = i;
					i = _tmp3_ + 1;
				}
				_tmp2_ = FALSE;
				_tmp4_ = i;
				_tmp5_ = self->priv->bindings;
				_tmp6_ = g_ptr_array_get_length (_tmp5_);
				_tmp7_ = _tmp6_;
				if (!(_tmp4_ < (_tmp7_ - 1))) {
					break;
				}
				_tmp8_ = self->priv->query_builder;
				g_string_append (_tmp8_, ",?");
			}
		}
	}
	_tmp9_ = self->priv->query_builder;
	g_string_append (_tmp9_, ");");
	_tmp10_ = self->db;
	_tmp11_ = self->priv->query_builder;
	_tmp12_ = _tmp11_->str;
	_tmp14_ = sqlite3_prepare_v2 (_tmp10_, _tmp12_, -1, &_tmp13_, NULL);
	_sqlite3_finalize0 (stmt);
	stmt = _tmp13_;
	ok = _tmp14_;
	_tmp15_ = ok;
	if (_tmp15_ != SQLITE_OK) {
		sqlite3* _tmp16_ = NULL;
		const gchar* _tmp17_ = NULL;
		_tmp16_ = self->db;
		_tmp17_ = sqlite3_errmsg (_tmp16_);
		g_critical ("InsertStatement.vala:47: %s", _tmp17_);
		result = (gint64) -1;
		_sqlite3_finalize0 (stmt);
		return result;
	}
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp18_ = FALSE;
			_tmp18_ = TRUE;
			while (TRUE) {
				gint _tmp20_ = 0;
				GPtrArray* _tmp21_ = NULL;
				gint _tmp22_ = 0;
				gint _tmp23_ = 0;
				sqlite3_stmt* _tmp24_ = NULL;
				gint _tmp25_ = 0;
				GPtrArray* _tmp26_ = NULL;
				gint _tmp27_ = 0;
				gconstpointer _tmp28_ = NULL;
				gchar* _tmp29_ = NULL;
				GDestroyNotify _tmp30_ = NULL;
				if (!_tmp18_) {
					gint _tmp19_ = 0;
					_tmp19_ = i;
					i = _tmp19_ + 1;
				}
				_tmp18_ = FALSE;
				_tmp20_ = i;
				_tmp21_ = self->priv->bindings;
				_tmp22_ = g_ptr_array_get_length (_tmp21_);
				_tmp23_ = _tmp22_;
				if (!(_tmp20_ < _tmp23_)) {
					break;
				}
				_tmp24_ = stmt;
				_tmp25_ = i;
				_tmp26_ = self->priv->bindings;
				_tmp27_ = i;
				_tmp28_ = g_ptr_array_index (_tmp26_, (guint) _tmp27_);
				_tmp29_ = g_strdup ((const gchar*) _tmp28_);
				_tmp30_ = g_free;
				sqlite3_bind_text (_tmp24_, _tmp25_ + 1, _tmp29_, -1, _tmp30_);
			}
		}
	}
	_tmp31_ = stmt;
	_tmp32_ = sqlite3_step (_tmp31_);
	ok = _tmp32_;
	_tmp33_ = ok;
	if (_tmp33_ != SQLITE_DONE) {
		sqlite3* _tmp34_ = NULL;
		const gchar* _tmp35_ = NULL;
		GString* err_msg = NULL;
		GString* _tmp36_ = NULL;
		GString* _tmp37_ = NULL;
		sqlite3_stmt* _tmp38_ = NULL;
		const gchar* _tmp39_ = NULL;
		GString* _tmp40_ = NULL;
		GString* _tmp52_ = NULL;
		const gchar* _tmp53_ = NULL;
		_tmp34_ = self->db;
		_tmp35_ = sqlite3_errmsg (_tmp34_);
		g_critical ("InsertStatement.vala:56: %s", _tmp35_);
		_tmp36_ = g_string_new ("");
		err_msg = _tmp36_;
		_tmp37_ = err_msg;
		_tmp38_ = stmt;
		_tmp39_ = sqlite3_sql (_tmp38_);
		_tmp40_ = g_string_append (_tmp37_, _tmp39_);
		g_string_append (_tmp40_, " --- ");
		{
			gint i = 0;
			i = 0;
			{
				gboolean _tmp41_ = FALSE;
				_tmp41_ = TRUE;
				while (TRUE) {
					gint _tmp43_ = 0;
					GPtrArray* _tmp44_ = NULL;
					gint _tmp45_ = 0;
					gint _tmp46_ = 0;
					GString* _tmp47_ = NULL;
					GPtrArray* _tmp48_ = NULL;
					gint _tmp49_ = 0;
					gconstpointer _tmp50_ = NULL;
					GString* _tmp51_ = NULL;
					if (!_tmp41_) {
						gint _tmp42_ = 0;
						_tmp42_ = i;
						i = _tmp42_ + 1;
					}
					_tmp41_ = FALSE;
					_tmp43_ = i;
					_tmp44_ = self->priv->bindings;
					_tmp45_ = g_ptr_array_get_length (_tmp44_);
					_tmp46_ = _tmp45_;
					if (!(_tmp43_ < _tmp46_)) {
						break;
					}
					_tmp47_ = err_msg;
					_tmp48_ = self->priv->bindings;
					_tmp49_ = i;
					_tmp50_ = g_ptr_array_index (_tmp48_, (guint) _tmp49_);
					_tmp51_ = g_string_append (_tmp47_, (const gchar*) _tmp50_);
					g_string_append (_tmp51_, ", ");
				}
			}
		}
		_tmp52_ = err_msg;
		_tmp53_ = _tmp52_->str;
		g_critical ("InsertStatement.vala:62: %s", _tmp53_);
		_g_string_free0 (err_msg);
	}
	self->priv->ran = TRUE;
	_tmp54_ = self->db;
	_tmp55_ = sqlite3_last_insert_rowid (_tmp54_);
	result = _tmp55_;
	_sqlite3_finalize0 (stmt);
	return result;
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


SqlInsertStatement* sql_insert_statement_val (SqlInsertStatement* self, const gchar* col_name, const gchar* col_value) {
	SqlInsertStatement* result = NULL;
	GPtrArray* _tmp0_ = NULL;
	gint _tmp1_ = 0;
	gint _tmp2_ = 0;
	GString* _tmp4_ = NULL;
	GString* _tmp5_ = NULL;
	const gchar* _tmp6_ = NULL;
	GString* _tmp7_ = NULL;
	GPtrArray* _tmp8_ = NULL;
	const gchar* _tmp9_ = NULL;
	gchar* _tmp10_ = NULL;
	SqlInsertStatement* _tmp11_ = NULL;
	g_return_val_if_fail (SQL_IS_INSERT_STATEMENT (self), NULL);
	g_return_val_if_fail (col_name != NULL, NULL);
	g_return_val_if_fail (col_value != NULL, NULL);
	_tmp0_ = self->priv->bindings;
	_tmp1_ = g_ptr_array_get_length (_tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ > 0) {
		GString* _tmp3_ = NULL;
		_tmp3_ = self->priv->query_builder;
		g_string_append (_tmp3_, ", ");
	}
	_tmp4_ = self->priv->query_builder;
	_tmp5_ = g_string_append (_tmp4_, "`");
	_tmp6_ = col_name;
	_tmp7_ = g_string_append (_tmp5_, _tmp6_);
	g_string_append (_tmp7_, "`");
	_tmp8_ = self->priv->bindings;
	_tmp9_ = col_value;
	_tmp10_ = g_strdup (_tmp9_);
	g_ptr_array_add (_tmp8_, _tmp10_);
	_tmp11_ = _g_object_ref0 (self);
	result = _tmp11_;
	return result;
}


SqlInsertStatement* sql_insert_statement_vali (SqlInsertStatement* self, const gchar* col_name, gint col_value) {
	SqlInsertStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gint _tmp1_ = 0;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	SqlInsertStatement* _tmp4_ = NULL;
	SqlInsertStatement* _tmp5_ = NULL;
	g_return_val_if_fail (SQL_IS_INSERT_STATEMENT (self), NULL);
	g_return_val_if_fail (col_name != NULL, NULL);
	_tmp0_ = col_name;
	_tmp1_ = col_value;
	_tmp2_ = g_strdup_printf ("%i", _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = sql_insert_statement_val (self, _tmp0_, _tmp3_);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	result = _tmp5_;
	return result;
}


SqlInsertStatement* sql_insert_statement_vali64 (SqlInsertStatement* self, const gchar* col_name, gint64 col_value) {
	SqlInsertStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gint64 _tmp1_ = 0LL;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	SqlInsertStatement* _tmp4_ = NULL;
	SqlInsertStatement* _tmp5_ = NULL;
	g_return_val_if_fail (SQL_IS_INSERT_STATEMENT (self), NULL);
	g_return_val_if_fail (col_name != NULL, NULL);
	_tmp0_ = col_name;
	_tmp1_ = col_value;
	_tmp2_ = g_strdup_printf ("%" G_GINT64_FORMAT, _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = sql_insert_statement_val (self, _tmp0_, _tmp3_);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	result = _tmp5_;
	return result;
}


SqlInsertStatement* sql_insert_statement_valb (SqlInsertStatement* self, const gchar* col_name, gboolean col_value) {
	SqlInsertStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_ = NULL;
	SqlInsertStatement* _tmp3_ = NULL;
	g_return_val_if_fail (SQL_IS_INSERT_STATEMENT (self), NULL);
	g_return_val_if_fail (col_name != NULL, NULL);
	_tmp1_ = col_value;
	if (_tmp1_) {
		_tmp0_ = "1";
	} else {
		_tmp0_ = "0";
	}
	_tmp2_ = col_name;
	_tmp3_ = sql_insert_statement_val (self, _tmp2_, _tmp0_);
	result = _tmp3_;
	return result;
}


static void sql_insert_statement_class_init (SqlInsertStatementClass * klass) {
	sql_insert_statement_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SqlInsertStatementPrivate));
	G_OBJECT_CLASS (klass)->finalize = sql_insert_statement_finalize;
}


static void sql_insert_statement_instance_init (SqlInsertStatement * self) {
	GString* _tmp0_ = NULL;
	GPtrArray* _tmp1_ = NULL;
	self->priv = SQL_INSERT_STATEMENT_GET_PRIVATE (self);
	_tmp0_ = g_string_new ("");
	self->priv->query_builder = _tmp0_;
	_tmp1_ = g_ptr_array_new_full ((guint) 0, _g_free0_);
	self->priv->bindings = _tmp1_;
	self->priv->ran = FALSE;
}


static void sql_insert_statement_finalize (GObject* obj) {
	SqlInsertStatement * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SQL_TYPE_INSERT_STATEMENT, SqlInsertStatement);
	_g_string_free0 (self->priv->query_builder);
	_g_ptr_array_unref0 (self->priv->bindings);
	G_OBJECT_CLASS (sql_insert_statement_parent_class)->finalize (obj);
}


GType sql_insert_statement_get_type (void) {
	static volatile gsize sql_insert_statement_type_id__volatile = 0;
	if (g_once_init_enter (&sql_insert_statement_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SqlInsertStatementClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sql_insert_statement_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SqlInsertStatement), 0, (GInstanceInitFunc) sql_insert_statement_instance_init, NULL };
		GType sql_insert_statement_type_id;
		sql_insert_statement_type_id = g_type_register_static (G_TYPE_OBJECT, "SqlInsertStatement", &g_define_type_info, 0);
		g_once_init_leave (&sql_insert_statement_type_id__volatile, sql_insert_statement_type_id);
	}
	return sql_insert_statement_type_id__volatile;
}



