00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBSqlBuilderSelectSphinx extends DBSqlBuilderSelect {
00009
00010
00011
00012
00013
00014 public function get_sql() {
00015 $table = $this->query->get_table();
00016 return serialize(
00017 array(
00018 'conditions' => $this->get_where($this->query->get_wheres()),
00019 'fields' => $this->get_fieldnames($this->fields, $table),
00020 'from' => $this->get_table($table),
00021 'limit' => $this->get_limit(Arr::get_item($this->params, 'limit', array(0,0))),
00022 'order' => $this->get_order_by(Arr::get_item($this->params, 'order_by', array())),
00023 'features' => $this->get_features($this->query)
00024 )
00025 );
00026
00027 }
00028
00029
00030
00031
00032
00033
00034 protected function get_where(IDBWhere $where) {
00035 return $where->get_sql();
00036 }
00037
00038
00039
00040
00041 protected function get_fieldnames($arr_fields, IDBTable $table) {
00042 $ret = '';
00043 $fieldnames = array();
00044 foreach($arr_fields as $key => $name) {
00045 $has_alias = !is_numeric($key);
00046 $fieldname = $has_alias ? $key : $name;
00047 $fieldalias = $name;
00048
00049 $statement = $this->prefix_column($fieldname, $table);
00050 if ($fieldalias != '*') {
00051 $statement .= ' AS ' . DB::escape_database_entity($fieldalias, $table->get_table_driver(), IDBDriver::ALIAS);
00052 }
00053
00054 $fieldnames[] = $statement;
00055 }
00056 if (count($fieldnames)) {
00057 $ret = implode(', ', $fieldnames);
00058 }
00059 else {
00060 $ret = $this->prefix_column('*', $table);
00061 }
00062 return $ret;
00063 }
00064
00065
00066
00067
00068 protected function get_limit($arr_limit) {
00069 $arr_limit = array_map('intval', $arr_limit);
00070 $ret = '';
00071 if ($arr_limit[1] > 0) {
00072 $ret = $arr_limit[0] . ';' . $arr_limit[1];
00073 }
00074 return $ret;
00075 }
00076
00077
00078
00079
00080 protected function get_order_by($arr_orders) {
00081 $ret = '';
00082 if (count($arr_orders) > 0) {
00083 $items = array();
00084 foreach($arr_orders as $order) {
00085 $column = Arr::get_item($order, 'field', '');
00086 if (empty($column)) {
00087 continue;
00088 }
00089 $table = Arr::get_item($order, 'table', null);
00090 $direction = Arr::get_item($order, 'direction', 'ASC');
00091 $items[] = $column. ' ' . $direction;
00092 }
00093 $ret .= implode(', ', $items);
00094 }
00095 return $ret;
00096 }
00097
00098
00099
00100
00101
00102
00103 protected function get_features($query) {
00104 $ret = array();
00105 if (isset($query->sphinx_features)) {
00106 $ret = $query->sphinx_features;
00107 }
00108 return $ret;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 protected function prefix_column($column, $table) {
00119 return DB::escape_database_entity($column, $table->get_table_driver(), IDBDriver::FIELD);
00120 }
00121 }