Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1285 → Rev 1286

/FileXch/branches/php-impl/lib/db.php
0,0 → 1,272
<?php
function createDbInterface($type, $host, $user, $password, $database)
{
if($type == "mysql") {
return new DbMySql($host, $user, $password, $database);
}
else {
throw new Exception("Unknown DB type '$type'");
}
}
 
class DbMySql
{
private $endOfLife;
private $host;
private $user;
private $password;
private $database;
private $conn;
private $result;
private $rollbacked;
 
function __construct($host, $user, $password, $database)
{
debugMsg("DbMySql::__construct");
 
$this->endOfLife = false;
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
}
 
function __destruct()
{
debugMsg("DbMySql::__destruct");
 
$this->disconnect();
}
 
function endTheLife()
{
$this->checkLiveCycle();
$this->disconnect();
 
$this->endOfLife = true;
}
 
function checkLiveCycle()
{
if($this->endOfLife) {
throw new Exception("I'm die");
}
}
 
function connect()
{
$this->checkLiveCycle();
 
if(!$this->conn) {
debugMsg("DbMySql::connect");
 
$this->conn = mysql_connect($this->host, $this->user, $this->password);
if(!$this->conn) {
throw new Exception("Cannot connect to MySql database: " . mysql_error());
}
 
mysql_select_db($this->database, $this->conn);
}
}
 
function disconnect()
{
if($this->conn) {
$this->checkLiveCycle();
 
debugMsg("DbMySql::disconnect");
 
if(!$this->rollbacked) {
$this->commit();
}
 
mysql_close($this->conn);
$this->conn = null;
}
}
 
function beginTransaction()
{
$this->execute("begin");
$this->rollbacked = false;
}
 
function commit()
{
$this->execute("commit");
}
 
function rollback()
{
$this->execute("rollback");
$this->rollbacked = true;
}
 
function query($sql)
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::query [$sql]");
$this->connect();
$this->result = mysql_query($sql);
 
if(!$this->result) {
throw new Exception('Cannot execute query: ' . mysql_error() . "\n" . $sql);
}
 
return $this->result;
}
 
function freeResult()
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::freeResult");
 
mysql_free_result($this->result);
}
 
function fetchRow()
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::fetchRow");
 
if($this->result) {
return mysql_fetch_assoc($this->result);
}
else {
return false;
}
}
 
function execute($sql)
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::execute [$sql]");
 
$this->connect();
 
$success = mysql_query($sql);
 
if(!$success) {
throw new Exception('Cannot execute query: ' . mysql_error() . "\n" . $sql);
}
}
 
function escape($s)
{
$this->checkLiveCycle();
 
$this->connect();
 
return mysql_real_escape_string($s);
}
 
function formatString($s, $nullable = true)
{
$this->checkLiveCycle();
 
if($s === null && $nullable) {
return ($nullable ? 'null' : "''");
}
else if(is_string($s)) {
return "'" . $this->escape($s) . "'";
}
else {
throw new Exception("Not a string");
}
}
 
function formatBoolean($b, $nullable = true)
{
$this->checkLiveCycle();
 
if($b === null) {
return ($nullable ? 'null' : 'false');
}
else {
return ($b ? 'true' : 'false');
}
}
 
function formatInteger($i, $def = null)
{
$this->checkLiveCycle();
 
if($i === null) {
if($def === null) {
return 'null';
}
else {
return '' . $def;
}
}
else if(is_int($i)) {
return '' . $i;
}
else {
throw new Exception("Not an integer");
}
}
 
function formatDate($d, $def = null)
{
$this->checkLiveCycle();
 
if($d === null) {
if($def === null) {
return 'null';
}
else {
return "'" . date_format($def, 'Y-m-d H:i:s') . "'";
}
}
else if($d instanceof DateTime) {
return "'" . date_format($d, 'Y-m-d H:i:s') . "'";
}
else {
throw new Exception("Not a date");
}
}
 
function parseInteger($s, $def = null)
{
$this->checkLiveCycle();
 
if($s === null) {
return $def;
}
else {
return intval($s);
}
}
 
function parseBoolean($s, $def = null)
{
$this->checkLiveCycle();
 
if($s === null) {
return $def;
}
else {
return ($s ? true : false);
}
}
 
function parseDate($s, $def = null)
{
$this->checkLiveCycle();
 
if($s === null) {
return $def;
}
else {
return date_create($s);
}
}
 
}
?>