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

/*
 * Copyright (C) 2009,2011 Jens Georg <mail@jensge.org>.
 *
 * Author: Jens Georg <mail@jensge.org>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "rygel-db.h"
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <glib/gi18n-lib.h>
#include "rygel-core.h"

enum  {
	RYGEL_DATABASE_DATABASE_0_PROPERTY,
	RYGEL_DATABASE_DATABASE_NAME_PROPERTY,
	RYGEL_DATABASE_DATABASE_FLAVOR_PROPERTY,
	RYGEL_DATABASE_DATABASE_FLAGS_PROPERTY,
	RYGEL_DATABASE_DATABASE_NUM_PROPERTIES
};
static GParamSpec* rygel_database_database_properties[RYGEL_DATABASE_DATABASE_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _sqlite3_close0(var) ((var == NULL) ? NULL : (var = (sqlite3_close (var), NULL)))
#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 _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _RygelDatabaseDatabasePrivate {
	gchar* _name;
	RygelDatabaseFlavor _flavor;
	RygelDatabaseFlags _flags;
	sqlite3* db;
};

static gint RygelDatabaseDatabase_private_offset;
static gpointer rygel_database_database_parent_class = NULL;
static GInitableIface * rygel_database_database_g_initable_parent_iface = NULL;

gint rygel_database_utf8_collate_str (guint8* a,
                                      gint a_length1,
                                      guint8* b,
                                      gint b_length1);
static gchar* rygel_database_database_build_path (RygelDatabaseDatabase* self);
static const gchar* rygel_database_database_get_name (RygelDatabaseDatabase* self);
static RygelDatabaseFlavor rygel_database_database_get_flavor (RygelDatabaseDatabase* self);
static gboolean rygel_database_database_real_init (GInitable* base,
                                            GCancellable* cancellable,
                                            GError** error);
static RygelDatabaseFlags rygel_database_database_get_flags (RygelDatabaseDatabase* self);
static void _rygel_database_database_utf8_contains_sqlite_user_func_callback (sqlite3_context* context,
                                                                       gint values_length1,
                                                                       sqlite3_value** values);
static gint _rygel_database_database_utf8_collate_sqlite_compare_callback (gpointer self,
                                                                    gint alen,
                                                                    void* a,
                                                                    gint blen,
                                                                    void* b);
static void rygel_database_database_on_trace (RygelDatabaseDatabase* self,
                                       const gchar* message);
static void _rygel_database_database_on_trace_sqlite_trace_callback (gpointer self,
                                                              const gchar* message);
static void rygel_database_database_finalize (GObject * obj);
static GType rygel_database_database_get_type_once (void);
static void _vala_rygel_database_database_get_property (GObject * object,
                                                 guint property_id,
                                                 GValue * value,
                                                 GParamSpec * pspec);
static void _vala_rygel_database_database_set_property (GObject * object,
                                                 guint property_id,
                                                 const GValue * value,
                                                 GParamSpec * pspec);

GQuark
rygel_database_database_error_quark (void)
{
	return g_quark_from_static_string ("rygel-database-database-error-quark");
}

static GType
rygel_database_database_error_get_type_once (void)
{
	static const GEnumValue values[] = {{RYGEL_DATABASE_DATABASE_ERROR_SQLITE_ERROR, "RYGEL_DATABASE_DATABASE_ERROR_SQLITE_ERROR", "sqlite-error"}, {RYGEL_DATABASE_DATABASE_ERROR_OPEN, "RYGEL_DATABASE_DATABASE_ERROR_OPEN", "open"}, {RYGEL_DATABASE_DATABASE_ERROR_PREPARE, "RYGEL_DATABASE_DATABASE_ERROR_PREPARE", "prepare"}, {RYGEL_DATABASE_DATABASE_ERROR_BIND, "RYGEL_DATABASE_DATABASE_ERROR_BIND", "bind"}, {RYGEL_DATABASE_DATABASE_ERROR_STEP, "RYGEL_DATABASE_DATABASE_ERROR_STEP", "step"}, {0, NULL, NULL}};
	GType rygel_database_database_error_type_id;
	rygel_database_database_error_type_id = g_enum_register_static ("RygelDatabaseDatabaseError", values);
	return rygel_database_database_error_type_id;
}

GType
rygel_database_database_error_get_type (void)
{
	static volatile gsize rygel_database_database_error_type_id__once = 0;
	if (g_once_init_enter (&rygel_database_database_error_type_id__once)) {
		GType rygel_database_database_error_type_id;
		rygel_database_database_error_type_id = rygel_database_database_error_get_type_once ();
		g_once_init_leave (&rygel_database_database_error_type_id__once, rygel_database_database_error_type_id);
	}
	return rygel_database_database_error_type_id__once;
}

static GType
rygel_database_flavor_get_type_once (void)
{
	static const GEnumValue values[] = {{RYGEL_DATABASE_FLAVOR_CACHE, "RYGEL_DATABASE_FLAVOR_CACHE", "cache"}, {RYGEL_DATABASE_FLAVOR_CONFIG, "RYGEL_DATABASE_FLAVOR_CONFIG", "config"}, {RYGEL_DATABASE_FLAVOR_FOREIGN, "RYGEL_DATABASE_FLAVOR_FOREIGN", "foreign"}, {0, NULL, NULL}};
	GType rygel_database_flavor_type_id;
	rygel_database_flavor_type_id = g_enum_register_static ("RygelDatabaseFlavor", values);
	return rygel_database_flavor_type_id;
}

GType
rygel_database_flavor_get_type (void)
{
	static volatile gsize rygel_database_flavor_type_id__once = 0;
	if (g_once_init_enter (&rygel_database_flavor_type_id__once)) {
		GType rygel_database_flavor_type_id;
		rygel_database_flavor_type_id = rygel_database_flavor_get_type_once ();
		g_once_init_leave (&rygel_database_flavor_type_id__once, rygel_database_flavor_type_id);
	}
	return rygel_database_flavor_type_id__once;
}

static GType
rygel_database_flags_get_type_once (void)
{
	static const GEnumValue values[] = {{RYGEL_DATABASE_FLAGS_READ_ONLY, "RYGEL_DATABASE_FLAGS_READ_ONLY", "read-only"}, {RYGEL_DATABASE_FLAGS_WRITE_ONLY, "RYGEL_DATABASE_FLAGS_WRITE_ONLY", "write-only"}, {RYGEL_DATABASE_FLAGS_READ_WRITE, "RYGEL_DATABASE_FLAGS_READ_WRITE", "read-write"}, {RYGEL_DATABASE_FLAGS_SHARED, "RYGEL_DATABASE_FLAGS_SHARED", "shared"}, {0, NULL, NULL}};
	GType rygel_database_flags_type_id;
	rygel_database_flags_type_id = g_enum_register_static ("RygelDatabaseFlags", values);
	return rygel_database_flags_type_id;
}

GType
rygel_database_flags_get_type (void)
{
	static volatile gsize rygel_database_flags_type_id__once = 0;
	if (g_once_init_enter (&rygel_database_flags_type_id__once)) {
		GType rygel_database_flags_type_id;
		rygel_database_flags_type_id = rygel_database_flags_get_type_once ();
		g_once_init_leave (&rygel_database_flags_type_id__once, rygel_database_flags_type_id);
	}
	return rygel_database_flags_type_id__once;
}

/**
     * Special GValue to pass to exec or exec_cursor to bind a column to
     * NULL
     */
void
rygel_database_null (GValue* result)
{
	GValue v = {0};
	GValue _tmp0_ = {0};
	g_value_init (&_tmp0_, G_TYPE_POINTER);
	v = _tmp0_;
	g_value_set_pointer (&v, NULL);
	*result = v;
	return;
}

static inline gpointer
rygel_database_database_get_instance_private (RygelDatabaseDatabase* self)
{
	return G_STRUCT_MEMBER_P (self, RygelDatabaseDatabase_private_offset);
}

/**
     * Function to implement the custom SQL function 'contains'
     */
void
rygel_database_database_utf8_contains (sqlite3_context* context,
                                       sqlite3_value** args,
                                       gint args_length1)
{
	gboolean _tmp0_ = FALSE;
	sqlite3_value* _tmp1_;
	const gchar* _tmp2_;
	gchar* pattern = NULL;
	sqlite3_value* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	const gchar* _tmp8_;
	sqlite3_value* _tmp9_;
	const gchar* _tmp10_;
	g_return_if_fail (context != NULL);
	_vala_return_if_fail (args_length1 == 2, "args.length == 2");
	_tmp1_ = args[0];
	_tmp2_ = (const gchar*) sqlite3_value_text (_tmp1_);
	if (_tmp2_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		sqlite3_value* _tmp3_;
		const gchar* _tmp4_;
		_tmp3_ = args[1];
		_tmp4_ = (const gchar*) sqlite3_value_text (_tmp3_);
		_tmp0_ = _tmp4_ == NULL;
	}
	if (_tmp0_) {
		sqlite3_result_int (context, 0);
		return;
	}
	_tmp5_ = args[1];
	_tmp6_ = (const gchar*) sqlite3_value_text (_tmp5_);
	_tmp7_ = g_regex_escape_string (_tmp6_, -1);
	pattern = _tmp7_;
	_tmp8_ = pattern;
	_tmp9_ = args[0];
	_tmp10_ = (const gchar*) sqlite3_value_text (_tmp9_);
	if (g_regex_match_simple (_tmp8_, _tmp10_, G_REGEX_CASELESS, 0)) {
		sqlite3_result_int (context, 1);
	} else {
		sqlite3_result_int (context, 0);
	}
	_g_free0 (pattern);
}

/**
     * Function to implement the custom SQLite collation 'CASEFOLD'.
     *
     * Uses utf8 case-fold to compare the strings.
     */
gint
rygel_database_database_utf8_collate (gint alen,
                                      void* a,
                                      gint blen,
                                      void* b)
{
	guint8* _a = NULL;
	gint _a_length1;
	gint __a_size_;
	guint8* _b = NULL;
	gint _b_length1;
	gint __b_size_;
	gint result;
	_a = (guint8*) a;
	_a_length1 = -1;
	__a_size_ = _a_length1;
	_a_length1 = alen;
	_b = (guint8*) b;
	_b_length1 = -1;
	__b_size_ = _b_length1;
	_b_length1 = blen;
	result = rygel_database_utf8_collate_str (_a, (gint) _a_length1, _b, (gint) _b_length1);
	return result;
}

static gchar*
rygel_database_database_build_path (RygelDatabaseDatabase* self)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_;
	gboolean name_is_path = FALSE;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp2_ = self->priv->_name;
	if (g_strcmp0 (_tmp2_, ":memory:") == 0) {
		_tmp1_ = TRUE;
	} else {
		const gchar* _tmp3_;
		_tmp3_ = self->priv->_name;
		_tmp1_ = g_path_is_absolute (_tmp3_);
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		RygelDatabaseFlavor _tmp4_;
		_tmp4_ = self->priv->_flavor;
		_tmp0_ = _tmp4_ == RYGEL_DATABASE_FLAVOR_FOREIGN;
	}
	name_is_path = _tmp0_;
	if (!name_is_path) {
		const gchar* _tmp5_ = NULL;
		RygelDatabaseFlavor _tmp6_;
		gchar* dirname = NULL;
		gchar* _tmp9_;
		const gchar* _tmp10_;
		const gchar* _tmp11_;
		const gchar* _tmp12_;
		gchar* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		gchar* _tmp16_;
		_tmp6_ = self->priv->_flavor;
		if (_tmp6_ == RYGEL_DATABASE_FLAVOR_CACHE) {
			const gchar* _tmp7_;
			_tmp7_ = g_get_user_cache_dir ();
			_tmp5_ = _tmp7_;
		} else {
			const gchar* _tmp8_;
			_tmp8_ = g_get_user_config_dir ();
			_tmp5_ = _tmp8_;
		}
		_tmp9_ = g_build_filename (_tmp5_, "rygel", NULL);
		dirname = _tmp9_;
		_tmp10_ = dirname;
		g_mkdir_with_parents (_tmp10_, 0750);
		_tmp11_ = dirname;
		_tmp12_ = self->priv->_name;
		_tmp13_ = g_strdup_printf ("%s.db", _tmp12_);
		_tmp14_ = _tmp13_;
		_tmp15_ = g_build_filename (_tmp11_, _tmp14_, NULL);
		_tmp16_ = _tmp15_;
		_g_free0 (_tmp14_);
		result = _tmp16_;
		_g_free0 (dirname);
		return result;
	} else {
		const gchar* _tmp17_;
		gchar* _tmp18_;
		rygel_database_database_set_flavor (self, RYGEL_DATABASE_FLAVOR_FOREIGN);
		_tmp17_ = self->priv->_name;
		_tmp18_ = g_strdup (_tmp17_);
		result = _tmp18_;
		return result;
	}
}

/**
     * Connect to a SQLite database file
     *
     * @param name Name of the database which is used to create the file-name
     * @param flavor Specifies the flavor of the database
     * @param flags How to open the database
     */
RygelDatabaseDatabase*
rygel_database_database_construct (GType object_type,
                                   const gchar* name,
                                   RygelDatabaseFlavor flavor,
                                   RygelDatabaseFlags flags,
                                   GError** error)
{
	RygelDatabaseDatabase * self = NULL;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (name != NULL, NULL);
	self = (RygelDatabaseDatabase*) g_object_new (object_type, "name", name, "flavor", flavor, "flags", flags, NULL);
	g_initable_init ((GInitable*) self, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	return self;
}

RygelDatabaseDatabase*
rygel_database_database_new (const gchar* name,
                             RygelDatabaseFlavor flavor,
                             RygelDatabaseFlags flags,
                             GError** error)
{
	return rygel_database_database_construct (RYGEL_DATABASE_TYPE_DATABASE, name, flavor, flags, error);
}

/**
     * Initialize database. Implemented for Initiable interface.
     *
     * @param cancellable a cancellable (unused)
     * @return true on success, false on error
     * @throws DatabaseError if anything goes wrong
     */
static void
_rygel_database_database_utf8_contains_sqlite_user_func_callback (sqlite3_context* context,
                                                                  gint values_length1,
                                                                  sqlite3_value** values)
{
	rygel_database_database_utf8_contains (context, values, values_length1);
}

static gint
_rygel_database_database_utf8_collate_sqlite_compare_callback (gpointer self,
                                                               gint alen,
                                                               void* a,
                                                               gint blen,
                                                               void* b)
{
	gint result;
	result = rygel_database_database_utf8_collate (alen, a, blen, b);
	return result;
}

static void
_rygel_database_database_on_trace_sqlite_trace_callback (gpointer self,
                                                         const gchar* message)
{
	rygel_database_database_on_trace ((RygelDatabaseDatabase*) self, message);
}

static gboolean
rygel_database_database_real_init (GInitable* base,
                                   GCancellable* cancellable,
                                   GError** error)
{
	RygelDatabaseDatabase * self;
	gchar* path = NULL;
	gchar* _tmp0_;
	RygelDatabaseFlags _tmp1_;
	sqlite3* _tmp6_;
	const gchar* _tmp14_;
	RygelDatabaseFlags _tmp15_;
	RygelDatabaseFlags _tmp17_;
	sqlite3* _tmp20_;
	sqlite3* _tmp21_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	self = (RygelDatabaseDatabase*) base;
	_tmp0_ = rygel_database_database_build_path (self);
	path = _tmp0_;
	_tmp1_ = self->priv->_flags;
	if (_tmp1_ == RYGEL_DATABASE_FLAGS_READ_ONLY) {
		const gchar* _tmp2_;
		sqlite3* _tmp3_ = NULL;
		_tmp2_ = path;
		sqlite3_open_v2 (_tmp2_, &_tmp3_, SQLITE_OPEN_READONLY, NULL);
		_sqlite3_close0 (self->priv->db);
		self->priv->db = _tmp3_;
	} else {
		const gchar* _tmp4_;
		sqlite3* _tmp5_ = NULL;
		_tmp4_ = path;
		sqlite3_open (_tmp4_, &_tmp5_);
		_sqlite3_close0 (self->priv->db);
		self->priv->db = _tmp5_;
	}
	_tmp6_ = self->priv->db;
	if (sqlite3_errcode (_tmp6_) != SQLITE_OK) {
		gchar* msg = NULL;
		gchar* _tmp7_;
		const gchar* _tmp8_;
		const gchar* _tmp9_;
		sqlite3* _tmp10_;
		const gchar* _tmp11_;
		GError* _tmp12_;
		gboolean _tmp13_ = FALSE;
		_tmp7_ = g_strdup (_ ("Error while opening SQLite database %s: %s"));
		msg = _tmp7_;
		_tmp8_ = msg;
		_tmp9_ = path;
		_tmp10_ = self->priv->db;
		_tmp11_ = sqlite3_errmsg (_tmp10_);
		_tmp12_ = g_error_new (RYGEL_DATABASE_DATABASE_ERROR, RYGEL_DATABASE_DATABASE_ERROR_OPEN, _tmp8_, _tmp9_, _tmp11_);
		_inner_error0_ = _tmp12_;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (msg);
		_g_free0 (path);
		return _tmp13_;
	}
	_tmp14_ = path;
	g_debug ("database.vala:179: Using database file %s", _tmp14_);
	_tmp15_ = self->priv->_flags;
	if (_tmp15_ != RYGEL_DATABASE_FLAGS_READ_ONLY) {
		rygel_database_database_exec (self, "PRAGMA synchronous = OFF", NULL, (gint) 0, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp16_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_free0 (path);
			return _tmp16_;
		}
	}
	_tmp17_ = self->priv->_flags;
	if ((_tmp17_ & RYGEL_DATABASE_FLAGS_SHARED) == RYGEL_DATABASE_FLAGS_SHARED) {
		rygel_database_database_exec (self, "PRAGMA journal_mode = WAL", NULL, (gint) 0, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp18_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_free0 (path);
			return _tmp18_;
		}
	} else {
		rygel_database_database_exec (self, "PRAGMA temp_store = MEMORY", NULL, (gint) 0, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp19_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_free0 (path);
			return _tmp19_;
		}
	}
	_tmp20_ = self->priv->db;
	sqlite3_create_function (_tmp20_, "contains", 2, SQLITE_UTF8, NULL, _rygel_database_database_utf8_contains_sqlite_user_func_callback, NULL, NULL);
	_tmp21_ = self->priv->db;
	sqlite3_create_collation (_tmp21_, "CASEFOLD", SQLITE_UTF8, NULL, (int (*)(void *, int,  const void *, int,  const void *)) _rygel_database_database_utf8_collate_sqlite_compare_callback);
	{
		RygelMetaConfig* config = NULL;
		RygelMetaConfig* _tmp22_;
		gboolean _tmp23_ = FALSE;
		RygelMetaConfig* _tmp24_;
		_tmp22_ = rygel_meta_config_get_default ();
		config = _tmp22_;
		_tmp24_ = config;
		_tmp23_ = rygel_configuration_get_bool ((RygelConfiguration*) _tmp24_, "Database", "Debug", &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (config);
			goto __catch0_g_error;
		}
		if (_tmp23_) {
			sqlite3* _tmp25_;
			_tmp25_ = self->priv->db;
			sqlite3_trace (_tmp25_, _rygel_database_database_on_trace_sqlite_trace_callback, self);
		}
		_g_object_unref0 (config);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
		g_debug ("database.vala:209: Not setting up SQL tracing");
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp26_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (path);
		return _tmp26_;
	}
	result = TRUE;
	_g_free0 (path);
	return result;
}

static void
rygel_database_database_on_trace (RygelDatabaseDatabase* self,
                                  const gchar* message)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (message != NULL);
	g_debug ("database.vala:216: SQLITE: %s", message);
}

/**
     * SQL query function.
     *
     * Use for all queries that return a result set.
     *
     * @param sql The SQL query to run.
     * @param arguments Values to bind in the SQL query or null.
     * @throws DatabaseError if the underlying SQLite operation fails.
     */
RygelDatabaseCursor*
rygel_database_database_exec_cursor (RygelDatabaseDatabase* self,
                                     const gchar* sql,
                                     GValue* arguments,
                                     gint arguments_length1,
                                     GError** error)
{
	RygelDatabaseCursor* _tmp0_ = NULL;
	sqlite3* _tmp1_;
	RygelDatabaseCursor* _tmp2_;
	RygelDatabaseCursor* _tmp3_;
	GError* _inner_error0_ = NULL;
	RygelDatabaseCursor* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (sql != NULL, NULL);
	_tmp1_ = self->priv->db;
	_tmp2_ = rygel_database_cursor_new (_tmp1_, sql, arguments, arguments_length1, &_inner_error0_);
	_tmp0_ = _tmp2_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			g_propagate_error (error, _inner_error0_);
			return NULL;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
	}
	_tmp3_ = _tmp0_;
	_tmp0_ = NULL;
	result = _tmp3_;
	_g_object_unref0 (_tmp0_);
	return result;
}

/**
     * Simple SQL query execution function.
     *
     * Use for all queries that don't return anything.
     *
     * @param sql The SQL query to run.
     * @param arguments Values to bind in the SQL query or null.
     * @throws DatabaseError if the underlying SQLite operation fails.
     */
static gint
_sqlite3_exec (sqlite3* self,
               const gchar* sql,
               sqlite3_callback callback,
               gpointer callback_target,
               gchar** errmsg)
{
	gchar* _vala_errmsg = NULL;
	const gchar* sqlite_errmsg = NULL;
	gint ec = 0;
	const gchar* _tmp0_ = NULL;
	gint _tmp1_;
	const gchar* _tmp4_;
	gint result;
	g_return_val_if_fail (self != NULL, 0);
	g_return_val_if_fail (sql != NULL, 0);
	_tmp1_ = sqlite3_exec (self, sql, callback, callback_target, (char**) (&_tmp0_));
	sqlite_errmsg = _tmp0_;
	ec = _tmp1_;
	if ((&_vala_errmsg) != NULL) {
		const gchar* _tmp2_;
		gchar* _tmp3_;
		_tmp2_ = sqlite_errmsg;
		_tmp3_ = g_strdup (_tmp2_);
		_g_free0 (_vala_errmsg);
		_vala_errmsg = _tmp3_;
	} else {
		_g_free0 (_vala_errmsg);
		_vala_errmsg = NULL;
	}
	_tmp4_ = sqlite_errmsg;
	sqlite3_free ((void*) _tmp4_);
	result = ec;
	if (errmsg) {
		*errmsg = _vala_errmsg;
	} else {
		_g_free0 (_vala_errmsg);
	}
	return result;
}

void
rygel_database_database_exec (RygelDatabaseDatabase* self,
                              const gchar* sql,
                              GValue* arguments,
                              gint arguments_length1,
                              GError** error)
{
	RygelDatabaseCursor* cursor = NULL;
	RygelDatabaseCursor* _tmp7_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (sql != NULL);
	if (arguments == NULL) {
		sqlite3* _tmp0_;
		sqlite3* _tmp1_;
		_tmp0_ = self->priv->db;
		_sqlite3_exec (_tmp0_, sql, NULL, NULL, NULL);
		_tmp1_ = self->priv->db;
		if (sqlite3_errcode (_tmp1_) != SQLITE_OK) {
			gchar* msg = NULL;
			gchar* _tmp2_;
			const gchar* _tmp3_;
			sqlite3* _tmp4_;
			const gchar* _tmp5_;
			GError* _tmp6_;
			_tmp2_ = g_strdup ("Failed to run query %s: %s");
			msg = _tmp2_;
			_tmp3_ = msg;
			_tmp4_ = self->priv->db;
			_tmp5_ = sqlite3_errmsg (_tmp4_);
			_tmp6_ = g_error_new (RYGEL_DATABASE_DATABASE_ERROR, RYGEL_DATABASE_DATABASE_ERROR_SQLITE_ERROR, _tmp3_, sql, _tmp5_);
			_inner_error0_ = _tmp6_;
			if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
				g_propagate_error (error, _inner_error0_);
				_g_free0 (msg);
				return;
			} else {
				_g_free0 (msg);
				g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return;
			}
			_g_free0 (msg);
		}
		return;
	}
	_tmp7_ = rygel_database_database_exec_cursor (self, sql, arguments, (gint) arguments_length1, &_inner_error0_);
	cursor = _tmp7_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			g_propagate_error (error, _inner_error0_);
			return;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
	while (TRUE) {
		gboolean _tmp8_ = FALSE;
		RygelDatabaseCursor* _tmp9_;
		RygelDatabaseCursor* _tmp10_;
		_tmp9_ = cursor;
		_tmp8_ = rygel_database_cursor_has_next (_tmp9_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
				g_propagate_error (error, _inner_error0_);
				_g_object_unref0 (cursor);
				return;
			} else {
				_g_object_unref0 (cursor);
				g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return;
			}
		}
		if (!_tmp8_) {
			break;
		}
		_tmp10_ = cursor;
		rygel_database_cursor_next (_tmp10_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
				g_propagate_error (error, _inner_error0_);
				_g_object_unref0 (cursor);
				return;
			} else {
				_g_object_unref0 (cursor);
				g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
				g_clear_error (&_inner_error0_);
				return;
			}
		}
	}
	_g_object_unref0 (cursor);
}

/**
     * Execute a SQL query that returns a single number.
     *
     * @param sql The SQL query to run.
     * @param args Values to bind in the SQL query or null.
     * @return The contents of the first row's column as an int.
     * @throws DatabaseError if the underlying SQLite operation fails.
     */
gint
rygel_database_database_query_value (RygelDatabaseDatabase* self,
                                     const gchar* sql,
                                     GValue* args,
                                     gint args_length1,
                                     GError** error)
{
	RygelDatabaseCursor* cursor = NULL;
	RygelDatabaseCursor* _tmp0_;
	sqlite3_stmt* statement = NULL;
	RygelDatabaseCursor* _tmp3_;
	sqlite3_stmt* _tmp4_;
	sqlite3_stmt* _tmp7_;
	GError* _inner_error0_ = NULL;
	gint result;
	g_return_val_if_fail (self != NULL, 0);
	g_return_val_if_fail (sql != NULL, 0);
	_tmp0_ = rygel_database_database_exec_cursor (self, sql, args, (gint) args_length1, &_inner_error0_);
	cursor = _tmp0_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			gint _tmp1_ = -1;
			g_propagate_error (error, _inner_error0_);
			return _tmp1_;
		} else {
			gint _tmp2_ = -1;
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return _tmp2_;
		}
	}
	_tmp3_ = cursor;
	_tmp4_ = rygel_database_cursor_next (_tmp3_, &_inner_error0_);
	statement = _tmp4_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			gint _tmp5_ = -1;
			g_propagate_error (error, _inner_error0_);
			_g_object_unref0 (cursor);
			return _tmp5_;
		} else {
			gint _tmp6_ = -1;
			_g_object_unref0 (cursor);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return _tmp6_;
		}
	}
	_tmp7_ = statement;
	result = sqlite3_column_int (_tmp7_, 0);
	_g_object_unref0 (cursor);
	return result;
}

/**
     * Analyze triggers of database
     */
void
rygel_database_database_analyze (RygelDatabaseDatabase* self)
{
	sqlite3* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->db;
	_sqlite3_exec (_tmp0_, "ANALYZE", NULL, NULL, NULL);
}

/**
     * Start a transaction
     */
void
rygel_database_database_begin (RygelDatabaseDatabase* self,
                               GError** error)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	rygel_database_database_exec (self, "BEGIN", NULL, (gint) 0, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			g_propagate_error (error, _inner_error0_);
			return;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
}

/**
     * Commit a transaction
     */
void
rygel_database_database_commit (RygelDatabaseDatabase* self,
                                GError** error)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	rygel_database_database_exec (self, "COMMIT", NULL, (gint) 0, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			g_propagate_error (error, _inner_error0_);
			return;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
}

/**
     * Rollback a transaction
     */
void
rygel_database_database_rollback (RygelDatabaseDatabase* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		rygel_database_database_exec (self, "ROLLBACK", NULL, (gint) 0, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
				goto __catch0_rygel_database_database_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
	goto __finally0;
	__catch0_rygel_database_database_error:
	{
		GError* _error_ = NULL;
		GError* _tmp0_;
		const gchar* _tmp1_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp0_ = _error_;
		_tmp1_ = _tmp0_->message;
		g_critical (_ ("Failed to roll back transaction: %s"), _tmp1_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

/**
     * Check for an empty SQLite database.
     * @return true if the file is an empty SQLite database, false otherwise
     * @throws DatabaseError if the SQLite meta table does not exist which
     * usually indicates that the file is not a databsae
     */
gboolean
rygel_database_database_is_empty (RygelDatabaseDatabase* self,
                                  GError** error)
{
	gint _tmp0_ = 0;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = rygel_database_database_query_value (self, "SELECT count(type) FROM " "sqlite_master WHERE rowid = 1", NULL, (gint) 0, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_DATABASE_DATABASE_ERROR) {
			gboolean _tmp1_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			return _tmp1_;
		} else {
			gboolean _tmp2_ = FALSE;
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return _tmp2_;
		}
	}
	result = _tmp0_ == 0;
	return result;
}

static const gchar*
rygel_database_database_get_name (RygelDatabaseDatabase* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_name;
	result = _tmp0_;
	return result;
}

void
rygel_database_database_set_name (RygelDatabaseDatabase* self,
                                  const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = rygel_database_database_get_name (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_name);
		self->priv->_name = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, rygel_database_database_properties[RYGEL_DATABASE_DATABASE_NAME_PROPERTY]);
	}
}

static RygelDatabaseFlavor
rygel_database_database_get_flavor (RygelDatabaseDatabase* self)
{
	RygelDatabaseFlavor result;
	g_return_val_if_fail (self != NULL, 0);
	result = self->priv->_flavor;
	return result;
}

void
rygel_database_database_set_flavor (RygelDatabaseDatabase* self,
                                    RygelDatabaseFlavor value)
{
	RygelDatabaseFlavor old_value;
	g_return_if_fail (self != NULL);
	old_value = rygel_database_database_get_flavor (self);
	if (old_value != value) {
		self->priv->_flavor = value;
		g_object_notify_by_pspec ((GObject *) self, rygel_database_database_properties[RYGEL_DATABASE_DATABASE_FLAVOR_PROPERTY]);
	}
}

static RygelDatabaseFlags
rygel_database_database_get_flags (RygelDatabaseDatabase* self)
{
	RygelDatabaseFlags result;
	g_return_val_if_fail (self != NULL, 0);
	result = self->priv->_flags;
	return result;
}

void
rygel_database_database_set_flags (RygelDatabaseDatabase* self,
                                   RygelDatabaseFlags value)
{
	RygelDatabaseFlags old_value;
	g_return_if_fail (self != NULL);
	old_value = rygel_database_database_get_flags (self);
	if (old_value != value) {
		self->priv->_flags = value;
		g_object_notify_by_pspec ((GObject *) self, rygel_database_database_properties[RYGEL_DATABASE_DATABASE_FLAGS_PROPERTY]);
	}
}

static void
rygel_database_database_class_init (RygelDatabaseDatabaseClass * klass,
                                    gpointer klass_data)
{
	rygel_database_database_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelDatabaseDatabase_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_rygel_database_database_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_rygel_database_database_set_property;
	G_OBJECT_CLASS (klass)->finalize = rygel_database_database_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), RYGEL_DATABASE_DATABASE_NAME_PROPERTY, rygel_database_database_properties[RYGEL_DATABASE_DATABASE_NAME_PROPERTY] = g_param_spec_string ("name", "name", "name", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
	g_object_class_install_property (G_OBJECT_CLASS (klass), RYGEL_DATABASE_DATABASE_FLAVOR_PROPERTY, rygel_database_database_properties[RYGEL_DATABASE_DATABASE_FLAVOR_PROPERTY] = g_param_spec_enum ("flavor", "flavor", "flavor", RYGEL_DATABASE_TYPE_FLAVOR, RYGEL_DATABASE_FLAVOR_CACHE, G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
	g_object_class_install_property (G_OBJECT_CLASS (klass), RYGEL_DATABASE_DATABASE_FLAGS_PROPERTY, rygel_database_database_properties[RYGEL_DATABASE_DATABASE_FLAGS_PROPERTY] = g_param_spec_enum ("flags", "flags", "flags", RYGEL_DATABASE_TYPE_FLAGS, RYGEL_DATABASE_FLAGS_READ_WRITE, G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

static void
rygel_database_database_g_initable_interface_init (GInitableIface * iface,
                                                   gpointer iface_data)
{
	rygel_database_database_g_initable_parent_iface = g_type_interface_peek_parent (iface);
	iface->init = (gboolean (*) (GInitable*, GCancellable*, GError**)) rygel_database_database_real_init;
}

static void
rygel_database_database_instance_init (RygelDatabaseDatabase * self,
                                       gpointer klass)
{
	self->priv = rygel_database_database_get_instance_private (self);
	self->priv->_flavor = RYGEL_DATABASE_FLAVOR_CACHE;
	self->priv->_flags = RYGEL_DATABASE_FLAGS_READ_WRITE;
}

static void
rygel_database_database_finalize (GObject * obj)
{
	RygelDatabaseDatabase * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_DATABASE_TYPE_DATABASE, RygelDatabaseDatabase);
	_g_free0 (self->priv->_name);
	_sqlite3_close0 (self->priv->db);
	G_OBJECT_CLASS (rygel_database_database_parent_class)->finalize (obj);
}

/**
 * This class is a thin wrapper around SQLite's database object.
 *
 * It adds statement preparation based on GValue and a cancellable exec
 * function.
 */
static GType
rygel_database_database_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelDatabaseDatabaseClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_database_database_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelDatabaseDatabase), 0, (GInstanceInitFunc) rygel_database_database_instance_init, NULL };
	static const GInterfaceInfo g_initable_info = { (GInterfaceInitFunc) rygel_database_database_g_initable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType rygel_database_database_type_id;
	rygel_database_database_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelDatabaseDatabase", &g_define_type_info, 0);
	g_type_add_interface_static (rygel_database_database_type_id, g_initable_get_type (), &g_initable_info);
	RygelDatabaseDatabase_private_offset = g_type_add_instance_private (rygel_database_database_type_id, sizeof (RygelDatabaseDatabasePrivate));
	return rygel_database_database_type_id;
}

GType
rygel_database_database_get_type (void)
{
	static volatile gsize rygel_database_database_type_id__once = 0;
	if (g_once_init_enter (&rygel_database_database_type_id__once)) {
		GType rygel_database_database_type_id;
		rygel_database_database_type_id = rygel_database_database_get_type_once ();
		g_once_init_leave (&rygel_database_database_type_id__once, rygel_database_database_type_id);
	}
	return rygel_database_database_type_id__once;
}

static void
_vala_rygel_database_database_get_property (GObject * object,
                                            guint property_id,
                                            GValue * value,
                                            GParamSpec * pspec)
{
	RygelDatabaseDatabase * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, RYGEL_DATABASE_TYPE_DATABASE, RygelDatabaseDatabase);
	switch (property_id) {
		case RYGEL_DATABASE_DATABASE_NAME_PROPERTY:
		g_value_set_string (value, rygel_database_database_get_name (self));
		break;
		case RYGEL_DATABASE_DATABASE_FLAVOR_PROPERTY:
		g_value_set_enum (value, rygel_database_database_get_flavor (self));
		break;
		case RYGEL_DATABASE_DATABASE_FLAGS_PROPERTY:
		g_value_set_enum (value, rygel_database_database_get_flags (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_rygel_database_database_set_property (GObject * object,
                                            guint property_id,
                                            const GValue * value,
                                            GParamSpec * pspec)
{
	RygelDatabaseDatabase * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, RYGEL_DATABASE_TYPE_DATABASE, RygelDatabaseDatabase);
	switch (property_id) {
		case RYGEL_DATABASE_DATABASE_NAME_PROPERTY:
		rygel_database_database_set_name (self, g_value_get_string (value));
		break;
		case RYGEL_DATABASE_DATABASE_FLAVOR_PROPERTY:
		rygel_database_database_set_flavor (self, g_value_get_enum (value));
		break;
		case RYGEL_DATABASE_DATABASE_FLAGS_PROPERTY:
		rygel_database_database_set_flags (self, g_value_get_enum (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

