DBInstance.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace ORM;
  3. class DBInstance
  4. {
  5. private static function addPrefix($sql, $instance = 'default')
  6. {
  7. $con = Connections::getConnection($instance);
  8. //Where is preg_replace when we need it D`:
  9. $sql = str_replace("\n", "", $sql);
  10. while (strpos($sql, " ") > -1) {
  11. $sql = str_replace(" ", " ", $sql);
  12. }
  13. $sql = str_replace("{", $con->prefix, $sql);
  14. $sql = str_replace("}", "", $sql);
  15. return $sql;
  16. }
  17. /**
  18. * Execute
  19. *
  20. * This method will execute a SQL on the database
  21. *
  22. * @ctag DBInstance::execute($sql);
  23. * @ctag DBInstance::execute($sql, []);
  24. *
  25. * @param String $sql The SQL to be executed
  26. * @param Array $data Pass the info to be replaced on the query_prepare
  27. * @param String $instance Select the connection to execute
  28. */
  29. public static function execute($sql, $data = null, $instance = 'default')
  30. {
  31. $sql = self::addPrefix($sql, $instance);
  32. if ($data) {
  33. self::queryPrepare($sql, $data, $instance);
  34. } else {
  35. $con = Connections::getConnection($instance);
  36. $con->exec($sql);
  37. }
  38. }
  39. /*
  40. *
  41. * @ctag DBInstance::lastInsert();
  42. */
  43. public static function lastInsert($instance = 'default')
  44. {
  45. $con = Connections::getConnection($instance);
  46. return $con->lastInsertId();
  47. }
  48. /**
  49. * Query
  50. *
  51. * This method will execute a SQL and return a pointer to the resultset
  52. *
  53. * @ctag DBInstance::query($sql);
  54. * @ctag DBInstance::query($sql, []);
  55. *
  56. * @param String $sql The SQL to be executed
  57. * @param Array $data Pass the info to be replaced on the query_prepare
  58. * @param String $instance Select the connection to execute
  59. *
  60. * @return pointer A pointer for a foreach loop
  61. */
  62. public static function query($sql, $data = null, $instance = 'default')
  63. {
  64. $sql = self::addPrefix($sql, $instance);
  65. if ($data) {
  66. return self::queryPrepare($sql, $data, $instance);
  67. } else {
  68. $con = Connections::getConnection($instance);
  69. return $con->query($sql, \PDO::FETCH_OBJ);
  70. }
  71. }
  72. /**
  73. * queryOne
  74. *
  75. * @ctag DBInstance::queryOne($sql);
  76. * @ctag DBInstance::queryOne($sql, []);
  77. *
  78. * This method will execute a SQL and return the first featched register
  79. *
  80. * @param String $sql The SQL to be executed
  81. * @param Array $data Pass the info to be replaced on the query_prepare
  82. * @param String $instance Select the connection to execute
  83. *
  84. * @return stdClass A object with the record info
  85. */
  86. public static function queryOne($sql, $data = null, $instance = 'default')
  87. {
  88. $sql = self::addPrefix($sql, $instance);
  89. if ($data) {
  90. return self::queryPrepare($sql, $data, $instance);
  91. } else {
  92. $con = Connections::getConnection($instance);
  93. return $con->query($sql, \PDO::FETCH_OBJ)->fetch();
  94. }
  95. }
  96. /**
  97. * getRecord
  98. *
  99. * This method will execute get a register from a table
  100. *
  101. * @ctag DBInstance::getRecord($table);
  102. * @ctag DBInstance::getRecord($table, []);
  103. * @ctag DBInstance::getRecord($table, [], []);
  104. *
  105. * @param String $table The table to be searched
  106. * @param Array $data Pass the info to be replaced on the query_prepare
  107. * @param Array $select Info to be put on the SELECT part of the query
  108. * @param String $instance Select the connection to execute
  109. *
  110. * @return stdClass A object with the record info
  111. */
  112. public static function getRecord($table, $data = array(), $select = array('*'), $instance = 'default')
  113. {
  114. $select = implode(', ', $select);
  115. $conditions = "";
  116. $params = array();
  117. foreach ($data as $key => $cond) {
  118. if (is_array($cond)) {
  119. if (is_array($cond[1])) {
  120. $inner_conditions = "$key $cond[0] (";
  121. foreach ($cond[1] as $in_key => $c) {
  122. $inner_conditions .= ":" . $key . "_" . $in_key . ", ";
  123. $params[$key . "_" . $in_key] = $c;
  124. }
  125. $conditions .= rtrim($inner_conditions, ', ') . ") AND ";
  126. } else {
  127. $conditions .= "$key $cond[0] :$key AND ";
  128. $params[$key] = $cond[1];
  129. }
  130. } else {
  131. $conditions .= "$key = :$key AND ";
  132. $params[$key] = $cond;
  133. }
  134. }
  135. $conditions = rtrim($conditions, 'AND ');
  136. if ($conditions == "") {
  137. $conditions = "1 = 1";
  138. }
  139. $sql = "SELECT $select FROM {" . $table . "} WHERE $conditions LIMIT 1";
  140. return self::queryOne($sql, $params, $instance);
  141. }
  142. /**
  143. * getRecordSql
  144. *
  145. * This method will get a record from the database using SQL
  146. *
  147. * @ctag DBInstance::getRecordSql($sql);
  148. * @ctag DBInstance::getRecordSql($sql, []);
  149. *
  150. * @param String $sql The SQL to be executed
  151. * @param Array $data Pass the info to be replaced on the query_prepare
  152. * @param String $instance Select the connection to execute
  153. *
  154. * @return stdClass A object with the record info
  155. */
  156. public static function getRecordSql($sql, $data = null, $instance = 'default')
  157. {
  158. $sql = self::addPrefix($sql, $instance);
  159. if ($data) {
  160. $pointer = self::queryPrepare($sql, $data, $instance);
  161. } else {
  162. $con = Connections::getConnection($instance);
  163. return $con->query($sql, \PDO::FETCH_OBJ)->fetch();
  164. }
  165. }
  166. /**
  167. * getRecords
  168. *
  169. * This method will get records from the database
  170. *
  171. * @ctag DBInstance::getRecords($table);
  172. * @ctag DBInstance::getRecords($table, []);
  173. *
  174. * @param String $table The table to be searched
  175. * @param Array $data Pass the info to be replaced on the query_prepare
  176. * @param Array $select Info to be put on the SELECT part of the query
  177. * @param String $instance Select the connection to execute
  178. *
  179. * @return stdClass A object with the record info
  180. */
  181. public static function getRecords($table, $data = null, $select = array('*'), $limits = array(), $instance = 'default')
  182. {
  183. $pointer = self::getRecordsPointer($table, $data, $select, $limits, $instance);
  184. $elements = array();
  185. foreach ($pointer as $point) {
  186. $key = array_keys((array) $point)[0];
  187. $elements[((array) $point)[$key]] = $point;
  188. }
  189. return $elements;
  190. }
  191. /**
  192. * getRecordsPointer
  193. *
  194. * This method will get records from the database
  195. *
  196. * @ctag DBInstance::getRecordsPointer($table);
  197. * @ctag DBInstance::getRecordsPointer($table, []);
  198. *
  199. * @param String $table The table to be searched
  200. * @param Array $data Pass the info to be replaced on the query_prepare
  201. * @param Array $select Info to be put on the SELECT part of the query
  202. * @param String $instance Select the connection to execute
  203. *
  204. * @return stdClass A object with the record info
  205. */
  206. public static function getRecordsPointer($table, $data = array(), $select = array('*'), $limits = array(), $instance = 'default')
  207. {
  208. $select = implode(', ', $select);
  209. $conditions = "";
  210. $limits_sql = "";
  211. $params = array();
  212. foreach ($data as $key => $cond) {
  213. if (is_array($cond)) {
  214. if (is_array($cond[1])) {
  215. $inner_conditions = "$key $cond[0] (";
  216. foreach ($cond[1] as $in_key => $c) {
  217. $inner_conditions .= ":" . $key . "_" . $in_key . ", ";
  218. $params[$key . "_" . $in_key] = $c;
  219. }
  220. $conditions .= rtrim($inner_conditions, ', ') . ") AND ";
  221. } else {
  222. $conditions .= "$key $cond[0] :$key AND ";
  223. $params[$key] = $cond[1];
  224. }
  225. } else {
  226. $conditions .= "$key = :$key AND ";
  227. $params[$key] = $cond;
  228. }
  229. }
  230. foreach ($limits as $key => $value) {
  231. $limits_sql .= "$key $value ";
  232. }
  233. $conditions = rtrim($conditions, 'AND ');
  234. if ($conditions == "") {
  235. $conditions = "1 = 1";
  236. $params = false;
  237. }
  238. $sql = "SELECT $select FROM {" . $table . "} WHERE $conditions $limits_sql";
  239. return self::query($sql, $params, $instance);
  240. }
  241. /**
  242. * getRecordsSql
  243. *
  244. * This method will get records from the database
  245. *
  246. * @ctag DBInstance::getRecordsSql($sql);
  247. * @ctag DBInstance::getRecordsSql($sql, []);
  248. *
  249. * @param String $sql The SQL to be executed
  250. * @param Array $data Pass the info to be replaced on the query_prepare
  251. * @param String $instance Select the connection to execute
  252. *
  253. * @return Array Array with objects representing the database data
  254. */
  255. public static function getRecordsSql($sql, $data = null, $instance = 'default')
  256. {
  257. $sql = self::addPrefix($sql, $instance);
  258. $pointer = array();
  259. if ($data) {
  260. $pointer = self::queryPrepare($sql, $data, $instance);
  261. } else {
  262. $con = Connections::getConnection($instance);
  263. $pointer = $con->query($sql, \PDO::FETCH_OBJ);
  264. }
  265. $elements = array();
  266. foreach ($pointer as $point) {
  267. $key = array_keys((array) $point)[0];
  268. $elements[((array) $point)[$key]] = $point;
  269. }
  270. return $elements;
  271. }
  272. public static function insertRecord($table, $data = array(), $connection = 'default')
  273. {
  274. if (is_object($data)) {
  275. $data = (array)$data;
  276. }
  277. $columns = array_keys($data);
  278. $first_argument = '';
  279. $second_argument = '';
  280. $insert_data = array();
  281. foreach ($columns as $column) {
  282. $first_argument .= $column . ', ';
  283. $insert_data[$column] = $data[$column];
  284. $second_argument .= ":$column, ";
  285. }
  286. $first_argument = rtrim($first_argument, ', ');
  287. $second_argument = rtrim($second_argument, ', ');
  288. $sql = "INSERT INTO {" . $table . "} ($first_argument) VALUES ($second_argument)";
  289. DBInstance::execute($sql, $insert_data, $connection);
  290. }
  291. public static function queryPrepare($sql, $data = array(), $instance = 'default')
  292. {
  293. $sql = self::addPrefix($sql, $instance);
  294. $con = Connections::getConnection($instance);
  295. $statement = $con->prepare($sql);
  296. $statement->execute($data);
  297. return $statement->fetchAll(\PDO::FETCH_OBJ);
  298. }
  299. }