Библиотека для cis, online, cms1
This commit is contained in:
commit
3c2e614d87
269 changed files with 39854 additions and 0 deletions
50
core/drivers/database.mysql.php
Normal file
50
core/drivers/database.mysql.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
require_once 'core/drivers/db.php';
|
||||
|
||||
/**
|
||||
* Ïðîñòîé êëàññ äëÿ ðàáîòû ñ áàçàìè äàííûõ
|
||||
*/
|
||||
class Database_MYSQL extends DB implements IDatabase
|
||||
{
|
||||
public function connect(array $dsn)
|
||||
{
|
||||
$db = @mysql_pconnect($dsn['hostspec'], $dsn['username'], $dsn['password'])
|
||||
or die("Unable connect to database!");
|
||||
|
||||
mysql_select_db($dsn['database']);
|
||||
return ($this->db = $db);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
return mysql_close($this->db);
|
||||
}
|
||||
|
||||
public function query($query)
|
||||
{
|
||||
$res = mysql_query($this->db, $query)
|
||||
or die("Error: wrong SQL query #$query#");
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function fetchAllArray($query)
|
||||
{
|
||||
$res = $this->query($query);
|
||||
|
||||
while ($row = mysql_fetch_array ($res))
|
||||
$rows[] = $row;
|
||||
mysql_free_result($res);
|
||||
return ($rows) ? $rows : array();
|
||||
}
|
||||
|
||||
public function fetchOneArray($query)
|
||||
{
|
||||
$res = $this->query($query);
|
||||
$row = mysql_fetch_array($res);
|
||||
mysql_free_result($res);
|
||||
return ($row) ? $row : array();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
54
core/drivers/database.odbc.php
Normal file
54
core/drivers/database.odbc.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
require_once 'core/drivers/db.php';
|
||||
|
||||
/**
|
||||
* Ïðîñòîé êëàññ äëÿ ðàáîòû ñ áàçàìè äàííûõ
|
||||
*/
|
||||
class Database_ODBC extends DB implements IDatabase
|
||||
{
|
||||
public function connect(array $dsn)
|
||||
{
|
||||
$db = @odbc_connect($dsn['database'], $dsn['username'], $dsn['password'])
|
||||
or die("Unable connect to database!");
|
||||
return ($this->db = $db);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
return odbc_close($this->db);
|
||||
}
|
||||
|
||||
public function query($query)
|
||||
{
|
||||
$res = odbc_exec($this->db, $query)
|
||||
or die("Error: wrong SQL query #$query#");
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function fetchAllArray($query)
|
||||
{
|
||||
$res = $this->query($query);
|
||||
$to = odbc_num_fields($res);
|
||||
while (odbc_fetch_row($res)) {
|
||||
for ($i = 1; $i <= $to; $i++) {
|
||||
$row [odbc_field_name($res, $i)] = trim(odbc_result($res, $i));
|
||||
}
|
||||
$rows[] = $row;
|
||||
}
|
||||
return ($rows)? $rows : array();
|
||||
}
|
||||
|
||||
public function fetchOneArray($query)
|
||||
{
|
||||
$res = $this->query($query);
|
||||
if (!odbc_fetch_row($res)) return array ();
|
||||
$to = odbc_num_fields($res);
|
||||
for ($i = 1; $i <= $to; $i++) {
|
||||
$row [odbc_field_name($res, $i)] = trim(odbc_result($res, $i));
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
74
core/drivers/database.pgsql.php
Normal file
74
core/drivers/database.pgsql.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
require_once 'core/drivers/db.php';
|
||||
|
||||
/**
|
||||
* Ïðîñòîé êëàññ äëÿ ðàáîòû ñ áàçàìè äàííûõ
|
||||
*/
|
||||
class Database_PGSQL extends DB implements IDatabase
|
||||
{
|
||||
public function connect(array $dsn)
|
||||
{
|
||||
if (isset($dsn['port'])) {
|
||||
$port = "port={$dsn['port']}";
|
||||
} else {
|
||||
$port = "port=5432";
|
||||
}
|
||||
$str = "host={$dsn['hostspec']} $port dbname={$dsn['database']} user={$dsn['username']} password={$dsn['password']}";
|
||||
$db = @pg_connect($str)
|
||||
or die("Unable connect to database!");
|
||||
|
||||
return ($this->db = $db);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
return pg_close($this->db);
|
||||
}
|
||||
|
||||
public function query($query)
|
||||
{
|
||||
$res = pg_query($this->db, $query)
|
||||
or die("Error: wrong SQL query #$query#");
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function fetchAllArray($query, $type = PGSQL_ASSOC)
|
||||
{
|
||||
$res = $this->query($query);
|
||||
|
||||
$rows = array();
|
||||
while ($row = pg_fetch_array($res, NULL, $type)) {
|
||||
$rows[] = $this->clean($row);
|
||||
}
|
||||
pg_free_result($res);
|
||||
return ($rows) ? $rows : array();
|
||||
}
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return pg_affected_rows($this->db);
|
||||
}
|
||||
|
||||
private function clean($row)
|
||||
{
|
||||
foreach ($row as $key => $value) {
|
||||
$row[$key] = trim($value);
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function fetchOneArray($query, $type = PGSQL_ASSOC)
|
||||
{
|
||||
$res = $this->query($query);
|
||||
$row = pg_fetch_array($res, NULL, $type);
|
||||
pg_free_result($res);
|
||||
return ($row) ? $this->clean($row) : array();
|
||||
}
|
||||
|
||||
function getNextId($seq)
|
||||
{
|
||||
$result = $this->fetchOneArray("SELECT nextval('$seq')");
|
||||
return $result['nextval'];
|
||||
}
|
||||
}
|
||||
50
core/drivers/db.php
Normal file
50
core/drivers/db.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Èíòåðôåéñ äðàéâåðà êëàññà áàç äàííûõ
|
||||
*/
|
||||
interface IDatabase
|
||||
{
|
||||
public function connect(array $dsn);
|
||||
public function close();
|
||||
public function query($query);
|
||||
public function fetchAllArray($query);
|
||||
public function fetchOneArray($query);
|
||||
}
|
||||
|
||||
abstract class DB implements IDatabase
|
||||
{
|
||||
const limit = 1024;
|
||||
protected $db;
|
||||
|
||||
public static function quote($x)
|
||||
{
|
||||
return "'" . $x . "'";
|
||||
}
|
||||
|
||||
private static function assign_quote($x, $y)
|
||||
{
|
||||
return $x . "='" . $y . "'";
|
||||
}
|
||||
|
||||
function insert($table, array $values)
|
||||
{
|
||||
return $this->query("INSERT INTO $table (" . implode(",", array_keys($values))
|
||||
. ") VALUES (" . implode(",", array_map(array('self', 'quote'), array_values($values))) . ")");
|
||||
}
|
||||
|
||||
function update($table, array $values, $cond)
|
||||
{
|
||||
return $this->query("UPDATE $table SET " . implode(",",
|
||||
array_map(array('self', 'assign_quote'), array_keys($values), array_values($values))) . " WHERE $cond");
|
||||
}
|
||||
|
||||
function check_text($text)
|
||||
{
|
||||
if(strlen($text) > self::limit) $text = substr($text, 0, self::limit);
|
||||
$text = htmlspecialchars(trim($text));
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue