diff options
Diffstat (limited to 'h-source/Library/Db/Mysqli.php')
-rw-r--r-- | h-source/Library/Db/Mysqli.php | 264 |
1 files changed, 26 insertions, 238 deletions
diff --git a/h-source/Library/Db/Mysqli.php b/h-source/Library/Db/Mysqli.php index bd3ce1c..df57a54 100644 --- a/h-source/Library/Db/Mysqli.php +++ b/h-source/Library/Db/Mysqli.php @@ -2,7 +2,7 @@ // EasyGiant is a PHP framework for creating and managing dynamic content // -// Copyright (C) 2009 - 2014 Antonio Gallo (info@laboratoriolibero.com) +// Copyright (C) 2009 - 2011 Antonio Gallo // See COPYRIGHT.txt and LICENSE.txt. // // This file is part of EasyGiant @@ -27,31 +27,14 @@ if (!defined('EG')) die('Direct access not allowed!'); class Db_Mysqli { - private $autocommit = true; - private $transactionBatchSize = 100; - - public $transactionBatch = array(); - public $query = null; //the last query executed - public $queries = array(); //array containing all the queries executed - public $charsetError = true; //true: non eccor occurred during the modification of the connection charset, false: one error occurred public $charset = null; //the charset of the client connection private static $instance = null; //instance of this class private $db; - - private $charTypes = array('varchar','char'); - private $textTypes = array('tinytext','text','mediumtext','longtext'); - private $integerTypes = array('tinyint','smallint','int','mediumint','bigint'); - private $floatTypes = array('real','float','double'); - private $dateTypes = array('date'); - private $enumTypes = array('enum'); - private $decimalTypes = array('decimal'); - private $uniqueIndexStrings = array('UNI'); - - private $fieldsType = array(); + private $fieldsType = array('tinyint','smallint','int','mediumint','bigint','float','double'); //PHP-Mysql charset translation table private $charsetTranslationTable = array( @@ -70,8 +53,7 @@ class Db_Mysqli private function __construct($host,$user,$pwd,$db_name) { - $this->fieldsType = array_merge($this->integerTypes, $this->floatTypes); - + $this->db = new mysqli($host,$user,$pwd,$db_name); if (mysqli_connect_error()) @@ -103,46 +85,7 @@ class Db_Mysqli return self::$instance; } - public function getUniqueIndexStrings() - { - return $this->uniqueIndexStrings; - } - - public function getTextTypes() - { - return $this->textTypes; - } - - public function getDecimalTypes() - { - return $this->decimalTypes; - } - - public function getEnumTypes() - { - return $this->enumTypes; - } - - public function getCharTypes() - { - return $this->charTypes; - } - - public function getIntegerTypes() - { - return $this->integerTypes; - } - - public function getFloatTypes() - { - return $this->floatTypes; - } - - public function getDateTypes() - { - return $this->dateTypes; - } - + //close the connection public function disconnect() { @@ -227,30 +170,16 @@ class Db_Mysqli public function get_num_rows($table,$where=null,$group_by=null,$on=array(),$using=array(),$join=array()) { - $select = isset($group_by) ? "*" : 'count(*) as number'; - - $query = $this->createSelectQuery($table,$select,$where,$group_by,null,null,$on,$using,$join); + $query = $this->createSelectQuery($table,'*',$where,$group_by,null,null,$on,$using,$join); $this->query = $query; - $this->queries[] = $query; - $ris = $this->db->query($query); if ($ris) { - - if (isset($group_by)) - { - $num_rows = $ris->num_rows; - } - else - { - $row = $ris->fetch_array(); - $num_rows = $row['number']; - } - + $num_rows = $ris->num_rows; $ris->close(); - return (int)$num_rows; + return $num_rows; } else { - return 0; + return false; } } @@ -259,8 +188,6 @@ class Db_Mysqli $query = $this->createSelectQuery($table,"$func($field) AS m",$where,$group_by,null,null,$on,$using,$join); $this->query = $query; - $this->queries[] = $query; - $result = $this->db->query($query); if ($result) { @@ -303,8 +230,6 @@ class Db_Mysqli $query = $this->createSelectQuery($table,$fields,$where,$group_by,$order_by,$limit,$on,$using,$join); $this->query = $query; - $this->queries[] = $query; - $result = $this->db->query($query); return $this->getData($result); } @@ -337,85 +262,44 @@ class Db_Mysqli $result->close(); return $data; } else { - return array(); + return false; } } - private function getFieldsFeature($feature, $table, $fields, $full = false, $associative = false ) + //return an array containing all the types of the fields (indicated in $fields) of a table (indicated in $table) + public function getTypes($table, $fields) { $query = "DESCRIBE $table;"; $result = $this->db->query($query); $temp = array(); while ($row = $result->fetch_assoc()) { - if ($full) - { - $temp[$row['Field']] = $row[$feature]; - } - else - { - $e = explode('(',$row[$feature]); - $temp[$row['Field']] = strcmp($feature,"Type") === 0 ? strtolower(reset($e)) : reset($e); - } + $temp[$row['Field']] = reset(explode('(',$row['Type'])); } $result->close(); - $this->queries[] = $query; - - //return all fields types - if ($fields === "*") - { - $fields = implode(",",array_keys($temp)); - } - $types = array(); $fields = explode(',',$fields); for ($i = 0; $i < count($fields); $i++) { if (!array_key_exists($fields[$i],$temp)) return false; - - if ($associative) - { - $types[$fields[$i]] = $temp[$fields[$i]]; - } - else - { - $types[] = $temp[$fields[$i]]; - } + $types[] = $temp[$fields[$i]]; } return $types; } - //return an array containing all the keys of the fields (indicated in $fields) of a table (indicated in $table) - public function getKeys($table, $fields, $full = false, $associative = false) - { - return $this->getFieldsFeature('Key', $table, $fields, $full, $associative); - } - - //return an array containing all the default values of the fields (indicated in $fields) of a table (indicated in $table) - public function getDefaultValues($table, $fields, $full = false, $associative = false) - { - return $this->getFieldsFeature('Default', $table, $fields, $full, $associative); - } - - //return an array containing all the types of the fields (indicated in $fields) of a table (indicated in $table) - public function getTypes($table, $fields, $full = false, $associative = false) - { - return $this->getFieldsFeature('Type', $table, $fields, $full, $associative); - } - public function insert($table,$fields,$values) { + #$table is a string #$fields has to be a string with comma as separator: name1,name2,... #$values has to be an array - $values = array_values($values); if (strcmp($fields,'') !== 0) { //get the type of the fields $types = $this->getTypes($table,$fields); if (!$types) return false; - + for($i = 0; $i < count($values); $i++) { if (!in_array($types[$i],$this->fieldsType)) @@ -431,103 +315,21 @@ class Db_Mysqli $values = implode(',',$values); $query="INSERT INTO $table ($fields) VALUES ($values);"; $this->query=$query; - $this->queries[] = $query; - if ($this->autocommit) - { - $ris = $this->db->query($query); + $ris = $this->db->query($query); - #check the result - if ($ris) { - return true; - } else { - return false; - } - } - else - { - $this->transactionBatch[] = $query; + #check the result + if ($ris) { return true; + } else { + return false; } + } else { return false; } } - //set the autocommit attribute - public function setAutocommit($value) - { - if ($value === true or $value === false) - { - $this->autocommit = $value; - $this->db->autocommit($value); - } - else - { - $this->autocommit = true; - $this->db->autocommit(true); - } - } - - //set the transactionBatchSize attribute - public function setTransactionBatchSize($size) - { - $this->transactionBatchSize = abs($size); - } - - //commit a batch of queries - //$batch: array of queries - public function commitBatch($batch) - { - foreach ($batch as $sql) - { - $this->db->query($sql); - } - - if (!$this->autocommit and $this->db->commit()) - { - return true; - } - else - { - return false; - } - } - - //commit the transaction - public function commitTransaction() - { - $returnArray = array(); - - if (!$this->autocommit) - { - if (count($this->transactionBatch) > 0) - { - if ($this->transactionBatchSize === 0) - { - $returnArray[] = $this->commitBatch($this->transactionBatch); - } - else - { - $batchArray = array_chunk($this->transactionBatch, $this->transactionBatchSize); - - foreach ($batchArray as $batch) - { - $returnArray[] = $this->commitBatch($batch); - } - } - } - } - - if (count(array_filter($returnArray)) === count($returnArray)) - { - $this->transactionBatch = array(); - return true; - } - - return false; - } - // Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). public function lastId() { @@ -569,23 +371,13 @@ class Db_Mysqli $str=implode(',',$str); $query="UPDATE $table SET $str $where;"; $this->query=$query; - $this->queries[] = $query; - - if ($this->autocommit) - { - $ris = $this->db->query($query); + $ris = $this->db->query($query); - #check the result - if ($ris) { - return true; - } else { - return false; - } - } - else - { - $this->transactionBatch[] = $query; + #check the result + if ($ris) { return true; + } else { + return false; } } else { return false; @@ -602,8 +394,6 @@ class Db_Mysqli // } $query="DELETE FROM $table $where;"; $this->query=$query; - $this->queries[] = $query; - $ris = $this->db->query($query); #check the result @@ -638,8 +428,6 @@ class Db_Mysqli public function query($query) { $this->query = $query; - $this->queries[] = $query; - $result = $this->db->query($query); if ($result === true) { |