PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/interbase/php_ibase_udf.c

https://github.com/gmphp/PHP
C | 409 lines | 231 code | 71 blank | 107 comment | 29 complexity | 78633b30dc2ec825c75030aa8c49bead MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Ard Biesheuvel <a.k.biesheuvel@ewi.tudelft.nl> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: php_ibase_udf.c 306939 2011-01-01 02:19:59Z felipe $ */
  19. /**
  20. * This UDF library adds the ability to call PHP functions from SQL
  21. * statements. Because of SQL's strong typing, you will have to declare
  22. * an external function for every combination { output type, #args } that
  23. * your application requires.
  24. *
  25. * Declare the functions like this:
  26. *
  27. * DECLARE EXTERNAL FUNCTION CALL_PHP1
  28. * CSTRING(xx),
  29. * <return type> BY DESCRIPTOR,
  30. * INTEGER BY DESCRIPTOR
  31. * RETURNS PARAMETER 2
  32. * ENTRY_POINT 'udf_call_php1' MODULE_NAME 'php_ibase_udf'
  33. *
  34. * DECLARE EXTERNAL FUNCTION CALL_PHP2
  35. * CSTRING(xx),
  36. * <return type> BY DESCRIPTOR,
  37. * INTEGER BY DESCRIPTOR,
  38. * INTEGER BY DESCRIPTOR
  39. * RETURNS PARAMETER 2
  40. * ENTRY_POINT 'udf_call_php2' MODULE_NAME 'php_ibase_udf'
  41. *
  42. * ... and so on. [for up to 8 input arguments]
  43. *
  44. * The first input parameter contains the name of the PHP function you want
  45. * to call. The second argument is the result. (omit this argument when calling
  46. * the function) The return type of the function is the declared type of the
  47. * result. The value returned from the PHP function being called will
  48. * automatically be converted if necessary.
  49. * The arguments should have their types declared as well, but you're free
  50. * to pass arguments of other types instead. They will be converted to the
  51. * best matching PHP type before being passed to the PHP function.
  52. *
  53. * The declared functions can be called from SQL like:
  54. *
  55. * SELECT * FROM <table> WHERE CALL_PHP1('soundex',<field>) NOT LIKE ?
  56. * or
  57. * UPDATE <table> SET <field> = CALL_PHP1('ucwords',<field>)
  58. *
  59. * Additionally, there's a function 'exec_php' which allows the contents
  60. * of text BLOB fields to be parsed and executed by PHP. This is most useful
  61. * for declaring functions that can then be called with CALL_PHPx.
  62. *
  63. * DECLARE EXTERNAL FUNCTION EXEC_PHP
  64. * BLOB,
  65. * INTEGER BY DESCRIPTOR,
  66. * SMALLINT
  67. * RETURNS PARAMETER 2
  68. * ENTRY_POINT 'exec_php' MODULE_NAME 'php_ibase_udf'
  69. *
  70. * The function will return 1 if execution succeeded and 0 if an error
  71. * occurred. The result that is returned from the executed PHP code is
  72. * ignored. You can pass a non-zero value as second argument to force
  73. * the embedded PHP engine to re-initialise.
  74. *
  75. * There are several ways to build this library, depending on which way the
  76. * database is accessed. If you're using the classic server on a local
  77. * connection, you should compile the library like this:
  78. *
  79. * gcc -shared `php-config --includes` `php-config --ldflags` \
  80. * `php-config --libs` -o php_ibase_udf.so php_ibase_udf.c
  81. *
  82. * If you connect to the classic server by TCP/IP, you should build the
  83. * PHP embedded static library and link against that.
  84. *
  85. * gcc -shared `php-config --includes` `php-config --ldflags` \
  86. * `php-config --libs` -o php_ibase_udf.so php_ibase_udf.c \
  87. * /usr/lib/libphp5.a
  88. *
  89. * If you use the super server, you should also link against the embedded
  90. * library, but be sure to enable thread safety, as the super server is
  91. * multi-threaded. After building, copy the resulting file to the folder
  92. * where your database expects to find its UDFs.
  93. */
  94. #include "zend.h"
  95. #include "zend_API.h"
  96. #include "php.h"
  97. #include "php_ini.h"
  98. #include "ibase.h"
  99. #define min(a,b) ((a)<(b)?(a):(b))
  100. #ifdef PHP_WIN32
  101. #define LL_LIT(lit) lit ## I64
  102. #else
  103. #define LL_LIT(lit) lit ## ll
  104. #endif
  105. #ifdef ZTS
  106. # include <pthread.h>
  107. static void ***tsrm_ls;
  108. pthread_mutex_t mtx_res = PTHREAD_MUTEX_INITIALIZER;
  109. #define LOCK() do { pthread_mutex_lock(&mtx_res); } while (0)
  110. #define UNLOCK() do { pthread_mutex_unlock(&mtx_res); } while (0)
  111. #else
  112. #define LOCK()
  113. #define UNLOCK()
  114. #endif
  115. #ifdef PHP_EMBED
  116. # include "php_main.h"
  117. # include "sapi/embed/php_embed.h"
  118. static void __attribute__((constructor)) init()
  119. {
  120. php_embed_init(0, NULL PTSRMLS_CC);
  121. }
  122. static void __attribute__((destructor)) fini()
  123. {
  124. php_embed_shutdown(TSRMLS_C);
  125. }
  126. #endif
  127. /**
  128. * Gets the contents of the BLOB b and offers it to Zend for parsing/execution
  129. */
  130. void exec_php(BLOBCALLBACK b, PARAMDSC *res, ISC_SHORT *init)
  131. {
  132. int result, remaining = b->blob_total_length, i = 0;
  133. char *code = pemalloc(remaining+1, 1);
  134. ISC_USHORT read;
  135. for (code[remaining] = '\0'; remaining > 0; remaining -= read)
  136. b->blob_get_segment(b->blob_handle, &code[i++<<16],min(0x10000,remaining), &read);
  137. LOCK();
  138. switch (init && *init) {
  139. default:
  140. #ifdef PHP_EMBED
  141. php_request_shutdown(NULL);
  142. if (FAILURE == (result = php_request_startup(TSRMLS_C))) {
  143. break;
  144. }
  145. case 0:
  146. #endif
  147. /* feed it to the parser */
  148. zend_first_try {
  149. result = zend_eval_stringl(code, b->blob_total_length, NULL, "Firebird Embedded PHP engine" TSRMLS_CC);
  150. } zend_end_try();
  151. }
  152. UNLOCK();
  153. free(code);
  154. res->dsc_dtype = dtype_long;
  155. *(ISC_LONG*)res->dsc_address = (result == SUCCESS);
  156. }
  157. static ISC_INT64 const scales[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 100000000, 1000000000,
  158. 1000000000, LL_LIT(10000000000),LL_LIT(100000000000),LL_LIT(10000000000000),LL_LIT(100000000000000),
  159. LL_LIT(1000000000000000),LL_LIT(1000000000000000),LL_LIT(1000000000000000000) };
  160. static void call_php(char *name, PARAMDSC *r, int argc, PARAMDSC **argv)
  161. {
  162. do {
  163. zval callback, args[4], *argp[4], return_value;
  164. PARAMVARY *res = (PARAMVARY*)r->dsc_address;
  165. int i;
  166. INIT_ZVAL(callback);
  167. ZVAL_STRING(&callback,name,0);
  168. LOCK();
  169. /* check if the requested function exists */
  170. if (!zend_is_callable(&callback, 0, NULL TSRMLS_CC)) {
  171. break;
  172. }
  173. UNLOCK();
  174. /* create the argument array */
  175. for (i = 0; i < argc; ++i) {
  176. INIT_ZVAL(args[i]);
  177. argp[i] = &args[i];
  178. /* test arg for null */
  179. if (argv[i]->dsc_flags & DSC_null) {
  180. ZVAL_NULL(argp[i]);
  181. continue;
  182. }
  183. switch (argv[i]->dsc_dtype) {
  184. ISC_INT64 l;
  185. struct tm t;
  186. char const *fmt;
  187. char d[64];
  188. case dtype_cstring:
  189. ZVAL_STRING(argp[i], (char*)argv[i]->dsc_address,0);
  190. break;
  191. case dtype_text:
  192. ZVAL_STRINGL(argp[i], (char*)argv[i]->dsc_address, argv[i]->dsc_length,0);
  193. break;
  194. case dtype_varying:
  195. ZVAL_STRINGL(argp[i], ((PARAMVARY*)argv[i]->dsc_address)->vary_string,
  196. ((PARAMVARY*)argv[i]->dsc_address)->vary_length,0);
  197. break;
  198. case dtype_short:
  199. if (argv[i]->dsc_scale == 0) {
  200. ZVAL_LONG(argp[i], *(short*)argv[i]->dsc_address);
  201. } else {
  202. ZVAL_DOUBLE(argp[i],
  203. ((double)*(short*)argv[i]->dsc_address)/scales[-argv[i]->dsc_scale]);
  204. }
  205. break;
  206. case dtype_long:
  207. if (argv[i]->dsc_scale == 0) {
  208. ZVAL_LONG(argp[i], *(ISC_LONG*)argv[i]->dsc_address);
  209. } else {
  210. ZVAL_DOUBLE(argp[i],
  211. ((double)*(ISC_LONG*)argv[i]->dsc_address)/scales[-argv[i]->dsc_scale]);
  212. }
  213. break;
  214. case dtype_int64:
  215. l = *(ISC_INT64*)argv[i]->dsc_address;
  216. if (argv[i]->dsc_scale == 0 && l <= LONG_MAX && l >= LONG_MIN) {
  217. ZVAL_LONG(argp[i], (long)l);
  218. } else {
  219. ZVAL_DOUBLE(argp[i], ((double)l)/scales[-argv[i]->dsc_scale]);
  220. }
  221. break;
  222. case dtype_real:
  223. ZVAL_DOUBLE(argp[i], *(float*)argv[i]->dsc_address);
  224. break;
  225. case dtype_double:
  226. ZVAL_DOUBLE(argp[i], *(double*)argv[i]->dsc_address);
  227. break;
  228. case dtype_sql_date:
  229. isc_decode_sql_date((ISC_DATE*)argv[i]->dsc_address, &t);
  230. ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.dateformat"), &t),1);
  231. break;
  232. case dtype_sql_time:
  233. isc_decode_sql_time((ISC_TIME*)argv[i]->dsc_address, &t);
  234. ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.timeformat"), &t),1);
  235. break;
  236. case dtype_timestamp:
  237. isc_decode_timestamp((ISC_TIMESTAMP*)argv[i]->dsc_address, &t);
  238. ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.timestampformat"), &t),1);
  239. break;
  240. }
  241. }
  242. LOCK();
  243. /* now call the function */
  244. if (FAILURE == call_user_function(EG(function_table), NULL,
  245. &callback, &return_value, argc, argp TSRMLS_CC)) {
  246. UNLOCK();
  247. break;
  248. }
  249. UNLOCK();
  250. for (i = 0; i < argc; ++i) {
  251. switch (argv[i]->dsc_dtype) {
  252. case dtype_sql_date:
  253. case dtype_sql_time:
  254. case dtype_timestamp:
  255. zval_dtor(argp[i]);
  256. }
  257. }
  258. /* return whatever type we got back from the callback: let DB handle conversion */
  259. switch (Z_TYPE(return_value)) {
  260. case IS_LONG:
  261. r->dsc_dtype = dtype_long;
  262. *(long*)r->dsc_address = Z_LVAL(return_value);
  263. r->dsc_length = sizeof(long);
  264. break;
  265. case IS_DOUBLE:
  266. r->dsc_dtype = dtype_double;
  267. *(double*)r->dsc_address = Z_DVAL(return_value);
  268. r->dsc_length = sizeof(double);
  269. break;
  270. case IS_NULL:
  271. r->dsc_flags |= DSC_null;
  272. break;
  273. default:
  274. convert_to_string(&return_value);
  275. case IS_STRING:
  276. r->dsc_dtype = dtype_varying;
  277. memcpy(res->vary_string, Z_STRVAL(return_value),
  278. (res->vary_length = min(r->dsc_length-2,Z_STRLEN(return_value))));
  279. r->dsc_length = res->vary_length+2;
  280. break;
  281. }
  282. zval_dtor(&return_value);
  283. return;
  284. } while (0);
  285. /**
  286. * If we end up here, we should report an error back to the DB engine, but
  287. * that's not possible. We can however report it back to PHP.
  288. */
  289. LOCK();
  290. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error calling function '%s' from database", name);
  291. UNLOCK();
  292. }
  293. /* Entry points for the DB engine */
  294. void udf_call_php1(char *name, PARAMDSC *r, PARAMDSC *arg1)
  295. {
  296. PARAMDSC *args[1] = { arg1 };
  297. call_php(name, r, 1, args);
  298. }
  299. void udf_call_php2(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2)
  300. {
  301. PARAMDSC *args[2] = { arg1, arg2 };
  302. call_php(name, r, 2, args);
  303. }
  304. void udf_call_php3(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3)
  305. {
  306. PARAMDSC *args[3] = { arg1, arg2, arg3 };
  307. call_php(name, r, 3, args);
  308. }
  309. void udf_call_php4(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  310. PARAMDSC *arg4)
  311. {
  312. PARAMDSC *args[4] = { arg1, arg2, arg3, arg4 };
  313. call_php(name, r, 4, args);
  314. }
  315. void udf_call_php5(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  316. PARAMDSC *arg4, PARAMDSC *arg5)
  317. {
  318. PARAMDSC *args[5] = { arg1, arg2, arg3, arg4, arg5 };
  319. call_php(name, r, 5, args);
  320. }
  321. void udf_call_php6(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  322. PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6)
  323. {
  324. PARAMDSC *args[6] = { arg1, arg2, arg3, arg4, arg5, arg6 };
  325. call_php(name, r, 6, args);
  326. }
  327. void udf_call_php7(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  328. PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6, PARAMDSC *arg7)
  329. {
  330. PARAMDSC *args[7] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
  331. call_php(name, r, 7, args);
  332. }
  333. void udf_call_php8(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  334. PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6, PARAMDSC *arg7, PARAMDSC *arg8)
  335. {
  336. PARAMDSC *args[8] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 };
  337. call_php(name, r, 8, args);
  338. }