Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. <?php
  2.  
  3. class Database extends PDO
  4. {
  5. private $query;
  6. private $erro;
  7. private $linhas;
  8. private $colunas;
  9.  
  10. public function __construct($options = null, $lc = false)
  11. {
  12. $hostname = Config::get('mysql-hostname');
  13. $username = Config::get('mysql-username');
  14. $password = Config::get('mysql-password');
  15. $database = Config::get('mysql-database');
  16. try{
  17. $dsn = 'mysql:host=' . $hostname .
  18. ';dbname=' . $database .
  19. ';charset=' . Config::get('mysql-charset');
  20. parent::__construct($dsn, $username, $password, $options);
  21. if(empty($lc)){ $lc = 'pt_Pt'; }
  22. parent::exec("SET lc_time_names = '{$lc}'");
  23. parent::exec("SET lc_messages = '{$lc}'");
  24. } catch(PDOException $e){
  25. $this->erro = $e->getMessage();
  26. #die($e->getMessage());
  27. log_write('Database #Construct', $e->getMessage());
  28. }
  29. }
  30.  
  31. public function query($stmt, $parameters = array()){
  32. try{
  33. if($this->query = parent::prepare($stmt)){
  34. if(isset($parameters) && !empty($parameters)){
  35. foreach($parameters as $key=>&$valor){
  36. $this->query->bindParam(":{$key}", $valor);
  37. }
  38. }
  39. $this->query->execute();
  40. }
  41. } catch(PDOException $e){
  42. $this->erro = $e->getMessage();
  43. #die($e->getMessage());
  44. log_write('Database #Query', $e->getMessage());
  45. } catch(Exception $ex)
  46. {
  47. $this->erro = $ex->getMessage();
  48. #die($e->getMessage());
  49. log_write('Database #Query', $e->getMessage());
  50. }
  51. return $this;
  52. }
  53.  
  54. public function action($method, $table, $arguments = array())
  55. {
  56. if(!empty($method) && !empty($table) && !empty($arguments)){
  57. $field = $arguments[0];
  58. $value = strtoupper($arguments[1]) === 'LIKE' ? array($field=>"%{$arguments[2]}%") : array($field=>$arguments[2]);
  59. $operator = $arguments[1];
  60. $order = isset($arguments[3]) ? $this->order($method, $arguments[3]) : $this->order($method);
  61. $limit = isset($arguments[4]) && is_numeric($arguments[4]) ? 'LIMIT ' . (int) $arguments[4] : '';
  62. $marker = strtoupper($operator) === 'IN' ? '(' . $this->parameters($value, 'param', true) .')' : $this->parameters(array($field=>$value), 'param', true);
  63. $stmt = "{$method} FROM {$table} WHERE {$field} {$operator} {$marker} {$order} {$limit}";
  64. return $this->query($stmt, $value);
  65. }
  66. return false;
  67. }
  68.  
  69. public function select($table, $fields = array())
  70. {
  71. if($this->action('SELECT *', $table, $fields)){
  72. return $this;
  73. }
  74. return false;
  75. }
  76.  
  77. public function delete($table, $fields = array())
  78. {
  79. if($this->action('DELETE', $table, $fields)){
  80. return $this;
  81. }
  82. return false;
  83. }
  84.  
  85. public function selectAll($table)
  86. {
  87. if(!empty($table)){
  88. $order = $limit = '';
  89. if(isset(func_get_args()[1])){
  90. switch(func_get_args()[1]){
  91. case is_numeric(func_get_args()[1]):
  92. $limit = 'LIMIT ' . func_get_args()[1];
  93. break;
  94. default:
  95. $order = 'ORDER BY ' . func_get_args()[1];
  96. }
  97. }
  98. if(isset(func_get_args()[2])){
  99. switch(func_get_args()[2]){
  100. case is_numeric(func_get_args()[2]):
  101. $limit = 'LIMIT ' . func_get_args()[2];
  102. break;
  103. default:
  104. $order = 'ORDER BY ' . func_get_args()[2];
  105. }
  106. }
  107.  
  108. if($this->query("SELECT * FROM {$table} {$order} {$limit}")){
  109. return $this;
  110. }
  111. }
  112. return false;
  113. }
  114.  
  115. public function insert($table, $fields = array())
  116. {
  117. if(!empty($table)){
  118. if(!empty($fields) && is_array($fields)){
  119. $values = $this->parameters($fields, 'param', true);
  120. $columns = $this->parameters($fields, 'coluna', true);
  121. $stmt = "INSERT INTO {$table} ({$columns}) VALUES ({$values})";
  122. return $this->query($stmt, $fields);
  123. }
  124. }
  125. return false;
  126. }
  127.  
  128. public function update($table, $id, $fields, $ref = 'id')
  129. {
  130. if(!empty($table) && !empty($id)){
  131. if(!empty($fields) && is_array($fields)){
  132. $set = $this->parameters($fields, 'update', true);
  133. $equals = $this->parameters([$ref=>$id], 'update', true);
  134. $fields['id'] = (int)$id;
  135. $stmt = "UPDATE {$table} SET {$set} WHERE ({$equals})";
  136. return $this->query($stmt, $fields);
  137. }
  138. }
  139. return false;
  140. }
  141.  
  142. public function fetch($mode = null)
  143. {
  144. if($this->query){
  145. return $this->query->fetchAll(PDO::FETCH_OBJ);
  146. }
  147. return false;
  148. }
  149.  
  150. public function first($mode = null)
  151. {
  152. if($this->query){
  153. return $this->query->fetchAll(PDO::FETCH_OBJ)[0];
  154. }
  155. return false;
  156. }
  157.  
  158. public function erro()
  159. {
  160. if(!empty($this->erro)){
  161. return $this->erro;
  162. }
  163. return false;
  164. }
  165.  
  166. protected function order($arguments, $set = null)
  167. {
  168. if(strtoupper($arguments) === 'SELECT' || 'SELECT *'){
  169. if(isset($set) && !is_numeric($set)){
  170. if(!empty($set)){
  171. $order = 'ORDER BY {$set} ASC';
  172. } else {
  173. $order = 'ORDER BY {$set} DESC';
  174. }
  175. } else {
  176. $order = '';
  177. }
  178. return $order;
  179. }
  180. return false;
  181. }
  182.  
  183. public function rows()
  184. {
  185. return $this->query->rowCount();
  186. }
  187.  
  188. protected function parameters($arguments = array(), $type = null, $named = false )
  189. {
  190. $column = isset($arguments['colunas']) ? $arguments['colunas'] : array_keys($arguments);
  191. $i=1;
  192. $parameters = '';
  193. foreach($arguments as $key=>$argument){
  194. if(!empty($type) && $type === 'update'){
  195. if($named){
  196. $parameters .= "{$key} = :{$key}";
  197. } else {
  198. $parameters .= "{$key} = ?";
  199. }
  200. } else {
  201. if($named){
  202. $parameters .= ":{$key}";
  203. } else {
  204. $parameters .= "?";
  205. }
  206. }
  207. if($i < count($arguments)){
  208. $parameters .= ', ';
  209. }
  210. $i++;
  211. }
  212. if(!empty($type)){
  213. switch($type):
  214. case 'coluna':
  215. $return = implode(', ', $column);
  216. break;
  217. case 'param':
  218. $return = $parameters;
  219. break;
  220. case 'update':
  221. $return = $parameters;
  222. break;
  223. default:
  224. $return = ['colunas'=>implode(', ', $column), 'valores'=>$parameters];
  225. break;
  226. endswitch;
  227. return $return;
  228. }
  229. return ['colunas'=>implode(', ', $column), 'valores'=>$parameters];
  230. }
  231.  
  232. public function __destruct()
  233. {
  234. $this->query = null;
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement