PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/phalcon/datamapper/query/update.zep.c

http://github.com/phalcon/cphalcon
C | 541 lines | 403 code | 64 blank | 74 comment | 31 complexity | 1f67ca7d0b3b29dcceca8cddaf83bbf4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifdef HAVE_CONFIG_H
  2. #include "../../../ext_config.h"
  3. #endif
  4. #include <php.h>
  5. #include "../../../php_ext.h"
  6. #include "../../../ext.h"
  7. #include <Zend/zend_operators.h>
  8. #include <Zend/zend_exceptions.h>
  9. #include <Zend/zend_interfaces.h>
  10. #include "kernel/main.h"
  11. #include "kernel/fcall.h"
  12. #include "kernel/object.h"
  13. #include "kernel/memory.h"
  14. #include "kernel/concat.h"
  15. #include "kernel/operators.h"
  16. #include "kernel/array.h"
  17. /**
  18. * This file is part of the Phalcon Framework.
  19. *
  20. * (c) Phalcon Team <team@phalcon.io>
  21. *
  22. * For the full copyright and license information, please view the LICENSE.txt
  23. * file that was distributed with this source code.
  24. *
  25. * Implementation of this file has been influenced by AtlasPHP
  26. *
  27. * @link https://github.com/atlasphp/Atlas.Query
  28. * @license https://github.com/atlasphp/Atlas.Qyert/blob/1.x/LICENSE.md
  29. */
  30. /**
  31. * Class Update
  32. */
  33. ZEPHIR_INIT_CLASS(Phalcon_DataMapper_Query_Update)
  34. {
  35. ZEPHIR_REGISTER_CLASS_EX(Phalcon\\DataMapper\\Query, Update, phalcon, datamapper_query_update, phalcon_datamapper_query_abstractconditions_ce, phalcon_datamapper_query_update_method_entry, 0);
  36. return SUCCESS;
  37. }
  38. /**
  39. * Update constructor.
  40. *
  41. * @param Connection $connection
  42. * @param Bind $bind
  43. */
  44. PHP_METHOD(Phalcon_DataMapper_Query_Update, __construct)
  45. {
  46. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  47. zend_long ZEPHIR_LAST_CALL_STATUS;
  48. zephir_fcall_cache_entry *_0 = NULL;
  49. zval *connection, connection_sub, *bind, bind_sub, _1, _2, _3, _4;
  50. zval *this_ptr = getThis();
  51. ZVAL_UNDEF(&connection_sub);
  52. ZVAL_UNDEF(&bind_sub);
  53. ZVAL_UNDEF(&_1);
  54. ZVAL_UNDEF(&_2);
  55. ZVAL_UNDEF(&_3);
  56. ZVAL_UNDEF(&_4);
  57. #if PHP_VERSION_ID >= 80000
  58. bool is_null_true = 1;
  59. ZEND_PARSE_PARAMETERS_START(2, 2)
  60. Z_PARAM_OBJECT_OF_CLASS(connection, phalcon_datamapper_pdo_connection_ce)
  61. Z_PARAM_OBJECT_OF_CLASS(bind, phalcon_datamapper_query_bind_ce)
  62. ZEND_PARSE_PARAMETERS_END();
  63. #endif
  64. ZEPHIR_MM_GROW();
  65. zephir_fetch_params(1, 2, 0, &connection, &bind);
  66. ZEPHIR_CALL_PARENT(NULL, phalcon_datamapper_query_update_ce, getThis(), "__construct", &_0, 0, connection, bind);
  67. zephir_check_call_status();
  68. ZEPHIR_INIT_VAR(&_1);
  69. ZVAL_STRING(&_1, "FROM");
  70. ZEPHIR_INIT_VAR(&_2);
  71. ZVAL_STRING(&_2, "");
  72. zephir_update_property_array(this_ptr, SL("store"), &_1, &_2);
  73. ZEPHIR_INIT_VAR(&_3);
  74. array_init(&_3);
  75. ZEPHIR_INIT_VAR(&_4);
  76. ZVAL_STRING(&_4, "RETURNING");
  77. zephir_update_property_array(this_ptr, SL("store"), &_4, &_3);
  78. ZEPHIR_MM_RESTORE();
  79. }
  80. /**
  81. * Sets a column for the `UPDATE` query
  82. *
  83. * @param string $column
  84. *
  85. * @return Update
  86. */
  87. PHP_METHOD(Phalcon_DataMapper_Query_Update, column)
  88. {
  89. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  90. zend_long type, ZEPHIR_LAST_CALL_STATUS;
  91. zval *column_param = NULL, *value = NULL, value_sub, *type_param = NULL, __$null, _1$$3, _2$$3;
  92. zval column, _0;
  93. zval *this_ptr = getThis();
  94. ZVAL_UNDEF(&column);
  95. ZVAL_UNDEF(&_0);
  96. ZVAL_UNDEF(&value_sub);
  97. ZVAL_NULL(&__$null);
  98. ZVAL_UNDEF(&_1$$3);
  99. ZVAL_UNDEF(&_2$$3);
  100. #if PHP_VERSION_ID >= 80000
  101. bool is_null_true = 1;
  102. ZEND_PARSE_PARAMETERS_START(1, 3)
  103. Z_PARAM_STR(column)
  104. Z_PARAM_OPTIONAL
  105. Z_PARAM_ZVAL_OR_NULL(value)
  106. Z_PARAM_LONG(type)
  107. ZEND_PARSE_PARAMETERS_END();
  108. #endif
  109. ZEPHIR_MM_GROW();
  110. zephir_fetch_params(1, 1, 2, &column_param, &value, &type_param);
  111. zephir_get_strval(&column, column_param);
  112. if (!value) {
  113. value = &value_sub;
  114. value = &__$null;
  115. }
  116. if (!type_param) {
  117. type = -1;
  118. } else {
  119. type = zephir_get_intval(type_param);
  120. }
  121. ZEPHIR_INIT_VAR(&_0);
  122. ZEPHIR_CONCAT_SV(&_0, ":", &column);
  123. zephir_update_property_array_multi(this_ptr, SL("store"), &_0, SL("sz"), 3, SL("COLUMNS"), &column);
  124. if (Z_TYPE_P(value) != IS_NULL) {
  125. zephir_read_property(&_1$$3, this_ptr, ZEND_STRL("bind"), PH_NOISY_CC | PH_READONLY);
  126. ZVAL_LONG(&_2$$3, type);
  127. ZEPHIR_CALL_METHOD(NULL, &_1$$3, "setvalue", NULL, 0, &column, value, &_2$$3);
  128. zephir_check_call_status();
  129. }
  130. RETURN_THIS();
  131. }
  132. /**
  133. * Mass sets columns and values for the `UPDATE`
  134. *
  135. * @param array $columns
  136. *
  137. * @return Update
  138. */
  139. PHP_METHOD(Phalcon_DataMapper_Query_Update, columns)
  140. {
  141. zend_string *_3;
  142. zend_ulong _2;
  143. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  144. zephir_fcall_cache_entry *_4 = NULL;
  145. zend_long ZEPHIR_LAST_CALL_STATUS;
  146. zval *columns_param = NULL, column, value, *_0, _1;
  147. zval columns;
  148. zval *this_ptr = getThis();
  149. ZVAL_UNDEF(&columns);
  150. ZVAL_UNDEF(&column);
  151. ZVAL_UNDEF(&value);
  152. ZVAL_UNDEF(&_1);
  153. #if PHP_VERSION_ID >= 80000
  154. bool is_null_true = 1;
  155. ZEND_PARSE_PARAMETERS_START(1, 1)
  156. Z_PARAM_ARRAY(columns)
  157. ZEND_PARSE_PARAMETERS_END();
  158. #endif
  159. ZEPHIR_MM_GROW();
  160. zephir_fetch_params(1, 1, 0, &columns_param);
  161. zephir_get_arrval(&columns, columns_param);
  162. zephir_is_iterable(&columns, 0, "phalcon/DataMapper/Query/Update.zep", 76);
  163. if (Z_TYPE_P(&columns) == IS_ARRAY) {
  164. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(&columns), _2, _3, _0)
  165. {
  166. ZEPHIR_INIT_NVAR(&column);
  167. if (_3 != NULL) {
  168. ZVAL_STR_COPY(&column, _3);
  169. } else {
  170. ZVAL_LONG(&column, _2);
  171. }
  172. ZEPHIR_INIT_NVAR(&value);
  173. ZVAL_COPY(&value, _0);
  174. if (Z_TYPE_P(&column) == IS_LONG) {
  175. ZEPHIR_CALL_METHOD(NULL, this_ptr, "column", &_4, 0, &value);
  176. zephir_check_call_status();
  177. } else {
  178. ZEPHIR_CALL_METHOD(NULL, this_ptr, "column", &_4, 0, &column, &value);
  179. zephir_check_call_status();
  180. }
  181. } ZEND_HASH_FOREACH_END();
  182. } else {
  183. ZEPHIR_CALL_METHOD(NULL, &columns, "rewind", NULL, 0);
  184. zephir_check_call_status();
  185. while (1) {
  186. ZEPHIR_CALL_METHOD(&_1, &columns, "valid", NULL, 0);
  187. zephir_check_call_status();
  188. if (!zend_is_true(&_1)) {
  189. break;
  190. }
  191. ZEPHIR_CALL_METHOD(&column, &columns, "key", NULL, 0);
  192. zephir_check_call_status();
  193. ZEPHIR_CALL_METHOD(&value, &columns, "current", NULL, 0);
  194. zephir_check_call_status();
  195. if (Z_TYPE_P(&column) == IS_LONG) {
  196. ZEPHIR_CALL_METHOD(NULL, this_ptr, "column", &_4, 0, &value);
  197. zephir_check_call_status();
  198. } else {
  199. ZEPHIR_CALL_METHOD(NULL, this_ptr, "column", &_4, 0, &column, &value);
  200. zephir_check_call_status();
  201. }
  202. ZEPHIR_CALL_METHOD(NULL, &columns, "next", NULL, 0);
  203. zephir_check_call_status();
  204. }
  205. }
  206. ZEPHIR_INIT_NVAR(&value);
  207. ZEPHIR_INIT_NVAR(&column);
  208. RETURN_THIS();
  209. }
  210. /**
  211. * Adds table(s) in the query
  212. *
  213. * @param string $table
  214. *
  215. * @return Update
  216. */
  217. PHP_METHOD(Phalcon_DataMapper_Query_Update, from)
  218. {
  219. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  220. zval *table_param = NULL, _0;
  221. zval table;
  222. zval *this_ptr = getThis();
  223. ZVAL_UNDEF(&table);
  224. ZVAL_UNDEF(&_0);
  225. #if PHP_VERSION_ID >= 80000
  226. bool is_null_true = 1;
  227. ZEND_PARSE_PARAMETERS_START(1, 1)
  228. Z_PARAM_STR(table)
  229. ZEND_PARSE_PARAMETERS_END();
  230. #endif
  231. ZEPHIR_MM_GROW();
  232. zephir_fetch_params(1, 1, 0, &table_param);
  233. zephir_get_strval(&table, table_param);
  234. ZEPHIR_INIT_VAR(&_0);
  235. ZVAL_STRING(&_0, "FROM");
  236. zephir_update_property_array(this_ptr, SL("store"), &_0, &table);
  237. RETURN_THIS();
  238. }
  239. /**
  240. * @return string
  241. */
  242. PHP_METHOD(Phalcon_DataMapper_Query_Update, getStatement)
  243. {
  244. zval _0, _1, _2, _3, _4, _5, _6;
  245. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  246. zend_long ZEPHIR_LAST_CALL_STATUS;
  247. zval *this_ptr = getThis();
  248. ZVAL_UNDEF(&_0);
  249. ZVAL_UNDEF(&_1);
  250. ZVAL_UNDEF(&_2);
  251. ZVAL_UNDEF(&_3);
  252. ZVAL_UNDEF(&_4);
  253. ZVAL_UNDEF(&_5);
  254. ZVAL_UNDEF(&_6);
  255. ZEPHIR_MM_GROW();
  256. ZEPHIR_CALL_METHOD(&_0, this_ptr, "buildflags", NULL, 0);
  257. zephir_check_call_status();
  258. zephir_read_property(&_1, this_ptr, ZEND_STRL("store"), PH_NOISY_CC | PH_READONLY);
  259. zephir_array_fetch_string(&_2, &_1, SL("FROM"), PH_NOISY | PH_READONLY, "phalcon/DataMapper/Query/Update.zep", 101);
  260. ZEPHIR_CALL_METHOD(&_3, this_ptr, "buildcolumns", NULL, 211);
  261. zephir_check_call_status();
  262. ZEPHIR_INIT_VAR(&_5);
  263. ZVAL_STRING(&_5, "WHERE");
  264. ZEPHIR_CALL_METHOD(&_4, this_ptr, "buildcondition", NULL, 0, &_5);
  265. zephir_check_call_status();
  266. ZEPHIR_CALL_METHOD(&_6, this_ptr, "buildreturning", NULL, 0);
  267. zephir_check_call_status();
  268. ZEPHIR_CONCAT_SVSVVVV(return_value, "UPDATE", &_0, " ", &_2, &_3, &_4, &_6);
  269. RETURN_MM();
  270. }
  271. /**
  272. * Whether the query has columns or not
  273. *
  274. * @return bool
  275. */
  276. PHP_METHOD(Phalcon_DataMapper_Query_Update, hasColumns)
  277. {
  278. zval _0, _1;
  279. zval *this_ptr = getThis();
  280. ZVAL_UNDEF(&_0);
  281. ZVAL_UNDEF(&_1);
  282. zephir_read_property(&_0, this_ptr, ZEND_STRL("store"), PH_NOISY_CC | PH_READONLY);
  283. zephir_array_fetch_string(&_1, &_0, SL("COLUMNS"), PH_NOISY | PH_READONLY, "phalcon/DataMapper/Query/Update.zep", 113);
  284. RETURN_BOOL(zephir_fast_count_int(&_1) > 0);
  285. }
  286. /**
  287. * Adds the `RETURNING` clause
  288. *
  289. * @param array $columns
  290. *
  291. * @return Update
  292. */
  293. PHP_METHOD(Phalcon_DataMapper_Query_Update, returning)
  294. {
  295. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  296. zval *columns_param = NULL, _0, _1, _2, _3;
  297. zval columns;
  298. zval *this_ptr = getThis();
  299. ZVAL_UNDEF(&columns);
  300. ZVAL_UNDEF(&_0);
  301. ZVAL_UNDEF(&_1);
  302. ZVAL_UNDEF(&_2);
  303. ZVAL_UNDEF(&_3);
  304. #if PHP_VERSION_ID >= 80000
  305. bool is_null_true = 1;
  306. ZEND_PARSE_PARAMETERS_START(1, 1)
  307. Z_PARAM_ARRAY(columns)
  308. ZEND_PARSE_PARAMETERS_END();
  309. #endif
  310. ZEPHIR_MM_GROW();
  311. zephir_fetch_params(1, 1, 0, &columns_param);
  312. zephir_get_arrval(&columns, columns_param);
  313. ZEPHIR_INIT_VAR(&_0);
  314. zephir_read_property(&_1, this_ptr, ZEND_STRL("store"), PH_NOISY_CC | PH_READONLY);
  315. zephir_array_fetch_string(&_2, &_1, SL("RETURNING"), PH_NOISY | PH_READONLY, "phalcon/DataMapper/Query/Update.zep", 126);
  316. zephir_fast_array_merge(&_0, &_2, &columns);
  317. ZEPHIR_INIT_VAR(&_3);
  318. ZVAL_STRING(&_3, "RETURNING");
  319. zephir_update_property_array(this_ptr, SL("store"), &_3, &_0);
  320. RETURN_THIS();
  321. }
  322. /**
  323. * Resets the internal store
  324. */
  325. PHP_METHOD(Phalcon_DataMapper_Query_Update, reset)
  326. {
  327. zval _1, _2, _3, _4;
  328. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  329. zend_long ZEPHIR_LAST_CALL_STATUS;
  330. zephir_fcall_cache_entry *_0 = NULL;
  331. zval *this_ptr = getThis();
  332. ZVAL_UNDEF(&_1);
  333. ZVAL_UNDEF(&_2);
  334. ZVAL_UNDEF(&_3);
  335. ZVAL_UNDEF(&_4);
  336. ZEPHIR_MM_GROW();
  337. ZEPHIR_CALL_PARENT(NULL, phalcon_datamapper_query_update_ce, getThis(), "reset", &_0, 0);
  338. zephir_check_call_status();
  339. ZEPHIR_INIT_VAR(&_1);
  340. ZVAL_STRING(&_1, "FROM");
  341. ZEPHIR_INIT_VAR(&_2);
  342. ZVAL_STRING(&_2, "");
  343. zephir_update_property_array(this_ptr, SL("store"), &_1, &_2);
  344. ZEPHIR_INIT_VAR(&_3);
  345. array_init(&_3);
  346. ZEPHIR_INIT_VAR(&_4);
  347. ZVAL_STRING(&_4, "RETURNING");
  348. zephir_update_property_array(this_ptr, SL("store"), &_4, &_3);
  349. ZEPHIR_MM_RESTORE();
  350. }
  351. /**
  352. * Sets a column = value condition
  353. *
  354. * @param string $column
  355. * @param mixed|null $value
  356. *
  357. * @return Update
  358. */
  359. PHP_METHOD(Phalcon_DataMapper_Query_Update, set)
  360. {
  361. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  362. zend_long ZEPHIR_LAST_CALL_STATUS;
  363. zval *column_param = NULL, *value = NULL, value_sub, __$null, _0;
  364. zval column;
  365. zval *this_ptr = getThis();
  366. ZVAL_UNDEF(&column);
  367. ZVAL_UNDEF(&value_sub);
  368. ZVAL_NULL(&__$null);
  369. ZVAL_UNDEF(&_0);
  370. #if PHP_VERSION_ID >= 80000
  371. bool is_null_true = 1;
  372. ZEND_PARSE_PARAMETERS_START(1, 2)
  373. Z_PARAM_STR(column)
  374. Z_PARAM_OPTIONAL
  375. Z_PARAM_ZVAL_OR_NULL(value)
  376. ZEND_PARSE_PARAMETERS_END();
  377. #endif
  378. ZEPHIR_MM_GROW();
  379. zephir_fetch_params(1, 1, 1, &column_param, &value);
  380. zephir_get_strval(&column, column_param);
  381. if (!value) {
  382. value = &value_sub;
  383. ZEPHIR_CPY_WRT(value, &__$null);
  384. } else {
  385. ZEPHIR_SEPARATE_PARAM(value);
  386. }
  387. if (Z_TYPE_P(value) == IS_NULL) {
  388. ZEPHIR_INIT_NVAR(value);
  389. ZVAL_STRING(value, "NULL");
  390. }
  391. zephir_update_property_array_multi(this_ptr, SL("store"), value, SL("sz"), 3, SL("COLUMNS"), &column);
  392. zephir_read_property(&_0, this_ptr, ZEND_STRL("bind"), PH_NOISY_CC | PH_READONLY);
  393. ZEPHIR_CALL_METHOD(NULL, &_0, "remove", NULL, 0, &column);
  394. zephir_check_call_status();
  395. RETURN_THIS();
  396. }
  397. /**
  398. * Builds the column list
  399. *
  400. * @return string
  401. */
  402. PHP_METHOD(Phalcon_DataMapper_Query_Update, buildColumns)
  403. {
  404. zend_string *_5;
  405. zend_ulong _4;
  406. zval assignments;
  407. zval column, value, _0, _1, *_2, _3, _11, _12, _6$$3, _8$$3, _9$$4, _10$$4;
  408. zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
  409. zephir_fcall_cache_entry *_7 = NULL;
  410. zend_long ZEPHIR_LAST_CALL_STATUS;
  411. zval *this_ptr = getThis();
  412. ZVAL_UNDEF(&column);
  413. ZVAL_UNDEF(&value);
  414. ZVAL_UNDEF(&_0);
  415. ZVAL_UNDEF(&_1);
  416. ZVAL_UNDEF(&_3);
  417. ZVAL_UNDEF(&_11);
  418. ZVAL_UNDEF(&_12);
  419. ZVAL_UNDEF(&_6$$3);
  420. ZVAL_UNDEF(&_8$$3);
  421. ZVAL_UNDEF(&_9$$4);
  422. ZVAL_UNDEF(&_10$$4);
  423. ZVAL_UNDEF(&assignments);
  424. ZEPHIR_MM_GROW();
  425. ZEPHIR_INIT_VAR(&assignments);
  426. array_init(&assignments);
  427. zephir_read_property(&_0, this_ptr, ZEND_STRL("store"), PH_NOISY_CC | PH_READONLY);
  428. zephir_array_fetch_string(&_1, &_0, SL("COLUMNS"), PH_NOISY | PH_READONLY, "phalcon/DataMapper/Query/Update.zep", 175);
  429. zephir_is_iterable(&_1, 0, "phalcon/DataMapper/Query/Update.zep", 179);
  430. if (Z_TYPE_P(&_1) == IS_ARRAY) {
  431. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(&_1), _4, _5, _2)
  432. {
  433. ZEPHIR_INIT_NVAR(&column);
  434. if (_5 != NULL) {
  435. ZVAL_STR_COPY(&column, _5);
  436. } else {
  437. ZVAL_LONG(&column, _4);
  438. }
  439. ZEPHIR_INIT_NVAR(&value);
  440. ZVAL_COPY(&value, _2);
  441. ZEPHIR_CALL_METHOD(&_6$$3, this_ptr, "quoteidentifier", &_7, 0, &column);
  442. zephir_check_call_status();
  443. ZEPHIR_INIT_NVAR(&_8$$3);
  444. ZEPHIR_CONCAT_VSV(&_8$$3, &_6$$3, " = ", &value);
  445. zephir_array_append(&assignments, &_8$$3, PH_SEPARATE, "phalcon/DataMapper/Query/Update.zep", 176);
  446. } ZEND_HASH_FOREACH_END();
  447. } else {
  448. ZEPHIR_CALL_METHOD(NULL, &_1, "rewind", NULL, 0);
  449. zephir_check_call_status();
  450. while (1) {
  451. ZEPHIR_CALL_METHOD(&_3, &_1, "valid", NULL, 0);
  452. zephir_check_call_status();
  453. if (!zend_is_true(&_3)) {
  454. break;
  455. }
  456. ZEPHIR_CALL_METHOD(&column, &_1, "key", NULL, 0);
  457. zephir_check_call_status();
  458. ZEPHIR_CALL_METHOD(&value, &_1, "current", NULL, 0);
  459. zephir_check_call_status();
  460. ZEPHIR_CALL_METHOD(&_9$$4, this_ptr, "quoteidentifier", &_7, 0, &column);
  461. zephir_check_call_status();
  462. ZEPHIR_INIT_NVAR(&_10$$4);
  463. ZEPHIR_CONCAT_VSV(&_10$$4, &_9$$4, " = ", &value);
  464. zephir_array_append(&assignments, &_10$$4, PH_SEPARATE, "phalcon/DataMapper/Query/Update.zep", 176);
  465. ZEPHIR_CALL_METHOD(NULL, &_1, "next", NULL, 0);
  466. zephir_check_call_status();
  467. }
  468. }
  469. ZEPHIR_INIT_NVAR(&value);
  470. ZEPHIR_INIT_NVAR(&column);
  471. ZEPHIR_INIT_VAR(&_12);
  472. ZVAL_STRING(&_12, ",");
  473. ZEPHIR_CALL_METHOD(&_11, this_ptr, "indent", NULL, 0, &assignments, &_12);
  474. zephir_check_call_status();
  475. ZEPHIR_CONCAT_SV(return_value, " SET", &_11);
  476. RETURN_MM();
  477. }