fireartist has asked for the wisdom of the Perl Monks concerning the following question:
If anybody would be willing to look over the code and make any suggestions, or point out problems, I'd much appreciate it.
Shop::Sql::Select - Create and return a SQL SELECT statement
use SQL::Builder::Select;
my $select = new SQL::Builder::Select;
or
my $select = SQL::Builder::Select->new;
An OO interface to build a (MySql) SQL SELECT statement, or a WHERE clause for use in a SELECT statement.
These methods are used to input data to build the SQL statement.
Accepts a column name or list of names.
Further calls are added to the end of the list.
$select->column( $column );
or
$select->column( @column );
Failure to set column() before calling return() will croak (kill the program).
The return value of column() is the total number of arguments held in column()
Accepts a table name or list of names.
Further calls are added to the end of the list.
$select->table( $tablename );
or
$select->table( @tablename );
Failure to set table() before calling return() will croak (kill the program).
The return value of column() is the total number of arguments held in table()
Accepts a single complete where statement (minus the leading 'WHERE ').
Further calls will be joined using the value of where_logic(). In this case, where_logic() should only hold 1 'AND' or 'OR' value, and all where() statements will be joined using this value.
$select->where( $statement );
The return value of where() is the total number of arguments held in where()
Accepts a where statement or list of statements.
Further calls are added to the end of the list.
$select->where_and( $where );
or
$select->where_and( @where );
The return value of where_and() is the total number of arguments held in where_and()
Accepts a where statement or list of statements.
Further calls are added to the end of the list.
$select->where_or( $where );
or
$select->where_or( @where );
The return value of where_or() is the total number of arguments held in where_or()
Accepts a where value or list of values.
Each value is made into a '$value = ?' statement and added to the end of the where_and() list.
$select->where_eq_and( $where );
or
$select->where_eq_and( @where );
The return value of where_eq_and() is the total number of arguments held in where_and()
Accepts a where value or list of values.
Each value is made into a '$value != ?' statement and added to the end of the where_and() list.
$select->where_ne_and( $where );
or
$select->where_ne_and( @where );
The return value of where_ne_and() is the total number of arguments held in where_and()
Accepts a where value or list of values.
Each value is made into a '$value = ?' statement and added to the end of the where_or() list.
$select->where_eq_or( $where );
or
$select->where_eq_or( @where );
The return value of where_eq_or() is the total number of arguments held in where_or()
Accepts a where value or list of values.
Each value is made into a '$value != ?' statement and added to the end of the where_or() list.
$select->where_ne_or( $where );
or
$select->where_ne_or( @where );
The return value of where_ne_or() is the total number of arguments held in where_or()
$select->where_logic( $logic );
where $logic equals
'AND' or 'OR'
If where_logic() is not set, it defaults to 'AND'
Of the methods where(), where_and(), where_or(), the first method in that list which has been set will be processed, and subsequent methods are ignored.
where[0] AND||OR where1 ...
or
where_and[0] AND||OR where_and1 ...
or
where_or[0] AND||OR where_or1 ...
$select->where_logic( $logic );
where $logic equals
'w AND a'
or
'a OR o'
or any other combination of 2 functions seperated by a 'AND' or 'OR', where
w = where()
a = where_and()
o = where_or()
The logic 'w AND a' would return the equivalent of
( where[0] AND where1 ... ) AND ( where_and[0] AND where_and1 ... ).
The logic I<'w OR a'> would return the equivalent of
( where[0] OR where1 ... ) OR ( where_and[0] AND where_and1 ... ).
If where logic() is passed an argument that does not represent the other functions set, unexpected results will likely occur.
The limitations of where_logic() can be overcome by using the function return_where() to create part-statements to be passed to the where() method of another SQL::Builder::Select object.
Further calls to where_logic() replace the existing value.
Accepts a column name or list of names.
Further calls are added to the end of the list.
$select->order_by( $column );
or
$select->order_by( @column );
The return value of order_by() is the total number of arguments held in order_by()
Accepts an integer.
Further calls replace the existing value.
$select->limit_offset( $offset );
Accepts an integer.
Further calls replace the existing value.
$select->limit_value( $limit );
This method is used to build and return the SQL statement.
This builds the SQL statement and returns it.
$sql = $select->return();
This builds the SQL statement for all where() functions, and returns it.
$sql = $select->return_where();
Boolean.
If true, then return() will print debugging data and then the complete statement to STDOUT then exit.
If ran under a web-server, will print text/plain header first.
0 or '' == false.
If no value is passed, returns 1 if set.
Now, here's the code
Example 1
Gives the outputuse SQL::Builder::Select; $select = new SQL::Builder::Select; $select->column('col1', 'col2'); $select->table('table'); $select->order_by('col1'); $select->limit_value('10'); $select->limit_offset('10'); print $select->return();
Example 2SELECT col1, col2 FROM table ORDER BY col1 LIMIT 10, 10
Gives the outputuse SQL::Builder::Select; $select = new SQL::Builder::Select; $select->column('*'); $select->table('table'); $select->where_and('col1 = 1', 'col2 = 1'); print $select->return();
Example 3SELECT * FROM table WHERE (col1 = 1) AND (col2 = 1)
Gives the outputuse SQL::Builder::Select; $select = new SQL::Builder::Select; $select->where_eq_or('col1', 'col2'); $where = $select->return_where(); $select = new SQL::Builder::Select; $select->column('*'); $select->table('table'); $select->where( $where ); $select->where_ne_or('col1', 'col2'); $select->where_logic('w AND o'); print $select->return();
SELECT * FROM table WHERE ((col1 = ?) OR (col2 = ?)) AND ((col1 != ?) +OR (col2 != ?))
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sql builder module - feedback request
by blokhead (Monsignor) on Nov 21, 2002 at 17:18 UTC | |
by fireartist (Chaplain) on Nov 22, 2002 at 09:57 UTC | |
|
Re: sql builder module - feedback request
by princepawn (Parson) on Nov 21, 2002 at 19:07 UTC | |
by fireartist (Chaplain) on Nov 22, 2002 at 09:03 UTC |