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


#include <glib.h>
#include <glib-object.h>
#include "SQLHeavy.h"
#include <stdlib.h>
#include <string.h>
#include <config.h>

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _SQLHeavyProfilingDatabasePrivate {
	SQLHeavyQuery* query;
	GStaticRecMutex __lock_query;
};


static gpointer sql_heavy_profiling_database_parent_class = NULL;

#define SQL_HEAVY_PROFILING_DATABASE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SQL_HEAVY_TYPE_PROFILING_DATABASE, SQLHeavyProfilingDatabasePrivate))
enum  {
	SQL_HEAVY_PROFILING_DATABASE_DUMMY_PROPERTY
};
void sql_heavy_profiling_database_insert (SQLHeavyProfilingDatabase* self, SQLHeavyQueryResult* query_result);
static GObject * sql_heavy_profiling_database_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void sql_heavy_profiling_database_finalize (GObject* obj);


void sql_heavy_profiling_database_insert (SQLHeavyProfilingDatabase* self, SQLHeavyQueryResult* query_result) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (query_result != NULL);
	{
		SQLHeavyQuery* _tmp0_;
		_tmp0_ = self->priv->query;
		g_static_rec_mutex_lock (&self->priv->__lock_query);
		{
			{
				SQLHeavyQuery* _tmp1_;
				SQLHeavyQueryResult* _tmp2_;
				SQLHeavyQuery* _tmp3_;
				SQLHeavyQuery* _tmp4_;
				const gchar* _tmp5_;
				const gchar* _tmp6_;
				SQLHeavyQuery* _tmp8_;
				SQLHeavyQueryResult* _tmp9_;
				gdouble _tmp10_;
				gdouble _tmp11_;
				SQLHeavyQuery* _tmp13_;
				SQLHeavyQueryResult* _tmp14_;
				gint _tmp15_;
				gint _tmp16_;
				SQLHeavyQuery* _tmp18_;
				SQLHeavyQueryResult* _tmp19_;
				gint _tmp20_;
				gint _tmp21_;
				SQLHeavyQuery* _tmp23_;
				SQLHeavyQueryResult* _tmp24_ = NULL;
				SQLHeavyQueryResult* _tmp25_;
				_tmp1_ = self->priv->query;
				_tmp2_ = query_result;
				_tmp3_ = sql_heavy_query_result_get_query (_tmp2_);
				_tmp4_ = _tmp3_;
				_tmp5_ = sql_heavy_query_get_sql (_tmp4_);
				_tmp6_ = _tmp5_;
				sql_heavy_query_set_string (_tmp1_, ":sql", _tmp6_, &_inner_error_);
				if (_inner_error_ != NULL) {
					if (_inner_error_->domain == SQL_HEAVY_ERROR) {
						goto __catch18_sql_heavy_error;
					}
					{
						SQLHeavyQuery* _tmp7_;
						_tmp7_ = self->priv->query;
						g_static_rec_mutex_unlock (&self->priv->__lock_query);
					}
					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;
				}
				_tmp8_ = self->priv->query;
				_tmp9_ = query_result;
				_tmp10_ = sql_heavy_query_result_get_execution_time (_tmp9_);
				_tmp11_ = _tmp10_;
				sql_heavy_query_set_double (_tmp8_, ":clock", _tmp11_, &_inner_error_);
				if (_inner_error_ != NULL) {
					if (_inner_error_->domain == SQL_HEAVY_ERROR) {
						goto __catch18_sql_heavy_error;
					}
					{
						SQLHeavyQuery* _tmp12_;
						_tmp12_ = self->priv->query;
						g_static_rec_mutex_unlock (&self->priv->__lock_query);
					}
					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;
				}
				_tmp13_ = self->priv->query;
				_tmp14_ = query_result;
				_tmp15_ = sql_heavy_query_result_get_full_scan_steps (_tmp14_);
				_tmp16_ = _tmp15_;
				sql_heavy_query_set_int64 (_tmp13_, ":fullscan_step", (gint64) _tmp16_, &_inner_error_);
				if (_inner_error_ != NULL) {
					if (_inner_error_->domain == SQL_HEAVY_ERROR) {
						goto __catch18_sql_heavy_error;
					}
					{
						SQLHeavyQuery* _tmp17_;
						_tmp17_ = self->priv->query;
						g_static_rec_mutex_unlock (&self->priv->__lock_query);
					}
					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;
				}
				_tmp18_ = self->priv->query;
				_tmp19_ = query_result;
				_tmp20_ = sql_heavy_query_result_get_sort_operations (_tmp19_);
				_tmp21_ = _tmp20_;
				sql_heavy_query_set_int64 (_tmp18_, ":sort", (gint64) _tmp21_, &_inner_error_);
				if (_inner_error_ != NULL) {
					if (_inner_error_->domain == SQL_HEAVY_ERROR) {
						goto __catch18_sql_heavy_error;
					}
					{
						SQLHeavyQuery* _tmp22_;
						_tmp22_ = self->priv->query;
						g_static_rec_mutex_unlock (&self->priv->__lock_query);
					}
					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;
				}
				_tmp23_ = self->priv->query;
				_tmp24_ = sql_heavy_query_execute (_tmp23_, NULL, &_inner_error_, NULL);
				_tmp25_ = _tmp24_;
				_g_object_unref0 (_tmp25_);
				if (_inner_error_ != NULL) {
					if (_inner_error_->domain == SQL_HEAVY_ERROR) {
						goto __catch18_sql_heavy_error;
					}
					{
						SQLHeavyQuery* _tmp26_;
						_tmp26_ = self->priv->query;
						g_static_rec_mutex_unlock (&self->priv->__lock_query);
					}
					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;
				}
			}
			goto __finally18;
			__catch18_sql_heavy_error:
			{
				GError* e = NULL;
				GError* _tmp27_;
				const gchar* _tmp28_;
				e = _inner_error_;
				_inner_error_ = NULL;
				_tmp27_ = e;
				_tmp28_ = _tmp27_->message;
				g_warning ("sqlheavy-profiling-database.vala:29: Unable to insert entry into profi" \
"ling database: %s", _tmp28_);
				_g_error_free0 (e);
			}
			__finally18:
			if (_inner_error_ != NULL) {
				{
					SQLHeavyQuery* _tmp29_;
					_tmp29_ = self->priv->query;
					g_static_rec_mutex_unlock (&self->priv->__lock_query);
				}
				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;
			}
		}
		__finally17:
		{
			SQLHeavyQuery* _tmp30_;
			_tmp30_ = self->priv->query;
			g_static_rec_mutex_unlock (&self->priv->__lock_query);
		}
		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;
		}
	}
}


/**
     * Create a new ProfilingDatabase
     *
     * @param filename the location of the database
     */
SQLHeavyProfilingDatabase* sql_heavy_profiling_database_construct (GType object_type, const gchar* filename, GError** error) {
	SQLHeavyProfilingDatabase * self = NULL;
	gchar* _tmp0_ = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_;
	gchar* schema;
	const gchar* _tmp4_;
	GError * _inner_error_ = NULL;
	_tmp0_ = sql_heavy_version_api ();
	_tmp1_ = _tmp0_;
	_tmp2_ = g_build_filename (SQL_HEAVY_CONFIG_PATH_PACKAGE_DATA, _tmp1_, "schemas", "profiling", NULL);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	schema = _tmp3_;
	_tmp4_ = filename;
	self = (SQLHeavyProfilingDatabase*) g_object_new (object_type, "filename", _tmp4_, "schema", schema, NULL);
	sql_heavy_database_init ((SQLHeavyDatabase*) self, NULL, &_inner_error_);
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == SQL_HEAVY_ERROR) {
			g_propagate_error (error, _inner_error_);
			_g_free0 (schema);
			_g_object_unref0 (self);
			return NULL;
		} else {
			_g_free0 (schema);
			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 NULL;
		}
	}
	_g_free0 (schema);
	return self;
}


SQLHeavyProfilingDatabase* sql_heavy_profiling_database_new (const gchar* filename, GError** error) {
	return sql_heavy_profiling_database_construct (SQL_HEAVY_TYPE_PROFILING_DATABASE, filename, error);
}


static GObject * sql_heavy_profiling_database_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	SQLHeavyProfilingDatabase * self;
	GError * _inner_error_ = NULL;
	parent_class = G_OBJECT_CLASS (sql_heavy_profiling_database_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = SQL_HEAVY_PROFILING_DATABASE (obj);
	sql_heavy_database_set_synchronous ((SQLHeavyDatabase*) self, SQL_HEAVY_SYNCHRONOUS_MODE_OFF);
	sql_heavy_database_set_journal_mode ((SQLHeavyDatabase*) self, SQL_HEAVY_JOURNAL_MODE_OFF);
	{
		SQLHeavyQuery* _tmp0_;
		SQLHeavyQuery* _tmp1_;
		SQLHeavyQuery* _tmp2_;
		_tmp0_ = sql_heavy_query_new ((SQLHeavyQueryable*) self, "INSERT INTO `queries` (`sql`, `clock`, `fullscan_step`, `sort`) VALUES" \
" (:sql, :clock, :fullscan_step, :sort);", &_inner_error_);
		_tmp1_ = _tmp0_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch19_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_);
		}
		_g_object_unref0 (self->priv->query);
		self->priv->query = _tmp1_;
		_tmp2_ = self->priv->query;
		sql_heavy_query_set_auto_clear (_tmp2_, TRUE);
	}
	goto __finally19;
	__catch19_sql_heavy_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		GError* _tmp5_;
		gint _tmp6_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->code;
		g_warning ("sqlheavy-profiling-database.vala:43: Unable to insert profiling inform" \
"ation: %s (%d)", _tmp4_, _tmp6_);
		_g_error_free0 (e);
	}
	__finally19:
	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 obj;
}


static void sql_heavy_profiling_database_class_init (SQLHeavyProfilingDatabaseClass * klass) {
	sql_heavy_profiling_database_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SQLHeavyProfilingDatabasePrivate));
	G_OBJECT_CLASS (klass)->constructor = sql_heavy_profiling_database_constructor;
	G_OBJECT_CLASS (klass)->finalize = sql_heavy_profiling_database_finalize;
}


static void sql_heavy_profiling_database_instance_init (SQLHeavyProfilingDatabase * self) {
	self->priv = SQL_HEAVY_PROFILING_DATABASE_GET_PRIVATE (self);
	g_static_rec_mutex_init (&self->priv->__lock_query);
}


static void sql_heavy_profiling_database_finalize (GObject* obj) {
	SQLHeavyProfilingDatabase * self;
	self = SQL_HEAVY_PROFILING_DATABASE (obj);
	g_static_rec_mutex_free (&self->priv->__lock_query);
	_g_object_unref0 (self->priv->query);
	G_OBJECT_CLASS (sql_heavy_profiling_database_parent_class)->finalize (obj);
}


/**
   * Database used to hold profiling information.
   *
   * Note that this database will have {@link Database.synchronous}
   * property set to OFF. This provides a drastic performance increase
   * but means that sudden power loss could lead to a corrupt
   * profiling database.
   *
   * This database will also have {@link Database.journal_mode} set to
   * OFF. This will approximately halve the amount of time time spent
   * inserting profiling data, but the database will likely become
   * corrupt if the application crashes.
   *
   * @see Database.profiling_data
   */
GType sql_heavy_profiling_database_get_type (void) {
	static volatile gsize sql_heavy_profiling_database_type_id__volatile = 0;
	if (g_once_init_enter (&sql_heavy_profiling_database_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SQLHeavyProfilingDatabaseClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sql_heavy_profiling_database_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SQLHeavyProfilingDatabase), 0, (GInstanceInitFunc) sql_heavy_profiling_database_instance_init, NULL };
		GType sql_heavy_profiling_database_type_id;
		sql_heavy_profiling_database_type_id = g_type_register_static (SQL_HEAVY_TYPE_VERSIONED_DATABASE, "SQLHeavyProfilingDatabase", &g_define_type_info, 0);
		g_once_init_leave (&sql_heavy_profiling_database_type_id__volatile, sql_heavy_profiling_database_type_id);
	}
	return sql_heavy_profiling_database_type_id__volatile;
}



