Guest User

Untitled

a guest
Oct 24th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.34 KB | None | 0 0
  1. <?php
  2. /************\
  3. | Class: Sql |
  4. \************/
  5.  
  6. class Sql
  7. {
  8.     // Variables
  9.     public $handle = '';
  10.     public $type = '';
  11.     public $prefix = '';
  12.     public $database = '';
  13.    
  14.     // Constructor
  15.     //  Usage: $OBJECT_VAR = new Sql(TYPE, USERNAME, PASSWORD, SERVER, PREFIX[, DATABASE]);
  16.     public function __construct($sql_type = "mysql", $sql_user = "root", $sql_pass = "", $sql_src = "localhost", $sql_prefix = "", $sql_base = null)
  17.     {
  18.         $this->type = $sql_type;
  19.         $this->prefix = $sql_pref;
  20.         $this->database = $sql_base;
  21.        
  22.         if ($sql_type == "mysql")
  23.             $this->handle = mysql_connect($sql_host, $sql_user, $sql_pass);
  24.         elseif ($sql_type == "pgsql")
  25.             $this->handle = pg_connect("host=$sql_host user=$sql_user password=$sql_pass");
  26.         elseif ($sql_type == "sqlite")
  27.             $this->handle = sqlite_open($sql_base, 0666, $error);
  28.     }
  29.    
  30.     // Query
  31.     //  Usage: $OBJECT_VAR->query(QUERY);
  32.     public function query($query)
  33.     {
  34.         $this->focus($this->database);
  35.         if ($this->type == "mysql")
  36.             return mysql_query($query, $this->handle);
  37.         elseif ($this->type == "pgsql")
  38.             return pg_query($this->handle, $query);
  39.         elseif ($this->type == "sqlite")
  40.             return sqlite_query($this->handle, $query);
  41.     }
  42.    
  43.     // Query (alias)
  44.     //  Usage: $OBJECT_VAR->q(QUERY);
  45.     public function q($query)
  46.     {
  47.         return $this->query($query);
  48.     }
  49.    
  50.     // SQL Select Database
  51.     //  Usage: $OBJECT_VAR->focus(DATABASE_NAME);
  52.     public function focus($database)
  53.     {
  54.         if ($this->type == "mysql")
  55.             return mysql_select_db($database, $this->handle);
  56.         elseif ($this->type == "sqlite")
  57.             return true;
  58.         else
  59.             return mysql_query("USE `$database`", $this->handle);
  60.     }
  61.    
  62.     // SQL Result Resource to Array
  63.     //  Usage: $OBJECT_VAR->r2Array(RESOURCE);
  64.     public function r2Array($result)
  65.     {
  66.         if ($this->type == "mysql")
  67.             while ($array[] = mysql_fetch_assoc($result));
  68.         if ($this->type == "pgsql")
  69.             while ($array[] = pg_fetch_assoc($result));
  70.         elseif ($this->type == "sqlite")
  71.             return sqlite_fetch_array($result, SQLITE_ASSOC);
  72.         array_pop($array);
  73.         return $array;
  74.     }
  75.    
  76.     // SQL Table to Array
  77.     //  Usage: $OBJECT_VAR->t2Array(TABLE);
  78.     public function t2Array($table)
  79.     {
  80.         $result = query("SELECT * FROM `$table`");
  81.         $array = r2array($result);
  82.         return $array;
  83.     }
  84.    
  85.     // SQL Number of Rows
  86.     //  Usage: $OBJECT_VAR->num_rows(RESOURCE);
  87.     public function num_rows($resource)
  88.     {
  89.         if ($this->type == "mysql")
  90.             return mysql_num_rows($resource);
  91.         if ($this->type == "pgsql")
  92.             return pg_num_rows($resource);
  93.         if ($this->type == "sqlite")
  94.             return sqlite_num_rows($resource);
  95.     }
  96.    
  97.     // SQL Escape String
  98.     //  Usage: $OBJECT_VAR->es(STRING);
  99.     public function es($string)
  100.     {
  101.         if ($this->type == "mysql")
  102.             return mysql_real_escape_string($string);
  103.         elseif ($this->type == "pgsql")
  104.             return pg_escape_string($string);
  105.         else
  106.             return addslashes($string);
  107.     }
  108.    
  109.     // SQL Close
  110.     //  Usage: $OBJECT_VAR->close(RESOURCE);
  111.     public function close($resource = null)
  112.     {
  113.         if ($this->type == "mysql")
  114.             return mysql_close($resource);
  115.         elseif ($this->type == "pgsql")
  116.             return pg_close($resource);
  117.         elseif ($this->type == "sqlite")
  118.             return sqlite_close($resource);
  119.     }
  120.    
  121.     // SQL Error
  122.     //  Usage: $OBJECT_VAR->error();
  123.     public function error()
  124.     {
  125.         if ($this->type == "mysql")
  126.             return mysql_error($this->handle);
  127.         elseif ($this->type == "pgsql")
  128.             return pg_result_error($this->handle);
  129.         elseif ($this->type == "sqlite")
  130.             return sqlite_error_string(sqlite_last_error($this->handle));
  131.     }
  132.    
  133.     /*
  134.     ####################
  135.     # ADVANCED METHODS #
  136.     ####################
  137.     */
  138.     // Do A Search Adaptation
  139.     //  Usage: $this->doSearchAdaptation(ORIGINAL_FILTER, CONTENT, SEARCH_ADAPTATIONS_EVAL_ARRAY);
  140.     public function doSearchAdaptation($filter, $content, $adaptations_array)
  141.     {
  142.         if ($adaptations_array[$filter])
  143.         {
  144.             $to_eval = str_replace("%%%%", var_export($content, true), $adaptations_array[$filter]);
  145.             eval('$content = '.$to_eval);
  146.         }
  147.         return $content;
  148.     }
  149.    
  150.     // Build Search Query
  151.     //  Usage: $this->buildSearch(ARRAY_SEARCH, ARRAY_SEARCHASSOCIATIONS, ARRAY_SEARCHADAPTATIONS[, TABLE]);
  152.     public function buildSearch($search = null, $search_associations = null, $search_adaptations = null, $search_table = null)
  153.     {
  154.         if ($search_table)
  155.             $query = "SELECT * FROM `".$this->prefix.$search_table."`";
  156.         $continue_building = true;
  157.         is_array($search) ? $query .= " WHERE " : $continue_building = false;
  158.         $i = 1;
  159.         if ($continue_building) foreach ($search as $filter => $data)
  160.         {
  161.             $FILTER = $filter;
  162.             if ($search_associations[$filter]) $FILTER = $search_associations[$filter];
  163.             $OPERATOR = "=";
  164.             if (is_array($data))
  165.             {
  166.                 if ($data["BETWEEN"])
  167.                 {
  168.                     $query .= "`$FILTER` BETWEEN ".$this->es($this->doSearchAdaptation($filter, $data["BETWEEN"][0], $search_adaptations))." AND ".$this->es($this->doSearchAdaptation($filter, $data["BETWEEN"][1], $search_adaptations));
  169.                 }
  170.                 else
  171.                 {
  172.                     if ($data["OPERATOR"])
  173.                     {
  174.                         $OPERATOR = $data["OPERATOR"];
  175.                     }
  176.                     if (is_numeric($data["VALUE"]))
  177.                     {
  178.                         $query .= "`$FILTER` $OPERATOR ".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations));
  179.                     }
  180.                     else
  181.                     {
  182.                         $query .= "`$FILTER` $OPERATOR '".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations))."'";
  183.                     }
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 $query .= "`$FILTER` $OPERATOR '".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations))."'";
  189.             }
  190.             $theres_more = false;
  191.             $reached_now = false;
  192.             if ($i < count($search))
  193.             {
  194.                 $query .= " AND ";
  195.             }
  196.             $i ++;
  197.         }
  198.         return $query;
  199.     }
  200.    
  201.     // Build Insert Query
  202.     //  Usage: $this->buildInsert(ARRAY_INSERTDATA, ARRAY_INSERTASSOCIATIONS, ARRAY_INSERTADAPTATIONS[, TABLE]);
  203.     public function buildInsert($insert_data = null, $insert_associations = null, $insert_adaptations = null, $insert_table = null)
  204.     {
  205.         if ($insert_table)
  206.             $query = "INSERT INTO `".$this->prefix.$insert_table."` ";
  207.         if (is_array($insert_data)) $query .= "("; else return false;
  208.         $values = " VALUES (";
  209.         $i = 1;
  210.         foreach ($insert_data as $filter => $data)
  211.         {
  212.             $FILTER = $filter;
  213.             if ($insert_associations[$filter]) $FILTER = $insert_associations[$filter];# else $FILTER = false;
  214.             if ($FILTER)
  215.             {
  216.                 if (is_numeric($insert_data[$filter]))
  217.                 {
  218.                     $query .= "`$FILTER`";
  219.                     $values .= $this->es($this->doSearchAdaptation($filter, $data, $insert_adaptations));
  220.                 }
  221.                 else
  222.                 {
  223.                     $query .= "`$FILTER`";
  224.                     $values .= "'".$this->es($this->doSearchAdaptation($filter, $data, $insert_adaptations))."'";
  225.                 }
  226.                 $theres_more = false;
  227.                 $reached_now = false;
  228.                 foreach ($insert_data as $checker => $temp)
  229.                 {
  230.                     if ($reached_now && $insert_associations[$checker]) $theres_more = true;
  231.                     if ($checker == $filter) $reached_now = true;
  232.                 }
  233.                 if ($i < count($insert_data))#$theres_more)
  234.                 {
  235.                     $query .= ", ";
  236.                     $values .= ", ";
  237.                 }
  238.             }
  239.             $i ++;
  240.         }
  241.         $query .= ")"; $values .= ")";
  242.         $query .= $values;
  243.        
  244.         return $query;
  245.     }
  246.    
  247.     // Build Update Query
  248.     //  Usage: $this->buildUpdate(ARRAY_UPDATE, ARRAY_SEARCH, ARRAY_SEARCHASSOCIATIONS, ARRAY_SEARCHADAPTATIONS[, TABLE]);
  249.     public function buildUpdate($update = null, $search = null, $search_associations = null, $search_adaptations = null, $search_table = null)
  250.     {
  251.         if ($search_table)
  252.             $query = "UPDATE `".$this->prefix.$search_table."`";
  253.         $continue_building = true;
  254.         if (is_array($update)) $query .= " SET "; else return false;
  255.         $i = 1;
  256.         foreach ($update as $filter => $data)
  257.         {
  258.             $FILTER = $filter;
  259.             if ($search_associations[$filter]) $FILTER = $search_associations[$filter];
  260.             $OPERATOR = "=";
  261.             if (is_numeric($search[$filter]))
  262.             {
  263.                 $query .= "`$FILTER` = ".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations));
  264.             }
  265.             else
  266.             {
  267.                 $query .= "`$FILTER` = '".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations))."'";
  268.             }
  269.             $theres_more = false;
  270.             $reached_now = false;
  271.             foreach ($update as $checker => $temp)
  272.             {
  273.                 if ($reached_now && $search_associations[$checker]) $theres_more = true;
  274.                 if ($checker == $filter) $reached_now = true;
  275.             }
  276.             if ($i < count($update))#$theres_more)
  277.             {
  278.                 $query .= ", ";
  279.             }
  280.             $i ++;
  281.         }
  282.         $query .= $this->buildSearch($search, $search_associations, $search_adaptations);
  283.        
  284.         return $query;
  285.     }
  286. }
  287.  
  288. ?>
Add Comment
Please, Sign In to add comment