.

DBSqlBuilderBase Class Reference
[Model]

Base class for SQL builders. More...

Inheritance diagram for DBSqlBuilderBase:
IDBSqlBuilder DBSqlBuilderDelete DBSqlBuilderInsert DBSqlBuilderReplace DBSqlBuilderSelect DBSqlBuilderUpdate DBSqlBuilderDeleteMysql DBSqlBuilderInsertMysql DBSqlBuilderCount DBSqlBuilderSelectMysql DBSqlBuilderUpdateMysql DBSqlBuilderReplaceMysql DBSqlBuilderCountMysql

List of all members.

Public Member Functions

  __construct ($query, $params)
  get_sql ()
  Return SQL fragment.

Protected Member Functions

  get_connection ()
  get_feature_sql ($arr_params, $name, $sql)
  get_limit ($arr_limit)
  get_order_by ($arr_orders)
  get_sql_template ()
  get_substitutes ()
  get_table (IDBTable $table)
  get_table_and_alias (IDBTable $table)
  get_where (IDBWhere $where)
  is_function ($column)
  Returns true, if column passed is a function.
  is_non_prefixable_colum ($column, $table)
  Returns true, if coulumn is not prefixable, e.g.
  prefix_column ($column, $table)
  Prefix column with table alias.
  substitute_template ($template, $vars)
  SUbsitute vars in given template.

Protected Attributes

  $fields
  $params
  $query

Detailed Description

Base class for SQL builders.

Author:
Gerd Riesselmann

Definition at line 8 of file dbsqlbuilderbase.cls.php.


Constructor & Destructor Documentation

DBSqlBuilderBase::__construct ( query,
params  
)
Parameters:
DBQuery  $query

Definition at line 21 of file dbsqlbuilderbase.cls.php.

00021                                                      {
00022                 $this->query = $query;
00023                 $this->params = $params;
00024                 $this->fields = Arr::get_item($params, 'fields', array());
00025         }

Member Function Documentation

DBSqlBuilderBase::get_connection (  )  [protected]
Returns:
IDBDriver|string

Definition at line 114 of file dbsqlbuilderbase.cls.php.

00114                                             {
00115                 return $this->query->get_table()->get_table_driver();
00116         }
DBSqlBuilderBase::get_feature_sql ( arr_params,
name,
sql  
) [protected]

Definition at line 63 of file dbsqlbuilderbase.cls.php.

00063                                                                      {
00064                 return Arr::get_item($arr_params, $name, false) ? ' ' . $sql : '';
00065         }
DBSqlBuilderBase::get_limit ( arr_limit  )  [protected]

Reimplemented in DBSqlBuilderUpdate.

Definition at line 102 of file dbsqlbuilderbase.cls.php.

00102                                                  {
00103                 $arr_limit = array_map('intval', $arr_limit);
00104                 $ret = '';
00105                 if ($arr_limit[1] > 0) {
00106                         $ret = ' LIMIT ' . $arr_limit[0] . ',' . $arr_limit[1];
00107                 }
00108                 return $ret;
00109         }
DBSqlBuilderBase::get_order_by ( arr_orders  )  [protected]

Definition at line 83 of file dbsqlbuilderbase.cls.php.

00083                                                      {
00084                 $ret = '';
00085                 if (count($arr_orders) > 0) {
00086                         $ret .= ' ORDER BY ';
00087                         $items = array();
00088                         foreach($arr_orders as $order) {
00089                                 $column = Arr::get_item($order, 'field', '');
00090                                 if (empty($column)) {
00091                                         continue;
00092                                 }
00093                                 $table =  Arr::get_item($order, 'table', null);
00094                                 $direction = Arr::get_item($order, 'direction', 'ASC');
00095                                 $items[] = $column. ' ' . $direction;
00096                         }
00097                         $ret .= implode(', ', $items);
00098                 }
00099                 return $ret;
00100         }
DBSqlBuilderBase::get_sql (  ) 

Return SQL fragment.

Returns:
string

Implements IDBSqlBuilder.

Reimplemented in DBSqlBuilderReplace, and DBSqlBuilderDeleteMysql.

Definition at line 32 of file dbsqlbuilderbase.cls.php.

00032                                   {
00033                 $template = $this->get_sql_template();
00034                 $vars = $this->get_substitutes();
00035                 return $this->substitute_template($template, $vars);
00036         }
DBSqlBuilderBase::get_sql_template (  )  [protected]

Reimplemented in DBSqlBuilderCount, DBSqlBuilderDelete, DBSqlBuilderInsert, DBSqlBuilderSelect, DBSqlBuilderUpdate, and DBSqlBuilderReplaceMysql.

Definition at line 55 of file dbsqlbuilderbase.cls.php.

00055                                               {
00056                 throw new Exception(tr('Not implemented'));
00057         }
DBSqlBuilderBase::get_substitutes (  )  [protected]

Reimplemented in DBSqlBuilderCount, DBSqlBuilderDelete, DBSqlBuilderInsert, DBSqlBuilderSelect, DBSqlBuilderUpdate, and DBSqlBuilderReplaceMysql.

Definition at line 59 of file dbsqlbuilderbase.cls.php.

00059                                              {
00060                 return array();
00061         }
DBSqlBuilderBase::get_table ( IDBTable table  )  [protected]

Reimplemented in DBSqlBuilderInsert.

Definition at line 79 of file dbsqlbuilderbase.cls.php.

00079                                                       {
00080                 return $table->get_table_name_escaped();
00081         }
DBSqlBuilderBase::get_table_and_alias ( IDBTable table  )  [protected]

Definition at line 75 of file dbsqlbuilderbase.cls.php.

00075                                                                 {
00076                 return $table->get_table_name_escaped() . ' AS ' . $table->get_table_alias_escaped();
00077         }
DBSqlBuilderBase::get_where ( IDBWhere where  )  [protected]

Definition at line 67 of file dbsqlbuilderbase.cls.php.

00067                                                       {
00068                 $ret = $where->get_sql();
00069                 if (!empty($ret)) {
00070                         $ret = ' WHERE ' . $ret;
00071                 }
00072                 return $ret;
00073         }
DBSqlBuilderBase::is_function ( column  )  [protected]

Returns true, if column passed is a function.

Definition at line 172 of file dbsqlbuilderbase.cls.php.

00172                                                 {
00173                 return (strpos($column, '(') !== false);
00174         }
DBSqlBuilderBase::is_non_prefixable_colum ( column,
table  
) [protected]

Returns true, if coulumn is not prefixable, e.g.

becuase it is a function

Parameters:
string  $column
IDBTable  | string $table
Returns:
bool

Definition at line 154 of file dbsqlbuilderbase.cls.php.

00154                                                                     {
00155                 $ret = false;
00156                 if (strpos($column, '.') !== false) {
00157                         $ret = true; // Already prefixed
00158                 }
00159                 else if ($this->is_function($column)) {
00160                         $ret = true; // a fucntion
00161                 }
00162                 else {
00163                         $test = substr($column, 0, 1);
00164                         $ret = ($test === '"' || $test === "'");
00165                 }
00166                 return $ret;
00167         }
DBSqlBuilderBase::prefix_column ( column,
table  
) [protected]

Prefix column with table alias.

Parameters:
string  $column
IDBTable  | string $table
Returns:
string

Definition at line 125 of file dbsqlbuilderbase.cls.php.

00125                                                           {
00126                 if ($this->is_non_prefixable_colum($column, $table)) {
00127                         return $column; // Already has prefix
00128                 }
00129 
00130                 $is_itable = ($table instanceof IDBTable);
00131                 $connection = ($is_itable) ? $table->get_table_driver() : $this->get_connection();      
00132 
00133                 if ($column !== '*') {
00134                         $column = DB::escape_database_entity($column, $connection, IDBDriver::FIELD);
00135                 }
00136                 if ($is_itable) {
00137                         return $table->get_table_alias_escaped() . '.' . $column;
00138                 }
00139 
00140                 if (!empty($table)) {
00141                         return DB::escape_database_entity($table, $this->get_connection(), IDBDriver::TABLE) . '.' . $column;
00142                 }
00143 
00144                 return '';
00145         }
DBSqlBuilderBase::substitute_template ( template,
vars  
) [protected]

SUbsitute vars in given template.

Parameters:
string  $template
array  $vars
Returns:
string

Definition at line 45 of file dbsqlbuilderbase.cls.php.

00045                                                                  {
00046                 foreach($vars as $key => $value) {
00047                         $template = str_replace($key, $value, $template);
00048                         if (empty($value) && substr($key, 0, 2) == '%!') {
00049                                 throw new Exception(tr('Required SQL substitute %param not set', 'core', array('%param' => $key)));
00050                         }
00051                 }
00052                 return $template;               
00053         }

Member Data Documentation

DBSqlBuilderBase::$fields [protected]

Definition at line 16 of file dbsqlbuilderbase.cls.php.

DBSqlBuilderBase::$params [protected]

Definition at line 9 of file dbsqlbuilderbase.cls.php.

DBSqlBuilderBase::$query [protected]

Definition at line 15 of file dbsqlbuilderbase.cls.php.


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