- upgraded FatFreeFramework 3.6.1 -> 3.6.2

This commit is contained in:
Mark Friedrich
2018-01-06 19:51:16 +01:00
parent 6fdb1ed91b
commit ac417fb19f
26 changed files with 842 additions and 580 deletions

View File

@@ -124,7 +124,7 @@ class SQL {
$val=str_replace(',','.',$val);
return $val;
case \PDO::PARAM_NULL:
return (unset)$val;
return NULL;
case \PDO::PARAM_INT:
return (int)$val;
case \PDO::PARAM_BOOL:
@@ -185,7 +185,7 @@ class SQL {
continue;
$now=microtime(TRUE);
$keys=$vals=[];
if ($fw->get('CACHE') && $ttl && ($cached=$cache->exists(
if ($fw->CACHE && $ttl && ($cached=$cache->exists(
$hash=$fw->hash($this->dsn.$cmd.
$fw->stringify($arg)).($tag?'.'.$tag:'').'.sql',$result)) &&
$cached[0]+$ttl>microtime(TRUE)) {
@@ -220,13 +220,15 @@ class SQL {
'/';
}
if ($log)
$this->log.=($stamp?(date('r').' '):'').'('.
sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
$this->log.=($stamp?(date('r').' '):'').' (-0ms) '.
preg_replace($keys,$vals,
str_replace('?',chr(0).'?',$cmd),1).PHP_EOL;
$query->execute();
$error=$query->errorinfo();
if ($error[0]!=\PDO::ERR_NONE) {
if ($log)
$this->log=str_replace('(-0ms)',
'('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms)',
$this->log);
if (($error=$query->errorinfo()) && $error[0]!=\PDO::ERR_NONE) {
// Statement-level error occurred
if ($this->trans)
$this->rollback();
@@ -246,7 +248,7 @@ class SQL {
$result[$pos][trim($key,'\'"[]`')]=$val;
}
$this->rows=count($result);
if ($fw->get('CACHE') && $ttl)
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$result,$ttl);
}
@@ -255,15 +257,13 @@ class SQL {
$query->closecursor();
unset($query);
}
else {
$error=$this->errorinfo();
if ($error[0]!=\PDO::ERR_NONE) {
// PDO-level error occurred
if ($this->trans)
$this->rollback();
user_error('PDO: '.$error[2],E_USER_ERROR);
}
elseif (($error=$this->errorinfo()) && $error[0]!=\PDO::ERR_NONE) {
// PDO-level error occurred
if ($this->trans)
$this->rollback();
user_error('PDO: '.$error[2],E_USER_ERROR);
}
}
if ($this->trans && $auto)
$this->commit();
@@ -280,8 +280,8 @@ class SQL {
/**
* Return SQL profiler results (or disable logging)
* @param $flag bool
* @return string
* @param $flag bool
**/
function log($flag=TRUE) {
if ($flag)
@@ -289,6 +289,20 @@ class SQL {
$this->log=FALSE;
}
/**
* Return TRUE if table exists
* @return bool
* @param $table string
**/
function exists($table) {
$mode=$this->pdo->getAttribute(\PDO::ATTR_ERRMODE);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_SILENT);
$out=$this->pdo->
query('SELECT 1 FROM '.$this->quotekey($table).' LIMIT 1');
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE,$mode);
return is_object($out);
}
/**
* Retrieve schema of SQL table
* @return array|FALSE
@@ -297,43 +311,50 @@ class SQL {
* @param $ttl int|array
**/
function schema($table,$fields=NULL,$ttl=0) {
$fw=\Base::instance();
$cache=\Cache::instance();
if ($fw->CACHE && $ttl &&
($cached=$cache->exists(
$hash=$fw->hash($this->dsn.$table).'.schema',$result)) &&
$cached[0]+$ttl>microtime(TRUE))
return $result;
if (strpos($table,'.'))
list($schema,$table)=explode('.',$table);
// Supported engines
$cmd=[
'sqlite2?'=>[
'PRAGMA table_info("'.$table.'")',
'PRAGMA table_info(`'.$table.'`)',
'name','type','dflt_value','notnull',0,'pk',TRUE],
'mysql'=>[
'SHOW columns FROM `'.$this->dbname.'`.`'.$table.'`',
'Field','Type','Default','Null','YES','Key','PRI'],
'mssql|sqlsrv|sybase|dblib|pgsql|odbc'=>[
'SELECT '.
'c.column_name AS field,'.
'c.data_type AS type,'.
'c.column_default AS defval,'.
'c.is_nullable AS nullable,'.
't.constraint_type AS pkey '.
'FROM information_schema.columns AS c '.
'C.COLUMN_NAME AS field,'.
'C.DATA_TYPE AS type,'.
'C.COLUMN_DEFAULT AS defval,'.
'C.IS_NULLABLE AS nullable,'.
'T.CONSTRAINT_TYPE AS pkey '.
'FROM INFORMATION_SCHEMA.COLUMNS AS C '.
'LEFT OUTER JOIN '.
'information_schema.key_column_usage AS k '.
'INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K '.
'ON '.
'c.table_name=k.table_name AND '.
'c.column_name=k.column_name AND '.
'c.table_schema=k.table_schema '.
'C.TABLE_NAME=K.TABLE_NAME AND '.
'C.COLUMN_NAME=K.COLUMN_NAME AND '.
'C.TABLE_SCHEMA=K.TABLE_SCHEMA '.
($this->dbname?
('AND c.table_catalog=k.table_catalog '):'').
('AND C.TABLE_CATALOG=K.TABLE_CATALOG '):'').
'LEFT OUTER JOIN '.
'information_schema.table_constraints AS t ON '.
'k.table_name=t.table_name AND '.
'k.constraint_name=t.constraint_name AND '.
'k.table_schema=t.table_schema '.
'INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS T ON '.
'K.TABLE_NAME=T.TABLE_NAME AND '.
'K.CONSTRAINT_NAME=T.CONSTRAINT_NAME AND '.
'K.TABLE_SCHEMA=T.TABLE_SCHEMA '.
($this->dbname?
('AND k.table_catalog=t.table_catalog '):'').
('AND K.TABLE_CATALOG=T.TABLE_CATALOG '):'').
'WHERE '.
'c.table_name='.$this->quote($table).
'C.TABLE_NAME='.$this->quote($table).
($this->dbname?
(' AND c.table_catalog='.
(' AND C.TABLE_CATALOG='.
$this->quote($this->dbname)):''),
'field','type','defval','nullable','YES','pkey','PRIMARY KEY'],
'oci'=>[
@@ -354,32 +375,34 @@ class SQL {
];
if (is_string($fields))
$fields=\Base::instance()->split($fields);
$conv=[
'int\b|integer'=>\PDO::PARAM_INT,
'bool'=>\PDO::PARAM_BOOL,
'blob|bytea|image|binary'=>\PDO::PARAM_LOB,
'float|real|double|decimal|numeric'=>self::PARAM_FLOAT,
'.+'=>\PDO::PARAM_STR
];
foreach ($cmd as $key=>$val)
if (preg_match('/'.$key.'/',$this->engine)) {
$rows=[];
foreach ($this->exec($val[0],NULL,$ttl) as $row) {
if (!$fields || in_array($row[$val[1]],$fields))
foreach ($this->exec($val[0],NULL) as $row)
if (!$fields || in_array($row[$val[1]],$fields)) {
foreach ($conv as $regex=>$type)
if (preg_match('/'.$regex.'/i',$row[$val[2]]))
break;
$rows[$row[$val[1]]]=[
'type'=>$row[$val[2]],
'pdo_type'=>
preg_match('/int\b|integer/i',$row[$val[2]])?
\PDO::PARAM_INT:
(preg_match('/bool/i',$row[$val[2]])?
\PDO::PARAM_BOOL:
(preg_match(
'/blob|bytea|image|binary/i',
$row[$val[2]])?\PDO::PARAM_LOB:
(preg_match(
'/float|decimal|real|numeric|double/i',
$row[$val[2]])?self::PARAM_FLOAT:
\PDO::PARAM_STR))),
'pdo_type'=>$type,
'default'=>is_string($row[$val[3]])?
preg_replace('/^\s*([\'"])(.*)\1\s*/','\2',
$row[$val[3]]):$row[$val[3]],
'nullable'=>$row[$val[4]]==$val[5],
'pkey'=>$row[$val[6]]==$val[7]
];
}
}
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$rows,$ttl);
return $rows;
}
user_error(sprintf(self::E_PKey,$table),E_USER_ERROR);
@@ -448,8 +471,8 @@ class SQL {
**/
function quotekey($key, $split=TRUE) {
$delims=[
'mysql'=>'``',
'sqlite2?|pgsql|oci'=>'""',
'sqlite2?|mysql'=>'``',
'pgsql|oci'=>'""',
'mssql|sqlsrv|odbc|sybase|dblib'=>'[]'
];
$use='';
@@ -492,7 +515,7 @@ class SQL {
$options=[];
if (isset($parts[0]) && strstr($parts[0],':',TRUE)=='mysql')
$options+=[\PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES '.
strtolower(str_replace('-','',$fw->get('ENCODING'))).';'];
strtolower(str_replace('-','',$fw->ENCODING)).';'];
$this->pdo=new \PDO($dsn,$user,$pw,$options);
$this->engine=$this->pdo->getattribute(\PDO::ATTR_DRIVER_NAME);
}