/components/charcoal/db/SmartGateway.class.php
PHP | 1489 lines | 909 code | 277 blank | 303 comment | 42 complexity | 81959cdf8110579620aa64360b2669c5 MD5 | raw file
- <?php
- /**
- * SmartGateway???
- *
- * PHP version 5
- *
- * @package core
- * @author CharcoalPHP Development Team
- * @copyright 2008 - 2012 CharcoalPHP Development Team
- */
- class Charcoal_SmartGateway extends Charcoal_CharcoalComponent implements Charcoal_IComponent
- {
- private $_source;
- private $_builder;
- /*
- * ???????
- */
- public function __construct()
- {
- parent::__construct();
- }
- /*
- * Initialize instance
- *
- * @param Charcoal_Config $config configuration data
- */
- public function configure( Charcoal_Config $config )
- {
- log_info( "smart_gateway", "???????????????" );
- // ?????????????
- $data_source_name = Charcoal_Profile::getString( s('DB_DATA_SOURCE') );
- log_info( "smart_gateway", "???????: $data_source_name" );
- $this->_source = Charcoal_Factory::createObject( s($data_source_name), s('data_source') );
- log_info( "smart_gateway", "??????????????" );
- // SQL??????????
- $builder_name = $this->_source->getBackend();
- log_info( "smart_gateway", "SQL????: $builder_name" );
- $this->_builder = Charcoal_Factory::createObject( s($builder_name), s('sql_builder') );
- log_info( "smart_gateway", "???????????????????" );
- }
- /*
- * ?????????
- */
- public function initDataSource()
- {
- // ?????????????
- if ( !$this->_source ){
- $data_source_name = Charcoal_Profile::getString( s('DB_DATA_SOURCE') );
- $this->_source = Charcoal_Factory::createObject( s($data_source_name), s('data_source') );
- }
- }
- /*
- * SQL??????
- */
- public function initSQLBuilder()
- {
- // SQL??????????
- if ( !$this->_builder ){
- $builder_name = $this->_source->getBackend();
- $this->_builder = Charcoal_Factory::createObject( s($builder_name), s('sql_builder') );
- }
- }
- /*
- * ?????????
- */
- public function getDataSource()
- {
- return $this->_source;
- }
- /*
- * ?????????
- */
- public function setDataSource( Charcoal_IDataSource $source )
- {
- $this->_source = $source;
- }
- /*
- * SQL???
- */
- public function getSQLBuilder()
- {
- return $this->_builder;
- }
- /*
- * ?????????ON/OFF
- */
- public function autoCommit( Charcoal_Boolean $onoff = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- try{
- if ( $onoff ){
- $this->_source->autoCommit($onoff);
- }
- else{
- $this->_source->autoCommit();
- }
- log_debug( "debug, smart_gateway", "done: autoCommit" );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBAutoCommitException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ???????????
- */
- public function beginTrans()
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- try{
- $this->_source->beginTrans();
- log_debug( "debug, smart_gateway", "done: beginTrans" );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBBeginTransactionException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ???????
- */
- public function commitTrans()
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- try{
- $this->_source->commitTrans();
- log_debug( "debug, smart_gateway", "done: commitTrans" );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBCommitTransactionException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ?????????
- */
- public function rollbackTrans()
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- try{
- $this->_source->rollbackTrans();
- log_debug( "debug, smart_gateway", "done: rollbackTrans" );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBRollbackTransactionException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ???????????????????
- */
- public function updateAll( Charcoal_String $model_name, Charcoal_TableDTO $data, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordSaveFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- try{
- list( $sql, $params ) = $this->_builder->buildUpdateSQL( $model, $data, $criteria );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // ???????
- if ( $filter ){
- $filter->beforeSave( $data );
- }
- // SQL??
- $this->_source->prepareExecute( s($sql), v($params) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ???????????
- */
- public function updateField( Charcoal_String $model_name, Charcoal_Integer $data_id, Charcoal_String $field, $value )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- try{
- // DTO
- $dto = $model->createDTO();
- $dto->$field = us($value);
- // log_debug( "debug, smart_gateway", "dto:" . print_r($dto,true) );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // ??
- $pk = $model->getPrimaryKey();
- $where = "$pk = ?";
- $params = array( ui($data_id) );
- $criteria = new Charcoal_SQLCriteria( s($where), v($params) );
- // SQL????????
- list( $sql, $params ) = $this->_builder->buildUpdateSQL( $model, $dto, $criteria );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $this->_source->prepareExecute( s($sql), v($params) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ?????????????(NOW)???
- */
- public function updateFieldNow( Charcoal_String $model_name, Charcoal_Integer $data_id, Charcoal_String $field )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- try{
- $field = us($field);
- // ??????????????
- $override[$field]['update'] = new Charcoal_AnnotationValue( $model, s('update'), s('function'), v(array('now')) );
- // log_debug( "debug, smart_gateway", "override:" . print_r($override,true) );
- // ??
- $pk = $model->getPrimaryKey();
- $where = "$pk = ?";
- $params = array( ui($data_id) );
- $criteria = new Charcoal_SQLCriteria( s($where), v($params) );
- // SQL????????
- list( $sql, $params ) = $this->_builder->buildUpdateSQL( $model, $model->createDTO(), $criteria, p($override) );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $this->_source->prepareExecute( s($sql), v($params) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ??????????????
- */
- public function updateFields( Charcoal_String $model_name, Charcoal_Integer $data_id, Charcoal_Properties $data )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- try{
- // DTO
- $dto = $model->createDTO();
- $data = up($data);
- foreach( $data as $field => $value ){
- $dto->$field = $value;
- }
- log_debug( "debug, smart_gateway", "dto:" . print_r($dto,true) );
- // ??
- $pk = $model->getPrimaryKey();
- $where = "$pk = ?";
- $params = array( ui($data_id) );
- $criteria = new Charcoal_SQLCriteria( s($where), v($params) );
- // SQL????????
- list( $sql, $params ) = $this->_builder->buildUpdateSQL( $model, $dto, $criteria );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $this->_source->prepareExecute( s($sql), v($params) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ??
- */
- public function insert( Charcoal_String $model_name, Charcoal_TableDTO $save_data, Charcoal_IRecordSaveFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- $dto = clone $save_data;
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- try{
- // ???????
- if ( $filter ){
- $filter->beforeSave( $dto );
- }
- // SQL???
- list( $sql, $params ) = $this->_builder->buildInsertSQL( $model, $dto );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $this->_source->prepareExecute( s($sql), v($params) );
- // ID???
- $sql = $this->_builder->buildLastIdSQL();
-
- // ??
- $result = $this->_source->prepareExecute( s($sql) );
- // ????
- $row = $this->_source->fetchArray( $result );
- $new_id = $row[0];
- log_debug( "debug, smart_gateway", "new_id:$new_id" );
- return $new_id;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ??
- */
- public function save( Charcoal_String $model_name, Charcoal_TableDTO $save_data, Charcoal_IRecordSaveFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- $dto = clone $save_data;
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- try{
- // UPDATE?INSERT???
- $pk = us($model->getPrimaryKey());
- if ( isset($dto->$pk) ){
- // ???????????????????????INSERT????UPDATE
- $db_dto = $this->findByID( $model_name, s($dto->$pk) );
- $is_new = ($db_dto === NULL);
- }
- else{
- // ????????????????????INSERT
- $is_new = TRUE;
- }
- // ???????
- if ( $filter ){
- $filter->beforeSave( $dto );
- }
- // SQL???
- if ( $is_new ){
- list( $sql, $params ) = $this->_builder->buildInsertSQL( $model, $dto );
- }
- else{
- list( $sql, $params ) = $this->_builder->buildUpdateSQL( $model, $dto );
- }
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $this->_source->prepareExecute( s($sql), v($params) );
- // ID???
- if ( $is_new ){
- $sql = $this->_builder->buildLastIdSQL();
- log_debug( "debug, smart_gateway", "sql:$sql" );
-
- // ??
- $result = $this->_source->prepareExecute( s($sql) );
- // ????
- $row = $this->_source->fetchArray( $result );
- $new_id = $row[0];
- }
- else{
- $new_id = $dto->$pk;
- }
- log_debug( "debug, smart_gateway", "new_id:$new_id" );
- return $new_id;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * SQL??(INSERT/DELETE/UPDATE)
- */
- public function execute( Charcoal_String $sql, Charcoal_Vector $params = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ??
- $this->_source->prepareExecute( $sql, $params );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * SQL??(SELECT)
- */
- public function queryValue( Charcoal_String $sql, Charcoal_Vector $params = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // DataSource??????????
- $ds = $this->_source;
- // ??
- $result = $ds->prepareExecute( $sql, $params );
- // ????
- $a = array();
- while( $row = $ds->fetchAssoc( $result ) ){
- $value = array_shift($row);
- log_debug( "debug, smart_gateway", "queryValue:$value" );
- return $value;
- }
- return NULL;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * SQL??(SELECT)
- */
- public function query( Charcoal_String $sql, Charcoal_Vector $params = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // DataSource??????????
- $ds = $this->_source;
- // ??
- $result = $ds->prepareExecute( $sql, $params );
- // ????
- $a = array();
- while( $row = $ds->fetchAssoc( $result ) ){
- $a[] = $row;
- }
- return $a;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * SQL??
- */
- public function recursiveFindBySQL( Charcoal_String $model_name, Charcoal_String $sql, Charcoal_Vector $params = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- $ret_val = array();
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- // DataSource???
- $ds = $this->_source;
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = trim($model_name_list[0]);
- $current_model = Charcoal_TableModelCache::get( s($current_model) );
- $model_name_list->removeHead();
- // ???????
- $relation_list = $current_model->getRelationList();
- // ???????
- $field_list = $current_model->getFieldList();
- // ??
- $result = $ds->prepareExecute( s($sql), $params );
- // ????????
- $num_rows = $ds->numRows( $result );
- // ????
- while( $row = $ds->fetchAssoc( $result ) )
- {
- $dto = $current_model->createDTO();
- // SQL???????DTO??????
- foreach ($field_list as $name)
- {
- // ??????
- $value = isset($row[$name]) ? $row[ $name ] : NULL;
- // ????????
- if ( !property_exists($dto,$name) ){
- $clazz = get_class($dto);
- log_error( "error", "DTO[$clazz] does not have property[$name]. properties are:" . print_r(get_object_vars($dto),true) );
- _throw( new Charcoal_DBException( s("Field[$name] is not defined on DTO[$clazz]") ) );
- }
- $dto->$name = $value;
- }
- // ?????????
- if ( $filter ){
- $filter->afterFind( $dto );
- }
- // ?????
- foreach ($relation_list as $field => $relation)
- {
- $target = $relation->target; // ??????????
- $foreign_key = $relation->foreign_key; // ?????
- $key_field = $relation->key_field; // ????????
- $linkage = $relation->linkage; // ???????
- $order_by = $relation->order_by; // ????
- $extract = $relation->extract; // ???????
- $restrict = $relation->restrict; // ??
- $table_name = $target->getTableName(); // ???????
- // SQL??
- $params = array();
- switch ( $linkage->getValue() ){
- case 'inner':
- {
- // ??????? : ????????????????
- $params[] = isset($row[ $foreign_key ]) ? $row[ $foreign_key ] : NULL;
- $sql = "select * from $table_name where $key_field = ?";
- }
- break;
- case 'outer':
- {
- // ??????? : ???????????????
- $params[] = isset($row[ $key_field ]) ? $row[ $key_field ] : NULL;
- $sql = "select * from $table_name where $foreign_key = ?";
- }
- break;
- default:
- {
- // ?????????
- _throw( new Charcoal_DBException( "Invalid linkage anottation : $linkage" ) );
- }
- break;
- }
- // ????????@restrict
- if ( $restrict ){
- if ( $restrict instanceof Charcoal_AnnotationValue ){
- $restrict_value = $restrict->getValue();
- $restrict_params = $restrict->getParameters();
- switch( count($restrict_params) ){
- case 1:
- {
- $p = $restrict_params[0];
- $sql .= " and $restrict_value = ?";
- $params[] = ($p == 'NULL') ? NULL : $p;
- }
- break;
- case 2:
- {
- $op = $restrict_params[0];
- $p = $restrict_params[1];
- if ( stripos($p,'NOW') === 0 ){
- $sql .= " and $restrict_value $op NOW()";
- }
- else{
- $sql .= " and $restrict_value $op ?";
- $params[] = ($p == 'NULL') ? NULL : $p;
- }
- }
- break;
- }
- }
- else if ( is_array($restrict) ){
- foreach( $restrict as $restrict_item )
- {
- $restrict_value = $restrict_item->getValue();
- $restrict_params = $restrict_item->getParameters();
- switch( count($restrict_params) ){
- case 1:
- {
- $p = $restrict_params[0];
- $sql .= " and $restrict_value = ?";
- $params[] = ($p == 'NULL') ? NULL : $p;
- }
- break;
- case 2:
- {
- $op = $restrict_params[0];
- $p = $restrict_params[1];
- if ( stripos($p,'NOW') === 0 ){
- $sql .= " and $restrict_value $op NOW()";
- }
- else{
- $sql .= " and $restrict_value $op ?";
- $params[] = ($p == 'NULL') ? NULL : $p;
- }
- }
- break;
- }
- }
- }
- }
- // ????
- if ( !empty($order_by) ){
- $sql .= " order by " . $order_by->getValue();
- }
- // ?????????
- $index = -1;
- $target_model_id = trim( us($target->getModelID()) );
- if ( $model_name_list instanceof Charcoal_Vector ){
- foreach( $model_name_list as $key => $model_id ){
- if ( trim($model_id) == $target_model_id ){
- $index = $key;
- }
- }
- }
- if ( $index >= 0 ){
- // ????????????????????????
- $related_model_name_list = clone $model_name_list;
- list( $related_model_name ) = $related_model_name_list->remove( i($index) );
- $join = $related_model_name_list->join( s(',') );
- $related_model_name = $join ? $related_model_name . ',' . $join : $related_model_name;
- // ???DTO????
- switch ( $extract->getValue() ){
- case 'field':
- {
- $sql .= " limit 1";
- $child_result = $this->recursiveFindBySQL( s($related_model_name), s($sql), v($params), $filter );
- // ????????
- if ( $child_result ){
- // ???????????
- $child = array_shift( $child_result );
- if ( $child ){
- // ????????????????DTO????
- $dto->$field = $child;
- }
- }
- }
- break;
- case 'array':
- {
- $child_result = $this->recursiveFindBySQL( s($related_model_name), s($sql), v($params), $filter );
- // ??????
- $dto->$field = $child_result;
- }
- break;
- }
- }
- else{
- $dto->$field = NULL;
- }
- }
-
- $ret_val[] = $dto;
- }
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- return $ret_val;
- }
- /*
- * ???????
- */
- public function findFirst( Charcoal_String $model_name, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- try{
- if ( !$criteria ){
- $criteria = new Charcoal_SQLCriteria();
- }
- // LIMIT=1?
- $criteria->setLimit( i(1) );
- // ????
- $result = $this->findAll( $model_name, $criteria, $filter );
- return $result ? array_shift($result) : NULL;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ????????????
- */
- public function findFirstForUpdate( Charcoal_String $model_name, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- try{
- if ( !$criteria ){
- $criteria = new Charcoal_SQLCriteria();
- }
- // LIMIT=1?
- $criteria->setLimit( i(1) );
- // ????
- $result = $this->findAllForUpdate( $model_name, $criteria, $filter );
- return $result ? array_shift($result) : NULL;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ????
- */
- public function findAll( Charcoal_String $model_name, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
-
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = trim($model_name_list[0]);
- $current_model = Charcoal_TableModelCache::get( s($current_model) );
- // ?????
- $table = $current_model->getTableName();
- // SQL??????
- $builder = $this->_builder;
- // SQL???
- $sql = $builder->buildSelectSQL( $current_model, $criteria );
- // ?????
- $params = $criteria ? $criteria->getParams() : NULL;
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // ????
- $result = $this->recursiveFindBySQL( s($model_name), s($sql), v($params), $filter );
- return $result;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ?????????
- */
- public function findAllForUpdate( Charcoal_String $model_name, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
-
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = trim($model_name_list[0]);
- $current_model = Charcoal_TableModelCache::get( s($current_model) );
- // ?????
- $table = $current_model->getTableName();
- // SQL??????
- $builder = $this->_builder;
- // SQL???
- $sql = $builder->buildSelectSQLForUpdate( $current_model, $criteria );
- // ?????
- $params = $criteria ? $criteria->getParams() : NULL;
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // ????
- $result = $this->recursiveFindBySQL( s($model_name), s($sql), v($params), $filter );
- return $result;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ??????????
- */
- public function findDistinct( Charcoal_String $model_name, Charcoal_Vector $fields, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = Charcoal_TableModelCache::get( s($model_name_list[0]) );
- // ?????
- $table = $current_model->getTableName();
- // SQL??????
- $builder = $this->_builder;
- // SQL???
- $sql = $builder->buildSelectDistinctSQL( $current_model, $fields, $criteria );
- // ?????
- $params = $criteria ? $criteria->getParams() : NULL;
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // ????
- $result = $this->recursiveFindBySQL( s($model_name), s($sql), v($params), $filter );
- return $result;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ?????????????
- */
- public function findAllBy( Charcoal_String $model_name, Charcoal_String $field, Charcoal_String $value, Charcoal_SQLCriteria $criteria = NULL, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- if ( !$criteria ){
- $criteria = new Charcoal_SQLCriteria();
- }
- $field = us( $field );
- $criteria->setWhere( $field . ' = ?' );
- $criteria->setParams( array( $value ) );
- $a = $this->findAll( $model_name, $criteria, $filter );
- return $a;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ID????(??????????)
- */
- public function findByID( Charcoal_String $model_name, Charcoal_String$id, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = Charcoal_TableModelCache::get( s($model_name_list[0]) );
- $model_name_list->removeHead();
- // ????????????????
- $pk = $current_model->getPrimaryKey();
- // ??????????
- $where_clause = s( $pk . ' = ?');
- $params = v(array( Charcoal_System::toString($id) ));
- $criteria = new Charcoal_SQLCriteria( $where_clause, $params );
- $result = $this->findAll( $model_name, $criteria, $filter );
- return $result ? array_shift( $result ) : NULL;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ID????
- */
- public function findAllByID( Charcoal_String $model_name, Charcoal_Vector $id_array, Charcoal_IRecordFindFilter $filter = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
-
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = Charcoal_TableModelCache::get( s($model_name_list[0]) );
- $model_name_list->removeHead();
- // ?????????
- $where = array();
- $params = array();
-
- foreach( $id_array as $id ){
- $where[] = "?";
- $params[] = $id;
- }
- $pk = $current_model->getPrimaryKey();
- $where_clause = "$pk in (" . implode(",",$where) . ")";
- $criteria = new Charcoal_SQLCriteria( s($where_clause), v($params) );
- $a = $this->findAll( $model_name, $criteria, $filter );
- return $a;
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ??????????????
- */
- public function destroyById( Charcoal_String $model_name, Charcoal_Integer $id )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
-
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- $id = us( $id );
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- // ????????
- $pk = us($model->getPrimaryKey());
- // SQL?????
- $params = array( ui($id) );
- // ??
- $criteria = new Charcoal_SQLCriteria();
- $criteria->setWhere( s($pk . ' = ?') );
- $criteria->setParams( v($params) );
- // SQL??????SQL???
- $sql = $this->_builder->buildDeleteSQL( $model, $criteria );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- $this->execute( s($sql), v($params) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ?????????????
- */
- public function destroyAllById( Charcoal_String $model_name, Charcoal_Vector $id_array )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- // ?????
- $table = $model->getTableName();
- // ?????????
- $where = array();
- $params = array();
-
- foreach( $id_array as $id ){
- $where[] = "?";
- $params[] = $id;
- }
- $sql = "delete from $table where id in (" . implode(",",$where) . ")";
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- $this->execute( $sql, $params );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ?????????????????????
- */
- public function destroyBy( Charcoal_String $model_name, Charcoal_String $field, Charcoal_String $value )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- $where = us($field) . ' = ?';
- $params = array( us($value) );
- $criteria = new Charcoal_SQLCriteria( s($where), v($params) );
- $this->destroyAll( $model_name, $criteria );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ???????????????
- */
- public function destroyAll( Charcoal_String $model_name, Charcoal_SQLCriteria $criteria = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- // ????????
- // $pk = $model->getPrimaryKey();
- // SQL??????SQL???
- $sql = $this->_builder->buildDeleteSQL( $model, $criteria );
- // ?????
- $params = $criteria ? $criteria->getParams() : NULL;
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL???
- $this->execute( s($sql), v($params) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * Check SQL Builder Instance
- */
- private function _checkSQLBuilder()
- {
- if ( !$this->_builder ){
- _throw( new Charcoal_DBException( 'No SQL Builder Specified' ) );
- }
- }
- /*
- * ?????????????
- */
- public function count( Charcoal_String $model_name, Charcoal_SQLCriteria $criteria = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model_name_list = $model_name->split( s(',') );
- if ( $model_name_list->size() < 1 ){
- _throw( new Charcoal_DBException( "current model is not specified(model name empty?)" ) );
- }
- $current_model = Charcoal_TableModelCache::get( s($model_name_list[0]) );
- // SQL????????
- $this->_checkSQLBuilder();
- // SQL
- $sql = $this->_builder->buildCountSQL( $current_model, $criteria );
- // ?????
- $params = $criteria ? $criteria->getParams() : NULL;
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $result = $this->_source->prepareExecute( s($sql), v($params) );
- // ????
- $rows = $this->_source->fetchArray( $result );
- // ????
- $cnt = $rows[0] ? intval($rows[0]) : 0;
- log_debug( "debug, smart_gateway", "count:$cnt" );
- return i($cnt);
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ??????????????
- */
- public function max( Charcoal_String $model_name, Charcoal_String $field, Charcoal_String $where_clause = NULL, Charcoal_Vector $params = NULL )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- if ( $where_clause ){
- $criteria = new Charcoal_SQLCriteria();
- $criteria->setWhere( $where_clause );
- $criteria->setParams( $params );
- }
- else{
- $criteria = NULL;
- }
- // SQL
- $sql = $this->_builder->buildMaxSQL( $model, $field, $criteria );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- log_debug( "debug, smart_gateway", "params:" . print_r($params,true) );
- // SQL??
- $result = $this->_source->prepareExecute( $sql, $params );
- // ????
- $rows = $this->_source->fetchArray( $result );
- return $rows[0];
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * DB???
- */
- public function createDatabase( Charcoal_String $db_name, Charcoal_String $charset )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // SQL??????SQL???
- $sql = $this->_builder->buildCreateDatabaseSQL( $db_name, $charset );
- log_debug( "debug, smart_gateway", "sql:$sql" );
- // SQL??
- $this->_source->execute( s($sql) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- /*
- * ???????
- */
- public function createTable( Charcoal_String $model_name )
- {
- list( $file, $line ) = Charcoal_System::caller(0);
- log_debug( "debug, smart_gateway", "SmartGateway#" . __METHOD__ . "() called from: $file($line)" );
- // ?????????
- $this->initDataSource();
- // SQL??????
- $this->initSQLBuilder();
- try{
- // ?????????
- $model = Charcoal_TableModelCache::get( $model_name );
- // SQL??????SQL???
- $sql = $this->_builder->buildCreateTableSQL( $model );
- // SQL??
- $this->_source->execute( s($sql) );
- }
- catch ( Exception $e )
- {
- _catch( $e );
- _throw( new Charcoal_DBException( s(__METHOD__." Failed."), $e ) );
- }
- }
- }
- return __FILE__;