]> granicus.if.org Git - php/commitdiff
Added ibase_gen_id()
authorArd Biesheuvel <abies@php.net>
Tue, 23 Sep 2003 20:58:14 +0000 (20:58 +0000)
committerArd Biesheuvel <abies@php.net>
Tue, 23 Sep 2003 20:58:14 +0000 (20:58 +0000)
# Lightweight shortcut to SELECT GEN_ID() ... function

ext/interbase/interbase.c
ext/interbase/php_interbase.h

index a3cf0c2fd8d4ae6ecc300c6fa2868d760e9b9585..de1902318bf88d22f7143425fdd2aced8f39400d 100644 (file)
@@ -116,7 +116,7 @@ function_entry ibase_functions[] = {
 #if HAVE_STRFTIME
        PHP_FE(ibase_timefmt, NULL)
 #endif
-
+    PHP_FE(ibase_gen_id, NULL)
        PHP_FE(ibase_num_fields, NULL)
        PHP_FE(ibase_num_params, NULL)
 #if abies_0
@@ -4403,6 +4403,113 @@ PHP_FUNCTION(ibase_free_event_handler)
 }      
 /* }}} */
   
+/* {{{ proto int ibase_gen_id([ resource link_identifier, ] string generator [, int increment ])
+   Increments the named generator and returns its new value */
+PHP_FUNCTION(ibase_gen_id)
+{
+       char query[128];
+       
+       zval **arg1, **arg2, **arg3, **query_arg;
+       ibase_db_link *ib_link;
+       ibase_trans *trans = NULL;
+       long increment;
+#ifdef SQL_INT64
+       ISC_INT64 result;
+#else
+       ISC_LONG result;
+#endif
+
+       XSQLDA out_sqlda;
+
+       RESET_ERRMSG;
+
+       switch (ZEND_NUM_ARGS()) {
+               case 1:
+                       if (zend_get_parameters_ex(1, &arg1) == FAILURE) {
+                               RETURN_FALSE;
+                       }
+                       ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, NULL, IBG(default_link), "InterBase link", le_link, le_plink);
+                       query_arg = arg1;
+                       increment = 1;
+                       break;
+               case 2:
+                       if (zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) {
+                               RETURN_FALSE;
+                       }
+
+                       if (Z_TYPE_PP(arg1) == IS_STRING) /* first param is generator, second is inc */
+                       {
+                               query_arg = arg1;
+                               convert_to_long_ex(arg2);
+                               increment = Z_LVAL_PP(arg2);
+
+                               ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, NULL, IBG(default_link), "InterBase link", le_link, le_plink);
+                       } else {
+                               _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, arg1, &ib_link, &trans);
+                               query_arg = arg2;
+                               increment = 1;
+                       }
+                       break;
+               case 3:
+                       if (zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE) {
+                               RETURN_FALSE;
+                       }
+                       ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link*, arg1, -1, "InterBase link", le_link, le_plink);
+                       
+                       query_arg = arg2;
+                       convert_to_long_ex(arg3);
+                       increment = Z_LVAL_PP(arg3);
+                       
+                       break;                  
+               default:
+                       WRONG_PARAM_COUNT;
+                       break;
+       }
+       
+       convert_to_string_ex(query_arg);
+       sprintf(query, "SELECT GEN_ID(%s,%ld) FROM rdb$database", Z_STRVAL_PP(query_arg), increment);
+
+       /* open default transaction */
+       if (_php_ibase_def_trans(ib_link, &trans TSRMLS_CC) == FAILURE) {
+               RETURN_FALSE;
+       }
+       
+       /* allocate a minimal descriptor area */
+       out_sqlda.sqln = out_sqlda.sqld = 1;
+       out_sqlda.version = SQLDA_CURRENT_VERSION;
+       
+       /* allocate the field for the result */
+#ifdef SQL_INT64
+       out_sqlda.sqlvar[0].sqltype = SQL_INT64;
+#else
+       out_sqlda.sqlvar[0].sqltype = SQL_LONG;
+#endif
+       out_sqlda.sqlvar[0].sqlscale = 0;
+       out_sqlda.sqlvar[0].sqllen = sizeof(result);
+       out_sqlda.sqlvar[0].sqldata = (void*) &result;
+
+       /* execute the query */
+       if (isc_dsql_exec_immed2(IB_STATUS, &ib_link->handle, &trans->handle, 0, query, SQL_DIALECT_CURRENT, NULL, &out_sqlda))
+       {
+               _php_ibase_error(TSRMLS_C);
+               RETURN_FALSE;
+       }
+
+       /* don't return the generator value as a string unless it doesn't fit in a long */
+#ifdef SQL_INT64
+       if (result > LONG_MAX) 
+#endif
+       {
+               char res[24];
+
+               sprintf(res,"%" LL_MASK "d", result);
+               RETURN_STRING(res,1);
+       }
+       RETURN_LONG((long)result);
+}
+
+/* }}} */
+
 #endif /* HAVE_IBASE */
 
 /*
index 6fc8f22896ae0ab424284ee551fcdc5b714553d3..176dacdbe6223441f9dc53fb7ecb883d0c0f6332 100644 (file)
@@ -50,7 +50,7 @@ PHP_FUNCTION(ibase_free_query);
 #if HAVE_STRFTIME
 PHP_FUNCTION(ibase_timefmt);
 #endif
-
+PHP_FUNCTION(ibase_gen_id);
 PHP_FUNCTION(ibase_num_fields);
 PHP_FUNCTION(ibase_num_params);
 #if abies_0