PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/mysqlquery.php

https://bitbucket.org/deditec/finoplaner
PHP | 562 lines | 447 code | 97 blank | 18 comment | 85 complexity | c399189eb043ae297e593b1cba2dc15f MD5 | raw file
  1. <?php
  2. namespace Core;
  3. use \mysqli as mysqli;
  4. class MySQLQuery extends mysqli{
  5. public $db = null;
  6. protected $_dbHandle;
  7. protected $_result;
  8. protected $_query;
  9. protected $_table;
  10. protected $_describe = array();
  11. protected $_extraConditions = array();
  12. protected $_orderConditions = array();
  13. protected $_hO;
  14. protected $_hM;
  15. protected $_hMABTM;
  16. protected $_page;
  17. protected $_limit;
  18. function __construct() {
  19. $address = MySQL_DB_HOST;
  20. $account = MySQL_DB_USER;
  21. $pwd = MySQL_DB_PASSWORD;
  22. $name = MySQL_DB_NAME;
  23. return $this->s_connect($address, $account, $pwd, $name);
  24. }
  25. /** Connects to database **/
  26. function s_connect($address, $account, $pwd, $name)
  27. {
  28. $this->_dbHandle = mysql_connect($address, $account, $pwd);
  29. if ($this->_dbHandle != 0)
  30. {
  31. if (mysql_select_db($name, $this->_dbHandle))
  32. {
  33. return TRUE;
  34. }
  35. else
  36. {
  37. return FALSE;
  38. }
  39. }
  40. else
  41. {
  42. return FALSE;
  43. }
  44. }
  45. /** Disconnects from database **/
  46. function disconnect() {
  47. if (@mysql_close($this->_dbHandle) != 0) {
  48. return 1;
  49. } else {
  50. return 0;
  51. }
  52. }
  53. /** Select Query **/
  54. function where($field, $value, $compare_sign='=')
  55. {
  56. array_push($this->_extraConditions, '`'.$this->_model.'`.`'.$field.'` '.$compare_sign.' '.mysql_real_escape_string($value));
  57. return $this;
  58. }
  59. function where_in($field, $value)
  60. {
  61. if (!is_array($value))
  62. {
  63. $value = mysql_real_escape_string($value);
  64. }
  65. else
  66. {
  67. foreach ($value as $k=>$v)
  68. {
  69. $value[$k] = mysql_real_escape_string($value);
  70. }
  71. $value = implode(',',$value);
  72. }
  73. array_push($this->_extraConditions, '`'.$this->_model.'`.`'.$field.'` IN ( '.$value.')');
  74. return $this;
  75. }
  76. function like($field, $value) {
  77. $this->_extraConditions .= '`'.$this->_model.'`.`'.$field.'` LIKE \'%'.mysql_real_escape_string($value).'%\' AND ';
  78. return $this;
  79. }
  80. function fetch_associations($associations)
  81. {
  82. if ((isset($associations['one'])) && ($associations['one'] === TRUE))
  83. {
  84. $this->_hO = 1;
  85. }
  86. if ((isset($associations['many'])) && ($associations['many'] === TRUE))
  87. {
  88. $this->_hM = 1;
  89. }
  90. if ((isset($associations['manytomany'])) && ($associations['manytomany'] === TRUE))
  91. {
  92. $this->_hMABTM = 1;
  93. }
  94. return $this;
  95. }
  96. function limit($limit) {
  97. $this->_limit = $limit;
  98. return $this;
  99. }
  100. function page($page) {
  101. $this->_page = $page;
  102. return $this;
  103. }
  104. function order_by($orderBy, $order = 'ASC', $binary = FALSE, $model = NULL)
  105. {
  106. $orderBinary = '';
  107. if ($binary === TRUE)
  108. {
  109. $orderBinary = 'BINARY ';
  110. }
  111. if ($model === NULL)
  112. {
  113. $model = $this->_model;
  114. array_push($this->_orderConditions, $orderBinary.'`'.$model.'`.`'.$orderBy.'` '.$order);
  115. }
  116. return $this;
  117. }
  118. function find() {
  119. global $inflect;
  120. $from = '`'.$this->_table.'` as `'.$this->_model.'` ';
  121. $conditions = '\'1\'=\'1\'';
  122. $conditionsChild = '';
  123. $fromChild = '';
  124. if ($this->_hO == 1 && isset($this->hasOne)) {
  125. foreach ($this->hasOne as $alias => $model) {
  126. $table = strtolower($inflect->pluralize($model));
  127. $singularAlias = strtolower($alias);
  128. $from .= 'LEFT JOIN `'.$table.'` as `'.$alias.'` ';
  129. $from .= 'ON `'.$this->_model.'`.`'.$singularAlias.'_id` = `'.$alias.'`.`id` ';
  130. }
  131. }
  132. if ($this->id) {
  133. $conditions .= ' AND `'.$this->_model.'`.`id` = \''.mysql_real_escape_string($this->id).'\'';
  134. }
  135. if (!empty($this->_extraConditions)) {
  136. $conditions .= ' AND '.implode(' AND', $this->_extraConditions);
  137. }
  138. if (!empty($this->_orderConditions)) {
  139. $conditions .= ' ORDER BY '.implode(',',$this->_orderConditions);
  140. }
  141. if (isset($this->_page)) {
  142. $offset = ($this->_page-1)*$this->_limit;
  143. $conditions .= ' LIMIT '.$this->_limit.' OFFSET '.$offset;
  144. }
  145. $this->_query = 'SELECT * FROM '.$from.' WHERE '.$conditions;
  146. #echo '<!--'.$this->_query.'-->';
  147. $this->_result = mysql_query($this->_query, $this->_dbHandle);
  148. $result = array();
  149. $table = array();
  150. $field = array();
  151. $tempResults = array();
  152. $numOfFields = mysql_num_fields($this->_result);
  153. for ($i = 0; $i < $numOfFields; ++$i) {
  154. array_push($table,mysql_field_table($this->_result, $i));
  155. array_push($field,mysql_field_name($this->_result, $i));
  156. }
  157. if (mysql_num_rows($this->_result) > 0 ) {
  158. while ($row = mysql_fetch_row($this->_result)) {
  159. for ($i = 0;$i < $numOfFields; ++$i) {
  160. $tempResults[$table[$i]][$field[$i]] = $row[$i];
  161. }
  162. if ($this->_hM == 1 && isset($this->hasMany)) {
  163. foreach ($this->hasMany as $aliasChild => $modelChild) {
  164. $queryChild = '';
  165. $conditionsChild = '';
  166. $fromChild = '';
  167. $tableChild = strtolower($inflect->pluralize($modelChild));
  168. $pluralAliasChild = strtolower($inflect->pluralize($aliasChild));
  169. $singularAliasChild = strtolower($aliasChild);
  170. $fromChild .= '`'.$tableChild.'` as `'.$aliasChild.'`';
  171. $conditionsChild .= '`'.$aliasChild.'`.`'.strtolower($this->_model).'_id` = \''.$tempResults[$this->_model]['id'].'\'';
  172. $queryChild = 'SELECT * FROM '.$fromChild.' WHERE '.$conditionsChild;
  173. #echo '<!--'.$queryChild.'-->';
  174. $resultChild = mysql_query($queryChild, $this->_dbHandle);
  175. $tableChild = array();
  176. $fieldChild = array();
  177. $tempResultsChild = array();
  178. $resultsChild = array();
  179. if (mysql_num_rows($resultChild) > 0) {
  180. $numOfFieldsChild = mysql_num_fields($resultChild);
  181. for ($j = 0; $j < $numOfFieldsChild; ++$j) {
  182. array_push($tableChild,mysql_field_table($resultChild, $j));
  183. array_push($fieldChild,mysql_field_name($resultChild, $j));
  184. }
  185. while ($rowChild = mysql_fetch_row($resultChild)) {
  186. for ($j = 0;$j < $numOfFieldsChild; ++$j) {
  187. $tempResultsChild[$tableChild[$j]][$fieldChild[$j]] = $rowChild[$j];
  188. }
  189. array_push($resultsChild,$tempResultsChild);
  190. }
  191. }
  192. $tempResults[$aliasChild] = $resultsChild;
  193. mysql_free_result($resultChild);
  194. }
  195. }
  196. if ($this->_hMABTM == 1 && isset($this->hasManyAndBelongsToMany)) {
  197. foreach ($this->hasManyAndBelongsToMany as $aliasChild => $tableChild) {
  198. $queryChild = '';
  199. $conditionsChild = '';
  200. $fromChild = '';
  201. $tableChild = strtolower($inflect->pluralize($tableChild));
  202. $pluralAliasChild = strtolower($inflect->pluralize($aliasChild));
  203. $singularAliasChild = strtolower($aliasChild);
  204. $sortTables = array($this->_table,$pluralAliasChild);
  205. sort($sortTables);
  206. $joinTable = implode('_',$sortTables);
  207. $fromChild .= '`'.$tableChild.'` as `'.$aliasChild.'`,';
  208. $fromChild .= '`'.$joinTable.'`,';
  209. $conditionsChild .= '`'.$joinTable.'`.`'.$singularAliasChild.'_id` = `'.$aliasChild.'`.`id` AND ';
  210. $conditionsChild .= '`'.$joinTable.'`.`'.strtolower($this->_model).'_id` = \''.$tempResults[$this->_model]['id'].'\'';
  211. $fromChild = substr($fromChild,0,-1);
  212. $queryChild = 'SELECT * FROM '.$fromChild.' WHERE '.$conditionsChild;
  213. #echo '<!--'.$queryChild.'-->';
  214. $resultChild = mysql_query($queryChild, $this->_dbHandle);
  215. $tableChild = array();
  216. $fieldChild = array();
  217. $tempResultsChild = array();
  218. $resultsChild = array();
  219. if (mysql_num_rows($resultChild) > 0) {
  220. $numOfFieldsChild = mysql_num_fields($resultChild);
  221. for ($j = 0; $j < $numOfFieldsChild; ++$j) {
  222. array_push($tableChild,mysql_field_table($resultChild, $j));
  223. array_push($fieldChild,mysql_field_name($resultChild, $j));
  224. }
  225. while ($rowChild = mysql_fetch_row($resultChild)) {
  226. for ($j = 0;$j < $numOfFieldsChild; ++$j) {
  227. $tempResultsChild[$tableChild[$j]][$fieldChild[$j]] = $rowChild[$j];
  228. }
  229. array_push($resultsChild,$tempResultsChild);
  230. }
  231. }
  232. $tempResults[$aliasChild] = $resultsChild;
  233. mysql_free_result($resultChild);
  234. }
  235. }
  236. array_push($result,$tempResults);
  237. }
  238. if (mysql_num_rows($this->_result) == 1 && $this->id != null) {
  239. mysql_free_result($this->_result);
  240. $this->clear();
  241. return($result[0]);
  242. } else {
  243. mysql_free_result($this->_result);
  244. $this->clear();
  245. return($result);
  246. }
  247. } else {
  248. mysql_free_result($this->_result);
  249. $this->clear();
  250. return $result;
  251. }
  252. }
  253. /** Custom SQL Query **/
  254. function custom($query) {
  255. global $inflect;
  256. $this->_result = mysql_query($query, $this->_dbHandle);
  257. $result = array();
  258. $table = array();
  259. $field = array();
  260. $tempResults = array();
  261. if (mysql_num_rows($this->_result) > 0)
  262. {
  263. if(substr_count(strtoupper($query),"SELECT")>0)
  264. {
  265. $numOfFields = mysql_num_fields($this->_result);
  266. for ($i = 0; $i < $numOfFields; ++$i)
  267. {
  268. array_push($table,mysql_field_table($this->_result, $i));
  269. array_push($field,mysql_field_name($this->_result, $i));
  270. }
  271. while ($row = mysql_fetch_row($this->_result))
  272. {
  273. for ($i = 0;$i < $numOfFields; ++$i)
  274. {
  275. $table[$i] = ucfirst($inflect->singularize($table[$i]));
  276. $tempResults[$table[$i]][$field[$i]] = $row[$i];
  277. }
  278. array_push($result,$tempResults);
  279. }
  280. }
  281. else
  282. {
  283. $numOfFields = mysql_num_fields($this->_result);
  284. for ($i = 0; $i < $numOfFields; ++$i)
  285. {
  286. array_push($field,mysql_field_name($this->_result, $i));
  287. }
  288. while ($row = mysql_fetch_row($this->_result))
  289. {
  290. for ($i = 0;$i < $numOfFields; ++$i)
  291. {
  292. $tempResults[$field[$i]] = $row[$i];
  293. }
  294. array_push($result,$tempResults);
  295. }
  296. }
  297. }
  298. mysql_free_result($this->_result);
  299. $this->clear();
  300. return($result);
  301. }
  302. /** Describes a Table **/
  303. protected function _describe() {
  304. global $cache;
  305. $this->_describe = $cache->get('describe'.$this->_table);
  306. if (!$this->_describe) {
  307. $this->_describe = array();
  308. $query = 'DESCRIBE '.$this->_table;
  309. $this->_result = mysql_query($query, $this->_dbHandle);
  310. while ($row = mysql_fetch_row($this->_result)) {
  311. array_push($this->_describe,$row[0]);
  312. }
  313. mysql_free_result($this->_result);
  314. $cache->set('describe'.$this->_table,$this->_describe);
  315. }
  316. foreach ($this->_describe as $field) {
  317. $this->$field = null;
  318. }
  319. }
  320. /** Delete an Object **/
  321. function delete() {
  322. if ($this->id) {
  323. $query = 'DELETE FROM '.$this->_table.' WHERE `id`=\''.mysql_real_escape_string($this->id).'\'';
  324. $this->_result = mysql_query($query, $this->_dbHandle);
  325. $this->clear();
  326. if ($this->_result == 0) {
  327. /** Error Generation **/
  328. return -1;
  329. }
  330. } else {
  331. /** Error Generation **/
  332. return -1;
  333. }
  334. }
  335. /** Saves an Object i.e. Updates/Inserts Query **/
  336. function save() {
  337. $query = '';
  338. if (isset($this->id)) {
  339. $updates = '';
  340. foreach ($this->_describe as $field) {
  341. if ($this->$field) {
  342. $updates .= '`'.$field.'` = \''.mysql_real_escape_string($this->$field).'\',';
  343. }
  344. }
  345. $updates = substr($updates,0,-1);
  346. $query = 'UPDATE '.$this->_table.' SET '.$updates.' WHERE `id`=\''.mysql_real_escape_string($this->id).'\'';
  347. } else {
  348. $fields = '';
  349. $values = '';
  350. foreach ($this->_describe as $field) {
  351. if ($this->$field) {
  352. $fields .= '`'.$field.'`,';
  353. $values .= '\''.mysql_real_escape_string($this->$field).'\',';
  354. }
  355. }
  356. $values = substr($values,0,-1);
  357. $fields = substr($fields,0,-1);
  358. $query = 'INSERT INTO '.$this->_table.' ('.$fields.') VALUES ('.$values.')';
  359. }
  360. $this->_result = mysql_query($query, $this->_dbHandle);
  361. $this->clear();
  362. if ($this->_result == 0) {
  363. /** Error Generation **/
  364. return -1;
  365. }
  366. }
  367. function save2() {
  368. $query = '';
  369. if (isset($this->id)) {
  370. $updates = '';
  371. foreach ($this->_describe as $key=>$value) {
  372. if ($value) {
  373. $updates .= '`'.$key.'` = \''.mysql_real_escape_string($value).'\',';
  374. }
  375. }
  376. $updates = substr($updates,0,-1);
  377. $query = 'UPDATE '.$this->_table.' SET '.$updates.' WHERE `id`=\''.mysql_real_escape_string($this->id).'\'';
  378. } else {
  379. $fields = '';
  380. $values = '';
  381. foreach ($this->_describe as $key=>$value) {
  382. if ($value) {
  383. $fields .= '`'.$key.'`,';
  384. $values .= '\''.@mysql_real_escape_string($value).'\',';
  385. }
  386. }
  387. $values = substr($values,0,-1);
  388. $fields = substr($fields,0,-1);
  389. $query = 'INSERT INTO '.$this->_table.' ('.$fields.') VALUES ('.$values.')';
  390. }
  391. $this->_brada = $query;;
  392. $this->_result = mysql_query($query, $this->_dbHandle);
  393. $this->clear();
  394. if ($this->_result == 0) {
  395. /** Error Generation **/
  396. return -1;
  397. }
  398. $this->clear();
  399. }
  400. /** Clear All Variables **/
  401. function clear() {
  402. foreach($this->_describe as $field) {
  403. $this->$field = null;
  404. }
  405. $this->_orderby = null;
  406. $this->_extraConditions = null;
  407. $this->_hO = null;
  408. $this->_hM = null;
  409. $this->_hMABTM = null;
  410. $this->_page = null;
  411. $this->_order = null;
  412. }
  413. /** Pagination Count **/
  414. function total_pages() {
  415. global $inflect;
  416. $from = '`'.$this->_table.'` as `'.$this->_model.'` ';
  417. $conditions = '\'1\'=\'1\'';
  418. $conditionsChild = '';
  419. $fromChild = '';
  420. if ($this->_hO == 1 && isset($this->hasOne)) {
  421. foreach ($this->hasOne as $alias => $model) {
  422. $table = strtolower($inflect->pluralize($model));
  423. $singularAlias = strtolower($alias);
  424. $from .= 'LEFT JOIN `'.$table.'` as `'.$alias.'` ';
  425. $from .= 'ON `'.$this->_model.'`.`'.$singularAlias.'_id` = `'.$alias.'`.`id` ';
  426. }
  427. }
  428. if (($this->id) && ($this->_dbHandle !== NULL))
  429. {
  430. $conditions .= 'AND `'.$this->_model.'`.`id` = '.$this->_dbHandle->quote($this->id);
  431. }
  432. if (!empty($this->_extraConditions)) {
  433. $conditions .= ' AND '.implode(' AND', $this->_extraConditions);
  434. }
  435. if (!empty($this->_orderConditions)) {
  436. $conditions .= ' ORDER BY '.implode(',',$this->_orderConditions);
  437. }
  438. if (isset($this->_page)) {
  439. $offset = ($this->_page-1)*$this->_limit;
  440. $conditions .= ' LIMIT '.$this->_limit.' OFFSET '.$offset;
  441. }
  442. $this->_query = 'SELECT * FROM '.$from.' WHERE '.$conditions;
  443. if ($this->_limit && ($this->_dbHandle !== NULL)) {
  444. $pattern = '/SELECT (.*?) FROM (.*)LIMIT(.*)/i';
  445. $replacement = 'SELECT COUNT(*) FROM $2';
  446. $countQuery = preg_replace($pattern, $replacement, $this->_query);
  447. $this->_result = $this->_dbHandle->prepare($countQuery);
  448. $this->_result->execute();
  449. $count = $this->_result->fetch(PDO::FETCH_BOTH);
  450. $totalPages = ceil($count[0]/$this->_limit);
  451. return $totalPages;
  452. } else {
  453. /* Error Generation Code Here */
  454. return -1;
  455. }
  456. }
  457. /** Get error string **/
  458. function getError() {
  459. return mysql_error($this->_dbHandle);
  460. }
  461. function getErrorNo()
  462. {
  463. return mysql_errno($this->_dbHandle);
  464. }
  465. }