PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Db/test.php

https://github.com/lboaretto/magento-pt_br
PHP | 480 lines | 267 code | 49 blank | 164 comment | 73 complexity | d36d3115dff95fcca98de01f9a9c85c2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. //****************************************************************************
  3. // phpDBTree 1.4
  4. //****************************************************************************
  5. // Author: Maxim Poltarak <maxx at e dash taller dot net>
  6. // WWW: http://dev.e-taller.net/dbtree/
  7. // Category: Databases
  8. // Description: PHP class implementing a Nested Sets approach to storing
  9. // tree-like structures in database tables. This technique was
  10. // introduced by Joe Celko <http://www.celko.com/> and has some
  11. // advantages over a widely used method called Adjacency Matrix.
  12. //****************************************************************************
  13. // The lib is FREEWARE. That means you may use it anywhere you want, you may
  14. // do anything with it. The Author mentioned above is NOT responsible for any
  15. // consequences of using this library.
  16. // If you don't agree with this, you are NOT ALLOWED to use the lib!
  17. //****************************************************************************
  18. // You're welcome to send me all improvings, feature requests, bug reports...
  19. //****************************************************************************
  20. // SAMPLE DB TABLE STRUCTURE:
  21. //
  22. // CREATE TABLE categories (
  23. // cat_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  24. // cat_left INT UNSIGNED NOT NULL,
  25. // cat_right INT UNSIGNED NOT NULL,
  26. // cat_level INT UNSIGNED NOT NULL,
  27. // PRIMARY KEY(cat_id),
  28. // KEY(cat_left, cat_right, cat_level)
  29. // );
  30. //
  31. // This is believed to be the optimal Nested Sets use case. Use `one-to-one`
  32. // relations on `cat_id` field between this `structure` table and
  33. // another `data` table in your database.
  34. //
  35. // Don't forget to make a single call to clear()
  36. // to set up the Root node in an empty table.
  37. //
  38. //****************************************************************************
  39. // NOTE: Although you may use this library to retrieve data from the table,
  40. // it is recommended to write your own queries for doing that.
  41. // The main purpose of the library is to provide a simpler way to
  42. // create, update and delete records. Not to SELECT them.
  43. //****************************************************************************
  44. //
  45. // IMPORTANT! DO NOT create either UNIQUE or PRIMARY keys on the set of
  46. // fields (`cat_left`, `cat_right` and `cat_level`)!
  47. // Unique keys will destroy the Nested Sets structure!
  48. //
  49. //****************************************************************************
  50. // CHANGELOG:
  51. // 16-Apr-2003 -=- 1.1
  52. // - Added moveAll() method
  53. // - Added fourth parameter to the constructor
  54. // - Renamed getElementInfo() to getNodeInfo() /keeping BC/
  55. // - Added "Sample Table Structure" comment
  56. // - Now it trigger_error()'s in case of any serious error, because if not you
  57. // will lose the Nested Sets structure in your table
  58. // 19-Feb-2004 -=- 1.2
  59. // - Fixed a bug in moveAll() method?
  60. // Thanks to Maxim Matyukhin for the patch.
  61. // 13-Jul-2004 -=- 1.3
  62. // - Changed all die()'s for a more standard trigger_error()
  63. // Thanks to Dmitry Romakhin for pointing out an issue with
  64. // incorrect error message call.
  65. // 09-Nov-2004 -=- 1.4
  66. // - Added insertNear() method.
  67. // Thanks to Michael Krenz who sent its implementation.
  68. // - Removed IGNORE keyword from UPDATE clauses in insert() methods.
  69. // It was dumb to have it there all the time. Sorry. Anyway, you had
  70. // to follow the important note mencioned above. If you hadn't, you're
  71. // in problem.
  72. //
  73. //****************************************************************************
  74. // Note: For best viewing of the code Tab size 4 is recommended
  75. //****************************************************************************
  76. class CDBTree {
  77. var $db; // CDatabase class to plug to
  78. var $table; // Table with Nested Sets implemented
  79. var $id; // Name of the ID-auto_increment-field in the table.
  80. // These 3 variables are names of fields which are needed to implement
  81. // Nested Sets. All 3 fields should exist in your table!
  82. // However, you may want to change their names here to avoid name collisions.
  83. var $left = 'cat_left';
  84. var $right = 'cat_right';
  85. var $level = 'cat_level';
  86. var $qryParams = '';
  87. var $qryFields = '';
  88. var $qryTables = '';
  89. var $qryWhere = '';
  90. var $qryGroupBy = '';
  91. var $qryHaving = '';
  92. var $qryOrderBy = '';
  93. var $qryLimit = '';
  94. var $sqlNeedReset = true;
  95. var $sql; // Last SQL query
  96. //************************************************************************
  97. // Constructor
  98. // $DB : CDatabase class instance to link to
  99. // $tableName : table in database where to implement nested sets
  100. // $itemId : name of the field which will uniquely identify every record
  101. // $fieldNames : optional configuration array to set field names. Example:
  102. // array(
  103. // 'left' => 'cat_left',
  104. // 'right' => 'cat_right',
  105. // 'level' => 'cat_level'
  106. // )
  107. function CDBTree(&$DB, $tableName, $itemId, $fieldNames=array()) {
  108. if(empty($tableName)) trigger_error("phpDbTree: Unknown table", E_USER_ERROR);
  109. if(empty($itemId)) trigger_error("phpDbTree: Unknown ID column", E_USER_ERROR);
  110. $this->db = $DB;
  111. $this->table = $tableName;
  112. $this->id = $itemId;
  113. if(is_array($fieldNames) && sizeof($fieldNames))
  114. foreach($fieldNames as $k => $v)
  115. $this->$k = $v;
  116. }
  117. //************************************************************************
  118. // Returns a Left and Right IDs and Level of an element or false on error
  119. // $ID : an ID of the element
  120. function getElementInfo($ID) { return $this->getNodeInfo($ID); }
  121. function getNodeInfo($ID) {
  122. $this->sql = 'SELECT '.$this->left.','.$this->right.','.$this->level.' FROM '.$this->table.' WHERE '.$this->id.'=\''.$ID.'\'';
  123. if(($query=$this->db->query($this->sql)) && ($this->db->num_rows($query) == 1) && ($Data = $this->db->fetch_array($query)))
  124. return array((int)$Data[$this->left], (int)$Data[$this->right], (int)$Data[$this->level]);
  125. else trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  126. }
  127. //************************************************************************
  128. // Clears table and creates 'root' node
  129. // $data : optional argument with data for the root node
  130. function clear($data=array()) {
  131. // clearing table
  132. if((!$this->db->query('TRUNCATE '.$this->table)) && (!$this->db->query('DELETE FROM '.$this->table))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  133. // preparing data to be inserted
  134. if(sizeof($data)) {
  135. $fld_names = implode(',', array_keys($data)).',';
  136. if(sizeof($data)) $fld_values = '\''.implode('\',\'', array_values($data)).'\',';
  137. }
  138. $fld_names .= $this->left.','.$this->right.','.$this->level;
  139. $fld_values .= '1,2,0';
  140. // inserting new record
  141. $this->sql = 'INSERT INTO '.$this->table.'('.$fld_names.') VALUES('.$fld_values.')';
  142. if(!($this->db->query($this->sql))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  143. return $this->db->insert_id();
  144. }
  145. //************************************************************************
  146. // Updates a record
  147. // $ID : element ID
  148. // $data : array with data to update: array(<field_name> => <fields_value>)
  149. function update($ID, $data) {
  150. $sql_set = '';
  151. foreach($data as $k=>$v) $sql_set .= ','.$k.'=\''.addslashes($v).'\'';
  152. return $this->db->query('UPDATE '.$this->table.' SET '.substr($sql_set,1).' WHERE '.$this->id.'=\''.$ID.'\'');
  153. }
  154. //************************************************************************
  155. // Inserts a record into the table with nested sets
  156. // $ID : an ID of the parent element
  157. // $data : array with data to be inserted: array(<field_name> => <field_value>)
  158. // Returns : true on success, or false on error
  159. function insert($ID, $data) {
  160. if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  161. // preparing data to be inserted
  162. if(sizeof($data)) {
  163. $fld_names = implode(',', array_keys($data)).',';
  164. $fld_values = '\''.implode('\',\'', array_values($data)).'\',';
  165. }
  166. $fld_names .= $this->left.','.$this->right.','.$this->level;
  167. $fld_values .= ($rightId).','.($rightId+1).','.($level+1);
  168. // creating a place for the record being inserted
  169. if($ID) {
  170. $this->sql = 'UPDATE '.$this->table.' SET '
  171. . $this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'+2,'.$this->left.'),'
  172. . $this->right.'=IF('.$this->right.'>='.$rightId.','.$this->right.'+2,'.$this->right.')'
  173. . 'WHERE '.$this->right.'>='.$rightId;
  174. if(!($this->db->query($this->sql))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  175. }
  176. // inserting new record
  177. $this->sql = 'INSERT INTO '.$this->table.'('.$fld_names.') VALUES('.$fld_values.')';
  178. if(!($this->db->query($this->sql))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  179. return $this->db->insert_id();
  180. }
  181. //************************************************************************
  182. // Inserts a record into the table with nested sets
  183. // $ID : ID of the element after which (i.e. at the same level) the new element
  184. // is to be inserted
  185. // $data : array with data to be inserted: array(<field_name> => <field_value>)
  186. // Returns : true on success, or false on error
  187. function insertNear($ID, $data) {
  188. if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID)))
  189. trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  190. // preparing data to be inserted
  191. if(sizeof($data)) {
  192. $fld_names = implode(',', array_keys($data)).',';
  193. $fld_values = '\''.implode('\',\'', array_values($data)).'\',';
  194. }
  195. $fld_names .= $this->left.','.$this->right.','.$this->level;
  196. $fld_values .= ($rightId+1).','.($rightId+2).','.($level);
  197. // creating a place for the record being inserted
  198. if($ID) {
  199. $this->sql = 'UPDATE '.$this->table.' SET '
  200. .$this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'+2,'.$this->left.'),'
  201. .$this->right.'=IF('.$this->right.'>'.$rightId.','.$this->right.'+2,'.$this->right.')'
  202. . 'WHERE '.$this->right.'>'.$rightId;
  203. if(!($this->db->query($this->sql))) trigger_error("phpDbTree error:".$this->db->error(), E_USER_ERROR);
  204. }
  205. // inserting new record
  206. $this->sql = 'INSERT INTO '.$this->table.'('.$fld_names.') VALUES('.$fld_values.')';
  207. if(!($this->db->query($this->sql))) trigger_error("phpDbTree error:".$this->db->error(), E_USER_ERROR);
  208. return $this->db->insert_id();
  209. }
  210. //************************************************************************
  211. // Assigns a node with all its children to another parent
  212. // $ID : node ID
  213. // $newParentID : ID of new parent node
  214. // Returns : false on error
  215. function moveAll($ID, $newParentId) {
  216. if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  217. if(!(list($leftIdP, $rightIdP, $levelP) = $this->getNodeInfo($newParentId))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  218. if($ID == $newParentId || $leftId == $leftIdP || ($leftIdP >= $leftId && $leftIdP <= $rightId)) return false;
  219. // whether it is being moved upwards along the path
  220. if ($leftIdP < $leftId && $rightIdP > $rightId && $levelP < $level - 1 ) {
  221. $this->sql = 'UPDATE '.$this->table.' SET '
  222. . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->level.sprintf('%+d', -($level-1)+$levelP).', '.$this->level.'), '
  223. . $this->right.'=IF('.$this->right.' BETWEEN '.($rightId+1).' AND '.($rightIdP-1).', '.$this->right.'-'.($rightId-$leftId+1).', '
  224. .'IF('.$this->left.' BETWEEN '.($leftId).' AND '.($rightId).', '.$this->right.'+'.((($rightIdP-$rightId-$level+$levelP)/2)*2 + $level - $levelP - 1).', '.$this->right.')), '
  225. . $this->left.'=IF('.$this->left.' BETWEEN '.($rightId+1).' AND '.($rightIdP-1).', '.$this->left.'-'.($rightId-$leftId+1).', '
  226. .'IF('.$this->left.' BETWEEN '.$leftId.' AND '.($rightId).', '.$this->left.'+'.((($rightIdP-$rightId-$level+$levelP)/2)*2 + $level - $levelP - 1).', '.$this->left. ')) '
  227. . 'WHERE '.$this->left.' BETWEEN '.($leftIdP+1).' AND '.($rightIdP-1)
  228. ;
  229. } elseif($leftIdP < $leftId) {
  230. $this->sql = 'UPDATE '.$this->table.' SET '
  231. . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->level.sprintf('%+d', -($level-1)+$levelP).', '.$this->level.'), '
  232. . $this->left.'=IF('.$this->left.' BETWEEN '.$rightIdP.' AND '.($leftId-1).', '.$this->left.'+'.($rightId-$leftId+1).', '
  233. . 'IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->left.'-'.($leftId-$rightIdP).', '.$this->left.') '
  234. . '), '
  235. . $this->right.'=IF('.$this->right.' BETWEEN '.$rightIdP.' AND '.$leftId.', '.$this->right.'+'.($rightId-$leftId+1).', '
  236. . 'IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->right.'-'.($leftId-$rightIdP).', '.$this->right.') '
  237. . ') '
  238. . 'WHERE '.$this->left.' BETWEEN '.$leftIdP.' AND '.$rightId
  239. // !!! added this line (Maxim Matyukhin)
  240. .' OR '.$this->right.' BETWEEN '.$leftIdP.' AND '.$rightId
  241. ;
  242. } else {
  243. $this->sql = 'UPDATE '.$this->table.' SET '
  244. . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->level.sprintf('%+d', -($level-1)+$levelP).', '.$this->level.'), '
  245. . $this->left.'=IF('.$this->left.' BETWEEN '.$rightId.' AND '.$rightIdP.', '.$this->left.'-'.($rightId-$leftId+1).', '
  246. . 'IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->left.'+'.($rightIdP-1-$rightId).', '.$this->left.')'
  247. . '), '
  248. . $this->right.'=IF('.$this->right.' BETWEEN '.($rightId+1).' AND '.($rightIdP-1).', '.$this->right.'-'.($rightId-$leftId+1).', '
  249. . 'IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->right.'+'.($rightIdP-1-$rightId).', '.$this->right.') '
  250. . ') '
  251. . 'WHERE '.$this->left.' BETWEEN '.$leftId.' AND '.$rightIdP
  252. // !!! added this line (Maxim Matyukhin)
  253. . ' OR '.$this->right.' BETWEEN '.$leftId.' AND '.$rightIdP
  254. ;
  255. }
  256. return $this->db->query($this->sql) or trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  257. }
  258. //************************************************************************
  259. // Deletes a record wihtout deleting its children
  260. // $ID : an ID of the element to be deleted
  261. // Returns : true on success, or false on error
  262. function delete($ID) {
  263. if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  264. // Deleting record
  265. $this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->id.'=\''.$ID.'\'';
  266. if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  267. // Clearing blank spaces in a tree
  268. $this->sql = 'UPDATE '.$this->table.' SET '
  269. . $this->left.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->left.'-1,'.$this->left.'),'
  270. . $this->right.'=IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->right.'-1,'.$this->right.'),'
  271. . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->level.'-1,'.$this->level.'),'
  272. . $this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'-2,'.$this->left.'),'
  273. . $this->right.'=IF('.$this->right.'>'.$rightId.','.$this->right.'-2,'.$this->right.') '
  274. . 'WHERE '.$this->right.'>'.$leftId
  275. ;
  276. if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  277. return true;
  278. }
  279. //************************************************************************
  280. // Deletes a record with all its children
  281. // $ID : an ID of the element to be deleted
  282. // Returns : true on success, or false on error
  283. function deleteAll($ID) {
  284. if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  285. // Deleteing record(s)
  286. $this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->left.' BETWEEN '.$leftId.' AND '.$rightId;
  287. if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  288. // Clearing blank spaces in a tree
  289. $deltaId = ($rightId - $leftId)+1;
  290. $this->sql = 'UPDATE '.$this->table.' SET '
  291. . $this->left.'=IF('.$this->left.'>'.$leftId.','.$this->left.'-'.$deltaId.','.$this->left.'),'
  292. . $this->right.'=IF('.$this->right.'>'.$leftId.','.$this->right.'-'.$deltaId.','.$this->right.') '
  293. . 'WHERE '.$this->right.'>'.$rightId
  294. ;
  295. if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  296. return true;
  297. }
  298. //************************************************************************
  299. // Enumerates children of an element
  300. // $ID : an ID of an element which children to be enumerated
  301. // $start_level : relative level from which start to enumerate children
  302. // $end_level : the last relative level at which enumerate children
  303. // 1. If $end_level isn't given, only children of
  304. // $start_level levels are enumerated
  305. // 2. Level values should always be greater than zero.
  306. // Level 1 means direct children of the element
  307. // Returns : a result id for using with other DB functions
  308. function enumChildrenAll($ID) { return $this->enumChildren($ID, 1, 0); }
  309. function enumChildren($ID, $start_level=1, $end_level=1) {
  310. if($start_level < 0) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  311. // We could use sprintf() here, but it'd be too slow
  312. $whereSql1 = ' AND '.$this->table.'.'.$this->level;
  313. $whereSql2 = '_'.$this->table.'.'.$this->level.'+';
  314. if(!$end_level) $whereSql = $whereSql1.'>='.$whereSql2.(int)$start_level;
  315. else {
  316. $whereSql = ($end_level <= $start_level)
  317. ? $whereSql1.'='.$whereSql2.(int)$start_level
  318. : ' AND '.$this->table.'.'.$this->level.' BETWEEN _'.$this->table.'.'.$this->level.'+'.(int)$start_level
  319. .' AND _'.$this->table.'.'.$this->level.'+'.(int)$end_level;
  320. }
  321. $this->sql = $this->sqlComposeSelect(array(
  322. '', // Params
  323. '', // Fields
  324. $this->table.' _'.$this->table.', '.$this->table, // Tables
  325. '_'.$this->table.'.'.$this->id.'=\''.$ID.'\''
  326. .' AND '.$this->table.'.'.$this->left.' BETWEEN _'.$this->table.'.'.$this->left.' AND _'.$this->table.'.'.$this->right
  327. .$whereSql
  328. ));
  329. return $this->db->query($this->sql);
  330. }
  331. //************************************************************************
  332. // Enumerates the PATH from an element to its top level parent
  333. // $ID : an ID of an element
  334. // $showRoot : whether to show root node in a path
  335. // Returns : a result id for using with other DB functions
  336. function enumPath($ID, $showRoot=false) {
  337. $this->sql = $this->sqlComposeSelect(array(
  338. '', // Params
  339. '', // Fields
  340. $this->table.' _'.$this->table.', '.$this->table, // Tables
  341. '_'.$this->table.'.'.$this->id.'=\''.$ID.'\''
  342. .' AND _'.$this->table.'.'.$this->left.' BETWEEN '.$this->table.'.'.$this->left.' AND '.$this->table.'.'.$this->right
  343. .(($showRoot) ? '' : ' AND '.$this->table.'.'.$this->level.'>0'), // Where
  344. '', // GroupBy
  345. '', // Having
  346. $this->table.'.'.$this->left // OrderBy
  347. ));
  348. return $this->db->query($this->sql);
  349. }
  350. //************************************************************************
  351. // Returns query result to fetch data of the element's parent
  352. // $ID : an ID of an element which parent to be retrieved
  353. // $level : Relative level of parent
  354. // Returns : a result id for using with other DB functions
  355. function getParent($ID, $level=1) {
  356. if($level < 1) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
  357. $this->sql = $this->sqlComposeSelect(array(
  358. '', // Params
  359. '', // Fields
  360. $this->table.' _'.$this->table.', '.$this->table, // Tables
  361. '_'.$this->table.'.'.$this->id.'=\''.$ID.'\''
  362. .' AND _'.$this->table.'.'.$this->left.' BETWEEN '.$this->table.'.'.$this->left.' AND '.$this->table.'.'.$this->right
  363. .' AND '.$this->table.'.'.$this->level.'=_'.$this->table.'.'.$this->level.'-'.(int)$level // Where
  364. ));
  365. return $this->db->query($this->sql);
  366. }
  367. //************************************************************************
  368. function sqlReset() {
  369. $this->qryParams = ''; $this->qryFields = ''; $this->qryTables = '';
  370. $this->qryWhere = ''; $this->qryGroupBy = ''; $this->qryHaving = '';
  371. $this->qryOrderBy = ''; $this->qryLimit = '';
  372. return true;
  373. }
  374. //************************************************************************
  375. function sqlSetReset($resetMode) { $this->sqlNeedReset = ($resetMode) ? true : false; }
  376. //************************************************************************
  377. function sqlParams($param='') { return (empty($param)) ? $this->qryParams : $this->qryParams = $param; }
  378. function sqlFields($param='') { return (empty($param)) ? $this->qryFields : $this->qryFields = $param; }
  379. function sqlSelect($param='') { return $this->sqlFields($param); }
  380. function sqlTables($param='') { return (empty($param)) ? $this->qryTables : $this->qryTables = $param; }
  381. function sqlFrom($param='') { return $this->sqlTables($param); }
  382. function sqlWhere($param='') { return (empty($param)) ? $this->qryWhere : $this->qryWhere = $param; }
  383. function sqlGroupBy($param='') { return (empty($param)) ? $this->qryGroupBy : $this->qryGroupBy = $param; }
  384. function sqlHaving($param='') { return (empty($param)) ? $this->qryHaving : $this->qryHaving = $param; }
  385. function sqlOrderBy($param='') { return (empty($param)) ? $this->qryOrderBy : $this->qryOrderBy = $param; }
  386. function sqlLimit($param='') { return (empty($param)) ? $this->qryLimit : $this->qryLimit = $param; }
  387. //************************************************************************
  388. function sqlComposeSelect($arSql) {
  389. $joinTypes = array('join'=>1, 'cross'=>1, 'inner'=>1, 'straight'=>1, 'left'=>1, 'natural'=>1, 'right'=>1);
  390. $this->sql = 'SELECT '.$arSql[0].' ';
  391. if(!empty($this->qryParams)) $this->sql .= $this->sqlParams.' ';
  392. if(empty($arSql[1]) && empty($this->qryFields)) $this->sql .= $this->table.'.'.$this->id;
  393. else {
  394. if(!empty($arSql[1])) $this->sql .= $arSql[1];
  395. if(!empty($this->qryFields)) $this->sql .= ((empty($arSql[1])) ? '' : ',') . $this->qryFields;
  396. }
  397. $this->sql .= ' FROM ';
  398. $isJoin = ($tblAr=explode(' ',trim($this->qryTables))) && ($joinTypes[strtolower($tblAr[0])]);
  399. if(empty($arSql[2]) && empty($this->qryTables)) $this->sql .= $this->table;
  400. else {
  401. if(!empty($arSql[2])) $this->sql .= $arSql[2];
  402. if(!empty($this->qryTables)) {
  403. if(!empty($arSql[2])) $this->sql .= (($isJoin)?' ':',');
  404. elseif($isJoin) $this->sql .= $this->table.' ';
  405. $this->sql .= $this->qryTables;
  406. }
  407. }
  408. if((!empty($arSql[3])) || (!empty($this->qryWhere))) {
  409. $this->sql .= ' WHERE ' . $arSql[3] . ' ';
  410. if(!empty($this->qryWhere)) $this->sql .= (empty($arSql[3])) ? $this->qryWhere : 'AND('.$this->qryWhere.')';
  411. }
  412. if((!empty($arSql[4])) || (!empty($this->qryGroupBy))) {
  413. $this->sql .= ' GROUP BY ' . $arSql[4] . ' ';
  414. if(!empty($this->qryGroupBy)) $this->sql .= (empty($arSql[4])) ? $this->qryGroupBy : ','.$this->qryGroupBy;
  415. }
  416. if((!empty($arSql[5])) || (!empty($this->qryHaving))) {
  417. $this->sql .= ' HAVING ' . $arSql[5] . ' ';
  418. if(!empty($this->qryHaving)) $this->sql .= (empty($arSql[5])) ? $this->qryHaving : 'AND('.$this->qryHaving.')';
  419. }
  420. if((!empty($arSql[6])) || (!empty($this->qryOrderBy))) {
  421. $this->sql .= ' ORDER BY ' . $arSql[6] . ' ';
  422. if(!empty($this->qryOrderBy)) $this->sql .= (empty($arSql[6])) ? $this->qryOrderBy : ','.$this->qryOrderBy;
  423. }
  424. if(!empty($arSql[7])) $this->sql .= ' LIMIT '.$arSql[7];
  425. elseif(!empty($this->qryLimit)) $this->sql .= ' LIMIT '.$this->qryLimit;
  426. if($this->sqlNeedReset) $this->sqlReset();
  427. return $this->sql;
  428. }
  429. //************************************************************************
  430. }
  431. ?>