00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBSqlBuilderWhere implements IDBSqlBuilder {
00009
00010
00011
00012 protected $where = null;
00013
00014
00015
00016
00017 public function __construct($where, $params = false) {
00018 $this->where = $where;
00019 }
00020
00021
00022
00023
00024
00025
00026 public function get_sql() {
00027 $operator = $this->where->get_operator();
00028 $column = $this->where->get_column();
00029 if (empty($operator)) {
00030 return $column;
00031 }
00032
00033 $table = $this->where->get_table();
00034 $field = $this->prefix_column($column, $table);
00035 $value = $this->where->get_value();
00036 $ret = '';
00037 switch ($operator) {
00038 case IDBWhere::OP_IN:
00039 case IDBWhere::OP_NOT_IN:
00040 if ($value instanceof DBQuerySelect) {
00041 $value = $value->get_sql();
00042 }
00043 else {
00044 $arr_formatted_values = array();
00045 foreach(Arr::force($value) as $orgvalue) {
00046 $arr_formatted_values[] = DB::format_where($orgvalue, $table, $column);
00047 }
00048 $value = implode(', ', $arr_formatted_values);
00049 }
00050 $value = '(' . $value . ')';
00051 break;
00052 case IDBWhere::OP_IS_NULL:
00053 case IDBWhere::OP_NOT_NULL:
00054 $value = '';
00055 break;
00056 case IDBWhere::OP_IN_SET:
00057 $value = DB::format_where($value, $table, $column);
00058 $value = $value . ' = ' . $value;
00059 $operator = '&';
00060 break;
00061 case IDBWhere::OP_NOT_IN_SET:
00062 $value = DB::format_where($value, $table, $column);
00063 $value = $value . ' = 0';
00064 $operator = '&';
00065 break;
00066 default:
00067 $value = DB::format_where($value, $table, $column);
00068 break;
00069 }
00070 return '(' . $field . ' ' . $operator . ' ' . $value . ')';
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080 protected function prefix_column($column, $table) {
00081 $ret = $column;
00082 if (!String::contains($column, '.')) {
00083 if ($table instanceof IDBTable) {
00084 $ret = DB::escape_database_entity($column, $table->get_table_driver(), IDBDriver::FIELD);
00085 if ($table->get_table_field($column)) {
00086 $ret = $table->get_table_alias_escaped() . '.' . $ret;
00087 }
00088 }
00089 else {
00090 $ret = DB::escape_database_entity($column, DB::DEFAULT_CONNECTION, IDBDriver::FIELD);
00091 }
00092 }
00093 return $ret;
00094 }
00095 }