50 lines
No EOL
1.2 KiB
PHP
50 lines
No EOL
1.2 KiB
PHP
<?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;
|
|
}
|
|
}
|
|
|
|
?>
|