00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBSqlBuilderBase implements IDBSqlBuilder {
00009 protected $params;
00010
00011
00012
00013
00014
00015 protected $query;
00016 protected $fields;
00017
00018
00019
00020
00021 public function __construct($query, $params) {
00022 $this->query = $query;
00023 $this->params = $params;
00024 $this->fields = Arr::get_item($params, 'fields', array());
00025 }
00026
00027
00028
00029
00030
00031
00032 public function get_sql() {
00033 $template = $this->get_sql_template();
00034 $vars = $this->get_substitutes();
00035 return $this->substitute_template($template, $vars);
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
00045 protected function substitute_template($template, $vars) {
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 }
00054
00055 protected function get_sql_template() {
00056 throw new Exception(tr('Not implemented'));
00057 }
00058
00059 protected function get_substitutes() {
00060 return array();
00061 }
00062
00063 protected function get_feature_sql($arr_params, $name, $sql) {
00064 return Arr::get_item($arr_params, $name, false) ? ' ' . $sql : '';
00065 }
00066
00067 protected function get_where(IDBWhere $where) {
00068 $ret = $where->get_sql();
00069 if (!empty($ret)) {
00070 $ret = ' WHERE ' . $ret;
00071 }
00072 return $ret;
00073 }
00074
00075 protected function get_table_and_alias(IDBTable $table) {
00076 return $table->get_table_name_escaped() . ' AS ' . $table->get_table_alias_escaped();
00077 }
00078
00079 protected function get_table(IDBTable $table) {
00080 return $table->get_table_name_escaped();
00081 }
00082
00083 protected function get_order_by($arr_orders) {
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 }
00101
00102 protected function get_limit($arr_limit) {
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 }
00110
00111
00112
00113
00114 protected function get_connection() {
00115 return $this->query->get_table()->get_table_driver();
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125 protected function prefix_column($column, $table) {
00126 if ($this->is_non_prefixable_colum($column, $table)) {
00127 return $column;
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 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 protected function is_non_prefixable_colum($column, $table) {
00155 $ret = false;
00156 if (strpos($column, '.') !== false) {
00157 $ret = true;
00158 }
00159 else if ($this->is_function($column)) {
00160 $ret = true;
00161 }
00162 else {
00163 $test = substr($column, 0, 1);
00164 $ret = ($test === '"' || $test === "'");
00165 }
00166 return $ret;
00167 }
00168
00169
00170
00171
00172 protected function is_function($column) {
00173 return (strpos($column, '(') !== false);
00174 }
00175 }