Current oav website

This commit is contained in:
Charlie Root
2023-03-20 12:18:38 +01:00
commit a096ce07cf
3270 changed files with 261778 additions and 0 deletions

View File

@ -0,0 +1,250 @@
<?php
/**
* @class cursor
* @brief DBLayer Cursor
*
* This class implements facilities to insert or update in a table.
*
* @package Clearbricks
* @subpackage DBLayer
*
* @copyright Olivier Meunier & Association Dotclear
* @copyright GPL-2.0-only
*/
class cursor
{
private $__con;
private $__data = [];
private $__table;
/**
* Constructor
*
* Init cursor object on a given table. Note that you can init it with
* {@link dbLayer::openCursor() openCursor()} method of your connection object.
*
* Example:
* <code>
* <?php
* $cur = $con->openCursor('table');
* $cur->field1 = 1;
* $cur->field2 = 'foo';
* $cur->insert(); // Insert field ...
*
* $cur->update('WHERE field3 = 4'); // ... or update field
* ?>
* </code>
*
* @see dbLayer::openCursor()
* @param dbLayer &$con Connection object
* @param string $table Table name
*/
public function __construct($con, $table)
{
$this->__con = &$con;
$this->setTable($table);
}
/**
* Set table
*
* Changes working table and resets data
*
* @param string $table Table name
*/
public function setTable($table)
{
$this->__table = $table;
$this->__data = [];
}
/**
* Set field
*
* Set value <var>$v</var> to a field named <var>$n</var>. Value could be
* an string, an integer, a float, a null value or an array.
*
* If value is an array, its first value will be interpreted as a SQL
* command. String values will be automatically escaped.
*
* @see __set()
* @param string $n Field name
* @param mixed $v Field value
*/
public function setField($n, $v)
{
$this->__data[$n] = $v;
}
/**
* Unset field
*
* Remove a field from data set.
*
* @param string $n Field name
*/
public function unsetField($n)
{
unset($this->__data[$n]);
}
/**
* Field exists
*
* @return boolean true if field named <var>$n</var> exists
*/
public function isField($n)
{
return isset($this->__data[$n]);
}
/**
* Field value
*
* @see __get()
* @return mixed value for a field named <var>$n</var>
*/
public function getField($n)
{
if (isset($this->__data[$n])) {
return $this->__data[$n];
}
return;
}
/**
* Set Field
*
* Magic alias for {@link setField()}
*/
public function __set($n, $v)
{
$this->setField($n, $v);
}
/**
* Field value
*
* Magic alias for {@link getField()}
*
* @return mixed value for a field named <var>$n</var>
*/
public function __get($n)
{
return $this->getField($n);
}
/**
* Empty data set
*
* Removes all data from data set
*/
public function clean()
{
$this->__data = [];
}
private function formatFields()
{
$data = [];
foreach ($this->__data as $k => $v) {
$k = $this->__con->escapeSystem($k);
if (is_null($v)) {
$data[$k] = 'NULL';
} elseif (is_string($v)) {
$data[$k] = "'" . $this->__con->escape($v) . "'";
} elseif (is_array($v)) {
$data[$k] = is_string($v[0]) ? "'" . $this->__con->escape($v[0]) . "'" : $v[0];
} else {
$data[$k] = $v;
}
}
return $data;
}
/**
* Get insert query
*
* Returns the generated INSERT query
*
* @return string
*/
public function getInsert()
{
$data = $this->formatFields();
$insReq = 'INSERT INTO ' . $this->__con->escapeSystem($this->__table) . " (\n" .
implode(",\n", array_keys($data)) . "\n) VALUES (\n" .
implode(",\n", array_values($data)) . "\n) ";
return $insReq;
}
/**
* Get update query
*
* Returns the generated UPDATE query
*
* @param string $where WHERE condition
* @return string
*/
public function getUpdate($where)
{
$data = $this->formatFields();
$fields = [];
$updReq = 'UPDATE ' . $this->__con->escapeSystem($this->__table) . " SET \n";
foreach ($data as $k => $v) {
$fields[] = $k . ' = ' . $v . "";
}
$updReq .= implode(",\n", $fields);
$updReq .= "\n" . $where;
return $updReq;
}
/**
* Execute insert query
*
* Executes the generated INSERT query
*/
public function insert()
{
if (!$this->__table) {
throw new Exception('No table name.');
}
$insReq = $this->getInsert();
$this->__con->execute($insReq);
return true;
}
/**
* Execute update query
*
* Executes the generated UPDATE query
*
* @param string $where WHERE condition
*/
public function update($where)
{
if (!$this->__table) {
throw new Exception('No table name.');
}
$updReq = $this->getUpdate($where);
$this->__con->execute($updReq);
return true;
}
}

View File

@ -0,0 +1,256 @@
<?php
/**
* @class mysqlConnection
* @brief MySQL Database Driver
*
* See the {@link dbLayer} documentation for common methods.
*
* @package Clearbricks
* @subpackage DBLayer
*
* @copyright Olivier Meunier & Association Dotclear
* @copyright GPL-2.0-only
*/
/** @cond ONCE */
if (class_exists('dbLayer')) {
/** @endcond */
class mysqlConnection extends dbLayer implements i_dbLayer
{
public static $weak_locks = false; ///< boolean: Enables weak locks if true
protected $__driver = 'mysql';
protected $__syntax = 'mysql';
public function db_connect($host, $user, $password, $database)
{
if (!function_exists('mysql_connect')) {
throw new Exception('PHP MySQL functions are not available');
}
if (($link = @mysql_connect($host, $user, $password, true)) === false) {
throw new Exception('Unable to connect to database');
}
$this->db_post_connect($link, $database);
return $link;
}
public function db_pconnect($host, $user, $password, $database)
{
if (!function_exists('mysql_pconnect')) {
throw new Exception('PHP MySQL functions are not available');
}
if (($link = @mysql_pconnect($host, $user, $password)) === false) {
throw new Exception('Unable to connect to database');
}
$this->db_post_connect($link, $database);
return $link;
}
private function db_post_connect($link, $database)
{
if (@mysql_select_db($database, $link) === false) {
throw new Exception('Unable to use database ' . $database);
}
if (version_compare($this->db_version($link), '4.1', '>=')) {
$this->db_query($link, 'SET NAMES utf8');
$this->db_query($link, 'SET CHARACTER SET utf8');
$this->db_query($link, "SET COLLATION_CONNECTION = 'utf8_general_ci'");
$this->db_query($link, "SET COLLATION_SERVER = 'utf8_general_ci'");
$this->db_query($link, "SET CHARACTER_SET_SERVER = 'utf8'");
$this->db_query($link, "SET CHARACTER_SET_DATABASE = 'utf8'");
}
}
public function db_close($handle)
{
if (is_resource($handle)) {
mysql_close($handle);
}
}
public function db_version($handle)
{
if (is_resource($handle)) {
return mysql_get_server_info();
}
return;
}
public function db_query($handle, $query)
{
if (is_resource($handle)) {
$res = @mysql_query($query, $handle);
if ($res === false) {
$e = new Exception($this->db_last_error($handle));
$e->sql = $query;
throw $e;
}
return $res;
}
}
public function db_exec($handle, $query)
{
return $this->db_query($handle, $query);
}
public function db_num_fields($res)
{
if (is_resource($res)) {
return mysql_num_fields($res);
}
return 0;
}
public function db_num_rows($res)
{
if (is_resource($res)) {
return mysql_num_rows($res);
}
return 0;
}
public function db_field_name($res, $position)
{
if (is_resource($res)) {
return mysql_field_name($res, $position);
}
}
public function db_field_type($res, $position)
{
if (is_resource($res)) {
return mysql_field_type($res, $position);
}
}
public function db_fetch_assoc($res)
{
if (is_resource($res)) {
return mysql_fetch_assoc($res);
}
}
public function db_result_seek($res, $row)
{
if (is_resource($res)) {
return mysql_data_seek($res, $row);
}
}
public function db_changes($handle, $res)
{
if (is_resource($handle)) {
return mysql_affected_rows($handle);
}
}
public function db_last_error($handle)
{
if (is_resource($handle)) {
$e = mysql_error($handle);
if ($e) {
return $e . ' (' . mysql_errno($handle) . ')';
}
}
return false;
}
public function db_escape_string($str, $handle = null)
{
if (is_resource($handle)) {
return mysql_real_escape_string($str, $handle);
} else {
return mysql_escape_string($str);
}
}
public function db_write_lock($table)
{
try {
$this->execute('LOCK TABLES ' . $this->escapeSystem($table) . ' WRITE');
} catch (Exception $e) {
# As lock is a privilege in MySQL, we can avoid errors with weak_locks static var
if (!self::$weak_locks) {
throw $e;
}
}
}
public function db_unlock()
{
try {
$this->execute('UNLOCK TABLES');
} catch (Exception $e) {
if (!self::$weak_locks) {
throw $e;
}
}
}
public function vacuum($table)
{
$this->execute('OPTIMIZE TABLE ' . $this->escapeSystem($table));
}
public function dateFormat($field, $pattern)
{
$pattern = str_replace('%M', '%i', $pattern);
return 'DATE_FORMAT(' . $field . ',' . "'" . $this->escape($pattern) . "') ";
}
public function orderBy()
{
$default = [
'order' => '',
'collate' => false
];
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = $v;
} elseif (is_array($v) && !empty($v['field'])) {
$v = array_merge($default, $v);
$v['order'] = (strtoupper($v['order']) == 'DESC' ? 'DESC' : '');
$res[] = $v['field'] . ($v['collate'] ? ' COLLATE utf8_unicode_ci' : '') . ' ' . $v['order'];
}
}
return empty($res) ? '' : ' ORDER BY ' . implode(',', $res) . ' ';
}
public function lexFields()
{
$fmt = '%s COLLATE utf8_unicode_ci';
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = sprintf($fmt, $v);
} elseif (is_array($v)) {
$res = array_map(function ($i) use ($fmt) {return sprintf($fmt, $i);}, $v);
}
}
return empty($res) ? '' : implode(',', $res);
}
public function concat()
{
$args = func_get_args();
return 'CONCAT(' . implode(',', $args) . ')';
}
public function escapeSystem($str)
{
return '`' . $str . '`';
}
}
/** @cond ONCE */
}
/** @endcond */

View File

@ -0,0 +1,307 @@
<?php
/**
* @class mysqliConnection
* @brief MySQLi Database Driver
*
* See the {@link dbLayer} documentation for common methods.
*
* @package Clearbricks
* @subpackage DBLayer
*
* @copyright Olivier Meunier & Association Dotclear
* @copyright GPL-2.0-only
*/
/** @cond ONCE */
if (class_exists('dbLayer')) {
/** @endcond */
class mysqliConnection extends dbLayer implements i_dbLayer
{
public static $weak_locks = true; ///< boolean: Enables weak locks if true
protected $__driver = 'mysqli';
protected $__syntax = 'mysql';
public function db_connect($host, $user, $password, $database)
{
if (!function_exists('mysqli_connect')) {
throw new Exception('PHP MySQLi functions are not available');
}
$port = ini_get("mysqli.default_port");
$socket = null;
if (strpos($host, ':') !== false) {
// Port or socket given
$bits = explode(':', $host);
$host = array_shift($bits);
$socket = array_shift($bits);
if (abs((integer) $socket) > 0) {
// TCP/IP connection on given port
$port = abs((integer) $socket);
$socket = null;
} else {
// Socket connection
$port = null;
}
}
if (($link = @mysqli_connect($host, $user, $password, $database, $port, $socket)) === false) {
throw new Exception('Unable to connect to database');
}
$this->db_post_connect($link, $database);
return $link;
}
public function db_pconnect($host, $user, $password, $database)
{
// No pconnect wtih mysqli, below code is for comatibility
return $this->db_connect($host, $user, $password, $database);
}
private function db_post_connect($link, $database)
{
if (version_compare($this->db_version($link), '4.1', '>=')) {
$this->db_query($link, 'SET NAMES utf8');
$this->db_query($link, 'SET CHARACTER SET utf8');
$this->db_query($link, "SET COLLATION_CONNECTION = 'utf8_general_ci'");
$this->db_query($link, "SET COLLATION_SERVER = 'utf8_general_ci'");
$this->db_query($link, "SET CHARACTER_SET_SERVER = 'utf8'");
if (version_compare($this->db_version($link), '8.0', '<')) {
// Setting CHARACTER_SET_DATABASE is obosolete for MySQL 8.0+
$this->db_query($link, "SET CHARACTER_SET_DATABASE = 'utf8'");
}
$link->set_charset("utf8");
}
}
public function db_close($handle)
{
if ($handle instanceof MySQLi) {
mysqli_close($handle);
}
}
public function db_version($handle)
{
if ($handle instanceof MySQLi) {
return mysqli_get_server_info($handle);
}
return;
}
public function db_query($handle, $query)
{
if ($handle instanceof MySQLi) {
$res = @mysqli_query($handle, $query);
if ($res === false) {
$e = new Exception($this->db_last_error($handle));
$e->sql = $query;
throw $e;
}
return $res;
}
}
public function db_exec($handle, $query)
{
return $this->db_query($handle, $query);
}
public function db_num_fields($res)
{
if ($res instanceof MySQLi_Result) {
//return mysql_num_fields($res);
return $res->field_count;
}
return 0;
}
public function db_num_rows($res)
{
if ($res instanceof MySQLi_Result) {
return $res->num_rows;
}
return 0;
}
public function db_field_name($res, $position)
{
if ($res instanceof MySQLi_Result) {
$res->field_seek($position);
$finfo = $res->fetch_field();
return $finfo->name;
}
}
public function db_field_type($res, $position)
{
if ($res instanceof MySQLi_Result) {
$res->field_seek($position);
$finfo = $res->fetch_field();
return $this->_convert_types($finfo->type);
}
}
public function db_fetch_assoc($res)
{
if ($res instanceof MySQLi_Result) {
$v = $res->fetch_assoc();
return ($v === null) ? false : $v;
}
}
public function db_result_seek($res, $row)
{
if ($res instanceof MySQLi_Result) {
return $res->data_seek($row);
}
}
public function db_changes($handle, $res)
{
if ($handle instanceof MySQLi) {
return mysqli_affected_rows($handle);
}
}
public function db_last_error($handle)
{
if ($handle instanceof MySQLi) {
$e = mysqli_error($handle);
if ($e) {
return $e . ' (' . mysqli_errno($handle) . ')';
}
}
return false;
}
public function db_escape_string($str, $handle = null)
{
if ($handle instanceof MySQLi) {
return mysqli_real_escape_string($handle, $str);
}
return addslashes($str);
}
public function db_write_lock($table)
{
try {
$this->execute('LOCK TABLES ' . $this->escapeSystem($table) . ' WRITE');
} catch (Exception $e) {
# As lock is a privilege in MySQL, we can avoid errors with weak_locks static var
if (!self::$weak_locks) {
throw $e;
}
}
}
public function db_unlock()
{
try {
$this->execute('UNLOCK TABLES');
} catch (Exception $e) {
if (!self::$weak_locks) {
throw $e;
}
}
}
public function vacuum($table)
{
$this->execute('OPTIMIZE TABLE ' . $this->escapeSystem($table));
}
public function dateFormat($field, $pattern)
{
$pattern = str_replace('%M', '%i', $pattern);
return 'DATE_FORMAT(' . $field . ',' . "'" . $this->escape($pattern) . "') ";
}
public function orderBy()
{
$default = [
'order' => '',
'collate' => false
];
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = $v;
} elseif (is_array($v) && !empty($v['field'])) {
$v = array_merge($default, $v);
$v['order'] = (strtoupper($v['order']) == 'DESC' ? 'DESC' : '');
$res[] = $v['field'] . ($v['collate'] ? ' COLLATE utf8_unicode_ci' : '') . ' ' . $v['order'];
}
}
return empty($res) ? '' : ' ORDER BY ' . implode(',', $res) . ' ';
}
public function lexFields()
{
$fmt = '%s COLLATE utf8_unicode_ci';
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = sprintf($fmt, $v);
} elseif (is_array($v)) {
$res = array_map(function ($i) use ($fmt) {return sprintf($fmt, $i);}, $v);
}
}
return empty($res) ? '' : implode(',', $res);
}
public function concat()
{
$args = func_get_args();
return 'CONCAT(' . implode(',', $args) . ')';
}
public function escapeSystem($str)
{
return '`' . $str . '`';
}
protected function _convert_types($id)
{
$id2type = [
'1' => 'int',
'2' => 'int',
'3' => 'int',
'8' => 'int',
'9' => 'int',
'16' => 'int', //BIT type recognized as unknown with mysql adapter
'4' => 'real',
'5' => 'real',
'246' => 'real',
'253' => 'string',
'254' => 'string',
'10' => 'date',
'11' => 'time',
'12' => 'datetime',
'13' => 'year',
'7' => 'timestamp',
'252' => 'blob'
];
$type = 'unknown';
if (isset($id2type[$id])) {
$type = $id2type[$id];
}
return $type;
}
}
/** @cond ONCE */
}
/** @endcond */

View File

@ -0,0 +1,309 @@
<?php
/**
* @class mysqlimb4Connection
* @brief MySQLi utf8-mb4 Database Driver
*
* See the {@link dbLayer} documentation for common methods.
*
* @package Clearbricks
* @subpackage DBLayer
*
* @copyright Olivier Meunier & Association Dotclear
* @copyright GPL-2.0-only
*/
/** @cond ONCE */
if (class_exists('dbLayer')) {
/** @endcond */
class mysqlimb4Connection extends dbLayer implements i_dbLayer
{
public static $weak_locks = true; ///< boolean: Enables weak locks if true
protected $__driver = 'mysqlimb4';
protected $__syntax = 'mysql';
public function db_connect($host, $user, $password, $database)
{
if (!function_exists('mysqli_connect')) {
throw new Exception('PHP MySQLi functions are not available');
}
$port = ini_get("mysqli.default_port");
$socket = null;
if (strpos($host, ':') !== false) {
// Port or socket given
$bits = explode(':', $host);
$host = array_shift($bits);
$socket = array_shift($bits);
if (abs((integer) $socket) > 0) {
// TCP/IP connection on given port
$port = abs((integer) $socket);
$socket = null;
} else {
// Socket connection
$port = null;
}
}
if (($link = @mysqli_connect($host, $user, $password, $database, $port, $socket)) === false) {
throw new Exception('Unable to connect to database');
}
$this->db_post_connect($link, $database);
return $link;
}
public function db_pconnect($host, $user, $password, $database)
{
// No pconnect wtih mysqli, below code is for comatibility
return $this->db_connect($host, $user, $password, $database);
}
private function db_post_connect($link, $database)
{
if (version_compare($this->db_version($link), '5.7.7', '>=')) {
$this->db_query($link, 'SET NAMES utf8mb4');
$this->db_query($link, 'SET CHARACTER SET utf8mb4');
$this->db_query($link, "SET COLLATION_CONNECTION = 'utf8mb4_unicode_ci'");
$this->db_query($link, "SET COLLATION_SERVER = 'utf8mb4_unicode_ci'");
$this->db_query($link, "SET CHARACTER_SET_SERVER = 'utf8mb4'");
if (version_compare($this->db_version($link), '8.0', '<')) {
// Setting CHARACTER_SET_DATABASE is obosolete for MySQL 8.0+
$this->db_query($link, "SET CHARACTER_SET_DATABASE = 'utf8mb4'");
}
$link->set_charset("utf8mb4");
} else {
throw new Exception('Unable to connect to an utf8mb4 database');
}
}
public function db_close($handle)
{
if ($handle instanceof MySQLi) {
mysqli_close($handle);
}
}
public function db_version($handle)
{
if ($handle instanceof MySQLi) {
return mysqli_get_server_info($handle);
}
return;
}
public function db_query($handle, $query)
{
if ($handle instanceof MySQLi) {
$res = @mysqli_query($handle, $query);
if ($res === false) {
$e = new Exception($this->db_last_error($handle));
$e->sql = $query;
throw $e;
}
return $res;
}
}
public function db_exec($handle, $query)
{
return $this->db_query($handle, $query);
}
public function db_num_fields($res)
{
if ($res instanceof MySQLi_Result) {
//return mysql_num_fields($res);
return $res->field_count;
}
return 0;
}
public function db_num_rows($res)
{
if ($res instanceof MySQLi_Result) {
return $res->num_rows;
}
return 0;
}
public function db_field_name($res, $position)
{
if ($res instanceof MySQLi_Result) {
$res->field_seek($position);
$finfo = $res->fetch_field();
return $finfo->name;
}
}
public function db_field_type($res, $position)
{
if ($res instanceof MySQLi_Result) {
$res->field_seek($position);
$finfo = $res->fetch_field();
return $this->_convert_types($finfo->type);
}
}
public function db_fetch_assoc($res)
{
if ($res instanceof MySQLi_Result) {
$v = $res->fetch_assoc();
return ($v === null) ? false : $v;
}
}
public function db_result_seek($res, $row)
{
if ($res instanceof MySQLi_Result) {
return $res->data_seek($row);
}
}
public function db_changes($handle, $res)
{
if ($handle instanceof MySQLi) {
return mysqli_affected_rows($handle);
}
}
public function db_last_error($handle)
{
if ($handle instanceof MySQLi) {
$e = mysqli_error($handle);
if ($e) {
return $e . ' (' . mysqli_errno($handle) . ')';
}
}
return false;
}
public function db_escape_string($str, $handle = null)
{
if ($handle instanceof MySQLi) {
return mysqli_real_escape_string($handle, $str);
}
return addslashes($str);
}
public function db_write_lock($table)
{
try {
$this->execute('LOCK TABLES ' . $this->escapeSystem($table) . ' WRITE');
} catch (Exception $e) {
# As lock is a privilege in MySQL, we can avoid errors with weak_locks static var
if (!self::$weak_locks) {
throw $e;
}
}
}
public function db_unlock()
{
try {
$this->execute('UNLOCK TABLES');
} catch (Exception $e) {
if (!self::$weak_locks) {
throw $e;
}
}
}
public function vacuum($table)
{
$this->execute('OPTIMIZE TABLE ' . $this->escapeSystem($table));
}
public function dateFormat($field, $pattern)
{
$pattern = str_replace('%M', '%i', $pattern);
return 'DATE_FORMAT(' . $field . ',' . "'" . $this->escape($pattern) . "') ";
}
public function orderBy()
{
$default = [
'order' => '',
'collate' => false
];
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = $v;
} elseif (is_array($v) && !empty($v['field'])) {
$v = array_merge($default, $v);
$v['order'] = (strtoupper($v['order']) == 'DESC' ? 'DESC' : '');
$res[] = $v['field'] . ($v['collate'] ? ' COLLATE utf8mb4_unicode_ci' : '') . ' ' . $v['order'];
}
}
return empty($res) ? '' : ' ORDER BY ' . implode(',', $res) . ' ';
}
public function lexFields()
{
$fmt = '%s COLLATE utf8mb4_unicode_ci';
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = sprintf($fmt, $v);
} elseif (is_array($v)) {
$res = array_map(function ($i) use ($fmt) {return sprintf($fmt, $i);}, $v);
}
}
return empty($res) ? '' : implode(',', $res);
}
public function concat()
{
$args = func_get_args();
return 'CONCAT(' . implode(',', $args) . ')';
}
public function escapeSystem($str)
{
return '`' . $str . '`';
}
protected function _convert_types($id)
{
$id2type = [
'1' => 'int',
'2' => 'int',
'3' => 'int',
'8' => 'int',
'9' => 'int',
'16' => 'int', //BIT type recognized as unknown with mysql adapter
'4' => 'real',
'5' => 'real',
'246' => 'real',
'253' => 'string',
'254' => 'string',
'10' => 'date',
'11' => 'time',
'12' => 'datetime',
'13' => 'year',
'7' => 'timestamp',
'252' => 'blob'
];
$type = 'unknown';
if (isset($id2type[$id])) {
$type = $id2type[$id];
}
return $type;
}
}
/** @cond ONCE */
}
/** @endcond */

View File

@ -0,0 +1,312 @@
<?php
/**
* @class pgsqlConnection
* @brief PostgreSQL Database Driver
*
* See the {@link dbLayer} documentation for common methods.
*
* This class adds a method for PostgreSQL only: {@link callFunction()}.
*
* @package Clearbricks
* @subpackage DBLayer
*
* @copyright Olivier Meunier & Association Dotclear
* @copyright GPL-2.0-only
*/
/** @cond ONCE */
if (class_exists('dbLayer')) {
/** @endcond */
class pgsqlConnection extends dbLayer implements i_dbLayer
{
protected $__driver = 'pgsql';
protected $__syntax = 'postgresql';
protected $utf8_unicode_ci = null;
private function get_connection_string($host, $user, $password, $database)
{
$str = '';
$port = false;
if ($host) {
if (strpos($host, ':') !== false) {
$bits = explode(':', $host);
$host = array_shift($bits);
$port = abs((integer) array_shift($bits));
}
$str .= "host = '" . addslashes($host) . "' ";
if ($port) {
$str .= 'port = ' . $port . ' ';
}
}
if ($user) {
$str .= "user = '" . addslashes($user) . "' ";
}
if ($password) {
$str .= "password = '" . addslashes($password) . "' ";
}
if ($database) {
$str .= "dbname = '" . addslashes($database) . "' ";
}
return $str;
}
public function db_connect($host, $user, $password, $database)
{
if (!function_exists('pg_connect')) {
throw new Exception('PHP PostgreSQL functions are not available');
}
$str = $this->get_connection_string($host, $user, $password, $database);
if (($link = @pg_connect($str)) === false) {
throw new Exception('Unable to connect to database');
}
$this->db_post_connect($link, $database);
return $link;
}
public function db_pconnect($host, $user, $password, $database)
{
if (!function_exists('pg_pconnect')) {
throw new Exception('PHP PostgreSQL functions are not available');
}
$str = $this->get_connection_string($host, $user, $password, $database);
if (($link = @pg_pconnect($str)) === false) {
throw new Exception('Unable to connect to database');
}
$this->db_post_connect($link, $database);
return $link;
}
private function db_post_connect($handle, $database)
{
if (version_compare($this->db_version($handle), '9.1') >= 0) {
// Only for PostgreSQL 9.1+
$result = $this->db_query($handle, "SELECT * FROM pg_collation WHERE (collcollate LIKE '%.utf8')");
if ($this->db_num_rows($result) > 0) {
$this->db_result_seek($result, 0);
$row = $this->db_fetch_assoc($result);
$this->utf8_unicode_ci = '"' . $row['collname'] . '"';
}
}
}
public function db_close($handle)
{
if (is_resource($handle)) {
pg_close($handle);
}
}
public function db_version($handle)
{
if (is_resource($handle)) {
return pg_parameter_status($handle, 'server_version');
}
return;
}
public function db_query($handle, $query)
{
if (is_resource($handle)) {
$res = @pg_query($handle, $query);
if ($res === false) {
$e = new Exception($this->db_last_error($handle));
$e->sql = $query;
throw $e;
}
return $res;
}
}
public function db_exec($handle, $query)
{
return $this->db_query($handle, $query);
}
public function db_num_fields($res)
{
if (is_resource($res)) {
return pg_num_fields($res);
}
return 0;
}
public function db_num_rows($res)
{
if (is_resource($res)) {
return pg_num_rows($res);
}
return 0;
}
public function db_field_name($res, $position)
{
if (is_resource($res)) {
return pg_field_name($res, $position);
}
}
public function db_field_type($res, $position)
{
if (is_resource($res)) {
return pg_field_type($res, $position);
}
}
public function db_fetch_assoc($res)
{
if (is_resource($res)) {
return pg_fetch_assoc($res);
}
}
public function db_result_seek($res, $row)
{
if (is_resource($res)) {
return pg_result_seek($res, (int) $row);
}
return false;
}
public function db_changes($handle, $res)
{
if (is_resource($handle) && is_resource($res)) {
return pg_affected_rows($res);
}
}
public function db_last_error($handle)
{
if (is_resource($handle)) {
return pg_last_error($handle);
}
return false;
}
public function db_escape_string($str, $handle = null)
{
return pg_escape_string($str);
}
public function db_write_lock($table)
{
$this->execute('BEGIN');
$this->execute('LOCK TABLE ' . $this->escapeSystem($table) . ' IN EXCLUSIVE MODE');
}
public function db_unlock()
{
$this->execute('END');
}
public function vacuum($table)
{
$this->execute('VACUUM FULL ' . $this->escapeSystem($table));
}
public function dateFormat($field, $pattern)
{
$rep = [
'%d' => 'DD',
'%H' => 'HH24',
'%M' => 'MI',
'%m' => 'MM',
'%S' => 'SS',
'%Y' => 'YYYY'
];
$pattern = str_replace(array_keys($rep), array_values($rep), $pattern);
return 'TO_CHAR(' . $field . ',' . "'" . $this->escape($pattern) . "') ";
}
public function orderBy()
{
$default = [
'order' => '',
'collate' => false
];
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = $v;
} elseif (is_array($v) && !empty($v['field'])) {
$v = array_merge($default, $v);
$v['order'] = (strtoupper($v['order']) == 'DESC' ? 'DESC' : '');
if ($v['collate']) {
if ($this->utf8_unicode_ci) {
$res[] = $v['field'] . ' COLLATE ' . $this->utf8_unicode_ci . ' ' . $v['order'];
} else {
$res[] = 'LOWER(' . $v['field'] . ') ' . $v['order'];
}
} else {
$res[] = $v['field'] . ' ' . $v['order'];
}
}
}
return empty($res) ? '' : ' ORDER BY ' . implode(',', $res) . ' ';
}
public function lexFields()
{
$fmt = $this->utf8_unicode_ci ? '%s COLLATE ' . $this->utf8_unicode_ci : 'LOWER(%s)';
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = sprintf($fmt, $v);
} elseif (is_array($v)) {
$res = array_map(function ($i) use ($fmt) {return sprintf($fmt, $i);}, $v);
}
}
return empty($res) ? '' : implode(',', $res);
}
/**
* Function call
*
* Calls a PostgreSQL function an returns the result as a {@link record}.
* After <var>$name</var>, you can add any parameters you want to append
* them to the PostgreSQL function. You don't need to escape string in
* arguments.
*
* @param string $name Function name
* @return record
*/
public function callFunction($name)
{
$data = func_get_args();
array_shift($data);
foreach ($data as $k => $v) {
if (is_null($v)) {
$data[$k] = 'NULL';
} elseif (is_string($v)) {
$data[$k] = "'" . $this->escape($v) . "'";
} elseif (is_array($v)) {
$data[$k] = $v[0];
} else {
$data[$k] = $v;
}
}
$req =
'SELECT ' . $name . "(\n" .
implode(",\n", array_values($data)) .
"\n) ";
return $this->select($req);
}
}
/** @cond ONCE */
}
/** @endcond */

View File

@ -0,0 +1,294 @@
<?php
/**
* @class sqliteConnection
* @brief SQLite Database Driver
*
* See the {@link dbLayer} documentation for common methods.
*
* @package Clearbricks
* @subpackage DBLayer
*
* @copyright Olivier Meunier & Association Dotclear
* @copyright GPL-2.0-only
*/
/** @cond ONCE */
if (class_exists('dbLayer')) {
/** @endcond */
class sqliteConnection extends dbLayer implements i_dbLayer
{
protected $__driver = 'sqlite';
protected $__syntax = 'sqlite';
protected $utf8_unicode_ci = null;
public function db_connect($host, $user, $password, $database)
{
if (!class_exists('PDO') || !in_array('sqlite', PDO::getAvailableDrivers())) {
throw new Exception('PDO SQLite class is not available');
}
$link = new PDO('sqlite:' . $database);
$this->db_post_connect($link, $database);
return $link;
}
public function db_pconnect($host, $user, $password, $database)
{
if (!class_exists('PDO') || !in_array('sqlite', PDO::getAvailableDrivers())) {
throw new Exception('PDO SQLite class is not available');
}
$link = new PDO('sqlite:' . $database, null, null, [PDO::ATTR_PERSISTENT => true]);
$this->db_post_connect($link, $database);
return $link;
}
private function db_post_connect($handle, $database)
{
if ($handle instanceof PDO) {
$this->db_exec($handle, 'PRAGMA short_column_names = 1');
$this->db_exec($handle, 'PRAGMA encoding = "UTF-8"');
$handle->sqliteCreateFunction('now', [$this, 'now'], 0);
if (class_exists('Collator') && method_exists($handle, 'sqliteCreateCollation')) {
$this->utf8_unicode_ci = new Collator('root');
if (!$handle->sqliteCreateCollation('utf8_unicode_ci', [$this->utf8_unicode_ci, 'compare'])) {
$this->utf8_unicode_ci = null;
}
}
}
}
public function db_close($handle)
{
if ($handle instanceof PDO) {
$handle = null;
$this->__link = null;
}
}
public function db_version($handle)
{
if ($handle instanceof PDO) {
return $handle->getAttribute(PDO::ATTR_SERVER_VERSION);
}
}
# There is no other way than get all selected data in a staticRecord
public function select($sql)
{
$result = $this->db_query($this->__link, $sql);
$this->__last_result = &$result;
$info = [];
$info['con'] = &$this;
$info['cols'] = $this->db_num_fields($result);
$info['info'] = [];
for ($i = 0; $i < $info['cols']; $i++) {
$info['info']['name'][] = $this->db_field_name($result, $i);
$info['info']['type'][] = $this->db_field_type($result, $i);
}
$data = [];
while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
$R = [];
foreach ($r as $k => $v) {
$k = preg_replace('/^(.*)\./', '', $k);
$R[$k] = $v;
$R[] = &$R[$k];
}
$data[] = $R;
}
$info['rows'] = count($data);
$result->closeCursor();
return new staticRecord($data, $info);
}
public function db_query($handle, $query)
{
if ($handle instanceof PDO) {
$res = $handle->query($query);
if ($res === false) {
$e = new Exception($this->db_last_error($handle));
$e->sql = $query;
throw $e;
}
return $res;
}
}
public function db_exec($handle, $query)
{
return $this->db_query($handle, $query);
}
public function db_num_fields($res)
{
if ($res instanceof PDOStatement) {
return $res->columnCount();
}
return 0;
}
public function db_num_rows($res)
{
}
public function db_field_name($res, $position)
{
if ($res instanceof PDOStatement) {
$m = $res->getColumnMeta($position);
return preg_replace('/^.+\./', '', $m['name']); # we said short_column_names = 1
}
}
public function db_field_type($res, $position)
{
if ($res instanceof PDOStatement) {
$m = $res->getColumnMeta($position);
switch ($m['pdo_type']) {
case PDO::PARAM_BOOL:
return 'boolean';
case PDO::PARAM_NULL:
return 'null';
case PDO::PARAM_INT:
return 'integer';
default:
return 'varchar';
}
}
}
public function db_fetch_assoc($res)
{
}
public function db_result_seek($res, $row)
{
}
public function db_changes($handle, $res)
{
if ($res instanceof PDOStatement) {
return $res->rowCount();
}
}
public function db_last_error($handle)
{
if ($handle instanceof PDO) {
$err = $handle->errorInfo();
return $err[2] . ' (' . $err[1] . ')';
}
return false;
}
public function db_escape_string($str, $handle = null)
{
if ($handle instanceof PDO) {
return trim($handle->quote($str), "'");
}
return $str;
}
public function escapeSystem($str)
{
return "'" . $this->escape($str) . "'";
}
public function begin()
{
if ($this->__link instanceof PDO) {
$this->__link->beginTransaction();
}
}
public function commit()
{
if ($this->__link instanceof PDO) {
$this->__link->commit();
}
}
public function rollback()
{
if ($this->__link instanceof PDO) {
$this->__link->rollBack();
}
}
public function db_write_lock($table)
{
$this->execute('BEGIN EXCLUSIVE TRANSACTION');
}
public function db_unlock()
{
$this->execute('END');
}
public function vacuum($table)
{
$this->execute('VACUUM ' . $this->escapeSystem($table));
}
public function dateFormat($field, $pattern)
{
return "strftime('" . $this->escape($pattern) . "'," . $field . ') ';
}
public function orderBy()
{
$default = [
'order' => '',
'collate' => false
];
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = $v;
} elseif (is_array($v) && !empty($v['field'])) {
$v = array_merge($default, $v);
$v['order'] = (strtoupper($v['order']) == 'DESC' ? 'DESC' : '');
if ($v['collate']) {
if ($this->utf8_unicode_ci instanceof Collator) {
$res[] = $v['field'] . ' COLLATE utf8_unicode_ci ' . $v['order'];
} else {
$res[] = 'LOWER(' . $v['field'] . ') ' . $v['order'];
}
} else {
$res[] = $v['field'] . ' ' . $v['order'];
}
}
}
return empty($res) ? '' : ' ORDER BY ' . implode(',', $res) . ' ';
}
public function lexFields()
{
$fmt = $this->utf8_unicode_ci instanceof Collator ? '%s COLLATE utf8_unicode_ci' : 'LOWER(%s)';
foreach (func_get_args() as $v) {
if (is_string($v)) {
$res[] = sprintf($fmt, $v);
} elseif (is_array($v)) {
$res = array_map(function ($i) use ($fmt) {return sprintf($fmt, $i);}, $v);
}
}
return empty($res) ? '' : implode(',', $res);
}
# Internal SQLite function that adds NOW() SQL function.
public function now()
{
return date('Y-m-d H:i:s');
}
}
/** @cond ONCE */
}
/** @endcond */

File diff suppressed because it is too large Load Diff