PageRenderTime 83ms CodeModel.GetById 26ms RepoModel.GetById 11ms app.codeStats 0ms

/classes/Base/DB/ORM/Select/Proxy.php

http://github.com/spadefoot/kohana-orm-leap
PHP | 387 lines | 119 code | 30 blank | 238 comment | 6 complexity | 63eb9ac71cebf18b24b4d37b8ebcfff3 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Copyright © 2011–2013 Spadefoot Team.
  4. *
  5. * Unless otherwise noted, LEAP is licensed under the Apache License,
  6. * Version 2.0 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License
  8. * at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * This class builds an SQL select statement.
  20. *
  21. * @package Leap
  22. * @category ORM
  23. * @version 2013-02-03
  24. *
  25. * @abstract
  26. */
  27. abstract class Base_DB_ORM_Select_Proxy extends Core_Object implements DB_SQL_Statement {
  28. /**
  29. * This variable stores an instance of the SQL builder class.
  30. *
  31. * @access protected
  32. * @var DB_SQL_Select_Builder
  33. */
  34. protected $builder;
  35. /**
  36. * This variable stores a reference to the data source.
  37. *
  38. * @access protected
  39. * @var DB_DataSource
  40. */
  41. protected $data_source;
  42. /**
  43. * This variable stores an instance of the ORM builder extension class.
  44. *
  45. * @access protected
  46. * @var DB_ORM_Builder
  47. */
  48. protected $extension;
  49. /**
  50. * This variable stores the model's name.
  51. *
  52. * @access protected
  53. * @var string
  54. */
  55. protected $model;
  56. /**
  57. * This variable stores the name of the model's table.
  58. *
  59. * @access protected
  60. * @var string
  61. */
  62. protected $table = NULL;
  63. /**
  64. * This function attempts to call an otherwise inaccessible function on the model's
  65. * builder extension.
  66. *
  67. * @access public
  68. * @override
  69. * @param string $function the name of the called function
  70. * @param array $arguments an array with the parameters passed
  71. * @return mixed the result of the called function
  72. * @throws Throwable_UnimplementedMethod_Exception indicates that the called function is
  73. * inaccessible
  74. */
  75. public function __call($function, $arguments) {
  76. if ($this->extension !== NULL) {
  77. if (method_exists($this->extension, $function)) {
  78. $result = call_user_func_array(array($this->extension, $function), $arguments);
  79. if ($result instanceof DB_ORM_Builder) {
  80. return $this;
  81. }
  82. return $result;
  83. }
  84. }
  85. throw new Throwable_UnimplementedMethod_Exception('Message: Call to undefined member function. Reason: Function :function has not been defined in class :class.', array(':class' => get_class($this->extension), ':function' => $function, ':arguments' => $arguments));
  86. }
  87. /**
  88. * This constructor instantiates this class using the specified model's name.
  89. *
  90. * @access public
  91. * @param string $model the model's name
  92. * @param array $columns the columns to be selected
  93. */
  94. public function __construct($model, Array $columns = array()) {
  95. $name = $model;
  96. $model = DB_ORM_Model::model_name($name);
  97. $this->data_source = DB_DataSource::instance($model::data_source(DB_DataSource::SLAVE_INSTANCE));
  98. $builder = 'DB_' . $this->data_source->dialect . '_Select_Builder';
  99. $this->table = $model::table();
  100. $this->builder = new $builder($this->data_source, $columns);
  101. if (empty($columns)) {
  102. $this->builder->all("{$this->table}.*");
  103. }
  104. $this->builder->from($this->table);
  105. $extension = DB_ORM_Model::builder_name($name);
  106. if (class_exists($extension)) {
  107. $this->extension = new $extension($this->builder);
  108. }
  109. $this->model = $model;
  110. }
  111. /**
  112. * This function returns the raw SQL statement.
  113. *
  114. * @access public
  115. * @override
  116. * @return string the raw SQL statement
  117. */
  118. public function __toString() {
  119. return $this->builder->statement(TRUE);
  120. }
  121. /**
  122. * This function sets the wildcard to be used.
  123. *
  124. * @access public
  125. * @param string $wildcard the wildcard to be used
  126. * @return DB_ORM_Select_Proxy a reference to the current instance
  127. */
  128. public function all($wildcard = '*') {
  129. $this->builder->all("{$this->table}.*");
  130. return $this;
  131. }
  132. /**
  133. * This function explicits sets the specified column to be selected.
  134. *
  135. * @access public
  136. * @param string $column the column to be selected
  137. * @param string $alias the alias to be used for the specified column
  138. * @return DB_ORM_Select_Proxy a reference to the current instance
  139. */
  140. public function column($column, $alias = NULL) {
  141. $this->builder->column($column, $alias);
  142. return $this;
  143. }
  144. /**
  145. * This function combines another SQL statement using the specified operator.
  146. *
  147. * @access public
  148. * @param string $operator the operator to be used to append
  149. * the specified SQL statement
  150. * @param string $statement the SQL statement to be appended
  151. * @return DB_ORM_Select_Proxy a reference to the current instance
  152. */
  153. public function combine($operator, $statement) {
  154. $this->builder->combine($operator, $statement);
  155. return $this;
  156. }
  157. /**
  158. * This function sets whether to constrain the SQL statement to only distinct records.
  159. *
  160. * @access public
  161. * @param boolean $distinct whether to constrain the SQL statement to only
  162. * distinct records
  163. * @return DB_ORM_Select_Proxy a reference to the current instance
  164. */
  165. public function distinct($distinct = TRUE) {
  166. $this->builder->distinct($distinct);
  167. return $this;
  168. }
  169. /**
  170. * This function adds a "group by" clause.
  171. *
  172. * @access public
  173. * @param string $column the column to be grouped
  174. * @return DB_ORM_Select_Proxy a reference to the current instance
  175. */
  176. public function group_by($column) {
  177. $this->builder->group_by($column);
  178. return $this;
  179. }
  180. /**
  181. * This function adds a "having" constraint.
  182. *
  183. * @access public
  184. * @param string $column the column to be constrained
  185. * @param string $operator the operator to be used
  186. * @param string $value the value the column is constrained with
  187. * @param string $connector the connector to be used
  188. * @return DB_ORM_Select_Proxy a reference to the current instance
  189. */
  190. public function having($column, $operator, $value, $connector = 'AND') {
  191. $this->builder->having($column, $operator, $value, $connector);
  192. return $this;
  193. }
  194. /**
  195. * This function either opens or closes a "having" group.
  196. *
  197. * @access public
  198. * @param string $parenthesis the parenthesis to be used
  199. * @param string $connector the connector to be used
  200. * @return DB_ORM_Select_Proxy a reference to the current instance
  201. */
  202. public function having_block($parenthesis, $connector = 'AND') {
  203. $this->builder->having_block($parenthesis, $connector);
  204. return $this;
  205. }
  206. /**
  207. * This function joins a table.
  208. *
  209. * @access public
  210. * @param string $type the type of join
  211. * @param string $table the table to be joined
  212. * @param string $alias the alias to be used for the specified table
  213. * @return DB_ORM_Select_Proxy a reference to the current instance
  214. */
  215. public function join($type, $table, $alias = NULL) {
  216. $this->builder->join($type, $table, $alias);
  217. return $this;
  218. }
  219. /**
  220. * This function sets a "limit" constraint on the statement.
  221. *
  222. * @access public
  223. * @param integer $limit the "limit" constraint
  224. * @return DB_ORM_Select_Proxy a reference to the current instance
  225. */
  226. public function limit($limit) {
  227. $this->builder->limit($limit);
  228. return $this;
  229. }
  230. /**
  231. * This function sets an "offset" constraint on the statement.
  232. *
  233. * @access public
  234. * @param integer $offset the "offset" constraint
  235. * @return DB_ORM_Select_Proxy a reference to the current instance
  236. */
  237. public function offset($offset) {
  238. $this->builder->offset($offset);
  239. return $this;
  240. }
  241. /**
  242. * This function sets an "on" constraint for the last join specified.
  243. *
  244. * @access public
  245. * @param string $column0 the column to be constrained on
  246. * @param string $operator the operator to be used
  247. * @param string $column1 the constraint column
  248. * @return DB_ORM_Select_Proxy a reference to the current instance
  249. * @throws Throwable_SQL_Exception indicates an invalid SQL build instruction
  250. */
  251. public function on($column0, $operator, $column1) {
  252. $this->builder->on($column0, $operator, $column1);
  253. return $this;
  254. }
  255. /**
  256. * This function sets how a column will be sorted.
  257. *
  258. * @access public
  259. * @param string $column the column to be sorted
  260. * @param string $ordering the ordering token that signals whether the
  261. * column will sorted either in ascending or
  262. * descending order
  263. * @param string $nulls the weight to be given to null values
  264. * @return DB_ORM_Select_Proxy a reference to the current instance
  265. */
  266. public function order_by($column, $ordering = 'ASC', $nulls = 'DEFAULT') {
  267. $this->builder->order_by($column, $ordering, $nulls);
  268. return $this;
  269. }
  270. /**
  271. * This function sets both the "offset" constraint and the "limit" constraint on
  272. * the statement.
  273. *
  274. * @access public
  275. * @param integer $offset the "offset" constraint
  276. * @param integer $limit the "limit" constraint
  277. * @return DB_SQL_Select_Builder a reference to the current instance
  278. */
  279. public function page($offset, $limit) {
  280. $this->builder->page($offset, $limit);
  281. return $this;
  282. }
  283. /**
  284. * This function performs a query using the built SQL statement.
  285. *
  286. * @access public
  287. * @param integer $limit the "limit" constraint
  288. * @return DB_ResultSet the result set
  289. */
  290. public function query($limit = NULL) {
  291. if ($limit !== NULL) {
  292. $this->limit($limit);
  293. }
  294. $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
  295. $records = $connection->query($this->statement(), $this->model);
  296. return $records;
  297. }
  298. /**
  299. * This function resets the current builder.
  300. *
  301. * @access public
  302. * @return DB_ORM_Select_Proxy a reference to the current instance
  303. */
  304. public function reset() {
  305. $this->builder->reset();
  306. return $this;
  307. }
  308. /**
  309. * This function returns the SQL statement.
  310. *
  311. * @access public
  312. * @override
  313. * @param boolean $terminated whether to add a semi-colon to the end
  314. * of the statement
  315. * @return string the SQL statement
  316. */
  317. public function statement($terminated = TRUE) {
  318. return $this->builder->statement($terminated);
  319. }
  320. /**
  321. * This function sets a "using" constraint for the last join specified.
  322. *
  323. * @access public
  324. * @param string $column the column to be constrained
  325. * @return DB_ORM_Select_Proxy a reference to the current instance
  326. */
  327. public function using($column) {
  328. $this->builder->using($column);
  329. return $this;
  330. }
  331. /**
  332. * This function adds a "where" constraint.
  333. *
  334. * @access public
  335. * @param string $column the column to be constrained
  336. * @param string $operator the operator to be used
  337. * @param string $value the value the column is constrained with
  338. * @param string $connector the connector to be used
  339. * @return DB_ORM_Select_Proxy a reference to the current instance
  340. */
  341. public function where($column, $operator, $value, $connector = 'AND') {
  342. $this->builder->where($column, $operator, $value, $connector);
  343. return $this;
  344. }
  345. /**
  346. * This function either opens or closes a "where" group.
  347. *
  348. * @access public
  349. * @param string $parenthesis the parenthesis to be used
  350. * @param string $connector the connector to be used
  351. * @return DB_ORM_Select_Proxy a reference to the current instance
  352. */
  353. public function where_block($parenthesis, $connector = 'AND') {
  354. $this->builder->where_block($parenthesis, $connector);
  355. return $this;
  356. }
  357. }