PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/basicCRUDPDO.class.php

https://gitlab.com/karl3/gs_libs
PHP | 364 lines | 191 code | 60 blank | 113 comment | 40 complexity | 87b428949dec9f6fcfebcffb1dfe6551 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Extends the db class to perform basic database interaction. Since 99% of
  5. * all the work I do is creating the same CRUD stuff over and over, it became
  6. * very apparent that what I needed was a simple class to do all these things
  7. * rather than writing the same fucking code over and over and over and over
  8. *
  9. */
  10. class basicCRUDPDO extends dbPDO
  11. {
  12. /**
  13. * @param string $dbName database name
  14. * @param string $user database username
  15. * @param string $pass database password
  16. * @param array $opts connection options.
  17. * @param string $dbType type of database - must be 'mysql', 'sqlite', or 'pgsql'
  18. * @param string $hostORpath ip or name of host (in the case of mysql or pgsql) or file path to db (in case of sqlite)
  19. * @param string $port
  20. */
  21. public function __construct($dbName, $user, $pass, $opts = null, $dbType = 'mysql', $hostORpath = 'localhost', $port = '')
  22. {
  23. parent::__construct($dbName, $user, $pass, $opts, $dbType, $hostORpath, $port);
  24. }
  25. /**
  26. * Destructor. Unsets all object variables
  27. */
  28. public function __destruct()
  29. {
  30. $vars = get_object_vars($this);
  31. if (is_array($vars)) {
  32. foreach ($vars as $key => $val) {
  33. $this->$key = null;
  34. }
  35. }
  36. parent::__destruct();
  37. }
  38. /**
  39. * Inserts a single record in the database using an associative array of values
  40. *
  41. * @param array $data array of key=>value pairs for insertion
  42. * @param string $table the table to insert into
  43. * @param bool $checkFields (optional) whether or not to check for each fields' existence
  44. * @param bool $checkTable (optional) whether or not to check for the table's existence
  45. *
  46. * @return bool true on success
  47. */
  48. public function insert($data, $table, $checkFields = false, $checkTable = false)
  49. {
  50. // (optional) check to make sure the table actually exists
  51. if (true === $checkTable) {
  52. if (false === $this->TableExists($this->dbName, $table)) {
  53. return false;
  54. }
  55. }
  56. // (optional) check to make sure each field actually exists
  57. // populate $fData (final data) with the right stuff
  58. if (true === $checkFields) {
  59. $tCols = $this->ListFields($table);
  60. $count = count($tCols);
  61. for ($x = 0; $x < $count; $x++) {
  62. $cols[] = $tCols[$x]['Field'];
  63. }
  64. foreach ($data AS $key => $val) {
  65. if (in_array($key, $cols)) {
  66. $fData[$key] = $val;
  67. }
  68. }
  69. } // otherwise $fData (final data) is the same data, untouched
  70. else {
  71. $fData = $data;
  72. }
  73. return $this->InsertArray($fData, $table);
  74. }
  75. /**
  76. * Updates a record in the database using an associative array of values
  77. *
  78. * @param array $data data for the updated record
  79. * @param string $table table
  80. * @param string $criteriaField field (such as the PK field)
  81. * @param string $criteriaValue value for the criteria field (again, such as the PK)
  82. * @param string $extras (optional) any extra stuff for the query
  83. * @param bool $checkTable (optional) whether or not to check that the table exists first
  84. * @param bool $checkFields (optional) whether or not to check that each field exists first
  85. *
  86. * @return bool
  87. */
  88. public function update($data, $table, $criteriaField, $criteriaValue, $extras = null, $checkTable = false, $checkFields = false)
  89. {
  90. // (optional) check to make sure the table actually exists
  91. if (true === $checkTable) {
  92. if (false === $this->TableExists($this->dbName, $table)) {
  93. return false;
  94. }
  95. }
  96. // (optional) check to make sure each field actually exists
  97. // populate $fData (final data) with the right stuff
  98. if (true === $checkFields) {
  99. $tCols = $this->ListFields($table);
  100. $count = count($tCols);
  101. for ($x = 0; $x < $count; $x++) {
  102. $cols[] = $tCols[$x]['Field'];
  103. }
  104. foreach ($data AS $key => $val) {
  105. if (in_array($key, $cols)) {
  106. $fData[$key] = $val;
  107. }
  108. }
  109. } // otherwise $fData (final data) is the same data, untouched
  110. else {
  111. $fData = $data;
  112. }
  113. return $this->UpdateArray($table, $fData, $criteriaField, $criteriaValue, $extras);
  114. }
  115. /**
  116. * Deletes a record. Can include some other criteria for the deletion(s)
  117. *
  118. * @param string $table table from which we're deleting records
  119. * @param string $criteriaField the criteria field (such as the PK field)
  120. * @param string $criteriaValue the criteria value (such as the PK value)
  121. * @param string $extras (optional) any extra stuff for the query
  122. * @param bool $checkTable (optional) whether or not to check that the table exists first
  123. * @param bool $checkField (optional) whether or not to check that the criteria field exists first
  124. *
  125. * @return bool
  126. */
  127. public function delete($table, $criteriaField, $criteriaValue, $extras = null, $checkTable = false, $checkField = false)
  128. {
  129. $query = '';
  130. // (optional) check to make sure the table actually exists
  131. if (true === $checkTable) {
  132. if (false === $this->TableExists($this->dbName, $table)) {
  133. return false;
  134. }
  135. }
  136. // (optional) check to make sure the criteriaField actually exists
  137. if (true === $checkField) {
  138. $tCols = $this->ListFields($table);
  139. $count = count($tCols);
  140. for ($x = 0; $x < $count; $x++) {
  141. $cols[] = $tCols[$x]['Field'];
  142. }
  143. if (!in_array($criteriaField, $cols)) {
  144. return false;
  145. }
  146. }
  147. // put together the query
  148. $query .= sprintf("DELETE FROM %s WHERE %s='%s'", $this->EscapeString($table), $this->EscapeString($criteriaField), $this->EscapeString($criteriaValue));
  149. if (!is_null($extras)) {
  150. $query .= sprintf(" %s ", $this->EscapeString($extras));
  151. }
  152. return $this->DeleteData($query);
  153. }
  154. /**
  155. * This selects a SINGLE record from the database based on specific criteria (such as a unique ID)
  156. *
  157. * @param string $table the table to retrieve from
  158. * @param string $idField the field which holds a unique ID
  159. * @param string $id the unique ID value
  160. * @param string $fields the fields to retrieve in the query
  161. * @param bool $checkTable (optional) whether or not to check that the table exists first
  162. * @param bool $checkField (optional) whether or not to check that the ID field exists
  163. *
  164. * @return array
  165. */
  166. public function getByID($table, $idField, $id, $fields = ' * ', $checkTable = false, $checkField = false)
  167. {
  168. // (optional) check to make sure the table actually exists
  169. if (true === $checkTable) {
  170. if (false === $this->TableExists($this->dbName, $table)) {
  171. return false;
  172. }
  173. }
  174. // (optional) check to make sure the criteriaField actually exists
  175. if (true === $checkField) {
  176. $tCols = $this->ListFields($table);
  177. $count = count($tCols);
  178. for ($x = 0; $x < $count; $x++) {
  179. $cols[] = $tCols[$x]['Field'];
  180. }
  181. if (!in_array($idField, $cols)) {
  182. return false;
  183. }
  184. }
  185. // put together the query
  186. $query = sprintf("SELECT %s FROM %s WHERE %s='%s'",
  187. $this->EscapeString($fields),
  188. $this->EscapeString($table),
  189. $this->EscapeString($idField),
  190. $this->EscapeString($id));
  191. return $this->SelectSingleRecord($query, PDO::FETCH_ASSOC);
  192. }
  193. /**
  194. * Selects all records from a table. Can include a number of optional criteria
  195. *
  196. * @param string $table the table to retrieve from
  197. * @param string $fields the fields to retrieve
  198. * @param string $criteriaField (optional) any specific fields to base the query on
  199. * @param string $criteriaValue (optional) the value for the criteria field
  200. * @param string $extras (optional) extra stuff in the query, such as extra criteria
  201. * @param string $orderBy (optional) column to sort by
  202. * @param string $orderDir (optional) direction of the sort
  203. * @param int $limit (optional) limit to the # of returned results
  204. * @param bool $checkTable (optional) whether to check that the table exists
  205. * @param bool $checkField (optional) whether to check that the criteria field exists
  206. *
  207. * @return array
  208. */
  209. public function getAll($table, $fields = ' * ', $criteriaField = null, $criteriaValue = null, $extras = null, $orderBy = null, $orderDir = '', $limit = null, $checkTable = false, $checkField = false)
  210. {
  211. $query = '';
  212. // (optional) check to make sure the table actually exists
  213. if (true === $checkTable) {
  214. if (false === $this->TableExists($this->dbName, $table)) {
  215. return false;
  216. }
  217. }
  218. // (optional) check to make sure the criteriaField actually exists
  219. if ((true == $checkField) && (!is_null($criteriaField))) {
  220. $tCols = $this->ListFields($table);
  221. $count = count($tCols);
  222. for ($x = 0; $x < $count; $x++) {
  223. $cols[] = $tCols[$x]['Field'];
  224. }
  225. if (!in_array($criteriaField, $cols)) {
  226. return false;
  227. }
  228. }
  229. $query .= sprintf(" SELECT %s FROM %s ", $this->EscapeString($fields), $this->EscapeString($table));
  230. if ((!is_null($criteriaField)) && (!is_null($criteriaValue))) {
  231. $query .= sprintf(" WHERE %s='%s' ", $this->EscapeString($criteriaField), $this->EscapeString($criteriaValue));
  232. }
  233. if (!is_null($extras)) {
  234. $query .= sprintf(" %s ", $this->EscapeString($extras));
  235. }
  236. if (!is_null($orderBy)) {
  237. $query .= sprintf(" ORDER BY %s %s ", $this->EscapeString($orderBy), $this->EscapeString($orderDir));
  238. }
  239. if (!is_null($limit)) {
  240. $query .= sprintf(" LIMIT %s ", $this->EscapeString($limit));
  241. }
  242. return $this->SelectData($query, PDO::FETCH_ASSOC);
  243. }
  244. /**
  245. * Gets a count of all items based on criteria
  246. *
  247. * @param string $table the table to get a count from
  248. * @param string $extras any optional extra criteria for selecting records
  249. *
  250. * @return int
  251. */
  252. public function countItems($table, $extras = null)
  253. {
  254. $query = sprintf("SELECT count(*) AS count FROM %s", $this->EscapeString($table));
  255. if (!is_null($extras)) {
  256. $query .= $extras;
  257. }
  258. $data = $this->SelectData($query, PDO::FETCH_NUM);
  259. return $data[0]['count'];
  260. }
  261. /**
  262. * Takes a SQL query and turns it into an HTML table
  263. *
  264. * @param string $query the query to select the data
  265. * @param array $attrs (optional) any attributes for the table
  266. *
  267. * @return string
  268. */
  269. public function SQL2HTMLTable($query, $attrs = null)
  270. {
  271. $output = '';
  272. $data = $this->SelectData($query, PDO::FETCH_ASSOC);
  273. if (false === $data) {
  274. return false;
  275. }
  276. $output .= "<table ";
  277. if (!is_null($attrs)) {
  278. foreach ($attrs AS $key => $val) {
  279. $output .= " $key=\"$val\" ";
  280. }
  281. }
  282. $output .= ">";
  283. $keys = array_keys($data[0]);
  284. $output .= "<thead>";
  285. $output .= "<tr>";
  286. foreach ($keys AS $val) {
  287. $output .= "<th scope=\"col\">$val</th>";
  288. }
  289. $output .= "</tr>";
  290. $output .= "</thead>";
  291. $output .= "<tbody>";
  292. $count = count($data);
  293. for ($x = 0; $x < $count; $x++) {
  294. $output .= "<tr>";
  295. foreach ($data[$x] AS $val) {
  296. $output .= "<td>$val</td>";
  297. }
  298. $output .= "</tr>";
  299. }
  300. $output .= "</tbody>";
  301. $output .= "</table>";
  302. return $output;
  303. }
  304. }