Six Apart Ltd. > Data-ObjectDriver-0.09 > Data::ObjectDriver::SQL

Download:
Data-ObjectDriver-0.09.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

New  9
Open  4
View/Report Bugs
Source  

NAME ^

Data::ObjectDriver::SQL - an SQL statement

SYNOPSIS ^

    my $sql = Data::ObjectDriver::SQL->new();
    $sql->select([ 'id', 'name', 'bucket_id', 'note_id' ]);
    $sql->from([ 'foo' ]);
    $sql->add_where('name',      'fred');
    $sql->add_where('bucket_id', { op => '!=', value => 47 });
    $sql->add_where('note_id',   \'IS NULL');
    $sql->limit(1);

    my $sth = $dbh->prepare($sql->as_sql);
    $sth->execute(@{ $sql->{bind} });
    my @values = $sth->selectrow_array();

    my $obj = SomeObject->new();
    $obj->set_columns(...);

DESCRIPTION ^

Data::ObjectDriver::SQL represents an SQL statement. SQL statements are used internally to Data::ObjectDriver::Driver::DBI object drivers to convert database operations (search(), update(), etc) into database operations, but sometimes you just gotta use SQL.

ATTRIBUTES ^

Data::ObjectDriver::SQL sports several data attributes that represent the parts of the modeled SQL statement. These attributes all have accessor and mutator methods. Note that some attributes have more convenient methods of modification (for example, add_where() for the where attribute).

select (arrayref)

The database columns to select in a SELECT query.

distinct (boolean)

Whether the SELECT query should return DISTINCT rows only.

select_map (hashref)

The map of database column names to object fields in a SELECT query. Use this mapping to convert members of the select list to column names.

select_map_reverse (hashref)

The map of object fields to database column names in a SELECT query. Use this map to reverse the select_map mapping where needed.

from (arrayref)

The list of tables from which to query results in a SELECT query.

Note if you perform a SELECT query with multiple tables, the rows will be selected as Cartesian products that you'll need to reduce with WHERE clauses. Your query might be better served with real joins specified through the joins attribute of your statement.

joins (arrayref of hashrefs containing scalars and hashrefs)

The list of JOIN clauses to use in the table list of the statement. Each clause is a hashref containing these members:

where (arrayref)

The list of WHERE clauses that apply to the SQL statement. Individual members of the list are strings of SQL. All members of this attribute must be true for a record to be included as a result; that is, the list members are ANDed together to form the full WHERE clause.

where_values (hashref of variant structures)

The set of data structures used to generate the WHERE clause SQL found in the where attributes, keyed on the associated column names.

bind (arrayref)

The list of values to bind to the query when performed. That is, the list of values to be replaced for the ?es in the SQL.

limit (scalar)

The maximum number of results on which to perform the query.

offset (scalar)

The number of records to skip before performing the query. Combined with a limit and application logic to increase the offset in subsequent queries, you can paginate a set of records with a moving window containing limit records.

group (hashref, or an arrayref of hashrefs)

The fields on which to group the results. Grouping fields are hashrefs containing these members:

Note you can set a single grouping field, or use an arrayref containing multiple grouping fields.

having (arrayref)

The list of clauses to specify in the HAVING portion of a GROUP ... HAVING clause. Individual clauses are simple strings containing the conditional expression, as in where.

order (hashref, or an arrayref of hashrefs)

Returns or sets the fields by which to order the results. Ordering fields are hashrefs containing these members:

Note you can set a single ordering field, or use an arrayref containing multiple ordering fields.

$sql->comment([ $comment ])

Returns or sets a simple comment to the SQL statement

USAGE ^

Data::ObjectDriver::SQL->new()

Creates a new, empty SQL statement.

$sql->add_select($column [, $term ])

Adds the database column $column to the list of fields to return in a SELECT query. The requested object member will be indicated to be $term in the statement's select_map and select_map_reverse attributes.

$term is optional, and defaults to the same value as $column.

$sql->add_join($table, \@joins)

Adds the join statement indicated by $table and \@joins to the list of JOIN table references for the statement. The structure for the set of joins are as described for the joins attribute member above.

$sql->add_index_hint($table, $index)

Specifies a particular index to use for a particular table.

$sql->add_where($column, $value)

Adds a condition on the value of the database column $column to the statement's WHERE clause. A record will be tested against the below conditions according to what type of data structure $value is:

All individual conditions specified with add_where() must be true for a record to be a result of the query.

Beware that you can create a circular reference that will recursively generate an infinite SQL statement (for example, by specifying a arrayref $value that itself contains $value). As add_where() evaluates your expressions before storing the conditions in the where attribute as a generated SQL string, this will occur when calling add_where(), not as_sql(). So don't do that.

$sql->add_complex_where(\@list)

This method accepts an array reference of clauses that are glued together with logical operators. With it, you can express where clauses that mix logical operators together to produce more complex queries. For instance:

    [ { foo => 1, bar => 2 }, -or => { baz => 3 } ]

The values given for the columns support all the variants documented for the add_where() method above. Logical operators used inbetween the hashref elements can be one of: '-or', '-and', '-or_not', '-and_not'.

$sql->has_where($column, [$value])

Returns whether a where clause for the column $column was added to the statement with the add_where() method.

The $value argument is currently ignored.

$sql->add_having($column, $value)

Adds an expression to the HAVING portion of the statement's GROUP ... HAVING clause. The expression compares $column using $value, which can be any of the structures described above for the add_where() method.

$sql->add_index_hint($table, \@hints)

Addes the index hint into a SELECT query. The structure for the set of \@hints are arrayref of hashrefs containing these members:

$sql->as_sql()

Returns the SQL fully representing the SQL statement $sql.

$sql->as_sql_having()

Returns the SQL representing the HAVING portion of $sql's GROUP ... HAVING clause.

$sql->as_sql_where()

Returns the SQL representing $sql's WHERE clause.

$sql->as_limit()

Returns the SQL for the LIMIT ... OFFSET clause of the statement.

$sql->as_aggregate($set)

Returns the SQL representing the aggregation clause of type $set for the SQL statement $sql. Reasonable values of $set are ORDER and GROUP.

DIAGNOSTICS ^

BUGS AND LIMITATIONS ^

Data::ObjectDriver::SQL does not provide the functionality for turning SQL statements into instances of object classes.

SEE ALSO ^

LICENSE ^

Data::ObjectDriver is free software; you may redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR & COPYRIGHT ^

Except where otherwise noted, Data::ObjectDriver is Copyright 2005-2006 Six Apart, cpan@sixapart.com. All rights reserved.

syntax highlighting: