qofsql.c File Reference

QOF client-side SQL parser - interfaces with libgda. More...

#include "config.h"
#include <stdlib.h>
#include <errno.h>
#include <glib.h>
#include <libintl.h>
#include "sql_parser.h"
#include <time.h>
#include "qof.h"
#include "qofsql-p.h"
#include "qofquery-p.h"

Go to the source code of this file.

Defines

#define _(String)   dgettext (GETTEXT_PACKAGE, String)
#define QSQL_KVP_TABLE   "sql_kvp"
#define END_DB_VERSION   " dbversion int );"

Typedefs

typedef struct ent_and_string eas

Functions

QofSqlQuery * qof_sql_query_new (void)
void qof_sql_query_destroy (QofSqlQuery *q)
QofQueryqof_sql_query_get_query (QofSqlQuery *q)
void qof_sql_query_set_book (QofSqlQuery *q, QofBook *book)
void qof_sql_query_set_kvp (QofSqlQuery *q, KvpFrame *kvp)
static void get_table_and_param (char *str, char **tab, char **param)
static char * dequote_string (char *str)
static QofQueryhandle_single_condition (QofSqlQuery *query, sql_condition *cond)
static QofQueryhandle_where (QofSqlQuery *query, sql_where *swear)
static void handle_sort_order (QofSqlQuery *query, GList *sorder_list)
static void qof_sql_insertCB (const QofParam *param, const gchar *insert_string, QofSqlQuery *query)
static void qof_query_set_insert_table (QofSqlQuery *query)
static QofEntityqof_query_insert (QofSqlQuery *query)
static const char * sql_type_as_string (sql_statement_type type)
void qof_sql_query_parse (QofSqlQuery *query, const char *str)
GList * qof_sql_query_run (QofSqlQuery *query, const char *str)
GList * qof_sql_query_rerun (QofSqlQuery *query)
static void create_sql_from_param_cb (QofParam *param, gpointer user_data)
static gchar * string_param_to_sql (QofParam *param)
static void string_param_foreach (QofParam *param, gpointer user_data)
static void create_param_list (QofParam *param, gpointer user_data)
 list just the parameter names
static void kvpvalue_to_sql_insert (const gchar *key, KvpValue *val, gpointer user_data)
static void kvpvalue_to_sql_update (const gchar *key, KvpValue *val, gpointer user_data)
gchar * qof_sql_object_create_table (QofObject *obj)
 Build a SQL 'CREATE' statement for this object.
gchar * qof_sql_entity_create_table (QofEntity *ent)
 Build a SQL 'CREATE' statement for this entity.
gchar * qof_sql_entity_insert (QofEntity *ent)
 Build a SQL 'INSERT' statement for this entity.
static void collect_kvp (QofEntity *ent, gpointer user_data)
gchar * qof_sql_entity_update (QofEntity *ent)
 Build a SQL 'UPDATE' statement for the current entity parameter.
gchar * qof_sql_entity_update_kvp (QofEntity *ent)
 Build a SQL 'UPDATE' statement for the KVP data in this entity.
gchar * qof_sql_entity_update_list (QofEntity *ent, GList **params)
 Build a SQL 'UPDATE' statement for a list of parameters.
gchar * qof_sql_entity_delete (QofEntity *ent)
 Build a SQL 'DELETE' statement for this entity.
gchar * qof_sql_entity_drop_table (QofEntity *ent)
 Build a SQL 'DROP' statement for this entity type.
void qof_sql_entity_set_kvp_tablename (const gchar *name)
 Set a default KVP table name for each backend.
void qof_sql_entity_set_kvp_id (gulong id)
 Set the initial index value of the KVP table.
gulong qof_sql_entity_get_kvp_id (void)
 Get the index value of the KVP table after the operation(s).
void qof_sql_entity_set_kvp_exists (gboolean exist)
 Set or clear a flag that the KVP table exists or not.

Variables

static gchar * kvp_table_name = NULL
static QofLogModule log_module = "qof-query"
static gulong kvp_id = 0
static gboolean kvp_table_exists = FALSE


Detailed Description

QOF client-side SQL parser - interfaces with libgda.

Author:
Copyright (C) 2004 Linas Vepstas <linas@linas.org>

Copyright 2008 Neil Williams <linux@codehelp.co.uk>

Intended to parse incoming SQL into QOF queries (SELECT or INSERT) and prepare SQL commands from QOF entities (CREATE, UPDATE, INSERT, DELETE and DROP) for use in SQL-based backends.

Definition in file qofsql.c.


Typedef Documentation

typedef struct ent_and_string eas

simple helper struct for passing between SQL-generating functions


Function Documentation

static void create_param_list ( QofParam param,
gpointer  user_data 
) [static]

list just the parameter names

Note:
Must match the number and order of the list of parameter values from create_each_param

Definition at line 1220 of file qofsql.c.

01221 {
01222     eas * data = (eas *)user_data;
01223     g_return_if_fail (data->str);
01224     /* avoid creating database fields for calculated values */
01225     if (!param->param_setfcn)
01226         return;
01227     /* avoid setting KVP even if a param_setfcn has been set
01228        because a QofSetterFunc for KVP is quite pointless. */
01229     if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
01230     {
01231         PINFO (" kvp support tag");
01232         return;
01233     }
01234     if (!g_str_has_suffix (data->str, "("))
01235     {
01236         gchar *add;
01237         add = g_strconcat (data->str, ", ", param->param_name, NULL);
01238         g_free (data->str);
01239         data->str = add;
01240     }
01241     else
01242     {
01243         gchar * add;
01244         add = g_strjoin ("", data->str, param->param_name, NULL);
01245         g_free (data->str);
01246         data->str = add;
01247     }
01248 }

static void kvpvalue_to_sql_insert ( const gchar *  key,
KvpValue val,
gpointer  user_data 
) [static]

returns the VALUES for INSERT in pre-defined order

Definition at line 1252 of file qofsql.c.

01253 {
01254     eas * data;
01255     KvpValueType n;
01256     gchar * path;
01257 
01258     path = g_strdup("");
01259     ENTER (" ");
01260     data = (eas*)user_data;
01261     g_return_if_fail (key && val && data);
01262     n = kvp_value_get_type (val);
01263     switch (n)
01264     {
01265     case KVP_TYPE_GINT64:
01266     case KVP_TYPE_DOUBLE:
01267     case KVP_TYPE_NUMERIC:
01268     case KVP_TYPE_STRING:
01269     case KVP_TYPE_GUID:
01270     case KVP_TYPE_TIME:
01271     case KVP_TYPE_BOOLEAN:
01272         {
01273             path = g_strjoin ("/", data->full_kvp_path, key, NULL);
01274             data->str =
01275                 g_strdup_printf ("'%s', '%s', '%s'", key, path,
01276                 kvp_value_to_bare_string (val));
01277             DEBUG (" %s", data->str);
01278             break;
01279         }
01280     case KVP_TYPE_FRAME:
01281         {
01282             path = g_strjoin ("/", data->full_kvp_path, key, NULL);
01283             g_free (data->full_kvp_path);
01284             data->full_kvp_path = path;
01285             kvp_frame_for_each_slot (kvp_value_get_frame (val),
01286                 kvpvalue_to_sql_insert, data);
01287             break;
01288         }
01289     default:
01290         {
01291             PERR (" unsupported value = %d", kvp_value_get_type (val));
01292             break;
01293         }
01294     }
01295     LEAVE (" %s", data->str);
01296 }

static void kvpvalue_to_sql_update ( const gchar *  key,
KvpValue val,
gpointer  user_data 
) [static]

returns the VALUES for UPDATE in pre-defined order

Definition at line 1300 of file qofsql.c.

01301 {
01302     eas * data;
01303     KvpValueType n;
01304     gchar * path;
01305 
01306     ENTER (" key=%s", key);
01307     data = (eas*)user_data;
01308     g_return_if_fail (key && val && data);
01309     n = kvp_value_get_type (val);
01310     switch (n)
01311     {
01312     case KVP_TYPE_GINT64:
01313     case KVP_TYPE_DOUBLE:
01314     case KVP_TYPE_NUMERIC:
01315     case KVP_TYPE_STRING:
01316     case KVP_TYPE_GUID:
01317     case KVP_TYPE_TIME:
01318     case KVP_TYPE_BOOLEAN:
01319         {
01320             path = g_strjoin ("/", data->full_kvp_path, key, NULL);
01321             data->str =
01322                 g_strdup_printf ("type='%s', value='%s' WHERE path='%s' and ", 
01323                     key, kvp_value_to_bare_string (val), path);
01324             DEBUG (" %s", data->str);
01325             break;
01326         }
01327     case KVP_TYPE_FRAME:
01328         {
01329             path = g_strjoin ("/", data->full_kvp_path, key, NULL);
01330             g_free (data->full_kvp_path);
01331             data->full_kvp_path = path;
01332             kvp_frame_for_each_slot (kvp_value_get_frame (val),
01333                 kvpvalue_to_sql_update, data);
01334             break;
01335         }
01336     default:
01337         {
01338             PERR (" unsupported value = %d", kvp_value_get_type (val));
01339             break;
01340         }
01341     }
01342     LEAVE (" %s", data->str);
01343 }

static gchar* string_param_to_sql ( QofParam param  )  [static]

Note:
These strings are fairly standard SQL but SQL means different things to different programs. Keep these strings as simple and plain as possible. Hopefully, what works in SQLite0 will work in most other SQL based programs.
Bug:
This won't work - collect types can vary unpredictably - need a KVP implementation.

Definition at line 1134 of file qofsql.c.

01135 {
01142     /* Handle the entity GUID. Ensure that reference GUIDs
01143        must not also try to be primary keys and can be NULL. */
01144     if ((0 == safe_strcmp (param->param_type, QOF_TYPE_GUID)) &&
01145         (0 == safe_strcmp (param->param_name, QOF_PARAM_GUID)))
01146         return g_strdup_printf (" %s char(32) primary key not null",
01147             param->param_name);
01148     if (0 == safe_strcmp (param->param_type, QOF_TYPE_GUID))
01149         return g_strdup_printf (" %s char(32)", param->param_name);
01150     /* avoid creating database fields for calculated values */
01151     if (!param->param_setfcn)
01152         return NULL;
01153     if (0 == safe_strcmp (param->param_type, QOF_TYPE_STRING))
01154         return g_strdup_printf (" %s mediumtext", param->param_name);
01155     if (0 == safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN))
01156         return g_strdup_printf (" %s int", param->param_name);
01157     if ((0 == safe_strcmp (param->param_type, QOF_TYPE_NUMERIC))
01158         || (0 == safe_strcmp (param->param_type, QOF_TYPE_DOUBLE))
01159         || (0 == safe_strcmp (param->param_type, QOF_TYPE_DEBCRED)))
01160     {
01161         return g_strdup_printf (" %s text", param->param_name);
01162     }
01163     if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT32))
01164         return g_strdup_printf (" %s int", param->param_name);
01165     if (0 == safe_strcmp (param->param_type, QOF_TYPE_TIME))
01166         return g_strdup_printf (" %s datetime", param->param_name);
01167     if (0 == safe_strcmp (param->param_type, QOF_TYPE_CHAR))
01168         return g_strdup_printf (" %s char(1)", param->param_name);
01169     /* kvp data is stored separately - actually this is really
01170        a no-op because entities do not need a param_setfcn for kvp data. */
01171     if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
01172         return g_strdup ("");
01175     if (0 == safe_strcmp (param->param_type, QOF_TYPE_COLLECT))
01176         return g_strdup ("");
01177 //      return g_strdup_printf (" %s char(32)", param->param_name);
01178     /* catch references */
01179     return g_strdup_printf (" %s char(32)", param->param_name);
01180 }


Variable Documentation

gchar* kvp_table_name = NULL [static]

One KVP table per file for all instances.

Definition at line 51 of file qofsql.c.


Generated on Mon Jul 13 05:15:21 2009 for QOF by  doxygen 1.5.9