PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/component/charcoal/db/SmartGatewayImpl.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 1641 lines | 780 code | 320 blank | 541 comment | 29 complexity | c4cba2d9017ff654d6f6ec8373cafca7 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * implementation class of SmartGateway
  4. *
  5. * PHP version 5
  6. *
  7. * @package component.charcoal.db
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 stk2k, sazysoft
  10. */
  11. require_once( 'EnumQueryOption.class.php' );
  12. class Charcoal_SmartGatewayImpl
  13. {
  14. const TAG = 'smart_gateway';
  15. private $sandbox;
  16. private $data_source;
  17. private $sql_builder;
  18. private $model_cache;
  19. /**
  20. * Constructor
  21. *
  22. * @param Charcoal_Sandbox $sandbox sandbox object
  23. * @param Charcoal_IDataSource $data_source data source object
  24. * @param Charcoal_ISQLBuilder $sql_builder SQL builder object
  25. */
  26. public function __construct( $sandbox, $data_source, $sql_builder )
  27. {
  28. $this->sandbox = $sandbox;
  29. $this->data_source = $data_source;
  30. $this->sql_builder = $sql_builder;
  31. }
  32. /**
  33. * get data source
  34. *
  35. * @return Charcoal_IDataSource currently selected data source
  36. */
  37. public function getDataSource()
  38. {
  39. return $this->data_source;
  40. }
  41. /**
  42. * select data source
  43. *
  44. * @param Charcoal_IDataSource $data_source data source to select
  45. */
  46. public function setDataSource( $data_source )
  47. {
  48. Charcoal_ParamTrait::validateIsA( 1, 'Charcoal_IDataSource', $data_source );
  49. $this->data_source = $data_source;
  50. }
  51. /**
  52. * get last SQL history
  53. *
  54. * @param bool|Charcoal_Boolean $throw If TRUE, throws Charcoal_StackEmptyException when executed SQL stack is empty.
  55. *
  56. * @return Charcoal_SQLHistory executed SQL
  57. */
  58. public function popSQLHistory( $throw = FALSE )
  59. {
  60. return $this->data_source->popSQLHistory( $throw );
  61. }
  62. /**
  63. * get all SQL histories
  64. *
  65. * @return array array of Charcoal_SQLHistory object
  66. */
  67. public function getAllSQLHistories()
  68. {
  69. return $this->data_source->getAllSQLHistories();
  70. }
  71. /**
  72. * List models
  73. *
  74. * @param Charcoal_Sandbox $sandbox
  75. * @param int $find_path
  76. *
  77. * @return Charcoal_ITableModel[]
  78. */
  79. public function listModels( $sandbox, $find_path )
  80. {
  81. // get root path of framework,project,web_app
  82. $dir_framework = Charcoal_ResourceLocator::getFrameworkPath();
  83. $dir_project = Charcoal_ResourceLocator::getProjectPath();
  84. $dir_application = Charcoal_ResourceLocator::getApplicationPath();
  85. // get module root path of framework,project,web_app
  86. $dir_framework_module = $dir_framework . '/module';
  87. $dir_project_module = $dir_project . '/module';
  88. $dir_application_module = $dir_application . '/module';
  89. $config_target_list = array();
  90. if ( Charcoal_System::isAnyBitSet( $find_path, Charcoal_EnumFindPath::FIND_PATH_FRAMEWORK ) ){
  91. $config_target_list[] = $dir_framework . '/config/table_model';
  92. $config_target_list[] = $dir_framework_module . '/config/table_model';
  93. }
  94. if ( Charcoal_System::isAnyBitSet( $find_path, Charcoal_EnumFindPath::FIND_PATH_PROJECT ) ){
  95. $config_target_list[] = $dir_project . '/config/table_model';
  96. $config_target_list[] = $dir_project_module . '/config/table_model';
  97. }
  98. if ( Charcoal_System::isAnyBitSet( $find_path, Charcoal_EnumFindPath::FIND_PATH_APPLICATION ) ){
  99. $config_target_list[] = $dir_application . '/config/table_model';
  100. $config_target_list[] = $dir_application_module . '/config/table_model';
  101. }
  102. // find model names in target directory
  103. $table_model_names = array();
  104. foreach( $config_target_list as $path ){
  105. $found_models = $sandbox->getRegistry()->listObjects( $path, 'table_model' );
  106. $table_model_names = array_merge( $table_model_names, $found_models );
  107. }
  108. // convert model name into table model instance
  109. $table_models = array();
  110. foreach( $table_model_names as $model_name ){
  111. $model = $this->getModel( $model_name );
  112. if ( $model && $model instanceof Charcoal_ITableModel ){
  113. $table_models[$model_name] = $model;
  114. }
  115. }
  116. return $table_models;
  117. }
  118. /**
  119. * Reset component
  120. */
  121. public function reset()
  122. {
  123. if ( $this->data_source ){
  124. $this->data_source->reset();
  125. }
  126. }
  127. /**
  128. * Close connection and destory components
  129. */
  130. public function terminate()
  131. {
  132. if ( $this->data_source ){
  133. $this->data_source->disconnect();
  134. }
  135. }
  136. /**
  137. * Get last insert id
  138. */
  139. public function getLastInsertId()
  140. {
  141. return $this->data_source->getLastInsertId();
  142. }
  143. /**
  144. * returns count of rows which are affected by previous SQL(DELETE/INSERT/UPDATE)
  145. *
  146. * @return int count of rows
  147. */
  148. public function numRows()
  149. {
  150. return $this->data_source->numRows();
  151. }
  152. /**
  153. * get model
  154. *
  155. * @param string $model_name table model name
  156. *
  157. * @return Charcoal_ITableModel
  158. */
  159. private function getModel( $model_name )
  160. {
  161. // Charcoal_ParamTrait::validateString( 1, $model_name );
  162. $model_name = us($model_name);
  163. if ( isset($this->model_cache[$model_name]) ){
  164. return $this->model_cache[$model_name];
  165. }
  166. /**
  167. * @var Charcoal_ITableModel $model
  168. */
  169. $model = $this->sandbox->createObject( $model_name, 'table_model' );
  170. $model->setModelID( $model_name );
  171. // set in cache
  172. $this->model_cache[$model_name] = $model;
  173. return $model;
  174. }
  175. /**
  176. * select database
  177. *
  178. * @param string $database_key
  179. */
  180. public function selectDatabase( $database_key = null )
  181. {
  182. $this->data_source->selectDatabase( $database_key );
  183. }
  184. /**
  185. * get selected database
  186. *
  187. * @return string $database_key
  188. */
  189. public function getSelectedDatabase()
  190. {
  191. return $this->data_source->getSelectedDatabase();
  192. }
  193. /**
  194. * create recordset factory
  195. *
  196. * @param integer $fetch_mode fetch mode(defined at Charcoal_IRecordset::FETCHMODE_XXX)
  197. * @param array $options fetch mode options
  198. */
  199. public function createRecordsetFactory( $fetch_mode = NULL, $options = NULL )
  200. {
  201. return $this->data_source->createRecordsetFactory( $fetch_mode, $options );
  202. }
  203. /*
  204. * check if the table exists
  205. *
  206. * @param Charcoal_String|string $model_name
  207. *
  208. * @return boolean
  209. */
  210. public function existsTable( $model_name )
  211. {
  212. /** @var Charcoal_IDataSource $ds */
  213. $ds = $this->data_source;
  214. try{
  215. $model = $this->getModel( $model_name );
  216. $sql = $this->sql_builder->buildExistsTableSQL( $ds->getDatabaseName(), $model );
  217. $count = $this->queryValue( null, $sql );
  218. return $count > 0 ? true : false;
  219. }
  220. catch ( Exception $e )
  221. {
  222. _catch( $e );
  223. _throw( new Charcoal_DBException( __METHOD__." Failed.", $e ) );
  224. }
  225. return false;
  226. }
  227. /**
  228. * real implementation of Charcoal_SmartGateway::autoCommit()
  229. *
  230. * @param bool $on If TRUE, transaction will be automatically comitted.
  231. */
  232. public function autoCommit( $on )
  233. {
  234. $this->data_source->autoCommit( $on );
  235. }
  236. /**
  237. * real implementation of Charcoal_SmartGateway::beginTrans()
  238. */
  239. public function beginTrans()
  240. {
  241. $this->data_source->beginTrans();
  242. }
  243. /**
  244. * execute transaction command: COMMIT
  245. */
  246. public function commitTrans()
  247. {
  248. $this->data_source->commitTrans();
  249. }
  250. /**
  251. * execute transaction command: ROLLBACK
  252. */
  253. public function rollbackTrans()
  254. {
  255. $this->data_source->rollbackTrans();
  256. }
  257. /**
  258. * real implementation of Charcoal_SmartGateway::execute()
  259. *
  260. * @param Charcoal_String|string $comment comment text
  261. * @param Charcoal_String|string $sql SQL statement(placeholders can be included)
  262. * @param Charcoal_HashMap|array $params Parameter values for prepared statement
  263. * @param Charcoal_HashMap|array $driver_options Driver options
  264. */
  265. public function execute( $comment, $sql, $params = NULL, $driver_options = NULL )
  266. {
  267. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  268. Charcoal_ParamTrait::validateString( 2, $sql );
  269. Charcoal_ParamTrait::validateHashMap( 3, $params, TRUE );
  270. Charcoal_ParamTrait::validateHashMap( 4, $driver_options, TRUE );
  271. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  272. $this->data_source->prepareExecute( $sql, $params, $driver_options );
  273. }
  274. /**
  275. * real implementation of Charcoal_SmartGateway::query()
  276. *
  277. * @param Charcoal_String|string $comment comment text
  278. * @param Charcoal_String|string $sql SQL statement(placeholders can be included)
  279. * @param Charcoal_HashMap|array $params Parameter values for prepared statement
  280. * @param Charcoal_IRecordsetFactory $recordsetFactory
  281. * @param Charcoal_HashMap|array $driver_options Driver options
  282. *
  283. * @return array|Charcoal_IRecordset
  284. */
  285. public function query( $comment, $sql, $params = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  286. {
  287. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  288. Charcoal_ParamTrait::validateString( 2, $sql );
  289. Charcoal_ParamTrait::validateHashMap( 3, $params, TRUE );
  290. Charcoal_ParamTrait::validateIsA( 4, 'Charcoal_IRecordsetFactory', $recordsetFactory, TRUE );
  291. Charcoal_ParamTrait::validateHashMap( 5, $driver_options, TRUE );
  292. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  293. $result = $this->data_source->prepareExecute( $sql, $params, $driver_options );
  294. if ( $recordsetFactory )
  295. {
  296. return $recordsetFactory->createRecordset( $result );
  297. }
  298. else{
  299. $a = array();
  300. while( $row = $this->data_source->fetchAssoc( $result ) ){
  301. $a[] = $row;
  302. }
  303. $this->data_source->free( $result );
  304. return $a;
  305. }
  306. }
  307. /**
  308. * real implementation of Charcoal_SmartGateway::queryValue()
  309. *
  310. * @param Charcoal_String|string $comment comment text
  311. * @param Charcoal_String|string $sql SQL statement(placeholders can be included)
  312. * @param Charcoal_HashMap|array $params Parameter values for prepared statement
  313. * @param Charcoal_HashMap|array $driver_options Driver options
  314. *
  315. * @return mixed|NULL
  316. */
  317. public function queryValue( $comment, $sql, $params = NULL, $driver_options = NULL )
  318. {
  319. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  320. Charcoal_ParamTrait::validateString( 2, $sql );
  321. Charcoal_ParamTrait::validateHashMap( 3, $params, TRUE );
  322. Charcoal_ParamTrait::validateHashMap( 4, $driver_options, TRUE );
  323. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  324. $result = $this->data_source->prepareExecute( $sql, $params, $driver_options );
  325. while( $row = $this->data_source->fetchAssoc( $result ) ){
  326. $value = array_shift($row);
  327. log_debug( "debug,smart_gateway,sql", "queryValue:$value", self::TAG );
  328. return $value;
  329. }
  330. $this->data_source->free( $result );
  331. log_warning( "debug,smart_gateway,sql", "smart_gateway", "queryValue: no record" );
  332. return NULL;
  333. }
  334. /**
  335. * real implementation of Charcoal_SmartGateway::find()
  336. *
  337. * @param Charcoal_String|string $comment comment text
  338. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  339. * @param int $options
  340. * @param Charcoal_SQLCriteria $criteria
  341. * @param Charcoal_String|string $fields
  342. * @param Charcoal_IRecordsetFactory $recordsetFactory
  343. * @param Charcoal_HashMap|array $driver_options Driver options
  344. *
  345. * @return array|Charcoal_IRecordset
  346. */
  347. public function find( $comment, $query_target, $options, $criteria, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  348. {
  349. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  350. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  351. Charcoal_ParamTrait::validateInteger( 3, $options );
  352. Charcoal_ParamTrait::validateIsA( 4, 'Charcoal_SQLCriteria', $criteria );
  353. Charcoal_ParamTrait::validateString( 5, $fields, TRUE );
  354. Charcoal_ParamTrait::validateIsA( 6, 'Charcoal_IRecordsetFactory', $recordsetFactory, TRUE );
  355. Charcoal_ParamTrait::validateHashMap( 7, $driver_options, TRUE );
  356. $current_model_name = $query_target->getModelName();
  357. $current_model_alias = $query_target->getAlias();
  358. $current_model_joins = $query_target->getJoins();
  359. // get current model
  360. $current_model = $this->getModel( $current_model_name );
  361. $current_table_name = $current_model->getTableName();
  362. // make output fields
  363. if ( $fields !== NULL ){
  364. $fields = explode( ',', $fields );
  365. }
  366. else{
  367. // pickup fields from model
  368. $fields = $current_model->getFieldList();
  369. // add alias or table name as prefix
  370. $out_fields = NULL;
  371. foreach( $fields as $field ){
  372. $out_field = $field;
  373. if ( strlen($current_model_alias) > 0 ){
  374. $out_field = $current_model_alias . '.' . $out_field;
  375. }
  376. elseif ( $current_model_joins && count($current_model_joins) > 0 ){
  377. $out_field = $current_table_name . '.' . $out_field;
  378. }
  379. $out_fields[] = $out_field;
  380. }
  381. // add join fields
  382. if ( $current_model_joins ){
  383. foreach( $current_model_joins as $join ){
  384. $join_model_name = $join->getModelName();
  385. $join_alias = $join->getAlias();
  386. $join_model = $this->getModel( $join_model_name );
  387. $join_fields = $join_model->getFieldList();
  388. foreach( $join_fields as $field ){
  389. $out_field = $field;
  390. if ( strlen($join_alias) > 0 ){
  391. $out_field = $join_alias . '.' . $out_field;
  392. }
  393. else{
  394. $out_field = $join_model->getTableName() . '.' . $out_field;
  395. }
  396. $out_fields[] = $out_field;
  397. }
  398. }
  399. }
  400. // make vector fields
  401. $fields = new Charcoal_Vector( $out_fields );
  402. }
  403. // SQLの作成
  404. $sql = $this->sql_builder->buildSelectSQL( $current_model, $current_model_alias, $options, $criteria, $current_model_joins, $fields );
  405. // log_debug( "debug,smart_gateway,sql", "SQL: $sql", self::TAG );
  406. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  407. // パラメータ
  408. $params = $criteria->getParams();
  409. // log_debug( "debug,smart_gateway,sql", "params: $params", self::TAG );
  410. // 実行
  411. $result = $this->data_source->prepareExecute( $sql, $params, $driver_options );
  412. // log_debug( "debug,smart_gateway,sql", "executed SQL: $sql", self::TAG );
  413. if ( $recordsetFactory )
  414. {
  415. return $recordsetFactory->createRecordset( $result );
  416. }
  417. else{
  418. $rows = array();
  419. while( $row = $this->data_source->fetchAssoc( $result ) )
  420. {
  421. $rows[] = $row;
  422. }
  423. log_debug( "debug,smart_gateway,sql", "rows:" . print_r($rows,true), self::TAG );
  424. $this->data_source->free( $result );
  425. return $rows;
  426. }
  427. }
  428. /**
  429. * real implementation of Charcoal_SmartGateway::findFirst()
  430. *
  431. * @param Charcoal_String|string $comment comment text
  432. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  433. * @param Charcoal_SQLCriteria $criteria
  434. * @param Charcoal_String|string $fields comma-separated field list: like 'A,B,C...'
  435. * @param Charcoal_IRecordsetFactory $recordsetFactory
  436. * @param Charcoal_HashMap|array $driver_options Driver options
  437. *
  438. * @return Charcoal_DTO|NULL
  439. */
  440. public function findFirst( $comment, $query_target, $criteria, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  441. {
  442. $criteria->setLimit( 1 );
  443. $result = $this->find( $comment, $query_target, Charcoal_EnumQueryOption::NO_OPTIONS, $criteria, $fields, $recordsetFactory, $driver_options );
  444. return $result ? array_shift($result) : NULL;
  445. }
  446. /**
  447. * real implementation of Charcoal_SmartGateway::findFirstForUpdate()
  448. *
  449. * @param Charcoal_String|string $comment comment text
  450. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  451. * @param Charcoal_SQLCriteria $criteria
  452. * @param Charcoal_String|string $fields comma-separated field list: like 'A,B,C...'
  453. * @param Charcoal_IRecordsetFactory $recordsetFactory
  454. * @param Charcoal_HashMap|array $driver_options Driver options
  455. *
  456. * @return Charcoal_DTO|NULL
  457. */
  458. public function findFirstForUpdate( $comment, $query_target, $criteria, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  459. {
  460. $criteria->setLimit( 1 );
  461. $result = $this->find( $comment, $query_target, Charcoal_EnumQueryOption::FOR_UPDATE, $criteria, $fields, $recordsetFactory, $driver_options );
  462. return $result ? array_shift($result) : NULL;
  463. }
  464. /**
  465. * real implementation of Charcoal_SmartGateway::findAll()
  466. *
  467. * @param Charcoal_String|string $comment comment text
  468. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  469. * @param Charcoal_SQLCriteria $criteria
  470. * @param Charcoal_String|string $fields comma-separated field list: like 'A,B,C...'
  471. * @param Charcoal_IRecordsetFactory $recordsetFactory
  472. * @param Charcoal_HashMap|array $driver_options Driver options
  473. *
  474. * @return array|Charcoal_IRecordset
  475. */
  476. public function findAll( $comment, $query_target, $criteria, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  477. {
  478. return $this->find( $comment, $query_target, Charcoal_EnumQueryOption::NO_OPTIONS, $criteria, $fields, $recordsetFactory, $driver_options );
  479. }
  480. /**
  481. * real implementation of Charcoal_SmartGateway::findAllForUpdate()
  482. *
  483. * @param Charcoal_String|string $comment comment text
  484. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  485. * @param Charcoal_SQLCriteria $criteria
  486. * @param Charcoal_String|string $fields comma-separated field list: like 'A,B,C...'
  487. * @param Charcoal_IRecordsetFactory $recordsetFactory
  488. * @param Charcoal_HashMap|array $driver_options Driver options
  489. *
  490. * @return array|Charcoal_IRecordset
  491. */
  492. public function findAllForUpdate( $comment, $query_target, $criteria, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  493. {
  494. return $this->find( $comment, $query_target, Charcoal_EnumQueryOption::FOR_UPDATE, $criteria, $fields, $recordsetFactory, $driver_options );
  495. }
  496. /**
  497. * real implementation of Charcoal_SmartGateway::findAllDistinct()
  498. *
  499. * @param Charcoal_String|string $comment comment text
  500. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  501. * @param Charcoal_SQLCriteria $criteria
  502. * @param Charcoal_String|string $fields comma-separated field list: like 'A,B,C...'
  503. * @param Charcoal_IRecordsetFactory $recordsetFactory
  504. * @param Charcoal_HashMap|array $driver_options Driver options
  505. *
  506. * @return array|Charcoal_IRecordset
  507. */
  508. public function findAllDistinct( $comment, $query_target, $criteria, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  509. {
  510. return $this->find( $comment, $query_target, Charcoal_EnumQueryOption::DISTINCT, $criteria, $fields, $recordsetFactory, $driver_options );
  511. }
  512. /**
  513. * real implementation of Charcoal_SmartGateway::findAllBy()
  514. *
  515. * @param Charcoal_String|string $comment comment text
  516. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  517. * @param string $field
  518. * @param mixed $value
  519. * @param Charcoal_String|string $fields comma-separated field list: like 'A,B,C...'
  520. * @param Charcoal_IRecordsetFactory $recordsetFactory
  521. * @param Charcoal_HashMap|array $driver_options Driver options
  522. *
  523. * @return array|Charcoal_IRecordset
  524. */
  525. public function findAllBy( $comment, $query_target, $field, $value, $fields = NULL, $recordsetFactory = NULL, $driver_options = NULL )
  526. {
  527. $field = us( $field );
  528. $criteria = new Charcoal_SQLCriteria();
  529. $criteria->setWhere( $field . ' = ?' );
  530. $criteria->setParams( array( $value ) );
  531. return $this->findAll( $comment, $query_target, $criteria, $fields, $recordsetFactory, $driver_options );
  532. }
  533. /**
  534. * real implementation of Charcoal_SmartGateway::findById()
  535. *
  536. * @param Charcoal_String|string $comment comment text
  537. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  538. * @param int|bool|float|string $id primary key value of the entity
  539. *
  540. * @return array|NULL
  541. */
  542. public function findById( $comment, $query_target, $id )
  543. {
  544. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  545. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  546. Charcoal_ParamTrait::validateScalar( 3, $id );
  547. $model = $this->getModel( $query_target->getModelName() );
  548. $pk = $model->getPrimaryKey();
  549. $where_clause = $pk . ' = ?';
  550. $params = array( ui($id) );
  551. $criteria = new Charcoal_SQLCriteria( $where_clause, $params );
  552. $result = $this->findAll( $comment, $query_target, $criteria );
  553. return $result ? array_shift( $result ) : NULL;
  554. }
  555. /**
  556. * real implementation of Charcoal_SmartGateway::deleteById()
  557. *
  558. * @param Charcoal_String|string $comment comment text
  559. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  560. * @param int|bool|float|string $data_id primary key value of the entity
  561. *
  562. * @return int deleted rows
  563. */
  564. public function deleteById( $comment, $query_target, $data_id )
  565. {
  566. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  567. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  568. Charcoal_ParamTrait::validateScalar( 3, $data_id );
  569. $model = $this->getModel( $query_target->getModelName() );
  570. $alias = $query_target->getAlias();
  571. $data_id = us( $data_id );
  572. $pk = us($model->getPrimaryKey());
  573. $where = us($pk) . ' = ?';
  574. $params = array( ui($data_id) );
  575. $criteria = new Charcoal_SQLCriteria( $where, $params );
  576. $sql = $this->sql_builder->buildDeleteSQL( $model, $alias, $criteria );
  577. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  578. /// log_debug( "debug,smart_gateway,sql", "sql:$sql", self::TAG );
  579. // log_debug( "debug,smart_gateway,sql", "params:" . print_r($params,true), self::TAG );
  580. $this->data_source->prepareExecute( $sql, $params );
  581. return $this->data_source->numRows();
  582. }
  583. /**
  584. * real implementation of Charcoal_SmartGateway::deleteByIds()
  585. *
  586. * @param Charcoal_String|string $comment comment text
  587. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  588. * @param array|Charcoal_Vector $data_ids array of primary key values for the entity
  589. *
  590. * @return int deleted rows
  591. */
  592. public function deleteByIds( $comment, $query_target, $data_ids )
  593. {
  594. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  595. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  596. Charcoal_ParamTrait::validateVector( 3, $data_ids );
  597. $model = $this->getModel( $query_target->getModelName() );
  598. $alias = $query_target->getAlias();
  599. $pk = us($model->getPrimaryKey());
  600. $placeholders = array();
  601. $params = array();
  602. foreach( $data_ids as $id ){
  603. $placeholders[] = '?';
  604. $params[] = $id;
  605. }
  606. $where = us($pk) . ' in (' . implode(',',$placeholders) . ')';
  607. $criteria = new Charcoal_SQLCriteria( $where, $params );
  608. $sql = $this->sql_builder->buildDeleteSQL( $model, $alias, $criteria );
  609. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  610. /// log_debug( "debug,smart_gateway,sql", "sql:$sql", self::TAG );
  611. // log_debug( "debug,smart_gateway,sql", "params:" . print_r($params,true), self::TAG );
  612. $this->data_source->prepareExecute( $sql, $params );
  613. return $this->data_source->numRows();
  614. }
  615. /**
  616. * real implementation of Charcoal_SmartGateway::deleteBy()
  617. *
  618. * @param Charcoal_String|string $comment comment text
  619. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  620. * @param Charcoal_String|string $field field name to query
  621. * @param Charcoal_Scalar $value field value to query
  622. *
  623. * @return int deleted rows
  624. */
  625. public function deleteBy( $comment, $query_target, $field, $value )
  626. {
  627. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  628. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  629. Charcoal_ParamTrait::validateString( 3, $field );
  630. Charcoal_ParamTrait::validateScalar( 4, $value );
  631. $value = ($value instanceof Charcoal_Scalar) ? $value->unbox() : $value;
  632. $where = us($field) . ' = ?';
  633. $params = array( $value );
  634. $criteria = new Charcoal_SQLCriteria( $where, $params );
  635. return $this->deleteAll( $comment, $query_target, $criteria );
  636. }
  637. /**
  638. * real implementation of Charcoal_SmartGateway::deleteAll()
  639. *
  640. * @param Charcoal_String|string $comment comment text
  641. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  642. * @param Charcoal_SQLCriteria $criteria criteria object
  643. *
  644. * @return int deleted rows
  645. */
  646. public function deleteAll( $comment, $query_target, $criteria )
  647. {
  648. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  649. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  650. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_SQLCriteria', $criteria );
  651. $model = $this->getModel( $query_target->getModelName() );
  652. $alias = $query_target->getAlias();
  653. $sql = $this->sql_builder->buildDeleteSQL( $model, $alias, $criteria );
  654. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  655. $params = $criteria ? $criteria->getParams() : NULL;
  656. // log_debug( "debug,smart_gateway,sql", "sql:$sql", self::TAG );
  657. // log_debug( "debug,smart_gateway,sql", "params:" . print_r($params,true), self::TAG );
  658. $this->data_source->prepareExecute( $sql, $params );
  659. return $this->data_source->numRows();
  660. }
  661. /**
  662. * real implementation of Charcoal_SmartGateway::execAggregateQuery()
  663. *
  664. * @param Charcoal_String|string $comment comment text
  665. * @param int $aggregate_func identify aggregate function tpype
  666. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  667. * @param Charcoal_SQLCriteria|NULL $criteria criteria object
  668. * @param Charcoal_String|string|NULL $fields comma-separated field list: like 'A,B,C...'
  669. *
  670. * @return mixed
  671. */
  672. private function execAggregateQuery( $comment, $aggregate_func, $query_target, $criteria = NULL, $fields = NULL )
  673. {
  674. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  675. Charcoal_ParamTrait::validateInteger( 2, $aggregate_func );
  676. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_QueryTarget', $query_target );
  677. Charcoal_ParamTrait::validateIsA( 4, 'Charcoal_SQLCriteria', $criteria, TRUE );
  678. Charcoal_ParamTrait::validateString( 5, $fields, TRUE );
  679. // default criteria
  680. if ( $criteria === NULL ){
  681. $criteria = new Charcoal_SQLCriteria();
  682. }
  683. // default count fields
  684. if ( $fields === NULL ){
  685. $fields = '*';
  686. }
  687. $fields = explode( ',', $fields );
  688. $current_model_name = $query_target->getModelName();
  689. $current_model_alias = $query_target->getAlias();
  690. $current_model_joins = $query_target->getJoins();
  691. // get current model
  692. $model = $this->getModel( $current_model_name );
  693. $sql = $this->sql_builder->buildAggregateSQL(
  694. $model,
  695. $current_model_alias,
  696. $aggregate_func,
  697. $criteria,
  698. $current_model_joins,
  699. $fields );
  700. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  701. $params = $criteria->getParams();
  702. // log_debug( "debug,smart_gateway,sql", "sql:$sql", self::TAG );
  703. // log_debug( "debug,smart_gateway,sql", "params:" . print_r($params,true), self::TAG );
  704. // SQL実行
  705. $result = $this->data_source->prepareExecute( $sql, $params );
  706. // フェッチ
  707. $rows = $this->data_source->fetchArray( $result );
  708. // result
  709. $result = $rows[0] ? $rows[0] : 0;
  710. return $result;
  711. }
  712. /**
  713. * real implementation of Charcoal_SmartGateway::count()
  714. *
  715. * @param Charcoal_String|string $comment comment text
  716. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  717. * @param Charcoal_SQLCriteria|NULL $criteria criteria object
  718. * @param Charcoal_String|string|NULL $fields comma-separated field list: like 'A,B,C...'
  719. *
  720. * @return int
  721. */
  722. public function count( $comment, $query_target, $criteria = NULL, $fields = NULL )
  723. {
  724. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  725. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  726. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_SQLCriteria', $criteria, TRUE );
  727. Charcoal_ParamTrait::validateString( 4, $fields, TRUE );
  728. if ( $fields === NULL ){
  729. $fields = '*';
  730. }
  731. $result = $this->execAggregateQuery( $comment, Charcoal_EnumSQLAggregateFunc::FUNC_COUNT, $query_target, $criteria, $fields );
  732. log_debug( "debug,sql,smart_gateway", "COUNT result: $result", self::TAG );
  733. return intval($result);
  734. }
  735. /**
  736. * real implementation of Charcoal_SmartGateway::max()
  737. *
  738. * @param Charcoal_String|string $comment comment text
  739. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  740. * @param Charcoal_SQLCriteria|NULL $criteria criteria object
  741. * @param Charcoal_String|string|NULL $fields comma-separated field list: like 'A,B,C...'
  742. *
  743. * @return mixed
  744. */
  745. public function max( $comment, $query_target, $criteria = NULL, $fields = NULL )
  746. {
  747. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  748. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  749. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_SQLCriteria', $criteria, TRUE );
  750. Charcoal_ParamTrait::validateString( 4, $fields, TRUE );
  751. $result = $this->execAggregateQuery( $comment, Charcoal_EnumSQLAggregateFunc::FUNC_MAX, $query_target, $criteria, $fields );
  752. log_debug( "debug,sql,smart_gateway", "MAX result: $result", self::TAG );
  753. return $result;
  754. }
  755. /**
  756. * real implementation of Charcoal_SmartGateway::min()
  757. *
  758. * @param Charcoal_String|string $comment comment text
  759. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  760. * @param Charcoal_SQLCriteria|NULL $criteria criteria object
  761. * @param Charcoal_String|string|NULL $fields comma-separated field list: like 'A,B,C...'
  762. *
  763. * @return mixed
  764. */
  765. public function min( $comment, $query_target, $criteria = NULL, $fields = NULL )
  766. {
  767. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  768. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  769. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_SQLCriteria', $criteria, TRUE );
  770. Charcoal_ParamTrait::validateString( 4, $fields, TRUE );
  771. $result = $this->execAggregateQuery( $comment, Charcoal_EnumSQLAggregateFunc::FUNC_MIN, $query_target, $criteria, $fields );
  772. log_debug( "debug,sql,smart_gateway", "MIN result: $result", self::TAG );
  773. return $result;
  774. }
  775. /**
  776. * real implementation of Charcoal_SmartGateway::sum()
  777. *
  778. * @param Charcoal_String|string $comment comment text
  779. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  780. * @param Charcoal_SQLCriteria|NULL $criteria criteria object
  781. * @param Charcoal_String|string|NULL $fields comma-separated field list: like 'A,B,C...'
  782. *
  783. * @return mixed
  784. */
  785. public function sum( $comment, $query_target, $criteria = NULL, $fields = NULL )
  786. {
  787. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  788. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  789. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_SQLCriteria', $criteria, TRUE );
  790. Charcoal_ParamTrait::validateString( 4, $fields, TRUE );
  791. $result = $this->execAggregateQuery( $comment, Charcoal_EnumSQLAggregateFunc::FUNC_SUM, $query_target, $criteria, $fields );
  792. log_debug( "debug,sql,smart_gateway", "SUM result: $result", self::TAG );
  793. return $result;
  794. }
  795. /**
  796. * real implementation of Charcoal_SmartGateway::avg()
  797. *
  798. * @param Charcoal_String|string $comment comment text
  799. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  800. * @param Charcoal_SQLCriteria|NULL $criteria criteria object
  801. * @param Charcoal_String|string|NULL $fields comma-separated field list: like 'A,B,C...'
  802. *
  803. * @return mixed
  804. */
  805. public function avg( $comment, $query_target, $criteria = NULL, $fields = NULL )
  806. {
  807. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  808. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  809. Charcoal_ParamTrait::validateIsA( 3, 'Charcoal_SQLCriteria', $criteria, TRUE );
  810. Charcoal_ParamTrait::validateString( 4, $fields, TRUE );
  811. $result = $this->execAggregateQuery( $comment, Charcoal_EnumSQLAggregateFunc::FUNC_AVG, $query_target, $criteria, $fields );
  812. log_debug( "debug,sql,smart_gateway", "AVG result: $result", self::TAG );
  813. return $result;
  814. }
  815. /**
  816. * real implementation of Charcoal_SmartGateway::createTable()
  817. *
  818. * @param Charcoal_String|string $comment comment text
  819. * @param Charcoal_String|string $model_name
  820. * @param boolean|Charcoal_Boolean $if_not_exists If TRUE, output SQL includes "IF NOT EXISTS" wuth "CREATE TABLE"
  821. */
  822. public function createTable( $comment, $model_name, $if_not_exists = false )
  823. {
  824. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  825. Charcoal_ParamTrait::validateString( 2, $model_name );
  826. Charcoal_ParamTrait::validateBoolean( 3, $if_not_exists );
  827. $model = $this->getModel( $model_name );
  828. $sql = $this->sql_builder->buildCreateTableSQL( $model, $if_not_exists );
  829. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  830. $rows_affected = $this->data_source->execute( $sql );
  831. log_debug( "debug,sql,smart_gateway", "createTable result: $rows_affected", self::TAG );
  832. return $rows_affected;
  833. }
  834. /**
  835. * real implementation of Charcoal_SmartGateway::dropTable()
  836. *
  837. * @param Charcoal_String|string $comment comment text
  838. * @param Charcoal_String|string $model_name
  839. * @param boolean|Charcoal_Boolean $if_exists If TRUE, output SQL includes "IF EXISTS" wuth "DROP TABLE"
  840. */
  841. public function dropTable( $comment, $model_name, $if_exists = false )
  842. {
  843. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  844. Charcoal_ParamTrait::validateString( 2, $model_name );
  845. Charcoal_ParamTrait::validateBoolean( 3, $if_exists );
  846. $model = $this->getModel( $model_name );
  847. $sql = $this->sql_builder->buildDropTableSQL( $model, $if_exists );
  848. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  849. $rows_affected = $this->data_source->execute( $sql );
  850. log_debug( "debug,sql,smart_gateway", "dropTable result: $rows_affected", self::TAG );
  851. return $rows_affected;
  852. }
  853. /**
  854. * real implementation of Charcoal_SmartGateway::truncateTable()
  855. *
  856. * @param Charcoal_String|string $comment comment text
  857. * @param Charcoal_String|string $model_name
  858. */
  859. public function truncateTable( $comment, $model_name )
  860. {
  861. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  862. Charcoal_ParamTrait::validateString( 2, $model_name );
  863. $model = $this->getModel( $model_name );
  864. $sql = $this->sql_builder->buildTruncateTableSQL( $model );
  865. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  866. $rows_affected = $this->data_source->execute( $sql );
  867. log_debug( "debug,sql,smart_gateway", "truncateTable result: $rows_affected", self::TAG );
  868. return $rows_affected;
  869. }
  870. /**
  871. * real implementation of Charcoal_SmartGateway::save()
  872. *
  873. * @param Charcoal_String|string $comment comment text
  874. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  875. * @param Charcoal_DTO $data associative DTO object to insert
  876. *
  877. * @return int last inserted id
  878. */
  879. public function save( $comment, $query_target, $data )
  880. {
  881. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  882. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  883. Charcoal_ParamTrait::validateDTO( 3, $data );
  884. $new_id = 0;
  885. try{
  886. $model = $this->getModel( $query_target->getModelName() );
  887. $alias = $query_target->getAlias();
  888. // primary key
  889. $pk = $model->getPrimaryKey();
  890. log_debug( "debug,smart_gateway,sql", "pk:$pk", self::TAG );
  891. // validate primary key value
  892. $valid = $model->validatePrimaryKeyValue( $data );
  893. log_debug( "debug,smart_gateway,sql", "pk valid:$valid", self::TAG );
  894. if ( $valid ){
  895. // find entity
  896. $obj = self::findById( $comment, $query_target, $data->$pk );
  897. log_debug( "debug,smart_gateway,sql", "found entity:" . print_r($obj,true), self::TAG );
  898. // if not found, dto is regarded as a new entity
  899. $is_new = empty($obj);
  900. }
  901. else{
  902. // if promary key value is invalid, dto id rebgarded as a new entity
  903. $is_new = true;
  904. }
  905. log_debug( "debug,smart_gateway,sql", "is_new:$is_new", self::TAG );
  906. // build SQL
  907. if ( !$is_new ){
  908. // UPDATE
  909. $data_id = $data[$pk];
  910. $where = "$pk = ?";
  911. $params = array( ui($data_id) );
  912. $criteria = new Charcoal_SQLCriteria( $where, $params );
  913. list( $sql, $params ) = $this->sql_builder->buildUpdateSQL( $model, $alias, $data, $criteria );
  914. }
  915. else{
  916. // INSERT
  917. list( $sql, $params ) = $this->sql_builder->buildInsertSQL( $model, $alias, $data );
  918. $is_new = TRUE;
  919. }
  920. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  921. $this->data_source->prepareExecute( $sql, $params );
  922. if ( $is_new ){
  923. $sql = $this->sql_builder->buildLastIdSQL();
  924. $result = $this->data_source->prepareExecute( $sql );
  925. $row = $this->data_source->fetchArray( $result );
  926. $new_id = $row[0];
  927. }
  928. else{
  929. $new_id = $data[$pk];
  930. }
  931. log_debug( "debug,smart_gateway,sql", "new_id:$new_id", self::TAG );
  932. }
  933. catch ( Exception $e )
  934. {
  935. _catch( $e );
  936. _throw( new Charcoal_DBException( __METHOD__." Failed.", $e ) );
  937. }
  938. return $new_id;
  939. }
  940. /**
  941. * real implementation of Charcoal_SmartGateway::insert()
  942. *
  943. * @param Charcoal_String|string $comment comment text
  944. * @param string|Charcoal_QueryTarget $query_target description about target model, alias, or joins
  945. * @param Charcoal_DTO $data associative array/HashMap/DTO object to insert
  946. *
  947. * @return int last inserted id
  948. */
  949. public function insert( $comment, $query_target, $data )
  950. {
  951. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  952. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  953. Charcoal_ParamTrait::validateHashMapOrDTO( 3, $data );
  954. $model = $this->getModel( $query_target->getModelName() );
  955. $alias = $query_target->getAlias();
  956. list( $sql, $params ) = $this->sql_builder->buildInsertSQL( $model, $alias, $data );
  957. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  958. $this->data_source->prepareExecute( $sql, $params );
  959. $sql = $this->sql_builder->buildLastIdSQL();
  960. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  961. $result = $this->data_source->prepareExecute( $sql );
  962. $row = $this->data_source->fetchArray( $result );
  963. $new_id = $row[0];
  964. log_debug( "debug,smart_gateway,sql", "new_id:$new_id", self::TAG );
  965. return $new_id;
  966. }
  967. /**
  968. * real implementation of Charcoal_SmartGateway::insertAll()
  969. *
  970. * @param Charcoal_String|string $comment comment text
  971. * @param string|Charcoal_QueryTarget $query_target description about target model, alias, or joins
  972. * @param array|Charcoal_Vector $data_set array of array or DTO value to insert
  973. *
  974. * @return integer count of affected rows
  975. */
  976. public function insertAll( $comment, $query_target, $data_set )
  977. {
  978. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  979. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  980. Charcoal_ParamTrait::validateVector( 3, $data_set );
  981. $model = $this->getModel( $query_target->getModelName() );
  982. $alias = $query_target->getAlias();
  983. list( $sql, $params ) = $this->sql_builder->buildBulkInsertSQL( $model, $alias, $data_set );
  984. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  985. $this->data_source->prepareExecute( $sql, $params );
  986. return $this->data_source->numRows();
  987. }
  988. /**
  989. * real implementation of Charcoal_SmartGateway::updateFields()
  990. *
  991. * @param Charcoal_String|string $comment comment text
  992. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  993. * @param Charcoal_Integer|int $data_id identify database entity
  994. * @param Charcoal_HashMap|array $data associative array or HashMap object to update
  995. */
  996. public function updateFields( $comment, $query_target, $data_id, $data )
  997. {
  998. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  999. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  1000. Charcoal_ParamTrait::validateInteger( 3, $data_id );
  1001. Charcoal_ParamTrait::validateHashMap( 4, $data );
  1002. $model = $this->getModel( $query_target->getModelName() );
  1003. $alias = $query_target->getAlias();
  1004. $dto = $data instanceof Charcoal_DTO ? $data : $model->createDTO( um($data) );
  1005. // log_debug( "debug,smart_gateway,sql", "dto:" . print_r($dto,true), self::TAG );
  1006. $pk = $model->getPrimaryKey();
  1007. $where = "$pk = ?";
  1008. $params = array( ui($data_id) );
  1009. $criteria = new Charcoal_SQLCriteria( $where, $params );
  1010. list( $sql, $params ) = $this->sql_builder->buildUpdateSQL( $model, $alias, $dto, $criteria );
  1011. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  1012. // log_debug( "debug,smart_gateway,sql", "sql:$sql", self::TAG );
  1013. // log_debug( "debug,smart_gateway,sql", "params:" . print_r($params,true), self::TAG );
  1014. // SQL実行
  1015. $this->data_source->prepareExecute( $sql, $params );
  1016. }
  1017. /**
  1018. * real implementation of Charcoal_SmartGateway::updateFieldNow()
  1019. *
  1020. * @param Charcoal_String|string $comment comment text
  1021. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  1022. * @param Charcoal_Integer|int $data_id identify database entity
  1023. * @param Charcoal_String|string $field field name to update
  1024. */
  1025. public function updateFieldNow( $comment, $query_target, $data_id, $field )
  1026. {
  1027. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  1028. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  1029. Charcoal_ParamTrait::validateInteger( 3, $data_id );
  1030. Charcoal_ParamTrait::validateString( 4, $field );
  1031. $model = $this->getModel( $query_target->getModelName() );
  1032. $alias = $query_target->getAlias();
  1033. if ( !$model->fieldExists($field) ){
  1034. _throw( new Charcoal_InvalidArgumentException("field=[$field]") );
  1035. }
  1036. $field = us($field);
  1037. $override[$field]['update'] = new Charcoal_AnnotationValue( 'update', 'function', array('now') );
  1038. $pk = $model->getPrimaryKey();
  1039. $where = "$pk = ?";
  1040. $params = array( ui($data_id) );
  1041. $criteria = new Charcoal_SQLCriteria( $where, $params );
  1042. list( $sql, $params ) = $this->sql_builder->buildUpdateSQL( $model, $alias, $model->createDTO(), $criteria, $override );
  1043. $sql = !empty($comment) ? $this->sql_builder->prependComment( $sql, $comment ) : $sql;
  1044. // log_debug( "debug,smart_gateway,sql", "sql:$sql", self::TAG );
  1045. // log_debug( "debug,smart_gateway,sql", "params:" . print_r($params,true), self::TAG );
  1046. $this->data_source->prepareExecute( $sql, $params );
  1047. }
  1048. /**
  1049. * real implementation of Charcoal_SmartGateway::incrementField()
  1050. *
  1051. * @param Charcoal_String|string $comment comment text
  1052. * @param Charcoal_QueryTarget $query_target description about target model, alias, or joins
  1053. * @param Charcoal_Integer|int $data_id identify database entity
  1054. * @param Charcoal_String|string $field field name to increment
  1055. * @param Charcoal_Integer|int $increment_by amount of increment
  1056. */
  1057. public function incrementField( $comment, $query_target, $data_id, $field, $increment_by = 1 )
  1058. {
  1059. Charcoal_ParamTrait::validateString( 1, $comment , TRUE );
  1060. Charcoal_ParamTrait::validateIsA( 2, 'Charcoal_QueryTarget', $query_target );
  1061. Charcoal_ParamTrait::validateInteger( 3, $data_id );
  1062. Charcoal_ParamTrait::validateString( 4, $field );
  1063. Charcoal_ParamTrait::validateInteger( 5, $increment_by, TRUE );
  1064. $model = $this->getModel( $query_targe

Large files files are truncated, but you can click here to view the full file