2002-10-11 17:45:06 +08:00
|
|
|
<?php
|
|
|
|
//
|
2004-01-08 19:45:57 +08:00
|
|
|
// ZoneMinder web database interface file, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2019-08-13 23:45:50 +08:00
|
|
|
//
|
2002-10-11 17:45:06 +08:00
|
|
|
// This program 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 2
|
|
|
|
// of the License, or (at your option) any later version.
|
2019-08-13 23:45:50 +08:00
|
|
|
//
|
2002-10-11 17:45:06 +08:00
|
|
|
// This program 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.
|
2019-08-13 23:45:50 +08:00
|
|
|
//
|
2002-10-11 17:45:06 +08:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2019-08-13 23:45:50 +08:00
|
|
|
//
|
2002-10-11 17:45:06 +08:00
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
define('DB_LOG_OFF', 0);
|
|
|
|
define('DB_LOG_ONLY', 1);
|
|
|
|
define('DB_LOG_DEBUG', 2);
|
2008-07-14 21:54:50 +08:00
|
|
|
|
|
|
|
$GLOBALS['dbLogLevel'] = DB_LOG_OFF;
|
|
|
|
|
|
|
|
$GLOBALS['dbConn'] = false;
|
2020-06-04 23:34:16 +08:00
|
|
|
require_once('logger.php');
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbConnect() {
|
|
|
|
global $dbConn;
|
|
|
|
|
2020-06-06 07:02:43 +08:00
|
|
|
$dsn = ZM_DB_TYPE;
|
2020-06-04 23:34:16 +08:00
|
|
|
if ( ZM_DB_HOST ) {
|
|
|
|
if ( strpos(ZM_DB_HOST, ':') ) {
|
|
|
|
// Host variable may carry a port or socket.
|
|
|
|
list($host, $portOrSocket) = explode(':', ZM_DB_HOST, 2);
|
|
|
|
if ( ctype_digit($portOrSocket) ) {
|
2020-06-06 07:02:43 +08:00
|
|
|
$dsn .= ':host='.$host.';port='.$portOrSocket.';';
|
2020-06-04 23:34:16 +08:00
|
|
|
} else {
|
2020-06-06 07:02:43 +08:00
|
|
|
$dsn .= ':unix_socket='.$portOrSocket.';';
|
2020-06-04 23:34:16 +08:00
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
} else {
|
2020-06-06 07:02:43 +08:00
|
|
|
$dsn .= ':host='.ZM_DB_HOST.';';
|
2017-05-17 02:16:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-06 07:02:43 +08:00
|
|
|
$dsn .= ':host=localhost;';
|
2017-05-17 02:16:32 +08:00
|
|
|
}
|
2020-06-06 07:02:43 +08:00
|
|
|
$dsn .= 'dbname='.ZM_DB_NAME;
|
2017-05-17 02:16:32 +08:00
|
|
|
|
|
|
|
try {
|
2017-08-15 06:22:52 +08:00
|
|
|
$dbOptions = null;
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( defined('ZM_DB_SSL_CA_CERT') and ZM_DB_SSL_CA_CERT ) {
|
2017-08-15 06:22:52 +08:00
|
|
|
$dbOptions = array(
|
|
|
|
PDO::MYSQL_ATTR_SSL_CA => ZM_DB_SSL_CA_CERT,
|
|
|
|
PDO::MYSQL_ATTR_SSL_KEY => ZM_DB_SSL_CLIENT_KEY,
|
|
|
|
PDO::MYSQL_ATTR_SSL_CERT => ZM_DB_SSL_CLIENT_CERT,
|
|
|
|
);
|
2020-06-06 07:02:43 +08:00
|
|
|
$dbConn = new PDO($dsn, ZM_DB_USER, ZM_DB_PASS, $dbOptions);
|
2017-08-15 06:22:52 +08:00
|
|
|
} else {
|
2020-06-06 07:02:43 +08:00
|
|
|
$dbConn = new PDO($dsn, ZM_DB_USER, ZM_DB_PASS);
|
2017-08-15 06:22:52 +08:00
|
|
|
}
|
2017-08-16 07:40:02 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
$dbConn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
|
|
|
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2019-09-10 04:13:32 +08:00
|
|
|
} catch(PDOException $ex) {
|
2020-09-21 23:15:34 +08:00
|
|
|
global $error_message;
|
|
|
|
$error_message = "Unable to connect to ZM db using dsn $dsn<br/><br/>".$ex->getMessage();
|
2019-08-13 23:45:50 +08:00
|
|
|
error_log('Unable to connect to ZM DB ' . $ex->getMessage());
|
2017-05-17 02:16:32 +08:00
|
|
|
$dbConn = null;
|
|
|
|
}
|
2020-06-04 23:34:16 +08:00
|
|
|
return $dbConn;
|
|
|
|
} // end function dbConnect
|
2007-09-07 23:39:44 +08:00
|
|
|
|
2020-06-04 23:34:16 +08:00
|
|
|
if ( !dbConnect() ) {
|
2020-09-21 23:15:34 +08:00
|
|
|
include('views/no_database_connection.php');
|
|
|
|
exit();
|
2020-06-04 23:34:16 +08:00
|
|
|
}
|
2002-10-11 17:45:06 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbDisconnect() {
|
|
|
|
global $dbConn;
|
|
|
|
$dbConn = null;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
2005-01-03 22:44:38 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLogOff() {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_OFF;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLogOn() {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_ONLY;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
2007-08-30 06:33:22 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbLogDebug() {
|
|
|
|
global $dbLogLevel;
|
|
|
|
$dbLogLevel = DB_LOG_DEBUG;
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbDebug() {
|
|
|
|
dbLogDebug();
|
2008-07-14 21:54:50 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbLog($sql, $update=false) {
|
2017-05-17 02:16:32 +08:00
|
|
|
global $dbLogLevel;
|
|
|
|
$noExecute = $update && ($dbLogLevel >= DB_LOG_DEBUG);
|
|
|
|
if ( $dbLogLevel > DB_LOG_OFF )
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug( "SQL-LOG: $sql".($noExecute?' (not executed)':'') );
|
2017-05-17 02:16:32 +08:00
|
|
|
return( $noExecute );
|
2007-08-30 06:33:22 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbError($sql) {
|
2018-11-08 01:33:18 +08:00
|
|
|
global $dbConn;
|
|
|
|
$error = $dbConn->errorInfo();
|
|
|
|
if ( ! $error[0] )
|
|
|
|
return '';
|
|
|
|
|
|
|
|
$message = "SQL-ERR '".implode("\n",$dbConn->errorInfo())."', statement was '".$sql."'";
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Error($message);
|
2018-11-08 01:33:18 +08:00
|
|
|
return $message;
|
2007-08-30 06:33:22 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbEscape( $string ) {
|
|
|
|
global $dbConn;
|
2019-12-08 00:45:32 +08:00
|
|
|
if ( version_compare(phpversion(), '5.4', '<=') and get_magic_quotes_gpc() )
|
|
|
|
return $dbConn->quote(stripslashes($string));
|
2017-05-17 02:16:32 +08:00
|
|
|
else
|
2019-12-08 00:45:32 +08:00
|
|
|
return $dbConn->quote($string);
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 03:07:23 +08:00
|
|
|
function dbQuery($sql, $params=NULL, $debug = false) {
|
2017-05-17 02:16:32 +08:00
|
|
|
global $dbConn;
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( dbLog($sql, true) )
|
2017-05-17 02:16:32 +08:00
|
|
|
return;
|
|
|
|
$result = NULL;
|
|
|
|
try {
|
|
|
|
if ( isset($params) ) {
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( ! $result = $dbConn->prepare($sql) ) {
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Error("SQL: Error preparing $sql: " . $pdo->errorInfo);
|
2017-10-31 08:20:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( ! $result->execute($params) ) {
|
2019-09-20 02:56:34 +08:00
|
|
|
ZM\Error("SQL: Error executing $sql: " . print_r($result->errorInfo(), true));
|
2017-10-31 08:20:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
} else {
|
2020-12-02 03:07:23 +08:00
|
|
|
if ( defined('ZM_DB_DEBUG') or $debug ) {
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug("SQL: $sql values:" . ($params?implode(',',$params):''));
|
2018-02-09 22:07:40 +08:00
|
|
|
}
|
|
|
|
$result = $dbConn->query($sql);
|
2018-11-08 01:33:18 +08:00
|
|
|
if ( ! $result ) {
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Error("SQL: Error preparing $sql: " . $pdo->errorInfo);
|
2018-11-08 01:33:18 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-19 02:15:24 +08:00
|
|
|
}
|
2020-12-02 03:07:23 +08:00
|
|
|
if ( defined('ZM_DB_DEBUG') or $debug ) {
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug('SQL: '.$sql.' '.($params?implode(',',$params):'').' rows: '.$result->rowCount());
|
2018-01-23 00:42:19 +08:00
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
} catch(PDOException $e) {
|
2019-08-13 23:45:50 +08:00
|
|
|
ZM\Error("SQL-ERR '".$e->getMessage()."', statement was '".$sql."' params:" . ($params?implode(',',$params):''));
|
2018-01-30 02:41:24 +08:00
|
|
|
return NULL;
|
2017-05-17 02:16:32 +08:00
|
|
|
}
|
2018-01-30 02:41:24 +08:00
|
|
|
return $result;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbFetchOne($sql, $col=false, $params=NULL) {
|
|
|
|
$result = dbQuery($sql, $params);
|
|
|
|
if ( !$result ) {
|
|
|
|
ZM\Error("SQL-ERR dbFetchOne no result, statement was '".$sql."'".($params ? 'params: ' . join(',',$params) : ''));
|
2017-05-17 02:16:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( !$result->rowCount() ) {
|
2018-02-02 23:27:30 +08:00
|
|
|
# No rows is not an error
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
|
2019-05-25 02:52:04 +08:00
|
|
|
if ( $result && ($dbRow = $result->fetch(PDO::FETCH_ASSOC)) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
if ( $col ) {
|
2018-04-19 05:17:41 +08:00
|
|
|
if ( ! array_key_exists($col, $dbRow) ) {
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Warning("$col does not exist in the returned row " . print_r($dbRow, true));
|
2019-05-25 02:52:04 +08:00
|
|
|
return false;
|
2017-05-17 02:16:32 +08:00
|
|
|
}
|
|
|
|
return $dbRow[$col];
|
2019-08-13 23:45:50 +08:00
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
return $dbRow;
|
|
|
|
}
|
2018-04-19 02:21:27 +08:00
|
|
|
return false;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbFetchAll($sql, $col=false, $params=NULL) {
|
|
|
|
$result = dbQuery($sql, $params);
|
2017-05-17 02:16:32 +08:00
|
|
|
if ( ! $result ) {
|
2019-08-13 23:45:50 +08:00
|
|
|
ZM\Error("SQL-ERR dbFetchAll no result, statement was '".$sql."'".($params ? 'params: '.join(',', $params) : ''));
|
2017-05-17 02:16:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbRows = array();
|
2019-08-13 23:45:50 +08:00
|
|
|
while ( $dbRow = $result->fetch(PDO::FETCH_ASSOC) )
|
|
|
|
$dbRows[] = $col ? $dbRow[$col] : $dbRow;
|
2018-04-25 21:33:12 +08:00
|
|
|
return $dbRows;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbFetchAssoc($sql, $indexCol, $dataCol=false) {
|
|
|
|
$result = dbQuery($sql);
|
2008-12-06 07:15:08 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
$dbRows = array();
|
2019-08-13 23:45:50 +08:00
|
|
|
while( $dbRow = $result->fetch(PDO::FETCH_ASSOC) )
|
|
|
|
$dbRows[$dbRow[$indexCol]] = $dataCol ? $dbRow[$dataCol] : $dbRow;
|
|
|
|
return $dbRows;
|
2008-12-06 07:15:08 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbFetch($sql, $col=false) {
|
|
|
|
return dbFetchAll($sql, $col);
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function dbFetchNext($result, $col=false) {
|
2019-12-14 00:49:55 +08:00
|
|
|
if ( !$result ) {
|
|
|
|
ZM\Error("dbFetchNext called on null result.");
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( $dbRow = $result->fetch(PDO::FETCH_ASSOC) )
|
|
|
|
return $col ? $dbRow[$col] : $dbRow;
|
|
|
|
return false;
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbNumRows( $sql ) {
|
2019-08-13 23:45:50 +08:00
|
|
|
$result = dbQuery($sql);
|
|
|
|
return $result->rowCount();
|
2005-01-03 22:44:38 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function dbInsertId() {
|
|
|
|
global $dbConn;
|
2019-08-13 23:45:50 +08:00
|
|
|
return $dbConn->lastInsertId();
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function getEnumValues($table, $column) {
|
|
|
|
$row = dbFetchOne("DESCRIBE `$table` `$column`");
|
|
|
|
preg_match_all("/'([^']+)'/", $row['Type'], $matches);
|
|
|
|
return $matches[1];
|
2002-10-11 17:45:06 +08:00
|
|
|
}
|
2003-09-23 17:52:45 +08:00
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function getSetValues($table, $column) {
|
|
|
|
return getEnumValues($table, $column);
|
2003-09-23 17:52:45 +08:00
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
|
2019-08-13 23:45:50 +08:00
|
|
|
function getUniqueValues($table, $column, $asString=1) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$values = array();
|
2019-08-13 23:45:50 +08:00
|
|
|
$sql = "SELECT DISTINCT `$column` FROM `$table` WHERE (NOT isnull(`$column`) AND `$column` != '') ORDER BY `$column`";
|
|
|
|
foreach ( dbFetchAll($sql) as $row ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
if ( $asString )
|
|
|
|
$values[$row[$column]] = $row[$column];
|
|
|
|
else
|
|
|
|
$values[] = $row[$column];
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
return $values;
|
|
|
|
}
|
2005-03-14 04:41:06 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getTableColumns( $table, $asString=1 ) {
|
|
|
|
$columns = array();
|
2019-08-13 23:45:50 +08:00
|
|
|
$sql = "DESCRIBE `$table`";
|
|
|
|
foreach ( dbFetchAll($sql) as $row ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
if ( $asString )
|
|
|
|
$columns[$row['Field']] = $row['Type'];
|
|
|
|
else
|
|
|
|
$columns[] = $row['Type'];
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
return $columns;
|
|
|
|
}
|
2007-09-06 20:59:19 +08:00
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getTableAutoInc( $table ) {
|
2019-08-13 23:45:50 +08:00
|
|
|
$row = dbFetchOne('SHOW TABLE status WHERE Name=?', NULL, array($table));
|
|
|
|
return $row['Auto_increment'];
|
2009-03-31 18:53:51 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:16:32 +08:00
|
|
|
function getTableDescription( $table, $asString=1 ) {
|
|
|
|
$columns = array();
|
2019-08-13 23:45:50 +08:00
|
|
|
foreach( dbFetchAll("DESCRIBE `$table`") as $row ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc = array(
|
|
|
|
'name' => $row['Field'],
|
|
|
|
'required' => ($row['Null']=='NO')?true:false,
|
|
|
|
'default' => $row['Default'],
|
|
|
|
'db' => $row,
|
2010-05-27 20:18:06 +08:00
|
|
|
);
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( preg_match('/^varchar\((\d+)\)$/', $row['Type'], $matches) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['type'] = 'text';
|
|
|
|
$desc['typeAttrib'] = 'varchar';
|
|
|
|
$desc['maxLength'] = $matches[1];
|
2019-08-13 23:45:50 +08:00
|
|
|
} elseif ( preg_match('/^(\w+)?text$/', $row['Type'], $matches) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['type'] = 'text';
|
2019-08-13 23:45:50 +08:00
|
|
|
if ( !empty($matches[1]) )
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['typeAttrib'] = $matches[1];
|
|
|
|
switch ( $matches[1] ) {
|
|
|
|
case 'tiny' :
|
|
|
|
$desc['maxLength'] = 255;
|
|
|
|
break;
|
|
|
|
case 'medium' :
|
|
|
|
$desc['maxLength'] = 32768;
|
|
|
|
break;
|
|
|
|
case '' :
|
|
|
|
case 'big' :
|
|
|
|
//$desc['minLength'] = -128;
|
|
|
|
break;
|
|
|
|
default :
|
2019-08-13 23:45:50 +08:00
|
|
|
ZM\Error("Unexpected text qualifier '".$matches[1]."' found for field '".$row['Field']."' in table '".$table."'");
|
2017-05-17 02:16:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
} elseif ( preg_match('/^(enum|set)\((.*)\)$/', $row['Type'], $matches) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['type'] = 'text';
|
|
|
|
$desc['typeAttrib'] = $matches[1];
|
2019-08-13 23:45:50 +08:00
|
|
|
preg_match_all("/'([^']+)'/", $matches[2], $matches);
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['values'] = $matches[1];
|
2019-08-13 23:45:50 +08:00
|
|
|
} elseif ( preg_match('/^(\w+)?int\(\d+\)(?:\s+(unsigned))?$/', $row['Type'], $matches) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['type'] = 'integer';
|
|
|
|
switch ( $matches[1] ) {
|
|
|
|
case 'tiny' :
|
|
|
|
$desc['minValue'] = -128;
|
|
|
|
$desc['maxValue'] = 127;
|
|
|
|
break;
|
|
|
|
case 'small' :
|
|
|
|
$desc['minValue'] = -32768;
|
|
|
|
$desc['maxValue'] = 32767;
|
|
|
|
break;
|
|
|
|
case 'medium' :
|
|
|
|
$desc['minValue'] = -8388608;
|
|
|
|
$desc['maxValue'] = 8388607;
|
|
|
|
break;
|
|
|
|
case '' :
|
|
|
|
$desc['minValue'] = -2147483648;
|
|
|
|
$desc['maxValue'] = 2147483647;
|
|
|
|
break;
|
|
|
|
case 'big' :
|
|
|
|
//$desc['minValue'] = -128;
|
|
|
|
//$desc['maxValue'] = 127;
|
|
|
|
break;
|
|
|
|
default :
|
2019-08-13 23:45:50 +08:00
|
|
|
ZM\Error("Unexpected integer qualifier '".$matches[1]."' found for field '".$row['Field']."' in table '".$table."'");
|
2017-05-17 02:16:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( !empty($matches[1]) )
|
|
|
|
$desc['typeAttrib'] = $matches[1];
|
|
|
|
if ( $desc['unsigned'] = ( isset($matches[2]) && $matches[2] == 'unsigned' ) ) {
|
|
|
|
$desc['maxValue'] += (-$desc['minValue']);
|
|
|
|
$desc['minValue'] = 0;
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
} elseif ( preg_match('/^(?:decimal|numeric)\((\d+)(?:,(\d+))?\)(?:\s+(unsigned))?$/', $row['Type'], $matches) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['type'] = 'fixed';
|
|
|
|
$desc['range'] = $matches[1];
|
|
|
|
if ( isset($matches[2]) )
|
|
|
|
$desc['precision'] = $matches[2];
|
|
|
|
else
|
|
|
|
$desc['precision'] = 0;
|
|
|
|
$desc['unsigned'] = ( isset($matches[3]) && $matches[3] == 'unsigned' );
|
2019-08-13 23:45:50 +08:00
|
|
|
} elseif ( preg_match('/^(datetime|timestamp|date|time)$/', $row['Type'], $matches) ) {
|
2017-05-17 02:16:32 +08:00
|
|
|
$desc['type'] = 'datetime';
|
|
|
|
switch ( $desc['typeAttrib'] = $matches[1] ) {
|
|
|
|
case 'datetime' :
|
|
|
|
case 'timestamp' :
|
|
|
|
$desc['hasDate'] = true;
|
|
|
|
$desc['hasTime'] = true;
|
|
|
|
break;
|
|
|
|
case 'date' :
|
|
|
|
$desc['hasDate'] = true;
|
|
|
|
$desc['hasTime'] = false;
|
|
|
|
break;
|
|
|
|
case 'time' :
|
|
|
|
$desc['hasDate'] = false;
|
|
|
|
$desc['hasTime'] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-13 23:45:50 +08:00
|
|
|
ZM\Error("Can't parse database type '".$row['Type']."' found for field '".$row['Field']."' in table '".$table."'");
|
2010-05-27 20:18:06 +08:00
|
|
|
}
|
2017-05-17 02:16:32 +08:00
|
|
|
|
|
|
|
if ( $asString )
|
|
|
|
$columns[$row['Field']] = $desc;
|
|
|
|
else
|
|
|
|
$columns[] = $desc;
|
|
|
|
}
|
2019-08-13 23:45:50 +08:00
|
|
|
return $columns;
|
2007-09-06 20:59:19 +08:00
|
|
|
}
|
2002-10-11 17:45:06 +08:00
|
|
|
?>
|