Top |
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 GdaSqlBuilder 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 GdaSqlBuilder object, and each is identified using a single unsigned integer ID. That ID is dynamically allocated by the object.
The following example builds the equivalent of the "name='joe' AND age >= #<link linkend="ageparam-int"><type>“int”</type></link>" expression:
GdaSqlBuilder *b=... guint id_field = gda_sql_builder_add_id (b, "name"); // build the "name" SQL identifier guint id_value = gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "joe"); // 'joe' expression guint id_cond1 = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_EQ, id_field, id_value, 0); // "name='joe'" guint id_cond2 = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_GT, gda_sql_builder_add_id (b, "age"), // build the "age" SQL identifier gda_sql_builder_add_param (b, "ageparam", G_TYPE_INT, FALSE), // parameter 0); guint id_cond_and = gda_sql_builder_add_cond (b, GDA_SQL_OPERATOR_TYPE_AND, id_cond1, id_cond2, 0); // whole expression
For more examples, see the Build statements without using a parser section.
GdaSqlBuilder *
gda_sql_builder_new (GdaSqlStatementType stmt_type
);
Create a new GdaSqlBuilder object to build GdaStatement or GdaSqlStatement
objects of type stmt_type
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.
Since: 4.2
GdaSqlStatement *
gda_sql_builder_get_sql_statement (GdaSqlBuilder *builder
);
Creates a new GdaSqlStatement structure from builder
's contents.
The returned pointer belongs to builder
's internal representation.
Use gda_sql_statement_copy()
if you need to keep it.
[skip]
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.
Since: 4.2
void gda_sql_builder_add_field_value (GdaSqlBuilder *builder
,const gchar *field_name
,GType type
,...
);
Valid only for: INSERT, UPDATE statements.
Specifies that the field represented by field_name
will be set to the value identified
by @... of type type
. See gda_sql_builder_add_expr()
for more information.
This is a C convenience function. See also gda_sql_builder_add_field_value_as_gvalue()
.
builder |
a GdaSqlBuilder object |
|
field_name |
a field name |
|
type |
the GType of the following argument |
|
... |
value to set the field to, of the type specified by |
Since: 4.2
void gda_sql_builder_add_field_value_as_gvalue (GdaSqlBuilder *builder
,const gchar *field_name
,const GValue *value
);
Valid only for: INSERT, UPDATE statements.
Specifies that the field represented by field_name
will be set to the value identified
by value
builder |
a GdaSqlBuilder object |
|
field_name |
a field name |
|
value |
value to set the field to, or |
[nullable] |
Since: 4.2
void gda_sql_builder_add_field_value_id (GdaSqlBuilder *builder
,GdaSqlBuilderId field_id
,GdaSqlBuilderId value_id
);
Valid only for: INSERT, UPDATE, SELECT statements
For UPDATE: specifies that the field represented by field_id
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
For INSERT: if field_id
represents an SQL identifier (obtained using gda_sql_builder_add_id()
): then if
value_id
is not 0
then specifies that the field represented by field_id
will be set to the
value identified by value_id
, otherwise just specifies a named field to be given a value.
If field_id
represents a sub SELECT (obtained using gda_sql_builder_add_sub_select()
), then
this method call defines the sub SELECT from which values to insert are taken.
See also gda_sql_builder_add_field_value()
and gda_sql_builder_add_field_value_as_gvalue()
.
builder |
a GdaSqlBuilder object |
|
field_id |
the ID of the field's name or definition |
|
value_id |
the ID of the value to set the field to, or |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_function (GdaSqlBuilder *builder
,const gchar *func_name
,...
);
Builds a new expression which represents a function applied to some arguments
builder |
a GdaSqlBuilder object |
|
func_name |
the functions's name |
|
... |
a list, terminated with |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_function_v (GdaSqlBuilder *builder
,const gchar *func_name
,const GdaSqlBuilderId *args
,gint args_size
);
Builds a new expression which represents a function applied to some arguments
[rename-to gda_sql_builder_add_function]
builder |
a GdaSqlBuilder object |
|
func_name |
the functions's name |
|
args |
an array of IDs representing the function's arguments. |
[array length=args_size] |
args_size |
|
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_id (GdaSqlBuilder *builder
,const gchar *str
);
Defines an expression representing an identifier in builder
,
which may be reused to build other parts of a statement,
for instance as a parameter to gda_sql_builder_add_cond()
or
gda_sql_builder_add_field_value_id()
.
The new expression will contain the str
literal.
For example:
gda_sql_builder_add_id (b, "name") gda_sql_builder_add_id (b, "date")
will be rendered as SQL as:
name "date"
because "date" is an SQL reserved keyword.
For fields, see gda_sql_builder_add_field_id()
.
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_field_id (GdaSqlBuilder *builder
,const gchar *field_name
,const gchar *table_name
);
Defines an expression representing a field in builder
,
which may be reused to build other parts of a statement,
for instance as a parameter to gda_sql_builder_add_cond()
or
gda_sql_builder_add_field_value_id()
.
Calling this with a NULL
table_name
is equivalent to calling gda_sql_builder_add_id()
.
For SELECT queries, see gda_sql_builder_select_add_field()
.
builder |
a GdaSqlBuilder object |
|
field_name |
a field name |
|
table_name |
a table name, or |
[nullable] |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_expr (GdaSqlBuilder *builder
,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.
If type
is G_TYPE_STRING then it is possible to customize how the value has to be interpreted by passing a
specific GdaDataHandler object as dh
. This feature is very rarely used and the dh
argument should generally
be NULL
.
Note that for composite types such as GdaNumeric, Gdate, GdaTime, ... pointer to these structures are expected, they should no be passed by value. For example:
GDate *date = g_date_new_dmy (27, G_DATE_MAY, 1972); id = gda_sql_builder_add_expr (b, NULL, G_TYPE_DATE, date); g_date_free (date); id = gda_sql_builder_add_expr (b, NULL, G_TYPE_STRING, "my string"); id = gda_sql_builder_add_expr (b, NULL, G_TYPE_INT, 25);
will correspond in SQL to:
'05-27-1972' 'my string' 25
[skip]
builder |
a GdaSqlBuilder object |
|
dh |
deprecated useless argument, just pass |
[nullable] |
type |
the GType of the following argument |
|
... |
value to set the expression to, of the type specified by |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_expr_value (GdaSqlBuilder *builder
,GdaDataHandler *dh
,const GValue *value
);
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 value
argument.
If value
's type is a string then it is possible to customize how the value has to be interpreted by passing a
specific GdaDataHandler object as dh
. This feature is very rarely used and the dh
argument should generally
be NULL
.
builder |
a GdaSqlBuilder object |
|
dh |
deprecated useless argument, just pass |
[nullable] |
value |
value to set the expression to, or |
[nullable] |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_param (GdaSqlBuilder *builder
,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_add_param (b, "age", G_TYPE_INT, FALSE)
will be rendered as SQL as:
##age::int
builder |
a GdaSqlBuilder object |
|
param_name |
parameter's name |
|
type |
parameter's type |
|
nullok |
TRUE if the parameter can be set to |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_case (GdaSqlBuilder *builder
,GdaSqlBuilderId test_expr
,GdaSqlBuilderId else_expr
,...
);
Creates a new CASE ... WHEN ... THEN ... ELSE ... END expression.
builder |
a GdaSqlBuilder object |
|
test_expr |
the expression ID representing the test of the CASE, or |
|
else_expr |
the expression ID representing the ELSE expression, or |
|
... |
a list, terminated by a |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_case_v (GdaSqlBuilder *builder
,GdaSqlBuilderId test_expr
,GdaSqlBuilderId else_expr
,const GdaSqlBuilderId *when_array
,const GdaSqlBuilderId *then_array
,gint args_size
);
Creates a new CASE ... WHEN ... THEN ... ELSE ... END expression. The WHEN expression and the THEN
expression IDs are taken from the when_array
and then_array
at the same index, for each index inferior to
args_size
.
[rename-to gda_sql_builder_add_case]
builder |
a GdaSqlBuilder object |
|
test_expr |
the expression ID representing the test of the CASE, or |
|
else_expr |
the expression ID representing the ELSE expression, or |
|
when_array |
an array containing each WHEN expression ID, having at least |
[array length=args_size] |
then_array |
an array containing each THEN expression ID, having at least |
[array length=args_size] |
args_size |
the size of |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_sub_select (GdaSqlBuilder *builder
,GdaSqlStatement *sqlst
);
Adds an expression which is a subselect.
[skip]
builder |
a GdaSqlBuilder object |
|
sqlst |
a pointer to a GdaSqlStatement, which has to be a SELECT or compound SELECT. This will be copied. |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_cond (GdaSqlBuilder *builder
,GdaSqlOperatorType op
,GdaSqlBuilderId op1
,GdaSqlBuilderId op2
,GdaSqlBuilderId op3
);
Builds a new expression which represents a condition (or operation).
builder |
a GdaSqlBuilder object |
|
op |
type of condition |
|
op1 |
the ID of the 1st argument (not 0) |
|
op2 |
the ID of the 2nd argument (may be |
|
op3 |
the ID of the 3rd argument (may be |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_add_cond_v (GdaSqlBuilder *builder
,GdaSqlOperatorType op
,const GdaSqlBuilderId *op_ids
,gint op_ids_size
);
Builds a new expression which represents 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).
builder |
a GdaSqlBuilder object |
|
op |
type of condition |
|
op_ids |
an array of ID for the arguments (not |
[array length=op_ids_size] |
op_ids_size |
size of |
Since: 4.2
void gda_sql_builder_set_where (GdaSqlBuilder *builder
,GdaSqlBuilderId cond_id
);
Valid only for: UPDATE, DELETE, SELECT statements
Sets the WHERE condition of the statement
builder |
a GdaSqlBuilder object |
|
cond_id |
the ID of the expression to set as WHERE condition, or 0 to unset any previous WHERE condition |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_select_add_target (GdaSqlBuilder *builder
,const gchar *table_name
,const gchar *alias
);
Adds a new target to a SELECT statement
builder |
a GdaSqlBuilder object |
|
table_name |
the name of the target table |
|
alias |
the alias to give to the target, or |
[nullable] |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_select_add_target_id (GdaSqlBuilder *builder
,GdaSqlBuilderId table_id
,const gchar *alias
);
Adds a new target to a SELECT statement. If there already exists a target representing the same table and the same alias (or with the same absence of alias) then the same target ID is returned instead of the ID of a new target.
builder |
a GdaSqlBuilder object |
|
table_id |
the ID of the expression holding a table reference (not |
|
alias |
the alias to give to the target, or |
[nullable] |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_select_add_field (GdaSqlBuilder *builder
,const gchar *field_name
,const gchar *table_name
,const gchar *alias
);
Valid only for: SELECT statements.
Add a selected selected item to the SELECT statement.
For non-SELECT statements, see gda_sql_builder_add_field_id()
.
builder |
a GdaSqlBuilder object |
|
field_name |
a field name |
|
table_name |
a table name, or |
[nullable] |
alias |
an alias (eg. for the "AS" clause), or |
[nullable] |
Since: 4.2
GdaSqlBuilderId gda_sql_builder_select_join_targets (GdaSqlBuilder *builder
,GdaSqlBuilderId left_target_id
,GdaSqlBuilderId right_target_id
,GdaSqlSelectJoinType join_type
,GdaSqlBuilderId join_expr
);
Joins two targets in a SELECT statement, using the join_type
type of join.
Note: if the target represented by left_target_id
is actually situated after (on the right) of
the target represented by right_target_id
, then the actual type of join may be switched from
GDA_SQL_SELECT_JOIN_LEFT
to GDA_SQL_SELECT_JOIN_RIGHT
or from GDA_SQL_SELECT_JOIN_RIGHT
to
GDA_SQL_SELECT_JOIN_LEFT
.
[skip]
builder |
a GdaSqlBuilder object |
|
left_target_id |
the ID of the left target to use (not |
|
right_target_id |
the ID of the right target to use (not |
|
join_type |
the type of join |
|
join_expr |
joining expression's ID, or |
Since: 4.2
void gda_sql_builder_join_add_field (GdaSqlBuilder *builder
,GdaSqlBuilderId join_id
,const gchar *field_name
);
Alter a join in a SELECT statement to make its condition use equal field
values in the fields named field_name
in both tables, via the USING keyword.
builder |
a GdaSqlBuilder object |
|
join_id |
the ID of the join to modify (not |
|
field_name |
the name of the field to use in the join condition (not |
Since: 4.2
void gda_sql_builder_select_order_by (GdaSqlBuilder *builder
,GdaSqlBuilderId expr_id
,gboolean asc
,const gchar *collation_name
);
Adds a new ORDER BY expression to a SELECT statement.
Since: 4.2
void gda_sql_builder_select_set_distinct (GdaSqlBuilder *builder
,gboolean distinct
,GdaSqlBuilderId expr_id
);
Defines (if distinct
is TRUE
) or removes (if distinct
is FALSE
) a DISTINCT clause
for a SELECT statement.
If distinct
is TRUE
, then the ID of an expression can be specified as the expr_id
argument:
if not 0
, this is the expression used to apply the DISTINCT clause on (the resuting SQL
will then usually be "... DISTINCT ON <expression>...").
builder |
a GdaSqlBuilder object |
|
distinct |
set to |
|
expr_id |
the ID of the DISTINCT ON expression, or |
Since: 4.2
void gda_sql_builder_select_set_limit (GdaSqlBuilder *builder
,GdaSqlBuilderId limit_count_expr_id
,GdaSqlBuilderId limit_offset_expr_id
);
If limit_count_expr_id
is not 0
, defines the maximum number of rows in the GdaDataModel
resulting from the execution of the built statement. In this case, the offset from which the
rows must be collected can be defined by the limit_offset_expr_id
expression if not 0
(note that
this feature may not be supported by all the database providers).
If limit_count_expr_id
is 0
, then removes any LIMIT which may have been imposed by a previous
call to this method.
builder |
a GdaSqlBuilder object |
|
limit_count_expr_id |
the ID of the LIMIT expression, or |
|
limit_offset_expr_id |
the ID of the OFFSET expression, or |
Since: 4.2
void gda_sql_builder_select_set_having (GdaSqlBuilder *builder
,GdaSqlBuilderId cond_id
);
Valid only for: SELECT statements
Sets the HAVING condition of the statement
builder |
a GdaSqlBuilder object |
|
cond_id |
the ID of the expression to set as HAVING condition, or 0 to unset any previous HAVING condition |
Since: 4.2
void gda_sql_builder_select_group_by (GdaSqlBuilder *builder
,GdaSqlBuilderId expr_id
);
Valid only for: SELECT statements
Adds the expr_id
expression to the GROUP BY clause's expressions list
builder |
a GdaSqlBuilder object |
|
expr_id |
the ID of the expression to set use in the GROUP BY clause, or 0 to unset any previous GROUP BY clause |
Since: 4.2
void gda_sql_builder_compound_add_sub_select (GdaSqlBuilder *builder
,GdaSqlStatement *sqlst
);
Add a sub select to a COMPOUND statement
[skip]
builder |
a GdaSqlBuilder object |
|
sqlst |
a pointer to a GdaSqlStatement, which has to be a SELECT or compound SELECT. This will be copied. |
Since: 4.2
void gda_sql_builder_compound_set_type (GdaSqlBuilder *builder
,GdaSqlStatementCompoundType compound_type
);
Changes the type of compound which builder
is making, for a COMPOUND statement
Since: 4.2
GdaSqlExpr * gda_sql_builder_export_expression (GdaSqlBuilder *builder
,GdaSqlBuilderId id
);
Exports a part managed by builder
as a new GdaSqlExpr, which can represent any expression
in a statement.
[skip]
builder |
a GdaSqlBuilder object |
|
id |
the ID of the expression to be exported, (must be a valid ID in |
a pointer to a new GdaSqlExpr structure, free using gda_sql_expr_free()
when not
needed anymore. If the part with id
as ID cannot be found, the returned value is NULL
.
Since: 4.2
GdaSqlBuilderId gda_sql_builder_import_expression (GdaSqlBuilder *builder
,GdaSqlExpr *expr
);
Imports the expr
into builder
.
[skip]
builder |
a GdaSqlBuilder object |
|
expr |
a GdaSqlExpr obtained using |
Since: 4.2
“stmt-type”
property“stmt-type” GdaSqlStatementType
Specifies the type of statement to be built, can only be GDA_SQL_STATEMENT_SELECT, GDA_SQL_STATEMENT_INSERT, GDA_SQL_STATEMENT_UPDATE or GDA_SQL_STATEMENT_DELETE
Owner: GdaSqlBuilder
Flags: Write / Construct Only
Default value: GDA_SQL_STATEMENT_UNKNOWN