Changeset 20966 for lang/php

Show
Ignore:
Timestamp:
10/08/08 18:17:27 (3 months ago)
Author:
nowelium
Message:

DataSource取得について、Event方式に。selectとupdate系でdatasourceを切り替えるために。
その他もろもろあげわすれupdate

Location:
lang/php/misc/Hermit/src/Hermit
Files:
3 added
13 modified

Legend:

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

    r20314 r20966  
    3434    protected static function __create($targetClass){ 
    3535        if(is_object($targetClass)){ 
    36             return HermitObjectProxy::delegate(new ReflectionObject($targetClass), $targetClass); 
     36            $refObject = new ReflectionObject($targetClass); 
     37            $ctx = new HermitContext($refObject->getName()); 
     38            return HermitObjectProxy::delegate($ctx, $refObject, $targetClass); 
    3739        } 
    3840        $reflector = new ReflectionClass($targetClass); 
     41        $ctx = new HermitContext($targetClass); 
    3942        if($reflector->isInterface()){ 
    40             return HermitInterfaceProxy::delegate($reflector); 
     43            return HermitInterfaceProxy::delegate($ctx, $reflector); 
    4144        } 
    42         return HermitClassProxy::delegate($reflector); 
     45        return HermitClassProxy::delegate($ctx, $reflector); 
    4346    } 
    4447    protected static function wrap(HermitProxy $proxy, $targetClass){ 
  • lang/php/misc/Hermit/src/Hermit/HermitDataSourceManager.php

    r19775 r20966  
    66abstract class HermitDataSourceManager { 
    77    private static $default; 
     8    private static $callback; 
    89    private static $datasources = array(); 
    910    private function __construct(){ 
     11        // mp@ 
    1012    } 
    1113    public static function setDefault(PDO $default){ 
    1214        self::$default = $default; 
    1315    } 
     16    public static function hasDefault(){ 
     17        return null !== self::$default; 
     18    } 
     19    public static function setCallback(array $callback){ 
     20        self::$callback = $callback; 
     21    } 
     22    public static function hasCallback(){ 
     23        return null !== self::$callback; 
     24    } 
    1425    public static function set($targetClass, PDO $pdo){ 
    1526        self::$datasources[$targetClass] = $pdo; 
    1627    } 
    17     public static function get($targetClass){ 
     28    public static function get($targetClass, $method = null, $type = HermitEvent::UNKNOWN){ 
     29        if(self::hasCallback()){ 
     30            return call_user_func_array(self::$callback, array($targetClass, $method, $type)); 
     31        } 
     32 
    1833        if(!self::has($targetClass)){ 
    1934            return self::$default; 
  • lang/php/misc/Hermit/src/Hermit/command/HermitDeleteCommand.php

    r20200 r20966  
    44 * @author nowelium 
    55 */ 
    6 class HermitDeleteCommand implements HermitSqlCommand { 
    7     public function execute(PDO $pdo, array $params){ 
     6class HermitDeleteCommand extends AbstractHermitSqlCommand { 
     7    public function execute(array $parameters){ 
     8        $pdo = $this->getConnection(HermitEvent::EVT_DELETE); 
     9        $builder = new HermitStatementBuilder($this->method, $this->sqlCreator); 
     10        $stmt = $builder->build($pdo); 
     11        $stmt->execute($parameters); 
     12        $resultset = new HermitUpdateQueryResultSet; 
     13        return $resultset->execute($stmt, $this->type); 
    814    } 
    915} 
  • lang/php/misc/Hermit/src/Hermit/command/HermitInsertCommand.php

    r20200 r20966  
    44 * @author nowelium 
    55 */ 
    6 class HermitInsertCommand implements HermitSqlCommand { 
    7   public function execute(PDO $pdo, array $parameters){ 
    8   } 
     6class HermitInsertCommand extends AbstractHermitSqlCommand { 
     7    public function execute(array $parameters){ 
     8        $pdo = $this->getConnection(HermitEvent::EVT_INSERT); 
     9        $builder = new HermitStatementBuilder($this->method, $this->sqlCreator); 
     10        $stmt = $builder->build($pdo); 
     11        $stmt->execute($parameters); 
     12        $resultset = new HermitUpdateQueryResultSet; 
     13        return $resultset->execute($stmt, $this->type); 
     14    } 
    915} 
  • lang/php/misc/Hermit/src/Hermit/command/HermitProcedureCommand.php

    r20492 r20966  
    44 * @author nowelium 
    55 */ 
    6 class HermitProcedureCommand implements HermitSqlCommand { 
    7     private $method; 
    8     private $sqlCreator; 
    9     private $type; 
    10     private $annote; 
    11     public function setMethod(ReflectionMethod $method){ 
    12         $this->method = $method; 
    13     } 
    14     public function setSqlCreator(HermitSqlCreator $sqlCreator){ 
    15         $this->sqlCreator = $sqlCreator; 
    16     } 
    17     public function setValueType(HermitValueType $type){ 
    18         $this->type = $type; 
    19     } 
     6class HermitProcedureCommand extends AbstractHermitSqlCommand { 
     7    protected $annote; 
    208    public function setAnnote(HermitAnnote $annote){ 
    219        $this->annote = $annote; 
    2210    } 
    23     public function execute(PDO $pdo, array $parameters){ 
     11    public function execute(array $parameters){ 
     12        $pdo = $this->getConnection(HermitEvent::EVT_PROCEDURE); 
    2413        if($this->sqlCreator instanceof HermiSetupSqlCreator){ 
    2514            if($this->sqlCreator->hasSetupSql()){ 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSelectCommand.php

    r20387 r20966  
    44 * @author nowelium 
    55 */ 
    6 class HermitSelectCommand implements HermitSqlCommand { 
    7     private $method; 
    8     private $sqlCreator; 
    9     private $type; 
    10     public function __construct(ReflectionMethod $method, HermitSqlCreator $sqlCreator, HermitValueType $type){ 
    11         $this->method = $method; 
    12         $this->sqlCreator = $sqlCreator; 
    13         $this->type = $type; 
    14     } 
    15     public function execute(PDO $pdo, array $parameters){ 
     6class HermitSelectCommand extends AbstractHermitSqlCommand { 
     7    public function execute(array $parameters){ 
     8        $pdo = $this->getConnection(HermitEvent::EVT_SELECT); 
    169        $builder = new HermitStatementBuilder($this->method, $this->sqlCreator); 
    1710        $stmt = $builder->build($pdo); 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommand.php

    r19682 r20966  
    55 */ 
    66interface HermitSqlCommand { 
    7     public function execute(PDO $pdo, array $params); 
     7    public function execute(array $params); 
    88} 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommandFactory.php

    r20387 r20966  
    66class HermitSqlCommandFactory { 
    77 
     8    protected $context; 
    89    protected $annote; 
    910    protected $reflector; 
    1011    protected $createdCommands = array(); 
    1112 
    12     public function __construct(ReflectionClass $reflector){ 
     13    public function __construct(HermitContext $ctx, ReflectionClass $reflector){ 
     14        $this->context = $ctx; 
    1315        $this->annote = HermitAnnote::create($reflector); 
    1416        $this->reflector = $reflector; 
     
    1719        return $this->annote->hasMethod($methodName); 
    1820    } 
    19     public function create(PDO $pdo, $methodName){ 
     21    public function create($methodName){ 
    2022        if(isset($this->createdCommands[$methodName])){ 
    2123            return $this->createdCommands[$methodName]; 
    2224        } 
    2325        $method = $this->annote->getMethod($methodName); 
    24         $command = $this->createCommand($pdo, $method); 
     26        $command = $this->createCommand($method); 
    2527        return $this->createdCommands[$methodName] = $command; 
    2628    } 
    27     protected function createCommand(PDO $pdo, ReflectionMethod $method){ 
     29    protected function createCommand(ReflectionMethod $method){ 
    2830        $methodName = $method->getName(); 
     31        $pdo = HermitDataSourceManager::get($this->context->getTargetClass(), $methodName, HermitEvent::EVT_SETUP); 
    2932        if($this->annote->isProcedureMethod($method)){ 
    3033            return $this->createProcedureCommand($pdo, $method); 
     
    4851 
    4952        $command = new HermitProcedureCommand; 
     53        $command->setContext($this->context); 
    5054        $command->setMethod($method); 
    5155        $command->setSqlCreator($creator); 
     
    6973        throw new BadMethodCallException('method: "' . $method->getName() . '" was not apply to Procedure command'); 
    7074    } 
    71     protected function createInsertCommand(ReflectionMethod $method){ 
    72         throw new RuntimeException('T.B.D'); 
     75    protected function createInsertCommand(PDO $pdo, ReflectionMethod $method){ 
     76        $dbms = HermitDatabaseMetaFactory::getDbms($pdo); 
     77        $creator = $this->createInsertSqlCreator($method, $dbms); 
     78        $creator->initialize($pdo, $method, $this->annote); 
     79        $valueType = HermitValueTypeFactory::create($this->annote, $method); 
     80        $command = new HermitInsertCommand; 
     81        $command->setContext($this->context); 
     82        $command->setMethod($method); 
     83        $command->setSqlCreator($creator); 
     84        $command->setValueType($valueType); 
     85        return $command; 
    7386    } 
    74     protected function createUpdateCommand(ReflectionMethod $method){ 
    75         throw new RuntimeException('T.B.D'); 
     87    protected function createInsertSqlCreator($method, $dbms){ 
     88        $sql = $this->annote->getSql($method, $dbms); 
     89        if(null !== $sql){ 
     90            return new HermitStaticSqlCreator($sql); 
     91        } 
     92        $sql = $this->annote->getFile($method, $dbms); 
     93        if(null !== $sql){ 
     94            return new HermitStaticSqlCreator($sql); 
     95        } 
     96        return new HermitAutoInsertSqlCreator; 
    7697    } 
    77     protected function createDeleteCommand(ReflectionMethod $method){ 
    78         throw new RuntimeException('T.B.D'); 
     98    protected function createUpdateCommand(PDO $pdo, ReflectionMethod $method){ 
     99        $dbms = HermitDatabaseMetaFactory::getDbms($pdo); 
     100        $creator = $this->createUpdateSqlCreator($method, $dbms); 
     101        $creator->initialize($pdo, $method, $this->annote); 
     102        $valueType = HermitValueTypeFactory::create($this->annote, $method); 
     103        $command = new HermitUpdateCommand; 
     104        $command->setContext($this->context); 
     105        $command->setMethod($method); 
     106        $command->setSqlCreator($creator); 
     107        $command->setValueType($valueType); 
     108        return $command; 
     109    } 
     110    protected function createUpdateSqlCreator($method, $dbms){ 
     111        $sql = $this->annote->getSql($method, $dbms); 
     112        if(null !== $sql){ 
     113            return new HermitStaticSqlCreator($sql); 
     114        } 
     115        $sql = $this->annote->getFile($method, $dbms); 
     116        if(null !== $sql){ 
     117            return new HermitStaticSqlCreator($sql); 
     118        } 
     119        return new HermitAutoUpdateSqlCreator; 
     120    } 
     121    protected function createDeleteCommand(PDO $pdo, ReflectionMethod $method){ 
     122        $dbms = HermitDatabaseMetaFactory::getDbms($pdo); 
     123        $creator = $this->createDeleteSqlCreator($method, $dbms); 
     124        $creator->initialize($pdo, $method, $this->annote); 
     125        $valueType = HermitValueTypeFactory::create($this->annote, $method); 
     126        $command = new HermitDeleteCommand; 
     127        $command->setContext($this->context); 
     128        $command->setMethod($method); 
     129        $command->setSqlCreator($creator); 
     130        $command->setValueType($valueType); 
     131        return $command; 
     132    } 
     133    protected function createDeleteSqlCreator($method, $dbms){ 
     134        $sql = $this->annote->getSql($method, $dbms); 
     135        if(null !== $sql){ 
     136            return new HermitStaticSqlCreator($sql); 
     137        } 
     138        $sql = $this->annote->getFile($method, $dbms); 
     139        if(null !== $sql){ 
     140            return new HermitStaticSqlCreator($sql); 
     141        } 
     142        return new HermitAutoDeleteSqlCreator; 
    79143    } 
    80144    protected function createSelectCommand(PDO $pdo, ReflectionMethod $method){ 
     
    86150        } 
    87151        $valueType = HermitValueTypeFactory::create($this->annote, $method); 
    88         return new HermitSelectCommand($method, $creator, $valueType); 
     152        $command = new HermitSelectCommand; 
     153        $command->setContext($this->context); 
     154        $command->setMethod($method); 
     155        $command->setSqlCreator($creator); 
     156        $command->setValueType($valueType); 
     157        return $command; 
    89158    } 
    90159    protected function createSelectSqlCreator(ReflectionMethod $method, $dbms){ 
  • lang/php/misc/Hermit/src/Hermit/command/HermitUpdateCommand.php

    r20200 r20966  
    44 * @author nowelium 
    55 */ 
    6 class HermitUpdateCommand implements HermitSqlCommand { 
    7     public function execute(PDO $pdo, array $params){ 
     6class HermitUpdateCommand extends AbstractHermitSqlCommand { 
     7    public function execute(array $parameters){ 
     8        $pdo = $this->getConnection(HermitEvent::EVT_UPDATE); 
     9        $builder = new HermitStatementBuilder($this->method, $this->sqlCreator); 
     10        $stmt = $builder->build($pdo); 
     11        $stmt->execute($parameters); 
     12        $resultset = new HermitUpdateQueryResultSet; 
     13        return $resultset->execute($stmt, $this->type); 
    814    } 
    915} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitClassProxy.php

    r20200 r20966  
    55 */ 
    66class HermitClassProxy implements HermitFutureProxy { 
     7    private $context; 
    78    private $reflector; 
    8     protected function __construct(ReflectionClass $reflector){ 
     9    protected function __construct(HermitContext $ctx, ReflectionClass $reflector){ 
     10        $this->context = $ctx; 
    911        $this->reflector = $reflector; 
    1012    } 
    11     public static function delegate(ReflectionClass $reflector, $instance = null){ 
    12         return new self($reflector); 
     13    public static function delegate(HermitContext $ctx, ReflectionClass $reflector, $instance = null){ 
     14        return new self($ctx, $reflector); 
    1315    } 
    1416    public function request($name, array $params){ 
     17        throw new RuntimeException('T.B.D'); 
    1518    } 
    1619} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitFutureProxy.php

    r19682 r20966  
    55 */ 
    66interface HermitFutureProxy extends HermitProxy { 
    7     public static function delegate(ReflectionClass $reflector, $instance = null); 
     7    public static function delegate(HermitContext $ctx, ReflectionClass $reflector, $instance = null); 
    88} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitInterfaceProxy.php

    r19426 r20966  
    55 */ 
    66class HermitInterfaceProxy implements HermitFutureProxy { 
     7    protected $context; 
    78    protected $reflector; 
    89    protected $commandFactory; 
    9     protected function __construct(ReflectionClass $reflector){ 
     10    protected function __construct(HermitContext $ctx, ReflectionClass $reflector){ 
     11        $this->context = $ctx; 
    1012        $this->reflector = $reflector; 
    11         $this->commandFactory = new HermitSqlCommandFactory($reflector); 
     13        $this->commandFactory = new HermitSqlCommandFactory($ctx, $reflector); 
    1214    } 
    13     public static function delegate(ReflectionClass $reflector, $instance = null){ 
    14         return new self($reflector); 
     15    public static function delegate(HermitContext $ctx, ReflectionClass $reflector, $instance = null){ 
     16        return new self($ctx, $reflector); 
    1517    } 
    1618    public function request($name, array $params){ 
     
    1820            throw new BadMethodCallException($this->reflector->getName() . '::' . $name); 
    1921        } 
    20         $pdo = HermitDataSourceManager::get($this->reflector->getName()); 
    21         $command = $this->commandFactory->create($pdo, $name); 
    22         return $command->execute($pdo, $params); 
     22        $command = $this->commandFactory->create($this->context, $name); 
     23        return $command->execute($params); 
    2324    } 
    2425} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitObjectProxy.php

    r19836 r20966  
    55 */ 
    66class HermitObjectProxy implements HermitFutureProxy { 
     7    protected $context; 
    78    protected $target; 
    89    protected $annote; 
    9     protected function __construct(ReflectionClass $reflector, $target){ 
     10    protected function __construct(HermitContext $ctx, ReflectionClass $reflector, $target){ 
     11        $this->context = $ctx; 
    1012        $this->target = $target; 
    1113        $this->annote = HermitAnnote::create($reflector); 
    1214    } 
    13     public static function delegate(ReflectionClass $reflector, $instance = null){ 
    14         return new self($reflector, $instance); 
     15    public static function delegate(HermitContext $ctx, ReflectionClass $reflector, $instance = null){ 
     16        return new self($ctx, $reflector, $instance); 
    1517    } 
    1618    public function request($name, array $params){