Gnome::Gda::SqlBuilder Class Reference

SqlBuilder can be used to build a Statement from its structural description, much in the same way a SqlParser can be used to build a Statement from a SQL string. More...

Inheritance diagram for Gnome::Gda::SqlBuilder:
[legend]
Collaboration diagram for Gnome::Gda::SqlBuilder:
[legend]

List of all members.

## Public Types

typedef guint | Id

## Public Member Functions

virtual | ~SqlBuilder ()
GdaSqlBuilder* | gobj ()
| Provides access to the underlying C GObject.
const GdaSqlBuilder* | gobj () const
| Provides access to the underlying C GObject.
GdaSqlBuilder* | gobj_copy ()
| Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
Glib::RefPtr< Statement > | get_statement () const
| Creates a new Gda::Statement statement from builder's contents.
SqlStatement | get_sql_statement () const
| Creates a new Gda::SqlStatement structure from builder's contents.
Id | add_id (const Glib::ustring&string)
| 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 add_cond() or add_field_value_id().
Id | add_field_id (const Glib::ustring& field_name, const Glib::ustring& table_name=Glib::ustring())
| 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 add_cond() or add_field_value_id().
template<class ValueType >
Id | add_expr (const Glib::RefPtr< DataHandler >& dh, const ValueType&value)
| Defines an expression in Builder which may be reused to build other parts of a statement.
Id | add_expr_as_value (const Glib::RefPtr< DataHandler >& dh, const Value&value)
| Defines an expression in Builder which may be reused to build other parts of a statement.
template<class ValueType >
Id | add_expr (const ValueType&value)
| Defines an expression in Builder which may be reused to build other parts of a statement.
Id | add_expr_as_value (const Value&value)
| Defines an expression in Builder which may be reused to build other parts of a statement.
Id | add_param (const Glib::ustring& param_name, GType type, bool nullok=false)
| Defines a parameter which may be reused to build other parts of a statement.
Id | add_cond (SqlOperatorType op, Id op1, Id op2=0, Id op3=0)
| Builds a new expression which reprenents a condition (or operation).
Id | add_cond (SqlOperatorType op, const std::vector< Id >& op_ids)
| Builds a new expression which reprenents a condition (or operation).
Id | add_function (const Glib::ustring& function_name, const std::vector< Id >& args)
| Builds a new expression which represents a function applied to some arguments.
Id | add_function (const Glib::ustring& function_name, Id arg)
| Builds a new expression which represents a function applied to an argument.
Id | add_sub_select (const SqlStatement& sqlst)
| Adds an expression which is a subselect.
Id | select_add_field (const Glib::ustring& field_name, const Glib::ustring& table_name, const Glib::ustring& alias=Glib::ustring())
| Valid only for: SELECT statements.
Id | select_add_target (const Glib::ustring& table_name, const Glib::ustring& alias=Glib::ustring())
| Adds a new target to a SELECT statement.
Id | select_add_target_id (Id table_id, const Glib::ustring& alias=Glib::ustring())
| Adds a new target to a SELECT statement.
Id | select_join_targets (Id left_target_id, Id right_target_id, SqlSelectJoinType join_type, Id join_expr=0)
| Joins two targets in a SELECT statement.
void | join_add_field (Id join_id, const Glib::ustring& 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.
void | select_order_by (Id expr_id, bool asc=true, const Glib::ustring& collation_name=Glib::ustring())
| Adds a new ORDER BY expression to a SELECT statement.
void | select_set_distinct (bool distinct=true)
| Adds or removes a DISTINCT clause for a SELECT statement.
void | select_set_distinct (bool distinct, Id expr_id)
| Defines (if distinct is `true`) or removes (if distinct is `false`) a DISTINCT clause for a SELECT statement.
void | select_set_limit (Id limit_count, Id offset=0)
| Defines the maximum number of rows in the DataModel resulting from the execution of the built statement.
void | select_set_limit_id (Id limit_count_expr_id, Id limit_offest_expr_id=0)
| If limit_count_expr_id is not %0, defines the maximum number of rows in the Gda::DataModel resulting from the execution of the built statement.
void | select_set_having (Id cond_id)
| Valid only for: SELECT statements.
void | select_group_by (Id expr_id)
| Valid only for: SELECT statements.
void | set_table (const Glib::ustring& table_name)
| Valid only for: INSERT, UPDATE, DELETE statements.
void | set_where (Id cond_id)
| Valid only for: UPDATE, DELETE, SELECT statements.
void | add_field_value_id (Id field_id, Id value_id=0)
| Valid only for: INSERT, UPDATE, SELECT statements.
template<class ValueType >
void | add_field_value (const Glib::ustring& field_name, const ValueType&value)
| Specifies that the field represented by field_name will be set to the value.
void | add_field_value_as_value (const Glib::ustring& field_name, const Value&value)
| Valid only for: INSERT, UPDATE statements.
void | compound_add_sub_select (const SqlStatement& sqlst)
| Add a sub select to a COMPOUND statement.
SqlExpr | export_expression (Id id) const
| Exports a part managed by builder as a new Gda::SqlExpr, which can represent any expression in a statement.
Id | import_expression (const SqlExpr& expr)
| Imports the expression.

## Static Public Member Functions

static Glib::RefPtr< SqlBuilder > | create (SqlStatementType type)

## Protected Member Functions

| SqlBuilder (SqlStatementType type)

## Related Functions

(Note that these are not member functions.)
Glib::RefPtr < Gnome::Gda::SqlBuilder > | wrap (GdaSqlBuilder* object, bool take_copy=false)
| A Glib::wrap() method for this object.

## Detailed Description

SqlBuilder can be used to build a Statement from its structural description, much in the same way a SqlParser can be used to build a Statement from a SQL string.

You may, for instance, pass the SqlBuilder directly to Connection::statement_execute_select_builder() or Connection::statement_execute_non_select_builder(). Or you may instead call get_statement() and use the resulting SqlStatement.

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 SqlBuilder object, and each is identified using a single ID. That ID is dynamically allocated by the object.

This simple example builds the equivalent of this SQL query: "SELECT employees.name_first, employees.name_last FROM employees WHERE employees.employee_id = 123".

     Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
       Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
     builder->select_add_field("name_fist", "employees");
     builder->select_add_field("name_last", "employees");
     builder->select_add_target("employees");
     builder->set_where(
       builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
         builder->add_field_id("employee_id", "employees"),
         builder->add_expr(123)));

Note that, in this simple example, it's not necessary to specify the extra "employees" table name parameter to select_add_field() and add_field_id(), though it's often a good idea to avoid ambiguity in larger queries.

## Member Typedef Documentation

typedef guint Gnome::Gda::SqlBuilder::Id

## Constructor & Destructor Documentation

virtual Gnome::Gda::SqlBuilder::~SqlBuilder | ( |  | ) | `[virtual]`

Gnome::Gda::SqlBuilder::SqlBuilder | ( | SqlStatementType | type | ) | `[protected]`

## Member Function Documentation

Id Gnome::Gda::SqlBuilder::add_cond | ( | SqlOperatorType | op,
|  | Id | op1,
|  | Id | op2 = `0`,
|  | Id | op3 = `0`
| ) |  |

Builds a new expression which reprenents a condition (or operation).

Parameters:

  op | Type of condition
  op1 | The ID of the 1st argument (not 0)
  op2 | The ID of the 2st argument (maybe 0 if op only requires one argument)
  op3 | The ID of the 3st argument (maybe 0 if op only requires one or two arguments)

Returns:
  The ID of the new expression, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_cond | ( | SqlOperatorType | op,
|  | const std::vector< Id >& | op_ids
| ) |  |

Builds a new expression which reprenents a condition (or operation).

Parameters:

  op | Type of condition
  op_ids,: | list of IDs of the operands of the condition

Returns:
  The ID of the new expression, or 0 if there was an error.

template <class ValueType >

Id Gnome::Gda::SqlBuilder::add_expr | ( | const ValueType & | value | ) |

Defines an expression in Builder which may be reused to build other parts of a statement.

Parameters:

  value,: | value to set the expression to

See expr() except that no custom datahandler is given

Returns:
  The ID of the new expression, or 0 if there was an error.

template <class ValueType >

Id Gnome::Gda::SqlBuilder::add_expr | ( | const Glib::RefPtr< DataHandler >& | dh,
|  | const ValueType & | value
| ) |  |

Defines an expression in Builder which may be reused to build other parts of a statement.

Parameters:

  dh | The datahandler to use
  value,: | value to set the expression to

See expr() except that no custom datahandler is given

Returns:
  The ID of the new expression, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_expr_as_value | ( | const Glib::RefPtr< DataHandler >& | dh,
|  | const Value& | value
| ) |  |

Defines an expression in Builder which may be reused to build other parts of a statement.

Parameters:

  dh | The datahandler to use
  value,: | value to set the expression to

See expr() except that no custom datahandler is given

Returns:
  The ID of the new expression, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_expr_as_value | ( | const Value& | value | ) |

Defines an expression in Builder which may be reused to build other parts of a statement.

Parameters:

  value,: | value to set the expression to

See expr() except that no custom datahandler is given

Returns:
  The ID of the new expression, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_field_id | ( | const Glib::ustring & | field_name,
|  | const Glib::ustring & | table_name = `Glib::ustring()`
| ) |  |

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 add_cond() or add_field_value_id().

Calling this with a `0` table_name is equivalent to calling add_id().

For SELECT queries, see select_add_field().

Since libgdamm 4.2:

Parameters:

  field_name | A field name.
  table_name | A table name, or `0`.

Returns:
  The ID of the new expression, or %0 if there was an error.

template <class ValueType >

void Gnome::Gda::SqlBuilder::add_field_value | ( | const Glib::ustring & | field_name,
|  | const ValueType & | value
| ) |  |

Specifies that the field represented by field_name will be set to the value.

See gadd_expr() for more information. This is valid only for INSERT and UPDATE statements.

field_name A field name.

Parameters:

  value | The value to set the field to.

void Gnome::Gda::SqlBuilder::add_field_value_as_value | ( | const Glib::ustring & | field_name,
|  | const Value& | value
| ) |  |

Valid only for: INSERT, UPDATE statements.

Specifies that the field represented by field_name will be set to the value identified by value

Since libgdamm 4.2:

Parameters:

  field_name | A field name.
  value | Value to set the field to, or `0` or a GDA_TYPE_`0` value to represent an SQL `0`.

void Gnome::Gda::SqlBuilder::add_field_value_id | ( | Id | field_id,
|  | Id | value_id = `0`
| ) |  |

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 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 add_sub_select()), then this method call defines the sub SELECT from which values to insert are taken.

See also add_field_value() and add_field_value_as_gvalue().

Since libgdamm 4.2:

Parameters:

  field_id | The ID of the field's name or definition.
  value_id | The ID of the value to set the field to, or %0.

Id Gnome::Gda::SqlBuilder::add_function | ( | const Glib::ustring & | function_name,
|  | Id | arg
| ) |  |

Builds a new expression which represents a function applied to an argument.

Parameters:

  function_name | The name of the function to embed (e.g. MAX, MIN, ...)
  arg,: | The ids of the argument to the function

Returns:
  The ID of the new target, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_function | ( | const Glib::ustring & | function_name,
|  | const std::vector< Id >& | args
| ) |  |

Builds a new expression which represents a function applied to some arguments.

Parameters:

  function_name | The name of the function to embed (e.g. MAX, MIN, ...)
  args,: | list of ids of the arguments to the function

Returns:
  The ID of the new target, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_id | ( | const Glib::ustring & | string | ) |

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 add_cond() or add_field_value_id().

The new expression will contain the string 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 add_field_id().

Since libgdamm 4.2:

Parameters:

  string | A string.

Returns:
  The ID of the new expression, or %0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_param | ( | const Glib::ustring & | param_name,
|  | GType | type,
|  | bool | nullok = `false`
| ) |  |

Defines a parameter which may be reused to build other parts of a statement.

Parameters:

  | param_name: name of the parameter : GType of the parameter True if the parameter can be set to NULL

Returns:
  The ID of the new expression, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::add_sub_select | ( | const SqlStatement& | sqlst | ) |

Adds an expression which is a subselect.

Parameters:

  sqlst | A SqlStatement, which must be a SELECT or compound SELECT.

Returns:
  The ID of the new expression, or 0 if there was an error.

void Gnome::Gda::SqlBuilder::compound_add_sub_select | ( | const SqlStatement& | sqlst | ) |

Add a sub select to a COMPOUND statement.

Since libgdamm 4.2:

Parameters:

  sqlst | A pointer to a Gda::SqlStatement, which has to be a SELECT or compound SELECT. This will be copied.

static Glib::RefPtr<SqlBuilder> Gnome::Gda::SqlBuilder::create | ( | SqlStatementType | type | ) | `[static]`

SqlExpr Gnome::Gda::SqlBuilder::export_expression | ( | Id | id | ) | const

Exports a part managed by builder as a new Gda::SqlExpr, which can represent any expression in a statement.

Since libgdamm 4.2:

Parameters:

  id | The ID of the expression to be exported, (must be a valid ID in builder, not %0).

Returns:
  A pointer to a new Gda::SqlExpr 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 `0`.

SqlStatement Gnome::Gda::SqlBuilder::get_sql_statement | ( |  | ) | const

Creates a new Gda::SqlStatement 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.

Since libgdamm 4.2:

Returns:
  A Gda::SqlStatement pointer.

Glib::RefPtr<Statement> Gnome::Gda::SqlBuilder::get_statement | ( |  | ) | const

Creates a new Gda::Statement statement from builder's contents.

Since libgdamm 4.2:

Returns:
  A new Gda::Statement object, or `0` if an error occurred.

const GdaSqlBuilder* Gnome::Gda::SqlBuilder::gobj | ( |  | ) | const`[inline]`

Provides access to the underlying C GObject.

Reimplemented from Glib::ObjectBase.

GdaSqlBuilder* Gnome::Gda::SqlBuilder::gobj | ( |  | ) | `[inline]`

Provides access to the underlying C GObject.

Reimplemented from Glib::ObjectBase.

GdaSqlBuilder* Gnome::Gda::SqlBuilder::gobj_copy | ( |  | ) |

Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.

Id Gnome::Gda::SqlBuilder::import_expression | ( | const SqlExpr& | expr | ) |

Imports the expression.

Parameters:

  expr | A SqlExpr obtained using export_expression().

Returns:
  The ID of the new expression, or 0 if there was an error.

void Gnome::Gda::SqlBuilder::join_add_field | ( | Id | join_id,
|  | const Glib::ustring & | 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.

Since libgdamm 4.2:

Parameters:

  join_id | The ID of the join to modify (not %0).
  field_name | The name of the field to use in the join condition (not `0`).

Returns:
  The ID of the new join, or %0 if there was an error.

Id Gnome::Gda::SqlBuilder::select_add_field | ( | const Glib::ustring & | field_name,
|  | const Glib::ustring & | table_name,
|  | const Glib::ustring & | alias = `Glib::ustring()`
| ) |  |

Valid only for: SELECT statements.

Add a selected selected item to the SELECT statement.

For non-SELECT statements, see add_field_id().

Since libgdamm 4.2:

Parameters:

  field_name | A field name.
  table_name | A table name, or `0`.
  alias | An alias (eg. for the "AS" clause), or `0`.

Returns:
  The ID of the added field, or %0 if there was an error.

Id Gnome::Gda::SqlBuilder::select_add_target | ( | const Glib::ustring & | table_name,
|  | const Glib::ustring & | alias = `Glib::ustring()`
| ) |  |

Adds a new target to a SELECT statement.

Parameters:

  table_id | The ID of the expression holding a table reference.
  alias | The alias to give to the target.

Returns:
  The ID of the new target, or 0 if there was an error.

Id Gnome::Gda::SqlBuilder::select_add_target_id | ( | Id | table_id,
|  | const Glib::ustring & | alias = `Glib::ustring()`
| ) |  |

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.

Since libgdamm 4.2:

Parameters:

  table_id | The ID of the expression holding a table reference (not %0).
  alias | The alias to give to the target, or `0`.

Returns:
  The ID of the new (or existing) target, or %0 if there was an error.

void Gnome::Gda::SqlBuilder::select_group_by | ( | Id | expr_id | ) |

Valid only for: SELECT statements.

Adds the expr_id expression to the GROUP BY clause's expressions list

Since libgdamm 4.2:

Parameters:

  expr_id | The ID of the expression to set use in the GROUP BY clause, or 0 to unset any previous GROUP BY clause.

Id Gnome::Gda::SqlBuilder::select_join_targets | ( | Id | left_target_id,
|  | Id | right_target_id,
|  | SqlSelectJoinType | join_type,
|  | Id | join_expr = `0`
| ) |  |

Joins two targets in a SELECT statement.

Parameters:

  left_target_id | The ID of the left target to use (not %0)
  right_target_id | The ID of the right target to use (not %0)
  join_type | The type of join
  join_expr | Joining expression's ID, or %0

Returns:
  The ID of the new join, or 0 if there was an error.

void Gnome::Gda::SqlBuilder::select_order_by | ( | Id | expr_id,
|  | bool | asc = `true`,
|  | const Glib::ustring & | collation_name = `Glib::ustring()`
| ) |  |

Adds a new ORDER BY expression to a SELECT statement.

Since libgdamm 4.2:

Parameters:

  expr_id | The ID of the expression to use during sorting (not %0).
  asc | `true` for an ascending sorting.
  collation_name | Name of the collation to use when sorting, or `0`.

void Gnome::Gda::SqlBuilder::select_set_distinct | ( | bool | distinct = `true` | ) |

Adds or removes a DISTINCT clause for a SELECT statement.

Parameters:

  distinct | Whether a DISTINCT clause should be in the SELECT statement.

void Gnome::Gda::SqlBuilder::select_set_distinct | ( | bool | distinct,
|  | Id | 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>...").

Since libgdamm 4.2:

Parameters:

  distinct | Set to `true` to have the DISTINCT requirement.
  expr_id | The ID of the DISTINCT ON expression, or %0 if no expression is to be used. It is ignored if distinct is `false`.

void Gnome::Gda::SqlBuilder::select_set_having | ( | Id | cond_id | ) |

Valid only for: SELECT statements.

Sets the HAVING condition of the statement

Since libgdamm 4.2:

Parameters:

  cond_id | The ID of the expression to set as HAVING condition, or 0 to unset any previous HAVING condition.

void Gnome::Gda::SqlBuilder::select_set_limit | ( | Id | limit_count,
|  | Id | offset = `0`
| ) |  |

Defines the maximum number of rows in the DataModel resulting from the execution of the built statement.

Parameters:

  limit_count | The number of rows that should be returned.
  The | offset of the first row that should appear in the result. (note that this feature may not be supported by all the database providers.)

void Gnome::Gda::SqlBuilder::select_set_limit_id | ( | Id | limit_count_expr_id,
|  | Id | limit_offest_expr_id = `0`
| ) |  |

If limit_count_expr_id is not %0, defines the maximum number of rows in the Gda::DataModel 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.

Since libgdamm 4.2:

Parameters:

  limit_count_expr_id | The ID of the LIMIT expression, or %0.
  limit_offset_expr_id | The ID of the OFFSET expression, or %0.

void Gnome::Gda::SqlBuilder::set_table | ( | const Glib::ustring & | table_name | ) |

Valid only for: INSERT, UPDATE, DELETE statements.

Sets the name of the table on which the built statement operates.

Since libgdamm 4.2:

Parameters:

  table_name | A table name.

void Gnome::Gda::SqlBuilder::set_where | ( | Id | cond_id | ) |

Valid only for: UPDATE, DELETE, SELECT statements.

Sets the WHERE condition of the statement

Since libgdamm 4.2:

Parameters:

  cond_id | The ID of the expression to set as WHERE condition, or 0 to unset any previous WHERE condition.

## Friends And Related Function Documentation

Glib::RefPtr< Gnome::Gda::SqlBuilder > wrap | ( | GdaSqlBuilder * | object,
|  | bool | take_copy = `false`
| ) |  | `[related]`

A Glib::wrap() method for this object.

Parameters:

  object | The C instance.
  take_copy | False if the result should take ownership of the C instance. True if it should take a new copy or ref.

Returns:
  A C++ instance that wraps this C instance.

The documentation for this class was generated from the following file:

- libgdamm/sqlbuilder.h