2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
|
|
*
|
|
|
|
* This file is part of Hedera.
|
|
|
|
*
|
|
|
|
* Hedera 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gvn/gvn.h>
|
|
|
|
#include "db-model.h"
|
|
|
|
#include "db-row.h"
|
2014-05-29 14:30:11 +00:00
|
|
|
#include "db-model-private.c"
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
#define MODEL_NOT_READY(self) (self->priv->status != DB_MODEL_STATUS_READY)
|
2014-05-09 11:40:32 +00:00
|
|
|
#define VALID_ITER(iter, model) (iter->data && iter->stamp == model->priv->stamp)
|
|
|
|
#define DB_ROW_FIELD(row, field) (&((DbRow *) row)->value[field])
|
|
|
|
#define DB_ROW_POSITION(row) (((DbRow *) row)->position)
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION: db-model
|
|
|
|
* @Short_description: data vinculed to a SELECT query sent to the database
|
|
|
|
* @Title: DbModel
|
2013-10-14 12:08:06 +00:00
|
|
|
* @See_also: #DbConn, #DbModelHolder
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* The #DbModel class gets an SQL query statement to retrieve the data from the
|
2013-10-14 12:08:06 +00:00
|
|
|
* database connected by a #DbConn. It is normally used undirectly, using
|
|
|
|
* a #DbIterator or another #DbModelHolder instead.
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-05-09 11:40:32 +00:00
|
|
|
G_DEFINE_TYPE (DbModel, db_model, G_TYPE_OBJECT)
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DbModel:
|
|
|
|
* @data: (element-type Db.Row):
|
|
|
|
* @operation: (element-type Db.Operation):
|
|
|
|
* @row_ops: (element-type Db.Row Db.Operation):
|
|
|
|
**/
|
|
|
|
struct _DbModelPrivate
|
|
|
|
{
|
|
|
|
DbConn * conn;
|
|
|
|
SqlStmt * stmt;
|
|
|
|
gchar * sql;
|
|
|
|
gboolean use_file;
|
2014-05-09 11:40:32 +00:00
|
|
|
SqlBatch * batch;
|
2013-10-11 23:07:35 +00:00
|
|
|
GPtrArray * data;
|
|
|
|
DbColumn * column;
|
2014-05-30 09:19:53 +00:00
|
|
|
GHashTable * column_index;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbResult * result;
|
2014-05-30 09:19:53 +00:00
|
|
|
guint result_pos;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbRequest * request;
|
|
|
|
DbModelStatus status;
|
2014-09-22 17:41:32 +00:00
|
|
|
gboolean autoload;
|
2013-10-11 23:07:35 +00:00
|
|
|
gint stamp;
|
2013-10-14 12:08:06 +00:00
|
|
|
DbRow * null_row;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
GHashTable * defaults;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
gboolean fresh;
|
|
|
|
gint sort_column_id;
|
|
|
|
gint old_sort_column_id;
|
|
|
|
DbSortType order;
|
|
|
|
DbSortType old_order;
|
|
|
|
DbIterCompareFunc default_sort_func;
|
|
|
|
gpointer default_sort_data;
|
|
|
|
GDestroyNotify default_sort_destroy;
|
|
|
|
DbIterCompareFunc sort_func;
|
|
|
|
gpointer sort_data;
|
|
|
|
GDestroyNotify sort_destroy;
|
2014-05-30 09:19:53 +00:00
|
|
|
|
|
|
|
gboolean updatable_data_allocated;
|
|
|
|
DbModelUpdateFlags user_update_flags;
|
2014-06-05 15:59:35 +00:00
|
|
|
DbModelUpdateFlags update_flags;
|
|
|
|
TableInfo * main_table;
|
|
|
|
DbModelMode mode;
|
2014-05-30 09:19:53 +00:00
|
|
|
GHashTable * tables;
|
|
|
|
gint updated_col;
|
|
|
|
GQueue * operation;
|
|
|
|
GHashTable * row_ops;
|
|
|
|
GValue * updated_value;
|
|
|
|
GSList * pending_request;
|
2014-06-05 15:59:35 +00:00
|
|
|
TableInfo ** column_table;
|
|
|
|
gboolean partial_delete;
|
2014-05-30 09:19:53 +00:00
|
|
|
GSList * join;
|
2013-10-11 23:07:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Helper structures and methods
|
|
|
|
|
|
|
|
// Structures
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModel * self;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbIter * iter;
|
|
|
|
gint col;
|
|
|
|
}
|
|
|
|
JoinData;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar * schema;
|
|
|
|
gchar * table;
|
|
|
|
GPtrArray * name;
|
|
|
|
gboolean main;
|
|
|
|
}
|
|
|
|
DbModelField;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DbModelField * left;
|
|
|
|
DbModelField * right;
|
2014-06-12 13:48:53 +00:00
|
|
|
Field * master;
|
|
|
|
Field * slave;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
DbJoin;
|
|
|
|
|
2014-06-25 11:25:21 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint col;
|
|
|
|
gint order;
|
|
|
|
DbRow * null_row;
|
|
|
|
}
|
|
|
|
SortInfo;
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DB_MODEL_UNSORTED_SORT_COLUMN_ID = -2,
|
|
|
|
DB_MODEL_DEFAULT_SORT_COLUMN_ID
|
|
|
|
};
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Constructors
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_new:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @conn:(allow-none): a #DbConn
|
|
|
|
* @stmt:(allow-none): an #SqlStmt
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Returns the newly created #DbModel, filled with the data retrieved from the
|
|
|
|
* database with @stmt and through @conn.
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): a new #DbModel
|
|
|
|
**/
|
|
|
|
DbModel * db_model_new (DbConn * conn, SqlStmt * stmt)
|
|
|
|
{
|
|
|
|
return g_object_new (DB_TYPE_MODEL, "conn", conn, "stmt", stmt, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_new_with_sql:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @conn:(allow-none): a #DbConn
|
2013-10-11 23:07:35 +00:00
|
|
|
* @sql: a string containing an SQL statement
|
|
|
|
*
|
|
|
|
* Returns the newly created #DbModel, filled with the data retrieved from the
|
|
|
|
* database with @stmt and through @conn.
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): a new #DbModel
|
|
|
|
**/
|
|
|
|
DbModel * db_model_new_with_sql (DbConn * conn, const gchar * sql)
|
|
|
|
{
|
|
|
|
return g_object_new (DB_TYPE_MODEL,
|
|
|
|
"conn", conn, "use-file", FALSE, "sql", sql, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_new_with_file:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @conn:(allow-none): a #DbConn
|
2013-10-11 23:07:35 +00:00
|
|
|
* @file: the path to the file containing the SQL query to fill the model
|
|
|
|
*
|
|
|
|
* Returns a newly created #DbModel, filled with the data retrieved from @file.
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): a new #DbModel
|
|
|
|
**/
|
|
|
|
DbModel * db_model_new_with_file (DbConn * conn, const gchar * file)
|
|
|
|
{
|
|
|
|
return g_object_new (DB_TYPE_MODEL,
|
|
|
|
"conn", conn, "use-file", TRUE, "sql", file, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
|
|
|
// Prototypes
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_batch_changed (SqlBatch * batch, DbModel * self);
|
|
|
|
static void db_model_set_status (DbModel * self
|
2013-10-11 23:07:35 +00:00
|
|
|
,DbModelStatus status);
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_clear (DbModel * self);
|
|
|
|
static void db_model_free_operation (DbModel * self
|
2013-10-11 23:07:35 +00:00
|
|
|
,DbOperation * op);
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_manage_join (DbModel * self
|
2013-10-11 23:07:35 +00:00
|
|
|
,DbIter * iter
|
|
|
|
,gint col);
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
// Signal Handlers
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
STATUS_CHANGED
|
|
|
|
,LINE_INSERTED
|
|
|
|
,LINE_DELETED
|
|
|
|
,LINE_TOGGLED
|
|
|
|
,LINE_UPDATED
|
|
|
|
,LINES_REORDERED
|
|
|
|
,SORT_CHANGED
|
|
|
|
,OPERATIONS_DONE
|
|
|
|
|
|
|
|
,LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint db_model_signal[LAST_SIGNAL] = {0};
|
|
|
|
|
|
|
|
// Default signal handlers
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_line_updated (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
GValue * updated_value = self->priv->updated_value;
|
2013-10-11 23:07:35 +00:00
|
|
|
gint col;
|
|
|
|
|
|
|
|
if (!updated_value || !G_IS_VALUE (updated_value)) return;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
col = self->priv->updated_col;
|
2013-10-11 23:07:35 +00:00
|
|
|
gvn_value_ccopy (updated_value, DB_ROW_FIELD (iter->data, col));
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_manage_join (self, iter, col);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_value_unset (updated_value);
|
|
|
|
g_free (updated_value);
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->updated_value = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_line_deleted (DbModel * self, gint position)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint r_ind;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
for (r_ind = position + 1; r_ind < self->priv->result->nrows; r_ind++)
|
|
|
|
DB_ROW_POSITION (g_ptr_array_index (self->priv->data, (guint) r_ind))--;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_ptr_array_remove_index (self->priv->data, (guint) position);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->result->nrows--;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// External signal handlers
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_calculate_update_flags (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint i;
|
2014-05-29 14:30:11 +00:00
|
|
|
Table table;
|
2014-06-05 15:59:35 +00:00
|
|
|
TableInfo * tinfo;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-30 09:19:53 +00:00
|
|
|
// Allocates aditional memory when the model is updatable
|
|
|
|
|
|
|
|
if (priv->user_update_flags && !priv->updatable_data_allocated)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-30 09:19:53 +00:00
|
|
|
priv->updatable_data_allocated = TRUE;
|
|
|
|
priv->operation = g_queue_new ();
|
|
|
|
priv->row_ops = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
|
|
priv->tables = g_hash_table_new_full (
|
2014-06-05 15:59:35 +00:00
|
|
|
(GHashFunc) table_hash
|
|
|
|
,(GEqualFunc) table_equal
|
|
|
|
,(GDestroyNotify) table_free
|
|
|
|
,(GDestroyNotify) table_info_free
|
2014-05-30 09:19:53 +00:00
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 09:19:53 +00:00
|
|
|
if (priv->user_update_flags && priv->fresh && priv->result)
|
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
GArray * cols;
|
|
|
|
|
|
|
|
g_free (priv->column_table);
|
2014-05-30 09:19:53 +00:00
|
|
|
g_hash_table_remove_all (priv->tables);
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->column_table = g_new (TableInfo *, priv->result->ncols);
|
|
|
|
|
2014-05-30 09:19:53 +00:00
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
DbColumn col = priv->column[i];
|
|
|
|
|
|
|
|
if (!col.table)
|
|
|
|
{
|
|
|
|
priv->column_table[i] = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
table.name = col.table_alias;
|
|
|
|
table.schema = col.schema;
|
|
|
|
tinfo = g_hash_table_lookup (priv->tables, &table);
|
2014-05-30 09:19:53 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (!tinfo)
|
2014-05-30 09:19:53 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
tinfo = g_new (TableInfo, 1);
|
|
|
|
tinfo->name = g_strdup (col.table);
|
|
|
|
tinfo->schema = g_strdup (col.schema);
|
|
|
|
tinfo->alias = g_strdup (col.table_alias);
|
|
|
|
tinfo->columns = g_hash_table_new_full (
|
|
|
|
g_str_hash
|
|
|
|
,g_str_equal
|
|
|
|
,NULL
|
|
|
|
,(GDestroyNotify) g_array_free
|
|
|
|
);
|
|
|
|
tinfo->pkeys = NULL;
|
|
|
|
g_hash_table_insert (priv->tables, table_copy (&table), tinfo);
|
2014-05-30 09:19:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->column_table[i] = tinfo;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (col.info & DB_COLUMN_PRI_KEY)
|
|
|
|
tinfo->pkeys = g_slist_prepend (tinfo->pkeys, GINT_TO_POINTER (i));
|
|
|
|
|
|
|
|
cols = g_hash_table_lookup (tinfo->columns, col.name);
|
|
|
|
|
|
|
|
if (!cols)
|
|
|
|
{
|
|
|
|
cols = g_array_sized_new (TRUE, FALSE, sizeof (gint), 1);
|
|
|
|
g_hash_table_insert (tinfo->columns, g_strdup (col.name), cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_array_append_val (cols, i);
|
2014-05-30 09:19:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Searchs for the main table
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (priv->user_update_flags && priv->result)
|
2014-06-05 15:59:35 +00:00
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
|
|
|
if (priv->column[i].info & DB_COLUMN_PRI_KEY)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->main_table = priv->column_table[i];
|
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-30 09:19:53 +00:00
|
|
|
|
|
|
|
// Sets the updatable flags
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (priv->main_table)
|
2013-10-11 23:07:35 +00:00
|
|
|
priv->update_flags = DB_MODEL_ALL & priv->user_update_flags;
|
|
|
|
else
|
|
|
|
priv->update_flags = 0;
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
// Sets the params editable
|
|
|
|
|
|
|
|
if (priv->result)
|
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
gboolean editable = priv->main_table
|
|
|
|
&& priv->column_table[i]
|
2014-06-05 15:59:35 +00:00
|
|
|
&& priv->column_table[i]->pkeys
|
|
|
|
&& priv->update_flags & DB_MODEL_UPDATE;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
gvn_param_spec_set_editable (priv->column[i].spec, editable);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_data_ready (DbRequest * request, DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
DbResult * r;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (priv->request != request)
|
|
|
|
{
|
|
|
|
g_object_unref (request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < priv->result_pos; i++)
|
|
|
|
db_request_fetch_non_select (request, NULL);
|
|
|
|
|
|
|
|
if ((r = db_request_fetch_result (request, NULL)))
|
|
|
|
{
|
|
|
|
priv->column = r->column;
|
|
|
|
priv->data = r->data;
|
|
|
|
|
|
|
|
if (!r->data && !r->column)
|
|
|
|
{
|
|
|
|
db_result_free (r);
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_status (self, DB_MODEL_STATUS_CLEAN);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->result = r;
|
|
|
|
|
|
|
|
if (priv->fresh)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
2013-10-11 23:07:35 +00:00
|
|
|
g_hash_table_insert (priv->column_index,
|
2014-05-29 14:30:11 +00:00
|
|
|
g_strdup (priv->column[i].alias), GINT_TO_POINTER (i));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_sort_column_id (self,
|
2013-10-11 23:07:35 +00:00
|
|
|
priv->old_sort_column_id, priv->old_order);
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_calculate_update_flags (self);
|
2014-05-30 09:19:53 +00:00
|
|
|
priv->fresh = FALSE;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_status (self, DB_MODEL_STATUS_READY);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_status (self, DB_MODEL_STATUS_ERROR);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_clear_object (&priv->request);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_set_table_fields_null (DbModel * self, TableInfo * tinfo, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
gint i;
|
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
|
|
|
if (priv->column_table[i] == tinfo)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
priv->updated_col = i;
|
|
|
|
priv->updated_value =
|
|
|
|
g_value_init (g_new0 (GValue, 1), GVN_TYPE_NULL);
|
|
|
|
g_signal_emit (self, db_model_signal[LINE_UPDATED], 0, &iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_process_select (DbModel * self, DbRequest * request, DbRow * row, GError * err)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-27 07:55:01 +00:00
|
|
|
gint i, j;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2014-05-27 07:55:01 +00:00
|
|
|
DbResult * result;
|
|
|
|
DbRow * req_row;
|
|
|
|
DbIter iter;
|
2014-06-05 15:59:35 +00:00
|
|
|
Table table;
|
|
|
|
TableInfo * tinfo;
|
2014-05-27 07:55:01 +00:00
|
|
|
|
|
|
|
result = db_request_fetch_result (request, &err);
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (!(result && result->data && result->ncols > 0))
|
2014-06-05 15:59:35 +00:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
iter.data = row;
|
|
|
|
iter.stamp = priv->stamp;
|
|
|
|
req_row = g_ptr_array_index (result->data, 0);
|
|
|
|
|
|
|
|
table.name = result->column[0].table_alias;
|
|
|
|
table.schema = result->column[0].schema;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
tinfo = g_hash_table_lookup (priv->tables, &table);
|
|
|
|
|
|
|
|
if (!tinfo)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (result->nrows > 0)
|
2014-06-05 15:59:35 +00:00
|
|
|
for (i = 0; i < result->ncols; i++)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
GArray * cols = g_hash_table_lookup (tinfo->columns, result->column[i].name);
|
|
|
|
|
|
|
|
for (j = 0; j < cols->len; j++)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-30 09:19:53 +00:00
|
|
|
GValue * new_value = &req_row->value[i];
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (gvn_value_compare (new_value, DB_ROW_FIELD (row, i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
priv->updated_value = g_new0 (GValue, 1);
|
|
|
|
g_value_init (priv->updated_value, G_VALUE_TYPE (new_value));
|
|
|
|
g_value_copy (new_value, priv->updated_value);
|
|
|
|
|
|
|
|
priv->updated_col = g_array_index (cols, gint, j);
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_UPDATED], 0, &iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-12 13:48:53 +00:00
|
|
|
else
|
|
|
|
db_model_set_table_fields_null (self, tinfo, &iter);
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
exit:
|
2014-06-05 15:59:35 +00:00
|
|
|
db_result_free (result);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void db_model_on_operations_done (DbRequest * request, DbModelRequest * data)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
GList * l;
|
2014-05-27 07:55:01 +00:00
|
|
|
guint i = 0;
|
2014-05-30 09:19:53 +00:00
|
|
|
DbIter iter;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbOperation * op;
|
|
|
|
GError * err = NULL;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModel * self = data->self;
|
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
for (l = data->operations->head; l; l = l->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
op = l->data;
|
2014-06-05 15:59:35 +00:00
|
|
|
iter.stamp = priv->stamp;
|
|
|
|
iter.data = op->row;
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (op->type & DB_MODEL_ROW_OP_INSERT
|
|
|
|
&& op->type & DB_MODEL_ROW_OP_DELETE) // INSERT + DELETE
|
2014-05-29 14:30:11 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self,
|
2014-05-29 14:30:11 +00:00
|
|
|
db_model_signal[LINE_DELETED], 0, DB_ROW_POSITION (op->row));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-30 09:19:53 +00:00
|
|
|
if (!request || db_request_fetch_non_select (request, &err) == -1)
|
2014-05-29 14:30:11 +00:00
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_hash_table_remove (priv->row_ops, op->row);
|
|
|
|
|
|
|
|
if (op->type & DB_MODEL_ROW_OP_DELETE) // DELETE
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
if (!priv->partial_delete)
|
|
|
|
g_signal_emit (self,
|
2014-06-05 15:59:35 +00:00
|
|
|
db_model_signal[LINE_DELETED], 0, DB_ROW_POSITION (op->row));
|
2014-06-12 13:48:53 +00:00
|
|
|
else
|
|
|
|
db_model_set_table_fields_null (self, priv->main_table, &iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-27 07:55:01 +00:00
|
|
|
else if (op->type & DB_MODEL_ROW_OP_INSERT) // INSERT + SELECT
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_process_select (self, request, op->row, err);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-27 07:55:01 +00:00
|
|
|
else if (op->type & DB_MODEL_ROW_OP_UPDATE) // UPDATE || INSERT + SELECT
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-27 07:55:01 +00:00
|
|
|
guint j;
|
|
|
|
SqlList * list;
|
|
|
|
SqlObject * multi = sql_list_get (data->stmts, i);
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
g_object_get (multi, "stmts", &list, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
for (j = 0; j < sql_list_length (list); j++)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
if (j > 0)
|
|
|
|
db_request_fetch_non_select (request, &err);
|
|
|
|
|
|
|
|
if (G_OBJECT_TYPE (sql_list_get (list, j)) == SQL_TYPE_MULTI_STMT)
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_process_select (self, request, op->row, err);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_UPDATED], 0, &iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
i++;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 09:19:53 +00:00
|
|
|
if (request)
|
|
|
|
{
|
|
|
|
priv->pending_request =
|
|
|
|
g_slist_remove (priv->pending_request, request);
|
|
|
|
g_object_unref (request);
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (!err)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
while ((op = g_queue_pop_head (data->operations)))
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_free_operation (self, op);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[OPERATIONS_DONE], 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 17:41:32 +00:00
|
|
|
static void db_model_autorefresh (DbModel * self)
|
|
|
|
{
|
|
|
|
if (self->priv->autoload)
|
|
|
|
db_model_refresh (self);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_stmt_changed (SqlStmt * stmt, DbModel * self)
|
2014-05-29 14:30:11 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->fresh = TRUE;
|
2014-09-22 17:41:32 +00:00
|
|
|
db_model_autorefresh (self);
|
2014-05-29 14:30:11 +00:00
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
static void db_model_execute_stmt (DbModel * self)
|
|
|
|
{
|
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (priv->conn && (priv->stmt || priv->sql))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
if (priv->sql)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
SqlObject * string;
|
|
|
|
|
|
|
|
if (priv->use_file)
|
|
|
|
string = db_conn_create_stmt_from_file (priv->conn, priv->sql);
|
|
|
|
else
|
|
|
|
string = sql_string_new (priv->sql);
|
|
|
|
|
|
|
|
if (string)
|
|
|
|
self->priv->stmt = g_object_ref_sink (string);
|
|
|
|
else
|
|
|
|
return;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-20 13:46:39 +00:00
|
|
|
|
|
|
|
if (priv->stmt)
|
|
|
|
g_signal_connect (priv->stmt, "changed",
|
|
|
|
G_CALLBACK (db_model_on_stmt_changed), self);
|
|
|
|
|
|
|
|
self->priv->fresh = TRUE;
|
2014-09-22 17:41:32 +00:00
|
|
|
db_model_autorefresh (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-20 13:46:39 +00:00
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_batch_changed (SqlBatch * batch, DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-09-22 17:41:32 +00:00
|
|
|
db_model_autorefresh (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
static void db_model_on_join_query_done (DbRequest * request, JoinData * join_data)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
// db_model_process_select (self, request, row, err);
|
2014-05-29 14:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private helper methods and functions
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
static void join_data_free (JoinData * join_data)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_object_unref (join_data->self);
|
2013-10-11 23:07:35 +00:00
|
|
|
db_iter_free (join_data->iter);
|
|
|
|
g_free (join_data);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_free_operation (DbModel * self, DbOperation * op)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
if (op->updated)
|
|
|
|
g_slist_free_full (op->updated, (GDestroyNotify) db_updated_field_free);
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_hash_table_remove (self->priv->row_ops, op->row);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_free (op);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_clean_operations (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
DbOperation * op;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (priv->operation)
|
2013-10-11 23:07:35 +00:00
|
|
|
while ((op = g_queue_pop_head (priv->operation)))
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_free_operation (self, op);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void db_model_request_free (DbModelRequest * req)
|
|
|
|
{
|
|
|
|
if (req)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
DbOperation * op;
|
|
|
|
|
|
|
|
while ((op = g_queue_pop_head (req->operations)))
|
|
|
|
{
|
|
|
|
op->locked = FALSE;
|
2014-06-12 13:48:53 +00:00
|
|
|
g_queue_push_tail (req->self->priv->operation, op);
|
2014-05-29 14:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
g_queue_free (req->operations);
|
2014-06-12 13:48:53 +00:00
|
|
|
g_object_unref (req->self);
|
2014-05-27 07:55:01 +00:00
|
|
|
g_object_unref (req->stmts);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void db_operation_add_updated (DbOperation * op, gint col)
|
|
|
|
{
|
|
|
|
GSList * n;
|
|
|
|
DbUpdatedField * u;
|
|
|
|
|
|
|
|
for (n = op->updated; n; n = n->next)
|
|
|
|
if (((DbUpdatedField *) n->data)->column == col)
|
|
|
|
return;
|
|
|
|
|
|
|
|
u = g_new (DbUpdatedField, 1);
|
|
|
|
u->column = col;
|
2014-06-12 13:48:53 +00:00
|
|
|
u->value = g_value_init (g_new0 (GValue, 1)
|
|
|
|
,G_VALUE_TYPE (DB_ROW_FIELD (op->row, col)));
|
|
|
|
g_value_copy (DB_ROW_FIELD (op->row, col), u->value);
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
op->type |= DB_MODEL_ROW_OP_UPDATE;
|
|
|
|
op->updated = g_slist_prepend (op->updated, u);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static gboolean db_model_set_row_operation (DbModel * self,
|
2013-10-11 23:07:35 +00:00
|
|
|
DbRow * row, DbModelRowOp type, gint col)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbOperation * op = g_hash_table_lookup (self->priv->row_ops, row);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (!op)
|
|
|
|
{
|
|
|
|
DbOperation * new_op = g_new (DbOperation, 1);
|
|
|
|
new_op->locked = FALSE;
|
|
|
|
new_op->row = row;
|
|
|
|
new_op->updated = NULL;
|
|
|
|
new_op->type = type;
|
|
|
|
|
|
|
|
if (type & DB_MODEL_ROW_OP_UPDATE)
|
|
|
|
db_operation_add_updated (new_op, col);
|
2013-11-21 15:35:55 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_hash_table_insert (self->priv->row_ops, row, new_op);
|
|
|
|
g_queue_push_tail (self->priv->operation, new_op);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else if (!op->locked)
|
|
|
|
{
|
|
|
|
if (type & DB_MODEL_ROW_OP_DELETE)
|
|
|
|
op->type ^= DB_MODEL_ROW_OP_DELETE;
|
|
|
|
|
|
|
|
if (type & DB_MODEL_ROW_OP_UPDATE)
|
|
|
|
db_operation_add_updated (op, col);
|
|
|
|
}
|
2013-10-14 12:08:06 +00:00
|
|
|
else
|
2013-10-11 23:07:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_reverse_operations (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbOperation * op;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
while ((op = g_queue_pop_tail (priv->operation)))
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
DbIter iter;
|
|
|
|
iter.data = op->row;
|
|
|
|
iter.stamp = priv->stamp;
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
g_hash_table_remove (priv->row_ops, op->row);
|
|
|
|
|
|
|
|
if (op->type & DB_MODEL_ROW_OP_DELETE)
|
|
|
|
{
|
|
|
|
if (op->type & DB_MODEL_ROW_OP_INSERT)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self,
|
2013-10-11 23:07:35 +00:00
|
|
|
db_model_signal[LINE_DELETED], 0, DB_ROW_POSITION (op->row));
|
|
|
|
}
|
|
|
|
else
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_TOGGLED], 0, &iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else if (op->type & DB_MODEL_ROW_OP_INSERT)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self,
|
2013-10-11 23:07:35 +00:00
|
|
|
db_model_signal[LINE_DELETED], 0, DB_ROW_POSITION (op->row));
|
|
|
|
}
|
|
|
|
else if (op->type & DB_MODEL_ROW_OP_UPDATE)
|
|
|
|
{
|
|
|
|
GSList * n;
|
|
|
|
|
|
|
|
for (n = op->updated; n; n = n->next)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
DbUpdatedField * u = n->data;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
priv->updated_value = g_new0 (GValue, 1);
|
|
|
|
g_value_init (priv->updated_value, G_VALUE_TYPE (u->value));
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_copy (u->value, priv->updated_value);
|
2013-10-11 23:07:35 +00:00
|
|
|
priv->updated_col = u->column;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_UPDATED], 0, &iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_free_operation (self, op);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
// Utility functions
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Comparison between values, using case-insensitive and UTF-8 strings
|
|
|
|
*/
|
|
|
|
static gint db_model_value_compare0 (const GValue * a, const GValue * b)
|
|
|
|
{
|
|
|
|
GType a_type = G_VALUE_TYPE (a);
|
|
|
|
gboolean a_is_val = G_IS_VALUE (a);
|
|
|
|
gboolean b_is_val = G_IS_VALUE (b);
|
|
|
|
|
|
|
|
if (!(a_is_val && b_is_val))
|
|
|
|
{
|
|
|
|
if (a_is_val)
|
|
|
|
return 1;
|
|
|
|
if (b_is_val)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (a_type == G_VALUE_TYPE (b))
|
|
|
|
{
|
|
|
|
switch (a_type)
|
|
|
|
{
|
|
|
|
case G_TYPE_FLOAT:
|
|
|
|
{
|
|
|
|
gfloat aux = g_value_get_float (a) - g_value_get_float (b);
|
|
|
|
return (aux > 0.0) ? 1 : (aux < 0.0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
case G_TYPE_DOUBLE:
|
|
|
|
{
|
|
|
|
gdouble aux = g_value_get_double (a) - g_value_get_double (b);
|
|
|
|
return (aux > 0.0) ? 1 : (aux < 0.0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
case G_TYPE_INT:
|
|
|
|
return g_value_get_int (a) - g_value_get_int (b);
|
|
|
|
case G_TYPE_UINT:
|
|
|
|
return (gint) (g_value_get_uint (a) - g_value_get_uint (b));
|
|
|
|
case G_TYPE_LONG:
|
|
|
|
return (gint) (g_value_get_long (a) - g_value_get_long (b));
|
|
|
|
case G_TYPE_ULONG:
|
|
|
|
return (gint) (g_value_get_ulong (a) - g_value_get_ulong (b));
|
|
|
|
case G_TYPE_BOOLEAN:
|
|
|
|
return (gint) (g_value_get_boolean (a) - g_value_get_boolean (b));
|
|
|
|
case G_TYPE_CHAR:
|
|
|
|
return (gint) (g_value_get_schar (a) - g_value_get_schar (b));
|
|
|
|
case G_TYPE_STRING:
|
|
|
|
{
|
|
|
|
gchar * a_str = g_utf8_casefold (g_value_get_string (a), -1);
|
|
|
|
gchar * b_str = g_utf8_casefold (g_value_get_string (b), -1);
|
2013-10-14 12:08:06 +00:00
|
|
|
gint res = g_utf8_collate (a_str, b_str);
|
|
|
|
g_free (a_str);
|
|
|
|
g_free (b_str);
|
|
|
|
return res;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (a_type == G_TYPE_DATE)
|
|
|
|
return g_date_compare (g_value_get_boxed (a), g_value_get_boxed (b));
|
|
|
|
if (a_type == G_TYPE_DATE_TIME)
|
|
|
|
return g_date_time_compare (g_value_get_boxed (a), g_value_get_boxed (b));
|
|
|
|
else if (a_type == G_TYPE_BYTES)
|
|
|
|
return (gint) (g_value_get_boxed (a) - g_value_get_boxed (b));
|
|
|
|
else if (a_type == GVN_TYPE_NULL)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
g_warning ("Attempting to compare invalid types: %s\n",
|
|
|
|
g_type_name (a_type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (gvn_value_is_null (a))
|
|
|
|
return -1;
|
|
|
|
else if (gvn_value_is_null (b))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
static inline gint db_model_valcmp (gpointer * a, gpointer * b, SortInfo * info)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
gint order;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbRow * first = *a;
|
|
|
|
DbRow * second = *b;
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
if (first == info->null_row)
|
|
|
|
order = -1;
|
|
|
|
else if (second == info->null_row)
|
|
|
|
order = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gint dir = info->order == DB_SORT_ASCENDING ? 1 : -1;
|
|
|
|
order = dir * db_model_value_compare0 (&first->value[info->col]
|
|
|
|
,&second->value[info->col]);
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
return order;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_set_status (DbModel * self, DbModelStatus status)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->status = status;
|
|
|
|
g_signal_emit (self, db_model_signal[STATUS_CHANGED], 0, status);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_cancel_pending_requests (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
GSList * n;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
for (n = self->priv->pending_request; n; n = n->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
db_request_cancel (n->data);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_add_pending_request (DbModel * self, DbRequest * request)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->pending_request = g_slist_prepend (self->priv->pending_request,
|
2013-10-11 23:07:35 +00:00
|
|
|
request);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_clear (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
if (priv->request)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
db_request_cancel (priv->request);
|
|
|
|
priv->request = NULL;
|
|
|
|
}
|
|
|
|
else if (priv->result)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_clean_operations (self);
|
|
|
|
db_model_cancel_pending_requests (self);
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
db_result_free (priv->result);
|
|
|
|
priv->result = NULL;
|
|
|
|
priv->column = NULL;
|
|
|
|
priv->data = NULL;
|
|
|
|
|
|
|
|
priv->old_order = priv->order;
|
|
|
|
priv->old_sort_column_id = priv->sort_column_id;
|
|
|
|
priv->sort_column_id = DB_MODEL_UNSORTED_SORT_COLUMN_ID;
|
|
|
|
priv->stamp = g_random_int ();
|
|
|
|
|
|
|
|
priv->main_table = NULL;
|
|
|
|
priv->update_flags = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join related functions
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_manage_join (DbModel * self, DbIter * iter, gint col)
|
2014-06-05 15:59:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (priv->join
|
2014-06-05 15:59:35 +00:00
|
|
|
&& gvn_param_spec_get_editable (priv->column[col].spec)
|
|
|
|
&& !gvn_value_is_null (DB_ROW_FIELD (iter->data, col)))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
GSList * n;
|
|
|
|
gboolean send_request = FALSE, end = FALSE;
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlList * stmts = g_object_ref_sink (sql_list_new (SQL_TYPE_MULTI_STMT));
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
if (!end)
|
|
|
|
for (n = priv->join; n; n = n->next)
|
|
|
|
{
|
|
|
|
gint j;
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlObject * where, * select;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbModelField * main_field = NULL, * other_field = NULL;
|
2014-06-05 15:59:35 +00:00
|
|
|
DbJoin * join = n->data;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (join->left->main)
|
|
|
|
{
|
|
|
|
main_field = join->left;
|
|
|
|
other_field = join->right;
|
|
|
|
}
|
|
|
|
else if (join->right->main)
|
|
|
|
{
|
|
|
|
main_field = join->right;
|
|
|
|
other_field = join->left;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < main_field->name->len; i++)
|
|
|
|
if (!g_strcmp0 (priv->column[col].table, main_field->table)
|
|
|
|
&& !g_strcmp0 (priv->column[col].name,
|
|
|
|
g_ptr_array_index (main_field->name, i)))
|
|
|
|
{
|
|
|
|
send_request = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!send_request)
|
|
|
|
continue; // Continue to the next DbJoin in the list
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
select = sql_select_new ();
|
|
|
|
|
|
|
|
sql_object_add_child (select, "targets", sql_table_new (other_field->table, NULL));
|
|
|
|
|
|
|
|
where = sql_operation_new (SQL_OPERATION_TYPE_AND);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
|
|
|
if (!g_strcmp0 (priv->column[i].table, other_field->table))
|
2014-06-05 15:59:35 +00:00
|
|
|
{
|
|
|
|
sql_object_add_child (select, "fields",
|
|
|
|
sql_field_new_with_target (priv->column[i].name, other_field->table, NULL));
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
else if (!g_strcmp0 (priv->column[i].table, main_field->table))
|
2014-06-05 15:59:35 +00:00
|
|
|
{
|
|
|
|
for (j = 0; j < main_field->name->len; j++)
|
2013-10-11 23:07:35 +00:00
|
|
|
if (!g_strcmp0 (priv->column[i].name,
|
2014-06-05 15:59:35 +00:00
|
|
|
g_ptr_array_index (main_field->name, j)))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlObject * equal = sql_operation_new (SQL_OPERATION_TYPE_EQUAL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_add_child (equal, "operators",
|
|
|
|
sql_field_new_with_target (g_ptr_array_index (other_field->name, j)
|
|
|
|
,other_field->table, NULL));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_add_child (equal, "operators",
|
|
|
|
sql_value_new_with_value (DB_ROW_FIELD (iter->data, i)));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_add_child (where, "operators", equal);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_object_set (select, "where", where);
|
|
|
|
sql_list_add (stmts, select);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (send_request)
|
|
|
|
{
|
|
|
|
JoinData * join_data;
|
|
|
|
DbRequest * request;
|
|
|
|
|
|
|
|
join_data = g_new (JoinData, 1);
|
2014-06-12 13:48:53 +00:00
|
|
|
join_data->self = g_object_ref (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
join_data->iter = db_iter_copy (iter);
|
|
|
|
join_data->col = col;
|
|
|
|
|
|
|
|
request = db_conn_query_with_stmt_async (priv->conn
|
2014-06-05 15:59:35 +00:00
|
|
|
,g_object_new (SQL_TYPE_MULTI_STMT, "stmts", stmts, NULL)
|
|
|
|
,NULL
|
2013-10-11 23:07:35 +00:00
|
|
|
,(DbRequestDoneCallback) db_model_on_join_query_done
|
|
|
|
,join_data
|
|
|
|
,(GDestroyNotify) join_data_free
|
|
|
|
);
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_add_pending_request (self, request);
|
2014-06-05 15:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (stmts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
static DbModelField * db_model_field_new (const gchar * table, const gchar * schema)
|
|
|
|
{
|
|
|
|
DbModelField * field = g_new (DbModelField, 1);
|
|
|
|
field->schema = g_strdup (schema);
|
|
|
|
field->table = g_strdup (table);
|
|
|
|
field->name = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
|
|
|
|
field->main = FALSE;
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void db_model_field_free (DbModelField * field)
|
|
|
|
{
|
|
|
|
if (field)
|
|
|
|
{
|
|
|
|
if (field->schema)
|
|
|
|
g_free (field->schema);
|
|
|
|
if (field->table)
|
|
|
|
g_free (field->table);
|
|
|
|
if (field->name)
|
|
|
|
g_ptr_array_free (field->name, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (field);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void db_join_free (DbJoin * join)
|
|
|
|
{
|
|
|
|
if (join)
|
|
|
|
{
|
|
|
|
if (join->left)
|
|
|
|
db_model_field_free (join->left);
|
|
|
|
if (join->right)
|
|
|
|
db_model_field_free (join->right);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (join);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_calculate_col_def (DbModel * self, SqlJoin * join,
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlField * l_field, SqlField * r_field)
|
|
|
|
{
|
|
|
|
gint i, col = -1;
|
|
|
|
gchar * dst = NULL;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlField * f = NULL;
|
|
|
|
SqlTarget * l_table = join->target_left, * r_table = join->target_right;
|
|
|
|
|
|
|
|
for (i = 0; i < priv->result->ncols; i++)
|
|
|
|
{
|
|
|
|
f = NULL;
|
|
|
|
|
|
|
|
if (!g_strcmp0 (priv->column[i].name, l_field->name))
|
|
|
|
{
|
|
|
|
f = l_field;
|
|
|
|
dst = (join->type == SQL_JOIN_TYPE_RIGHT) ?
|
2013-10-11 23:07:35 +00:00
|
|
|
l_field->name : r_field->name;
|
|
|
|
}
|
|
|
|
else if (!g_strcmp0 (priv->column[i].name, r_field->name))
|
|
|
|
{
|
|
|
|
f = r_field;
|
|
|
|
dst = (join->type == SQL_JOIN_TYPE_LEFT) ?
|
|
|
|
l_field->name : r_field->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f)
|
|
|
|
{//TODO add schema checks
|
|
|
|
if (f->target)
|
|
|
|
{
|
|
|
|
if (!g_strcmp0 (priv->column[i].table, f->target)
|
|
|
|
|| (!g_strcmp0 (priv->column[i].table, SQL_TABLE (l_table)->name)
|
|
|
|
&& !g_strcmp0 (f->target, l_table->alias)))
|
|
|
|
{
|
|
|
|
col = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!g_strcmp0 (priv->column[i].table, SQL_TABLE (r_table)->name)
|
|
|
|
&& !g_strcmp0 (f->target, r_table->alias))
|
|
|
|
{
|
|
|
|
col = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
col = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f)
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_default_value_from_column (self, dst, col);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_set_join_fields (DbModel * self, SqlJoin * join,
|
2013-10-11 23:07:35 +00:00
|
|
|
SqlField * lsql_field, SqlField * rsql_field,
|
|
|
|
DbModelField * lfield, DbModelField * rfield)
|
|
|
|
{
|
|
|
|
gboolean check;
|
|
|
|
SqlTarget * ltarget = join->target_left, * rtarget = join->target_right;
|
|
|
|
|
|
|
|
check = !g_strcmp0 (lfield->schema, lsql_field->schema)
|
|
|
|
|| !g_strcmp0 (lfield->table, lsql_field->target)
|
|
|
|
|| !g_strcmp0 (ltarget->alias, lsql_field->target)
|
|
|
|
|| !g_strcmp0 (rfield->schema, rfield->schema)
|
|
|
|
|| !g_strcmp0 (rfield->table, rsql_field->target)
|
|
|
|
|| !g_strcmp0 (rtarget->alias, rsql_field->target);
|
|
|
|
|
|
|
|
g_ptr_array_add (lfield->name,
|
|
|
|
g_strdup (check ? lsql_field->name : rsql_field->name));
|
|
|
|
g_ptr_array_add (rfield->name,
|
|
|
|
g_strdup (check ? rsql_field->name : lsql_field->name));
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static gboolean db_model_analyse_join_op (DbModel * self, SqlOperation * op)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
gint i;
|
|
|
|
gint noperands;
|
|
|
|
SqlList * operands;
|
|
|
|
SqlOperationType operator = sql_operation_get_operator (op);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
noperands = sql_list_length (operands);
|
|
|
|
g_object_get (op, "operands", &operands, NULL);
|
|
|
|
|
|
|
|
if (operator == SQL_OPERATION_TYPE_EQUAL && noperands == 2)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
gpointer field1 = sql_list_get (operands, 0);
|
|
|
|
gpointer field2 = sql_list_get (operands, 1);
|
|
|
|
|
|
|
|
if (!SQL_IS_FIELD (field1) || !SQL_IS_FIELD (field2)
|
|
|
|
|| (g_strcmp0 (sql_field_get_target (field1), sql_field_get_target (field2))
|
|
|
|
&& g_strcmp0 (sql_field_get_schema (field1), sql_field_get_schema (field2))))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
else if (operator == SQL_OPERATION_TYPE_AND)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
for (i = 0; i < noperands; i++)
|
2014-06-12 13:48:53 +00:00
|
|
|
if (!db_model_analyse_join_op (self, sql_list_get (operands, i)))
|
2014-06-05 15:59:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_analyse_join (DbModel * self, SqlTarget * target)
|
2014-06-05 15:59:35 +00:00
|
|
|
{
|
|
|
|
SqlJoin * join;
|
|
|
|
SqlTarget * left;
|
|
|
|
SqlTarget * right;
|
|
|
|
SqlOperation * on;
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (!SQL_IS_JOIN (target))
|
|
|
|
return;
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
join = SQL_JOIN (target);
|
|
|
|
g_object_get (join
|
|
|
|
,"target-left", &left
|
|
|
|
,"target-right", &right
|
|
|
|
,"condition", &on
|
|
|
|
,NULL
|
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (SQL_IS_TABLE (left) || SQL_IS_TABLE (right))
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_analyse_join_op (self, on);
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
switch ((gint) sql_join_get_join_type (join))
|
|
|
|
{
|
|
|
|
case SQL_JOIN_TYPE_INNER:
|
|
|
|
break;
|
|
|
|
case SQL_JOIN_TYPE_LEFT:
|
|
|
|
break;
|
|
|
|
case SQL_JOIN_TYPE_RIGHT:
|
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_analyse_join (self, left);
|
|
|
|
db_model_analyse_join (self, right);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_load_join (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
gchar * sql;
|
|
|
|
SqlObject * stmt;
|
|
|
|
SqlObject * select;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2014-06-05 15:59:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
sql = db_conn_render (self->priv->conn, self->priv->stmt, NULL, NULL);
|
2014-06-05 15:59:35 +00:00
|
|
|
stmt = sql_parser_parse (sql);
|
|
|
|
g_free (sql);
|
|
|
|
|
|
|
|
if (!stmt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_object_ref_sink (stmt);
|
|
|
|
|
|
|
|
if (SQL_IS_MULTI_STMT (stmt))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlList * stmts;
|
|
|
|
g_object_get (stmt, "stmts", &stmts, NULL);
|
|
|
|
select = sql_list_get (stmts, priv->result_pos);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
else
|
|
|
|
select = stmt;
|
|
|
|
|
|
|
|
if (!SQL_IS_SELECT (select))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
gint i;
|
|
|
|
SqlList * targets;
|
|
|
|
|
|
|
|
g_object_get (select, "targets", &targets, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < sql_list_length (targets); i++)
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_analyse_join (self, sql_list_get (targets, i));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
exit:
|
|
|
|
g_object_unref (stmt);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
*/
|
2013-10-11 23:07:35 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
// Setters & getters
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_conn:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @conn: (allow-none): a #DbConn
|
|
|
|
*
|
|
|
|
* Sets the connection through which the communication is established with
|
|
|
|
* the database.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_conn (DbModel * self, DbConn * conn)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (DB_IS_CONN (conn) || !conn);
|
|
|
|
|
|
|
|
if (conn)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
if (!self->priv->conn)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->conn = g_object_ref (conn);
|
2014-06-20 13:46:39 +00:00
|
|
|
db_model_execute_stmt (self);
|
|
|
|
// db_model_on_stmt_changed (self->priv->stmt, self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
g_warning ("DbModel: The connection can only be set once");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_conn:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Returns the connection through which the communication is established with
|
|
|
|
* the database.
|
|
|
|
*
|
|
|
|
* Return value: (transfer none): the #DbConn of the model
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
DbConn * db_model_get_conn (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
return self->priv->conn;
|
2013-10-14 12:08:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
/**
|
|
|
|
* db_model_get_spec:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
|
|
|
* @col: the number of a column of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Returns the #GvnParamSpec of a field in the position @col of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: (transfer none): the #GvnParamSpec of the column with number @col
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
const GvnParamSpec * db_model_get_spec (DbModel * self, gint col)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if ((self->priv->result && 0 <= col
|
|
|
|
&& col < self->priv->result->ncols)
|
|
|
|
&& self->priv->column)
|
|
|
|
return self->priv->column[col].spec;
|
2013-10-11 23:07:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_column_name:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
|
|
|
* @col: the number of a column of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Retrieves the name of a field in the position @col of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: the name of the column with number @col
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
const gchar * db_model_get_column_name (DbModel * self, gint col)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if ((self->priv->result
|
2013-10-11 23:07:35 +00:00
|
|
|
&& 0 <= col
|
2014-06-12 13:48:53 +00:00
|
|
|
&& col < self->priv->result->ncols)
|
|
|
|
&& self->priv->column)
|
|
|
|
return self->priv->column[col].alias;
|
2014-07-09 07:49:56 +00:00
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_column_index:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
|
|
|
* @name: the name of a column of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Retrieves the position in the query of the column called @name.
|
|
|
|
*
|
|
|
|
* Return value: the index of the column with name @name or -1 if there isn't
|
|
|
|
* a column called @name or if @name is NULL
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gint db_model_get_column_index (DbModel * self, const gchar * name)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-09 07:49:56 +00:00
|
|
|
gpointer column;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), -1);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-07-09 07:49:56 +00:00
|
|
|
if (name && self->priv->column_index
|
|
|
|
&& g_hash_table_lookup_extended (self->priv->column_index, name, NULL, &column))
|
|
|
|
return GPOINTER_TO_INT (column);
|
2014-09-08 15:11:01 +00:00
|
|
|
else
|
|
|
|
g_warning ("DbModel: Column '%s' doesn't exists", name);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-07-09 07:49:56 +00:00
|
|
|
return -1;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_status:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Returns the current #DbModelStatus of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Return value: the status of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelStatus db_model_get_status (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), -1);
|
|
|
|
return self->priv->status;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
/**
|
|
|
|
* db_model_is_ready:
|
|
|
|
* @self: a #DbModel
|
|
|
|
*
|
|
|
|
* Checks if the model is ready.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if the model is ready, %FALSE otherwise
|
|
|
|
**/
|
|
|
|
gboolean db_model_is_ready (DbModel * self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
|
|
|
return self->priv->status == DB_MODEL_STATUS_READY;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
/**
|
|
|
|
* db_model_get_mode:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Retrieves the current working mode of @self. See #DbModelMode.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: the current mode
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelMode db_model_get_mode (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), -1);
|
|
|
|
return (self->priv->mode);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_mode:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @mode: a #DbModelMode to set the model on
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets the working mode of @self to @mode. See #DbModelMode.
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_mode (DbModel * self, DbModelMode mode)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
|
|
|
self->priv->mode = mode;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_toggle_mode:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Toogles the working mode of @self. See #DbModelMode to see the two possible
|
2013-10-11 23:07:35 +00:00
|
|
|
* modes.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_toggle_mode (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->mode = (self->priv->mode == DB_MODEL_MODE_ON_DEMAND)?
|
2013-10-11 23:07:35 +00:00
|
|
|
DB_MODEL_MODE_ON_CHANGE:
|
|
|
|
DB_MODEL_MODE_ON_DEMAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_result_pos:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @pos: position of the query
|
|
|
|
*
|
|
|
|
* Sets the position where the query that will fill @model with data will be
|
|
|
|
* placed in a multi-query statement.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_result_pos (DbModel * self, guint pos)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->result_pos = pos;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_update_flags:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Retrieves the update flags of @self. See #DbModelUpdateFlags for the details
|
2013-10-11 23:07:35 +00:00
|
|
|
* of those flags. See also db_model_request_update_flags().
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Return value: the #DbModelUpdateFlags of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelUpdateFlags db_model_get_update_flags (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
return self->priv->update_flags;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_request_update_flags:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
|
|
|
* @flags: the set of #DbModelUpdateFlags to be set to @self
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Requests the update flags of @self. See #DbModelUpdateFlags for the details
|
|
|
|
* of those flags. If @self does not allow an operation by itself, it will
|
2013-10-11 23:07:35 +00:00
|
|
|
* log a warning when trying to set this operation. If the flag set is
|
|
|
|
* #DB_MODEL_UPDATE it may not log until trying to update a non-updatable
|
|
|
|
* field, see db_model_set_value(). See also db_model_get_update_flags() and
|
|
|
|
* db_model_unset_update_flags().
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_request_update_flags (DbModel * self, DbModelUpdateFlags flags)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->user_update_flags = flags;
|
|
|
|
db_model_calculate_update_flags (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_unset_update_flags:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
|
|
|
* @flags: the set of #DbModelUpdateFlags to be unset in @self
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Unsets the update flags of @self. See #DbModelUpdateFlags for the details
|
2013-10-11 23:07:35 +00:00
|
|
|
* of those flags. See also db_model_get_update_flags() and
|
|
|
|
* db_model_request_update_flags().
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_unset_update_flags (DbModel * self, DbModelUpdateFlags flags)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->user_update_flags &= ~flags;
|
|
|
|
db_model_calculate_update_flags (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_last:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a @DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: (out): an unitialized #DbIter
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Points @iter to the last row of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Return value: %FALSE if @self is a valid #DbModel, %TRUE otherwise
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_get_last (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
iter->data = g_ptr_array_index
|
2014-06-12 13:48:53 +00:00
|
|
|
(self->priv->data, (guint) self->priv->data->len - 1);
|
|
|
|
iter->stamp = self->priv->stamp;
|
2013-10-11 23:07:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-12 13:48:53 +00:00
|
|
|
* db_model_add_join:
|
|
|
|
* @self: a #DbModel
|
|
|
|
* @master_field: the field on the left of the join
|
|
|
|
* @slave_field: the field on the right of the join
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Sets the binding betwen two joined fields. This will generate a SELECT query
|
|
|
|
* to the database to change the corresponding values in the model. Currently
|
|
|
|
* it's used by the internal parser.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_add_join (DbModel * self,
|
|
|
|
const gchar * master_field, const gchar * slave_field)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbJoin * join;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
|
|
|
g_return_if_fail (master_field);
|
|
|
|
g_return_if_fail (slave_field);
|
|
|
|
|
|
|
|
join = g_new (DbJoin, 1);
|
|
|
|
join->master = field_new_from_string (master_field);
|
|
|
|
join->slave = field_new_from_string (slave_field);
|
|
|
|
self->priv->join = g_slist_prepend (self->priv->join, join);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 11:40:32 +00:00
|
|
|
* db_model_get_batch:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-20 13:46:39 +00:00
|
|
|
* Gets the batch used by the model.
|
|
|
|
*
|
|
|
|
* Return value: (transfer none): the #SqlBatch
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
SqlBatch * db_model_get_batch (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
return self->priv->batch;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 11:40:32 +00:00
|
|
|
* db_model_set_batch:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2014-05-09 11:40:32 +00:00
|
|
|
* @batch: the #SqlBatch
|
2013-10-14 12:08:06 +00:00
|
|
|
*
|
2014-05-09 11:40:32 +00:00
|
|
|
* Sets the batch used by the model.
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_batch (DbModel * self, SqlBatch * batch)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
DbModelPrivate * priv;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2014-05-09 11:40:32 +00:00
|
|
|
g_return_if_fail (SQL_IS_BATCH (batch) || !batch);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
if (priv->batch)
|
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (priv->batch,
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_on_batch_changed, self);
|
2014-07-22 10:09:54 +00:00
|
|
|
g_clear_object (&priv->batch);
|
2014-05-09 11:40:32 +00:00
|
|
|
}
|
|
|
|
if (batch)
|
|
|
|
{
|
2014-07-22 10:09:54 +00:00
|
|
|
priv->batch = g_object_ref_sink (batch);
|
2014-05-09 11:40:32 +00:00
|
|
|
g_signal_connect (batch, "changed",
|
2014-06-12 13:48:53 +00:00
|
|
|
G_CALLBACK (db_model_on_batch_changed), self);
|
2014-07-22 10:09:54 +00:00
|
|
|
db_model_on_batch_changed (NULL, self);
|
2014-05-09 11:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static ColumnDef * db_model_create_column_def (DbModel * self, const gchar * field_str)
|
2014-06-05 15:59:35 +00:00
|
|
|
{
|
|
|
|
Field * field = field_new_from_string (field_str);
|
|
|
|
|
|
|
|
if (field->name && field->target && !field->schema)
|
|
|
|
{
|
|
|
|
ColumnDef * def = column_def_new ();
|
2014-06-12 13:48:53 +00:00
|
|
|
g_hash_table_insert (self->priv->defaults, field, def);
|
2014-06-05 15:59:35 +00:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
g_warning ("DbModel: Field string should specify name and table alias: %s", field_str);
|
2014-06-05 15:59:35 +00:00
|
|
|
field_free (field);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_on_param_changed (GvnParam * param, GValue * value, DbModel * self)
|
2014-06-05 15:59:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_refresh (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 11:40:32 +00:00
|
|
|
* db_model_set_default_value_from_column:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2014-05-29 14:30:11 +00:00
|
|
|
* @field_str: the field to be set
|
|
|
|
* @column_str: field from wich the value is picked
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_default_value_from_column (DbModel * self,
|
2014-05-29 14:30:11 +00:00
|
|
|
const gchar * field_str, const gchar * column_str)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
ColumnDef * def;
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2014-05-29 14:30:11 +00:00
|
|
|
g_return_if_fail (field_str);
|
|
|
|
g_return_if_fail (column_str);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
def = db_model_create_column_def (self, field_str);
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
if (def)
|
|
|
|
def->src_column = field_new_from_string (column_str);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 11:40:32 +00:00
|
|
|
* db_model_set_default_value_from_param:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2014-05-29 14:30:11 +00:00
|
|
|
* @field_str: the field to be set
|
2014-05-09 11:40:32 +00:00
|
|
|
* @param: a #GvnParam
|
2014-06-20 13:46:39 +00:00
|
|
|
* @link: whether to refresh when the parameter changes
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Get the default value for @field_str from @param.
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_default_value_from_param (DbModel * self,
|
2014-05-29 14:30:11 +00:00
|
|
|
const gchar * field_str, GvnParam * param, gboolean link)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
ColumnDef * def;
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2014-05-29 14:30:11 +00:00
|
|
|
g_return_if_fail (field_str);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (GVN_IS_PARAM (param));
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
def = db_model_create_column_def (self, field_str);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (!def)
|
|
|
|
return;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
def->param = g_object_ref_sink (param);
|
|
|
|
|
|
|
|
if (link)
|
|
|
|
def->link = g_signal_connect (param, "value-changed",
|
2014-06-12 13:48:53 +00:00
|
|
|
G_CALLBACK (db_model_on_param_changed), self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_main_table:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Returns the string with the name of the main table of @self. The main table
|
2013-10-11 23:07:35 +00:00
|
|
|
* is the only table on a #DbModel whose rows can be deleted or inserted. By
|
|
|
|
* default the main table is set to the table the first column field belongs.
|
|
|
|
* To override this behaviour, use @db_model_request_main_table.
|
|
|
|
*
|
|
|
|
* Note that the requested main table may be different fron the actual main
|
|
|
|
* table used by the model
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Return value: the name of the main table of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
const gchar * db_model_get_main_table (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), NULL);
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->main_table)
|
|
|
|
return self->priv->main_table->name;
|
2014-05-29 14:30:11 +00:00
|
|
|
|
|
|
|
return NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_stmt:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Returns the #SqlStmt which queries to the database about the data of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Return value: (transfer none): the #SqlStmt property of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
const SqlStmt * db_model_get_stmt (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
return self->priv->stmt;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_stmt:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @stmt: the #SqlStmt
|
|
|
|
*
|
|
|
|
* Sets the "stmt" property of the model.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_stmt (DbModel * self, SqlStmt * stmt)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
if (!stmt)
|
|
|
|
return;
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (!self->priv->stmt);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (SQL_IS_STRING (stmt) || SQL_IS_SELECT (stmt));
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->stmt = g_object_ref_sink (stmt);
|
2014-06-20 13:46:39 +00:00
|
|
|
db_model_execute_stmt (self);
|
|
|
|
/* g_signal_connect (stmt, "changed", G_CALLBACK (db_model_on_stmt_changed), self);
|
|
|
|
db_model_on_stmt_changed (stmt, self);*/
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_sql:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @sql: a value for the "sql property
|
|
|
|
*
|
|
|
|
* Sets the "sql" property to @sql.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_sql (DbModel * self, const gchar * sql)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
// SqlObject * string;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (!sql)
|
|
|
|
return;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_free (self->priv->sql);
|
|
|
|
self->priv->sql = g_strdup (sql);
|
2014-06-20 13:46:39 +00:00
|
|
|
db_model_execute_stmt (self);
|
|
|
|
/* if (self->priv->use_file)
|
2014-06-12 13:48:53 +00:00
|
|
|
string = db_conn_create_stmt_from_file (self->priv->conn, sql);
|
2013-10-11 23:07:35 +00:00
|
|
|
else
|
|
|
|
string = sql_string_new (sql);
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
db_model_set_stmt (self, SQL_STMT (string)); */
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
* @...: (out callee-allocates): pairs of column number and value return
|
|
|
|
* locations, terminated by -1
|
|
|
|
*
|
|
|
|
* Gets the values on the specified columns and sets the return locations
|
|
|
|
* pointing to these values. The column numbers must be integers, while the
|
|
|
|
* return locations must be of the same type of the value being returned.
|
|
|
|
*
|
|
|
|
* Returned values of type G_TYPE_OBJECT have to be unreferenced, values of
|
|
|
|
* type G_TYPE_STRING or G_TYPE_BOXED have to be freed. Other values are passed
|
|
|
|
* by value.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_get (DbModel * self, DbIter * iter, ...)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
gint column;
|
|
|
|
GValue * val;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
|
|
|
g_return_if_fail (self->priv->result);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (iter != NULL);
|
|
|
|
|
|
|
|
va_start (va, iter);
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
while ((column = (va_arg (va, gint))) >= 0 && column < self->priv->result->ncols)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
val = DB_ROW_FIELD (iter->data, column);
|
|
|
|
|
|
|
|
if (gvn_value_is_null (val))
|
2014-06-12 13:48:53 +00:00
|
|
|
switch (gvn_param_spec_get_gtype (self->priv->column[column].spec))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
case G_TYPE_CHAR:
|
|
|
|
*va_arg (va, gchar*) = 0;
|
|
|
|
break;
|
|
|
|
case G_TYPE_INT:
|
|
|
|
*va_arg (va, gint*) = 0;
|
|
|
|
break;
|
|
|
|
case G_TYPE_LONG:
|
|
|
|
*va_arg (va, glong*) = 0;
|
|
|
|
break;
|
|
|
|
case G_TYPE_FLOAT:
|
|
|
|
*va_arg (va, gfloat*) = 0;
|
|
|
|
break;
|
|
|
|
case G_TYPE_DOUBLE:
|
|
|
|
*va_arg (va, gdouble*) = 0;
|
|
|
|
break;
|
|
|
|
default: // Objects or chararrays.
|
|
|
|
*va_arg (va, gpointer*) = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gvn_value_get_valist (val, va);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end (va);
|
|
|
|
g_return_if_fail (column == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
* @...: pairs of column number and value, terminated by -1
|
|
|
|
*
|
|
|
|
* Sets the values on the specified columns. The column numbers must be
|
|
|
|
* integers, while the values must be of the same type of the value being set.
|
|
|
|
*
|
|
|
|
* The value will be referenced by the model if it is a G_TYPE_OBJECT, and it
|
|
|
|
* will be copied if it is a G_TYPE_STRING or G_TYPE_BOXED.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set (DbModel * self, DbIter * iter, ...)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint column;
|
|
|
|
gpointer content;
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue val = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
va_list va;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
|
|
|
g_return_if_fail (self->priv->result);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (iter != NULL);
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (iter->data == self->priv->null_row)
|
2013-10-14 12:08:06 +00:00
|
|
|
return;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
va_start (va, iter);
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
while ((column = (va_arg (va, gint))) >= 0 && column < self->priv->result->ncols)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
content = va_arg (va, gpointer);
|
|
|
|
gvn_value_new_with_content (&val,
|
2014-06-12 13:48:53 +00:00
|
|
|
gvn_param_spec_get_gtype (self->priv->column[column].spec), content);
|
|
|
|
db_model_set_value (self, iter, column, &val, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_value_unset (&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end (va);
|
|
|
|
g_return_if_fail (column == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_value:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
|
|
|
* @iter: a #DbIter pointing to a row of @self
|
2013-10-11 23:07:35 +00:00
|
|
|
* @col: the number of the field to get the value
|
|
|
|
* @err: (out) (allow-none): a #GError or %NULL to ignore errors
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Gets a value from @self pointed to by @iter and @col and puts it in @err.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: the value pointed to by @iter and @col or %NULL in case of error
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
const GValue * db_model_get_value (DbModel * self, DbIter * iter, gint col, GError ** err)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), NULL);
|
|
|
|
g_return_val_if_fail (self->priv->data && self->priv->result, NULL);
|
|
|
|
g_return_val_if_fail (VALID_ITER (iter, self), NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY (self))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_set_error (err
|
|
|
|
,DB_MODEL_LOG_DOMAIN
|
|
|
|
,DB_MODEL_ERROR_NOT_READY
|
|
|
|
,"The model is not ready");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (0 > col || col >= self->priv->result->ncols)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_set_error (err
|
|
|
|
,DB_MODEL_LOG_DOMAIN
|
|
|
|
,DB_MODEL_ERROR_OUT_OF_RANGE
|
|
|
|
,"Column out of range");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DB_ROW_FIELD (iter->data, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_value:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter pointing to the row to be set
|
|
|
|
* @col: the column of the field to be set
|
|
|
|
* @value: new value for the field pointed to by @iter and @col
|
2013-12-13 09:07:52 +00:00
|
|
|
* @err: (out) (allow-none): a return location for a #GError or %NULL to ignore
|
|
|
|
* errors
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets the value of a single field to @value on @self as well as on the database.
|
2013-10-11 23:07:35 +00:00
|
|
|
* If the database update fails, the model is not updated and err is set
|
|
|
|
* if it's not %NULL.
|
|
|
|
* If @iter is pointing a new row (inserted by db_model_insert() but not yet
|
|
|
|
* commited with db_model_perform_operations()) db_model_set_value() will only set
|
|
|
|
* the value on the model.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE on success, %FALSE otherwise
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_set_value (DbModel * self, DbIter * iter, gint col, const GValue * value, GError ** err)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
|
|
|
g_return_val_if_fail (VALID_ITER (iter, self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY (self) || !priv->result)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_set_error (err, DB_MODEL_LOG_DOMAIN
|
|
|
|
,DB_MODEL_ERROR_NOT_READY, "Model not ready");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue new_value = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbRow * row = iter->data;
|
|
|
|
GvnParamSpec * spec = priv->column[col].spec;
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
g_return_val_if_fail (0 <= col && col < priv->result->ncols, FALSE);
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (iter->data == self->priv->null_row)
|
2013-10-14 12:08:06 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
if (!gvn_param_spec_validate (spec, value, err))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!gvn_value_is_null (value))
|
|
|
|
{
|
|
|
|
g_value_init (&new_value, gvn_param_spec_get_gtype (spec));
|
|
|
|
g_value_transform (value, &new_value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_value_init (&new_value, GVN_TYPE_NULL);
|
|
|
|
|
|
|
|
if (gvn_value_compare (&new_value, DB_ROW_FIELD (iter->data, col)))
|
|
|
|
{
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2014-06-12 13:48:53 +00:00
|
|
|
else if (!db_model_set_row_operation (self, row, DB_MODEL_ROW_OP_UPDATE, col))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_set_error (err, DB_MODEL_LOG_DOMAIN
|
|
|
|
,DB_MODEL_ERROR_NOT_UPDATABLE, "Row locked. "
|
|
|
|
"There are one or more operations being applied over this row.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
DbOperation * operation = g_hash_table_lookup (priv->row_ops, row);
|
|
|
|
DbModelRowOp row_op = operation ? operation->type : 0;
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
priv->updated_col = col;
|
|
|
|
priv->updated_value = g_new0 (GValue, 1);
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_init (priv->updated_value, G_VALUE_TYPE (&new_value));
|
|
|
|
g_value_copy (&new_value, priv->updated_value);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_UPDATED], 0, iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (priv->mode == DB_MODEL_MODE_ON_CHANGE
|
|
|
|
&& !(row_op & DB_MODEL_ROW_OP_INSERT))
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_perform_operations (self, FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_value_unset (&new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_insert:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: (out): a #DbIter that will point to the new row
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Inserts an empty row at the end of @self. The values for this row must be
|
2013-10-11 23:07:35 +00:00
|
|
|
* set one by one using db_model_set_value() or all at once with db_model_set().
|
|
|
|
* And then committed with db_model_perform_operations().
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_insert (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
DbRow * row;
|
|
|
|
DbModelPrivate * priv;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY (self) || !priv->result)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_log (g_quark_to_string (DB_MODEL_LOG_DOMAIN)
|
|
|
|
,G_LOG_LEVEL_WARNING, "Model not ready");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(priv->update_flags & DB_MODEL_INSERT))
|
|
|
|
{
|
|
|
|
g_log (g_quark_to_string (DB_MODEL_LOG_DOMAIN)
|
|
|
|
,G_LOG_LEVEL_WARNING, "Can't insert into this Model");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
row = db_row_new (priv->result->ncols, priv->result->nrows);
|
|
|
|
|
|
|
|
for (i = 0; i < row->len; i++)
|
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
Field field;
|
2014-06-05 15:59:35 +00:00
|
|
|
ColumnDef * def;
|
2014-05-09 11:40:32 +00:00
|
|
|
const GValue * def_value = NULL;
|
2014-05-29 14:30:11 +00:00
|
|
|
DbColumn col = priv->column[i];
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
field.name = col.name;
|
2014-06-05 15:59:35 +00:00
|
|
|
field.target = priv->column_table[i]->alias;
|
|
|
|
field.schema = NULL;
|
|
|
|
def = g_hash_table_lookup (priv->defaults, &field);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
if (def && def->param)
|
|
|
|
def_value = gvn_param_get_value (def->param);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (!def_value)
|
2014-05-29 14:30:11 +00:00
|
|
|
def_value = gvn_param_spec_get_default (col.spec);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (def_value && G_VALUE_TYPE (def_value) != SQL_TYPE_FUNCTION)
|
2013-10-14 12:08:06 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
g_value_init (&row->value[i], G_VALUE_TYPE (def_value));
|
|
|
|
g_value_copy (def_value, &row->value[i]);
|
2013-10-14 12:08:06 +00:00
|
|
|
}
|
2014-05-09 11:40:32 +00:00
|
|
|
else
|
2013-10-11 23:07:35 +00:00
|
|
|
g_value_init (&row->value[i], GVN_TYPE_NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_ptr_array_add (priv->data, row);
|
|
|
|
iter->data = g_ptr_array_index (priv->data, (guint) priv->data->len - 1);
|
|
|
|
iter->stamp = priv->stamp;
|
|
|
|
priv->result->nrows++;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_row_operation (self, iter->data, DB_MODEL_ROW_OP_INSERT, 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_INSERTED], 0, iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_delete:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter pointing to the row to be deleted
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Deletes the row pointed to by @iter on @self, as well as on the database (if
|
2013-10-11 23:07:35 +00:00
|
|
|
* it was already there, i.e. it's not a new row).
|
2014-06-12 13:48:53 +00:00
|
|
|
* If the deletion on the database fails, then the row on @self is not deleted
|
2013-10-11 23:07:35 +00:00
|
|
|
* and a log message is emitted.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_delete (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
|
|
|
g_return_if_fail (VALID_ITER (iter, self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY (self))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_log (g_quark_to_string (DB_MODEL_LOG_DOMAIN)
|
|
|
|
,G_LOG_LEVEL_WARNING, "Model not ready");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (!(self->priv->update_flags & DB_MODEL_DELETE))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_log (g_quark_to_string (DB_MODEL_LOG_DOMAIN)
|
|
|
|
,G_LOG_LEVEL_WARNING, "Can't delete from this Model");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (iter->data == self->priv->null_row)
|
2013-10-14 12:08:06 +00:00
|
|
|
return;
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
if (!db_model_set_row_operation
|
2014-06-12 13:48:53 +00:00
|
|
|
(self, g_ptr_array_index (self->priv->data, DB_ROW_POSITION (iter->data)),
|
2013-10-11 23:07:35 +00:00
|
|
|
DB_MODEL_ROW_OP_DELETE, 0))
|
|
|
|
return;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_TOGGLED], 0, iter);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->mode == DB_MODEL_MODE_ON_CHANGE)
|
|
|
|
db_model_perform_operations (self, FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
/**
|
|
|
|
* db_model_use_null_row:
|
2014-06-20 13:46:39 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-14 12:08:06 +00:00
|
|
|
* @use: wether to use a null row
|
|
|
|
*
|
|
|
|
* Sets wether to use a null row at the end of the model. This row can't be
|
|
|
|
* modified by the methods @db_model_delete or @db_model_set_value, the only way
|
|
|
|
* to remove it is to call @db_model_use_null_row again passing #FALSE.
|
|
|
|
**/
|
2014-06-20 13:46:39 +00:00
|
|
|
void db_model_use_null_row (DbModel * self, gboolean use)
|
2013-10-14 12:08:06 +00:00
|
|
|
{
|
|
|
|
DbModelPrivate * priv;
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
priv = self->priv;
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (MODEL_NOT_READY(self) || !priv->result
|
2013-10-14 12:08:06 +00:00
|
|
|
|| ((priv->null_row != NULL) == use))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (use)
|
|
|
|
{
|
|
|
|
if (!priv->null_row)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
DbIter iter;
|
2014-06-20 13:46:39 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-14 12:08:06 +00:00
|
|
|
DbRow * curr, * row = db_row_new (priv->result->ncols, 0);
|
|
|
|
gpointer * data = priv->data->pdata;
|
|
|
|
|
|
|
|
for (i = 0; i < row->len; i++)
|
|
|
|
g_value_init (&row->value[i], GVN_TYPE_NULL);
|
|
|
|
|
|
|
|
priv->null_row = row;
|
|
|
|
iter.data = row;
|
|
|
|
iter.stamp = priv->stamp;
|
|
|
|
|
|
|
|
priv->result->nrows++;
|
|
|
|
g_ptr_array_set_size (priv->data, priv->result->nrows);
|
|
|
|
curr = data[0];
|
|
|
|
data[0] = row;
|
|
|
|
|
|
|
|
for (i = 1; i < priv->result->nrows; i++)
|
|
|
|
{
|
|
|
|
DbRow * next = data[i];
|
|
|
|
DB_ROW_POSITION (curr)++;
|
|
|
|
data[i] = curr;
|
|
|
|
curr = next;
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_INSERTED], 0, &iter);
|
2013-10-14 12:08:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (priv->null_row)
|
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINE_DELETED], 0,
|
2013-10-14 12:08:06 +00:00
|
|
|
DB_ROW_POSITION (priv->null_row));
|
|
|
|
priv->null_row = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
/**
|
|
|
|
* db_model_order_by:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @col: the number of the column that will be the sort criteria
|
|
|
|
* @order: the order to sort in
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sorts @self in the order indicated by @order and using the data
|
2013-10-11 23:07:35 +00:00
|
|
|
* on the field @col.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_order_by (DbModel * self, gint col, DbSortType order)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
DbModelPrivate * priv;
|
2013-10-14 12:08:06 +00:00
|
|
|
SortInfo info;
|
2014-06-20 13:46:39 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2014-06-20 13:46:39 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
priv = self->priv;
|
2014-06-20 13:46:39 +00:00
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_if_fail (priv->result && col >= -2 && col < priv->result->ncols);
|
|
|
|
g_return_if_fail (order == DB_SORT_ASCENDING || order == DB_SORT_DESCENDING);
|
|
|
|
|
|
|
|
gint new_order[priv->result->nrows];
|
|
|
|
gint old_col = priv->sort_column_id;
|
|
|
|
DbRow * row_iter;
|
|
|
|
gint r_ind, i = 0;
|
|
|
|
|
|
|
|
if ((priv->order == order && priv->sort_column_id == col)
|
2013-10-14 12:08:06 +00:00
|
|
|
|| !priv->data
|
|
|
|
|| col == DB_MODEL_UNSORTED_SORT_COLUMN_ID)
|
2013-10-11 23:07:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (col == DB_MODEL_DEFAULT_SORT_COLUMN_ID
|
|
|
|
&& old_col != DB_MODEL_DEFAULT_SORT_COLUMN_ID)
|
|
|
|
col = old_col;
|
|
|
|
|
|
|
|
priv->order = order;
|
|
|
|
priv->sort_column_id = col;
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[SORT_CHANGED], 0, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
info.col = col;
|
|
|
|
info.null_row = priv->null_row;
|
|
|
|
info.order = order;
|
|
|
|
|
|
|
|
g_ptr_array_sort_with_data (priv->data
|
|
|
|
,(GCompareDataFunc) db_model_valcmp
|
|
|
|
,&info);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
for (r_ind = 0; r_ind < priv->result->nrows; r_ind++)
|
|
|
|
{
|
|
|
|
row_iter = g_ptr_array_index (priv->data, r_ind);
|
|
|
|
new_order[i] = DB_ROW_POSITION (row_iter);
|
|
|
|
DB_ROW_POSITION (row_iter) = i++;
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_signal_emit (self, db_model_signal[LINES_REORDERED], 0, col, new_order);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_search:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @col: the field to search in
|
|
|
|
* @iter: a #DbIter that will point the first found element
|
|
|
|
* @content: the value looked for or %NULL
|
|
|
|
*
|
|
|
|
* Looks for a the passed value in the field specified by @col.
|
|
|
|
*
|
|
|
|
* Return value: Returns %TRUE if the value is found and %FALSE otherwise
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_search (DbModel * self, gint col, DbIter * iter, gpointer content)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gboolean ret;
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue value = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
|
|
|
g_return_val_if_fail (self->priv->result
|
|
|
|
&& 0 <= self->priv->result->ncols
|
|
|
|
&& col < self->priv->result->ncols , FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
gvn_value_new_with_content (&value,
|
2014-06-12 13:48:53 +00:00
|
|
|
gvn_param_spec_get_gtype (self->priv->column[col].spec), content);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
ret = db_model_search_value (self, col, iter, &value);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_value_unset (&value);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_search_value:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @col: the field to search in
|
|
|
|
* @iter: a #DbIter that will point the first found element
|
|
|
|
* @value: the value to search for
|
|
|
|
*
|
|
|
|
* Looks for a the value pointed to by @value in the field specified by @col.
|
|
|
|
*
|
|
|
|
* Return value: Returns %TRUE if the value is found and %FALSE otherwise
|
|
|
|
**/
|
|
|
|
gboolean db_model_search_value
|
2014-06-12 13:48:53 +00:00
|
|
|
(DbModel * self, gint col, DbIter * iter, const GValue * value)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
GType type;
|
|
|
|
DbRow * row;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_return_val_if_fail (G_IS_VALUE (value), FALSE);
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (self->priv->result
|
2013-10-11 23:07:35 +00:00
|
|
|
&& 0 <= col
|
2014-06-12 13:48:53 +00:00
|
|
|
&& col < self->priv->result->ncols, FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
type = gvn_param_spec_get_gtype (self->priv->column[col].spec);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (gvn_value_is_null (value) || G_VALUE_TYPE (value) == type)
|
2014-06-12 13:48:53 +00:00
|
|
|
for (i = 0; i < self->priv->result->nrows; i++)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
row = g_ptr_array_index (self->priv->data, i);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (gvn_value_compare (DB_ROW_FIELD (row, col), value))
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
iter->stamp = self->priv->stamp;
|
2013-10-11 23:07:35 +00:00
|
|
|
iter->data = row;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_row_operations:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
*
|
|
|
|
* Returns #DbModelRowOp flags indicating the operations being applied to the
|
|
|
|
* row pointed to by @iter. If no operations are being applied on the row, it
|
|
|
|
* returns 0.
|
|
|
|
*
|
|
|
|
* Return value: the #DbModelRowOp of the row or 0
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelRowOp db_model_get_row_operations (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-20 13:46:39 +00:00
|
|
|
DbOperation * op = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), 0);
|
|
|
|
g_return_val_if_fail (VALID_ITER (iter, self), 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (self->priv->row_ops)
|
|
|
|
op = g_hash_table_lookup (self->priv->row_ops, (DbRow *) iter->data);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
return op ? op->type : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_has_pending_operations:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Returns whether there are pending operations in the model.
|
|
|
|
*
|
|
|
|
* Return value: #TRUE if the model has pending operations, #FALSE otherwise
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_has_pending_operations (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->row_ops)
|
|
|
|
return g_hash_table_size (self->priv->row_ops) > 0;
|
2014-05-30 09:19:53 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static SqlObject * db_model_create_where (DbModel * self,
|
2014-07-09 07:49:56 +00:00
|
|
|
TableInfo * tinfo, DbOperation * operation, gboolean use_new_values)
|
2014-05-09 11:40:32 +00:00
|
|
|
{
|
|
|
|
GSList * l;
|
|
|
|
DbUpdatedField * u;
|
|
|
|
GValue * g_value;
|
|
|
|
SqlObject * value;
|
|
|
|
DbRow * row = operation->row;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlObject * where;
|
|
|
|
SqlList * and_operands;
|
|
|
|
|
2014-06-25 11:25:21 +00:00
|
|
|
if (!tinfo->pkeys)
|
|
|
|
return NULL;
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
where = sql_operation_new (SQL_OPERATION_TYPE_AND);
|
|
|
|
|
|
|
|
and_operands = sql_list_new (SQL_TYPE_EXPR);
|
|
|
|
sql_operation_set_operands (SQL_OPERATION (where), and_operands);
|
2014-05-30 09:19:53 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
for (l = tinfo->pkeys; l; l = l->next)
|
2014-05-09 11:40:32 +00:00
|
|
|
{
|
2014-05-30 09:19:53 +00:00
|
|
|
GSList * n;
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlObject * equal;
|
|
|
|
SqlList * operands;
|
2014-05-29 14:30:11 +00:00
|
|
|
gint col = GPOINTER_TO_INT (l->data);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
value = NULL;
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
g_value = &row->value[col];
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-07-09 07:49:56 +00:00
|
|
|
if (!use_new_values)
|
2014-06-25 11:25:21 +00:00
|
|
|
{
|
|
|
|
for (n = operation->updated; n && (u = n->data); n = n->next)
|
|
|
|
if (u->column == col)
|
|
|
|
{
|
|
|
|
g_value = u->value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gvn_value_is_null (g_value))
|
|
|
|
value = sql_value_new_with_value (g_value);
|
2014-05-29 14:30:11 +00:00
|
|
|
}
|
2014-06-25 11:25:21 +00:00
|
|
|
else
|
2014-05-29 14:30:11 +00:00
|
|
|
{
|
|
|
|
const GValue * def = gvn_param_spec_get_default (priv->column[col].spec);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (def && G_IS_VALUE (def)
|
|
|
|
&& G_VALUE_TYPE (def) == SQL_TYPE_FUNCTION)
|
|
|
|
value = g_value_get_object (def);
|
2014-06-25 11:25:21 +00:00
|
|
|
else
|
|
|
|
value = sql_value_new_with_value (g_value);
|
2014-05-09 11:40:32 +00:00
|
|
|
}
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
g_object_unref (g_object_ref_sink (where));
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-09 07:49:56 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
equal = sql_operation_new (SQL_OPERATION_TYPE_EQUAL);
|
|
|
|
sql_list_add (and_operands, equal);
|
|
|
|
|
|
|
|
operands = sql_list_new (SQL_TYPE_EXPR);
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_list_add (operands, sql_field_new (priv->column[col].name));
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (operands, value);
|
|
|
|
sql_operation_set_operands (SQL_OPERATION (equal), operands);
|
2014-05-09 11:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return where;
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static SqlObject * db_model_create_insert (DbModel * self,
|
2014-06-05 15:59:35 +00:00
|
|
|
TableInfo * tinfo, DbOperation * operation)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
gint i;
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2014-05-09 11:40:32 +00:00
|
|
|
DbRow * row = operation->row;
|
2014-05-29 14:30:11 +00:00
|
|
|
const GValue * value;
|
|
|
|
SqlList * targets, * stmts, * sets, * fields, * values, * select_fields;
|
2014-06-05 15:59:35 +00:00
|
|
|
SqlObject * target, * insert, * set, * select, * where;
|
|
|
|
GHashTableIter iter;
|
|
|
|
Field * field;
|
|
|
|
ColumnDef * def;
|
|
|
|
GArray * cols;
|
|
|
|
gchar * colname;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
where = db_model_create_where (self, tinfo, operation, TRUE);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (!where)
|
|
|
|
return NULL;
|
2014-05-27 07:55:01 +00:00
|
|
|
|
|
|
|
fields = sql_list_new (SQL_TYPE_FIELD);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
sets = sql_list_new (SQL_TYPE_SET);
|
|
|
|
values = sql_list_new (SQL_TYPE_EXPR);
|
|
|
|
set = g_object_new (SQL_TYPE_SET
|
|
|
|
,"exprs", values
|
|
|
|
,NULL
|
|
|
|
);
|
|
|
|
sql_list_add (sets, set);
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
target = sql_table_new (tinfo->name, tinfo->schema);
|
2014-05-27 07:55:01 +00:00
|
|
|
insert = g_object_new (SQL_TYPE_INSERT
|
|
|
|
,"table", target
|
|
|
|
,"fields", fields
|
|
|
|
,"values", sets
|
|
|
|
,NULL
|
|
|
|
);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
select_fields = sql_list_new (SQL_TYPE_EXPR);
|
2014-05-27 07:55:01 +00:00
|
|
|
|
|
|
|
targets = sql_list_new (SQL_TYPE_TARGET);
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
target = sql_table_new (tinfo->name, tinfo->schema);
|
|
|
|
sql_target_set_alias (SQL_TARGET (target), tinfo->alias);
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (targets, target);
|
|
|
|
|
|
|
|
select = g_object_new (SQL_TYPE_SELECT
|
2014-05-29 14:30:11 +00:00
|
|
|
,"fields", select_fields
|
2014-05-27 07:55:01 +00:00
|
|
|
,"targets", targets
|
|
|
|
,"where", where
|
|
|
|
,NULL
|
|
|
|
);
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
g_hash_table_iter_init (&iter, priv->defaults);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer) &field, (gpointer) &def))
|
|
|
|
if (!g_strcmp0 (field->target, tinfo->alias)
|
|
|
|
&& !g_hash_table_lookup (tinfo->columns, field->name))
|
2014-05-09 11:40:32 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
if (def->param)
|
|
|
|
value = gvn_param_get_value (def->param);
|
|
|
|
else
|
|
|
|
value = NULL;
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
if (value)
|
2014-05-29 14:30:11 +00:00
|
|
|
{
|
|
|
|
sql_list_add (fields, sql_field_new (field->name));
|
|
|
|
sql_list_add (values, sql_value_new_with_value (value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
g_hash_table_iter_init (&iter, tinfo->columns);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer) &colname, (gpointer) &cols))
|
2014-05-29 14:30:11 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
i = g_array_index (cols, gint, 0);
|
2014-05-29 14:30:11 +00:00
|
|
|
value = &row->value[i];
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (!gvn_value_is_null (value))
|
2014-05-27 07:55:01 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_list_add (fields, sql_field_new (colname));
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (values, sql_value_new_with_value (value));
|
|
|
|
}
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_list_add (select_fields, sql_field_new (colname));
|
2014-05-09 11:40:32 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
stmts = sql_list_new (SQL_TYPE_STMT);
|
|
|
|
sql_list_add (stmts, insert);
|
|
|
|
sql_list_add (stmts, select);
|
|
|
|
return g_object_new (SQL_TYPE_MULTI_STMT, "stmts", stmts, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_perform_operations:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel with a new row, not yet inserted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @retry: whether to retry the failed operations (not implemented)
|
|
|
|
*
|
|
|
|
* Commits the changes made by db_model_set_value() to a new row, inserted
|
|
|
|
* by db_model_insert().
|
|
|
|
* Note that if this method is not called after db_model_insert(), all the
|
|
|
|
* changes made on the new row will be lost.
|
2014-06-12 13:48:53 +00:00
|
|
|
* If the @self is working in the #DB_MODEL_MODE_ON_DEMAND, non-interactive,
|
2013-10-11 23:07:35 +00:00
|
|
|
* mode, this method will perform every actions taken and not yet submitted.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_perform_operations (DbModel * self, gboolean retry)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
GList * l;
|
|
|
|
DbOperation * op;
|
2013-10-11 23:07:35 +00:00
|
|
|
DbModelPrivate * priv;
|
|
|
|
DbRow * row;
|
2014-05-09 11:40:32 +00:00
|
|
|
SqlObject * where;
|
2013-10-11 23:07:35 +00:00
|
|
|
SqlList * stmts;
|
|
|
|
DbRequest * request;
|
2014-05-29 14:30:11 +00:00
|
|
|
gboolean error = FALSE;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
priv = self->priv;
|
|
|
|
|
|
|
|
if (!priv->update_flags)
|
|
|
|
return;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY (self))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
g_log (g_quark_to_string (DB_MODEL_LOG_DOMAIN), G_LOG_LEVEL_WARNING,
|
|
|
|
"Model not ready");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!priv->operation->length)
|
|
|
|
return;
|
2014-05-29 14:30:11 +00:00
|
|
|
|
|
|
|
stmts = sql_list_new (SQL_TYPE_STMT);
|
|
|
|
g_object_ref_sink (stmts);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
for (l = priv->operation->head; l && !error; l = l->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlObject * stmt = NULL;
|
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
op = l->data;
|
|
|
|
op->locked = TRUE;
|
|
|
|
row = op->row;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (op->type & DB_MODEL_ROW_OP_DELETE) // DELETE
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
if (!(op->type & DB_MODEL_ROW_OP_INSERT))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
where = db_model_create_where (self, priv->main_table, op, FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (where)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
SqlList * targets = sql_list_new (SQL_TYPE_TARGET);
|
|
|
|
sql_list_add (targets, sql_table_new (
|
|
|
|
priv->main_table->name,
|
|
|
|
priv->main_table->schema
|
|
|
|
));
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
stmt = g_object_new (SQL_TYPE_DELETE
|
|
|
|
,"where", where
|
|
|
|
,"targets", targets
|
|
|
|
,NULL
|
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-29 14:30:11 +00:00
|
|
|
else
|
|
|
|
error = TRUE;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-29 14:30:11 +00:00
|
|
|
else if (op->type & DB_MODEL_ROW_OP_INSERT) // INSERT + SELECT
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
stmt = db_model_create_insert (self, priv->main_table, op);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
if (!stmt)
|
2014-05-09 11:40:32 +00:00
|
|
|
error = TRUE;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-29 14:30:11 +00:00
|
|
|
else if (op->type & DB_MODEL_ROW_OP_UPDATE) // UPDATE || INSERT + SELECT
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
GList * l;
|
|
|
|
GSList * n;
|
|
|
|
GQueue * fields;
|
|
|
|
TableInfo * tinfo;
|
2014-05-09 11:40:32 +00:00
|
|
|
GHashTableIter iter;
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlList * update_list;
|
2014-06-05 15:59:35 +00:00
|
|
|
DbUpdatedField * u;
|
|
|
|
|
|
|
|
GHashTable * tables = g_hash_table_new_full (
|
|
|
|
g_direct_hash
|
|
|
|
,g_direct_equal
|
|
|
|
,NULL
|
|
|
|
,(GDestroyNotify) g_queue_free
|
|
|
|
);
|
|
|
|
|
|
|
|
for (n = op->updated; n && (u = n->data); n = n->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
tinfo = priv->column_table[u->column];
|
|
|
|
fields = g_hash_table_lookup (tables, tinfo);
|
|
|
|
|
|
|
|
if (!fields)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
fields = g_queue_new ();
|
|
|
|
g_hash_table_insert (tables, tinfo, fields);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
g_queue_push_tail (fields, u);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
2014-05-29 14:30:11 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
update_list = sql_list_new (SQL_TYPE_STMT);
|
2014-06-05 15:59:35 +00:00
|
|
|
g_hash_table_iter_init (&iter, tables);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer) &tinfo, (gpointer) &fields))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
where = db_model_create_where (self, tinfo, op, FALSE);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (where)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlList * sets = sql_list_new (SQL_TYPE_UPDATE_SET);
|
|
|
|
SqlList * targets = sql_list_new (SQL_TYPE_TARGET);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
sql_list_add (targets, sql_table_new (tinfo->name, tinfo->schema));
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
for (l = fields->head; l && (u = l->data); l = l->next)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
GValue * new_value = DB_ROW_FIELD (row, u->column);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (sets, g_object_new (SQL_TYPE_UPDATE_SET
|
|
|
|
,"field", sql_field_new (priv->column[u->column].name)
|
|
|
|
,"expr", sql_value_new_with_value (new_value)
|
|
|
|
,NULL
|
|
|
|
));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (update_list, g_object_new (SQL_TYPE_UPDATE
|
|
|
|
,"where", where
|
|
|
|
,"targets", targets
|
|
|
|
,"sets", sets
|
|
|
|
,NULL
|
|
|
|
));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
SqlObject * insert = db_model_create_insert (self, tinfo, op);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
if (insert)
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (update_list, insert);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
g_hash_table_unref (tables);
|
2014-05-27 07:55:01 +00:00
|
|
|
stmt = g_object_new (SQL_TYPE_MULTI_STMT, "stmts", update_list, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
if (stmt)
|
|
|
|
sql_list_add (stmts, stmt);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 09:19:53 +00:00
|
|
|
if (!error)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
GQueue * ops = g_queue_new ();
|
|
|
|
|
|
|
|
while ((op = g_queue_pop_head (priv->operation)))
|
|
|
|
g_queue_push_tail (ops, op);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
DbModelRequest * data = g_new (DbModelRequest, 1);
|
2014-06-12 13:48:53 +00:00
|
|
|
data->self = g_object_ref (self);
|
2014-05-29 14:30:11 +00:00
|
|
|
data->operations = ops;
|
2014-05-27 07:55:01 +00:00
|
|
|
data->stmts = g_object_ref_sink (stmts);
|
2014-05-30 09:19:53 +00:00
|
|
|
|
|
|
|
if (sql_list_length (stmts) > 0)
|
|
|
|
{
|
|
|
|
SqlObject * multi = g_object_new (
|
|
|
|
SQL_TYPE_MULTI_STMT, "stmts", stmts, NULL);
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
request = db_conn_query_with_stmt_async (priv->conn
|
|
|
|
,SQL_STMT (multi)
|
2014-05-30 09:19:53 +00:00
|
|
|
,NULL
|
2013-10-11 23:07:35 +00:00
|
|
|
,(DbRequestDoneCallback) db_model_on_operations_done
|
|
|
|
,data
|
|
|
|
,(GDestroyNotify) db_model_request_free
|
|
|
|
);
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_add_pending_request (self, request);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-30 09:19:53 +00:00
|
|
|
db_model_on_operations_done (NULL, data);
|
|
|
|
db_model_request_free (data);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-09 11:40:32 +00:00
|
|
|
else
|
2014-05-30 09:19:53 +00:00
|
|
|
g_warning ("DbModel: Error performing operations.");
|
2014-05-29 14:30:11 +00:00
|
|
|
|
|
|
|
g_object_unref (stmts);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_refresh:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Executes the SELECT query and fills @self with the data returned by it.
|
2013-10-11 23:07:35 +00:00
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_refresh (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
DbModelPrivate * priv;
|
2014-05-29 14:30:11 +00:00
|
|
|
gboolean is_ready = FALSE;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
priv = self->priv;
|
|
|
|
db_model_clear (self);
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
if (priv->conn && priv->stmt)
|
2014-05-27 07:55:01 +00:00
|
|
|
{
|
2014-06-05 15:59:35 +00:00
|
|
|
Field * field;
|
|
|
|
ColumnDef * def;
|
|
|
|
SqlObject * link_op;
|
|
|
|
SqlList * link_operands;
|
|
|
|
SqlBatch * tmp_batch;
|
|
|
|
GHashTableIter iter;
|
|
|
|
|
|
|
|
// Gets all the holders from the statement
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
tmp_batch = sql_batch_new ();
|
|
|
|
g_object_ref_sink (tmp_batch);
|
2014-05-29 14:30:11 +00:00
|
|
|
sql_object_get_holders (SQL_OBJECT (priv->stmt), tmp_batch);
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_batch_merge (tmp_batch, priv->batch);
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
// Creates the link operation
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
link_op = sql_operation_new (SQL_OPERATION_TYPE_AND);
|
|
|
|
g_object_ref_sink (link_op);
|
|
|
|
|
|
|
|
link_operands = sql_list_new (SQL_TYPE_EXPR);
|
|
|
|
sql_operation_set_operands (SQL_OPERATION (link_op), link_operands);
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, priv->defaults);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer) &field, (gpointer) &def))
|
|
|
|
if (def->link)
|
|
|
|
{
|
|
|
|
SqlObject * sql_field;
|
|
|
|
SqlObject * equal_op;
|
|
|
|
SqlList * equal_operands;
|
|
|
|
|
|
|
|
equal_op = sql_operation_new (SQL_OPERATION_TYPE_EQUAL);
|
|
|
|
sql_list_add (link_operands, equal_op);
|
|
|
|
|
|
|
|
sql_field = sql_field_new_with_target (
|
|
|
|
field->name, field->target, NULL);
|
|
|
|
|
|
|
|
equal_operands = sql_list_new (SQL_TYPE_EXPR);
|
|
|
|
sql_list_add (equal_operands, sql_field);
|
|
|
|
sql_list_add (equal_operands, sql_value_new_with_param (def->param));
|
|
|
|
sql_operation_set_operands (SQL_OPERATION (equal_op), equal_operands);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sql_list_length (link_operands) > 0)
|
|
|
|
sql_batch_add (tmp_batch, "link", link_op);
|
2014-07-22 10:09:54 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
// Executes the statement if its ready
|
2014-05-29 14:30:11 +00:00
|
|
|
|
|
|
|
if (sql_batch_is_ready (tmp_batch))
|
|
|
|
{
|
|
|
|
is_ready = TRUE;
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_status (self, DB_MODEL_STATUS_LOADING);
|
2014-05-27 07:55:01 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
priv->request = db_conn_query_with_stmt_async (priv->conn
|
|
|
|
,priv->stmt
|
|
|
|
,tmp_batch
|
|
|
|
,(DbRequestDoneCallback) db_model_on_data_ready
|
2014-06-12 13:48:53 +00:00
|
|
|
,g_object_ref (self)
|
2014-05-29 14:30:11 +00:00
|
|
|
,(GDestroyNotify) g_object_unref
|
|
|
|
);
|
|
|
|
}
|
2014-06-05 15:59:35 +00:00
|
|
|
|
|
|
|
g_object_unref (link_op);
|
|
|
|
g_object_unref (tmp_batch);
|
2014-05-27 07:55:01 +00:00
|
|
|
}
|
2014-05-29 14:30:11 +00:00
|
|
|
|
|
|
|
if (!is_ready)
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_status (self, DB_MODEL_STATUS_CLEAN);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_nrows:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Returns the current number of rows on @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: the number of rows
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gint db_model_get_nrows (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->result)
|
|
|
|
return self->priv->result->nrows;
|
2013-10-11 23:07:35 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_iter_is_valid:
|
|
|
|
* @iter: a #DbIter
|
|
|
|
* @model: a #DbModel
|
|
|
|
*
|
|
|
|
* Checks if @iter is a valid #DbIter pointing inside @model.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_iter_is_valid (DbIter * iter, DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
return (iter && VALID_ITER (iter, self));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GtkTreeModel implementation methods.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_ncols:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Returns the number of columns supported by @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: the number of columns
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gint db_model_get_ncols (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), -1);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->result)
|
|
|
|
return self->priv->result->ncols;
|
2013-10-11 23:07:35 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_column_type:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @index: the number of the column
|
|
|
|
*
|
|
|
|
* Retrieves the type of the column specified by @index.
|
|
|
|
*
|
|
|
|
* Return value: the type of the column
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
GType db_model_get_column_type (DbModel * self, gint index)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), G_TYPE_INVALID);
|
|
|
|
g_return_val_if_fail (self->priv->result
|
|
|
|
&& 0 <= index && index < self->priv->result->ncols, G_TYPE_INVALID);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->column)
|
|
|
|
if (self->priv->column[index].spec)
|
|
|
|
return gvn_param_spec_get_gtype (self->priv->column[index].spec);
|
2013-10-11 23:07:35 +00:00
|
|
|
return G_TYPE_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_path:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
*
|
|
|
|
* Returns the number of the row pointed to by @iter.
|
|
|
|
*
|
|
|
|
* Return value: the number of the row pointed to by @iter
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gint db_model_get_path (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), 0);
|
|
|
|
if (MODEL_NOT_READY (self)) return 0;
|
|
|
|
g_return_val_if_fail (VALID_ITER (iter, self), 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
return DB_ROW_POSITION (iter->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_iter:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2014-02-11 12:16:39 +00:00
|
|
|
* @iter: (out): an unitialized #DbIter
|
2013-10-11 23:07:35 +00:00
|
|
|
* @path: the number of the row being accessed
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets @iter pointing to the row of @self specified by @path.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if the position is found, %FALSE otherwise
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_get_iter (DbModel * self, DbIter * iter, gint path)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if ((0 > path
|
2014-06-12 13:48:53 +00:00
|
|
|
|| (self->priv->result && path >= self->priv->result->nrows))
|
|
|
|
|| MODEL_NOT_READY (self))
|
2013-10-11 23:07:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
iter->stamp = self->priv->stamp;
|
|
|
|
iter->data = g_ptr_array_index (self->priv->data, (guint) path);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (iter->data)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_iter_first:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: (out): an unitialized #DbIter
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets @iter pointing to the first row of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if @iter is set right
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_get_iter_first (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2014-06-20 13:46:39 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY(self)
|
|
|
|
|| !self->priv->result || !self->priv->result->nrows)
|
2013-10-11 23:07:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
iter->stamp = self->priv->stamp;
|
|
|
|
iter->data = g_ptr_array_index (self->priv->data, 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_iter_prev:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: (inout): a valid #DbIter
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets @iter pointing to the previous row of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if the iter has been changed to the previous row
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_iter_prev (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
DbModelPrivate * priv;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
priv = self->priv;
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if (MODEL_NOT_READY(self)
|
2013-10-14 12:08:06 +00:00
|
|
|
|| !priv->result || !priv->result->nrows)
|
2013-10-11 23:07:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (VALID_ITER (iter, self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
if ((iter->data = g_ptr_array_index (priv->data, (guint) DB_ROW_POSITION (iter->data) - 1)))
|
2013-10-11 23:07:35 +00:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_iter_next:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: (inout): a valid #DbIter
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets @iter pointing to the next row of @self.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: %TRUE if the iter has been changed to the next row
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_iter_next (DbModel * self, DbIter * iter)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
gint pos;
|
2014-06-20 13:46:39 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2014-06-20 13:46:39 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (MODEL_NOT_READY(self)
|
|
|
|
|| !self->priv->result || !self->priv->result->nrows)
|
2013-10-11 23:07:35 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (VALID_ITER (iter, self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
pos = DB_ROW_POSITION (iter->data);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (pos < self->priv->result->nrows-1)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
iter->data = g_ptr_array_index (self->priv->data, (guint) pos + 1);
|
2013-10-11 23:07:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
iter->stamp = (iter->stamp) ? 0 : 1;
|
2013-10-11 23:07:35 +00:00
|
|
|
iter->data = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GtkTreeSortable implementation methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_get_sort_column_id:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @sort_column_id: (out): an integer
|
|
|
|
* @order: (out): a @DbSortType
|
|
|
|
*
|
|
|
|
* Fills in @sort_column_id and @order with the current sort
|
|
|
|
* column and the order. See #GtkTreeSortable.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if the sort column is not one of the special sort column
|
|
|
|
* ids
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
gboolean db_model_get_sort_column_id (DbModel * self, gint * sort_column_id,
|
2013-10-11 23:07:35 +00:00
|
|
|
DbSortType * order)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_val_if_fail (DB_IS_MODEL (self), FALSE);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (sort_column_id)
|
2014-06-12 13:48:53 +00:00
|
|
|
* sort_column_id = self->priv->sort_column_id;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (order)
|
2014-06-12 13:48:53 +00:00
|
|
|
* order = self->priv->order;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
if (self->priv->sort_column_id == DB_MODEL_DEFAULT_SORT_COLUMN_ID
|
|
|
|
|| self->priv->sort_column_id == DB_MODEL_UNSORTED_SORT_COLUMN_ID)
|
2013-10-11 23:07:35 +00:00
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* db_model_set_sort_column_id:
|
2014-06-12 13:48:53 +00:00
|
|
|
* @self: a #DbModel
|
2013-10-11 23:07:35 +00:00
|
|
|
* @sort_column_id: the column to sort by
|
|
|
|
* @order: the order in which the sort will be done
|
|
|
|
*
|
2014-06-12 13:48:53 +00:00
|
|
|
* Sets @sort_column_id to be the current sort column. @self will resort itself
|
2013-10-11 23:07:35 +00:00
|
|
|
* to reflect this change. See #GtkTreeSortable.
|
|
|
|
**/
|
2014-06-12 13:48:53 +00:00
|
|
|
void db_model_set_sort_column_id (DbModel * self, gint sort_column_id,
|
2013-10-11 23:07:35 +00:00
|
|
|
DbSortType order)
|
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
g_return_if_fail (DB_IS_MODEL (self));
|
|
|
|
db_model_order_by (self, sort_column_id, order);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PROP_CONN = 1
|
|
|
|
,PROP_STMT
|
|
|
|
,PROP_USE_FILE
|
2013-10-14 12:08:06 +00:00
|
|
|
,PROP_SQL
|
2013-10-11 23:07:35 +00:00
|
|
|
,PROP_MAIN_TABLE
|
|
|
|
,PROP_UPDATE_FLAGS
|
|
|
|
,PROP_RESULT_POS
|
2014-07-07 15:00:21 +00:00
|
|
|
,PROP_BATCH
|
2014-09-22 17:41:32 +00:00
|
|
|
,PROP_PARTIAL_DELETE
|
|
|
|
,PROP_AUTOLOAD
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
DbModelProp;
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_set_property (DbModel * self, guint property_id,
|
2013-10-11 23:07:35 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_CONN:
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_conn (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_STMT:
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_stmt (self, g_value_get_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
2013-10-14 12:08:06 +00:00
|
|
|
case PROP_SQL:
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_sql (self, g_value_get_string (value));
|
2013-10-14 12:08:06 +00:00
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
case PROP_USE_FILE:
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->use_file = g_value_get_boolean (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_UPDATE_FLAGS:
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_request_update_flags (self, g_value_get_flags (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_RESULT_POS:
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->result_pos = g_value_get_uint (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
2014-09-22 17:41:32 +00:00
|
|
|
case PROP_BATCH:
|
|
|
|
db_model_set_batch (self, g_value_get_object (value));
|
|
|
|
break;
|
2014-06-05 15:59:35 +00:00
|
|
|
case PROP_PARTIAL_DELETE:
|
2014-06-12 13:48:53 +00:00
|
|
|
self->priv->partial_delete = g_value_get_boolean (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
2014-09-22 17:41:32 +00:00
|
|
|
case PROP_AUTOLOAD:
|
|
|
|
self->priv->autoload = g_value_get_boolean (value);
|
2014-07-07 15:00:21 +00:00
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
default:
|
2014-06-12 13:48:53 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_get_property (DbModel * self, guint property_id,
|
2013-10-11 23:07:35 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_CONN:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_object (value, self->priv->conn);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_STMT:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_object (value, self->priv->stmt);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
2013-10-14 12:08:06 +00:00
|
|
|
case PROP_SQL:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_string (value, self->priv->sql);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_USE_FILE:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_boolean (value, self->priv->use_file);
|
2013-10-14 12:08:06 +00:00
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
case PROP_MAIN_TABLE:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_string (value, db_model_get_main_table (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_UPDATE_FLAGS:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_flags (value, self->priv->update_flags);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_RESULT_POS:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_uint (value, self->priv->result_pos);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
2014-09-22 17:41:32 +00:00
|
|
|
case PROP_BATCH:
|
|
|
|
g_value_set_object (value, self->priv->batch);
|
|
|
|
break;
|
2014-06-05 15:59:35 +00:00
|
|
|
case PROP_PARTIAL_DELETE:
|
2014-06-12 13:48:53 +00:00
|
|
|
g_value_set_boolean (value, self->priv->partial_delete);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
2014-09-22 17:41:32 +00:00
|
|
|
case PROP_AUTOLOAD:
|
|
|
|
g_value_set_boolean (value, self->priv->autoload);
|
|
|
|
break;
|
2013-10-11 23:07:35 +00:00
|
|
|
default:
|
2014-06-12 13:48:53 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_init (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv =
|
|
|
|
G_TYPE_INSTANCE_GET_PRIVATE (self, DB_TYPE_MODEL, DbModelPrivate);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
priv->conn = NULL;
|
|
|
|
priv->batch = NULL;
|
|
|
|
priv->stmt = NULL;
|
|
|
|
priv->use_file = FALSE;
|
|
|
|
priv->sql = NULL;
|
|
|
|
priv->request = NULL;
|
|
|
|
priv->status = DB_MODEL_STATUS_CLEAN;
|
|
|
|
priv->result = NULL;
|
|
|
|
priv->result_pos = 0;
|
|
|
|
priv->data = NULL;
|
|
|
|
priv->column = NULL;
|
2014-09-22 17:41:32 +00:00
|
|
|
priv->autoload = TRUE;
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->stamp = g_random_int ();
|
2014-05-29 14:30:11 +00:00
|
|
|
priv->column_index = g_hash_table_new_full (
|
|
|
|
g_str_hash,
|
|
|
|
g_str_equal,
|
|
|
|
(GDestroyNotify) g_free,
|
|
|
|
NULL
|
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->defaults = g_hash_table_new_full (
|
|
|
|
(GHashFunc) field_hash,
|
|
|
|
(GEqualFunc) field_equal,
|
|
|
|
(GDestroyNotify) field_free,
|
|
|
|
(GDestroyNotify) column_def_free
|
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
priv->fresh = TRUE;
|
2014-06-20 13:46:39 +00:00
|
|
|
priv->null_row = NULL;
|
2014-05-29 14:30:11 +00:00
|
|
|
priv->sort_column_id = DB_MODEL_UNSORTED_SORT_COLUMN_ID;
|
|
|
|
priv->default_sort_data = NULL;
|
|
|
|
priv->default_sort_func = NULL;
|
|
|
|
priv->default_sort_destroy = NULL;
|
2014-05-09 11:40:32 +00:00
|
|
|
|
2014-05-29 14:30:11 +00:00
|
|
|
priv->updatable_data_allocated = FALSE;
|
2014-06-20 13:46:39 +00:00
|
|
|
priv->user_update_flags = 0;
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->update_flags = 0;
|
2014-05-09 11:40:32 +00:00
|
|
|
priv->main_table = NULL;
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->mode = DB_MODEL_MODE_ON_CHANGE;
|
2014-05-09 11:40:32 +00:00
|
|
|
priv->operation = NULL;
|
|
|
|
priv->row_ops = NULL;
|
|
|
|
priv->pending_request = NULL;
|
2014-05-29 14:30:11 +00:00
|
|
|
priv->tables = NULL;
|
2014-06-05 15:59:35 +00:00
|
|
|
priv->column_table = NULL;
|
|
|
|
priv->partial_delete = FALSE;
|
|
|
|
priv->join = NULL;
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
static void db_model_finalize (DbModel * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-06-12 13:48:53 +00:00
|
|
|
DbModelPrivate * priv = self->priv;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_clear (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-09 11:40:32 +00:00
|
|
|
g_clear_object (&priv->conn);
|
|
|
|
g_clear_object (&priv->stmt);
|
|
|
|
g_free (priv->sql);
|
2014-05-29 14:30:11 +00:00
|
|
|
g_hash_table_destroy (priv->column_index);
|
2014-06-12 13:48:53 +00:00
|
|
|
db_model_set_batch (self, NULL);
|
2014-06-05 15:59:35 +00:00
|
|
|
g_hash_table_destroy (priv->defaults);
|
2014-05-29 14:30:11 +00:00
|
|
|
|
|
|
|
if (priv->updatable_data_allocated)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-29 14:30:11 +00:00
|
|
|
g_queue_free (priv->operation);
|
2014-06-05 15:59:35 +00:00
|
|
|
g_hash_table_unref (priv->row_ops);
|
|
|
|
g_hash_table_unref (priv->tables);
|
|
|
|
g_free (priv->column_table);
|
|
|
|
// g_slist_free_full (priv->join, (GDestroyNotify) db_join_free);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 13:46:39 +00:00
|
|
|
G_OBJECT_CLASS (db_model_parent_class)->finalize (G_OBJECT (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void db_model_class_init (DbModelClass *k)
|
|
|
|
{
|
|
|
|
GObjectClass * klass = G_OBJECT_CLASS (k);
|
|
|
|
klass->set_property = (GObjectSetPropertyFunc) db_model_set_property;
|
|
|
|
klass->get_property = (GObjectGetPropertyFunc) db_model_get_property;
|
|
|
|
klass->finalize = (GObjectFinalizeFunc) db_model_finalize;
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (DbModelPrivate));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::status-changed:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @status: the current status of @model
|
|
|
|
*
|
|
|
|
* This signal is emitted every time the status of @model changes.
|
|
|
|
*/
|
|
|
|
db_model_signal[STATUS_CHANGED] = g_signal_new ("status-changed"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_FIRST, 0, NULL, NULL
|
|
|
|
,g_cclosure_marshal_VOID__INT
|
|
|
|
,G_TYPE_NONE, 1, G_TYPE_INT
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::line-inserted:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
*
|
|
|
|
* This signal is emitted when a new row is inserted into @model. The inserted
|
|
|
|
* row is pointed to by @iter.
|
|
|
|
*/
|
|
|
|
db_model_signal[LINE_INSERTED] = g_signal_new ("line-inserted"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_FIRST, 0, NULL, NULL
|
|
|
|
,g_cclosure_marshal_VOID__BOXED
|
|
|
|
,G_TYPE_NONE, 1, DB_TYPE_ITER
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::line-deleted:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @position: the position of the deleted line
|
|
|
|
*
|
|
|
|
* Every time a row of @model is deleted, this signal is emitted.
|
|
|
|
*/
|
|
|
|
db_model_signal[LINE_DELETED] = g_signal_new_class_handler ("line-deleted"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_LAST, (GCallback) db_model_on_line_deleted
|
|
|
|
,NULL, NULL, g_cclosure_marshal_VOID__INT
|
|
|
|
,G_TYPE_NONE, 1, G_TYPE_INT
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::line-toggled:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
*
|
|
|
|
* Emitted to tell that the line is going to be deleted,
|
|
|
|
* but the operation is still.
|
|
|
|
*/
|
|
|
|
db_model_signal[LINE_TOGGLED] = g_signal_new ("line-toggled"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_FIRST, 0 ,NULL, NULL
|
|
|
|
,g_cclosure_marshal_VOID__BOXED
|
|
|
|
,G_TYPE_NONE, 1, DB_TYPE_ITER
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::line-updated:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @iter: a #DbIter
|
|
|
|
*
|
|
|
|
* This signal is emitted when any value in a row of @model is set.
|
|
|
|
*/
|
|
|
|
db_model_signal[LINE_UPDATED] = g_signal_new_class_handler ("line-updated"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_LAST, (GCallback) db_model_on_line_updated
|
|
|
|
,NULL, NULL, g_cclosure_marshal_VOID__BOXED
|
|
|
|
,G_TYPE_NONE, 1, DB_TYPE_ITER
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::lines-reordered:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @col: the sort column
|
|
|
|
* @new_order: (array) (element-type gint): an array mapped with the new
|
|
|
|
* positions over the old ones
|
|
|
|
*
|
|
|
|
* This signal is emitted by @model every time its rows are reordered.
|
|
|
|
*
|
|
|
|
* The @new_order array is an array of which the positions indexed after the
|
|
|
|
* old order of the elements of @model, contain the new position of these
|
|
|
|
* elements in the reordered @model.
|
|
|
|
*/
|
|
|
|
db_model_signal[LINES_REORDERED] = g_signal_new ("lines-reordered"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_FIRST, 0, NULL, NULL
|
|
|
|
,g_cclosure_marshal_VOID__UINT_POINTER
|
|
|
|
,G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_POINTER
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::sort-changed:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* This signal is emitted when a new column is selected as sort criteria for
|
|
|
|
* @model.
|
|
|
|
*/
|
|
|
|
db_model_signal[SORT_CHANGED] = g_signal_new ("sort-changed"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_FIRST, 0, NULL, NULL
|
|
|
|
,g_cclosure_marshal_VOID__VOID
|
|
|
|
,G_TYPE_NONE, 0
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DbModel::operations-done:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: the object on which the signal is emitted
|
2013-10-11 23:07:35 +00:00
|
|
|
* @success: whether the operation has failed or succeded
|
|
|
|
*
|
|
|
|
* When an operation on the model is performed on the DB, this signal
|
|
|
|
* is emitted.
|
|
|
|
*/
|
|
|
|
db_model_signal[OPERATIONS_DONE] = g_signal_new ("operations-done"
|
|
|
|
,DB_TYPE_MODEL, G_SIGNAL_RUN_FIRST, 0, NULL, NULL
|
|
|
|
,g_cclosure_marshal_VOID__VOID
|
|
|
|
,G_TYPE_NONE, 0
|
|
|
|
);
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_CONN,
|
|
|
|
g_param_spec_object ("conn"
|
2013-10-14 12:08:06 +00:00
|
|
|
,_("Connection")
|
|
|
|
,_("The DbConn that manages the connection to the database")
|
|
|
|
,DB_TYPE_CONN
|
|
|
|
,G_PARAM_READWRITE
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_STMT,
|
|
|
|
g_param_spec_object ("stmt"
|
2013-10-14 12:08:06 +00:00
|
|
|
,_("Statement")
|
|
|
|
,_("The statement which retrieves the data")
|
|
|
|
,SQL_TYPE_STMT
|
|
|
|
,G_PARAM_READWRITE
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_USE_FILE,
|
|
|
|
g_param_spec_boolean ("use-file"
|
2013-10-14 12:08:06 +00:00
|
|
|
,_("Use file")
|
|
|
|
,_("If this is set to TRUE, the \"sql\" property will "
|
|
|
|
"hold the name of a file containing a query, if "
|
|
|
|
"set to FALSE, \"sql\" is used as an SQL string")
|
|
|
|
,FALSE
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_SQL,
|
|
|
|
g_param_spec_string ("sql"
|
|
|
|
,_("SQL")
|
|
|
|
,_("Depending on the \"use-file\" property this will "
|
|
|
|
"be the path to a file with queries for the "
|
|
|
|
"model or a SQL string")
|
|
|
|
,NULL
|
|
|
|
,G_PARAM_READWRITE
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_MAIN_TABLE,
|
|
|
|
g_param_spec_string ("main-table"
|
2013-10-14 12:08:06 +00:00
|
|
|
,_("Main Table")
|
|
|
|
,_("The main table of the model")
|
|
|
|
,NULL
|
2014-06-20 13:46:39 +00:00
|
|
|
,G_PARAM_READABLE
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_UPDATE_FLAGS,
|
|
|
|
g_param_spec_flags ("update-flags"
|
2013-10-14 12:08:06 +00:00
|
|
|
,_("Update flags")
|
|
|
|
,_("The flags that indicate how a model can be modified")
|
|
|
|
,DB_TYPE_MODEL_UPDATE_FLAGS
|
2014-06-20 13:46:39 +00:00
|
|
|
,0
|
2013-10-14 12:08:06 +00:00
|
|
|
,G_PARAM_READWRITE
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
g_object_class_install_property (klass, PROP_RESULT_POS,
|
|
|
|
g_param_spec_uint ("result-pos"
|
2013-10-14 12:08:06 +00:00
|
|
|
,_("Result position")
|
|
|
|
,_("The position where the query that will fill the "
|
|
|
|
"model will be placed in a multi-query")
|
|
|
|
,0
|
|
|
|
,G_MAXUINT32
|
|
|
|
,0
|
|
|
|
,G_PARAM_READWRITE
|
2013-10-11 23:07:35 +00:00
|
|
|
));
|
2014-06-05 15:59:35 +00:00
|
|
|
|
2014-09-22 17:41:32 +00:00
|
|
|
g_object_class_install_property (klass, PROP_BATCH,
|
|
|
|
g_param_spec_object ("batch"
|
|
|
|
,_("Batch")
|
|
|
|
,_("The batch assigned to the model")
|
|
|
|
,SQL_TYPE_BATCH
|
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
|
|
|
|
2014-06-05 15:59:35 +00:00
|
|
|
g_object_class_install_property (klass, PROP_PARTIAL_DELETE,
|
|
|
|
g_param_spec_boolean ("partial-delete"
|
2014-06-20 13:46:39 +00:00
|
|
|
,_("Partial delete")
|
|
|
|
,_("When a row is deleted set all the fields from "
|
|
|
|
"the table to null rather than delete it.")
|
|
|
|
,FALSE
|
|
|
|
,G_PARAM_READWRITE
|
2014-06-05 15:59:35 +00:00
|
|
|
));
|
2014-07-07 15:00:21 +00:00
|
|
|
|
2014-09-22 17:41:32 +00:00
|
|
|
g_object_class_install_property (klass, PROP_AUTOLOAD,
|
|
|
|
g_param_spec_boolean ("autoload"
|
|
|
|
,_("Autoload")
|
|
|
|
,_("Wether to execute the query automatically.")
|
|
|
|
,TRUE
|
2014-07-07 15:00:21 +00:00
|
|
|
,G_PARAM_READWRITE
|
|
|
|
));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GType db_model_update_flags_get_type ()
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if (type == 0)
|
|
|
|
{
|
|
|
|
static const GFlagsValue values[] =
|
|
|
|
{
|
|
|
|
{DB_MODEL_INSERT, "DB_MODEL_INSERT", "insert"},
|
|
|
|
{DB_MODEL_DELETE, "DB_MODEL_DELETE", "delete"},
|
|
|
|
{DB_MODEL_UPDATE, "DB_MODEL_UPDATE", "update"},
|
2014-06-20 13:46:39 +00:00
|
|
|
{DB_MODEL_ALL, "DB_MODEL_ALL", "all"},
|
2013-10-11 23:07:35 +00:00
|
|
|
{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
type = g_flags_register_static
|
|
|
|
(g_intern_static_string ("DbModelUpdateFlags"), values);
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|