QOF
0.8.7
|
Typedefs | |
typedef struct _QofSqlQuery | QofSqlQuery |
Enumerations | |
enum | QsqlStatementType { SQL_NONE = 0, SQL_CREATE, SQL_LOAD, SQL_WRITE, SQL_INSERT, SQL_DELETE, SQL_UPDATE } |
Functions | |
void | qof_sql_entity_set_kvp_tablename (const gchar *name) |
Set a default KVP table name for each backend. More... | |
void | qof_sql_entity_set_kvp_id (gulong id) |
Set the initial index value of the KVP table. More... | |
gulong | qof_sql_entity_get_kvp_id (void) |
Get the index value of the KVP table after the operation(s). More... | |
void | qof_sql_entity_set_kvp_exists (gboolean exist) |
Set or clear a flag that the KVP table exists or not. More... | |
gchar * | qof_sql_entity_create_table (QofEntity *ent) |
Build a SQL 'CREATE' statement for this entity. More... | |
gchar * | qof_sql_entity_insert (QofEntity *ent) |
Build a SQL 'INSERT' statement for this entity. More... | |
gchar * | qof_sql_entity_update (QofEntity *ent) |
Build a SQL 'UPDATE' statement for the current entity parameter. More... | |
gchar * | qof_sql_entity_update_kvp (QofEntity *ent) |
Build a SQL 'UPDATE' statement for the KVP data in this entity. More... | |
gchar * | qof_sql_entity_update_list (QofEntity *ent, GList **params) |
Build a SQL 'UPDATE' statement for a list of parameters. More... | |
gchar * | qof_sql_entity_delete (QofEntity *ent) |
Build a SQL 'DELETE' statement for this entity. More... | |
gchar * | qof_sql_entity_drop_table (QofEntity *ent) |
Build a SQL 'DROP' statement for this entity type. More... | |
gchar * | qof_sql_object_create_table (QofObject *obj) |
Build a SQL 'CREATE' statement for this object. More... | |
QofSqlQuery * | qof_sql_query_new (void) |
void | qof_sql_query_destroy (QofSqlQuery *) |
void | qof_sql_query_set_book (QofSqlQuery *q, QofBook *book) |
GList * | qof_sql_query_run (QofSqlQuery *query, const gchar *str) |
Perform the query, return the results. More... | |
void | qof_sql_query_parse (QofSqlQuery *query, const gchar *str) |
QofQuery * | qof_sql_query_get_query (QofSqlQuery *) |
GList * | qof_sql_query_rerun (QofSqlQuery *query) |
void | qof_sql_query_set_kvp (QofSqlQuery *, KvpFrame *) |
The qof_sql_entity* functions are private - only accessible to QOF backends. The purpose is to make it easier for SQL-based backends to pass SQL commands to the relevant database. There is currently no QOF support for reading the entities back from the backend as each backend has specialized methods for data retrieval (GDA has GdaDataModel, sqlite uses **columnNames etc.) Actually, it is generally easier to read data from a SQL based backend than it is to create, update or delete data.
The types of SQL queries that are allowed at this point are very limited. In general, only the following types of queries are supported: SELECT * FROM SomeObj WHERE (param_a < 10.0) AND (param_b = "asdf") SORT BY param_c DESC; INSERT INTO SomeObj (param_a, param_b, param_c) VALUES ("value_a", true, "0/1");
For SELECT, the returned list is a list of all of the instances of 'SomeObj' that match the query. The 'SORT' term is optional. The 'WHERE' term is optional; but if you don't include 'WHERE', you will get a list of all of the object instances. The Boolean operations 'AND' and 'OR' together with parenthesis can be used to construct arbitrarily nested predicates.
For INSERT, the returned list is a list containing the newly created instance of 'SomeObj'.
Joins are not supported directly. SELECT * FROM ObjA,ObjB WHERE (ObjA.param_id = ObjB.param_other_id); The problem with the above is that the search requires a nested search loop, aka a 'join', which is not currently supported in the underlying QofQuery code.
However, by repeating queries and adding the entities to a new session using qof_entity_copy_list, a series of queries can be added to a single book. e.g. You can insert multiple entities and save out as a QSF XML file or use multiple SELECT queries to build a precise list - this can be used to replicate most of the functionality of a SQL join.
SELECT * from ObjA where param_id = value; SELECT * from ObjB where param_other_id = value;
Equivalent to: SELECT * from ObjA,ObjB where param_id = param_other_id and param_id = value;
When combined with a foreach callback on the value of param_id for each entity in the QofBook, you can produce the effect of a join from running the two SELECT queries for each value of param_id held in 'value'.
See QofEntityForeachCB and qof_object_foreach.
Date queries handle full date and time strings, using the format exported by the QSF backend. To query dates and times, convert user input into UTC time using the QOF_UTC_DATE_FORMAT string. See qof_date_print
If the param is a KVP frame, then we use a special markup to indicate frame values. The markup should look like /some/kvp/path:value. Thus, for example, SELECT * FROM SomeObj WHERE (param_a < '/some/kvp:10.0') will search for the object where param_a is a KVP frame, and this KVP frame contains a path '/some/kvp' and the value stored at that path is floating-point and that float value is less than 10.0.
The following are types of queries are NOT supported: SELECT a,b,c FROM ... I am thinking of implementing the above as a list of KVP's whose keys would be a,b,c and values would be the results of the search. (Linas)
XXX (Neil W). Alternatively, I need to use something like this when converting QOF objects between applications by using the returned parameter values to create a second object. One application using QOF could register objects from two applications and convert data from one to the other by using SELECT a,b,c FROM ObjA; SELECT d,f,k FROM ObjB; qof_object_new_instance(); ObjC_set_a(value_c); ObjC_set_b(value_k) etc. What's needed is for the SELECT to return a complete object that only contains the parameters selected.
Also unsupported: UPDATE.
Certain SQL commands can have no QOF equivalent and will generate a runtime parser error:
enum QsqlStatementType |
Definition at line 54 of file qofsql-p.h.
gchar* qof_sql_entity_create_table | ( | QofEntity * | ent | ) |
Build a SQL 'CREATE' statement for this entity.
Prepares a SQL statement that will create a table for this entity.
Definition at line 1368 of file qofsql.c.
gchar* qof_sql_entity_delete | ( | QofEntity * | ent | ) |
Build a SQL 'DELETE' statement for this entity.
Prepares a SQL statement that will delete the row for this entity into the appropriate table (which must already exist). The data for the entity must already have been INSERTed into the table.
Also deletes all KVP data for this entity.
Definition at line 1535 of file qofsql.c.
gchar* qof_sql_entity_drop_table | ( | QofEntity * | ent | ) |
gulong qof_sql_entity_get_kvp_id | ( | void | ) |
Get the index value of the KVP table after the operation(s).
Each backend table has an ID number for KVP entries as one QofEntity can have multiple KvpFrames. The ID number is mapped to the GUID of the entity when reading data back from the table.
The ID is incremented after each call to qof_sql_entity_insert where qof_instance_get_slots does not return an empty frame.
gchar* qof_sql_entity_insert | ( | QofEntity * | ent | ) |
Build a SQL 'INSERT' statement for this entity.
Prepares a SQL statement that will insert data for this entity into the appropriate table (which must already exist).
Definition at line 1389 of file qofsql.c.
void qof_sql_entity_set_kvp_exists | ( | gboolean | exist | ) |
Set or clear a flag that the KVP table exists or not.
The KVP table should only be created once per session - use this flag to indicate that the KVP table has been successfully created (or deleted).
qof_sql_entity_create_table will not attempt to create the KVP table if this flag is set. It is up to the backend to control this flag.
void qof_sql_entity_set_kvp_id | ( | gulong | id | ) |
Set the initial index value of the KVP table.
Each backend table has an ID number for KVP entries as one QofEntity can have multiple KvpFrames. The ID number is mapped to the GUID of the entity when reading data back from the table.
The ID is incremented after each call to qof_sql_entity_insert where qof_instance_get_slots does not return an empty frame.
void qof_sql_entity_set_kvp_tablename | ( | const gchar * | name | ) |
gchar* qof_sql_entity_update | ( | QofEntity * | ent | ) |
Build a SQL 'UPDATE' statement for the current entity parameter.
Prepares a SQL statement that will update a single parameter for this entity into the appropriate table (which must already exist). The data for the entity must already have been INSERTed into the table.
Definition at line 1460 of file qofsql.c.
gchar* qof_sql_entity_update_kvp | ( | QofEntity * | ent | ) |
Build a SQL 'UPDATE' statement for the KVP data in this entity.
Prepares a SQL statement that will update the KVP data for this entity (if any) into the KVP table.
This is a separate function because the KVP data can be modified independently of other parameters and updating a parameter should not cause an unwanted SQL operation on unchanged KVP data. If you know that the KVP data has changed, concatenate the two SQL commands into one.
Definition at line 1495 of file qofsql.c.
gchar* qof_sql_entity_update_list | ( | QofEntity * | ent, |
GList ** | params | ||
) |
Build a SQL 'UPDATE' statement for a list of parameters.
Prepares a SQL statement that will update the specified parameters for this entity into the appropriate table (which must already exist). The data for the entity must already have been INSERTed into the table.
gchar* qof_sql_object_create_table | ( | QofObject * | obj | ) |
Build a SQL 'CREATE' statement for this object.
Prepares a SQL statement that will create a table for this object for those times when an entity does not yet exist.
Definition at line 1346 of file qofsql.c.
QofQuery* qof_sql_query_get_query | ( | QofSqlQuery * | ) |
QofSqlQuery* qof_sql_query_new | ( | void | ) |
Create a new SQL-syntax query machine.
void qof_sql_query_parse | ( | QofSqlQuery * | query, |
const gchar * | str | ||
) |
Same qof_sql_query_run, but just parse/pre-process the query; do not actually run it over the dataset. The QofBook does not need to be set before calling this function.
GList* qof_sql_query_rerun | ( | QofSqlQuery * | query | ) |
Run the previously parsed query. The QofBook must be set before this function can be called. Note, teh QofBook can be changed between each successive call to this routine. This routine can be called after either qof_sql_query_parse() or qof_sql_query_run() because both will set up the parse.
Definition at line 1044 of file qofsql.c.
GList* qof_sql_query_run | ( | QofSqlQuery * | query, |
const gchar * | str | ||
) |
Perform the query, return the results.
The book must be set in order to be able to perform a query.
The returned list will have been sorted using the indicated sort order, (by default ascending order) and trimmed to the max_results length. Do NOT free the resulting list. This list is managed internally by QofSqlQuery.
void qof_sql_query_set_book | ( | QofSqlQuery * | q, |
QofBook * | book | ||
) |
void qof_sql_query_set_kvp | ( | QofSqlQuery * | , |
KvpFrame * | |||
) |
Set the kvp frame to be used for formulating 'indirect' predicates.
Although joins are not supported (see above), there is one special hack that one can use to pass data indirectly into the predicates. This is by using a KVP key name to reference the value to be used for a predicate. Thus, for example, SELECT * FROM SomeObj WHERE (param_a = KVP:/some/key/path); will look up the value stored at '/some/key/path', and use that value to form the actual predicate. So, for example, if the value stored at '/some/key/path' was 2, then the actual query run will be SELECT * FROM SomeObj WHERE (param_a = 2); The lookup occurs at the time that the query is formulated.
The query does not take over ownership of the kvp frame, nor does it copy it. Thus, the kvp frame must exist when the query is formulated, and it is the responsibility of the caller to free it when no longer needed.
Note that because this feature is a kind of a hack put in place due to the lack of support for joins, it will probably go away (be deprecated) if/when joins are implemented.