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


#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <SQLHeavy.h>
#include <stdlib.h>
#include <string.h>

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block1Data Block1Data;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _TestData TestData;
#define __vala_GValue_free0(var) ((var == NULL) ? NULL : (var = (_vala_GValue_free (var), NULL)))
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))

struct _Block1Data {
	int _ref_count_;
	GCancellable* cancellable;
	gpointer _async_data_;
};

struct _TestData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	SQLHeavyDatabase* db;
	Block1Data* _data1_;
	guint _tmp0_;
	guint id;
	SQLHeavyDatabase* _tmp1_;
	SQLHeavyQuery* _tmp2_;
	SQLHeavyQuery* stmt;
	GCancellable* _tmp3_;
	SQLHeavyQuery* _tmp4_;
	GCancellable* _tmp5_;
	SQLHeavyQueryResult* _tmp6_;
	SQLHeavyQueryResult* _tmp7_;
	GError* e;
	GError* _tmp8_;
	GError* _tmp9_;
	const gchar* _tmp10_;
	GError* _tmp11_;
	gint _tmp12_;
	guint _tmp13_;
	GMainLoop* _tmp14_;
	GError * _inner_error_;
};


extern GMainLoop* loop;
GMainLoop* loop = NULL;

static void test_data_free (gpointer _data);
void test (SQLHeavyDatabase* db, GAsyncReadyCallback _callback_, gpointer _user_data_);
void test_finish (GAsyncResult* _res_);
static gboolean test_co (TestData* _data_);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static gboolean __lambda2_ (Block1Data* _data1_);
static gboolean ___lambda2__gsource_func (gpointer self);
static void test_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
gint _vala_main (gchar** args, int args_length1);
static void _vala_GValue_free (GValue* self);


static void test_data_free (gpointer _data) {
	TestData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->db);
	g_slice_free (TestData, _data_);
}


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


void test (SQLHeavyDatabase* db, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	TestData* _data_;
	SQLHeavyDatabase* _tmp0_;
	SQLHeavyDatabase* _tmp1_;
	_data_ = g_slice_new0 (TestData);
	_data_->_async_result = g_simple_async_result_new (g_object_newv (G_TYPE_OBJECT, 0, NULL), _callback_, _user_data_, test);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, test_data_free);
	_tmp0_ = db;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_data_->db = _tmp1_;
	test_co (_data_);
}


void test_finish (GAsyncResult* _res_) {
	TestData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


/**
 * Some basic testing for the asnychronous functions.
 */
static Block1Data* block1_data_ref (Block1Data* _data1_) {
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}


static void block1_data_unref (void * _userdata_) {
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		_g_object_unref0 (_data1_->cancellable);
		g_slice_free (Block1Data, _data1_);
	}
}


static gboolean __lambda2_ (Block1Data* _data1_) {
	gboolean result = FALSE;
	GCancellable* _tmp0_;
	_tmp0_ = _data1_->cancellable;
	if (_tmp0_ != NULL) {
		GCancellable* _tmp1_;
		g_debug ("async.vala:10: Canceling...");
		_tmp1_ = _data1_->cancellable;
		g_cancellable_cancel (_tmp1_);
		result = FALSE;
		return result;
	}
	g_debug ("async.vala:14: Tick");
	result = TRUE;
	return result;
}


static gboolean ___lambda2__gsource_func (gpointer self) {
	gboolean result;
	result = __lambda2_ (self);
	return result;
}


static void test_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	TestData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	test_co (_data_);
}


static gboolean test_co (TestData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data1_ = g_slice_new0 (Block1Data);
	_data_->_data1_->_ref_count_ = 1;
	_data_->_data1_->_async_data_ = _data_;
	_data_->_data1_->cancellable = NULL;
	_data_->_tmp0_ = 0U;
	_data_->_tmp0_ = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, (guint) 1, ___lambda2__gsource_func, block1_data_ref (_data_->_data1_), block1_data_unref);
	_data_->id = _data_->_tmp0_;
	g_debug ("async.vala:18: Running query...");
	{
		_data_->_tmp1_ = _data_->db;
		_data_->_tmp2_ = NULL;
		_data_->_tmp2_ = sql_heavy_queryable_prepare ((SQLHeavyQueryable*) _data_->_tmp1_, "UPDATE `foo` SET `bar` = ((`bar` + 1729) / 7) % 3;", &_data_->_inner_error_);
		_data_->stmt = _data_->_tmp2_;
		if (_data_->_inner_error_ != NULL) {
			if (_data_->_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			block1_data_unref (_data_->_data1_);
			_data_->_data1_ = NULL;
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
		_data_->_tmp3_ = g_cancellable_new ();
		_g_object_unref0 (_data_->_data1_->cancellable);
		_data_->_data1_->cancellable = _data_->_tmp3_;
		_data_->_tmp4_ = _data_->stmt;
		_data_->_tmp5_ = _data_->_data1_->cancellable;
		_data_->_state_ = 1;
		sql_heavy_query_execute_async (_data_->_tmp4_, _data_->_tmp5_, test_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp6_ = NULL;
		_data_->_tmp6_ = sql_heavy_query_execute_finish (_data_->_tmp4_, _data_->_res_, &_data_->_inner_error_);
		_data_->_tmp7_ = _data_->_tmp6_;
		_g_object_unref0 (_data_->_tmp7_);
		if (_data_->_inner_error_ != NULL) {
			_g_object_unref0 (_data_->stmt);
			if (_data_->_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			_g_object_unref0 (_data_->stmt);
			block1_data_unref (_data_->_data1_);
			_data_->_data1_ = NULL;
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
		_g_object_unref0 (_data_->stmt);
	}
	goto __finally0;
	__catch0_sql_heavy_error:
	{
		_data_->e = _data_->_inner_error_;
		_data_->_inner_error_ = NULL;
		_data_->_tmp8_ = _data_->e;
		if (g_error_matches (_data_->_tmp8_, SQL_HEAVY_ERROR, SQL_HEAVY_ERROR_INTERRUPTED)) {
			g_debug ("async.vala:26: Query canceled.");
		} else {
			_data_->_tmp9_ = _data_->e;
			_data_->_tmp10_ = _data_->_tmp9_->message;
			_data_->_tmp11_ = _data_->e;
			_data_->_tmp12_ = _data_->_tmp11_->code;
			g_error ("async.vala:28: Execution threw an error: %s (%d)", _data_->_tmp10_, _data_->_tmp12_);
		}
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (_data_->_inner_error_ != NULL) {
		block1_data_unref (_data_->_data1_);
		_data_->_data1_ = NULL;
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
		g_clear_error (&_data_->_inner_error_);
		return FALSE;
	}
	g_debug ("async.vala:30: Finished running query.");
	_data_->_tmp13_ = _data_->id;
	g_source_remove (_data_->_tmp13_);
	_data_->_tmp14_ = loop;
	g_main_loop_quit (_data_->_tmp14_);
	block1_data_unref (_data_->_data1_);
	_data_->_data1_ = NULL;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void _vala_GValue_free (GValue* self) {
	g_value_unset (self);
	g_free (self);
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	GMainLoop* _tmp29_;
	GMainLoop* _tmp30_;
	GError * _inner_error_ = NULL;
	{
		SQLHeavyDatabase* _tmp0_;
		SQLHeavyDatabase* db;
		SQLHeavyDatabase* _tmp1_;
		SQLHeavyQuery* _tmp2_ = NULL;
		SQLHeavyQuery* stmt;
		SQLHeavyQuery* _tmp3_;
		SQLHeavyQueryResult* _tmp4_ = NULL;
		SQLHeavyQueryResult* _tmp5_;
		SQLHeavyDatabase* _tmp6_;
		SQLHeavyTransaction* _tmp7_ = NULL;
		SQLHeavyTransaction* trans;
		SQLHeavyTransaction* _tmp8_;
		SQLHeavyQuery* _tmp9_ = NULL;
		SQLHeavyQuery* _tmp10_;
		SQLHeavyTransaction* _tmp23_;
		SQLHeavyDatabase* _tmp24_;
		_tmp0_ = sql_heavy_database_new (NULL, (SQL_HEAVY_FILE_MODE_READ | SQL_HEAVY_FILE_MODE_WRITE) | SQL_HEAVY_FILE_MODE_CREATE, &_inner_error_);
		db = _tmp0_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch1_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 0;
		}
		_tmp1_ = db;
		_tmp2_ = sql_heavy_queryable_prepare ((SQLHeavyQueryable*) _tmp1_, "CREATE TABLE `foo` ( `bar` INT );", &_inner_error_);
		stmt = _tmp2_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (db);
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch1_sql_heavy_error;
			}
			_g_object_unref0 (db);
			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 0;
		}
		_tmp3_ = stmt;
		_tmp4_ = sql_heavy_query_execute (_tmp3_, NULL, &_inner_error_, NULL);
		_tmp5_ = _tmp4_;
		_g_object_unref0 (_tmp5_);
		if (_inner_error_ != NULL) {
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch1_sql_heavy_error;
			}
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			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 0;
		}
		g_debug ("async.vala:45: Populating database...");
		_tmp6_ = db;
		_tmp7_ = sql_heavy_queryable_begin_transaction ((SQLHeavyQueryable*) _tmp6_, &_inner_error_);
		trans = _tmp7_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch1_sql_heavy_error;
			}
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			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 0;
		}
		_tmp8_ = trans;
		_tmp9_ = sql_heavy_queryable_prepare ((SQLHeavyQueryable*) _tmp8_, "INSERT INTO `foo` (`bar`) VALUES (:value);", &_inner_error_);
		_tmp10_ = _tmp9_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (trans);
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch1_sql_heavy_error;
			}
			_g_object_unref0 (trans);
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			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 0;
		}
		_g_object_unref0 (stmt);
		stmt = _tmp10_;
		{
			gint i;
			i = 0;
			{
				gboolean _tmp11_;
				_tmp11_ = TRUE;
				while (TRUE) {
					gboolean _tmp12_;
					gint _tmp14_;
					gint _tmp15_;
					SQLHeavyQuery* _tmp17_;
					gint _tmp18_;
					GValue* _tmp19_ = NULL;
					SQLHeavyQuery* _tmp20_;
					SQLHeavyQueryResult* _tmp21_ = NULL;
					SQLHeavyQueryResult* _tmp22_;
					_tmp12_ = _tmp11_;
					if (!_tmp12_) {
						gint _tmp13_;
						_tmp13_ = i;
						i = _tmp13_ + 1;
					}
					_tmp11_ = FALSE;
					_tmp14_ = i;
					if (!(_tmp14_ < (65536 * 8))) {
						break;
					}
					_tmp15_ = i;
					if ((_tmp15_ & 0xffff) == 0xffff) {
						gint _tmp16_;
						_tmp16_ = i;
						g_debug ("async.vala:51: %d...", _tmp16_);
					}
					_tmp17_ = stmt;
					_tmp18_ = i;
					_tmp19_ = g_new0 (GValue, 1);
					g_value_init (_tmp19_, G_TYPE_INT);
					g_value_set_int (_tmp19_, _tmp18_);
					sql_heavy_query_set (_tmp17_, ":value", _tmp19_, &_inner_error_);
					__vala_GValue_free0 (_tmp19_);
					if (_inner_error_ != NULL) {
						_g_object_unref0 (trans);
						_g_object_unref0 (stmt);
						_g_object_unref0 (db);
						if (_inner_error_->domain == SQL_HEAVY_ERROR) {
							goto __catch1_sql_heavy_error;
						}
						_g_object_unref0 (trans);
						_g_object_unref0 (stmt);
						_g_object_unref0 (db);
						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 0;
					}
					_tmp20_ = stmt;
					_tmp21_ = sql_heavy_query_execute (_tmp20_, NULL, &_inner_error_, NULL);
					_tmp22_ = _tmp21_;
					_g_object_unref0 (_tmp22_);
					if (_inner_error_ != NULL) {
						_g_object_unref0 (trans);
						_g_object_unref0 (stmt);
						_g_object_unref0 (db);
						if (_inner_error_->domain == SQL_HEAVY_ERROR) {
							goto __catch1_sql_heavy_error;
						}
						_g_object_unref0 (trans);
						_g_object_unref0 (stmt);
						_g_object_unref0 (db);
						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 0;
					}
				}
			}
		}
		_tmp23_ = trans;
		sql_heavy_transaction_commit (_tmp23_, &_inner_error_);
		if (_inner_error_ != NULL) {
			_g_object_unref0 (trans);
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch1_sql_heavy_error;
			}
			_g_object_unref0 (trans);
			_g_object_unref0 (stmt);
			_g_object_unref0 (db);
			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 0;
		}
		_tmp24_ = db;
		test (_tmp24_, NULL, NULL);
		_g_object_unref0 (trans);
		_g_object_unref0 (stmt);
		_g_object_unref0 (db);
	}
	goto __finally1;
	__catch1_sql_heavy_error:
	{
		GError* e = NULL;
		GError* _tmp25_;
		const gchar* _tmp26_;
		GError* _tmp27_;
		gint _tmp28_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp25_ = e;
		_tmp26_ = _tmp25_->message;
		_tmp27_ = e;
		_tmp28_ = _tmp27_->code;
		g_debug ("async.vala:61: Error: %s (%d)", _tmp26_, _tmp28_);
		_g_error_free0 (e);
	}
	__finally1:
	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 0;
	}
	_tmp29_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (loop);
	loop = _tmp29_;
	_tmp30_ = loop;
	g_main_loop_run (_tmp30_);
	result = 0;
	return result;
}


int main (int argc, char ** argv) {
	g_type_init ();
	return _vala_main (argv, argc);
}



