00001 <?php
00002 require_once dirname(__FILE__) . '/dbquery.ordered.cls.php';
00003
00004
00005
00006
00007
00008
00009
00010 class DBQuerySelect extends DBQueryOrdered {
00011 const DISTINCT = 1;
00012 const FOR_UPDATE = 2;
00013
00014
00015
00016
00017
00018
00019 protected $subqueries = array();
00020
00021
00022
00023
00024
00025 protected $group_bys = array();
00026
00027
00028
00029
00030
00031 protected $havings;
00032
00033 public function __construct($table, $policy = self::NORMAL) {
00034 parent::__construct($table, $policy);
00035 $this->havings = new DBWhereGroup($table);
00036 }
00037
00038
00039
00040
00041
00042
00043 public function get_sql() {
00044 $params = $this->prepare_sql_params();
00045 $builder = $this->create_sql_builder($params);
00046 return $builder->get_sql();
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 protected function create_sql_builder($params) {
00056 $builder = DBSqlBuilderFactory::create_builder(DBSqlBuilderFactory::SELECT, $this, $params);
00057 return $builder;
00058 }
00059
00060
00061
00062
00063
00064
00065 protected function prepare_sql_params() {
00066 $params = array();
00067 if ($this->policy & self::DISTINCT) {
00068 $params['distinct'] = true;
00069 }
00070 if ($this->policy & self::FOR_UPDATE) {
00071 $params['for_update'] = true;
00072 }
00073 $params['fields'] = $this->fields;
00074 $params['group_by'] = $this->get_group_bys();
00075 $params['having'] = $this->get_havings();
00076 $params['limit'] = $this->get_limit();
00077 $params['order_by'] = $this->get_orders();
00078 return $params;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088 public function add_join(IDBTable $table, $policy = DBQueryJoined::AUTODETECT_CONDITIONS, $join_type = DBQueryJoined::INNER) {
00089 $join = new DBQueryJoined($table, $this, $join_type, $policy);
00090 $this->add_subquery($join);
00091 return $join;
00092 }
00093
00094
00095
00096
00097
00098
00099 public function get_subqueries() {
00100 return $this->subqueries;
00101 }
00102
00103
00104
00105
00106
00107
00108 public function add_subquery(DBQuerySelect $query) {
00109 $this->subqueries[] = $query;
00110 }
00111
00112
00113
00114
00115
00116
00117 public function get_wheres() {
00118 $ret = new DBWhereGroup($this->get_table());
00119 $ret->add_where_object(parent::get_wheres());
00120 foreach($this->subqueries as $subquery) {
00121 $ret->add_where_object($subquery->get_wheres());
00122 }
00123 return $ret;
00124 }
00125
00126
00127
00128
00129
00130
00131 public function get_havings() {
00132 return $this->havings;
00133 }
00134
00135
00136
00137
00138
00139 public function add_group_by($column) {
00140 if (empty($column)) {
00141 $this->group_bys = array();
00142 } else {
00143 $table = $this->table;
00144 $this->group_bys[] = array(
00145 'field' => $column,
00146 'table' => $table,
00147 );
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156 public function get_group_bys() {
00157 return $this->group_bys;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 public function add_having($column, $operator = null, $value = null, $mode = IDBWhere::LOGIC_AND) {
00169 $this->havings->add_where($column, $operator, $value, $mode);
00170 }
00171
00172
00173
00174
00175 public function add_having_object(IDBWhere $having) {
00176 $this->havings->add_where_object($having);
00177 }
00178 }