/* sqlheavy-user-functions.c generated by valac 0.16.0, the Vala compiler
 * generated from sqlheavy-user-functions.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include "SQLHeavy.h"
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <float.h>
#include <math.h>
#include <gobject/gvaluecollector.h>


#define SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA (sql_heavy_user_function_user_func_data_get_type ())
#define SQL_HEAVY_USER_FUNCTION_USER_FUNC_DATA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA, SQLHeavyUserFunctionUserFuncData))
#define SQL_HEAVY_USER_FUNCTION_USER_FUNC_DATA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA, SQLHeavyUserFunctionUserFuncDataClass))
#define SQL_HEAVY_USER_FUNCTION_IS_USER_FUNC_DATA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA))
#define SQL_HEAVY_USER_FUNCTION_IS_USER_FUNC_DATA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA))
#define SQL_HEAVY_USER_FUNCTION_USER_FUNC_DATA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA, SQLHeavyUserFunctionUserFuncDataClass))

typedef struct _SQLHeavyUserFunctionUserFuncData SQLHeavyUserFunctionUserFuncData;
typedef struct _SQLHeavyUserFunctionUserFuncDataClass SQLHeavyUserFunctionUserFuncDataClass;
typedef struct _SQLHeavyUserFunctionUserFuncDataPrivate SQLHeavyUserFunctionUserFuncDataPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_value_array_free0(var) ((var == NULL) ? NULL : (var = (g_value_array_free (var), NULL)))
#define __vala_GValue_free0(var) ((var == NULL) ? NULL : (var = (_vala_GValue_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
typedef struct _SQLHeavyUserFunctionParamSpecContext SQLHeavyUserFunctionParamSpecContext;
#define _sql_heavy_user_function_context_unref0(var) ((var == NULL) ? NULL : (var = (sql_heavy_user_function_context_unref (var), NULL)))

struct _SQLHeavyUserFunctionUserFuncData {
	GObject parent_instance;
	SQLHeavyUserFunctionUserFuncDataPrivate * priv;
	SQLHeavyDatabase* db;
	gchar* name;
	gint argc;
	gboolean is_scalar;
	SQLHeavyUserFunctionUserFunc func;
	gpointer func_target;
	GDestroyNotify func_target_destroy_notify;
	SQLHeavyUserFunctionFinalizeFunc final;
	gpointer final_target;
	GDestroyNotify final_target_destroy_notify;
};

struct _SQLHeavyUserFunctionUserFuncDataClass {
	GObjectClass parent_class;
};

struct _SQLHeavyUserFunctionContextPrivate {
	sqlite3_context* ctx;
	SQLHeavyUserFunctionUserFuncData* user_func_data;
	GHashTable* _data;
};

struct _SQLHeavyUserFunctionParamSpecContext {
	GParamSpec parent_instance;
};


static gpointer sql_heavy_user_function_user_func_data_parent_class = NULL;
static gpointer sql_heavy_user_function_context_parent_class = NULL;

GType sql_heavy_user_function_user_func_data_get_type (void) G_GNUC_CONST;
enum  {
	SQL_HEAVY_USER_FUNCTION_USER_FUNC_DATA_DUMMY_PROPERTY
};
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_new_scalar (SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target);
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_construct_scalar (GType object_type, SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target);
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_new_aggregate (SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target, SQLHeavyUserFunctionFinalizeFunc final, void* final_target);
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_construct_aggregate (GType object_type, SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target, SQLHeavyUserFunctionFinalizeFunc final, void* final_target);
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_new (void);
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_construct (GType object_type);
static void sql_heavy_user_function_user_func_data_finalize (GObject* obj);
void g_hash_table_unref (GHashTable* ht);
GHashTable* g_hash_table_ref (GHashTable* ht);
void g_boxed_free (GType type, void* ptr);
void sql_heavy_user_function_g_boxed_value_free (void* value);
#define SQL_HEAVY_USER_FUNCTION_CONTEXT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT, SQLHeavyUserFunctionContextPrivate))
enum  {
	SQL_HEAVY_USER_FUNCTION_CONTEXT_DUMMY_PROPERTY
};
static GHashTable* sql_heavy_user_function_context_get_data (SQLHeavyUserFunctionContext* self);
static GValue* _g_value_dup (GValue* self);
void sql_heavy_user_function_context_handle_result (SQLHeavyUserFunctionContext* self, GValue* value);
static guint8* _vala_array_dup3 (guint8* self, int length);
void sql_heavy_user_function_context_call_user_func (SQLHeavyUserFunctionContext* self, sqlite3_value** args, int args_length1);
GValueArray* sql_heavy_sqlite_value_array_to_g_value_array (sqlite3_value** values, int values_length1);
static void _vala_GValue_free (GValue* self);
gint sql_heavy_sqlite_code_from_error (GError* e);
void sql_heavy_user_function_context_call_finalize_func (SQLHeavyUserFunctionContext* self);
SQLHeavyUserFunctionContext* sql_heavy_user_function_context_new (sqlite3_context* ctx);
SQLHeavyUserFunctionContext* sql_heavy_user_function_context_construct (GType object_type, sqlite3_context* ctx);
static void sql_heavy_user_function_context_finalize (SQLHeavyUserFunctionContext* obj);
void sql_heavy_user_function_on_user_function_called (sqlite3_context* context, int args_length1, sqlite3_value** args);
void sql_heavy_user_function_on_user_finalize_called (sqlite3_context* context);


/**
       * Create context data for a scalar function
       *
       * @param db database to create this function on
       * @param name function name
       * @param argc the number of arguments that this function accepts
       * @param func callback to execute when the function is called
       * @see Database.register_scalar_function
       */
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_construct_scalar (GType object_type, SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target) {
	SQLHeavyUserFunctionUserFuncData * self = NULL;
	SQLHeavyDatabase* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gint _tmp3_;
	SQLHeavyUserFunctionUserFunc _tmp4_;
	void* _tmp4__target;
	g_return_val_if_fail (db != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	self = (SQLHeavyUserFunctionUserFuncData*) g_object_new (object_type, NULL);
	_tmp0_ = db;
	self->db = _tmp0_;
	_tmp1_ = name;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (self->name);
	self->name = _tmp2_;
	_tmp3_ = argc;
	self->argc = _tmp3_;
	self->is_scalar = TRUE;
	_tmp4_ = func;
	_tmp4__target = func_target;
	(self->func_target_destroy_notify == NULL) ? NULL : (self->func_target_destroy_notify (self->func_target), NULL);
	self->func = NULL;
	self->func_target = NULL;
	self->func_target_destroy_notify = NULL;
	self->func = _tmp4_;
	self->func_target = _tmp4__target;
	self->func_target_destroy_notify = NULL;
	(self->final_target_destroy_notify == NULL) ? NULL : (self->final_target_destroy_notify (self->final_target), NULL);
	self->final = NULL;
	self->final_target = NULL;
	self->final_target_destroy_notify = NULL;
	self->final = NULL;
	self->final_target = NULL;
	self->final_target_destroy_notify = NULL;
	return self;
}


SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_new_scalar (SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target) {
	return sql_heavy_user_function_user_func_data_construct_scalar (SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA, db, name, argc, func, func_target);
}


/**
       * Create context data for an aggregate function
       *
       * @param db database to create this function on
       * @param name function name
       * @param argc the number of arguments that this function accepts
       * @param func callback to execute once for each piece of data the function is called on
       * @param final callback to execute after the func has been called for the last time
       * @see Database.register_aggregate_function
       */
SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_construct_aggregate (GType object_type, SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target, SQLHeavyUserFunctionFinalizeFunc final, void* final_target) {
	SQLHeavyUserFunctionUserFuncData * self = NULL;
	SQLHeavyDatabase* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gint _tmp3_;
	SQLHeavyUserFunctionUserFunc _tmp4_;
	void* _tmp4__target;
	SQLHeavyUserFunctionFinalizeFunc _tmp5_;
	void* _tmp5__target;
	g_return_val_if_fail (db != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	self = (SQLHeavyUserFunctionUserFuncData*) g_object_new (object_type, NULL);
	_tmp0_ = db;
	self->db = _tmp0_;
	_tmp1_ = name;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (self->name);
	self->name = _tmp2_;
	_tmp3_ = argc;
	self->argc = _tmp3_;
	self->is_scalar = FALSE;
	_tmp4_ = func;
	_tmp4__target = func_target;
	(self->func_target_destroy_notify == NULL) ? NULL : (self->func_target_destroy_notify (self->func_target), NULL);
	self->func = NULL;
	self->func_target = NULL;
	self->func_target_destroy_notify = NULL;
	self->func = _tmp4_;
	self->func_target = _tmp4__target;
	self->func_target_destroy_notify = NULL;
	_tmp5_ = final;
	_tmp5__target = final_target;
	(self->final_target_destroy_notify == NULL) ? NULL : (self->final_target_destroy_notify (self->final_target), NULL);
	self->final = NULL;
	self->final_target = NULL;
	self->final_target_destroy_notify = NULL;
	self->final = _tmp5_;
	self->final_target = _tmp5__target;
	self->final_target_destroy_notify = NULL;
	return self;
}


SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_new_aggregate (SQLHeavyDatabase* db, const gchar* name, gint argc, SQLHeavyUserFunctionUserFunc func, void* func_target, SQLHeavyUserFunctionFinalizeFunc final, void* final_target) {
	return sql_heavy_user_function_user_func_data_construct_aggregate (SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA, db, name, argc, func, func_target, final, final_target);
}


SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_construct (GType object_type) {
	SQLHeavyUserFunctionUserFuncData * self = NULL;
	self = (SQLHeavyUserFunctionUserFuncData*) g_object_new (object_type, NULL);
	return self;
}


SQLHeavyUserFunctionUserFuncData* sql_heavy_user_function_user_func_data_new (void) {
	return sql_heavy_user_function_user_func_data_construct (SQL_HEAVY_USER_FUNCTION_TYPE_USER_FUNC_DATA);
}


static void sql_heavy_user_function_user_func_data_class_init (SQLHeavyUserFunctionUserFuncDataClass * klass) {
	sql_heavy_user_function_user_func_data_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = sql_heavy_user_function_user_func_data_finalize;
}


static void sql_heavy_user_function_user_func_data_instance_init (SQLHeavyUserFunctionUserFuncData * self) {
}


static void sql_heavy_user_function_user_func_data_finalize (GObject* obj) {
	SQLHeavyUserFunctionUserFuncData * self;
	self = SQL_HEAVY_USER_FUNCTION_USER_FUNC_DATA (obj);
	_g_free0 (self->name);
	(self->func_target_destroy_notify == NULL) ? NULL : (self->func_target_destroy_notify (self->func_target), NULL);
	self->func = NULL;
	self->func_target = NULL;
	self->func_target_destroy_notify = NULL;
	(self->final_target_destroy_notify == NULL) ? NULL : (self->final_target_destroy_notify (self->final_target), NULL);
	self->final = NULL;
	self->final_target = NULL;
	self->final_target_destroy_notify = NULL;
	G_OBJECT_CLASS (sql_heavy_user_function_user_func_data_parent_class)->finalize (obj);
}


/**
     * Context data for a user function
     */
GType sql_heavy_user_function_user_func_data_get_type (void) {
	static volatile gsize sql_heavy_user_function_user_func_data_type_id__volatile = 0;
	if (g_once_init_enter (&sql_heavy_user_function_user_func_data_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SQLHeavyUserFunctionUserFuncDataClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sql_heavy_user_function_user_func_data_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SQLHeavyUserFunctionUserFuncData), 0, (GInstanceInitFunc) sql_heavy_user_function_user_func_data_instance_init, NULL };
		GType sql_heavy_user_function_user_func_data_type_id;
		sql_heavy_user_function_user_func_data_type_id = g_type_register_static (G_TYPE_OBJECT, "SQLHeavyUserFunctionUserFuncData", &g_define_type_info, 0);
		g_once_init_leave (&sql_heavy_user_function_user_func_data_type_id__volatile, sql_heavy_user_function_user_func_data_type_id);
	}
	return sql_heavy_user_function_user_func_data_type_id__volatile;
}


/**
     * Free a boxed GValue, or do nothing if called on a null
     *
     * @param value the GValue to free
     */
void sql_heavy_user_function_g_boxed_value_free (void* value) {
	void* _tmp0_;
	_tmp0_ = value;
	if (_tmp0_ != NULL) {
		void* _tmp1_;
		_tmp1_ = value;
		g_boxed_free (G_TYPE_VALUE, (void*) _tmp1_);
	}
}


/**
       * Set user data
       *
       * SQLHeavy user defined functions can store data in a hash
       * table.
       *
       * For scalar functions, this is uses the Function Auxillary Data
       * (see [[http://sqlite.org/c3ref/get_auxdata.html]])
       * feature of SQLite, meaning that, "If the same value is passed
       * to multiple invocations of the same SQL function during query
       * execution, under some circumstances the associated metadata
       * may be preserved."
       *
       * For aggregate functions, it uses the Aggregate Function Context
       * (see [[http://sqlite.org/c3ref/aggregate_context.html]])
       * feature of SQLite, allowing user data to be shared accross an
       * entire aggregate operation.
       *
       * @param key key of user data
       * @param value value of user data
       * @see get_user_data
       */
static GValue* _g_value_dup (GValue* self) {
	return g_boxed_copy (G_TYPE_VALUE, self);
}


static gpointer __g_value_dup0 (gpointer self) {
	return self ? _g_value_dup (self) : NULL;
}


void sql_heavy_user_function_context_set_user_data (SQLHeavyUserFunctionContext* self, const gchar* key, GValue* value) {
	GHashTable* _tmp0_;
	GHashTable* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	GValue _tmp4_;
	GValue _tmp5_;
	GValue* _tmp6_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (value != NULL);
	_tmp0_ = sql_heavy_user_function_context_get_data (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = key;
	_tmp3_ = g_strdup (_tmp2_);
	_tmp4_ = *value;
	_tmp5_ = _tmp4_;
	_tmp6_ = __g_value_dup0 (&_tmp5_);
	g_hash_table_replace (_tmp1_, _tmp3_, _tmp6_);
}


/**
       * Get user data
       *
       * @param key key of user data
       * @return value of user data
       * @see set_user_data
       */
GValue* sql_heavy_user_function_context_get_user_data (SQLHeavyUserFunctionContext* self, const gchar* key) {
	GValue* result = NULL;
	GHashTable* _tmp0_;
	GHashTable* _tmp1_;
	const gchar* _tmp2_;
	gconstpointer _tmp3_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (key != NULL, NULL);
	_tmp0_ = sql_heavy_user_function_context_get_data (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = key;
	_tmp3_ = g_hash_table_lookup (_tmp1_, _tmp2_);
	result = (GValue*) _tmp3_;
	return result;
}


/**
       * Take a GValue and place the data in the result of the SQLite context
       */
static guint8* _vala_array_dup3 (guint8* self, int length) {
	return g_memdup (self, length * sizeof (guint8));
}


void sql_heavy_user_function_context_handle_result (SQLHeavyUserFunctionContext* self, GValue* value) {
	GValue* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	if (_tmp0_ == NULL) {
		sqlite3_context* _tmp1_;
		_tmp1_ = self->priv->ctx;
		sqlite3_result_null (_tmp1_);
	} else {
		GValue* _tmp2_;
		gboolean _tmp3_ = FALSE;
		_tmp2_ = value;
		_tmp3_ = G_VALUE_HOLDS (_tmp2_, G_TYPE_INT);
		if (_tmp3_) {
			sqlite3_context* _tmp4_;
			GValue* _tmp5_;
			gint _tmp6_ = 0;
			_tmp4_ = self->priv->ctx;
			_tmp5_ = value;
			_tmp6_ = g_value_get_int (_tmp5_);
			sqlite3_result_int (_tmp4_, _tmp6_);
		} else {
			GValue* _tmp7_;
			gboolean _tmp8_ = FALSE;
			_tmp7_ = value;
			_tmp8_ = G_VALUE_HOLDS (_tmp7_, G_TYPE_INT64);
			if (_tmp8_) {
				sqlite3_context* _tmp9_;
				GValue* _tmp10_;
				gint64 _tmp11_ = 0LL;
				_tmp9_ = self->priv->ctx;
				_tmp10_ = value;
				_tmp11_ = g_value_get_int64 (_tmp10_);
				sqlite3_result_int64 (_tmp9_, _tmp11_);
			} else {
				GValue* _tmp12_;
				gboolean _tmp13_ = FALSE;
				_tmp12_ = value;
				_tmp13_ = G_VALUE_HOLDS (_tmp12_, G_TYPE_DOUBLE);
				if (_tmp13_) {
					sqlite3_context* _tmp14_;
					GValue* _tmp15_;
					gdouble _tmp16_ = 0.0;
					_tmp14_ = self->priv->ctx;
					_tmp15_ = value;
					_tmp16_ = g_value_get_double (_tmp15_);
					sqlite3_result_double (_tmp14_, _tmp16_);
				} else {
					GValue* _tmp17_;
					gboolean _tmp18_ = FALSE;
					_tmp17_ = value;
					_tmp18_ = G_VALUE_HOLDS (_tmp17_, G_TYPE_STRING);
					if (_tmp18_) {
						sqlite3_context* _tmp19_;
						GValue* _tmp20_;
						const gchar* _tmp21_ = NULL;
						gchar* _tmp22_;
						GDestroyNotify _tmp23_;
						_tmp19_ = self->priv->ctx;
						_tmp20_ = value;
						_tmp21_ = g_value_get_string (_tmp20_);
						_tmp22_ = g_strdup (_tmp21_);
						_tmp23_ = g_free;
						sqlite3_result_text (_tmp19_, _tmp22_, -1, _tmp23_);
					} else {
						GValue* _tmp24_;
						gboolean _tmp25_ = FALSE;
						_tmp24_ = value;
						_tmp25_ = G_VALUE_HOLDS (_tmp24_, G_TYPE_BOOLEAN);
						if (_tmp25_) {
							gint _tmp26_ = 0;
							GValue* _tmp27_;
							gboolean _tmp28_ = FALSE;
							sqlite3_context* _tmp29_;
							gint _tmp30_;
							_tmp27_ = value;
							_tmp28_ = g_value_get_boolean (_tmp27_);
							if (_tmp28_) {
								_tmp26_ = 1;
							} else {
								_tmp26_ = 0;
							}
							_tmp29_ = self->priv->ctx;
							_tmp30_ = _tmp26_;
							sqlite3_result_int (_tmp29_, _tmp30_);
						} else {
							GValue* _tmp31_;
							gboolean _tmp32_ = FALSE;
							_tmp31_ = value;
							_tmp32_ = G_VALUE_HOLDS (_tmp31_, G_TYPE_BYTE_ARRAY);
							if (_tmp32_) {
								sqlite3_context* _tmp33_;
								GValue* _tmp34_;
								void* _tmp35_ = NULL;
								guint8* _tmp36_;
								gint _tmp36__length1;
								guint8* _tmp37_;
								gint _tmp37__length1;
								GDestroyNotify _tmp38_;
								_tmp33_ = self->priv->ctx;
								_tmp34_ = value;
								_tmp35_ = g_value_get_boxed (_tmp34_);
								_tmp36_ = ((GByteArray*) _tmp35_)->data;
								_tmp36__length1 = (gint) ((GByteArray*) _tmp35_)->len;
								_tmp37_ = (_tmp36_ != NULL) ? _vala_array_dup3 (_tmp36_, _tmp36__length1) : ((gpointer) _tmp36_);
								_tmp37__length1 = _tmp36__length1;
								_tmp38_ = g_free;
								sqlite3_result_blob (_tmp33_, _tmp37_, _tmp37__length1, _tmp38_);
							} else {
								GValue* _tmp39_;
								const gchar* _tmp40_ = NULL;
								_tmp39_ = value;
								_tmp40_ = G_VALUE_TYPE_NAME (_tmp39_);
								g_critical ("sqlheavy-user-functions.vala:204: Unknown return type (%s).", _tmp40_);
							}
						}
					}
				}
			}
		}
	}
}


/**
       * Call the supplied user function callback
       */
static void _vala_GValue_free (GValue* self) {
	g_value_unset (self);
	g_free (self);
}


void sql_heavy_user_function_context_call_user_func (SQLHeavyUserFunctionContext* self, sqlite3_value** args, int args_length1) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	{
		SQLHeavyUserFunctionUserFuncData* _tmp0_;
		SQLHeavyUserFunctionUserFunc _tmp1_;
		void* _tmp1__target;
		sqlite3_value** _tmp2_;
		gint _tmp2__length1;
		GValueArray* _tmp3_ = NULL;
		GValueArray* _tmp4_;
		GValue* _tmp5_ = NULL;
		GValue* _tmp6_;
		GValue* res;
		_tmp0_ = self->priv->user_func_data;
		_tmp1_ = _tmp0_->func;
		_tmp1__target = _tmp0_->func_target;
		_tmp2_ = args;
		_tmp2__length1 = args_length1;
		_tmp3_ = sql_heavy_sqlite_value_array_to_g_value_array (_tmp2_, _tmp2__length1);
		_tmp4_ = _tmp3_;
		_tmp5_ = _tmp1_ (self, _tmp4_, _tmp1__target, &_inner_error_);
		_tmp6_ = _tmp5_;
		_g_value_array_free0 (_tmp4_);
		res = _tmp6_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch47_sql_heavy_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return;
		}
		sql_heavy_user_function_context_handle_result (self, res);
		__vala_GValue_free0 (res);
	}
	goto __finally47;
	__catch47_sql_heavy_error:
	{
		GError* e = NULL;
		sqlite3_context* _tmp7_;
		sqlite3_context* _tmp8_;
		sqlite3* _tmp9_ = NULL;
		const gchar* _tmp10_ = NULL;
		GError* _tmp11_;
		gint _tmp12_ = 0;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp7_ = self->priv->ctx;
		_tmp8_ = self->priv->ctx;
		_tmp9_ = sqlite3_context_db_handle (_tmp8_);
		_tmp10_ = sqlite3_errmsg (_tmp9_);
		_tmp11_ = e;
		_tmp12_ = sql_heavy_sqlite_code_from_error (_tmp11_);
		sqlite3_result_error (_tmp7_, _tmp10_, _tmp12_);
		_g_error_free0 (e);
	}
	__finally47:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
}


/**
       * Call the supplied user finalize function callback
       */
void sql_heavy_user_function_context_call_finalize_func (SQLHeavyUserFunctionContext* self) {
	SQLHeavyUserFunctionUserFuncData* _tmp0_;
	gboolean _tmp1_;
	GHashTable* _tmp4_;
	GHashTable* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->user_func_data;
	_tmp1_ = _tmp0_->is_scalar;
	if (!_tmp1_) {
		SQLHeavyUserFunctionUserFuncData* _tmp2_;
		SQLHeavyUserFunctionFinalizeFunc _tmp3_;
		void* _tmp3__target;
		_tmp2_ = self->priv->user_func_data;
		_tmp3_ = _tmp2_->final;
		_tmp3__target = _tmp2_->final_target;
		_tmp3_ (self, _tmp3__target);
	}
	_tmp4_ = sql_heavy_user_function_context_get_data (self);
	_tmp5_ = _tmp4_;
	g_hash_table_unref (_tmp5_);
}


SQLHeavyUserFunctionContext* sql_heavy_user_function_context_construct (GType object_type, sqlite3_context* ctx) {
	SQLHeavyUserFunctionContext* self = NULL;
	sqlite3_context* _tmp0_;
	sqlite3_context* _tmp1_;
	gconstpointer _tmp2_ = NULL;
	g_return_val_if_fail (ctx != NULL, NULL);
	self = (SQLHeavyUserFunctionContext*) g_type_create_instance (object_type);
	_tmp0_ = ctx;
	self->priv->ctx = _tmp0_;
	_tmp1_ = ctx;
	_tmp2_ = sqlite3_user_data (_tmp1_);
	self->priv->user_func_data = (SQLHeavyUserFunctionUserFuncData*) _tmp2_;
	return self;
}


SQLHeavyUserFunctionContext* sql_heavy_user_function_context_new (sqlite3_context* ctx) {
	return sql_heavy_user_function_context_construct (SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT, ctx);
}


static GHashTable* sql_heavy_user_function_context_get_data (SQLHeavyUserFunctionContext* self) {
	GHashTable* result;
	GHashTable* _tmp0_;
	GHashTable* _tmp23_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_data;
	if (_tmp0_ == NULL) {
		SQLHeavyUserFunctionUserFuncData* _tmp1_;
		gboolean _tmp2_;
		_tmp1_ = self->priv->user_func_data;
		_tmp2_ = _tmp1_->is_scalar;
		if (_tmp2_) {
			sqlite3_context* _tmp3_;
			gconstpointer _tmp4_ = NULL;
			GHashTable* _tmp5_;
			_tmp3_ = self->priv->ctx;
			_tmp4_ = sqlite3_get_auxdata (_tmp3_, 0);
			self->priv->_data = (GHashTable*) _tmp4_;
			_tmp5_ = self->priv->_data;
			if (_tmp5_ == NULL) {
				sqlite3_context* _tmp6_;
				GHashFunc _tmp7_;
				GEqualFunc _tmp8_;
				GDestroyNotify _tmp9_;
				GHashTable* _tmp10_;
				sqlite3_context* _tmp11_;
				gconstpointer _tmp12_ = NULL;
				_tmp6_ = self->priv->ctx;
				_tmp7_ = g_str_hash;
				_tmp8_ = g_str_equal;
				_tmp9_ = g_free;
				_tmp10_ = g_hash_table_new_full (_tmp7_, _tmp8_, _tmp9_, (GDestroyNotify) sql_heavy_user_function_g_boxed_value_free);
				sqlite3_set_auxdata (_tmp6_, 0, _tmp10_, g_hash_table_unref);
				_tmp11_ = self->priv->ctx;
				_tmp12_ = sqlite3_get_auxdata (_tmp11_, 0);
				self->priv->_data = (GHashTable*) _tmp12_;
			}
		} else {
			sqlite3_context* _tmp13_;
			void* _tmp14_ = NULL;
			GHashTable* _tmp15_;
			_tmp13_ = self->priv->ctx;
			_tmp14_ = sqlite3_aggregate_context (_tmp13_, (gint) sizeof (GHashTable*));
			memcpy (&self->priv->_data, _tmp14_, (gsize) sizeof (GHashTable*));
			_tmp15_ = self->priv->_data;
			if (_tmp15_ == NULL) {
				GHashFunc _tmp16_;
				GEqualFunc _tmp17_;
				GDestroyNotify _tmp18_;
				GDestroyNotify _tmp19_;
				GHashTable* _tmp20_;
				GHashTable* _tmp21_;
				GHashTable* _tmp22_ = NULL;
				_tmp16_ = g_str_hash;
				_tmp17_ = g_str_equal;
				_tmp18_ = g_free;
				_tmp19_ = g_object_unref;
				_tmp20_ = g_hash_table_new_full (_tmp16_, _tmp17_, _tmp18_, _tmp19_);
				_tmp21_ = _tmp20_;
				_tmp22_ = g_hash_table_ref (_tmp21_);
				self->priv->_data = _tmp22_;
				_g_hash_table_unref0 (_tmp21_);
			}
		}
	}
	_tmp23_ = self->priv->_data;
	result = (GHashTable*) _tmp23_;
	return result;
}


static void sql_heavy_user_function_value_context_init (GValue* value) {
	value->data[0].v_pointer = NULL;
}


static void sql_heavy_user_function_value_context_free_value (GValue* value) {
	if (value->data[0].v_pointer) {
		sql_heavy_user_function_context_unref (value->data[0].v_pointer);
	}
}


static void sql_heavy_user_function_value_context_copy_value (const GValue* src_value, GValue* dest_value) {
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = sql_heavy_user_function_context_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}


static gpointer sql_heavy_user_function_value_context_peek_pointer (const GValue* value) {
	return value->data[0].v_pointer;
}


static gchar* sql_heavy_user_function_value_context_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		SQLHeavyUserFunctionContext* object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = sql_heavy_user_function_context_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* sql_heavy_user_function_value_context_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	SQLHeavyUserFunctionContext** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = sql_heavy_user_function_context_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* sql_heavy_user_function_param_spec_context (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	SQLHeavyUserFunctionParamSpecContext* spec;
	g_return_val_if_fail (g_type_is_a (object_type, SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}


gpointer sql_heavy_user_function_value_get_context (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT), NULL);
	return value->data[0].v_pointer;
}


void sql_heavy_user_function_value_set_context (GValue* value, gpointer v_object) {
	SQLHeavyUserFunctionContext* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		sql_heavy_user_function_context_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		sql_heavy_user_function_context_unref (old);
	}
}


void sql_heavy_user_function_value_take_context (GValue* value, gpointer v_object) {
	SQLHeavyUserFunctionContext* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, SQL_HEAVY_USER_FUNCTION_TYPE_CONTEXT));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		sql_heavy_user_function_context_unref (old);
	}
}


static void sql_heavy_user_function_context_class_init (SQLHeavyUserFunctionContextClass * klass) {
	sql_heavy_user_function_context_parent_class = g_type_class_peek_parent (klass);
	SQL_HEAVY_USER_FUNCTION_CONTEXT_CLASS (klass)->finalize = sql_heavy_user_function_context_finalize;
	g_type_class_add_private (klass, sizeof (SQLHeavyUserFunctionContextPrivate));
}


static void sql_heavy_user_function_context_instance_init (SQLHeavyUserFunctionContext * self) {
	self->priv = SQL_HEAVY_USER_FUNCTION_CONTEXT_GET_PRIVATE (self);
	self->priv->ctx = NULL;
	self->priv->user_func_data = NULL;
	self->priv->_data = NULL;
	self->ref_count = 1;
}


static void sql_heavy_user_function_context_finalize (SQLHeavyUserFunctionContext* obj) {
	SQLHeavyUserFunctionContext * self;
	self = SQL_HEAVY_USER_FUNCTION_CONTEXT (obj);
}


/**
     * Context used to manage a call to a user defined function
     */
GType sql_heavy_user_function_context_get_type (void) {
	static volatile gsize sql_heavy_user_function_context_type_id__volatile = 0;
	if (g_once_init_enter (&sql_heavy_user_function_context_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { sql_heavy_user_function_value_context_init, sql_heavy_user_function_value_context_free_value, sql_heavy_user_function_value_context_copy_value, sql_heavy_user_function_value_context_peek_pointer, "p", sql_heavy_user_function_value_context_collect_value, "p", sql_heavy_user_function_value_context_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (SQLHeavyUserFunctionContextClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sql_heavy_user_function_context_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SQLHeavyUserFunctionContext), 0, (GInstanceInitFunc) sql_heavy_user_function_context_instance_init, &g_define_type_value_table };
		static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
		GType sql_heavy_user_function_context_type_id;
		sql_heavy_user_function_context_type_id = g_type_register_fundamental (g_type_fundamental_next (), "SQLHeavyUserFunctionContext", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&sql_heavy_user_function_context_type_id__volatile, sql_heavy_user_function_context_type_id);
	}
	return sql_heavy_user_function_context_type_id__volatile;
}


gpointer sql_heavy_user_function_context_ref (gpointer instance) {
	SQLHeavyUserFunctionContext* self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}


void sql_heavy_user_function_context_unref (gpointer instance) {
	SQLHeavyUserFunctionContext* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		SQL_HEAVY_USER_FUNCTION_CONTEXT_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}


/**
     * The function callback supplied to the SQLite context
     */
void sql_heavy_user_function_on_user_function_called (sqlite3_context* context, int args_length1, sqlite3_value** args) {
	sqlite3_context* _tmp0_;
	SQLHeavyUserFunctionContext* _tmp1_;
	SQLHeavyUserFunctionContext* ctx;
	sqlite3_value** _tmp2_;
	gint _tmp2__length1;
	g_return_if_fail (context != NULL);
	_tmp0_ = context;
	_tmp1_ = sql_heavy_user_function_context_new (_tmp0_);
	ctx = _tmp1_;
	_tmp2_ = args;
	_tmp2__length1 = args_length1;
	sql_heavy_user_function_context_call_user_func (ctx, _tmp2_, _tmp2__length1);
	_sql_heavy_user_function_context_unref0 (ctx);
}


/**
     * The finalize function callback supplied to the SQLite context
     */
void sql_heavy_user_function_on_user_finalize_called (sqlite3_context* context) {
	sqlite3_context* _tmp0_;
	SQLHeavyUserFunctionContext* _tmp1_;
	SQLHeavyUserFunctionContext* ctx;
	g_return_if_fail (context != NULL);
	_tmp0_ = context;
	_tmp1_ = sql_heavy_user_function_context_new (_tmp0_);
	ctx = _tmp1_;
	sql_heavy_user_function_context_call_finalize_func (ctx);
	_sql_heavy_user_function_context_unref0 (ctx);
}



