Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /************\
- | Class: Sql |
- \************/
- class Sql
- {
- // Variables
- public $handle = '';
- public $type = '';
- public $prefix = '';
- public $database = '';
- // Constructor
- // Usage: $OBJECT_VAR = new Sql(TYPE, USERNAME, PASSWORD, SERVER, PREFIX[, DATABASE]);
- public function __construct($sql_type = "mysql", $sql_user = "root", $sql_pass = "", $sql_src = "localhost", $sql_prefix = "", $sql_base = null)
- {
- $this->type = $sql_type;
- $this->prefix = $sql_pref;
- $this->database = $sql_base;
- if ($sql_type == "mysql")
- $this->handle = mysql_connect($sql_host, $sql_user, $sql_pass);
- elseif ($sql_type == "pgsql")
- $this->handle = pg_connect("host=$sql_host user=$sql_user password=$sql_pass");
- elseif ($sql_type == "sqlite")
- $this->handle = sqlite_open($sql_base, 0666, $error);
- }
- // Query
- // Usage: $OBJECT_VAR->query(QUERY);
- public function query($query)
- {
- $this->focus($this->database);
- if ($this->type == "mysql")
- return mysql_query($query, $this->handle);
- elseif ($this->type == "pgsql")
- return pg_query($this->handle, $query);
- elseif ($this->type == "sqlite")
- return sqlite_query($this->handle, $query);
- }
- // Query (alias)
- // Usage: $OBJECT_VAR->q(QUERY);
- public function q($query)
- {
- return $this->query($query);
- }
- // SQL Select Database
- // Usage: $OBJECT_VAR->focus(DATABASE_NAME);
- public function focus($database)
- {
- if ($this->type == "mysql")
- return mysql_select_db($database, $this->handle);
- elseif ($this->type == "sqlite")
- return true;
- else
- return mysql_query("USE `$database`", $this->handle);
- }
- // SQL Result Resource to Array
- // Usage: $OBJECT_VAR->r2Array(RESOURCE);
- public function r2Array($result)
- {
- if ($this->type == "mysql")
- while ($array[] = mysql_fetch_assoc($result));
- if ($this->type == "pgsql")
- while ($array[] = pg_fetch_assoc($result));
- elseif ($this->type == "sqlite")
- return sqlite_fetch_array($result, SQLITE_ASSOC);
- array_pop($array);
- return $array;
- }
- // SQL Table to Array
- // Usage: $OBJECT_VAR->t2Array(TABLE);
- public function t2Array($table)
- {
- $result = query("SELECT * FROM `$table`");
- $array = r2array($result);
- return $array;
- }
- // SQL Number of Rows
- // Usage: $OBJECT_VAR->num_rows(RESOURCE);
- public function num_rows($resource)
- {
- if ($this->type == "mysql")
- return mysql_num_rows($resource);
- if ($this->type == "pgsql")
- return pg_num_rows($resource);
- if ($this->type == "sqlite")
- return sqlite_num_rows($resource);
- }
- // SQL Escape String
- // Usage: $OBJECT_VAR->es(STRING);
- public function es($string)
- {
- if ($this->type == "mysql")
- return mysql_real_escape_string($string);
- elseif ($this->type == "pgsql")
- return pg_escape_string($string);
- else
- return addslashes($string);
- }
- // SQL Close
- // Usage: $OBJECT_VAR->close(RESOURCE);
- public function close($resource = null)
- {
- if ($this->type == "mysql")
- return mysql_close($resource);
- elseif ($this->type == "pgsql")
- return pg_close($resource);
- elseif ($this->type == "sqlite")
- return sqlite_close($resource);
- }
- // SQL Error
- // Usage: $OBJECT_VAR->error();
- public function error()
- {
- if ($this->type == "mysql")
- return mysql_error($this->handle);
- elseif ($this->type == "pgsql")
- return pg_result_error($this->handle);
- elseif ($this->type == "sqlite")
- return sqlite_error_string(sqlite_last_error($this->handle));
- }
- /*
- ####################
- # ADVANCED METHODS #
- ####################
- */
- // Do A Search Adaptation
- // Usage: $this->doSearchAdaptation(ORIGINAL_FILTER, CONTENT, SEARCH_ADAPTATIONS_EVAL_ARRAY);
- public function doSearchAdaptation($filter, $content, $adaptations_array)
- {
- if ($adaptations_array[$filter])
- {
- $to_eval = str_replace("%%%%", var_export($content, true), $adaptations_array[$filter]);
- eval('$content = '.$to_eval);
- }
- return $content;
- }
- // Build Search Query
- // Usage: $this->buildSearch(ARRAY_SEARCH, ARRAY_SEARCHASSOCIATIONS, ARRAY_SEARCHADAPTATIONS[, TABLE]);
- public function buildSearch($search = null, $search_associations = null, $search_adaptations = null, $search_table = null)
- {
- if ($search_table)
- $query = "SELECT * FROM `".$this->prefix.$search_table."`";
- $continue_building = true;
- is_array($search) ? $query .= " WHERE " : $continue_building = false;
- $i = 1;
- if ($continue_building) foreach ($search as $filter => $data)
- {
- $FILTER = $filter;
- if ($search_associations[$filter]) $FILTER = $search_associations[$filter];
- $OPERATOR = "=";
- if (is_array($data))
- {
- if ($data["BETWEEN"])
- {
- $query .= "`$FILTER` BETWEEN ".$this->es($this->doSearchAdaptation($filter, $data["BETWEEN"][0], $search_adaptations))." AND ".$this->es($this->doSearchAdaptation($filter, $data["BETWEEN"][1], $search_adaptations));
- }
- else
- {
- if ($data["OPERATOR"])
- {
- $OPERATOR = $data["OPERATOR"];
- }
- if (is_numeric($data["VALUE"]))
- {
- $query .= "`$FILTER` $OPERATOR ".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations));
- }
- else
- {
- $query .= "`$FILTER` $OPERATOR '".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations))."'";
- }
- }
- }
- else
- {
- $query .= "`$FILTER` $OPERATOR '".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations))."'";
- }
- $theres_more = false;
- $reached_now = false;
- if ($i < count($search))
- {
- $query .= " AND ";
- }
- $i ++;
- }
- return $query;
- }
- // Build Insert Query
- // Usage: $this->buildInsert(ARRAY_INSERTDATA, ARRAY_INSERTASSOCIATIONS, ARRAY_INSERTADAPTATIONS[, TABLE]);
- public function buildInsert($insert_data = null, $insert_associations = null, $insert_adaptations = null, $insert_table = null)
- {
- if ($insert_table)
- $query = "INSERT INTO `".$this->prefix.$insert_table."` ";
- if (is_array($insert_data)) $query .= "("; else return false;
- $values = " VALUES (";
- $i = 1;
- foreach ($insert_data as $filter => $data)
- {
- $FILTER = $filter;
- if ($insert_associations[$filter]) $FILTER = $insert_associations[$filter];# else $FILTER = false;
- if ($FILTER)
- {
- if (is_numeric($insert_data[$filter]))
- {
- $query .= "`$FILTER`";
- $values .= $this->es($this->doSearchAdaptation($filter, $data, $insert_adaptations));
- }
- else
- {
- $query .= "`$FILTER`";
- $values .= "'".$this->es($this->doSearchAdaptation($filter, $data, $insert_adaptations))."'";
- }
- $theres_more = false;
- $reached_now = false;
- foreach ($insert_data as $checker => $temp)
- {
- if ($reached_now && $insert_associations[$checker]) $theres_more = true;
- if ($checker == $filter) $reached_now = true;
- }
- if ($i < count($insert_data))#$theres_more)
- {
- $query .= ", ";
- $values .= ", ";
- }
- }
- $i ++;
- }
- $query .= ")"; $values .= ")";
- $query .= $values;
- return $query;
- }
- // Build Update Query
- // Usage: $this->buildUpdate(ARRAY_UPDATE, ARRAY_SEARCH, ARRAY_SEARCHASSOCIATIONS, ARRAY_SEARCHADAPTATIONS[, TABLE]);
- public function buildUpdate($update = null, $search = null, $search_associations = null, $search_adaptations = null, $search_table = null)
- {
- if ($search_table)
- $query = "UPDATE `".$this->prefix.$search_table."`";
- $continue_building = true;
- if (is_array($update)) $query .= " SET "; else return false;
- $i = 1;
- foreach ($update as $filter => $data)
- {
- $FILTER = $filter;
- if ($search_associations[$filter]) $FILTER = $search_associations[$filter];
- $OPERATOR = "=";
- if (is_numeric($search[$filter]))
- {
- $query .= "`$FILTER` = ".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations));
- }
- else
- {
- $query .= "`$FILTER` = '".$this->es($this->doSearchAdaptation($filter, $data, $search_adaptations))."'";
- }
- $theres_more = false;
- $reached_now = false;
- foreach ($update as $checker => $temp)
- {
- if ($reached_now && $search_associations[$checker]) $theres_more = true;
- if ($checker == $filter) $reached_now = true;
- }
- if ($i < count($update))#$theres_more)
- {
- $query .= ", ";
- }
- $i ++;
- }
- $query .= $this->buildSearch($search, $search_associations, $search_adaptations);
- return $query;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment