Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database extends PDO
- {
- private $query;
- private $erro;
- private $linhas;
- private $colunas;
- public function __construct($options = null, $lc = false)
- {
- $hostname = Config::get('mysql-hostname');
- $username = Config::get('mysql-username');
- $password = Config::get('mysql-password');
- $database = Config::get('mysql-database');
- try{
- $dsn = 'mysql:host=' . $hostname .
- ';dbname=' . $database .
- ';charset=' . Config::get('mysql-charset');
- parent::__construct($dsn, $username, $password, $options);
- if(empty($lc)){ $lc = 'pt_Pt'; }
- parent::exec("SET lc_time_names = '{$lc}'");
- parent::exec("SET lc_messages = '{$lc}'");
- } catch(PDOException $e){
- $this->erro = $e->getMessage();
- #die($e->getMessage());
- log_write('Database #Construct', $e->getMessage());
- }
- }
- public function query($stmt, $parameters = array()){
- try{
- if($this->query = parent::prepare($stmt)){
- if(isset($parameters) && !empty($parameters)){
- foreach($parameters as $key=>&$valor){
- $this->query->bindParam(":{$key}", $valor);
- }
- }
- $this->query->execute();
- }
- } catch(PDOException $e){
- $this->erro = $e->getMessage();
- #die($e->getMessage());
- log_write('Database #Query', $e->getMessage());
- } catch(Exception $ex)
- {
- $this->erro = $ex->getMessage();
- #die($e->getMessage());
- log_write('Database #Query', $e->getMessage());
- }
- return $this;
- }
- public function action($method, $table, $arguments = array())
- {
- if(!empty($method) && !empty($table) && !empty($arguments)){
- $field = $arguments[0];
- $value = strtoupper($arguments[1]) === 'LIKE' ? array($field=>"%{$arguments[2]}%") : array($field=>$arguments[2]);
- $operator = $arguments[1];
- $order = isset($arguments[3]) ? $this->order($method, $arguments[3]) : $this->order($method);
- $limit = isset($arguments[4]) && is_numeric($arguments[4]) ? 'LIMIT ' . (int) $arguments[4] : '';
- $marker = strtoupper($operator) === 'IN' ? '(' . $this->parameters($value, 'param', true) .')' : $this->parameters(array($field=>$value), 'param', true);
- $stmt = "{$method} FROM {$table} WHERE {$field} {$operator} {$marker} {$order} {$limit}";
- return $this->query($stmt, $value);
- }
- return false;
- }
- public function select($table, $fields = array())
- {
- if($this->action('SELECT *', $table, $fields)){
- return $this;
- }
- return false;
- }
- public function delete($table, $fields = array())
- {
- if($this->action('DELETE', $table, $fields)){
- return $this;
- }
- return false;
- }
- public function selectAll($table)
- {
- if(!empty($table)){
- $order = $limit = '';
- if(isset(func_get_args()[1])){
- switch(func_get_args()[1]){
- case is_numeric(func_get_args()[1]):
- $limit = 'LIMIT ' . func_get_args()[1];
- break;
- default:
- $order = 'ORDER BY ' . func_get_args()[1];
- }
- }
- if(isset(func_get_args()[2])){
- switch(func_get_args()[2]){
- case is_numeric(func_get_args()[2]):
- $limit = 'LIMIT ' . func_get_args()[2];
- break;
- default:
- $order = 'ORDER BY ' . func_get_args()[2];
- }
- }
- if($this->query("SELECT * FROM {$table} {$order} {$limit}")){
- return $this;
- }
- }
- return false;
- }
- public function insert($table, $fields = array())
- {
- if(!empty($table)){
- if(!empty($fields) && is_array($fields)){
- $values = $this->parameters($fields, 'param', true);
- $columns = $this->parameters($fields, 'coluna', true);
- $stmt = "INSERT INTO {$table} ({$columns}) VALUES ({$values})";
- return $this->query($stmt, $fields);
- }
- }
- return false;
- }
- public function update($table, $id, $fields, $ref = 'id')
- {
- if(!empty($table) && !empty($id)){
- if(!empty($fields) && is_array($fields)){
- $set = $this->parameters($fields, 'update', true);
- $equals = $this->parameters([$ref=>$id], 'update', true);
- $fields['id'] = (int)$id;
- $stmt = "UPDATE {$table} SET {$set} WHERE ({$equals})";
- return $this->query($stmt, $fields);
- }
- }
- return false;
- }
- public function fetch($mode = null)
- {
- if($this->query){
- return $this->query->fetchAll(PDO::FETCH_OBJ);
- }
- return false;
- }
- public function first($mode = null)
- {
- if($this->query){
- return $this->query->fetchAll(PDO::FETCH_OBJ)[0];
- }
- return false;
- }
- public function erro()
- {
- if(!empty($this->erro)){
- return $this->erro;
- }
- return false;
- }
- protected function order($arguments, $set = null)
- {
- if(strtoupper($arguments) === 'SELECT' || 'SELECT *'){
- if(isset($set) && !is_numeric($set)){
- if(!empty($set)){
- $order = 'ORDER BY {$set} ASC';
- } else {
- $order = 'ORDER BY {$set} DESC';
- }
- } else {
- $order = '';
- }
- return $order;
- }
- return false;
- }
- public function rows()
- {
- return $this->query->rowCount();
- }
- protected function parameters($arguments = array(), $type = null, $named = false )
- {
- $column = isset($arguments['colunas']) ? $arguments['colunas'] : array_keys($arguments);
- $i=1;
- $parameters = '';
- foreach($arguments as $key=>$argument){
- if(!empty($type) && $type === 'update'){
- if($named){
- $parameters .= "{$key} = :{$key}";
- } else {
- $parameters .= "{$key} = ?";
- }
- } else {
- if($named){
- $parameters .= ":{$key}";
- } else {
- $parameters .= "?";
- }
- }
- if($i < count($arguments)){
- $parameters .= ', ';
- }
- $i++;
- }
- if(!empty($type)){
- switch($type):
- case 'coluna':
- $return = implode(', ', $column);
- break;
- case 'param':
- $return = $parameters;
- break;
- case 'update':
- $return = $parameters;
- break;
- default:
- $return = ['colunas'=>implode(', ', $column), 'valores'=>$parameters];
- break;
- endswitch;
- return $return;
- }
- return ['colunas'=>implode(', ', $column), 'valores'=>$parameters];
- }
- public function __destruct()
- {
- $this->query = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement