Transactd PHP ORM 1.5.0
  • Namespace
  • Class

Namespaces

  • None
  • Transactd
    • boot
      • Laravel

Classes

  • Transactd\AggregateFunction
  • Transactd\boot\Laravel\TransactdLaravelServiceProvider
  • Transactd\CachedQueryExecuter
  • Transactd\Collection
  • Transactd\CollectionIterator
  • Transactd\DatabaseManager
  • Transactd\Model
  • Transactd\QueryAdapter
  • Transactd\QueryExecuter
  • Transactd\Relation
  • Transactd\TableForwordIterator
  • Transactd\TableIterator
  • Transactd\TableReverseIterator

Traits

  • Transactd\JsonSerializable

Exceptions

  • Transactd\IOException
  • Transactd\ModelNotFoundException
  • Transactd\ModelUserCancelException

Constants

  • INDENT

Functions

  • generateModel
  • get_singular
  • get_singular_dictionary
  • getPhpType
  • getTableDef
  • main
  • makeClass
  • makeClassDoc
  • makeHeader
  • perseAlias
  • perseDir
  • perseUri
  • printUSAGE
  • removeUnderscore
  • writeToFile
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 
<?php

namespace Transactd;

require_once(__DIR__ .'/Require.php');

use BizStation\Transactd\Transactd;
use BizStation\Transactd\PooledDbManager;
use BizStation\Transactd\ConnectParams;
use \Transactd\Model;
use Transactd\IOException;

/**
 * @method \BizStation\Transactd\Database master()
 * @method \BizStation\Transactd\Database slave()
 * @method QueryExecuter queryExecuter(string $tableName, string $className = 'stdClass')
 * @method \BizStation\Transactd\Table table(string $tableName)
 * @method void beginTrn(int $bias = null)
 * @method void endTrn()
 * @method void abortTrn()
 * @method void beginTransaction(int $bias = null)
 * @method void commit()
 * @method void rollBack()
 * @method void beginSnapshot(int $bias = Transactd::CONSISTENT_READ)
 * @method void endSnapshot()

 */
class DatabaseManager
{
    public static $tableCash = false;
    private static $dbmArray = array();
    private $pdm = null;
    private $pds = null;

    private static function throwError($db)
    {
        throw new IOException($db->statMsg(), $db->stat());
    }

    private function doConnect($urim, $uris, $otherConnection = false)
    {
        if ($urim != '') {
            PooledDbManager::setMaxConnections(PooledDbManager::maxConnections() + 1);
            $uri = new ConnectParams($urim);
            $this->urim = $uri;
            $this->pdm = new PooledDbManager();
            $this->pdm->c_use($uri);
            if (!$otherConnection && $urim === $uris) {
                $this->pds = $this->pdm;
            }
        }
        if ($uris != '' && ($urim !== $uris || $otherConnection)) {
            PooledDbManager::setMaxConnections(PooledDbManager::maxConnections() + 1);
            $uri = new ConnectParams($uris);
            $this->uris = $uri;
            $this->pds = new PooledDbManager();
            $this->pds->c_use($uri);
        }
    }
    
    protected function _master()
    {
        return $this->pdm->db();
    }

    protected function _slave()
    {
        if ($this->pds !== null) {
            return $this->pds->db();
        }
        return null;
    }
    /**
     * Create a cachedQueryExecuter
     * 
     * @param string $tableName
     * @param string $className
     * @return \Transactd\CachedQueryExecuter
     */
    public function cachedQueryExecuter($tableName, $className = 'stdClass')
    {
        if (self::$tableCash === true) {
            return new CachedQueryExecuter($tableName, $this->pdm, $this->pds, $className);
        }
        return new CachedQueryExecuter($tableName, $this->_master(), $this->_slave(), $className);
    }

    protected function _table($tableName)
    {
        if (self::$tableCash === true) {
            return $this->pdm->table($tableName);
        }
        return $this->_master()->openTable($tableName);
    }

    protected function _queryExecuter($tableName, $className = 'stdClass')
    {
        if (self::$tableCash === true) {
            return new QueryExecuter($tableName, $this->pdm, $this->pds, $className);
        }
        return new QueryExecuter($tableName, $this->_master(), $this->_slave(), $className);
    }

    protected function _beginTrn($bias = null)
    {
        $this->pdm->beginTrn($bias);
    }

    protected function _endTrn()
    {
        $this->pdm->endTrn();
    }

    protected function _abortTrn()
    {
        $this->pdm->abortTrn();
        Model::clearTableCache();
    }

    protected function _beginSnapshot($bias = Transactd::CONSISTENT_READ)
    {
        return $this->pds->beginSnapshot($bias);
    }

    protected function _endSnapshot()
    {
        return $this->pds->endSnapshot();
    }

    protected function _beginTransaction($bias = null)
    {
        $this->beginTrn($bias);
    }

    protected function _commit()
    {
        $this->endTrn();
    }

    protected function _rollBack()
    {
        $this->abortTrn();
    }

    public function __call($name, $arguments)
    {
        if (method_exists('Transactd\DatabaseManager', '_'.$name)) {
            $reflectionMethod = new \ReflectionMethod($this, '_'.$name);
            $reflectionMethod->setAccessible(true);
            return $reflectionMethod->invokeArgs($this, $arguments);
        }
        throw new \BadMethodCallException($name);
    }
    /**
     *
     * @param string $urim Uri for master
     * @param string $uris Uri for slave
     * @param string $name Connection name
     * @param bool $otherConnection Force creates other connection if $urim === $uris. 
     * @return self
     */
    public static function connect($urim, $uris, $name = 'default', $otherConnection = false)
    {
        if (!array_key_exists($name, self::$dbmArray)) {
            if (!array_key_exists($name, self::$dbmArray)) {
                $dbm = new self();
                $dbm->name = $name;
                $dbm->doConnect($urim, $uris, $otherConnection);
                self::$dbmArray[$name] = $dbm;
                return $dbm;
            }
        }
    }
    
    /**
     *
     * @param string $name Connection name
     * @return self
     * @throws IOException
     */
    public static function connection($name = 'default')
    {
        if (!array_key_exists($name, self::$dbmArray)) {
            throw new IOException('No connection', 1);
        }
        return self::$dbmArray[$name];
    }
    
    /**
     * Release all connections force.
     * 
     * @return void
     */
    public static function reset()
    {
        if (!(bool)self::$dbmArray) {
            return;
        }
        Model::clearTableCache();
        foreach(self::$dbmArray as $dbm) {
            if ($dbm->pds !== null) {
                $dbm->pds->unUse();
            }
            if ($dbm->pdm !== null) {
                $dbm->pdm->unUse();
            }   
        }
        reset(self::$dbmArray);
        $dbm = current(self::$dbmArray);
        self::$dbmArray = array();
        if ($dbm->pdm !== null) {
            $dbm->pdm->reset(3);
        }
    }

    /**
     * Implemets of __callStatic.
     * When the name is object method , redirected to the default connection object.
     *
     * @param string $name
     * @param array $arguments
     * @return mixed
     * @throws \BadMethodCallException
     */
    public static function __callStatic($name, $arguments)
    {
        if (method_exists('Transactd\DatabaseManager', '_'.$name)) {
            $obj = self::connection('default');
            $reflectionMethod = new \ReflectionMethod('Transactd\DatabaseManager', '_'.$name);
            $reflectionMethod->setAccessible(true);
            return $reflectionMethod->invokeArgs($obj, $arguments);
        }
        throw new \BadMethodCallException($name);
    }
}
Transactd PHP ORM 1.5.0 API documentation generated by ApiGen