1 | <?php |
||
13 | class Select extends ZendDbSqlSelect implements AdapterAwareInterface |
||
14 | { |
||
15 | /** |
||
16 | * |
||
17 | * @var Adapter |
||
18 | */ |
||
19 | protected $adapter; |
||
20 | |||
21 | /** |
||
22 | * Constructor |
||
23 | * |
||
24 | * @param Adapter $adapter |
||
25 | * @param null|string|array|\Zend\Db\Sql\TableIdentifier $table |
||
26 | */ |
||
27 | 103 | public function __construct(Adapter $adapter = null, $table = null) |
|
28 | { |
||
29 | 103 | if ($adapter) { |
|
30 | $this->setDbAdapter($adapter); |
||
31 | } |
||
32 | 103 | parent::__construct($table); |
|
33 | 103 | } |
|
34 | |||
35 | |||
36 | /** |
||
37 | * Create an where clause with 'OR' |
||
38 | * |
||
39 | * @param \Zend\Db\Sql\Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
40 | * @throws Zend\Db\Sql\Exception\InvalidArgumentException |
||
41 | * @return Select |
||
42 | */ |
||
43 | 1 | public function orWhere($predicate) |
|
47 | |||
48 | |||
49 | |||
50 | /** |
||
51 | * Add table prefixed columns, will automatically |
||
52 | * quote table parts identifiers found in the column name. |
||
53 | * It provides an alternative for defining columns from multiple/joined |
||
54 | * table in one go. |
||
55 | * |
||
56 | * <code> |
||
57 | * $select->from(array('p' =>'product') |
||
58 | * ->prefixedColumns(array('product_title' => 'p.title')); |
||
59 | * </code> |
||
60 | * Possible valid states: |
||
61 | * array(value, ...) |
||
62 | * value can be strings or Expression objects |
||
63 | * |
||
64 | * array(string => value, ...) |
||
65 | * key string will be use as alias, |
||
66 | * value can be string or Expression objects |
||
67 | * |
||
68 | * @throws Exception\InvalidArgumentException when usage not correct |
||
69 | * @param array $columns |
||
70 | * @return Select |
||
71 | */ |
||
72 | 11 | public function prefixedColumns(array $columns) |
|
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * Set database adapter |
||
133 | * |
||
134 | * @param Adapter $adapter |
||
135 | * @return Select |
||
136 | */ |
||
137 | 100 | public function setDbAdapter(Adapter $adapter) |
|
142 | |||
143 | /** |
||
144 | * Return an sql string accordingly to the internat database adapter |
||
145 | * |
||
146 | * @throws Exception\InvalidUsageException |
||
147 | * @return string |
||
148 | */ |
||
149 | 5 | public function getSql() |
|
158 | |||
159 | /** |
||
160 | * Execute the query and return a Zend\Db\Resultset\ResultSet object |
||
161 | * |
||
162 | * @throws Exception\InvalidUsageException |
||
163 | * @return \Zend\Db\ResultSet\ResultSet |
||
164 | */ |
||
165 | 81 | public function execute() |
|
166 | { |
||
167 | 81 | if ($this->adapter === null) { |
|
168 | 1 | $msg = __METHOD__ . ": Error, prior to use execute method you must provide a valid database adapter. See Select::setDbAdapter() method."; |
|
169 | 1 | throw new Exception\InvalidUsageException($msg); |
|
170 | } |
||
171 | 80 | $sql = new Sql($this->adapter); |
|
172 | 80 | $sql_string = $sql->getSqlStringForSqlObject($this); |
|
173 | |||
174 | //return $this->adapter->createStatement($sql_string)->execute(); |
||
175 | //return $this->adapter->query($sql_string, Adapter::QUERY_MODE_EXECUTE); |
||
176 | 80 | $result = $this->adapter->getDriver()->getConnection()->execute($sql_string); |
|
177 | 75 | $resultset = new ResultSet(); |
|
178 | 75 | $resultset->initialize($result); |
|
179 | 75 | return $resultset; |
|
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Return an sql string accordingly to the internat database adapter |
||
185 | * |
||
186 | * @throws Exception\InvalidUsageException |
||
187 | * @return string |
||
188 | */ |
||
189 | 2 | public function __toString() |
|
193 | } |
||
194 |