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

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

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define QLITE_STATEMENT_BUILDER_TYPE_ABSTRACT_FIELD (qlite_statement_builder_abstract_field_get_type ())
#define QLITE_STATEMENT_BUILDER_ABSTRACT_FIELD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), QLITE_STATEMENT_BUILDER_TYPE_ABSTRACT_FIELD, QliteStatementBuilderAbstractField))
#define QLITE_STATEMENT_BUILDER_ABSTRACT_FIELD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), QLITE_STATEMENT_BUILDER_TYPE_ABSTRACT_FIELD, QliteStatementBuilderAbstractFieldClass))
#define QLITE_STATEMENT_BUILDER_IS_ABSTRACT_FIELD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), QLITE_STATEMENT_BUILDER_TYPE_ABSTRACT_FIELD))
#define QLITE_STATEMENT_BUILDER_IS_ABSTRACT_FIELD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), QLITE_STATEMENT_BUILDER_TYPE_ABSTRACT_FIELD))
#define QLITE_STATEMENT_BUILDER_ABSTRACT_FIELD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), QLITE_STATEMENT_BUILDER_TYPE_ABSTRACT_FIELD, QliteStatementBuilderAbstractFieldClass))

typedef struct _QliteStatementBuilderAbstractField QliteStatementBuilderAbstractField;
typedef struct _QliteStatementBuilderAbstractFieldClass QliteStatementBuilderAbstractFieldClass;
#define _qlite_table_unref0(var) ((var == NULL) ? NULL : (var = (qlite_table_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

#define QLITE_STATEMENT_BUILDER_TYPE_FIELD (qlite_statement_builder_field_get_type ())
#define QLITE_STATEMENT_BUILDER_FIELD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), QLITE_STATEMENT_BUILDER_TYPE_FIELD, QliteStatementBuilderField))
#define QLITE_STATEMENT_BUILDER_FIELD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), QLITE_STATEMENT_BUILDER_TYPE_FIELD, QliteStatementBuilderFieldClass))
#define QLITE_STATEMENT_BUILDER_IS_FIELD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), QLITE_STATEMENT_BUILDER_TYPE_FIELD))
#define QLITE_STATEMENT_BUILDER_IS_FIELD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), QLITE_STATEMENT_BUILDER_TYPE_FIELD))
#define QLITE_STATEMENT_BUILDER_FIELD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), QLITE_STATEMENT_BUILDER_TYPE_FIELD, QliteStatementBuilderFieldClass))

typedef struct _QliteStatementBuilderField QliteStatementBuilderField;
typedef struct _QliteStatementBuilderFieldClass QliteStatementBuilderFieldClass;

#define QLITE_STATEMENT_BUILDER_TYPE_NULL_FIELD (qlite_statement_builder_null_field_get_type ())
#define QLITE_STATEMENT_BUILDER_NULL_FIELD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), QLITE_STATEMENT_BUILDER_TYPE_NULL_FIELD, QliteStatementBuilderNullField))
#define QLITE_STATEMENT_BUILDER_NULL_FIELD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), QLITE_STATEMENT_BUILDER_TYPE_NULL_FIELD, QliteStatementBuilderNullFieldClass))
#define QLITE_STATEMENT_BUILDER_IS_NULL_FIELD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), QLITE_STATEMENT_BUILDER_TYPE_NULL_FIELD))
#define QLITE_STATEMENT_BUILDER_IS_NULL_FIELD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), QLITE_STATEMENT_BUILDER_TYPE_NULL_FIELD))
#define QLITE_STATEMENT_BUILDER_NULL_FIELD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), QLITE_STATEMENT_BUILDER_TYPE_NULL_FIELD, QliteStatementBuilderNullFieldClass))

typedef struct _QliteStatementBuilderNullField QliteStatementBuilderNullField;
typedef struct _QliteStatementBuilderNullFieldClass QliteStatementBuilderNullFieldClass;
typedef struct _QliteStatementBuilderAbstractFieldPrivate QliteStatementBuilderAbstractFieldPrivate;
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
#define _sqlite3_finalize0(var) ((var == NULL) ? NULL : (var = (sqlite3_finalize (var), NULL)))

struct _QliteUpsertBuilderPrivate {
	QliteTable* table;
	gchar* table_name;
	QliteStatementBuilderAbstractField** keys;
	gint keys_length1;
	gint _keys_size_;
	QliteStatementBuilderAbstractField** fields;
	gint fields_length1;
	gint _fields_size_;
};

struct _QliteStatementBuilderAbstractField {
	GTypeInstance parent_instance;
	volatile int ref_count;
	QliteStatementBuilderAbstractFieldPrivate * priv;
	gpointer value;
	QliteColumn* column;
};

struct _QliteStatementBuilderAbstractFieldClass {
	GTypeClass parent_class;
	void (*finalize) (QliteStatementBuilderAbstractField *self);
	void (*bind) (QliteStatementBuilderAbstractField* self, sqlite3_stmt* stmt, gint index);
};

static gint QliteUpsertBuilder_private_offset;
static gpointer qlite_upsert_builder_parent_class = NULL;

VALA_EXTERN gpointer qlite_statement_builder_abstract_field_ref (gpointer instance);
VALA_EXTERN void qlite_statement_builder_abstract_field_unref (gpointer instance);
VALA_EXTERN GParamSpec* qlite_statement_builder_param_spec_abstract_field (const gchar* name,
                                                               const gchar* nick,
                                                               const gchar* blurb,
                                                               GType object_type,
                                                               GParamFlags flags);
VALA_EXTERN void qlite_statement_builder_value_set_abstract_field (GValue* value,
                                                       gpointer v_object);
VALA_EXTERN void qlite_statement_builder_value_take_abstract_field (GValue* value,
                                                        gpointer v_object);
VALA_EXTERN gpointer qlite_statement_builder_value_get_abstract_field (const GValue* value);
VALA_EXTERN GType qlite_statement_builder_abstract_field_get_type (void) G_GNUC_CONST ;
VALA_EXTERN sqlite3_stmt* qlite_statement_builder_prepare (QliteStatementBuilder* self);
VALA_EXTERN QliteUpsertBuilder* qlite_upsert_builder_new (QliteDatabase* db,
                                              QliteTable* table);
VALA_EXTERN QliteUpsertBuilder* qlite_upsert_builder_construct (GType object_type,
                                                    QliteDatabase* db,
                                                    QliteTable* table);
VALA_EXTERN QliteStatementBuilder* qlite_statement_builder_construct (GType object_type,
                                                          QliteDatabase* db);
VALA_EXTERN QliteStatementBuilderField* qlite_statement_builder_field_new (GType t_type,
                                                               GBoxedCopyFunc t_dup_func,
                                                               GDestroyNotify t_destroy_func,
                                                               QliteColumn* column,
                                                               gconstpointer value);
VALA_EXTERN QliteStatementBuilderField* qlite_statement_builder_field_construct (GType object_type,
                                                                     GType t_type,
                                                                     GBoxedCopyFunc t_dup_func,
                                                                     GDestroyNotify t_destroy_func,
                                                                     QliteColumn* column,
                                                                     gconstpointer value);
VALA_EXTERN GType qlite_statement_builder_field_get_type (void) G_GNUC_CONST ;
static void _vala_array_add16 (QliteStatementBuilderAbstractField** * array,
                        gint* length,
                        gint* size,
                        QliteStatementBuilderAbstractField* value);
static void _vala_array_add17 (QliteStatementBuilderAbstractField** * array,
                        gint* length,
                        gint* size,
                        QliteStatementBuilderAbstractField* value);
VALA_EXTERN QliteStatementBuilderNullField* qlite_statement_builder_null_field_new (GType t_type,
                                                                        GBoxedCopyFunc t_dup_func,
                                                                        GDestroyNotify t_destroy_func,
                                                                        QliteColumn* column);
VALA_EXTERN QliteStatementBuilderNullField* qlite_statement_builder_null_field_construct (GType object_type,
                                                                              GType t_type,
                                                                              GBoxedCopyFunc t_dup_func,
                                                                              GDestroyNotify t_destroy_func,
                                                                              QliteColumn* column);
VALA_EXTERN GType qlite_statement_builder_null_field_get_type (void) G_GNUC_CONST ;
static void _vala_array_add18 (QliteStatementBuilderAbstractField** * array,
                        gint* length,
                        gint* size,
                        QliteStatementBuilderAbstractField* value);
static void _vala_array_add19 (QliteStatementBuilderAbstractField** * array,
                        gint* length,
                        gint* size,
                        QliteStatementBuilderAbstractField* value);
static sqlite3_stmt* qlite_upsert_builder_real_prepare (QliteStatementBuilder* base);
VALA_EXTERN sqlite3_stmt* qlite_upsert_builder_prepare_upsert (QliteUpsertBuilder* self);
VALA_EXTERN sqlite3_stmt* qlite_database_prepare (QliteDatabase* self,
                                      const gchar* sql);
VALA_EXTERN void qlite_statement_builder_abstract_field_bind (QliteStatementBuilderAbstractField* self,
                                                  sqlite3_stmt* stmt,
                                                  gint index);
VALA_EXTERN gint qlite_database_errcode (QliteDatabase* self);
VALA_EXTERN gchar* qlite_database_errmsg (QliteDatabase* self);
VALA_EXTERN gint64 qlite_database_last_insert_rowid (QliteDatabase* self);
static void qlite_upsert_builder_finalize (QliteStatementBuilder * obj);
static GType qlite_upsert_builder_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static inline gpointer
qlite_upsert_builder_get_instance_private (QliteUpsertBuilder* self)
{
	return G_STRUCT_MEMBER_P (self, QliteUpsertBuilder_private_offset);
}

static gpointer
_qlite_table_ref0 (gpointer self)
{
	return self ? qlite_table_ref (self) : NULL;
}

QliteUpsertBuilder*
qlite_upsert_builder_construct (GType object_type,
                                QliteDatabase* db,
                                QliteTable* table)
{
	QliteUpsertBuilder* self = NULL;
	QliteTable* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	g_return_val_if_fail (db != NULL, NULL);
	g_return_val_if_fail (table != NULL, NULL);
	self = (QliteUpsertBuilder*) qlite_statement_builder_construct (object_type, db);
	_tmp0_ = _qlite_table_ref0 (table);
	_qlite_table_unref0 (self->priv->table);
	self->priv->table = _tmp0_;
	_tmp1_ = qlite_table_get_name (table);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strdup (_tmp2_);
	_g_free0 (self->priv->table_name);
	self->priv->table_name = _tmp3_;
	return self;
}

QliteUpsertBuilder*
qlite_upsert_builder_new (QliteDatabase* db,
                          QliteTable* table)
{
	return qlite_upsert_builder_construct (QLITE_TYPE_UPSERT_BUILDER, db, table);
}

static void
_vala_array_add16 (QliteStatementBuilderAbstractField** * array,
                   gint* length,
                   gint* size,
                   QliteStatementBuilderAbstractField* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (QliteStatementBuilderAbstractField*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add17 (QliteStatementBuilderAbstractField** * array,
                   gint* length,
                   gint* size,
                   QliteStatementBuilderAbstractField* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (QliteStatementBuilderAbstractField*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static gpointer
_qlite_statement_builder_ref0 (gpointer self)
{
	return self ? qlite_statement_builder_ref (self) : NULL;
}

QliteUpsertBuilder*
qlite_upsert_builder_value (QliteUpsertBuilder* self,
                            GType t_type,
                            GBoxedCopyFunc t_dup_func,
                            GDestroyNotify t_destroy_func,
                            QliteColumn* column,
                            gconstpointer value,
                            gboolean key)
{
	QliteUpsertBuilder* _tmp2_;
	QliteUpsertBuilder* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (column != NULL, NULL);
	if (key) {
		QliteStatementBuilderField* _tmp0_;
		_tmp0_ = qlite_statement_builder_field_new (t_type, (GBoxedCopyFunc) t_dup_func, (GDestroyNotify) t_destroy_func, column, value);
		_vala_array_add16 (&self->priv->keys, &self->priv->keys_length1, &self->priv->_keys_size_, (QliteStatementBuilderAbstractField*) _tmp0_);
	} else {
		QliteStatementBuilderField* _tmp1_;
		_tmp1_ = qlite_statement_builder_field_new (t_type, (GBoxedCopyFunc) t_dup_func, (GDestroyNotify) t_destroy_func, column, value);
		_vala_array_add17 (&self->priv->fields, &self->priv->fields_length1, &self->priv->_fields_size_, (QliteStatementBuilderAbstractField*) _tmp1_);
	}
	_tmp2_ = _qlite_statement_builder_ref0 (self);
	result = _tmp2_;
	return result;
}

static void
_vala_array_add18 (QliteStatementBuilderAbstractField** * array,
                   gint* length,
                   gint* size,
                   QliteStatementBuilderAbstractField* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (QliteStatementBuilderAbstractField*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add19 (QliteStatementBuilderAbstractField** * array,
                   gint* length,
                   gint* size,
                   QliteStatementBuilderAbstractField* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (QliteStatementBuilderAbstractField*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

QliteUpsertBuilder*
qlite_upsert_builder_value_null (QliteUpsertBuilder* self,
                                 GType t_type,
                                 GBoxedCopyFunc t_dup_func,
                                 GDestroyNotify t_destroy_func,
                                 QliteColumn* column,
                                 gboolean key)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	QliteUpsertBuilder* _tmp6_;
	QliteUpsertBuilder* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (column != NULL, NULL);
	_tmp0_ = qlite_column_get_not_null (column);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		_tmp2_ = qlite_column_get_name (column);
		_tmp3_ = _tmp2_;
		g_error ("upsert_builder.vala:30: Can't set non-null column %s to null", _tmp3_);
	}
	if (key) {
		QliteStatementBuilderNullField* _tmp4_;
		_tmp4_ = qlite_statement_builder_null_field_new (t_type, (GBoxedCopyFunc) t_dup_func, (GDestroyNotify) t_destroy_func, column);
		_vala_array_add18 (&self->priv->keys, &self->priv->keys_length1, &self->priv->_keys_size_, (QliteStatementBuilderAbstractField*) _tmp4_);
	} else {
		QliteStatementBuilderNullField* _tmp5_;
		_tmp5_ = qlite_statement_builder_null_field_new (t_type, (GBoxedCopyFunc) t_dup_func, (GDestroyNotify) t_destroy_func, column);
		_vala_array_add19 (&self->priv->fields, &self->priv->fields_length1, &self->priv->_fields_size_, (QliteStatementBuilderAbstractField*) _tmp5_);
	}
	_tmp6_ = _qlite_statement_builder_ref0 (self);
	result = _tmp6_;
	return result;
}

static sqlite3_stmt*
qlite_upsert_builder_real_prepare (QliteStatementBuilder* base)
{
	QliteUpsertBuilder * self;
	self = (QliteUpsertBuilder*) base;
	g_error ("upsert_builder.vala:40: prepare() not available for upsert.");
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

sqlite3_stmt*
qlite_upsert_builder_prepare_upsert (QliteUpsertBuilder* self)
{
	GString* unique_fields = NULL;
	GString* _tmp0_;
	GString* unique_values = NULL;
	GString* _tmp1_;
	GString* update_fields = NULL;
	GString* _tmp2_;
	GString* update_values = NULL;
	GString* _tmp3_;
	GString* update_fields_vals = NULL;
	GString* _tmp4_;
	gchar* sql = NULL;
	const gchar* _tmp43_;
	const gchar* _tmp44_;
	GString* _tmp45_;
	const gchar* _tmp46_;
	const gchar* _tmp47_;
	GString* _tmp48_;
	const gchar* _tmp49_;
	const gchar* _tmp50_;
	GString* _tmp51_;
	const gchar* _tmp52_;
	const gchar* _tmp53_;
	GString* _tmp54_;
	const gchar* _tmp55_;
	const gchar* _tmp56_;
	gchar* _tmp57_;
	gchar* _tmp58_;
	GString* _tmp59_;
	const gchar* _tmp60_;
	const gchar* _tmp61_;
	GString* _tmp62_;
	const gchar* _tmp63_;
	const gchar* _tmp64_;
	gchar* _tmp65_;
	gchar* _tmp66_;
	gchar* _tmp67_;
	gchar* _tmp68_;
	sqlite3_stmt* stmt = NULL;
	QliteDatabase* _tmp69_;
	const gchar* _tmp70_;
	sqlite3_stmt* _tmp71_;
	sqlite3_stmt* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_string_new ("");
	unique_fields = _tmp0_;
	_tmp1_ = g_string_new ("");
	unique_values = _tmp1_;
	_tmp2_ = g_string_new ("");
	update_fields = _tmp2_;
	_tmp3_ = g_string_new ("");
	update_values = _tmp3_;
	_tmp4_ = g_string_new ("");
	update_fields_vals = _tmp4_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp5_ = FALSE;
			_tmp5_ = TRUE;
			while (TRUE) {
				QliteStatementBuilderAbstractField** _tmp7_;
				gint _tmp7__length1;
				GString* _tmp10_;
				QliteStatementBuilderAbstractField** _tmp11_;
				gint _tmp11__length1;
				QliteStatementBuilderAbstractField* _tmp12_;
				QliteColumn* _tmp13_;
				const gchar* _tmp14_;
				const gchar* _tmp15_;
				GString* _tmp16_;
				if (!_tmp5_) {
					gint _tmp6_;
					_tmp6_ = i;
					i = _tmp6_ + 1;
				}
				_tmp5_ = FALSE;
				_tmp7_ = self->priv->keys;
				_tmp7__length1 = self->priv->keys_length1;
				if (!(i < _tmp7__length1)) {
					break;
				}
				if (i != 0) {
					GString* _tmp8_;
					GString* _tmp9_;
					_tmp8_ = unique_fields;
					g_string_append (_tmp8_, ", ");
					_tmp9_ = unique_values;
					g_string_append (_tmp9_, ", ");
				}
				_tmp10_ = unique_fields;
				_tmp11_ = self->priv->keys;
				_tmp11__length1 = self->priv->keys_length1;
				_tmp12_ = _tmp11_[i];
				_tmp13_ = _tmp12_->column;
				_tmp14_ = qlite_column_get_name (_tmp13_);
				_tmp15_ = _tmp14_;
				g_string_append (_tmp10_, _tmp15_);
				_tmp16_ = unique_values;
				g_string_append (_tmp16_, "?");
			}
		}
	}
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp17_ = FALSE;
			_tmp17_ = TRUE;
			while (TRUE) {
				QliteStatementBuilderAbstractField** _tmp19_;
				gint _tmp19__length1;
				GString* _tmp23_;
				QliteStatementBuilderAbstractField** _tmp24_;
				gint _tmp24__length1;
				QliteStatementBuilderAbstractField* _tmp25_;
				QliteColumn* _tmp26_;
				const gchar* _tmp27_;
				const gchar* _tmp28_;
				GString* _tmp29_;
				GString* _tmp30_;
				QliteStatementBuilderAbstractField** _tmp31_;
				gint _tmp31__length1;
				QliteStatementBuilderAbstractField* _tmp32_;
				QliteColumn* _tmp33_;
				const gchar* _tmp34_;
				const gchar* _tmp35_;
				GString* _tmp36_;
				GString* _tmp37_;
				QliteStatementBuilderAbstractField** _tmp38_;
				gint _tmp38__length1;
				QliteStatementBuilderAbstractField* _tmp39_;
				QliteColumn* _tmp40_;
				const gchar* _tmp41_;
				const gchar* _tmp42_;
				if (!_tmp17_) {
					gint _tmp18_;
					_tmp18_ = i;
					i = _tmp18_ + 1;
				}
				_tmp17_ = FALSE;
				_tmp19_ = self->priv->fields;
				_tmp19__length1 = self->priv->fields_length1;
				if (!(i < _tmp19__length1)) {
					break;
				}
				if (i != 0) {
					GString* _tmp20_;
					GString* _tmp21_;
					GString* _tmp22_;
					_tmp20_ = update_fields;
					g_string_append (_tmp20_, ", ");
					_tmp21_ = update_values;
					g_string_append (_tmp21_, ", ");
					_tmp22_ = update_fields_vals;
					g_string_append (_tmp22_, ", ");
				}
				_tmp23_ = update_fields;
				_tmp24_ = self->priv->fields;
				_tmp24__length1 = self->priv->fields_length1;
				_tmp25_ = _tmp24_[i];
				_tmp26_ = _tmp25_->column;
				_tmp27_ = qlite_column_get_name (_tmp26_);
				_tmp28_ = _tmp27_;
				g_string_append (_tmp23_, _tmp28_);
				_tmp29_ = update_values;
				g_string_append (_tmp29_, "?");
				_tmp30_ = update_fields_vals;
				_tmp31_ = self->priv->fields;
				_tmp31__length1 = self->priv->fields_length1;
				_tmp32_ = _tmp31_[i];
				_tmp33_ = _tmp32_->column;
				_tmp34_ = qlite_column_get_name (_tmp33_);
				_tmp35_ = _tmp34_;
				_tmp36_ = g_string_append (_tmp30_, _tmp35_);
				_tmp37_ = g_string_append (_tmp36_, "=excluded.");
				_tmp38_ = self->priv->fields;
				_tmp38__length1 = self->priv->fields_length1;
				_tmp39_ = _tmp38_[i];
				_tmp40_ = _tmp39_->column;
				_tmp41_ = qlite_column_get_name (_tmp40_);
				_tmp42_ = _tmp41_;
				g_string_append (_tmp37_, _tmp42_);
			}
		}
	}
	_tmp43_ = self->priv->table_name;
	_tmp44_ = string_to_string (_tmp43_);
	_tmp45_ = unique_fields;
	_tmp46_ = _tmp45_->str;
	_tmp47_ = string_to_string (_tmp46_);
	_tmp48_ = update_fields;
	_tmp49_ = _tmp48_->str;
	_tmp50_ = string_to_string (_tmp49_);
	_tmp51_ = unique_values;
	_tmp52_ = _tmp51_->str;
	_tmp53_ = string_to_string (_tmp52_);
	_tmp54_ = update_values;
	_tmp55_ = _tmp54_->str;
	_tmp56_ = string_to_string (_tmp55_);
	_tmp57_ = g_strconcat ("INSERT INTO ", _tmp44_, " (", _tmp47_, ", ", _tmp50_, ") VALUES (", _tmp53_, ", ", _tmp56_, ") ", NULL);
	_tmp58_ = _tmp57_;
	_tmp59_ = unique_fields;
	_tmp60_ = _tmp59_->str;
	_tmp61_ = string_to_string (_tmp60_);
	_tmp62_ = update_fields_vals;
	_tmp63_ = _tmp62_->str;
	_tmp64_ = string_to_string (_tmp63_);
	_tmp65_ = g_strconcat ("ON CONFLICT (", _tmp61_, ") DO UPDATE SET ", _tmp64_, NULL);
	_tmp66_ = _tmp65_;
	_tmp67_ = g_strconcat (_tmp58_, _tmp66_, NULL);
	_tmp68_ = _tmp67_;
	_g_free0 (_tmp66_);
	_g_free0 (_tmp58_);
	sql = _tmp68_;
	_tmp69_ = ((QliteStatementBuilder*) self)->db;
	_tmp70_ = sql;
	_tmp71_ = qlite_database_prepare (_tmp69_, _tmp70_);
	stmt = _tmp71_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp72_ = FALSE;
			_tmp72_ = TRUE;
			while (TRUE) {
				QliteStatementBuilderAbstractField** _tmp74_;
				gint _tmp74__length1;
				QliteStatementBuilderAbstractField** _tmp75_;
				gint _tmp75__length1;
				QliteStatementBuilderAbstractField* _tmp76_;
				sqlite3_stmt* _tmp77_;
				if (!_tmp72_) {
					gint _tmp73_;
					_tmp73_ = i;
					i = _tmp73_ + 1;
				}
				_tmp72_ = FALSE;
				_tmp74_ = self->priv->keys;
				_tmp74__length1 = self->priv->keys_length1;
				if (!(i < _tmp74__length1)) {
					break;
				}
				_tmp75_ = self->priv->keys;
				_tmp75__length1 = self->priv->keys_length1;
				_tmp76_ = _tmp75_[i];
				_tmp77_ = stmt;
				qlite_statement_builder_abstract_field_bind (_tmp76_, _tmp77_, i + 1);
			}
		}
	}
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp78_ = FALSE;
			_tmp78_ = TRUE;
			while (TRUE) {
				QliteStatementBuilderAbstractField** _tmp80_;
				gint _tmp80__length1;
				QliteStatementBuilderAbstractField** _tmp81_;
				gint _tmp81__length1;
				QliteStatementBuilderAbstractField* _tmp82_;
				sqlite3_stmt* _tmp83_;
				QliteStatementBuilderAbstractField** _tmp84_;
				gint _tmp84__length1;
				if (!_tmp78_) {
					gint _tmp79_;
					_tmp79_ = i;
					i = _tmp79_ + 1;
				}
				_tmp78_ = FALSE;
				_tmp80_ = self->priv->fields;
				_tmp80__length1 = self->priv->fields_length1;
				if (!(i < _tmp80__length1)) {
					break;
				}
				_tmp81_ = self->priv->fields;
				_tmp81__length1 = self->priv->fields_length1;
				_tmp82_ = _tmp81_[i];
				_tmp83_ = stmt;
				_tmp84_ = self->priv->keys;
				_tmp84__length1 = self->priv->keys_length1;
				qlite_statement_builder_abstract_field_bind (_tmp82_, _tmp83_, (i + _tmp84__length1) + 1);
			}
		}
	}
	result = stmt;
	_g_free0 (sql);
	_g_string_free0 (update_fields_vals);
	_g_string_free0 (update_values);
	_g_string_free0 (update_fields);
	_g_string_free0 (unique_values);
	_g_string_free0 (unique_fields);
	return result;
}

gint64
qlite_upsert_builder_perform (QliteUpsertBuilder* self)
{
	sqlite3_stmt* _tmp0_;
	sqlite3_stmt* _tmp1_;
	gboolean _tmp2_;
	QliteDatabase* _tmp7_;
	gint64 result;
	g_return_val_if_fail (self != NULL, -1LL);
	_tmp0_ = qlite_upsert_builder_prepare_upsert (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = sqlite3_step (_tmp1_) != SQLITE_DONE;
	_sqlite3_finalize0 (_tmp1_);
	if (_tmp2_) {
		QliteDatabase* _tmp3_;
		QliteDatabase* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		_tmp3_ = ((QliteStatementBuilder*) self)->db;
		_tmp4_ = ((QliteStatementBuilder*) self)->db;
		_tmp5_ = qlite_database_errmsg (_tmp4_);
		_tmp6_ = _tmp5_;
		g_critical ("upsert_builder.vala:86: SQLite error: %d - %s", qlite_database_errcode (_tmp3_), _tmp6_);
		_g_free0 (_tmp6_);
	}
	_tmp7_ = ((QliteStatementBuilder*) self)->db;
	result = qlite_database_last_insert_rowid (_tmp7_);
	return result;
}

static void
qlite_upsert_builder_class_init (QliteUpsertBuilderClass * klass,
                                 gpointer klass_data)
{
	qlite_upsert_builder_parent_class = g_type_class_peek_parent (klass);
	((QliteStatementBuilderClass *) klass)->finalize = qlite_upsert_builder_finalize;
	g_type_class_adjust_private_offset (klass, &QliteUpsertBuilder_private_offset);
	((QliteStatementBuilderClass *) klass)->prepare = (sqlite3_stmt* (*) (QliteStatementBuilder*)) qlite_upsert_builder_real_prepare;
}

static void
qlite_upsert_builder_instance_init (QliteUpsertBuilder * self,
                                    gpointer klass)
{
	QliteStatementBuilderAbstractField** _tmp0_;
	QliteStatementBuilderAbstractField** _tmp1_;
	self->priv = qlite_upsert_builder_get_instance_private (self);
	_tmp0_ = g_new0 (QliteStatementBuilderAbstractField*, 0 + 1);
	self->priv->keys = _tmp0_;
	self->priv->keys_length1 = 0;
	self->priv->_keys_size_ = self->priv->keys_length1;
	_tmp1_ = g_new0 (QliteStatementBuilderAbstractField*, 0 + 1);
	self->priv->fields = _tmp1_;
	self->priv->fields_length1 = 0;
	self->priv->_fields_size_ = self->priv->fields_length1;
}

static void
qlite_upsert_builder_finalize (QliteStatementBuilder * obj)
{
	QliteUpsertBuilder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, QLITE_TYPE_UPSERT_BUILDER, QliteUpsertBuilder);
	_qlite_table_unref0 (self->priv->table);
	_g_free0 (self->priv->table_name);
	self->priv->keys = (_vala_array_free (self->priv->keys, self->priv->keys_length1, (GDestroyNotify) qlite_statement_builder_abstract_field_unref), NULL);
	self->priv->fields = (_vala_array_free (self->priv->fields, self->priv->fields_length1, (GDestroyNotify) qlite_statement_builder_abstract_field_unref), NULL);
	QLITE_STATEMENT_BUILDER_CLASS (qlite_upsert_builder_parent_class)->finalize (obj);
}

static GType
qlite_upsert_builder_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (QliteUpsertBuilderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) qlite_upsert_builder_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (QliteUpsertBuilder), 0, (GInstanceInitFunc) qlite_upsert_builder_instance_init, NULL };
	GType qlite_upsert_builder_type_id;
	qlite_upsert_builder_type_id = g_type_register_static (QLITE_TYPE_STATEMENT_BUILDER, "QliteUpsertBuilder", &g_define_type_info, 0);
	QliteUpsertBuilder_private_offset = g_type_add_instance_private (qlite_upsert_builder_type_id, sizeof (QliteUpsertBuilderPrivate));
	return qlite_upsert_builder_type_id;
}

GType
qlite_upsert_builder_get_type (void)
{
	static volatile gsize qlite_upsert_builder_type_id__once = 0;
	if (g_once_init_enter (&qlite_upsert_builder_type_id__once)) {
		GType qlite_upsert_builder_type_id;
		qlite_upsert_builder_type_id = qlite_upsert_builder_get_type_once ();
		g_once_init_leave (&qlite_upsert_builder_type_id__once, qlite_upsert_builder_type_id);
	}
	return qlite_upsert_builder_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

