PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/API/DB/DRIVERS/MYSQL/DAVE.php

https://github.com/ghosthamlet/PHP-DAVE-API
PHP | 513 lines | 420 code | 33 blank | 60 comment | 119 complexity | faf9d7327ff4e14af55604c8c87c5455 MD5 | raw file
  1. <?php
  2. /***********************************************
  3. DAVE PHP API
  4. https://github.com/evantahler/PHP-DAVE-API
  5. Evan Tahler | 2011
  6. I define the functions and classes to handle the general-case database actions of Delete, Add, View, and Edit (DAVE).
  7. The 4 major functions will return an array. The first value is true or false indicating if the SQL action worked. The second object is either an array of results, or an error message.
  8. See additional notes below.
  9. ***********************************************/
  10. /*
  11. Table should be defned in $TABLES
  12. $VARS will be the params of the row to be added. If unspecified, $PARAMS is used by default)
  13. */
  14. function _ADD($Table, $VARS = null)
  15. {
  16. Global $TABLES, $DBOBJ, $Connection, $PARAMS;
  17. if ($VARS == null){$VARS = $PARAMS;}
  18. if(_tableCheck($Table))
  19. {
  20. $UniqueVars = _getUniqueTableVars($Table);
  21. $RequiredVars = _getRequiredTableVars($Table);
  22. $AllTableVars = _getAllTableCols($Table);
  23. $SQLKeys = array();
  24. $SQLValues= array();
  25. $Status = $DBOBJ->GetStatus();
  26. if ($Status !== true)
  27. {
  28. return array(false,$Status);
  29. }
  30. foreach($RequiredVars as $req)
  31. {
  32. if(strlen($VARS[$req]) == 0)
  33. {
  34. return array(false,$req." is a required value and you must provide a value");
  35. }
  36. }
  37. foreach($VARS as $var => $val)
  38. {
  39. if (in_array($var,$AllTableVars))
  40. {
  41. if (in_array($var, $UniqueVars) && strlen($val) > 0) // unique
  42. {
  43. $SQL = 'SELECT COUNT(1) FROM `'.$Table.'` WHERE (`'.$var.'` = "'.$val.'") ;';
  44. $DBOBJ->Query($SQL);
  45. $Status = $DBOBJ->GetStatus();
  46. if ($Status === true){
  47. $results = $DBOBJ->GetResults();
  48. if ($results[0]['COUNT(1)'] > 0)
  49. {
  50. return array(false,"There is already an entry of '".$val."' for ".$var);
  51. }
  52. else // var OK!
  53. {
  54. $SQLKeys[] = $var;
  55. $SQLValues[] = $val;
  56. }
  57. }
  58. }
  59. elseif (strlen($val) > 0) // non-unique
  60. {
  61. $SQLKeys[] = $var;
  62. $SQLValues[] = $val;
  63. }
  64. }
  65. }
  66. //
  67. $SQL = "INSERT INTO `".$Table."` ( ";
  68. $i = 0;
  69. $needComma = false;
  70. while ($i < count($SQLKeys))
  71. {
  72. if ($needComma) { $SQL .= ", "; }
  73. $SQL .= ' `'.$SQLKeys[$i].'` ';
  74. $needComma = true;
  75. $i++;
  76. }
  77. $SQL .= " ) VALUES ( ";
  78. $i = 0;
  79. $needComma = false;
  80. while ($i < count($SQLValues))
  81. {
  82. if ($needComma) { $SQL .= ", "; }
  83. $SQL .= ' "'.mysql_real_escape_string($SQLValues[$i],$Connection).'" ';
  84. $needComma = true;
  85. $i++;
  86. }
  87. $SQL .= " ); ";
  88. $DBOBJ->Query($SQL);
  89. $Status = $DBOBJ->GetStatus();
  90. if ($Status === true)
  91. {
  92. $NewKey = $DBOBJ->GetLastInsert();
  93. return array(true,array( $TABLES[$Table]['META']['KEY'] => $NewKey));
  94. }
  95. else{return array(false,$Status); }
  96. }
  97. else
  98. {
  99. return array(false,"This table cannot be found");
  100. }
  101. }
  102. /***********************************************/
  103. /*
  104. Table should be defned in $TABLES
  105. $VARS will be the params of the row to be added. VARS should include a key/value pair which includes the primary key for the DB. If unspecified, $PARAMS is used by default)
  106. */
  107. function _EDIT($Table, $VARS = null)
  108. {
  109. Global $TABLES, $DBOBJ, $Connection, $PARAMS;
  110. if ($VARS == null){$VARS = $PARAMS;}
  111. if(_tableCheck($Table))
  112. {
  113. $UniqueVars = _getUniqueTableVars($Table);
  114. $RequiredVars = _getRequiredTableVars($Table);
  115. $AllTableVars = _getAllTableCols($Table);
  116. $SQLKeys = array();
  117. $SQLValues= array();
  118. $Status = $DBOBJ->GetStatus();
  119. if ($Status !== true)
  120. {
  121. return array(false,$Status);
  122. }
  123. // get the META KEY if it wasn't provided explicitly
  124. if ($VARS[$TABLES[$Table]['META']['KEY']] == "")
  125. {
  126. $SQL = 'SELECT '.$TABLES[$Table]['META']['KEY'].' FROM `'.$Table.'` WHERE ( ';
  127. $NeedAnd = false;
  128. foreach($VARS as $var => $val)
  129. {
  130. if (in_array($var,$UniqueVars) && $val != "")
  131. {
  132. if ($NeedAnd) { $SQL .= " AND "; }
  133. $SQL .= ' `'.$var.'` = "'.$val.'" ';
  134. $NeedAnd = true;
  135. }
  136. }
  137. $SQL .= ' ) ;';
  138. $DBOBJ->Query($SQL);
  139. $Status = $DBOBJ->GetStatus();
  140. if ($Status === true)
  141. {
  142. $results = $DBOBJ->GetResults();
  143. if (count($results) == 1)
  144. {
  145. $VARS[$TABLES[$Table]['META']['KEY']] = $results[0][$TABLES[$Table]['META']['KEY']];
  146. }
  147. else // var OK!
  148. {
  149. return array(false,"You need to supply the META KEY for this table, ".$TABLES[$Table]['META']['KEY']);
  150. }
  151. }
  152. else
  153. {
  154. return array(false,"You need to supply the META KEY for this table, ".$TABLES[$Table]['META']['KEY'].", or one of the unique keys.");
  155. }
  156. }
  157. //loop
  158. if(is_array($VARS))
  159. {
  160. foreach($VARS as $var => $val)
  161. {
  162. if ($var != $TABLES[$Table]['META']['KEY'])
  163. {
  164. // if (in_array($var, $RequiredVars) && _isSpecialString($val)) // required
  165. // {
  166. // return array(false,$var." is a required value and you must provide a value");
  167. // }
  168. if (in_array($var,$AllTableVars))
  169. {
  170. if (in_array($var, $UniqueVars) && strlen($val) > 0) // unique
  171. {
  172. $SQL = 'SELECT COUNT(1) FROM `'.$Table.'` WHERE (`'.$var.'` = "'.$val.'" AND `'.$TABLES[$Table]['META']['KEY'].'` != "'.$VARS[$TABLES[$Table]['META']['KEY']].'") ;';
  173. $DBOBJ->Query($SQL);
  174. $Status = $DBOBJ->GetStatus();
  175. if ($Status === true){
  176. $results = $DBOBJ->GetResults();
  177. if ($results[0]['COUNT(1)'] > 0)
  178. {
  179. return array(false,"There is already an entry of '".$val."' for ".$var);
  180. }
  181. else // var OK!
  182. {
  183. $SQLKeys[] = $var;
  184. $SQLValues[] = $val;
  185. }
  186. }
  187. }
  188. elseif (strlen($val) > 0) // non-unique
  189. {
  190. $SQLKeys[] = $var;
  191. $SQLValues[] = $val;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. //
  198. if(strlen($VARS[$TABLES[$Table]['META']['KEY']]) > 0)
  199. {
  200. if (count($SQLKeys) > 0)
  201. {
  202. $SQL = "UPDATE `".$Table."` SET ";
  203. $i = 0;
  204. $needComma = false;
  205. while ($i < count($SQLKeys))
  206. {
  207. if ($needComma) { $SQL .= ", "; }
  208. $SQL .= ' `'.$SQLKeys[$i].'` = "'.mysql_real_escape_string($SQLValues[$i],$Connection).'" ';
  209. $needComma = true;
  210. $i++;
  211. }
  212. $SQL .= ' WHERE ( `'.$TABLES[$Table]['META']['KEY'].'` = "'.$VARS[$TABLES[$Table]['META']['KEY']].'" ); ';
  213. $DBOBJ->Query($SQL);
  214. $Status = $DBOBJ->GetStatus();
  215. if ($Status === true)
  216. {
  217. $NewKey = $DBOBJ->GetLastInsert();
  218. return _VIEW($Table, $VARS); // do a view again to return fresh data
  219. }
  220. else{ return array(false,$Status); }
  221. }
  222. else
  223. {
  224. return array(false,"There is nothing to change");
  225. }
  226. }
  227. else
  228. {
  229. return array(false,"You need to provide a parameter for the KEY of this table, ".$VARS[$TABLES[$Table]['META']['KEY']]);
  230. }
  231. }
  232. else
  233. {
  234. return array(false,"This table cannot be found");
  235. }
  236. }
  237. /***********************************************/
  238. /*
  239. Table should be defned in $TABLES
  240. $VARS will be the params of the row to be added. VARS should include a key/value pair which includes either the primary key for the DB or one of the unique cols for the table. If unspecified, $PARAMS is used by default)
  241. Settins is an array that can contain:
  242. - $Settings["select"]: a replacement select statement (rather than "*"). Example: "FirstName as Name, Address as Addy". Only Name and Addy will be returned.
  243. - $Settings["join"]: Join statement (first "JOIN" is added automatically).
  244. - $Settings["where_additions"]: Specific where statement. Example: Birtday = "1984-08-27"
  245. - $Settings["sort"]: sort statment. Example: "Order by Date DESC"
  246. - $Settings["UpperLimit"]: used for LIMIT statement. Defaults to 100
  247. - $Settings["LowerLimit"]: used for LIMIT statement. Defaults to 0
  248. - $Settings["SQL_Override"]: normally, DAVE wants to only view a single row, and will error unless that row can be defined properly with unique values. set this true to bypass these checks, and view many rows at once
  249. */
  250. function _VIEW($Table, $VARS = null, $Settings = null )
  251. {
  252. Global $TABLES, $DBOBJ, $Connection, $PARAMS;
  253. if ($VARS == null){$VARS = $PARAMS;}
  254. // Additonal _VIEW Options and Configurations
  255. if ($Settings == null){ $Settings = array(); }
  256. $select = $Settings["select"];
  257. $join = $Settings["join"];
  258. $where_additions = $Settings["where_additions"];
  259. $sort = $Settings["sort"];
  260. $UpperLimit = $Settings["UpperLimit"];
  261. $LowerLimit = $Settings["LowerLimit"];
  262. $SQL_Override = $Settings["SQL_Override"];
  263. if ($UpperLimit === ""){$UpperLimit = $PARAMS["UpperLimit"];}
  264. if ($LowerLimit === ""){$LowerLimit = $PARAMS["LowerLimit"];}
  265. if(_tableCheck($Table))
  266. {
  267. $UniqueVars = _getUniqueTableVars($Table);
  268. $AllTableVars = _getAllTableCols($Table);
  269. if ($select != null)
  270. {
  271. $SQL = "SELECT ". $select . " ";
  272. }
  273. else
  274. {
  275. $SQL = "SELECT * FROM `".$Table."` ";
  276. }
  277. if ($join != null)
  278. {
  279. $SQL .= " JOIN ".$join." ";
  280. }
  281. $SQL .= " WHERE (";
  282. $NeedAnd = false;
  283. if (strlen($VARS[$TABLES[$Table]['META']['KEY']]) > 0 && $SQL_Override != true) // if the primary key is given, use JUST this
  284. {
  285. $SQL .= ' `'.$TABLES[$Table]['META']['KEY'].'` = "'.$VARS[$TABLES[$Table]['META']['KEY']].'" ';
  286. $NeedAnd = true;
  287. }
  288. else
  289. {
  290. foreach($VARS as $var => $val)
  291. {
  292. if (in_array($var, $AllTableVars) && strlen($val) > 0)
  293. {
  294. if ($NeedAnd) { $SQL .= " AND "; }
  295. $SQL .= ' `'.$var.'` = "'.$val.'" ';
  296. $NeedAnd = true;
  297. }
  298. }
  299. }
  300. if ($where_additions != null)
  301. {
  302. if ($NeedAnd) { $SQL .= " AND "; }
  303. $SQL .= " ".$where_additions." ";
  304. $NeedAnd = true;
  305. }
  306. if($NeedAnd == false && $SQL_Override != true)
  307. {
  308. $msg = "You have supplied none of the required parameters for this Action. At least one of the following is required: ";
  309. foreach($UniqueVars as $var)
  310. {
  311. $msg .= $var." ";
  312. }
  313. return array(false,$msg);
  314. }
  315. elseif ($NeedAnd == false && $SQL_Override == true)
  316. {
  317. $SQL .= " true ";
  318. }
  319. $SQL .= " ) ";
  320. if ($sort != null)
  321. {
  322. $SQL .= $sort;
  323. }
  324. if ($UpperLimit < $LowerLimit) { return array(false,"UpperLimit must be greater than LowerLimit"); }
  325. elseif ($LowerLimit !== "" && $UpperLimit !== "" && $LowerLimit != $UpperLimit) { $SQL .= " LIMIT ".$LowerLimit.",".($UpperLimit - $LowerLimit)." "; }
  326. //
  327. $Status = $DBOBJ->GetStatus();
  328. if ($Status === true)
  329. {
  330. $DBOBJ->Query($SQL);
  331. $Status = $DBOBJ->GetStatus();
  332. if ($Status === true){ return array(true, $DBOBJ->GetResults()); }
  333. else{ return array(false,$Status); }
  334. }
  335. else { return array(false,$Status); }
  336. }
  337. else
  338. {
  339. return array(false,"This table cannot be found");
  340. }
  341. }
  342. /***********************************************/
  343. /*
  344. Table should be defned in $TABLES
  345. $VARS will be the params of the row to be added. VARS should include a key/value pair which includes either the primary key for the DB or one of the unique cols for the table. If unspecified, $PARAMS is used by default)
  346. Settins is an array that can contain:
  347. - $Settings["join"]: Join statement (first "JOIN" is added automatically).
  348. - $Settings["where_additions"]: Specific where statement. Example: Birtday = "1984-08-27"
  349. */
  350. function _COUNT($Table, $VARS = null, $Settings = null)
  351. {
  352. Global $TABLES, $DBOBJ, $Connection, $PARAMS;
  353. if ($VARS == null){$VARS = $PARAMS;}
  354. // Additonal _VIEW Options and Configurations
  355. if ($Settings == null){ $Settings = array(); }
  356. $join = $Settings["join"];
  357. $where_additions = $Settings["where_additions"];
  358. if(_tableCheck($Table))
  359. {
  360. $AllTableVars = _getAllTableCols($Table);
  361. $SQL = "SELECT COUNT(1) as 'count' FROM ".$Table." ";
  362. if ($join != null)
  363. {
  364. $SQL .= " JOIN ".$join." ";
  365. }
  366. $SQL .= " WHERE (";
  367. $NeedAnd = false;
  368. foreach($VARS as $var => $val)
  369. {
  370. if (in_array($var, $AllTableVars) && strlen($val) > 0)
  371. {
  372. if ($NeedAnd) { $SQL .= " AND "; }
  373. $SQL .= ' `'.$var.'` = "'.$val.'" ';
  374. $NeedAnd = true;
  375. }
  376. }
  377. if ($where_additions != null)
  378. {
  379. if ($NeedAnd) { $SQL .= " AND "; }
  380. $SQL .= " ".$where_additions." ";
  381. $NeedAnd = true;
  382. }
  383. elseif ($NeedAnd == false)
  384. {
  385. $SQL .= " true ";
  386. }
  387. $SQL .= " ) ";
  388. //
  389. $Status = $DBOBJ->GetStatus();
  390. if ($Status === true)
  391. {
  392. $DBOBJ->Query($SQL);
  393. $Status = $DBOBJ->GetStatus();
  394. $results = $DBOBJ->GetResults();
  395. if ($Status === true){ return array(true, $results[0]['count']); }
  396. else{ return array(false,$Status); }
  397. }
  398. else { return array(false,$Status); }
  399. }
  400. else
  401. {
  402. return array(false,"This table cannot be found");
  403. }
  404. }
  405. /***********************************************/
  406. /*
  407. Table should be defned in $TABLES
  408. $VARS will be the params of the row to be added. If unspecified, $PARAMS is used by default)
  409. */
  410. function _DELETE($Table, $VARS = null)
  411. {
  412. Global $TABLES, $DBOBJ, $Connection, $PARAMS;
  413. if ($VARS == null){$VARS = $PARAMS;}
  414. if(_tableCheck($Table))
  415. {
  416. $UniqueVars = _getUniqueTableVars($Table);
  417. $AllTableVars = _getAllTableCols($Table);
  418. $SQL = "DELETE FROM `".$Table."` WHERE ( ";
  419. $SQL2 = "SELECT COUNT(1) FROM `".$Table."` WHERE ( ";
  420. $NeedAnd = false;
  421. if(is_array($VARS))
  422. {
  423. foreach($VARS as $var => $val)
  424. {
  425. if (in_array($var, $AllTableVars) && strlen($val) > 0)
  426. {
  427. if ($NeedAnd) { $SQL .= " AND "; $SQL2 .= " AND "; }
  428. $SQL .= ' `'.$var.'` = "'.$val.'" ';
  429. $SQL2 .= ' `'.$var.'` = "'.$val.'" ';
  430. $NeedAnd = true;
  431. }
  432. }
  433. }
  434. if($NeedAnd == false)
  435. {
  436. $msg = "You have supplied none of the required parameters to make this query. At least one of the following is required: ";
  437. foreach($UniqueVars as $var)
  438. {
  439. $msg .= $var." ";
  440. }
  441. return array(false,$msg);
  442. }
  443. $SQL .= " ) ;"; // There is no limit to allow more than one removal
  444. $SQL2 .= " ) ;";
  445. //
  446. $Status = $DBOBJ->GetStatus();
  447. if ($Status === true)
  448. {
  449. $DBOBJ->Query($SQL2);
  450. $Status = $DBOBJ->GetStatus();
  451. if ($Status === true)
  452. {
  453. $results = $DBOBJ->GetResults();
  454. if ($results[0]['COUNT(1)'] > 1)
  455. {
  456. return array(false,"More than one item matches these parameters. Only one row can be deleted at a time.");
  457. }
  458. elseif($results[0]['COUNT(1)'] < 1)
  459. {
  460. return array(false,"The row specified for deletion cannot be found.");
  461. }
  462. }
  463. else{ return array(false,"The item you are requesting to delete is not found"); }
  464. $DBOBJ->Query($SQL);
  465. $Status = $DBOBJ->GetStatus();
  466. if ($Status === true){ return array(true, true); }
  467. else{ return array(false,$Status); }
  468. }
  469. else {return array(false,$Status); }
  470. }
  471. else
  472. {
  473. return array(false,"This table cannot be found");
  474. }
  475. }
  476. ?>