Advertisement
Guest User

ESTUDANDO CRUD

a guest
Mar 27th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.50 KB | None | 0 0
  1. <?php
  2.  
  3. class MinPDOException extends ErrorException {
  4.    
  5. }
  6.  
  7. class MinPDO {
  8.  
  9.     public static $sgbd = "mysql";
  10.     public static $dbhost = "localhost";
  11.     public static $dbname = "minpdo";
  12.     public static $dbuser = "root";
  13.     public static $dbpass = "";
  14.  
  15.     public static function connect() {
  16.         try {
  17.             $conn = new PDO(self::$sgbd . ":host=" . self::$dbhost . ";dbname=" . self::$dbname . ";charset=utf8;", self::$dbuser, self::$dbpass);
  18.             $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19.             $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  20.             return $conn;
  21.         } catch (PDOException $ex) {
  22.             throw new MinPDOException($ex->getMessage());
  23.         }
  24.     }
  25.  
  26.     public static function update($table, $columns, $values, $where = NULL) {
  27.         $sucess = false;
  28.         $sql = null;
  29.         if (is_array($columns) and is_array($values)) {
  30.             if ((count($columns)) == (count($values))) {
  31.                 //montar SQL
  32.                 $valuesTotal = count($columns); //conta quantos valores
  33.                 $expression = null;
  34.                 for ($i = 0; $i < $valuesTotal; $i++) {
  35.                     $expression = $expression . "{$columns[$i]}=:value{$i},";
  36.                 }
  37.  
  38.                 $expression = substr($expression, 0, -1); // remove a última virgula
  39.  
  40.                 $table = "UPDATE " . $table . " "; // vai montando minha sql
  41.                 $expression = " SET " . $expression . " "; // vai montando minha sql
  42.                 $where = self::minwhere($where);
  43.                 $sql = $table . $expression . $where; // monta sql (ate aqui tudo bem)
  44.                 $sucess = true;
  45.             } else {
  46.                 throw new MinPDOException("We must have the same number of columns and values.");
  47.             }
  48.         } else if (is_array($columns) and ! is_array($values)) {
  49.             throw new MinPDOException("'values' must be an array.");
  50.         } else if (!is_array($columns) and is_array($values)) {
  51.             throw new MinPDOException("'columns' must be an array.");
  52.         } else {
  53.             $table = "UPDATE {$table} ";
  54.             $coluna = "SET {$columns}";
  55.             $value = "  = :value0 ";
  56.             $where = self::minwhere($where);
  57.  
  58.             $sql = $table . $coluna . $value . $where;
  59.  
  60.             if ($conn = self::connect()) { // se conectar
  61.                 $stmt = $conn->prepare($sql); //prepara
  62.                 $stmt->bindParam(":value0", $values);
  63.  
  64.                 if ($result = $stmt->execute()) {
  65.                     return true;
  66.                 }
  67.  
  68.                 if (!$result) {
  69.                     throw new MinPDOException($stmt->errorInfo());
  70.                 }
  71.  
  72.                 $conn = null;
  73.                 return true;
  74.             } else {
  75.                 throw new MinPDOException("Unable to connect to database!<br>\n<i>Check the connection variables.");
  76.             }
  77.         }
  78.  
  79.         if ($sucess == true) {
  80.             if ($conn = self::connect()) { // se conectar
  81.                 $stmt = $conn->prepare($sql); //prepara
  82.                 self::bind($stmt, $valuesTotal, $values);
  83.  
  84.                 if ($result = $stmt->execute()) {
  85.                     return true;
  86.                 }
  87.                 if (!$result) {
  88.                     throw new MinPDOException($stmt->errorInfo());
  89.                 }
  90.  
  91.                 $conn = null;
  92.                 return true;
  93.             } else {
  94.                 throw new MinPDOException("Unable to connect to database! Check the connection variables.");
  95.             }
  96.         }
  97.     }
  98.  
  99.     public static function consult($table, $columns = "*", $where = NULL, $order = NULL, $limit = NULL, $like = NULL) {
  100.         //conexao feita
  101.         if ($table) {
  102.             $table = "FROM " . $table . " ";
  103.         } else {
  104.             throw new MinPDOException("No table has been indicated.");
  105.         }
  106.  
  107.         if ($columns) {
  108.             $columns = "SELECT " . $columns . " ";
  109.         } else {
  110.             $columns = "SELECT * ";
  111.         }
  112.  
  113.         $where = self::minwhere($where);
  114.  
  115.         if ($order) {
  116.             $c = substr($order, -1);
  117.             $order = substr($order, 0, -1);
  118.             if ($c == "+") {
  119.                 $order = "ORDER BY " . $order . " ASC ";
  120.             } else if ($c == "-") {
  121.                 $order = "ORDER BY " . $order . " DESC ";
  122.             } else {
  123.                 throw new MinPDOException("Tell +/- at the end of variable order!");
  124.             }
  125.         } else {
  126.             $order = NULL;
  127.         }
  128.  
  129.         if ($limit) {
  130.             if (is_numeric($limit)) {
  131.                 $limit = "LIMIT " . $limit . " ";
  132.             } else {
  133.                 throw new MinPDOException("Enter a numeric limit!");
  134.             }
  135.         } else {
  136.             $limit = NULL;
  137.         }
  138.  
  139.         if ($like) {
  140.             $like = " LIKE '" . $like . "' ";
  141.         } else {
  142.             $like = NULL;
  143.         }
  144.  
  145.         $sql = $columns . $table . $where . $like . $order . $limit;
  146.  
  147.         if ($conn = self::connect()) {
  148.             if ($result = $conn->query($sql)) {
  149.                 $rows = $result->fetchAll(PDO::FETCH_ASSOC);
  150.                 if (empty($rows)) {
  151.                     throw new MinPDOException("No results!");
  152.                 }
  153.                 $conn = null;
  154.                 return $rows;
  155.             } else {
  156.                 throw new MinPDOException("No results!");
  157.             }
  158.         } else {
  159.             throw new MinPDOException("Unable to connect to database!<br>\n<i>Check the connection variables.");
  160.         }
  161.     }
  162.  
  163.     public static function insert($table, $columns, $values) {
  164.         if (is_array($columns) and is_array($values)) {
  165.             if ((count($columns)) == (count($values))) {
  166.                 // montar SQL
  167.                 $valuesTotal = count($values);
  168.                 $value = null;
  169.                 for ($i = 0; $i < $valuesTotal; $i++) {
  170.                     $value = $value . ":value{$i},";
  171.                 }
  172.                 $value = substr($value, 0, -1); // remove a última virgula
  173.  
  174.                 $table = "INSERT INTO " . $table . " ";
  175.                 $column = "(" . implode(", ", $columns) . ")";
  176.                 $value = " VALUES(" . $value . ")";
  177.                 $sql = $table . $column . $value;
  178.                 $sucesso = true;
  179.             } else {
  180.                 throw new MinPDOException("We must have the same number of columns and values.");
  181.             }
  182.         } else if (is_array($columns) and ! is_array($values)) {
  183.             throw new MinPDOException("'values' must be an array.");
  184.         } else if (!is_array($columns) and is_array($values)) {
  185.             throw new MinPDOException("'columns' must be an array.");
  186.         } else {
  187.             $table = "INSERT INTO {$table} ";
  188.             $column = "({$columns})";
  189.             $value = " VALUES(:value0)";
  190.             $sql = $table . $column . $value;
  191.  
  192.             if ($conn = self::connect()) { // se conectar
  193.                 $stmt = $conn->prepare($sql); //prepara
  194.                 $stmt->bindParam(":value0", $values);
  195.  
  196.                 if ($result = $stmt->execute()) {
  197.                     return true;
  198.                 }
  199.                 if (!$result) {
  200.                     throw new MinPDOException($stmt->errorInfo());
  201.                 }
  202.                 $conn = null;
  203.                 return true;
  204.             } else {
  205.                 throw new MinPDOException("Unable to connect to database!<br>\n<i>Check the connection variables.");
  206.             }
  207.         }
  208.  
  209.         if ($sucesso == true) {
  210.             $sql = $table . $column . $value;
  211.             if ($conn = self::connect()) {
  212.                 $stmt = $conn->prepare($sql);
  213.                 self::bind($stmt, $valuesTotal, $values);
  214.                 if ($result = $stmt->execute()) {
  215.                     return true;
  216.                 }
  217.                 if (!$result) {
  218.                     throw new MinPDOException($stmt->errorInfo());
  219.                 }
  220.                 $conn = null;
  221.                 return true;
  222.             } else {
  223.                 throw new MinPDOException("Unable to connect to database!<br>\n<i>Check the connection variables.");
  224.             }
  225.         }
  226.     }
  227.  
  228.     public static function delete($table, $where = NULL) {
  229.         if ($table) {
  230.             $table = "DELETE FROM " . $table . " ";
  231.         } else {
  232.             throw new MinPDOException("No table has been indicated.");
  233.         }
  234.  
  235.         $where = self::minwhere($where);
  236.  
  237.         $sql = $table . $where;
  238.  
  239.         if ($conn = self::connect()) {
  240.             if ($result = $conn->query($sql)) {
  241.                 $stmt = $conn->prepare($sql);
  242.                 if ($result = $stmt->execute()) {
  243.                     return true;
  244.                 } else {
  245.                     throw new MinPDOException("Invalid query!");
  246.                 }
  247.  
  248.                 $conn = null;
  249.                 return true;
  250.             } else {
  251.                 throw new MinPDOException("Invalid query!");
  252.             }
  253.         } else {
  254.             throw new MinPDOException("Unable to connect to database!<br>\n<i>Check the connection variables.");
  255.         }
  256.     }
  257.  
  258.     private static function minwhere($where) {
  259.         if ($where) {
  260.             $newwhere = "";
  261.             if (stripos($where, "where") !== false) {
  262.                 if (!strstr($where, "'")) {
  263.                     $arr1 = preg_split("/([^\w]+\s*)/", $where, -1, PREG_SPLIT_DELIM_CAPTURE);
  264.                     for ($j = 0; $j < count($arr1); $j++) {
  265.                         if (stripos($arr1[$j], "=") !== false || stripos($arr1[$j], "!") !== false ||
  266.                                 stripos($arr1[$j], "<") !== false || stripos($arr1[$j], ">") !== false) {
  267.                             $arr1[$j + 1] = "'" . $arr1[$j + 1] . "'";
  268.                         }
  269.                         $newwhere .= " " . $arr1[$j] . " ";
  270.                     }
  271.                 } else
  272.                     $newwhere = $where;
  273.             }
  274.             else {
  275.                 $newwhere = "WHERE ";
  276.                 if (!strstr($where, "'")) {
  277.                     $arr1 = preg_split("/([^\w\.\,]+\s*)/", $where, -1, PREG_SPLIT_DELIM_CAPTURE);
  278.                     for ($j = 0; $j < count($arr1); $j++) {
  279.                         if (stripos($arr1[$j], "=") !== false || stripos($arr1[$j], "!") !== false ||
  280.                                 stripos($arr1[$j], "<") !== false || stripos($arr1[$j], ">") !== false) {
  281.                             $arr1[$j + 1] = "'" . $arr1[$j + 1] . "'";
  282.                         }
  283.                         $newwhere .= " " . $arr1[$j] . " ";
  284.                     }
  285.                 } else
  286.                     $newwhere = "WHERE " . $where;
  287.             }
  288.         } else
  289.             $newwhere = NULL;
  290.  
  291.         return $newwhere;
  292.     }
  293.  
  294.     private static function bind($stmt, $quantity, $values) {
  295.         for ($i = 0; $i < $quantity; $i ++) { //substitui o bind criado
  296.             if (is_string($values[$i]))
  297.                 $stmt->bindParam(":value{$i}", $values[$i], PDO::PARAM_STR);
  298.             else if (is_numeric($values[$i]))
  299.                 $stmt->bindParam(":value{$i}", $values[$i], PDO::PARAM_INT);
  300.             else if (is_bool($values[$i]))
  301.                 $stmt->bindParam(":value{$i}", $values[$i]);
  302.             else if (is_null($values[$i]))
  303.                 $stmt->bindParam(":value{$i}", $values[$i], PDO::PARAM_NULL);
  304.             else if (is_long($values[$i]))
  305.                 $stmt->bindParam(":value{$i}", $values[$i], PDO::PARAM_LOB);
  306.         }
  307.     }
  308.  
  309. }
  310. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement