Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class crud{
- static private $conn;
- static private $host = 'localhost';
- static private $dbname = 'imobiliariamonaco';
- static private $user = 'root';
- static private $pass = '';
- /**
- * Construct, connection with database
- *
- * @param string $host
- *
- * @param string $dbname
- *
- * @param string $user
- *
- * @param string $pass
- *
- * @return void
- *
- * @access public
- */
- public function __construct(string $host = NULL, string $dbname = NULL, string $user = NULL, string $pass = NULL){
- self::connection($host, $dbname, $user, $pass);
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Connection database
- *
- * @access protected
- */
- protected function connection(string $host = NULL, string $dbname = NULL, string $user = NULL, string $pass = NULL){
- try{
- self::$conn = new PDO('mysql:host=' . (!$host ? self::$host : $host) . ';dbname=' . (!$dbname ? self::$dbname : $dbname), (!$user ? self::$user : $user), (!$pass ? self::$pass : $pass));
- self::$conn->exec('SET CHARACTER SET utf8');
- }catch(PDOExecption $e){
- print '<h1>Erro ao conectar banco de dados</h1>';
- }
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Prepare to execute PDO
- *
- * @param string $text
- *
- * @param int $count
- *
- * @param string $separator
- *
- * @return bool
- *
- * @access private
- */
- private function placeholders(string $text, $count = 0, $separator = ','){
- $result = array();
- if($count > 0):
- for($x=0; $x<$count; $x++):
- $result[] = $text;
- endfor;
- endif;
- return implode($separator, $result);
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Execute custom sql
- *
- * @param string $sql
- *
- * @param string $prepare
- *
- * @param array $values
- *
- * @param string $type
- *
- * @return bool
- *
- * @access private
- */
- private function exec(string $sql, string $prepare = NULL, array $values = NULL, string $type = NULL){
- $returns = false;
- if($prepare):
- $insert = self::$conn->prepare($sql);
- endif;
- try{
- self::$conn->beginTransaction();
- if($prepare):
- if($values):
- if(in_array(true, array_map('is_array', $values), true)):
- sort($values);
- $insertValues = array();
- foreach($values as $d):
- if(in_array(true, array_map('is_array', $d), true)):
- foreach($d as $tri):
- $insertValues = array_merge($insertValues, array_values($tri));
- endforeach;
- else:
- $insertValues = array_merge($insertValues, array_values($d));
- endif;
- endforeach;
- endif;
- endif;
- $exec = $insert->execute(($values ? (in_array(true, array_map('is_array', $values), true) ? $insertValues : array_values($values)) : array_values($values)));
- $lastId = self::$conn->lastInsertId();
- else:
- $exec = self::$conn->query($sql);
- endif;
- if($exec):
- self::$conn->commit();
- $returns = ($type ? $lastId : true);
- else:
- self::$conn->rollback();
- $returns = false;
- endif;
- }catch(PDOExecption $e){
- self::$conn->rollback();
- $returns = false;
- }
- return $returns;
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Mount prepare PDO
- *
- * @param array $data
- *
- * @param bool $mult
- *
- * @return string
- *
- * @access private
- */
- private function mountPrepare($data, $mult = false){
- $valuePrepare = '';
- $valuePrepare .= ($mult ? '(' : '');
- for($i = 0; $i < count($data); $i++):
- $valuePrepare .= '?' . ($i != (count($data) - 1) || !$mult ? ',' : '');
- endfor;
- $valuePrepare .= ($mult ? '),' : '');
- return $valuePrepare;
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Create
- *
- * @param string $table
- *
- * @param array $data
- *
- * @param string $type
- *
- * @return bool or int depends on type
- *
- * @access public
- */
- public function create(string $table, array $data, string $type = NULL){
- $chunk = (count($data) > 200 ? array_chunk($data, 2, true) : $data);
- $i = 0; foreach($chunk as $teste):
- $multidimensional = in_array(true, array_map('is_array', $teste), true);
- $fields = implode(', ', array_keys(($multidimensional ? $teste[0] : $teste)));
- $fieldsTri = array();
- $valuePrepare = '';
- if($multidimensional):
- foreach($teste as $item):
- $tridimensional = in_array(true, array_map('is_array', $item), true);
- if($tridimensional):
- foreach($item as $tri):
- $fieldsTri[] = $tri;
- $valuePrepare .= self::mountPrepare($tri, true);
- endforeach;
- else:
- $valuePrepare .= self::mountPrepare($item, true);
- endif;
- endforeach;
- else:
- $valuePrepare .= self::mountPrepare($teste);
- endif;
- if(count($fieldsTri) >= 1):
- $fields = implode(', ', array_keys($fieldsTri));
- endif;
- return self::exec('INSERT INTO ' . $table . ' (' . $fields . ') VALUES ' . (!$multidimensional ? '(' : '') . substr($valuePrepare,0,-1) . (!$multidimensional ? ')' : ''), 'create', $teste, $type);
- $i++; endforeach;
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Read
- *
- * @param string $table
- *
- * @param string $cond
- *
- * @param string $inner
- *
- * @return array
- *
- * @access public
- */
- public function read(string $table, string $cond = NULL, string $fields = NULL, string $inner = NULL){
- $select = self::$conn->query('SELECT ' . (!$fields ? '*' : $fields) . ' FROM ' . $table . ($inner ? ' as imob' : '') . ' ' . $inner . ' ' . $cond);
- for($y = 0; $y < $select->columnCount(); $y++):
- $names[$y] = $select->getColumnMeta($y);
- endfor;
- $result = array();
- for($x = 0; $row = $select->fetch(PDO::FETCH_ASSOC); $x++):
- for($i = 0; $i < $select->columnCount(); $i++):
- $result[$x][$names[$i]['name']] = $row[$names[$i]['name']];
- endfor;
- endfor;
- return $result;
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Update
- *
- * @param string $table
- *
- * @param array $data
- *
- * @param string $where
- *
- * @return bool
- *
- * @access public
- */
- public function update(string $table, array $data, string $where){
- foreach($data as $fields => $values):
- $sets[] = '$fields = ' . $values;
- endforeach;
- return self::exec('UPDATE ' . $table . ' SET ' . implode(', ', $sets) . ' WHERE ' . $where);
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Delete
- *
- * @param string $table
- *
- * @param string $where
- *
- * @return bool
- *
- * @access public
- */
- public function delete(string $table, string $where){
- return self::exec('DELETE FROM ' . $table . ' WHERE ' . $where);
- }
- /*------------------------------------------------------------------------------*/
- /**
- * Execute function of custom sql by type
- *
- * @param string $table
- *
- * @param string $type (drop, create_table)
- *
- * @param string $fields
- *
- * @return bool
- *
- * @access public
- */
- public function customSql(string $table, string $type, string $fields = NULL){
- if($type == 'drop'):
- $sql = 'DROP TABLE IF EXISTS ' . $table;
- elseif($type == 'create_table'):
- $sql = 'CREATE TABLE IF NOT EXISTS ' . $table . ' (' . $fields . ')';
- endif;
- return self::exec($sql, ($type == 'drop' ? 'prepare' : ''));
- }
- }
- ?>
Add Comment
Please, Sign In to add comment