![]() |
![]() |
![]() |
GNOME Data Access 4 manual | ![]() |
---|---|---|---|---|
Top | Description | Object Hierarchy |
GdaSqlBuilder; GdaSqlBuilder * gda_sql_builder_new (GdaSqlStatementType stmt_type); GdaStatement * gda_sql_builder_get_statement (GdaSqlBuilder *builder, GError **error); GdaSqlStatement * gda_sql_builder_get_sql_statement (GdaSqlBuilder *builder, gboolean copy_it); void gda_sql_builder_set_table (GdaSqlBuilder *builder, const gchar *table_name); void gda_sql_builder_add_field (GdaSqlBuilder *builder, guint field_id, guint value_id); guint gda_sql_builder_ident (GdaSqlBuilder *builder, guint id, const gchar *string); guint gda_sql_builder_expr (GdaSqlBuilder *builder, guint id, GdaDataHandler *dh, GType type, ...); guint gda_sql_builder_param (GdaSqlBuilder *builder, guint id, const gchar *param_name, GType type, gboolean nullok); guint gda_sql_builder_cond (GdaSqlBuilder *builder, guint id, GdaSqlOperatorType op, guint op1, guint op2, guint op3); guint gda_sql_builder_cond_v (GdaSqlBuilder *builder, guint id, GdaSqlOperatorType op, guint *op_ids, gint op_ids_size); void gda_sql_builder_set_where (GdaSqlBuilder *builder, guint cond_id); guint gda_sql_builder_select_add_target (GdaSqlBuilder *builder, guint id, guint table_id, const gchar *alias); guint gda_sql_builder_select_join_targets (GdaSqlBuilder *builder, guint id, guint left_target_id, guint right_target_id, GdaSqlSelectJoinType join_type, guint join_expr); void gda_sql_builder_join_add_field (GdaSqlBuilder *builder, guint join_id, const gchar *field_name); void gda_sql_builder_select_order_by (GdaSqlBuilder *builder, guint expr_id, gboolean asc, const gchar *collation_name);
The GdaSqlBuilder can be used to build a GdaStatement from its structural description, much in the same way a GdaSqlParser can be used to build a GdaStatement from an SQL string.
The GdaBuilder internally constructs a GdaSqlStatement and uses it when requested to produce
a GdaStatement (see gda_sql_builder_get_statement()
), or a GdaSqlStatement (see
gda_sql_builder_get_sql_statement()
).
During the building process, some pieces of the statement are constructed, and assembled into the final statement. Each of these pieces can be reused anytime in the same GdaBuilder object, and each is identified using a single unsigned integer ID. That ID can either be specified by the user, or be dynamically allocated by the object (pass 0 as the requested ID).
The following example builds the equivalent of the "name='joe' AND age >= ##ageparam::int" expression:
GdaSqlBuilder *b=... gda_sql_builder_ident (b, 1, "name"); // build the "name" SQL identifier with requested ID=1 gda_sql_builder_expr (b, 2, NULL, G_TYPE_STRING, "joe"); // 'joe' expression, requested ID=2 gda_sql_builder_cond2 (b, 3, GDA_SQL_OPERATOR_TYPE_EQ, 1, 2); // "name='joe'", requested ID=3 gda_sql_builder_cond2 (b, 4, GDA_SQL_OPERATOR_TYPE_GT, // requested ID=4 gda_sql_builder_ident (b, 0, "age"), // build the "age" SQL identifier, no specific ID gda_sql_builder_param (b, 0, "ageparam", G_TYPE_INT, FALSE) // parameter, no specific ID ); gda_sql_builder_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expression, requested ID=5
GdaSqlBuilder * gda_sql_builder_new (GdaSqlStatementType stmt_type);
Create a new GdaSqlBuilder object to build GdaStatement or GdaSqlStatement
objects of type stmt_type
|
the type of statement to build |
Returns : |
the newly created object, or NULL if an error occurred (such as unsupported
statement type)
|
Since 4.2
GdaStatement * gda_sql_builder_get_statement (GdaSqlBuilder *builder, GError **error);
Creates a new GdaStatement statement from builder
's contents.
|
a GdaSqlBuilder object |
|
a place to store errors, or NULL
|
Returns : |
a new GdaStatement object, or NULL if an error occurred
|
Since 4.2
GdaSqlStatement * gda_sql_builder_get_sql_statement (GdaSqlBuilder *builder, gboolean copy_it);
Creates a new GdaSqlStatement structure from builder
's contents.
If copy_it
is FALSE
, then the returned pointer is considered to be stolen from builder
's internal
representation and will make it unusable anymore (resulting in a GDA_SQL_BUILDER_MISUSE_ERROR
error or
some warnings if one tries to reuse it).
If, on the other
hand it is set to TRUE
, then the returned GdaSqlStatement is a copy of the on builder
uses
internally, making it reusable.
|
a GdaSqlBuilder object |
|
set to TRUE to be able to reuse builder
|
Returns : |
a GdaSqlStatement pointer |
Since 4.2
void gda_sql_builder_set_table (GdaSqlBuilder *builder, const gchar *table_name);
Valid only for: INSERT, UPDATE, DELETE statements
Sets the name of the table on which the built statement operates.
|
a GdaSqlBuilder object |
|
a table name |
Since 4.2
void gda_sql_builder_add_field (GdaSqlBuilder *builder, guint field_id, guint value_id);
Valid only for: INSERT, UPDATE, SELECT statements
For INSERT and UPDATE: specifies that the field named field_name
will be set to the value identified by value_id
.
For SELECT: add a selected item to the statement, and if value_id
is not 0
, then use it as an alias
|
a GdaSqlBuilder object |
|
the ID of the field's name or definition |
|
the ID of the value to set the field to |
Since 4.2
guint gda_sql_builder_ident (GdaSqlBuilder *builder, guint id, const gchar *string);
Defines an expression in builder
which may be reused to build other parts of a statement.
The new expression will contain the string
literal.
For example:
gda_sql_builder_expr_liretal (b, 0, "name")
will be rendered as SQL as:
name
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
a string |
Returns : |
the ID of the new expression, or 0 if there was an error |
Since 4.2
guint gda_sql_builder_expr (GdaSqlBuilder *builder, guint id, GdaDataHandler *dh, GType type, ...);
Defines an expression in builder
which may be reused to build other parts of a statement.
The new expression will contain the value passed as the @... argument. It is possible to
customize how the value has to be interpreted by passing a specific GdaDataHandler object as dh
.
For example:
gda_sql_builder_expr (b, 0, G_TYPE_INT, 15); gda_sql_builder_expr (b, 5, G_TYPE_STRING, "joe")
will be rendered as SQL as:
15 'joe'
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
a GdaDataHandler to use, or NULL
|
|
the GType of the following argument |
|
value to set the expression to, of the type specified by type
|
Returns : |
the ID of the new expression, or 0 if there was an error |
Since 4.2
guint gda_sql_builder_param (GdaSqlBuilder *builder, guint id, const gchar *param_name, GType type, gboolean nullok);
Defines a parameter in builder
which may be reused to build other parts of a statement.
The new expression will contain the string
literal.
For example:
gda_sql_builder_param (b, 0, "age", G_TYPE_INT, FALSE)
will be rendered as SQL as:
##age::int
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
parameter's name |
|
parameter's type |
|
TRUE if the parameter can be set to NULL
|
Returns : |
the ID of the new expression, or 0 if there was an error |
Since 4.2
guint gda_sql_builder_cond (GdaSqlBuilder *builder, guint id, GdaSqlOperatorType op, guint op1, guint op2, guint op3);
Builds a new expression which reprenents a condition (or operation).
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
type of condition |
|
the ID of the 1st argument (not 0) |
|
the ID of the 2nd argument (may be 0 if op needs only one operand)
|
|
the ID of the 3rd argument (may be 0 if op needs only one or two operand)
|
Returns : |
the ID of the new expression, or 0 if there was an error |
Since 4.2
guint gda_sql_builder_cond_v (GdaSqlBuilder *builder, guint id, GdaSqlOperatorType op, guint *op_ids, gint op_ids_size);
Builds a new expression which reprenents a condition (or operation).
As a side case, if ops_ids_size
is 1,
then op
is ignored, and the returned ID represents op_ids
[0] (this avoids any problem for example
when op
is GDA_SQL_OPERATOR_TYPE_AND and there is in fact only one operand).
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
type of condition |
|
an array of ID for the arguments (not 0 )
|
|
|
Returns : |
the ID of the new expression, or 0 if there was an error |
Since 4.2
void gda_sql_builder_set_where (GdaSqlBuilder *builder, guint cond_id);
Valid only for: UPDATE, DELETE, SELECT statements
Sets the WHERE condition of the statement
|
a GdaSqlBuilder object |
|
the ID of the expression to set as WHERE condition, or 0 to unset any previous WHERE condition |
Since 4.2
guint gda_sql_builder_select_add_target (GdaSqlBuilder *builder, guint id, guint table_id, const gchar *alias);
Adds a new target to a SELECT statement
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
the ID of the expression holding a table reference (not 0 )
|
|
the alias to give to the target, or NULL
|
Returns : |
the ID of the new target, or 0 if there was an error |
Since 4.2
guint gda_sql_builder_select_join_targets (GdaSqlBuilder *builder, guint id, guint left_target_id, guint right_target_id, GdaSqlSelectJoinType join_type, guint join_expr);
Joins two targets in a SELECT statement
|
a GdaSqlBuilder object |
|
the requested ID, or 0 if to be determined by builder
|
|
the ID of the left target to use (not 0 )
|
|
the ID of the right target to use (not 0 )
|
|
the type of join |
|
joining expression's ID, or 0
|
Returns : |
the ID of the new join, or 0 if there was an error |
Since 4.2
void gda_sql_builder_join_add_field (GdaSqlBuilder *builder, guint join_id, const gchar *field_name);
Alter a joins in a SELECT statement to make its condition on the field which name
is field_name
|
a GdaSqlBuilder object |
|
the ID of the join to modify (not 0 )
|
|
the name of the field to use in the join condition (not NULL )
|
Since 4.2
void gda_sql_builder_select_order_by (GdaSqlBuilder *builder, guint expr_id, gboolean asc, const gchar *collation_name);
Adds a new ORDER BY expression to a SELECT statement.
|
a GdaSqlBuiler |
|
the ID of the expression to use during sorting (not 0 )
|
|
TRUE for an ascending sorting
|
|
name of the collation to use when sorting, or NULL
|
Since 4.2