QOF  0.8.6
Macros | Typedefs | Functions | Variables
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.

Macros

#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.nosp@m.@lin.nosp@m.as.or.nosp@m.g
Copyright 2008 Neil Williams linux.nosp@m.@cod.nosp@m.ehelp.nosp@m..co..nosp@m.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.

{
eas * data = (eas *)user_data;
g_return_if_fail (data->str);
/* avoid creating database fields for calculated values */
if (!param->param_setfcn)
return;
/* avoid setting KVP even if a param_setfcn has been set
because a QofSetterFunc for KVP is quite pointless. */
if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
{
PINFO (" kvp support tag");
return;
}
if (!g_str_has_suffix (data->str, "("))
{
gchar *add;
add = g_strconcat (data->str, ", ", param->param_name, NULL);
g_free (data->str);
data->str = add;
}
else
{
gchar * add;
add = g_strjoin ("", data->str, param->param_name, NULL);
g_free (data->str);
data->str = add;
}
}
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.

{
eas * data;
gchar * path;
path = g_strdup("");
ENTER (" ");
data = (eas*)user_data;
g_return_if_fail (key && val && data);
n = kvp_value_get_type (val);
switch (n)
{
{
path = g_strjoin ("/", data->full_kvp_path, key, NULL);
data->str =
g_strdup_printf ("'%s', '%s', '%s'", key, path,
DEBUG (" %s", data->str);
break;
}
{
path = g_strjoin ("/", data->full_kvp_path, key, NULL);
g_free (data->full_kvp_path);
data->full_kvp_path = path;
break;
}
default:
{
PERR (" unsupported value = %d", kvp_value_get_type (val));
break;
}
}
LEAVE (" %s", data->str);
}
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.

{
eas * data;
gchar * path;
ENTER (" key=%s", key);
data = (eas*)user_data;
g_return_if_fail (key && val && data);
n = kvp_value_get_type (val);
switch (n)
{
{
path = g_strjoin ("/", data->full_kvp_path, key, NULL);
data->str =
g_strdup_printf ("type='%s', value='%s' WHERE path='%s' and ",
key, kvp_value_to_bare_string (val), path);
DEBUG (" %s", data->str);
break;
}
{
path = g_strjoin ("/", data->full_kvp_path, key, NULL);
g_free (data->full_kvp_path);
data->full_kvp_path = path;
break;
}
default:
{
PERR (" unsupported value = %d", kvp_value_get_type (val));
break;
}
}
LEAVE (" %s", data->str);
}
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.

{
/* Handle the entity GUID. Ensure that reference GUIDs
must not also try to be primary keys and can be NULL. */
if ((0 == safe_strcmp (param->param_type, QOF_TYPE_GUID)) &&
(0 == safe_strcmp (param->param_name, QOF_PARAM_GUID)))
return g_strdup_printf (" %s char(32) primary key not null",
param->param_name);
if (0 == safe_strcmp (param->param_type, QOF_TYPE_GUID))
return g_strdup_printf (" %s char(32)", param->param_name);
/* avoid creating database fields for calculated values */
if (!param->param_setfcn)
return NULL;
if (0 == safe_strcmp (param->param_type, QOF_TYPE_STRING))
return g_strdup_printf (" %s mediumtext", param->param_name);
if (0 == safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN))
return g_strdup_printf (" %s int", param->param_name);
if ((0 == safe_strcmp (param->param_type, QOF_TYPE_NUMERIC))
|| (0 == safe_strcmp (param->param_type, QOF_TYPE_DOUBLE))
|| (0 == safe_strcmp (param->param_type, QOF_TYPE_DEBCRED)))
{
return g_strdup_printf (" %s text", param->param_name);
}
if (0 == safe_strcmp (param->param_type, QOF_TYPE_INT32))
return g_strdup_printf (" %s int", param->param_name);
if (0 == safe_strcmp (param->param_type, QOF_TYPE_TIME))
return g_strdup_printf (" %s datetime", param->param_name);
if (0 == safe_strcmp (param->param_type, QOF_TYPE_CHAR))
return g_strdup_printf (" %s char(1)", param->param_name);
/* kvp data is stored separately - actually this is really
a no-op because entities do not need a param_setfcn for kvp data. */
if (0 == safe_strcmp (param->param_type, QOF_TYPE_KVP))
return g_strdup ("");
if (0 == safe_strcmp (param->param_type, QOF_TYPE_COLLECT))
return g_strdup ("");
// return g_strdup_printf (" %s char(32)", param->param_name);
/* catch references */
return g_strdup_printf (" %s char(32)", param->param_name);
}

Variable Documentation

gchar* kvp_table_name = NULL
static

One KVP table per file for all instances.

Definition at line 51 of file qofsql.c.