<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Copyright (c) 2008, Demian Turner |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +---------------------------------------------------------------------------+
// | Seagull 0.6 |
// +---------------------------------------------------------------------------+
// | DB.php |
// +---------------------------------------------------------------------------+
// | Authors: Demian Turner <demian@phpkitchen.com> |
// +---------------------------------------------------------------------------+
// $Id: DB.php,v 1.14 2005/06/20 10:56:31 demian Exp $
require_once 'MDB2.php';
/**
* Class for handling DB resources.
*
* @package SGL
* @author Demian Turner <demian@phpkitchen.com>
* @version $Revision: 1.14 $
*/
class SGL_MDB2 extends MDB2_Driver_Common
{
var $dbh;
var $dsn ;
private static $instance;
function SGL_MDB2()
{
parent::__construct();
}
// The singleton method
public static function singleton($dsn, $autoLoad=false)
{
if (!isset(self::$instance) || $autoLoad === true) {
$c = __CLASS__;
self::$instance = new SGL_MDB2();
$conn = self::$instance->_connect($dsn, $autoLoad);
if (SGL::isError($conn)) {
return $conn;
}
}
return self::$instance;
}
function _connect($dsn, $options = false)
{
$conn = MDB2::connect($dsn, $options);
if (SGL::isError($conn) === false) {
///Start load module
$conn->loadModule('Extended', null, false);
//the class to load for the module is specific to the phptype
$conn->loadModule('Manager', null, true);
$conn->loadModule('Reverse', null, true);
$conn->loadModule('Native', null, true);
$conn->loadModule('Function', null, true);
$conn->loadModule('Datatype', null, true);
///End load module
$conn->setFetchMode(MDB2_FETCHMODE_OBJECT);
//setOption
$conn->setOption('seqcol_name', 'id');
$conn->setOption('default_table_type', 'MYISAM');
foreach (self::$instance as $key=>$val) {
if (isset($conn->$key)) {
self::$instance->$key = $conn->$key;
}
}
/*
print_r(self::$instance);
foreach ($conn as $key=>$val) {
echo 'k1='.$key .'<br><br>';
if (isset(self::$instance->$key)) {
//echo "" . self::$instance . '->' . $key . '<br>';
self::$instance->$key = $val;
}
}
*/
$conn->dsn['database'] = $conn->database_name;
$conn->connected_dsn['database'] = $conn->database_name;
$this->dbh = $conn;
$this->dsn = $conn->dsn;
}
//echo '<pre>'; print_r($conn);
//register $dbh
//$locator = SGL_ServiceLocator::singleton();
//$locator->register('DB', $conn);
return $conn;
}
######## Start call to class MDB2 ##########
function isConnection($value)
{
if (method_exists($this->dbh, 'isConnection') === true) {
//return query
return $this->dbh->isConnection($value);
} else {
//TODO::return Error
}
}
function isError($data, $code = null)
{
if (method_exists($this->dbh, 'isError') === true) {
//return query
return $this->dbh->isError($data, $code);
} else {
//TODO::return Error
}
}
######## End call to class MDB2 ##########
/*######## Start call to class MDB2_Driver_Common ##########
function connect($dsn, $options = false)
{
$this->singleton($dsn, $options);
}*/
function getOne($query, $type = null, $colnum = 0)
{
if (method_exists($this->dbh, 'queryOne') === true) {
//return query
//return $this->dbh->queryOne($query, $type, $colnum);
return $this->dbh->queryOne($query);
} else {
//TODO::return Error
}
}
function getAll($query, $fetchmode=MDB2_FETCHMODE_DEFAULT, $types = null,
$rekey = false, $force_array = false, $group = false)
{
if (method_exists($this->dbh, 'queryAll') === true) {
//return query
return $this->dbh->queryAll($query, $types, $fetchmode
, $rekey, $force_array, $group);
} else {
//TODO::return Error
}
}
function getRow($query, $fetchmode = MDB2_FETCHMODE_DEFAULT, $types = null)
{
if (method_exists($this->dbh, 'queryRow') === true) {
//return query
return $this->dbh->queryRow($query, $types, $fetchmode);
} else {
//return Error
}
}
function prepare($query, $types = null, $result_types = null, $lobs = array())
{
if (method_exists($this->dbh, 'prepare') === true) {
//return query
return $this->dbh->prepare($query, $types, $result_types, $lobs);
} else {
//return Error
}
}
function execute($values = null, $result_class = true, $result_wrap_class = false)
{
if (method_exists($this->dbh, 'execute') === true) {
//return query
return $this->dbh->execute($values, $result_class, $result_wrap_class);
} else {
//return Error
}
}
function executeMultiple($stmt, $params = null)
{
if (method_exists($this->dbh->extended, 'getAssoc') === true) {
return $this->dbh->extended->executeMultiple($stmt, $params);
} else {
}
}
function escape($text, $escape_wildcards = false)
{
if (method_exists($this->dbh, 'escape') === true) {
//return query
return $this->dbh->escape($text, $escape_wildcards);
} else {
//return Error
}
}
function getCol($query, $type = null, $colnum = 0)
{
if (method_exists($this->dbh, 'queryCol') === true) {
//return query
return $this->dbh->queryCol($query, $type, $colnum);
} else {
//return Error
}
}
function getAssoc($query, $force_array = false, $params = array(),
$fetchmode = MDB2_FETCHMODE_ASSOC, $types = null, $param_types = null, $group = false)
{
if (method_exists($this->dbh->extended, 'getAssoc') === true) {
//return query
return $this->dbh->extended->getAssoc($query, $types, $params, $param_types,
$fetchmode, $force_array, $group);
} else {
//return Error
}
}
function query($query, $type = null)
{
if (method_exists($this->dbh, 'query') === true) {
//return query
return $this->dbh->query($query, $type);
} else {
//return Error
}
}
function quoteSmart($value, $type = null, $quote = true, $escape_wildcards = false)
{
if (method_exists($this->dbh, 'quote') === true) {
//return query
return $this->dbh->quote($value, $type, $quote, $escape_wildcards);
} else {
//return Error
}
}
function quote($value, $type = null, $quote = true, $escape_wildcards = false)
{
if (method_exists($this->dbh, 'quote') === true) {
//return query
return $this->dbh->quote($value, $type, $quote, $escape_wildcards);
} else {
//return Error
}
}
function quoteIdentifier($str, $check_option = false)
{
if (method_exists($this->dbh, 'quoteIdentifier') === true) {
//return query
return $this->dbh->quoteIdentifier($str, $check_option);
} else {
//return Error
}
}
function setFetchMode($fetchmode, $object_class = 'stdClass')
{
if (method_exists($this->dbh, 'setFetchMode') === true) {
//return query
$this->dbh->setFetchMode($fetchmode, $object_class);
} else {
//return Error
}
}
function autoExecute()
{
if (method_exists($this->dbh->extended, 'autoExecute') === true) {
//return query
return $this->dbh->extended->autoExecute();
} else {
//return Error
}
}
function nextID($seq_name, $ondemand = true)
{
if (method_exists($this->dbh, 'nextID') === true) {
$id = $this->dbh->nextID($seq_name, $ondemand);
//echo "t_name=" . $seq_name .'<br>';
//echo "nextID=" .$id .'<br><br>';
//return query
return $id;//$this->dbh->nextID($seq_name, $ondemand);
} else {
//echo 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
//return Error
}
}
function lastInsertID($table = null, $field = null)
{
if (method_exists($this->dbh, 'lastInsertID') === true) {
//return query
return $this->dbh->lastInsertID($table, $field);
} else {
//return Error
}
}
function beginTransaction($savepoint = null)
{
if (method_exists($this->dbh, 'beginTransaction') === true) {
//return query
$res = $this->dbh->beginTransaction($savepoint);
if($res === true)
{
$this->in_transaction = $this->dbh->in_transaction;
return true;
} else {
return $res;
}
} else {
//return Error
}
}
function exec($query)
{
if (method_exists($this->dbh, 'exec') === true) {
//return query
return $this->dbh->exec($query);
} else {
//return Error
}
}
//beginNestedTransaction
function beginNestedTransaction()
{
if (method_exists($this->dbh, 'beginNestedTransaction') === true) {
//return query
return $this->dbh->beginNestedTransaction();
} else {
//return Error
}
}
function commit($savepoint = null)
{
if (method_exists($this->dbh, 'commit') === true) {
//return query
$res = $this->dbh->commit($savepoint);
if($res === true)
{
$this->in_transaction = $this->dbh->in_transaction;
return true;
} else {
return $res;
}
} else {
//return Error
}
}
function databaseExists($name)
{
if (method_exists($this->dbh, 'databaseExists') === true) {
//return query
return $this->dbh->databaseExists($name);
} else {
//return Error
}
}
function getDsn($type = 'string', $hidepw = false)
{
if (method_exists($this->dbh, 'getDsn') === true) {
//return query
return $this->dbh->getDsn($type, $hidepw);
} else {
//return Error
}
}
function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
{
if (method_exists($this->dbh, 'fetchRow') === true) {
//return query
return $this->dbh->fetchRow(MDB2_FETCHMODE_ORDERED, $rownum);
} else {
//return Error
}
}
function supports($feature)
{
if (method_exists($this->dbh, 'supports') === true) {
//return query
return $this->dbh->supports($feature);
} else {
//return Error
}
}
//inTransaction
function inTransaction($ignore_nested = false)
{
if (method_exists($this->dbh, 'inTransaction') === true) {
//return query
return $this->dbh->inTransaction($ignore_nested);
} else {
//return Error
}
}
//rollBack
function rollBack($savepoint = null)
{
if (method_exists($this->dbh, 'rollBack') === true) {
//return query
$res = $this->dbh->rollBack($savepoint);
if($res === true)
{
$this->in_transaction = $this->dbh->in_transaction;
return true;
} else {
return $res;
}
} else {
//return Error
}
}
//getDatabase
function getDatabase()
{
if (method_exists($this->dbh, 'getDatabase') === true) {
//return query
return $this->dbh->getDatabase();
} else {
//return Error
}
}
//tableInfo
function tableInfo($table)
{
if (method_exists($this->dbh->reverse, 'tableInfo') === true) {
//return query
return $this->dbh->reverse->tableInfo($table);
} else {
//return Error
}
}
//getOption
function getOption($option)
{
if (method_exists($this->dbh, 'getOption') === true) {
//return query
return $this->dbh->getOption($option);
} else {
//return Error
}
}
//expectError
function expectError($code = '*')
{
if (method_exists($this->dbh, 'expectError') === true) {
//return query
return $this->dbh->expectError($code);
} else {
//return Error
}
}
//popExpect
function popExpect()
{
if (method_exists($this->dbh, 'popExpect') === true) {
//return query
return $this->dbh->popExpect();
} else {
//return Error
}
}
//setLimit
function setLimit($limit, $offset = null)
{
if (method_exists($this->dbh, 'setLimit') === true) {
//return query
return $this->dbh->setLimit($limit, $offset);
} else {
//return Error
}
}
//setOption
function setOptions($db, $options)
{
if (method_exists($this->dbh, 'setOption') === true) {
//return query
return $this->dbh->setOption($db, $options);
} else {
//return Error
}
}
//getSequenceName
function getSequenceName($sqn)
{
if (method_exists($this->dbh, 'getSequenceName') === true) {
//return query
return $this->dbh->getSequenceName($sqn);
} else {
//return Error
}
}
######## End call to class MDB2_Driver_Common ##########
function getPhpType()
{
return $this->dbh->phptype;
}
//limitQuery
function limitQuery($query, $offset = 0, $limit, $types = array(), $result_class = true,
$result_wrap_class = false)
{
if (method_exists($this->dbh->extended, 'limitQuery') === true) {
//return query
return $this->dbh->extended->limitQuery(
$query, $types, $limit, $offset, $result_class, $result_wrap_class);
} else {
//return Error
}
}
//getListOf
function getListOf($value)
{
switch (strtolower($value))
{
case 'tables':
if (method_exists($this->dbh->manager, 'listTables') === true) {
//return query
return $this->dbh->manager->listTables();
} else {
//return Error
}
break;
case 'sequences':
if (method_exists($this->dbh->manager, 'listSequences') === true) {
//return query
return $this->dbh->manager->listSequences();
} else {
//return Error
}
break;
}
}
}
?>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez