00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBSqlBuilderWhereSphinx extends DBSqlBuilderWhere {
00009
00010
00011
00012
00013
00014 public function get_sql() {
00015 $operator = $this->where->get_operator();
00016 $column = $this->where->get_column();
00017
00018 $value = $this->where->get_value();
00019 $table = $this->where->get_table();
00020 $dbfield = $table->get_table_field($column);
00021
00022 $ret = array('filter' => array(), 'query' => '');
00023 if ($dbfield && $dbfield->has_policy(DBDriverSphinx::SPHINX_ATTRIBUTE)) {
00024
00025 $ret['filter'][] = $this->process_as_filter($column, $operator, $value, $table);
00026 }
00027 else {
00028 $ret['query'] = $this->process_as_query($column, $operator, $value, $table);
00029 }
00030 return $ret;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 protected function process_as_query($column, $operator, $value, $table) {
00042 if (empty($operator)) {
00043 return $column;
00044 }
00045
00046
00047 $field = $this->prefix_column($column, $table);
00048 switch ($operator) {
00049 case '=':
00050 $value = DB::escape($value, $table->get_table_driver());
00051 break;
00052 default:
00053 throw new Exception('Only = operator supported with sphinx queries at this time');
00054 break;
00055 }
00056 $ret = $field . ' ' . $value;
00057
00058
00059 while (substr($ret, -2, -1) === "\\") {
00060 $str_failures = '-!()|@~"/^&';
00061 if (strpos($str_failures, substr($ret, -1)) !== false) {
00062 $ret = substr($ret, 0, -2);
00063 }
00064 else {
00065 break;
00066 }
00067 }
00068 return $ret;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 protected function process_as_filter($columm, $operator, $value) {
00080 $exclude = false;
00081 $values = Arr::force($value, true);
00082 switch ($operator) {
00083 case '=':
00084 case DBWhere::OP_IN:
00085 $exclude = false;
00086 break;
00087 case '!=':
00088 case '<>':
00089 case DBWhere::OP_NOT_IN:
00090 $exclude = true;
00091 break;
00092 default:
00093 throw new Exception('Only =, != and IN, NOT IN operator supported with sphinx attributes at this time');
00094 break;
00095 }
00096 return array(
00097 'attribute' => $columm,
00098 'exclude' => $exclude,
00099 'values' => $values
00100 );
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110 protected function prefix_column($column, $table) {
00111 return '@' . DB::escape_database_entity($column, $table->get_table_driver(), IDBDriver::FIELD);
00112 }
00113 }