/library/core/DatabaseFunctions.php

https://github.com/janislankovskis/OwlSite · PHP · 452 lines · 332 code · 102 blank · 18 comment · 40 complexity · 833924b36093ad9e34828ae7dd24bf1a MD5 · raw file

  1. <?php
  2. /* def cache */
  3. //TODO: move to the Cache class!
  4. $cache = new stdClass();
  5. $cache->executedQueries = array();
  6. $cache->executedQueries['____queriesCount'] = 0;
  7. $cache->executedQueries['____totTime'] = 0;
  8. function dbExecute($query=null, $class='')
  9. {
  10. // global $conf;
  11. global $cache;
  12. if(is_array($query))
  13. {
  14. $query = getQuery($query);
  15. }
  16. $time1 = microtime(true);
  17. $result = mysql_query($query);
  18. $list = GetListFromResource($result, $class);
  19. //if($conf['CACHEQUERIES'])
  20. //{
  21. $time2 = microtime(true);
  22. $cache->executedQueries[sha1($query)] = array(
  23. 'query' => $query,
  24. 'time' => $time2-$time1,
  25. );
  26. $cache->executedQueries['____totTime'] = $cache->executedQueries['____totTime'] + ($time2-$time1);
  27. $cache->executedQueries['____queriesCount']++;
  28. //}
  29. if($error = mysql_error())
  30. {
  31. if(DEV)
  32. {
  33. echo '<pre>' . $error . '</pre>';
  34. echo '<pre>' . $query . '</pre>';
  35. }
  36. return;
  37. }
  38. return $list;
  39. }
  40. function GetListFromResource($result, $class='')
  41. {
  42. if(gettype($result) == 'resource')
  43. {
  44. $list = array();
  45. if($class!='')
  46. {
  47. while($f = mysql_fetch_object($result, $class))
  48. {
  49. $list[] = stripslashesObject($f, $class);
  50. }
  51. }
  52. else
  53. {
  54. while($f = mysql_fetch_assoc($result))
  55. {
  56. $list[] = $f;
  57. }
  58. }
  59. return $list;
  60. }
  61. else
  62. {
  63. return $result;
  64. }
  65. }
  66. function stripslashesObject($row, $class)
  67. {
  68. foreach(get_class_vars($class) as $key=>$val)
  69. {
  70. if(isset($row->$key))
  71. {
  72. if(is_string($row->$key))
  73. {
  74. $row->$key = stripslashes($row->$key);
  75. }
  76. elseif(is_array($row->$key))
  77. {
  78. foreach($row->$key as $x=>$y)
  79. {
  80. if(is_string($row->$key))
  81. {
  82. $row->$key[$x] = stripslashes($y);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return $row;
  89. }
  90. function getQuery($parts)
  91. {
  92. $string = '';
  93. //select
  94. if(isset($parts['select']))
  95. {
  96. $string .= 'SELECT ';
  97. if(is_array($parts['select']))
  98. {
  99. $string .= implode(',', $parts['select']);
  100. }
  101. else
  102. {
  103. $string .= $parts['select'];
  104. }
  105. }
  106. //from
  107. if(isset($parts['from']))
  108. {
  109. if(is_array($parts['from']))
  110. {
  111. //recursion !
  112. $string .= ' FROM (' . getQuery($parts['from']) . ') as q';
  113. }
  114. else
  115. {
  116. $string .= ' FROM ' . $parts['from'];
  117. }
  118. }
  119. //use index
  120. if(isset($parts['useIndex']))
  121. {
  122. $string .= ' USE INDEX ( ' . $parts['useIndex'] . ' )';
  123. }
  124. //leftjoin
  125. if(isset($parts['leftJoin']))
  126. {
  127. if(is_array($parts['leftJoin']))
  128. {
  129. $left = '';
  130. foreach($parts['leftJoin'] as $item)
  131. {
  132. $left .= ' LEFT JOIN ' . $item;
  133. }
  134. }
  135. else
  136. {
  137. $left = 'LEFT JOIN ' . $parts['leftJoin'];
  138. }
  139. $string .= ' ' . $left;
  140. }
  141. //where
  142. if(isset($parts['where']))
  143. {
  144. $string .= ' WHERE';
  145. if(is_array($parts['where']))
  146. {
  147. $where = ' ';
  148. foreach($parts['where'] as $item)
  149. {
  150. $where .= '(' . $item . ') AND ';
  151. }
  152. $where = substr($where, 0, -5);
  153. }
  154. else
  155. {
  156. $where = $parts['where'];
  157. }
  158. $string .= ' ' . $where;
  159. }
  160. //group
  161. if(isset($parts['group']))
  162. {
  163. $string .= ' GROUP BY ' . $parts['group'];
  164. }
  165. //order
  166. if(isset($parts['order']))
  167. {
  168. if(is_array($parts['order']))
  169. {
  170. $order = ' ';
  171. foreach($parts['order'] as $item)
  172. {
  173. $order .= $item . ',';
  174. }
  175. $order = substr($order, 0, -1);
  176. }
  177. else
  178. {
  179. $order = $parts['order'];
  180. }
  181. $string .= ' ORDER BY ' . $order;
  182. }
  183. //limit
  184. if(isset($parts['limit']))
  185. {
  186. $string .= ' LIMIT ' . $parts['limit'];
  187. }
  188. return $string;
  189. }
  190. function returnFirstRow($query)
  191. {
  192. $result = dbExecute($query);
  193. if(sizeof($result))
  194. {
  195. return $result[0];
  196. }
  197. else
  198. {
  199. return false;
  200. }
  201. }
  202. function dbReplace($data, $table)
  203. {
  204. return dbInsert($data, $table, true);
  205. }
  206. function dbInsert($data, $table, $replace=false)
  207. {
  208. if(!$replace)
  209. {
  210. $q = 'INSERT INTO ' . $table . ' (';
  211. }
  212. else
  213. {
  214. $q = 'REPLACE INTO ' . $table . ' (';
  215. }
  216. //add fields
  217. $fields = '';
  218. foreach($data as $key=>$val)
  219. {
  220. $fields .= '`' . $key .'`, ';
  221. }
  222. $fields = substr($fields, 0, -2);
  223. $q .= $fields . ') VALUES (';
  224. $values = '';
  225. foreach($data as $key=>$val)
  226. {
  227. if(is_numeric($val))
  228. {
  229. $values .= $val . ', ';
  230. }
  231. elseif($val == '__NOW()__')
  232. {
  233. $values .= 'NOW(), ';
  234. }
  235. elseif(is_string($val))
  236. {
  237. $values .= '"' . mysql_real_escape_string($val) .'", ';
  238. }
  239. else //null etc
  240. {
  241. $values .= ' "", ';
  242. }
  243. }
  244. $values = substr($values, 0, -2);
  245. $q .= $values . ')';
  246. dbExecute($q);
  247. return true;
  248. }
  249. function dbDelete($key, $table)
  250. {
  251. if(!is_array($key) || sizeof($key)!=2)
  252. {
  253. return;
  254. }
  255. if(!is_numeric($key[1]))
  256. {
  257. $key[1] = '"' . $key[1] . '"';
  258. }
  259. $q = 'DELETE FROM ' . $table . ' WHERE ' . $key[0] .' = ' . $key[1] . ' LIMIT 1';
  260. dbExecute($q);
  261. return;
  262. }
  263. function dbGetOneObject($class, $object)
  264. {
  265. if(!is_numeric($object->id))
  266. {
  267. debug('fail');
  268. return;
  269. }
  270. $q = array(
  271. 'select' => 'c.*',
  272. 'from' => $object->tableName . ' c',
  273. 'where' => 'c.id = ' . $object->id
  274. );
  275. $list = dbGetObjectList($class, $q);
  276. if(!$list)
  277. {
  278. return false;
  279. }
  280. foreach($list as $obj)
  281. {
  282. //return first
  283. return $obj;
  284. }
  285. }
  286. function dbGetObjectList($class, $query)
  287. {
  288. global $conf;
  289. global $cache;
  290. if(is_array($query))
  291. {
  292. $query = getQuery($query);
  293. }
  294. if($conf['CACHEQUERIES']){ $time1 = microtime(true); }
  295. $result = mysql_query($query);
  296. if($conf['CACHEQUERIES'])
  297. {
  298. $time2 = microtime(true);
  299. $cache->DBCache[] = array(
  300. 'query' => $query,
  301. 'time' => $time2-$time1,
  302. );
  303. }
  304. if($error = mysql_error())
  305. {
  306. if(DEV)
  307. {
  308. debug ($error, false);
  309. debug ($query, false);
  310. }
  311. //TODO: if possible write error to DB
  312. return;
  313. }
  314. if(gettype($result) == 'resource')
  315. {
  316. $list = array();
  317. while($f = mysql_fetch_object($result, $class))
  318. {
  319. $list[] = $f;
  320. }
  321. return $list;
  322. }
  323. else
  324. {
  325. return $result;
  326. }
  327. }
  328. function dbGetId($table)
  329. {
  330. $q = 'SELECT MAX(id) as id FROM ' . $table;
  331. $r = returnFirstRow($q);
  332. if(isset($r['id']))
  333. {
  334. return $r['id'];
  335. }
  336. return false;
  337. }
  338. function setCachingOff()
  339. {
  340. global $conf;
  341. $conf['CACHEQUERIES'] = false;
  342. return;
  343. }
  344. function setCachingOn()
  345. {
  346. global $conf;
  347. $conf['CACHEQUERIES'] = true;
  348. return;
  349. }
  350. ?>