17 lines
536 KiB
PHP
17 lines
536 KiB
PHP
|
<?php
|
||
|
/* This file is part of DBSR.
|
||
|
*
|
||
|
* DBSR is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* DBSR is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with DBSR. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
class DBSR{const VERSION='2.2.0';const OPTION_CASE_INSENSITIVE=0;const OPTION_EXTENSIVE_SEARCH=1;const OPTION_SEARCH_PAGE_SIZE=2;const OPTION_VAR_MATCH_STRICT=3;const OPTION_FLOATS_PRECISION=4;const OPTION_CONVERT_CHARSETS=5;const OPTION_VAR_CAST_REPLACE=6;const OPTION_DB_WRITE_CHANGES=7;const OPTION_HANDLE_SERIALIZE=8;const OPTION_REVERSED_FILTERS=9;const OPTION_LOCK_TABLES=10;public static function createClass($className){if(!class_exists($className,false)){$classArray=explode('\\',$className);if(count($classArray)>1){$className=array_pop($classArray);$namespace=implode('\\',$classArray);eval('namespace '.$namespace.' { class '.$className.' {} }');}else{eval('class '.$className.' {}');}}}public static function getPHPType($mysql_type){$types=array('/^\s*BOOL(EAN)?\s*$/i'=>'boolean','/^\s*TINYINT\s*(?:\(\s*\d+\s*\)\s*)?$/i'=>'integer','/^\s*SMALLINT\s*(?:\(\s*\d+\s*\)\s*)?$/i'=>'integer','/^\s*MEDIUMINT\s*(?:\(\s*\d+\s*\)\s*)?$/i'=>'integer','/^\s*INT(EGER)?\s*(?:\(\s*\d+\s*\)\s*)?$/i'=>'integer','/^\s*BIGINT\s*(?:\(\s*\d+\s*\)\s*)?$/i'=>'integer','/^\s*FLOAT\s*(?:\(\s*\d+\s*(?:,\s*\d+\s*)?\)\s*)?$/i'=>'float','/^\s*DOUBLE(\s+PRECISION)?\s*(?:\(\s*\d+\s*(?:,\s*\d+\s*)?\)\s*)?$/i'=>'float','/^\s*REAL\s*(?:\(\s*\d+\s*(?:,\s*\d+\s*)?\)\s*)?$/i'=>'float','/^\s*DEC(IMAL)?\s*(?:\(\s*\d+\s*(?:,\s*\d+\s*)?\)\s*)?$/i'=>'float','/^\s*NUMERIC\s*(?:\(\s*\d+\s*(?:,\s*\d+\s*)?\)\s*)?$/i'=>'float','/^\s*FIXED\s*(?:\(\s*\d+\s*(?:,\s*\d+\s*)?\)\s*)?$/i'=>'float',);foreach($types as $regex=>$type){if(preg_match($regex,$mysql_type)){return $type;}}return 'string';}protected $pdo;private $_pdo_charset;private $_pdo_collation;private $_dbr_callback;protected $options=array(self::OPTION_CASE_INSENSITIVE=>false,self::OPTION_EXTENSIVE_SEARCH=>false,self::OPTION_SEARCH_PAGE_SIZE=>10000,self::OPTION_VAR_MATCH_STRICT=>true,self::OPTION_FLOATS_PRECISION=>5,self::OPTION_CONVERT_CHARSETS=>true,self::OPTION_VAR_CAST_REPLACE=>true,self::OPTION_DB_WRITE_CHANGES=>true,self::OPTION_HANDLE_SERIALIZE=>true,self::OPTION_REVERSED_FILTERS=>false,self::OPTION_LOCK_TABLES=>true,);protected $filters=array();protected $search=array();protected $replace=array();protected $search_converted=array();public function __construct(PDO$pdo){if(!extension_loaded('pcre')){throw new RuntimeException('The pcre (Perl-compatible regular expressions) extension is required for DBSR to work!');}if($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)!='mysql'){throw new InvalidArgumentException('The given PDO instance is not representing an MySQL database!');}$this->pdo=$pdo;}public function getOption($option){return isset($this->options[$option])?$this->options[$option]:null;}public function setOption($option,$value){if(!isset($this->options[$option])){return false;}switch($option){case static ::OPTION_SEARCH_PAGE_SIZE:if(is_int($value)&&$value>0){$this->options[$option]=$value;return true;}else{return false;}case static ::OPTION_FLOATS_PRECISION:if(is_int($value)&&$value>=0){$this->options[$option]=$value;return true;}else{return false;}default:if(gettype($this->options[$option])==gettype($value)){$this->options[$option]=$value;return true;}else{return false;}}}public function setFilters(array$filters){$filters_parsed=array();foreach($filters as $key=>$value){if(is_int($key)){if(is_string($value)){$filters_parsed[$value]=true;}elseif(is_array($value)){if(!count($value)){continue;}foreach($value as $v){if(!is_string($v)){throw new InvalidArgumentException('Only strings qualify as column names!');}}if(isset($filters_parsed['.'])){$filters_parsed['.']=array_values(array_unique(array_merge($filters_parsed['.'],array_values($value))));}else{$filters_parsed['.']=array_values(array_unique($value));}}else{throw new InvalidArgumentException('The filter array can only contain strings or arrays!');}}else{if(is_string($value)){if(isset($filters_parsed[$key])){$filters_parsed[$key]=array_values(array_unique(array_merge($filters_parsed[$key],array($value))));}else{$filters_parsed[$key]=array($value);}}elseif(is_array($value)){if(!count($value)){continue;}foreach($value as $v){if(!is_string($v)){throw new
|