Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.46 KB | None | 0 0
  1. <?php
  2. # ----------------------------------------
  3. # Creating CRUD class
  4. # ----------------------------------------
  5. class Crud
  6. {
  7.   private $pdo         = null; # Storing PDO connection
  8.  private $table       = null; # Storing table name
  9.  private static $crud = null; # Static attribute that contains a self instance
  10.  
  11.   # ----------------------------------------
  12.  # Class constructor -> PUBLIC method
  13.  # ----------------------------------------
  14.  public function __construct($connection, $table=null)
  15.   {
  16.     if (!empty($connection))
  17.     {
  18.       $this->pdo = $connection;
  19.     }
  20.     else
  21.     {
  22.       echo 'Conexão inexistente!';
  23.       exit();
  24.     }
  25.  
  26.     if (!empty($table)) $this->table =$table;
  27.   }
  28.  
  29.   # ----------------------------------------
  30.  # Static public method that returns a Crud class instance
  31.  # ----------------------------------------
  32.  public static function getInstance($connection, $table=null)
  33.   {
  34.     # Verifying if there's a class instance
  35.    if (!isset(self::$crud))
  36.     {
  37.       try
  38.       {
  39.         self::$crud = new Crud($connection, $table);
  40.       }
  41.       catch (Exception $e)
  42.       {
  43.         echo 'Error '.$e->getMessage();
  44.       }
  45.     }
  46.     return self::$crud;
  47.   }
  48.  
  49.   # ----------------------------------------
  50.  # Setting table name on $table
  51.  # ----------------------------------------
  52.  public function setTableName($table)
  53.   {
  54.     if (!empty($table))
  55.     {
  56.       $this->table = $table;
  57.     }
  58.   }
  59.  
  60.   # ----------------------------------------
  61.  # SQL INSERT builder
  62.  # ----------------------------------------
  63.  private function buildInsert($arrayData)
  64.   {
  65.     # Variables
  66.    $sql    = '';
  67.     $fields = '';
  68.     $values = '';
  69.  
  70.     # Constructing instruction with loop
  71.    foreach ($arrayData as $key => $value)
  72.     {
  73.       $fields .= $key.', ';
  74.       $values .= '?, ';
  75.     }
  76.  
  77.     # Removes comma from the end of the string
  78.    $fields = (substr($fields, -2) == ', ') ? trim(substr($fields, 0, (strlen($fields) - 2))) : $fields;
  79.  
  80.     # Removes comma from the end of the string
  81.    $values = (substr($values, -2) == ', ') ? trim(substr($values, 0, (strlen($values) - 2))) : $values;
  82.  
  83.     # Concatenating variables for instruction
  84.    $sql .= "INSERT INTO {$this->table} (".$fields.")VALUES(".$values.")";
  85.  
  86.     # Returns string with SQL instruction
  87.    return trim($sql);
  88.   }
  89.  
  90.   # ----------------------------------------
  91.  # SQL UPDATE builder
  92.  # ----------------------------------------
  93.  private function buildUpdate($arrayData, $arrayCondition)
  94.   {
  95.     # Variables
  96.    $sql       = '';
  97.     $fields    = '';
  98.     $condition = '';
  99.  
  100.     # Constructing instruction with loop
  101.    foreach ($arrayData as $key => $value)
  102.     {
  103.       $fields .= $key.'=?, ';
  104.     }
  105.  
  106.     # Constructing WHERE condition with loop
  107.    foreach ($arrayCondition as $key => $value)
  108.     {
  109.       $condition .= $key.'? AND ';
  110.     }
  111.  
  112.     # Removes comma from the end of the string
  113.    $fields = (substr($fields, -2) == ', ') ? trim(substr($fields, 0, (strlen($fields) - 2))) : $fields;
  114.  
  115.     # Removes comma from the end of the string
  116.    $condition = (substr($condition, -4) == 'AND ') ? trim(substr($condition, 0, (strlen($condition) - 4))) : $condition;
  117.  
  118.     # Concatenating variables for instruction
  119.    $sql .= "UPDATE {$this->table} SET ".$fields." WHERE ".$condition;
  120.  
  121.     # Returns string with SQL instruction
  122.    return trim($sql);
  123.   }
  124.  
  125.   # ----------------------------------------
  126.  # SQL DELETE builder
  127.  # ----------------------------------------
  128.  private function buildDelete($arrayCondition)
  129.   {
  130.     # Variables
  131.    $sql    = '';
  132.     $fields = '';
  133.  
  134.     # Constructing instruction with loop
  135.    foreach ($arrayCondition as $key => $value)
  136.     {
  137.       $fields .= $key.'? AND ';
  138.     }
  139.  
  140.     # Removes AND from the end of the string
  141.    $fields = (substr($fields, -4) == 'AND ') ? trim(substr($fields, 0, (strlen($fields) - 4))) : $fields;
  142.  
  143.     # Concatenating variables for instruction
  144.    $sql .= "DELETE FROM {$this->table} WHERE ".$fields;
  145.  
  146.     # Returns string with SQL instruction
  147.    return trim($sql);
  148.   }
  149.  
  150.   # ----------------------------------------
  151.  # SQL INSERT function
  152.  # ----------------------------------------
  153.  public function insert($arrayData)
  154.   {
  155.     try
  156.     {
  157.       # Insert SQL instruction built from method
  158.      $sql = $this->buildInsert($arrayData);
  159.  
  160.       # Transfer instruction to PDO
  161.      $stm = $this->pdo->prepare($sql);
  162.  
  163.       # Loop for data as parameter
  164.      $count = 1;
  165.  
  166.       foreach ($arrayData as $value)
  167.       {
  168.         $stm->bindValue($count, $value);
  169.         $count++;
  170.       }
  171.  
  172.       # Execute SQL instruction and returns
  173.      $result = $stm->execute();
  174.       return $result;
  175.     }
  176.     catch (PDOException $e)
  177.     {
  178.       echo 'Error: '.$e->getMessage();
  179.     }
  180.   }
  181.  
  182.   # ----------------------------------------
  183.  # SQL UPDATE function
  184.  # ----------------------------------------
  185.  public function update($arrayData, $arrayCondition)
  186.   {
  187.     try
  188.     {
  189.       # Insert SQL instruction built from method
  190.      $sql = $this->buildUpdate($arrayData, $arrayCondition);
  191.  
  192.       # Transfer instruction to PDO
  193.      $stm = $this->pdo->prepare($sql);
  194.  
  195.       # Loop for data as parameter
  196.      $count = 1;
  197.       foreach ($arrayData as $value)
  198.       {
  199.         $stm->bindValue($count, $value);
  200.         $count++;
  201.       }
  202.  
  203.       # Loop for data as parameter for WHERE clause
  204.      foreach ($arrayCondition as $value)
  205.       {
  206.         $stm->bindValue($count, $value);
  207.         $count++;
  208.       };
  209.  
  210.       # Execute SQL instruction and returns
  211.      $result = $stm->execute();
  212.       return $result;
  213.     }
  214.     catch (PDOException $e)
  215.     {
  216.       echo 'Error: '.$e->getMessage();
  217.     }
  218.   }
  219.  
  220.   # ----------------------------------------
  221.  # SQL DELETE function
  222.  # ----------------------------------------
  223.  public function delete($arrayCondition)
  224.   {
  225.     try
  226.     {
  227.       # Insert SQL instruction built from method
  228.      $sql = $this->buildDelete($arrayCondition);
  229.  
  230.       # Transfer instruction to PDO
  231.      $stm = $this->pdo->prepare($sql);
  232.  
  233.       # Loop for data as parameter for WHERE clause
  234.      $count = 1;
  235.       foreach ($arrayCondition as $value)
  236.       {
  237.         $stm->bindValue($count, $value);
  238.         $count++;
  239.       }
  240.  
  241.       # Execute SQL instruction and returns
  242.      $result = $stm->execute();
  243.       return $result;
  244.     }
  245.     catch (PDOException $e)
  246.     {
  247.       echo 'Error: '.$e->getMessage();
  248.     }
  249.   }
  250.  
  251.   # ----------------------------------------
  252.  # SQL SELECT(generic) function
  253.  # ----------------------------------------
  254.  public function getSQLGeneric($sql, $arrayParams=null, $fetchAll=true)
  255.   {
  256.     try
  257.     {
  258.       # Transfer instruction to PDO
  259.      $stm = $this->pdo->prepare($sql);
  260.  
  261.       # Check if there's conditions to load parameters
  262.      if (!empty($arrayParams))
  263.       {
  264.         # Loop for data as parameter for WHERE clause
  265.        $count = 1;
  266.         foreach ($arrayParams as $value)
  267.         {
  268.           $stm->bindValue($count, $value);
  269.           $count++;
  270.         }
  271.       }
  272.  
  273.       # Execute SQL instruction
  274.      $stm->execute();
  275.  
  276.       # Verify if it's necessary to return more than one record
  277.      if ($fetchAll)
  278.       {
  279.         $data = $stm->fetchAll(PDO::FETCH_OBJ);
  280.       }
  281.       else
  282.       {
  283.         $data = $stm->fetch(PDO::FETCH_OBJ);
  284.       }
  285.  
  286.       # Return
  287.      return $data;
  288.     }
  289.     catch (PDOException $e)
  290.     {
  291.       echo "Error: ".$e->getMessage();
  292.     }
  293.   }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement