/* UpdateStatement.c generated by valac 0.34.6, the Vala compiler
 * generated from UpdateStatement.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 <sqlite3.h>
#include <stdlib.h>
#include <string.h>


#define SQL_TYPE_UPDATE_STATEMENT (sql_update_statement_get_type ())
#define SQL_UPDATE_STATEMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SQL_TYPE_UPDATE_STATEMENT, SqlUpdateStatement))
#define SQL_UPDATE_STATEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SQL_TYPE_UPDATE_STATEMENT, SqlUpdateStatementClass))
#define SQL_IS_UPDATE_STATEMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SQL_TYPE_UPDATE_STATEMENT))
#define SQL_IS_UPDATE_STATEMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SQL_TYPE_UPDATE_STATEMENT))
#define SQL_UPDATE_STATEMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SQL_TYPE_UPDATE_STATEMENT, SqlUpdateStatementClass))

typedef struct _SqlUpdateStatement SqlUpdateStatement;
typedef struct _SqlUpdateStatementClass SqlUpdateStatementClass;
typedef struct _SqlUpdateStatementPrivate SqlUpdateStatementPrivate;
#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))

struct _SqlUpdateStatement {
	GObject parent_instance;
	SqlUpdateStatementPrivate * priv;
	sqlite3* db;
};

struct _SqlUpdateStatementClass {
	GObjectClass parent_class;
};

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


static gpointer sql_update_statement_parent_class = NULL;

GType sql_update_statement_get_type (void) G_GNUC_CONST;
#define SQL_UPDATE_STATEMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SQL_TYPE_UPDATE_STATEMENT, SqlUpdateStatementPrivate))
enum  {
	SQL_UPDATE_STATEMENT_DUMMY_PROPERTY
};
static void _g_free0_ (gpointer var);
SqlUpdateStatement* sql_update_statement_new (const gchar* table_name);
SqlUpdateStatement* sql_update_statement_construct (GType object_type, const gchar* table_name);
gint64 sql_update_statement_run (SqlUpdateStatement* self);
SqlUpdateStatement* sql_update_statement_where (SqlUpdateStatement* self, const gchar* where);
SqlUpdateStatement* sql_update_statement_where_eq (SqlUpdateStatement* self, const gchar* col, const gchar* value);
SqlUpdateStatement* sql_update_statement_where_eqi (SqlUpdateStatement* self, const gchar* col, gint64 iv);
SqlUpdateStatement* sql_update_statement_val (SqlUpdateStatement* self, const gchar* col_name, const gchar* col_value);
SqlUpdateStatement* sql_update_statement_vali (SqlUpdateStatement* self, const gchar* col_name, gint col_value);
SqlUpdateStatement* sql_update_statement_vali64 (SqlUpdateStatement* self, const gchar* col_name, gint64 col_value);
SqlUpdateStatement* sql_update_statement_valb (SqlUpdateStatement* self, const gchar* col_name, gboolean col_value);
static void sql_update_statement_finalize (GObject* obj);


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


SqlUpdateStatement* sql_update_statement_construct (GType object_type, const gchar* table_name) {
	SqlUpdateStatement * self = NULL;
	GString* _tmp0_ = NULL;
	GString* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	GString* _tmp3_ = NULL;
	g_return_val_if_fail (table_name != NULL, NULL);
	self = (SqlUpdateStatement*) g_object_new (object_type, NULL);
	_tmp0_ = self->priv->query_builder;
	_tmp1_ = g_string_append (_tmp0_, "UPDATE `");
	_tmp2_ = table_name;
	_tmp3_ = g_string_append (_tmp1_, _tmp2_);
	g_string_append (_tmp3_, "` SET ");
	return self;
}


SqlUpdateStatement* sql_update_statement_new (const gchar* table_name) {
	return sql_update_statement_construct (SQL_TYPE_UPDATE_STATEMENT, table_name);
}


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_update_statement_run (SqlUpdateStatement* self) {
	gint64 result = 0LL;
	sqlite3_stmt* stmt = NULL;
	GString* _tmp0_ = NULL;
	gint ok = 0;
	sqlite3* _tmp1_ = NULL;
	GString* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	sqlite3_stmt* _tmp4_ = NULL;
	gint _tmp5_ = 0;
	gint _tmp6_ = 0;
	sqlite3_stmt* _tmp22_ = NULL;
	gint _tmp23_ = 0;
	gint _tmp24_ = 0;
	sqlite3* _tmp29_ = NULL;
	gint64 _tmp30_ = 0LL;
	g_return_val_if_fail (SQL_IS_UPDATE_STATEMENT (self), 0LL);
	_tmp0_ = self->priv->query_builder;
	g_string_append (_tmp0_, ";");
	_tmp1_ = self->db;
	_tmp2_ = self->priv->query_builder;
	_tmp3_ = _tmp2_->str;
	_tmp5_ = sqlite3_prepare_v2 (_tmp1_, _tmp3_, -1, &_tmp4_, NULL);
	_sqlite3_finalize0 (stmt);
	stmt = _tmp4_;
	ok = _tmp5_;
	_tmp6_ = ok;
	if (_tmp6_ != SQLITE_OK) {
		sqlite3* _tmp7_ = NULL;
		const gchar* _tmp8_ = NULL;
		_tmp7_ = self->db;
		_tmp8_ = sqlite3_errmsg (_tmp7_);
		g_critical ("UpdateStatement.vala:37: %s", _tmp8_);
		result = (gint64) -1;
		_sqlite3_finalize0 (stmt);
		return result;
	}
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp9_ = FALSE;
			_tmp9_ = TRUE;
			while (TRUE) {
				gint _tmp11_ = 0;
				GPtrArray* _tmp12_ = NULL;
				gint _tmp13_ = 0;
				gint _tmp14_ = 0;
				sqlite3_stmt* _tmp15_ = NULL;
				gint _tmp16_ = 0;
				GPtrArray* _tmp17_ = NULL;
				gint _tmp18_ = 0;
				gconstpointer _tmp19_ = NULL;
				gchar* _tmp20_ = NULL;
				GDestroyNotify _tmp21_ = NULL;
				if (!_tmp9_) {
					gint _tmp10_ = 0;
					_tmp10_ = i;
					i = _tmp10_ + 1;
				}
				_tmp9_ = FALSE;
				_tmp11_ = i;
				_tmp12_ = self->priv->bindings;
				_tmp13_ = g_ptr_array_get_length (_tmp12_);
				_tmp14_ = _tmp13_;
				if (!(_tmp11_ < _tmp14_)) {
					break;
				}
				_tmp15_ = stmt;
				_tmp16_ = i;
				_tmp17_ = self->priv->bindings;
				_tmp18_ = i;
				_tmp19_ = g_ptr_array_index (_tmp17_, (guint) _tmp18_);
				_tmp20_ = g_strdup ((const gchar*) _tmp19_);
				_tmp21_ = g_free;
				sqlite3_bind_text (_tmp15_, _tmp16_ + 1, _tmp20_, -1, _tmp21_);
			}
		}
	}
	_tmp22_ = stmt;
	_tmp23_ = sqlite3_step (_tmp22_);
	ok = _tmp23_;
	_tmp24_ = ok;
	if (_tmp24_ == SQLITE_ERROR) {
		sqlite3* _tmp25_ = NULL;
		const gchar* _tmp26_ = NULL;
		sqlite3_stmt* _tmp27_ = NULL;
		const gchar* _tmp28_ = NULL;
		_tmp25_ = self->db;
		_tmp26_ = sqlite3_errmsg (_tmp25_);
		g_critical ("UpdateStatement.vala:45: %s", _tmp26_);
		_tmp27_ = stmt;
		_tmp28_ = sqlite3_sql (_tmp27_);
		g_critical ("UpdateStatement.vala:46: %s", _tmp28_);
		result = (gint64) -1;
		_sqlite3_finalize0 (stmt);
		return result;
	}
	self->priv->ran = TRUE;
	_tmp29_ = self->db;
	_tmp30_ = sqlite3_last_insert_rowid (_tmp29_);
	result = _tmp30_;
	_sqlite3_finalize0 (stmt);
	return result;
}


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


SqlUpdateStatement* sql_update_statement_where (SqlUpdateStatement* self, const gchar* where) {
	SqlUpdateStatement* result = NULL;
	GString* _tmp0_ = NULL;
	GString* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	SqlUpdateStatement* _tmp3_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_STATEMENT (self), NULL);
	g_return_val_if_fail (where != NULL, NULL);
	_tmp0_ = self->priv->query_builder;
	_tmp1_ = g_string_append (_tmp0_, " WHERE ");
	_tmp2_ = where;
	g_string_append (_tmp1_, _tmp2_);
	_tmp3_ = _g_object_ref0 (self);
	result = _tmp3_;
	return result;
}


SqlUpdateStatement* sql_update_statement_where_eq (SqlUpdateStatement* self, const gchar* col, const gchar* value) {
	SqlUpdateStatement* result = NULL;
	GString* _tmp0_ = NULL;
	GString* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	GString* _tmp3_ = NULL;
	GString* _tmp4_ = NULL;
	const gchar* _tmp5_ = NULL;
	GString* _tmp6_ = NULL;
	SqlUpdateStatement* _tmp7_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_STATEMENT (self), NULL);
	g_return_val_if_fail (col != NULL, NULL);
	g_return_val_if_fail (value != NULL, NULL);
	_tmp0_ = self->priv->query_builder;
	_tmp1_ = g_string_append (_tmp0_, " WHERE `");
	_tmp2_ = col;
	_tmp3_ = g_string_append (_tmp1_, _tmp2_);
	_tmp4_ = g_string_append (_tmp3_, "`='");
	_tmp5_ = value;
	_tmp6_ = g_string_append (_tmp4_, _tmp5_);
	g_string_append (_tmp6_, "'");
	_tmp7_ = _g_object_ref0 (self);
	result = _tmp7_;
	return result;
}


SqlUpdateStatement* sql_update_statement_where_eqi (SqlUpdateStatement* self, const gchar* col, gint64 iv) {
	SqlUpdateStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gint64 _tmp1_ = 0LL;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	SqlUpdateStatement* _tmp4_ = NULL;
	SqlUpdateStatement* _tmp5_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_STATEMENT (self), NULL);
	g_return_val_if_fail (col != NULL, NULL);
	_tmp0_ = col;
	_tmp1_ = iv;
	_tmp2_ = g_strdup_printf ("%" G_GINT64_FORMAT, _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = sql_update_statement_where_eq (self, _tmp0_, _tmp3_);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	result = _tmp5_;
	return result;
}


SqlUpdateStatement* sql_update_statement_val (SqlUpdateStatement* self, const gchar* col_name, const gchar* col_value) {
	SqlUpdateStatement* 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;
	SqlUpdateStatement* _tmp11_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_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;
}


SqlUpdateStatement* sql_update_statement_vali (SqlUpdateStatement* self, const gchar* col_name, gint col_value) {
	SqlUpdateStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gint _tmp1_ = 0;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	SqlUpdateStatement* _tmp4_ = NULL;
	SqlUpdateStatement* _tmp5_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_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_update_statement_val (self, _tmp0_, _tmp3_);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	result = _tmp5_;
	return result;
}


SqlUpdateStatement* sql_update_statement_vali64 (SqlUpdateStatement* self, const gchar* col_name, gint64 col_value) {
	SqlUpdateStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gint64 _tmp1_ = 0LL;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	SqlUpdateStatement* _tmp4_ = NULL;
	SqlUpdateStatement* _tmp5_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_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_update_statement_val (self, _tmp0_, _tmp3_);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	result = _tmp5_;
	return result;
}


SqlUpdateStatement* sql_update_statement_valb (SqlUpdateStatement* self, const gchar* col_name, gboolean col_value) {
	SqlUpdateStatement* result = NULL;
	const gchar* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_ = NULL;
	SqlUpdateStatement* _tmp3_ = NULL;
	g_return_val_if_fail (SQL_IS_UPDATE_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_update_statement_val (self, _tmp2_, _tmp0_);
	result = _tmp3_;
	return result;
}


static void sql_update_statement_class_init (SqlUpdateStatementClass * klass) {
	sql_update_statement_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SqlUpdateStatementPrivate));
	G_OBJECT_CLASS (klass)->finalize = sql_update_statement_finalize;
}


static void sql_update_statement_instance_init (SqlUpdateStatement * self) {
	GString* _tmp0_ = NULL;
	GPtrArray* _tmp1_ = NULL;
	self->priv = SQL_UPDATE_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_update_statement_finalize (GObject* obj) {
	SqlUpdateStatement * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SQL_TYPE_UPDATE_STATEMENT, SqlUpdateStatement);
	_g_string_free0 (self->priv->query_builder);
	_g_ptr_array_unref0 (self->priv->bindings);
	G_OBJECT_CLASS (sql_update_statement_parent_class)->finalize (obj);
}


GType sql_update_statement_get_type (void) {
	static volatile gsize sql_update_statement_type_id__volatile = 0;
	if (g_once_init_enter (&sql_update_statement_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SqlUpdateStatementClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sql_update_statement_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SqlUpdateStatement), 0, (GInstanceInitFunc) sql_update_statement_instance_init, NULL };
		GType sql_update_statement_type_id;
		sql_update_statement_type_id = g_type_register_static (G_TYPE_OBJECT, "SqlUpdateStatement", &g_define_type_info, 0);
		g_once_init_leave (&sql_update_statement_type_id__volatile, sql_update_statement_type_id);
	}
	return sql_update_statement_type_id__volatile;
}



