Changeset 20417

Show
Ignore:
Timestamp:
10/02/08 01:00:33 (3 months ago)
Author:
nowelium
Message:

fix:procedure & transaction scripts

Location:
lang/php/misc/Hermit
Files:
4 added
14 modified
1 moved

Legend:

Unmodified
Added
Removed
  • lang/php/misc/Hermit/src/Hermit/HermitTransactionManager.php

    r20314 r20417  
    3636    public function createProxy(HermitProxy $proxy, $targetClass){ 
    3737        $tx = self::get($targetClass); 
     38        $tx->resume(HermitDataSourceManager::get($targetClass)); 
    3839        return new HermitCallableProxy($proxy, array($tx, 'proceed')); 
    3940    } 
  • lang/php/misc/Hermit/src/Hermit/command/HermitProcedureCommand.php

    r20387 r20417  
    2626        $stmt = $builder->build($pdo); 
    2727        $stmt->execute($parameters); 
    28         $rs = new HermitProcedureResultSet($stmt->getSqlParameter()); 
    29         $rs->bindParameter($pdo, $parameters); 
    30         return $rs->create($stmt, $this->type); 
     28        $rs = HermitProcedureResultSetFactory::create($pdo, $stmt->getSqlParameter()); 
     29        if($rs instanceof HermitParameterBind){ 
     30            $rs->bindParameter($pdo, $parameters); 
     31        } 
     32        return $rs->execute($stmt, $this->type); 
    3133    } 
    3234} 
  • lang/php/misc/Hermit/src/Hermit/parameter/HermitProcedureParameter.php

    r20387 r20417  
    55 */ 
    66class HermitProcedureParameter extends HermitSqlParameterHash { 
    7     private $info; 
    8     private $dbms; 
    9     private $outParams = array(); 
     7    protected $info; 
     8    protected $dbms; 
     9    protected $outParams = array(); 
     10    protected static $pdoTypes = array( 
     11        'boolean' => PDO::PARAM_BOOL, 
     12        'NULL' => PDO::PARAM_NULL, 
     13        'integer' => PDO::PARAM_INT, 
     14        'string' => PDO::PARAM_STR 
     15    ); 
    1016    public function __construct(HermitProcedureInfo $info, $dbms){ 
    1117        $this->info = $info; 
    1218        $this->dbms = $dbms; 
    1319    } 
    14     public function replace($key, $name, $defaultValue){ 
    15         return call_user_func(array($this, 'replace_' . $this->dbms), $key, $name, $defaultValue); 
    16     } 
    17     public function replace_mysql($key, $name, $defaultValue){ 
    18         if($this->info->typeofIn($name)){ 
    19             $this->bindKeys[] = $name; 
    20             return ':' . $name; 
    21         } 
    22         if($this->info->typeofOut($name) || $this->info->typeofInOut($name)){ 
    23             $this->bindKeys[] = $name; 
    24             $this->outParams[] = $name; 
    25             return '@' . $name; 
    26         } 
    27         throw new RuntimeException('unknown "' . $name . '" parameter'); 
    28     } 
    29     public function bind(PDOStatement $stmt, $value){ 
    30         return call_user_func(array($this, 'bind_' . $this->dbms), $stmt, $value); 
    31     } 
    32  
    33     public function bind_mysql(PDOStatement $stmt, $value){ 
    34         $param = $value[0]; 
    35         foreach($this->bindKeys as $index => $key){ 
    36             $prefix = ''; 
    37             if($this->info->typeofIn($key)){ 
    38                 $stmt->bindParam(':' . $key, $param->$key); 
    39             } 
    40         } 
    41     } 
    42  
    4320    public function getOutParameters(){ 
    4421        return $this->outParams; 
    4522    } 
    46  
     23    public function replace($key, $name, $defaultValue){ 
     24        $this->bindKeys[] = $name; 
     25        if($this->info->typeofOut($name) || $this->info->typeofInOut($name)){ 
     26            $this->outParams[] = $name; 
     27        } 
     28        return ':' . $name; 
     29    } 
     30    public function bind(PDOStatement $stmt, $value){ 
     31        $param = $value[0]; 
     32        foreach($this->bindKeys as $index => $key){ 
     33            $bindKey = ':' . $key; 
     34            if($this->info->typeofIn($key)){ 
     35                $stmt->bindParam($bindKey, $param->$key); 
     36                continue; 
     37            } 
     38            $paramValue = $param->$key; 
     39            if(null === $paramValue){ 
     40                $stmt->bindParam($bindKey, null, PDO::PARAM_NULL | PDO::PARAM_INPUT_OUTPUT); 
     41                continue; 
     42            } 
     43            $gettype = gettype($paramValue); 
     44            $paramType = -1; 
     45            if(isset(self::$pdoTypes[$gettype])){ 
     46                $paramType = self::$pdoTypes[$gettype] | PDO::PARAM_INPUT_OUTPUT; 
     47            } else { 
     48                $paramType = PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT; 
     49            } 
     50            $stmt->bindParam($bindKey, $paramValue, $paramType); 
     51        } 
     52    } 
    4753    public function hasBindParameters(){ 
    48         return 'mysql' === $this->dbms && 0 < count($this->outParams); 
     54        return 0 < count($this->outParams); 
    4955    } 
    5056} 
  • lang/php/misc/Hermit/src/Hermit/resultset/HermitDefaultResultSet.php

    r20387 r20417  
    55 */ 
    66class HermitDefaultResultSet implements HermitResultSet { 
    7     public function create(HermitStatement $stmt, HermitValueType $type){ 
     7    public function execute(HermitStatement $stmt, HermitValueType $type){ 
    88        $type->apply($stmt); 
    99        return $stmt->fetch(); 
  • lang/php/misc/Hermit/src/Hermit/resultset/HermitListResultSet.php

    r20387 r20417  
    66 */ 
    77class HermitListResultSet implements HermitResultSet { 
    8     public function create(HermitStatement $stmt, HermitValueType $type){ 
     8    public function execute(HermitStatement $stmt, HermitValueType $type){ 
    99        $type->apply($stmt); 
    1010         
  • lang/php/misc/Hermit/src/Hermit/resultset/HermitProcedureResultSet.php

    r20387 r20417  
    55 */ 
    66class HermitProcedureResultSet implements HermitResultSet, HermitParameterBind { 
    7     private $procParameter; 
     7    protected $procParameter; 
    88    public function __construct(HermitProcedureParameter $procParameter){ 
    99        $this->procParameter = $procParameter; 
    1010    } 
    11     public function create(HermitStatement $stmt, HermitValueType $type){ 
     11    public function execute(HermitStatement $stmt, HermitValueType $type){ 
    1212        $type->apply($stmt); 
    1313 
     
    2727    } 
    2828    public function bindParameter(PDO $pdo, array $parameter){ 
    29         $param = $parameter[0]; 
    30         $dbms = HermitDatabaseMetaFactory::getDbms($pdo); 
    31         if('mysql' === $dbms){ 
    32             if(!$this->procParameter->hasBindParameters()){ 
    33                 return; 
    34             } 
    35             $out = $this->procParameter->getOutParameters(); 
    36             foreach($out as $name){ 
    37                 $stmt = $pdo->prepare('SELECT @' . $name); 
    38                 $stmt->bindColumn(1, $param->$name); 
    39                 $stmt->execute(); 
    40                  
    41                 $stmt->fetch(PDO::FETCH_BOUND); 
    42                 $stmt->closeCursor(); 
    43                 unset($stmt); 
    44             } 
    45         } 
    4629    } 
    4730} 
  • lang/php/misc/Hermit/src/Hermit/resultset/HermitResultSet.php

    r20387 r20417  
    55 */ 
    66interface HermitResultSet { 
    7     public function create(HermitStatement $stmt, HermitValueType $type); 
     7    public function execute(HermitStatement $stmt, HermitValueType $type); 
    88} 
  • lang/php/misc/Hermit/src/Hermit/statement/HermitProcedureStatementBuilder.php

    r20387 r20417  
    88    private $annote; 
    99    private $sqlCreator; 
     10 
     11    protected static $procedureParameters = array( 
     12        'mysql' => 'HermitMySqlProcedureParameter' 
     13    ); 
     14 
    1015    public function __construct(ReflectionMethod $method, HermitAnnote $annote, HermitSqlCreator $sqlCreator){ 
    1116        $this->method = $method; 
     
    1520 
    1621    public function build(PDO $pdo){ 
    17         $this->checkProcedureParameter(); 
     22        self::checkProcedureParameter($this->method); 
    1823 
    1924        $procedureName = $this->annote->getProcedure($this->method); 
     
    2227 
    2328        $dbms = HermitDatabaseMetaFactory::getDbms($pdo); 
    24         $parameter = new HermitProcedureParameter($info, $dbms); 
     29        $parameter = null; 
     30        if(isset(self::$procedureParameters[$dbms])){ 
     31            $className = self::$procedureParameters[$dbms]; 
     32            $parameter = new $className($info, $dbms); 
     33        } else { 
     34            $parameter = new HermitProcedureParameter($info, $dbms); 
     35        } 
    2536        $sql = $this->sqlCreator->createSql($pdo); 
    2637        $sql = self::preparedSql($parameter, $sql); 
     
    2839    } 
    2940     
    30     protected function checkProcedureParameter(){ 
    31         $parameters = $this->method->getParameters(); 
     41    protected function checkProcedureParameter(ReflectionMethod $method){ 
     42        $parameters = $method->getParameters(); 
    3243        $count = count($parameters); 
    3344        if(1 < $count){ 
  • lang/php/misc/Hermit/src/Hermit/tx/AbstractHermitTx.php

    r20152 r20417  
    77    private $txRule = array(); 
    88    private $connection; 
    9     public function hasTransaction(){ 
     9    private $begin = false; 
     10    public final function hasTransaction(){ 
     11        if($this->begin){ 
     12            return true; 
     13        } 
     14        return $this->begin = $this->connection->beginTransaction(); 
    1015    } 
    11     public function begin(){ 
     16    public final function begin(){ 
     17        try { 
     18            if(!$this->begin){ 
     19                $this->begin = $this->connection->beginTransaction(); 
     20            } 
     21        } catch(Exception $e){ 
     22            // TODO: Nest transaction 
     23            throw $e; 
     24        } 
    1225    } 
    13     public function commit(){ 
     26    public final function commit(){ 
     27        try { 
     28            if($this->connection->commit()){ 
     29                $this->begin = false; 
     30            } 
     31        } catch(Exception $e){ 
     32            throw $e; 
     33        }     
    1434    } 
    15     public function rollback(){ 
     35    public final function rollback(){ 
     36        try { 
     37            if ($this->hasTransaction()) { 
     38                return $this->connection->rollback(); 
     39            } 
     40        } catch(Exception $e){ 
     41            throw $e; 
     42        } 
    1643    } 
    17     public function suspend(){ 
     44    public final function suspend(){ 
     45        return $this->connection; 
    1846    } 
    19     public function resume(PDO $connection){ 
     47    public final function resume(PDO $connection){ 
     48        $this->connection = $connection; 
    2049    } 
    2150    public final function complete(Exception $e){ 
  • lang/php/misc/Hermit/src/Hermit/tx/HermitMandatoryTx.php

    r19344 r20417  
    55 */ 
    66class HermitMandatoryTx extends AbstractHermitTx { 
     7    public function proceed(HermitProxy $proxy, $name, array $parameters){ 
     8        if(!$this->hasTransaction()){ 
     9            throw new LogicException('No transaction'); 
     10        } 
     11        return $proxy->request($name, $parameters); 
     12    } 
    713} 
  • lang/php/misc/Hermit/src/Hermit/tx/HermitNeverTx.php

    r19344 r20417  
    55 */ 
    66class HermitNeverTx extends AbstractHermitTx { 
     7    public function proceed(HermitProxy $proxy, $name, array $parameters){ 
     8        if($this->hasTransaction()){ 
     9            throw new LogicException('Already associated with another transaction'); 
     10        } 
     11        return $proxy->request($name, $parameters); 
     12    } 
    713} 
  • lang/php/misc/Hermit/src/Hermit/tx/HermitRequiredTx.php

    r19344 r20417  
    55 */ 
    66class HermitRequiredTx extends AbstractHermitTx { 
     7    public function proceed(HermitProxy $proxy, $name, array $parameters){ 
     8        $began = false; 
     9 
     10        if(!$this->hasTransaction()){ 
     11            $this->begin(); 
     12            $began = true; 
     13        } 
     14        $ret = null; 
     15        try { 
     16            $ret = $proxy->request($name, $parameters); 
     17            if($began){ 
     18                $this->commit(); 
     19            } 
     20            return $ret; 
     21        } catch(Exception $e){ 
     22            if($began){ 
     23                $this->complete($e); 
     24            } 
     25            throw $e; 
     26        } 
     27    } 
    728} 
  • lang/php/misc/Hermit/src/Hermit/tx/HermitRequiresNewTx.php

    r19344 r20417  
    55 */ 
    66class HermitRequiresNewTx extends AbstractHermitTx { 
     7    public function proceed(HermitProxy $proxy, $name, array $parameters){ 
     8        $this->begin(); 
     9        try { 
     10            $ret = $proxy->request($name, $parameters); 
     11            $this->commit(); 
     12            return $ret; 
     13        } catch(Exception $e){ 
     14            $this->complete($e); 
     15            throw $e; 
     16        } 
     17    } 
    718} 
  • lang/php/misc/Hermit/test/db_init.php

    r20387 r20417  
    55    $_db_init = 'db_' . $name . '_init'; 
    66    $_db_init($pdo); 
    7      
     7 
    88    $sqlpath = dirname(__FILE__) . '/resource/test-' . $name . '.sql.php'; 
    99    if(file_exists($sqlpath)){