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

/*
* Copyright (c) 2011-2016 Felipe Escoto (https://github.com/Philip-Scott/Notes-up)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authored by: Felipe Escoto <felescoto95@hotmail.com>
*/

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

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define ENOTES_TYPE_TAG (enotes_tag_get_type ())
#define ENOTES_TAG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_TAG, ENotesTag))
#define ENOTES_TAG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_TAG, ENotesTagClass))
#define ENOTES_IS_TAG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_TAG))
#define ENOTES_IS_TAG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_TAG))
#define ENOTES_TAG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_TAG, ENotesTagClass))

typedef struct _ENotesTag ENotesTag;
typedef struct _ENotesTagClass ENotesTagClass;
typedef struct _ENotesTagPrivate ENotesTagPrivate;
enum  {
	ENOTES_TAG_0_PROPERTY,
	ENOTES_TAG_NUM_PROPERTIES
};
static GParamSpec* enotes_tag_properties[ENOTES_TAG_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))

#define TYPE_DATABASE_TABLE (database_table_get_type ())
#define DATABASE_TABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DATABASE_TABLE, DatabaseTable))
#define DATABASE_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DATABASE_TABLE, DatabaseTableClass))
#define IS_DATABASE_TABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DATABASE_TABLE))
#define IS_DATABASE_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DATABASE_TABLE))
#define DATABASE_TABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DATABASE_TABLE, DatabaseTableClass))

typedef struct _DatabaseTable DatabaseTable;
typedef struct _DatabaseTableClass DatabaseTableClass;
typedef struct _DatabaseTablePrivate DatabaseTablePrivate;

#define ENOTES_TYPE_TAGS_TABLE (enotes_tags_table_get_type ())
#define ENOTES_TAGS_TABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_TAGS_TABLE, ENotesTagsTable))
#define ENOTES_TAGS_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_TAGS_TABLE, ENotesTagsTableClass))
#define ENOTES_IS_TAGS_TABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_TAGS_TABLE))
#define ENOTES_IS_TAGS_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_TAGS_TABLE))
#define ENOTES_TAGS_TABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_TAGS_TABLE, ENotesTagsTableClass))

typedef struct _ENotesTagsTable ENotesTagsTable;
typedef struct _ENotesTagsTableClass ENotesTagsTableClass;
typedef struct _ENotesTagsTablePrivate ENotesTagsTablePrivate;
#define _database_table_unref0(var) ((var == NULL) ? NULL : (var = (database_table_unref (var), NULL)))
#define _sqlite3_finalize0(var) ((var == NULL) ? NULL : (var = (sqlite3_finalize (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define ENOTES_TYPE_PAGE (enotes_page_get_type ())
#define ENOTES_PAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_PAGE, ENotesPage))
#define ENOTES_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_PAGE, ENotesPageClass))
#define ENOTES_IS_PAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_PAGE))
#define ENOTES_IS_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_PAGE))
#define ENOTES_PAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_PAGE, ENotesPageClass))

typedef struct _ENotesPage ENotesPage;
typedef struct _ENotesPageClass ENotesPageClass;
typedef struct _ENotesPagePrivate ENotesPagePrivate;

struct _ENotesTag {
	GObject parent_instance;
	ENotesTagPrivate * priv;
	gint64 id;
	gchar* name;
	gchar* data;
};

struct _ENotesTagClass {
	GObjectClass parent_class;
};

struct _DatabaseTable {
	GTypeInstance parent_instance;
	volatile int ref_count;
	DatabaseTablePrivate * priv;
	gchar* table_name;
};

struct _DatabaseTableClass {
	GTypeClass parent_class;
	void (*finalize) (DatabaseTable *self);
};

struct _ENotesTagsTable {
	DatabaseTable parent_instance;
	ENotesTagsTablePrivate * priv;
};

struct _ENotesTagsTableClass {
	DatabaseTableClass parent_class;
};

struct _ENotesPage {
	GObject parent_instance;
	ENotesPagePrivate * priv;
	gint64 id;
	gint64 notebook_id;
	gchar* name;
	gchar* data;
	gchar* subtitle;
	gchar* html_cache;
	gint64 creation_date;
	gint64 modification_date;
	gboolean new_page;
	gboolean cache_changed;
	gchar* full_path;
};

struct _ENotesPageClass {
	GObjectClass parent_class;
};

static gpointer enotes_tag_parent_class = NULL;
static gpointer enotes_tags_table_parent_class = NULL;
static ENotesTagsTable* enotes_tags_table_instance;
static ENotesTagsTable* enotes_tags_table_instance = NULL;

VALA_EXTERN GType enotes_tag_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesTag, g_object_unref)
VALA_EXTERN ENotesTag* enotes_tag_new (void);
VALA_EXTERN ENotesTag* enotes_tag_construct (GType object_type);
static void enotes_tag_finalize (GObject * obj);
static GType enotes_tag_get_type_once (void);
VALA_EXTERN gpointer database_table_ref (gpointer instance);
VALA_EXTERN void database_table_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_database_table (const gchar* name,
                                       const gchar* nick,
                                       const gchar* blurb,
                                       GType object_type,
                                       GParamFlags flags);
VALA_EXTERN void value_set_database_table (GValue* value,
                               gpointer v_object);
VALA_EXTERN void value_take_database_table (GValue* value,
                                gpointer v_object);
VALA_EXTERN gpointer value_get_database_table (const GValue* value);
VALA_EXTERN GType database_table_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (DatabaseTable, database_table_unref)
VALA_EXTERN GType enotes_tags_table_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesTagsTable, database_table_unref)
VALA_EXTERN ENotesTagsTable* enotes_tags_table_get_instance (void);
static ENotesTagsTable* enotes_tags_table_new (void);
static ENotesTagsTable* enotes_tags_table_construct (GType object_type);
VALA_EXTERN DatabaseTable* database_table_construct (GType object_type);
VALA_EXTERN sqlite3_stmt* database_table_create_stmt (const gchar* data);
VALA_EXTERN void database_table_fatal (const gchar* op,
                           gint res);
VALA_EXTERN void database_table_set_table_name (DatabaseTable* self,
                                    const gchar* table_name);
VALA_EXTERN GeeArrayList* enotes_tags_table_get_tags_for_page (ENotesTagsTable* self,
                                                   gint64 page_id);
VALA_EXTERN void database_table_bind_int (sqlite3_stmt* stmt,
                              gint column,
                              gint64 data);
VALA_EXTERN GType enotes_page_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesPage, g_object_unref)
VALA_EXTERN void enotes_tags_table_create_tag (ENotesTagsTable* self,
                                   const gchar* name,
                                   ENotesPage* page);
VALA_EXTERN void database_table_bind_text (sqlite3_stmt* stmt,
                               gint column,
                               const gchar* data);
VALA_EXTERN void enotes_tags_table_delete_tag (ENotesTagsTable* self,
                                   ENotesTag* tag);
VALA_EXTERN void enotes_tags_table_remove_tags_from_page (ENotesTagsTable* self,
                                              gint64 page_id);
VALA_EXTERN void enotes_tags_table_remove_tag_from_page (ENotesTagsTable* self,
                                             gint64 tag_id,
                                             gint64 page_id);
VALA_EXTERN gboolean enotes_tags_table_save_tag (ENotesTagsTable* self,
                                     ENotesTag* tag);
VALA_EXTERN GeeArrayList* enotes_tags_table_get_tags (ENotesTagsTable* self);
VALA_EXTERN GeeArrayList* enotes_tags_table_get_pages_for_tag (ENotesTagsTable* self,
                                                   ENotesTag* tag);
VALA_EXTERN ENotesPage* enotes_page_new (void);
VALA_EXTERN ENotesPage* enotes_page_construct (GType object_type);
VALA_EXTERN void enotes_tags_table_reset_instance (void);
static void enotes_tags_table_finalize (DatabaseTable * obj);
static GType enotes_tags_table_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

ENotesTag*
enotes_tag_construct (GType object_type)
{
	ENotesTag * self = NULL;
	self = (ENotesTag*) g_object_new (object_type, NULL);
	return self;
}

ENotesTag*
enotes_tag_new (void)
{
	return enotes_tag_construct (ENOTES_TYPE_TAG);
}

static void
enotes_tag_class_init (ENotesTagClass * klass,
                       gpointer klass_data)
{
	enotes_tag_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = enotes_tag_finalize;
}

static void
enotes_tag_instance_init (ENotesTag * self,
                          gpointer klass)
{
	self->id = (gint64) -1;
}

static void
enotes_tag_finalize (GObject * obj)
{
	ENotesTag * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_TAG, ENotesTag);
	_g_free0 (self->name);
	_g_free0 (self->data);
	G_OBJECT_CLASS (enotes_tag_parent_class)->finalize (obj);
}

static GType
enotes_tag_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ENotesTagClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) enotes_tag_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ENotesTag), 0, (GInstanceInitFunc) enotes_tag_instance_init, NULL };
	GType enotes_tag_type_id;
	enotes_tag_type_id = g_type_register_static (G_TYPE_OBJECT, "ENotesTag", &g_define_type_info, 0);
	return enotes_tag_type_id;
}

GType
enotes_tag_get_type (void)
{
	static volatile gsize enotes_tag_type_id__once = 0;
	if (g_once_init_enter (&enotes_tag_type_id__once)) {
		GType enotes_tag_type_id;
		enotes_tag_type_id = enotes_tag_get_type_once ();
		g_once_init_leave (&enotes_tag_type_id__once, enotes_tag_type_id);
	}
	return enotes_tag_type_id__once;
}

static gpointer
_database_table_ref0 (gpointer self)
{
	return self ? database_table_ref (self) : NULL;
}

ENotesTagsTable*
enotes_tags_table_get_instance (void)
{
	ENotesTagsTable* _tmp0_;
	ENotesTagsTable* _tmp2_;
	ENotesTagsTable* _tmp3_;
	ENotesTagsTable* result;
	_tmp0_ = enotes_tags_table_instance;
	if (_tmp0_ == NULL) {
		ENotesTagsTable* _tmp1_;
		_tmp1_ = enotes_tags_table_new ();
		_database_table_unref0 (enotes_tags_table_instance);
		enotes_tags_table_instance = _tmp1_;
	}
	_tmp2_ = enotes_tags_table_instance;
	_tmp3_ = _database_table_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

static ENotesTagsTable*
enotes_tags_table_construct (GType object_type)
{
	ENotesTagsTable* self = NULL;
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	gint res = 0;
	sqlite3_stmt* _tmp1_;
	sqlite3_stmt* _tmp2_;
	sqlite3_stmt* _tmp3_;
	self = (ENotesTagsTable*) database_table_construct (object_type);
	_tmp0_ = database_table_create_stmt ("CREATE TABLE IF NOT EXISTS Tags (" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "name TEXT UNIQUE NOT NULL DEFAULT '', " "data TEXT NOT NULL DEFAULT '')");
	stmt = _tmp0_;
	_tmp1_ = stmt;
	res = sqlite3_step (_tmp1_);
	if (res != SQLITE_DONE) {
		database_table_fatal ("Failed to create tag table", res);
	}
	_tmp2_ = database_table_create_stmt ("CREATE TABLE IF NOT EXISTS TagsPage (" "page_id INTEGER, " "tag_id INTEGER)");
	_sqlite3_finalize0 (stmt);
	stmt = _tmp2_;
	_tmp3_ = stmt;
	res = sqlite3_step (_tmp3_);
	if (res != SQLITE_DONE) {
		database_table_fatal ("Failed to create tag table", res);
	}
	database_table_set_table_name ((DatabaseTable*) self, "Tags");
	_sqlite3_finalize0 (stmt);
	return self;
}

static ENotesTagsTable*
enotes_tags_table_new (void)
{
	return enotes_tags_table_construct (ENOTES_TYPE_TAGS_TABLE);
}

GeeArrayList*
enotes_tags_table_get_tags_for_page (ENotesTagsTable* self,
                                     gint64 page_id)
{
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	sqlite3_stmt* _tmp1_;
	GeeArrayList* tags = NULL;
	GeeArrayList* _tmp2_;
	GeeArrayList* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = database_table_create_stmt ("SELECT id, name, data " "FROM Tags tag " "INNER JOIN TagsPage tp " "   ON tp.tag_id = tag.id " "WHERE tp.page_id = ?");
	stmt = _tmp0_;
	_tmp1_ = stmt;
	database_table_bind_int (_tmp1_, 1, page_id);
	_tmp2_ = gee_array_list_new (ENOTES_TYPE_TAG, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	tags = _tmp2_;
	{
		gboolean _tmp3_ = FALSE;
		_tmp3_ = TRUE;
		while (TRUE) {
			gint res = 0;
			sqlite3_stmt* _tmp4_;
			ENotesTag* row = NULL;
			ENotesTag* _tmp5_;
			ENotesTag* _tmp6_;
			sqlite3_stmt* _tmp7_;
			ENotesTag* _tmp8_;
			sqlite3_stmt* _tmp9_;
			const gchar* _tmp10_;
			gchar* _tmp11_;
			ENotesTag* _tmp12_;
			sqlite3_stmt* _tmp13_;
			const gchar* _tmp14_;
			gchar* _tmp15_;
			GeeArrayList* _tmp16_;
			ENotesTag* _tmp17_;
			if (!_tmp3_) {
			}
			_tmp3_ = FALSE;
			_tmp4_ = stmt;
			res = sqlite3_step (_tmp4_);
			if (res == SQLITE_DONE) {
				break;
			} else {
				if (res != SQLITE_ROW) {
					database_table_fatal ("get_all_tags", res);
					break;
				}
			}
			_tmp5_ = enotes_tag_new ();
			row = _tmp5_;
			_tmp6_ = row;
			_tmp7_ = stmt;
			_tmp6_->id = sqlite3_column_int64 (_tmp7_, 0);
			_tmp8_ = row;
			_tmp9_ = stmt;
			_tmp10_ = (const gchar*) sqlite3_column_text (_tmp9_, 1);
			_tmp11_ = g_strdup (_tmp10_);
			_g_free0 (_tmp8_->name);
			_tmp8_->name = _tmp11_;
			_tmp12_ = row;
			_tmp13_ = stmt;
			_tmp14_ = (const gchar*) sqlite3_column_text (_tmp13_, 2);
			_tmp15_ = g_strdup (_tmp14_);
			_g_free0 (_tmp12_->data);
			_tmp12_->data = _tmp15_;
			_tmp16_ = tags;
			_tmp17_ = row;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp16_, _tmp17_);
			_g_object_unref0 (row);
		}
	}
	result = tags;
	_sqlite3_finalize0 (stmt);
	return result;
}

void
enotes_tags_table_create_tag (ENotesTagsTable* self,
                              const gchar* name,
                              ENotesPage* page)
{
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	sqlite3_stmt* _tmp1_;
	sqlite3_stmt* _tmp2_;
	sqlite3_stmt* _tmp3_;
	sqlite3_stmt* _tmp4_;
	sqlite3_stmt* _tmp5_;
	gint64 tag_id = 0LL;
	sqlite3_stmt* _tmp6_;
	sqlite3_stmt* _tmp16_;
	sqlite3_stmt* _tmp17_;
	sqlite3_stmt* _tmp18_;
	sqlite3_stmt* _tmp19_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (name != NULL);
	g_return_if_fail (page != NULL);
	_tmp0_ = database_table_create_stmt ("INSERT INTO Tags (name) VALUES (?)");
	stmt = _tmp0_;
	_tmp1_ = stmt;
	database_table_bind_text (_tmp1_, 1, name);
	_tmp2_ = stmt;
	sqlite3_step (_tmp2_);
	_tmp3_ = database_table_create_stmt ("SELECT id, name FROM Tags WHERE name = ?");
	_sqlite3_finalize0 (stmt);
	stmt = _tmp3_;
	_tmp4_ = stmt;
	database_table_bind_text (_tmp4_, 1, name);
	_tmp5_ = stmt;
	sqlite3_step (_tmp5_);
	_tmp6_ = stmt;
	tag_id = sqlite3_column_int64 (_tmp6_, 0);
	{
		GeeArrayList* _tag_list = NULL;
		GeeArrayList* _tmp7_;
		gint _tag_size = 0;
		GeeArrayList* _tmp8_;
		gint _tmp9_;
		gint _tmp10_;
		gint _tag_index = 0;
		_tmp7_ = enotes_tags_table_get_tags_for_page (self, page->id);
		_tag_list = _tmp7_;
		_tmp8_ = _tag_list;
		_tmp9_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp8_);
		_tmp10_ = _tmp9_;
		_tag_size = _tmp10_;
		_tag_index = -1;
		while (TRUE) {
			gint _tmp11_;
			gint _tmp12_;
			ENotesTag* tag = NULL;
			GeeArrayList* _tmp13_;
			gpointer _tmp14_;
			ENotesTag* _tmp15_;
			_tag_index = _tag_index + 1;
			_tmp11_ = _tag_index;
			_tmp12_ = _tag_size;
			if (!(_tmp11_ < _tmp12_)) {
				break;
			}
			_tmp13_ = _tag_list;
			_tmp14_ = gee_abstract_list_get ((GeeAbstractList*) _tmp13_, _tag_index);
			tag = (ENotesTag*) _tmp14_;
			_tmp15_ = tag;
			if (_tmp15_->id == tag_id) {
				_g_object_unref0 (tag);
				_g_object_unref0 (_tag_list);
				_sqlite3_finalize0 (stmt);
				return;
			}
			_g_object_unref0 (tag);
		}
		_g_object_unref0 (_tag_list);
	}
	_tmp16_ = database_table_create_stmt ("INSERT INTO TagsPage (tag_id, page_id) VALUES (?, ?)");
	_sqlite3_finalize0 (stmt);
	stmt = _tmp16_;
	_tmp17_ = stmt;
	database_table_bind_int (_tmp17_, 1, tag_id);
	_tmp18_ = stmt;
	database_table_bind_int (_tmp18_, 2, page->id);
	_tmp19_ = stmt;
	sqlite3_step (_tmp19_);
	_sqlite3_finalize0 (stmt);
}

void
enotes_tags_table_delete_tag (ENotesTagsTable* self,
                              ENotesTag* tag)
{
	sqlite3_stmt* tag_page = NULL;
	sqlite3_stmt* _tmp0_;
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (tag != NULL);
	_tmp0_ = database_table_create_stmt ("DELETE FROM TagsPage WHERE tag_id = ?");
	tag_page = _tmp0_;
	database_table_bind_int (tag_page, 1, tag->id);
	sqlite3_step (tag_page);
	_tmp1_ = database_table_create_stmt ("DELETE FROM Tags WHERE id = ?");
	stmt = _tmp1_;
	database_table_bind_int (stmt, 1, tag->id);
	sqlite3_step (stmt);
	_sqlite3_finalize0 (stmt);
	_sqlite3_finalize0 (tag_page);
}

void
enotes_tags_table_remove_tags_from_page (ENotesTagsTable* self,
                                         gint64 page_id)
{
	sqlite3_stmt* tag_page = NULL;
	sqlite3_stmt* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = database_table_create_stmt ("DELETE FROM TagsPage WHERE page_id = ?");
	tag_page = _tmp0_;
	database_table_bind_int (tag_page, 1, page_id);
	sqlite3_step (tag_page);
	_sqlite3_finalize0 (tag_page);
}

void
enotes_tags_table_remove_tag_from_page (ENotesTagsTable* self,
                                        gint64 tag_id,
                                        gint64 page_id)
{
	sqlite3_stmt* tag_page = NULL;
	sqlite3_stmt* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = database_table_create_stmt ("DELETE FROM TagsPage WHERE page_id = ? AND tag_id = ?");
	tag_page = _tmp0_;
	database_table_bind_int (tag_page, 1, page_id);
	database_table_bind_int (tag_page, 2, tag_id);
	sqlite3_step (tag_page);
	_sqlite3_finalize0 (tag_page);
}

gboolean
enotes_tags_table_save_tag (ENotesTagsTable* self,
                            ENotesTag* tag)
{
	sqlite3_stmt* tag_page = NULL;
	sqlite3_stmt* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gint res = 0;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (tag != NULL, FALSE);
	_tmp0_ = database_table_create_stmt ("UPDATE Tags SET name = ?, data = ? WHERE id = ?");
	tag_page = _tmp0_;
	_tmp1_ = tag->name;
	database_table_bind_text (tag_page, 1, _tmp1_);
	_tmp2_ = tag->data;
	database_table_bind_text (tag_page, 2, _tmp2_);
	database_table_bind_int (tag_page, 3, tag->id);
	res = sqlite3_step (tag_page);
	result = res == SQLITE_DONE;
	_sqlite3_finalize0 (tag_page);
	return result;
}

GeeArrayList*
enotes_tags_table_get_tags (ENotesTagsTable* self)
{
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	GeeArrayList* tags = NULL;
	GeeArrayList* _tmp1_;
	GeeArrayList* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = database_table_create_stmt ("SELECT id, name, data FROM Tags");
	stmt = _tmp0_;
	_tmp1_ = gee_array_list_new (ENOTES_TYPE_TAG, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	tags = _tmp1_;
	{
		gboolean _tmp2_ = FALSE;
		_tmp2_ = TRUE;
		while (TRUE) {
			gint res = 0;
			sqlite3_stmt* _tmp3_;
			ENotesTag* row = NULL;
			ENotesTag* _tmp4_;
			ENotesTag* _tmp5_;
			sqlite3_stmt* _tmp6_;
			ENotesTag* _tmp7_;
			sqlite3_stmt* _tmp8_;
			const gchar* _tmp9_;
			gchar* _tmp10_;
			ENotesTag* _tmp11_;
			sqlite3_stmt* _tmp12_;
			const gchar* _tmp13_;
			gchar* _tmp14_;
			GeeArrayList* _tmp15_;
			ENotesTag* _tmp16_;
			if (!_tmp2_) {
			}
			_tmp2_ = FALSE;
			_tmp3_ = stmt;
			res = sqlite3_step (_tmp3_);
			if (res == SQLITE_DONE) {
				break;
			} else {
				if (res != SQLITE_ROW) {
					database_table_fatal ("get_all_tags", res);
					break;
				}
			}
			_tmp4_ = enotes_tag_new ();
			row = _tmp4_;
			_tmp5_ = row;
			_tmp6_ = stmt;
			_tmp5_->id = sqlite3_column_int64 (_tmp6_, 0);
			_tmp7_ = row;
			_tmp8_ = stmt;
			_tmp9_ = (const gchar*) sqlite3_column_text (_tmp8_, 1);
			_tmp10_ = g_strdup (_tmp9_);
			_g_free0 (_tmp7_->name);
			_tmp7_->name = _tmp10_;
			_tmp11_ = row;
			_tmp12_ = stmt;
			_tmp13_ = (const gchar*) sqlite3_column_text (_tmp12_, 2);
			_tmp14_ = g_strdup (_tmp13_);
			_g_free0 (_tmp11_->data);
			_tmp11_->data = _tmp14_;
			_tmp15_ = tags;
			_tmp16_ = row;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp15_, _tmp16_);
			_g_object_unref0 (row);
		}
	}
	result = tags;
	_sqlite3_finalize0 (stmt);
	return result;
}

GeeArrayList*
enotes_tags_table_get_pages_for_tag (ENotesTagsTable* self,
                                     ENotesTag* tag)
{
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	sqlite3_stmt* _tmp1_;
	GeeArrayList* pages = NULL;
	GeeArrayList* _tmp2_;
	GeeArrayList* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (tag != NULL, NULL);
	_tmp0_ = database_table_create_stmt ("SELECT id, name, subtitle " "FROM Page p " "INNER JOIN TagsPage tp " "   ON tp.page_id = p.id " "WHERE tp.tag_id = ?");
	stmt = _tmp0_;
	_tmp1_ = stmt;
	database_table_bind_int (_tmp1_, 1, tag->id);
	_tmp2_ = gee_array_list_new (ENOTES_TYPE_PAGE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	pages = _tmp2_;
	{
		gboolean _tmp3_ = FALSE;
		_tmp3_ = TRUE;
		while (TRUE) {
			gint res = 0;
			sqlite3_stmt* _tmp4_;
			ENotesPage* row = NULL;
			ENotesPage* _tmp5_;
			ENotesPage* _tmp6_;
			sqlite3_stmt* _tmp7_;
			ENotesPage* _tmp8_;
			sqlite3_stmt* _tmp9_;
			const gchar* _tmp10_;
			gchar* _tmp11_;
			ENotesPage* _tmp12_;
			sqlite3_stmt* _tmp13_;
			const gchar* _tmp14_;
			gchar* _tmp15_;
			GeeArrayList* _tmp16_;
			ENotesPage* _tmp17_;
			if (!_tmp3_) {
			}
			_tmp3_ = FALSE;
			_tmp4_ = stmt;
			res = sqlite3_step (_tmp4_);
			if (res == SQLITE_DONE) {
				break;
			} else {
				if (res != SQLITE_ROW) {
					database_table_fatal ("get_pages_for_tag", res);
					break;
				}
			}
			_tmp5_ = enotes_page_new ();
			row = _tmp5_;
			_tmp6_ = row;
			_tmp7_ = stmt;
			_tmp6_->id = sqlite3_column_int64 (_tmp7_, 0);
			_tmp8_ = row;
			_tmp9_ = stmt;
			_tmp10_ = (const gchar*) sqlite3_column_text (_tmp9_, 1);
			_tmp11_ = g_strdup (_tmp10_);
			_g_free0 (_tmp8_->name);
			_tmp8_->name = _tmp11_;
			_tmp12_ = row;
			_tmp13_ = stmt;
			_tmp14_ = (const gchar*) sqlite3_column_text (_tmp13_, 2);
			_tmp15_ = g_strdup (_tmp14_);
			_g_free0 (_tmp12_->subtitle);
			_tmp12_->subtitle = _tmp15_;
			_tmp16_ = pages;
			_tmp17_ = row;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp16_, _tmp17_);
			_g_object_unref0 (row);
		}
	}
	result = pages;
	_sqlite3_finalize0 (stmt);
	return result;
}

void
enotes_tags_table_reset_instance (void)
{
	_database_table_unref0 (enotes_tags_table_instance);
	enotes_tags_table_instance = NULL;
}

static void
enotes_tags_table_class_init (ENotesTagsTableClass * klass,
                              gpointer klass_data)
{
	enotes_tags_table_parent_class = g_type_class_peek_parent (klass);
	((DatabaseTableClass *) klass)->finalize = enotes_tags_table_finalize;
}

static void
enotes_tags_table_instance_init (ENotesTagsTable * self,
                                 gpointer klass)
{
}

static void
enotes_tags_table_finalize (DatabaseTable * obj)
{
	ENotesTagsTable * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_TAGS_TABLE, ENotesTagsTable);
	DATABASE_TABLE_CLASS (enotes_tags_table_parent_class)->finalize (obj);
}

static GType
enotes_tags_table_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ENotesTagsTableClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) enotes_tags_table_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ENotesTagsTable), 0, (GInstanceInitFunc) enotes_tags_table_instance_init, NULL };
	GType enotes_tags_table_type_id;
	enotes_tags_table_type_id = g_type_register_static (TYPE_DATABASE_TABLE, "ENotesTagsTable", &g_define_type_info, 0);
	return enotes_tags_table_type_id;
}

GType
enotes_tags_table_get_type (void)
{
	static volatile gsize enotes_tags_table_type_id__once = 0;
	if (g_once_init_enter (&enotes_tags_table_type_id__once)) {
		GType enotes_tags_table_type_id;
		enotes_tags_table_type_id = enotes_tags_table_get_type_once ();
		g_once_init_leave (&enotes_tags_table_type_id__once, enotes_tags_table_type_id);
	}
	return enotes_tags_table_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

