Commit 44526c56 authored by Carsten  Rose's avatar Carsten Rose
Browse files

DB Connection: initialize. E.g. 'set names utf8'

config.example.ini: DB_INIT
Database.php: fire a given DB_INIT string.
parent 3e4e714e
......@@ -6,6 +6,8 @@ DB_NAME = <DB>
DB_NAME_TEST = <TESTDB>
DB_INIT = set names utf8
SESSION_NAME = qfq
SQL_LOG = sql.log
......
......@@ -258,6 +258,7 @@ const SYSTEM_DB_SERVER = 'DB_SERVER';
const SYSTEM_DB_PASSWORD = 'DB_PASSWORD';
const SYSTEM_DB_NAME = 'DB_NAME';
const SYSTEM_DB_NAME_TEST = 'DB_NAME_TEST';
const SYSTEM_DB_INIT = 'DB_INIT';
const SYSTEM_SESSION_NAME = 'SESSION_NAME';
const SYSTEM_SQL_LOG = 'SQL_LOG'; // Logging to file
const SYSTEM_SQL_LOG_MODE = 'SQL_LOG_MODE'; // Mode, which statements to log.
......@@ -405,6 +406,7 @@ const FLAG_DYNAMIC_UPDATE = 'flagDynamicUpdate';
const QUERY_TYPE_SELECT = 'type: select,show,describe,explain';
const QUERY_TYPE_INSERT = 'type: insert';
const QUERY_TYPE_UPDATE = 'type: update,replace,delete';
const QUERY_TYPE_CONTROL = 'type: set';
//Regexp
//const REGEXP_DATE_INT = '^\d{4}-\d{2}-\d{2}$';
......
......@@ -69,6 +69,12 @@ class Database {
$this->mysqli = $this->dbConnect();
}
$this->sqlLog = $this->store->getVar(SYSTEM_SQL_LOG, STORE_SYSTEM);
// DB Init
$dbInit = $this->store->getVar(SYSTEM_DB_INIT, STORE_SYSTEM);
if ($dbInit !== false && $dbInit != '') {
$this->sql($dbInit);
}
}
......@@ -95,96 +101,6 @@ class Database {
return $mysqli;
}
/**
* Return the number of rows returned by the last call to execute().
*
* If execute() has never been called, returns FALSE.
*
* @return mixed Number of rows returned by last call to execute(). If Database::execute()
* has never been called prior a call to this method, false is returned.
*/
public function getRowCount() {
if ($this->mysqli_result == null) {
return false;
}
return $this->mysqli_result->num_rows;
}
/**
* Get the values for a given ENUM or SET column
*
* @param string $table name of the table
* @param string $columnName name of the column
*
* @throws UserFormException if the table or column does not exist, or is not of type ENUM or SET
* @return array
*/
public function getEnumSetValueList($table, $columnName) {
$columnDefinition = $this->getFieldDefinitionFromTable($table, $columnName);
$setEnumDefinition = $columnDefinition["Type"];
// $setEnumDefinition holds now a string like
// String: enum('','red','blue','green')
$len = mb_strlen($setEnumDefinition);
# "enum('" = 6, "set('" = 5
$tokenLength = strpos($setEnumDefinition, "'") + 1;
// count("enum('") == 6, count("')") == 2
$enumSetString = mb_substr($setEnumDefinition, $tokenLength, $len - (2 + $tokenLength));
// String: ','red','blue','green
if (($setEnumValueList = explode("','", $enumSetString)) === false) {
return array();
}
return $setEnumValueList;
}
/**
* Get database column definition.
*
* If the column is not found in the table, an exception is thrown.
*
* @param string $table name of the table
*
* @param string $columnName name of the column
* @return array the definition of the column as retrieved by Database::getTableDefinition().
*
* @throws \qfq\DbException
*/
private function getFieldDefinitionFromTable($table, $columnName) {
$tableDefinition = $this->getTableDefinition($table);
foreach ($tableDefinition AS $row) {
if ($row["Field"] == $columnName) {
return $row;
}
}
throw new DbException("Column name '$columnName' not found in table '$table'.", ERROR_DB_COLUMN_NOT_FOUND_IN_TABLE);
}
/**
* Get all column definitions for a table. Return Assoc Array:
*
* Field Type Null Key Default Extra
* --------------------------------------------------------------------------
* id bigint(20) NO PRI NULL auto_increment
* name varchar(128) YES NULL
* firstname varchar(128) YES NULL
* gender enum('','male','female') NO male
* groups set('','a','b','c') NO a
*
* @param string $table table to retrieve column definition from
*
* @return array column definition of table as returned by SHOW FIELDS FROM as associative array.
*/
public function getTableDefinition($table) {
return $this->sql("SHOW FIELDS FROM `$table`");
}
/**
* Fires query $sql and fetches result als assoc array (all modes but ROW_KEYS) or as num array (mode: ROW_EKYS). Throws exception.
*
......@@ -344,11 +260,11 @@ class Database {
if (false === ($result = $this->mysqli_stmt->get_result())) {
throw new DbException('[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error, ERROR_DB_EXECUTE);
}
$queryType = QUERY_TYPE_SELECT;
$queryType = QUERY_TYPE_SELECT;
$this->mysqli_result = $result;
$stat[DB_NUM_ROWS] = $this->mysqli_result->num_rows;
$count = $stat[DB_NUM_ROWS];
$msg = 'Read rows: ' . $stat[DB_NUM_ROWS];
$stat[DB_NUM_ROWS] = $this->mysqli_result->num_rows;
$count = $stat[DB_NUM_ROWS];
$msg = 'Read rows: ' . $stat[DB_NUM_ROWS];
break;
case 'INSERT':
$queryType = QUERY_TYPE_INSERT;
......@@ -360,11 +276,18 @@ class Database {
case 'UPDATE':
case 'REPLACE':
case 'DELETE':
$queryType = QUERY_TYPE_UPDATE;
$stat[DB_AFFECTED_ROWS] = $this->mysqli->affected_rows;
$count = $stat[DB_AFFECTED_ROWS];
$queryType = QUERY_TYPE_UPDATE;
$stat[DB_AFFECTED_ROWS] = $this->mysqli->affected_rows;
$count = $stat[DB_AFFECTED_ROWS];
$msg = 'Affected rows: ' . $count;
break;
case 'SET':
$queryType = QUERY_TYPE_CONTROL;
$stat[DB_AFFECTED_ROWS] = 0;
$count = $stat[DB_AFFECTED_ROWS];
$msg = '';
break;
default:
throw new DbException('Unknown comand: "' . $command . '"', ERROR_DB_UNKNOWN_COMMAND);
break;
......@@ -510,6 +433,96 @@ class Database {
}
}
/**
* Return the number of rows returned by the last call to execute().
*
* If execute() has never been called, returns FALSE.
*
* @return mixed Number of rows returned by last call to execute(). If Database::execute()
* has never been called prior a call to this method, false is returned.
*/
public function getRowCount() {
if ($this->mysqli_result == null) {
return false;
}
return $this->mysqli_result->num_rows;
}
/**
* Get the values for a given ENUM or SET column
*
* @param string $table name of the table
* @param string $columnName name of the column
*
* @throws UserFormException if the table or column does not exist, or is not of type ENUM or SET
* @return array
*/
public function getEnumSetValueList($table, $columnName) {
$columnDefinition = $this->getFieldDefinitionFromTable($table, $columnName);
$setEnumDefinition = $columnDefinition["Type"];
// $setEnumDefinition holds now a string like
// String: enum('','red','blue','green')
$len = mb_strlen($setEnumDefinition);
# "enum('" = 6, "set('" = 5
$tokenLength = strpos($setEnumDefinition, "'") + 1;
// count("enum('") == 6, count("')") == 2
$enumSetString = mb_substr($setEnumDefinition, $tokenLength, $len - (2 + $tokenLength));
// String: ','red','blue','green
if (($setEnumValueList = explode("','", $enumSetString)) === false) {
return array();
}
return $setEnumValueList;
}
/**
* Get database column definition.
*
* If the column is not found in the table, an exception is thrown.
*
* @param string $table name of the table
*
* @param string $columnName name of the column
* @return array the definition of the column as retrieved by Database::getTableDefinition().
*
* @throws \qfq\DbException
*/
private function getFieldDefinitionFromTable($table, $columnName) {
$tableDefinition = $this->getTableDefinition($table);
foreach ($tableDefinition AS $row) {
if ($row["Field"] == $columnName) {
return $row;
}
}
throw new DbException("Column name '$columnName' not found in table '$table'.", ERROR_DB_COLUMN_NOT_FOUND_IN_TABLE);
}
/**
* Get all column definitions for a table. Return Assoc Array:
*
* Field Type Null Key Default Extra
* --------------------------------------------------------------------------
* id bigint(20) NO PRI NULL auto_increment
* name varchar(128) YES NULL
* firstname varchar(128) YES NULL
* gender enum('','male','female') NO male
* groups set('','a','b','c') NO a
*
* @param string $table table to retrieve column definition from
*
* @return array column definition of table as returned by SHOW FIELDS FROM as associative array.
*/
public function getTableDefinition($table) {
return $this->sql("SHOW FIELDS FROM `$table`");
}
/**
* Wrapper for sql(), to simplyfy access.
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment