1999-04-22 02:48:28 +00:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2004-01-08 08:18:22 +00:00
| PHP Version 5 |
1999-04-22 02:48:28 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2004-01-08 08:18:22 +00:00
| Copyright ( c ) 1997 - 2004 The PHP Group |
1999-04-22 02:48:28 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2003-06-10 20:04:29 +00:00
| This source file is subject to version 3.0 of the PHP license , |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE , and is |
2003-06-10 20:04:29 +00:00
| available through the world - wide - web at the following url : |
| http : //www.php.net/license/3_0.txt. |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ php . net so we can mail you a copy immediately . |
1999-04-22 02:48:28 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Authors : Zeev Suraski < zeev @ zend . com > |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2000-05-24 16:28:16 +00:00
| php_sybase_get_column_content_with_type ( ) based on code by : |
1999-04-22 02:48:28 +00:00
| Muhammad A Muquit < MA_Muquit @ fccc . edu > |
| Rasmus Lerdorf < rasmus @ lerdorf . on . ca > |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
/* $Id$ */
2001-05-24 10:07:29 +00:00
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
1999-04-22 02:48:28 +00:00
# include "php.h"
2000-06-30 16:24:17 +00:00
# include "php_sybase_db.h"
1999-12-04 19:19:57 +00:00
# include "ext/standard/php_standard.h"
2000-04-06 21:07:44 +00:00
# include "ext/standard/info.h"
1999-04-22 02:48:28 +00:00
# include "php_globals.h"
# if HAVE_SYBASE
# include <sybfront.h>
# include <sybdb.h>
# include <syberror.h>
# if BROKEN_SYBASE_PCONNECTS
# include "http_log.h"
# endif
2001-09-04 07:41:20 +00:00
/* Moved these structures/defines into the .c file (or into a *private* header),
because leaving them in php_sybase_db . h caused namespace pollution in
main / internal_functions . c . */
# define coltype(j) dbcoltype(sybase_ptr->link,j)
# define intcol(i) ((int) *(DBINT *) dbdata(sybase_ptr->link,i))
# define smallintcol(i) ((int) *(DBSMALLINT *) dbdata(sybase_ptr->link,i))
# define tinyintcol(i) ((int) *(DBTINYINT *) dbdata(sybase_ptr->link,i))
# define anyintcol(j) (coltype(j)==SYBINT4?intcol(j):(coltype(j)==SYBINT2?smallintcol(j):tinyintcol(j)))
# define charcol(i) ((DBCHAR *) dbdata(sybase_ptr->link,i))
# define floatcol(i) ((float) *(DBFLT8 *) dbdata(sybase_ptr->link,i))
typedef struct sybase_link_struct sybase_link ;
struct sybase_link_struct {
LOGINREC * login ;
DBPROCESS * link ;
int valid ;
} ;
# define SYBASE_ROWS_BLOCK 128
typedef struct {
char * name , * column_source ;
int max_length , numeric ;
int type ;
} sybase_field ;
typedef struct {
pval * * * data ;
sybase_field * fields ;
sybase_link * sybase_ptr ;
int cur_row , cur_field ;
int num_rows , num_fields ;
} sybase_result ;
1999-04-22 02:48:28 +00:00
function_entry sybase_functions [ ] = {
1999-12-18 03:20:53 +00:00
PHP_FE ( sybase_connect , NULL )
PHP_FE ( sybase_pconnect , NULL )
PHP_FE ( sybase_close , NULL )
PHP_FE ( sybase_select_db , NULL )
PHP_FE ( sybase_query , NULL )
PHP_FE ( sybase_free_result , NULL )
PHP_FE ( sybase_get_last_message , NULL )
PHP_FE ( sybase_num_rows , NULL )
PHP_FE ( sybase_num_fields , NULL )
PHP_FE ( sybase_fetch_row , NULL )
PHP_FE ( sybase_fetch_array , NULL )
PHP_FE ( sybase_fetch_object , NULL )
PHP_FE ( sybase_data_seek , NULL )
PHP_FE ( sybase_fetch_field , NULL )
PHP_FE ( sybase_field_seek , NULL )
PHP_FE ( sybase_result , NULL )
2001-03-16 01:21:32 +00:00
PHP_FE ( sybase_affected_rows , NULL )
1999-12-18 03:20:53 +00:00
PHP_FE ( sybase_min_error_severity , NULL )
PHP_FE ( sybase_min_message_severity , NULL )
PHP_FALIAS ( mssql_connect , sybase_connect , NULL )
PHP_FALIAS ( mssql_pconnect , sybase_pconnect , NULL )
PHP_FALIAS ( mssql_close , sybase_close , NULL )
PHP_FALIAS ( mssql_select_db , sybase_select_db , NULL )
PHP_FALIAS ( mssql_query , sybase_query , NULL )
PHP_FALIAS ( mssql_free_result , sybase_free_result , NULL )
PHP_FALIAS ( mssql_get_last_message , sybase_get_last_message , NULL )
PHP_FALIAS ( mssql_num_rows , sybase_num_rows , NULL )
PHP_FALIAS ( mssql_num_fields , sybase_num_fields , NULL )
PHP_FALIAS ( mssql_fetch_row , sybase_fetch_row , NULL )
PHP_FALIAS ( mssql_fetch_array , sybase_fetch_array , NULL )
2000-11-28 09:39:01 +00:00
PHP_FALIAS ( mssql_fetch_object , sybase_fetch_object , NULL )
1999-12-18 03:20:53 +00:00
PHP_FALIAS ( mssql_data_seek , sybase_data_seek , NULL )
PHP_FALIAS ( mssql_fetch_field , sybase_fetch_field , NULL )
PHP_FALIAS ( mssql_field_seek , sybase_field_seek , NULL )
PHP_FALIAS ( mssql_result , sybase_result , NULL )
2001-03-16 01:21:32 +00:00
PHP_FALIAS ( mssql_affected_rows , sybase_affected_rows , NULL )
1999-12-18 03:20:53 +00:00
PHP_FALIAS ( mssql_min_error_severity , sybase_min_error_severity , NULL )
PHP_FALIAS ( mssql_min_message_severity , sybase_min_message_severity , NULL )
1999-04-22 02:48:28 +00:00
{ NULL , NULL , NULL }
} ;
1999-12-17 20:55:31 +00:00
zend_module_entry sybase_module_entry = {
2001-10-11 23:33:59 +00:00
STANDARD_MODULE_HEADER ,
" sybase " , sybase_functions , PHP_MINIT ( sybase ) , PHP_MSHUTDOWN ( sybase ) , PHP_RINIT ( sybase ) , PHP_RSHUTDOWN ( sybase ) , PHP_MINFO ( sybase ) , NO_VERSION_YET , STANDARD_MODULE_PROPERTIES
1999-04-22 02:48:28 +00:00
} ;
2000-05-23 09:33:51 +00:00
# ifdef COMPILE_DL_SYBASE
2000-05-02 00:30:36 +00:00
ZEND_GET_MODULE ( sybase )
1999-04-22 02:48:28 +00:00
# endif
1999-12-18 03:20:53 +00:00
THREAD_LS sybase_module php_sybase_module ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
# define CHECK_LINK(link) { if (link==-1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: A link to the server could not be established"); RETURN_FALSE; } }
1999-04-22 02:48:28 +00:00
2000-06-30 16:24:17 +00:00
static void php_sybase_get_column_content ( sybase_link * sybase_ptr , int offset , pval * * result_ptr , int column_type ) ;
1999-04-22 02:48:28 +00:00
/* error handler */
1999-12-18 03:20:53 +00:00
static int php_sybase_error_handler ( DBPROCESS * dbproc , int severity , int dberr ,
1999-04-22 02:48:28 +00:00
int oserr , char * dberrstr , char * oserrstr )
{
1999-12-18 03:20:53 +00:00
if ( severity > = php_sybase_module . min_error_severity ) {
2003-10-03 13:03:40 +00:00
TSRMLS_FETCH ( ) ;
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase error: %s (severity %d) " , dberrstr , severity ) ;
1999-04-22 02:48:28 +00:00
}
return INT_CANCEL ;
}
/* message handler */
1999-12-18 03:20:53 +00:00
static int php_sybase_message_handler ( DBPROCESS * dbproc , DBINT msgno , int msgstate ,
1999-04-22 02:48:28 +00:00
int severity , char * msgtext , char * srvname ,
char * procname , DBUSMALLINT line )
{
1999-12-18 03:20:53 +00:00
if ( severity > = php_sybase_module . min_message_severity ) {
2003-10-03 13:03:40 +00:00
TSRMLS_FETCH ( ) ;
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase message: %s (severity %d) " , msgtext , severity ) ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
STR_FREE ( php_sybase_module . server_message ) ;
php_sybase_module . server_message = estrdup ( msgtext ) ;
1999-04-22 02:48:28 +00:00
return 0 ;
}
2001-07-31 04:53:54 +00:00
static int _clean_invalid_results ( list_entry * le TSRMLS_DC )
1999-04-22 02:48:28 +00:00
{
2001-09-25 21:58:48 +00:00
if ( Z_TYPE_P ( le ) = = php_sybase_module . le_result ) {
1999-04-22 02:48:28 +00:00
sybase_link * sybase_ptr = ( ( sybase_result * ) le - > ptr ) - > sybase_ptr ;
if ( ! sybase_ptr - > valid ) {
return 1 ;
}
}
return 0 ;
}
2001-07-31 05:44:11 +00:00
static void _free_sybase_result ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
1999-04-22 02:48:28 +00:00
{
2000-10-20 18:25:16 +00:00
sybase_result * result = ( sybase_result * ) rsrc - > ptr ;
1999-04-22 02:48:28 +00:00
int i , j ;
if ( result - > data ) {
for ( i = 0 ; i < result - > num_rows ; i + + ) {
for ( j = 0 ; j < result - > num_fields ; j + + ) {
2000-06-30 16:24:17 +00:00
zval_ptr_dtor ( & result - > data [ i ] [ j ] ) ;
1999-04-22 02:48:28 +00:00
}
efree ( result - > data [ i ] ) ;
}
efree ( result - > data ) ;
}
if ( result - > fields ) {
for ( i = 0 ; i < result - > num_fields ; i + + ) {
STR_FREE ( result - > fields [ i ] . name ) ;
STR_FREE ( result - > fields [ i ] . column_source ) ;
}
efree ( result - > fields ) ;
}
efree ( result ) ;
}
2001-07-31 05:44:11 +00:00
static void _close_sybase_link ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
1999-04-22 02:48:28 +00:00
{
2000-10-20 18:25:16 +00:00
sybase_link * sybase_ptr = ( sybase_link * ) rsrc - > ptr ;
2000-02-26 21:35:15 +00:00
1999-04-22 02:48:28 +00:00
sybase_ptr - > valid = 0 ;
2000-01-15 13:39:19 +00:00
/*
this can cause crashes in the current model .
if the resource gets destroyed via destroy_resource_list ( ) resource_list
2000-10-29 09:14:55 +00:00
will * not * be in a consistent state . thies @ thieso . net
2000-01-15 13:39:19 +00:00
*/
2001-07-31 04:53:54 +00:00
zend_hash_apply ( & EG ( regular_list ) , ( apply_func_t ) _clean_invalid_results TSRMLS_CC ) ;
1999-04-22 02:48:28 +00:00
dbclose ( sybase_ptr - > link ) ;
dbloginfree ( sybase_ptr - > login ) ;
efree ( sybase_ptr ) ;
1999-12-18 03:20:53 +00:00
php_sybase_module . num_links - - ;
1999-04-22 02:48:28 +00:00
}
2001-07-31 05:44:11 +00:00
static void _close_sybase_plink ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
1999-04-22 02:48:28 +00:00
{
2000-10-20 18:25:16 +00:00
sybase_link * sybase_ptr = ( sybase_link * ) rsrc - > ptr ;
2001-07-31 05:44:11 +00:00
1999-04-22 02:48:28 +00:00
dbclose ( sybase_ptr - > link ) ;
dbloginfree ( sybase_ptr - > login ) ;
free ( sybase_ptr ) ;
1999-12-18 03:20:53 +00:00
php_sybase_module . num_persistent - - ;
php_sybase_module . num_links - - ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
PHP_MINIT_FUNCTION ( sybase )
1999-04-22 02:48:28 +00:00
{
char * interface_file ;
if ( dbinit ( ) = = FAIL ) {
return FAILURE ;
}
1999-12-18 03:20:53 +00:00
dberrhandle ( ( EHANDLEFUNC ) php_sybase_error_handler ) ;
dbmsghandle ( ( MHANDLEFUNC ) php_sybase_message_handler ) ;
1999-04-22 02:48:28 +00:00
if ( cfg_get_string ( " sybase.interface_file " , & interface_file ) = = SUCCESS ) {
dbsetifile ( interface_file ) ;
}
1999-12-18 03:20:53 +00:00
if ( cfg_get_long ( " sybase.allow_persistent " , & php_sybase_module . allow_persistent ) = = FAILURE ) {
php_sybase_module . allow_persistent = 1 ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
if ( cfg_get_long ( " sybase.max_persistent " , & php_sybase_module . max_persistent ) = = FAILURE ) {
php_sybase_module . max_persistent = - 1 ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
if ( cfg_get_long ( " sybase.max_links " , & php_sybase_module . max_links ) = = FAILURE ) {
php_sybase_module . max_links = - 1 ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
if ( cfg_get_long ( " sybase.min_error_severity " , & php_sybase_module . cfg_min_error_severity ) = = FAILURE ) {
php_sybase_module . cfg_min_error_severity = 10 ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
if ( cfg_get_long ( " sybase.min_message_severity " , & php_sybase_module . cfg_min_message_severity ) = = FAILURE ) {
php_sybase_module . cfg_min_message_severity = 10 ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
if ( cfg_get_long ( " sybase.compatability_mode " , & php_sybase_module . compatability_mode ) = = FAILURE ) {
php_sybase_module . compatability_mode = 0 ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
php_sybase_module . num_persistent = 0 ;
2000-10-25 17:44:02 +00:00
php_sybase_module . le_link = zend_register_list_destructors_ex ( _close_sybase_link , NULL , " sybase-db link " , module_number ) ;
php_sybase_module . le_plink = zend_register_list_destructors_ex ( NULL , _close_sybase_plink , " sybase-db link persistent " , module_number ) ;
php_sybase_module . le_result = zend_register_list_destructors_ex ( _free_sybase_result , NULL , " sybase-db result " , module_number ) ;
1999-04-22 02:48:28 +00:00
return SUCCESS ;
}
1999-12-18 03:20:53 +00:00
PHP_RINIT_FUNCTION ( sybase )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
php_sybase_module . default_link = - 1 ;
php_sybase_module . num_links = php_sybase_module . num_persistent ;
2004-04-16 16:27:19 +00:00
php_sybase_module . appname = estrndup ( " PHP " PHP_VERSION , sizeof ( " PHP " PHP_VERSION ) ) ;
2004-07-19 07:19:50 +00:00
php_sybase_module . server_message = STR_EMPTY_ALLOC ( ) ;
1999-12-18 03:20:53 +00:00
php_sybase_module . min_error_severity = php_sybase_module . cfg_min_error_severity ;
php_sybase_module . min_message_severity = php_sybase_module . cfg_min_message_severity ;
1999-04-22 02:48:28 +00:00
return SUCCESS ;
}
1999-12-18 03:20:53 +00:00
PHP_MSHUTDOWN_FUNCTION ( sybase )
1999-04-22 02:48:28 +00:00
{
dbexit ( ) ;
return SUCCESS ;
}
1999-12-18 03:20:53 +00:00
PHP_RSHUTDOWN_FUNCTION ( sybase )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
efree ( php_sybase_module . appname ) ;
2004-07-10 07:46:17 +00:00
php_sybase_module . appname = NULL ;
1999-12-18 03:20:53 +00:00
STR_FREE ( php_sybase_module . server_message ) ;
2004-07-10 07:46:17 +00:00
php_sybase_module . server_message = NULL ;
1999-04-22 02:48:28 +00:00
return SUCCESS ;
}
1999-12-18 03:20:53 +00:00
static void php_sybase_do_connect ( INTERNAL_FUNCTION_PARAMETERS , int persistent )
1999-04-22 02:48:28 +00:00
{
2002-03-06 15:59:57 +00:00
char * user = NULL , * passwd = NULL , * host = NULL , * charset = NULL , * appname = NULL ;
1999-04-22 02:48:28 +00:00
char * hashed_details ;
int hashed_details_length ;
sybase_link sybase , * sybase_ptr ;
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-22 02:48:28 +00:00
case 0 : /* defaults */
hashed_details_length = 6 + 3 ;
hashed_details = ( char * ) emalloc ( hashed_details_length + 1 ) ;
strcpy ( hashed_details , " sybase___ " ) ;
break ;
case 1 : {
2003-01-15 03:01:17 +00:00
zval * * yyhost ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 1 , & yyhost ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_string_ex ( yyhost ) ;
host = Z_STRVAL_PP ( yyhost ) ;
hashed_details_length = Z_STRLEN_PP ( yyhost ) + 6 + 5 ;
1999-04-22 02:48:28 +00:00
hashed_details = ( char * ) emalloc ( hashed_details_length + 1 ) ;
2003-01-15 03:01:17 +00:00
sprintf ( hashed_details , " sybase_%s____ " , Z_STRVAL_PP ( yyhost ) ) ;
1999-04-22 02:48:28 +00:00
}
break ;
case 2 : {
2003-01-15 03:01:17 +00:00
zval * * yyhost , * * yyuser ;
1999-04-22 02:48:28 +00:00
2004-01-28 01:43:45 +00:00
if ( zend_get_parameters_ex ( 2 , & yyhost , & yyuser ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_string_ex ( yyhost ) ;
convert_to_string_ex ( yyuser ) ;
host = Z_STRVAL_PP ( yyhost ) ;
user = Z_STRVAL_PP ( yyuser ) ;
hashed_details_length = Z_STRLEN_PP ( yyhost ) + Z_STRLEN_PP ( yyuser ) + 6 + 5 ;
1999-04-22 02:48:28 +00:00
hashed_details = ( char * ) emalloc ( hashed_details_length + 1 ) ;
2003-01-15 03:01:17 +00:00
sprintf ( hashed_details , " sybase_%s_%s___ " , Z_STRVAL_PP ( yyhost ) , Z_STRVAL_PP ( yyuser ) ) ;
1999-04-22 02:48:28 +00:00
}
break ;
case 3 : {
2003-01-15 03:01:17 +00:00
zval * * yyhost , * * yyuser , * * yypasswd ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 3 , & yyhost , & yyuser , & yypasswd ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_string_ex ( yyhost ) ;
convert_to_string_ex ( yyuser ) ;
convert_to_string_ex ( yypasswd ) ;
host = Z_STRVAL_PP ( yyhost ) ;
user = Z_STRVAL_PP ( yyuser ) ;
passwd = Z_STRVAL_PP ( yypasswd ) ;
hashed_details_length = Z_STRLEN_PP ( yyhost ) + Z_STRLEN_PP ( yyuser ) + Z_STRLEN_PP ( yypasswd ) + 6 + 5 ;
1999-04-22 02:48:28 +00:00
hashed_details = ( char * ) emalloc ( hashed_details_length + 1 ) ;
2003-01-15 03:01:17 +00:00
sprintf ( hashed_details , " sybase_%s_%s_%s__ " , Z_STRVAL_PP ( yyhost ) , Z_STRVAL_PP ( yyuser ) , Z_STRVAL_PP ( yypasswd ) ) ; /* SAFE */
1999-04-22 02:48:28 +00:00
}
break ;
2000-08-07 00:15:39 +00:00
case 4 : {
2003-01-15 03:01:17 +00:00
zval * * yyhost , * * yyuser , * * yypasswd , * * yycharset ;
2000-08-07 00:15:39 +00:00
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 4 , & yyhost , & yyuser , & yypasswd , & yycharset ) = = FAILURE ) {
2000-08-07 00:15:39 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_string_ex ( yyhost ) ;
convert_to_string_ex ( yyuser ) ;
convert_to_string_ex ( yypasswd ) ;
convert_to_string_ex ( yycharset ) ;
host = Z_STRVAL_PP ( yyhost ) ;
user = Z_STRVAL_PP ( yyuser ) ;
passwd = Z_STRVAL_PP ( yypasswd ) ;
charset = Z_STRVAL_PP ( yycharset ) ;
hashed_details_length = Z_STRLEN_PP ( yyhost ) + Z_STRLEN_PP ( yyuser ) + Z_STRLEN_PP ( yypasswd ) + Z_STRLEN_PP ( yycharset ) + 6 + 5 ;
2000-08-07 00:15:39 +00:00
hashed_details = ( char * ) emalloc ( hashed_details_length + 1 ) ;
2003-01-15 03:01:17 +00:00
sprintf ( hashed_details , " sybase_%s_%s_%s_%s_ " , Z_STRVAL_PP ( yyhost ) , Z_STRVAL_PP ( yyuser ) , Z_STRVAL_PP ( yypasswd ) , Z_STRVAL_PP ( yycharset ) ) ; /* SAFE */
2002-03-06 15:59:57 +00:00
}
break ;
case 5 : {
2003-01-15 03:01:17 +00:00
zval * * yyhost , * * yyuser , * * yypasswd , * * yycharset , * * yyappname ;
2002-03-06 15:59:57 +00:00
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 5 , & yyhost , & yyuser , & yypasswd , & yycharset , & yyappname ) = = FAILURE ) {
2002-03-06 15:59:57 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_string_ex ( yyhost ) ;
convert_to_string_ex ( yyuser ) ;
convert_to_string_ex ( yypasswd ) ;
convert_to_string_ex ( yycharset ) ;
convert_to_string_ex ( yyappname ) ;
host = Z_STRVAL_PP ( yyhost ) ;
user = Z_STRVAL_PP ( yyuser ) ;
passwd = Z_STRVAL_PP ( yypasswd ) ;
charset = Z_STRVAL_PP ( yycharset ) ;
appname = Z_STRVAL_PP ( yyappname ) ;
2003-08-04 14:34:54 +00:00
hashed_details_length = Z_STRLEN_PP ( yyhost ) + Z_STRLEN_PP ( yyuser ) + Z_STRLEN_PP ( yypasswd ) + Z_STRLEN_PP ( yycharset ) + Z_STRLEN_PP ( yyappname ) + 6 + 5 ;
2002-03-06 15:59:57 +00:00
hashed_details = ( char * ) emalloc ( hashed_details_length + 1 ) ;
2003-01-15 03:01:17 +00:00
sprintf ( hashed_details , " sybase_%s_%s_%s_%s_%s " , Z_STRVAL_PP ( yyhost ) , Z_STRVAL_PP ( yyuser ) , Z_STRVAL_PP ( yypasswd ) , Z_STRVAL_PP ( yycharset ) , Z_STRVAL_PP ( yyappname ) ) ; /* SAFE */
2000-08-07 00:15:39 +00:00
}
break ;
1999-04-22 02:48:28 +00:00
default :
WRONG_PARAM_COUNT ;
break ;
}
/* set a DBLOGIN record */
if ( ( sybase . login = dblogin ( ) ) = = NULL ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Unable to allocate login record " ) ;
2003-04-15 01:48:02 +00:00
goto err ;
1999-04-22 02:48:28 +00:00
}
if ( user ) {
DBSETLUSER ( sybase . login , user ) ;
}
if ( passwd ) {
DBSETLPWD ( sybase . login , passwd ) ;
}
2000-08-07 00:15:39 +00:00
if ( charset ) {
DBSETLCHARSET ( sybase . login , charset ) ;
}
2002-03-06 15:59:57 +00:00
if ( appname ) {
DBSETLAPP ( sybase . login , appname ) ;
} else {
DBSETLAPP ( sybase . login , php_sybase_module . appname ) ;
}
1999-04-22 02:48:28 +00:00
sybase . valid = 1 ;
1999-12-18 03:20:53 +00:00
if ( ! php_sybase_module . allow_persistent ) {
1999-04-22 02:48:28 +00:00
persistent = 0 ;
}
if ( persistent ) {
list_entry * le ;
/* try to find if we already have this link in our persistent list */
2000-02-26 21:35:15 +00:00
if ( zend_hash_find ( & EG ( persistent_list ) , hashed_details , hashed_details_length + 1 , ( void * * ) & le ) = = FAILURE ) { /* we don't */
1999-04-22 02:48:28 +00:00
list_entry new_le ;
1999-12-18 03:20:53 +00:00
if ( php_sybase_module . max_links ! = - 1 & & php_sybase_module . num_links > = php_sybase_module . max_links ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Too many open links (%d) " , php_sybase_module . num_links ) ;
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
if ( php_sybase_module . max_persistent ! = - 1 & & php_sybase_module . num_persistent > = php_sybase_module . max_persistent ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Too many open persistent links (%d) " , php_sybase_module . num_persistent ) ;
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
/* create the link */
2001-05-02 18:38:17 +00:00
if ( ( sybase . link = PHP_SYBASE_DBOPEN ( sybase . login , host ) ) = = FAIL ) {
2003-01-15 03:01:17 +00:00
/*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to connect to server: %s",sybase_error(sybase));*/
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
if ( dbsetopt ( sybase . link , DBBUFFER , " 2 " , - 1 ) = = FAIL ) {
2003-04-15 01:48:02 +00:00
goto err_link ;
1999-04-22 02:48:28 +00:00
}
/* hash it up */
sybase_ptr = ( sybase_link * ) malloc ( sizeof ( sybase_link ) ) ;
memcpy ( sybase_ptr , & sybase , sizeof ( sybase_link ) ) ;
2001-09-25 21:58:48 +00:00
Z_TYPE ( new_le ) = php_sybase_module . le_plink ;
1999-04-22 02:48:28 +00:00
new_le . ptr = sybase_ptr ;
2000-02-26 21:35:15 +00:00
if ( zend_hash_update ( & EG ( persistent_list ) , hashed_details , hashed_details_length + 1 , ( void * ) & new_le , sizeof ( list_entry ) , NULL ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
free ( sybase_ptr ) ;
2003-04-15 01:48:02 +00:00
goto err_link ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
php_sybase_module . num_persistent + + ;
php_sybase_module . num_links + + ;
1999-04-22 02:48:28 +00:00
} else { /* we do */
2001-09-25 21:58:48 +00:00
if ( Z_TYPE_P ( le ) ! = php_sybase_module . le_plink ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Hashed persistent link is not a Sybase link! " ) ;
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
sybase_ptr = ( sybase_link * ) le - > ptr ;
/* test that the link hasn't died */
if ( DBDEAD ( sybase_ptr - > link ) = = TRUE ) {
2001-05-02 18:38:17 +00:00
if ( ( sybase_ptr - > link = PHP_SYBASE_DBOPEN ( sybase_ptr - > login , host ) ) = = FAIL ) {
2003-01-15 03:01:17 +00:00
/*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Link to server lost, unable to reconnect");*/
2000-02-26 21:35:15 +00:00
zend_hash_del ( & EG ( persistent_list ) , hashed_details , hashed_details_length + 1 ) ;
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
if ( dbsetopt ( sybase_ptr - > link , DBBUFFER , " 2 " , - 1 ) = = FAIL ) {
2000-02-26 21:35:15 +00:00
zend_hash_del ( & EG ( persistent_list ) , hashed_details , hashed_details_length + 1 ) ;
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
}
}
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( return_value ) = zend_list_insert ( sybase_ptr , php_sybase_module . le_plink ) ;
Z_TYPE_P ( return_value ) = IS_LONG ;
1999-04-22 02:48:28 +00:00
} else { /* non persistent */
list_entry * index_ptr , new_index_ptr ;
/* first we check the hash for the hashed_details key. if it exists,
* it should point us to the right offset where the actual sybase link sits .
* if it doesn ' t , open a new sybase link , add it to the resource list ,
* and add a pointer to it with hashed_details as the key .
*/
2000-02-26 21:35:15 +00:00
if ( zend_hash_find ( & EG ( regular_list ) , hashed_details , hashed_details_length + 1 , ( void * * ) & index_ptr ) = = SUCCESS ) {
1999-04-22 02:48:28 +00:00
int type , link ;
void * ptr ;
2001-09-25 21:58:48 +00:00
if ( Z_TYPE_P ( index_ptr ) ! = le_index_ptr ) {
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
link = ( int ) index_ptr - > ptr ;
1999-12-17 19:51:39 +00:00
ptr = zend_list_find ( link , & type ) ; /* check if the link is still there */
1999-12-18 03:20:53 +00:00
if ( ptr & & ( type = = php_sybase_module . le_link | | type = = php_sybase_module . le_plink ) ) {
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( return_value ) = php_sybase_module . default_link = link ;
Z_TYPE_P ( return_value ) = IS_LONG ;
1999-04-22 02:48:28 +00:00
efree ( hashed_details ) ;
2003-04-15 00:59:42 +00:00
dbloginfree ( sybase . login ) ;
1999-04-22 02:48:28 +00:00
return ;
} else {
2000-02-26 21:35:15 +00:00
zend_hash_del ( & EG ( regular_list ) , hashed_details , hashed_details_length + 1 ) ;
1999-04-22 02:48:28 +00:00
}
}
1999-12-18 03:20:53 +00:00
if ( php_sybase_module . max_links ! = - 1 & & php_sybase_module . num_links > = php_sybase_module . max_links ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Too many open links (%d) " , php_sybase_module . num_links ) ;
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
2001-05-02 18:38:17 +00:00
if ( ( sybase . link = PHP_SYBASE_DBOPEN ( sybase . login , host ) ) = = NULL ) {
2003-01-15 03:01:17 +00:00
/*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to connect to server: %s",sybase_error(sybase));*/
2003-04-15 01:48:02 +00:00
goto err_login ;
1999-04-22 02:48:28 +00:00
}
if ( dbsetopt ( sybase . link , DBBUFFER , " 2 " , - 1 ) = = FAIL ) {
2003-04-15 01:48:02 +00:00
goto err_link ;
1999-04-22 02:48:28 +00:00
}
/* add it to the list */
sybase_ptr = ( sybase_link * ) emalloc ( sizeof ( sybase_link ) ) ;
memcpy ( sybase_ptr , & sybase , sizeof ( sybase_link ) ) ;
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( return_value ) = zend_list_insert ( sybase_ptr , php_sybase_module . le_link ) ;
Z_TYPE_P ( return_value ) = IS_LONG ;
1999-04-22 02:48:28 +00:00
/* add it to the hash */
2001-09-25 21:58:48 +00:00
new_index_ptr . ptr = ( void * ) Z_LVAL_P ( return_value ) ;
Z_TYPE ( new_index_ptr ) = le_index_ptr ;
2000-02-26 21:35:15 +00:00
if ( zend_hash_update ( & EG ( regular_list ) , hashed_details , hashed_details_length + 1 , ( void * ) & new_index_ptr , sizeof ( list_entry ) , NULL ) = = FAILURE ) {
2003-04-15 01:48:02 +00:00
goto err_link ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
php_sybase_module . num_links + + ;
1999-04-22 02:48:28 +00:00
}
efree ( hashed_details ) ;
2001-09-25 21:58:48 +00:00
php_sybase_module . default_link = Z_LVAL_P ( return_value ) ;
2003-04-15 01:48:02 +00:00
return ;
err_link :
dbclose ( sybase . link ) ;
err_login :
dbloginfree ( sybase . login ) ;
err :
efree ( hashed_details ) ;
RETURN_FALSE ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
static int php_sybase_get_default_link ( INTERNAL_FUNCTION_PARAMETERS )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
if ( php_sybase_module . default_link = = - 1 ) { /* no link opened yet, implicitly open one */
1999-07-22 23:54:54 +00:00
ht = 0 ;
1999-12-18 03:20:53 +00:00
php_sybase_do_connect ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
1999-04-22 02:48:28 +00:00
}
1999-12-18 03:20:53 +00:00
return php_sybase_module . default_link ;
1999-04-22 02:48:28 +00:00
}
2002-03-06 15:59:57 +00:00
/* {{{ proto int sybase_connect([string host [, string user [, string password [, string charset [, string appname]]]]])
2000-05-24 16:28:16 +00:00
Open Sybase server connection */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_connect )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
php_sybase_do_connect ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2002-03-06 15:59:57 +00:00
/* {{{ proto int sybase_pconnect([string host [, string user [, string password [, string charset [, string appname]]]]])
2000-05-24 16:28:16 +00:00
Open persistent Sybase connection */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_pconnect )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
php_sybase_do_connect ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto bool sybase_close([int link_id])
Close Sybase connection */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_close )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_link_index ;
1999-04-22 02:48:28 +00:00
int id , type ;
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-22 02:48:28 +00:00
case 0 :
1999-12-18 03:20:53 +00:00
id = php_sybase_module . default_link ;
1999-04-22 02:48:28 +00:00
break ;
case 1 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 1 , & sybase_link_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_link_index ) ;
id = Z_LVAL_PP ( sybase_link_index ) ;
1999-04-22 02:48:28 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
1999-12-17 19:51:39 +00:00
zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_link & & type ! = php_sybase_module . le_plink ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase link index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
1999-12-17 19:51:39 +00:00
zend_list_delete ( id ) ;
1999-04-22 02:48:28 +00:00
RETURN_TRUE ;
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto bool sybase_select_db(string database [, int link_id])
Select Sybase database */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_select_db )
1999-04-22 02:48:28 +00:00
{
2003-08-05 16:02:12 +00:00
zval * * db , * * sybase_link_index ;
1999-04-22 02:48:28 +00:00
int id , type ;
sybase_link * sybase_ptr ;
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-22 02:48:28 +00:00
case 1 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 1 , & db ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
1999-12-18 03:20:53 +00:00
id = php_sybase_get_default_link ( INTERNAL_FUNCTION_PARAM_PASSTHRU ) ;
1999-04-22 02:48:28 +00:00
break ;
case 2 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 2 , & db , & sybase_link_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_link_index ) ;
id = Z_LVAL_PP ( sybase_link_index ) ;
1999-04-22 02:48:28 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
CHECK_LINK ( id ) ;
1999-12-17 19:51:39 +00:00
sybase_ptr = ( sybase_link * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_link & & type ! = php_sybase_module . le_plink ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase link index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-08-04 14:34:54 +00:00
convert_to_string_ex ( db ) ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
if ( dbuse ( sybase_ptr - > link , Z_STRVAL_PP ( db ) ) = = FAIL ) {
/*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to select database: %s",sybase_error(sybase));*/
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
} else {
RETURN_TRUE ;
}
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-06-30 16:24:17 +00:00
static void php_sybase_get_column_content ( sybase_link * sybase_ptr , int offset , pval * * result_ptr , int column_type )
1999-04-22 02:48:28 +00:00
{
2000-07-03 22:04:10 +00:00
zval * result ;
2000-06-30 16:24:17 +00:00
ALLOC_INIT_ZVAL ( result ) ;
2000-07-03 22:04:10 +00:00
* result_ptr = result ;
2000-06-30 16:24:17 +00:00
1999-04-22 02:48:28 +00:00
if ( dbdatlen ( sybase_ptr - > link , offset ) = = 0 ) {
2001-07-11 12:42:25 +00:00
ZVAL_FALSE ( result ) ;
1999-04-22 02:48:28 +00:00
return ;
}
switch ( column_type )
{
case SYBINT2 :
case SYBINT4 : {
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( result ) = ( long ) anyintcol ( offset ) ;
Z_TYPE_P ( result ) = IS_LONG ;
1999-04-22 02:48:28 +00:00
break ;
}
case SYBCHAR :
case SYBTEXT : {
int length ;
char * data = charcol ( offset ) ;
length = dbdatlen ( sybase_ptr - > link , offset ) ;
while ( length > 0 & & charcol ( offset ) [ length - 1 ] = = ' ' ) { /* nuke trailing whitespace */
length - - ;
}
2001-09-25 21:58:48 +00:00
Z_STRVAL_P ( result ) = estrndup ( data , length ) ;
Z_STRLEN_P ( result ) = length ;
Z_TYPE_P ( result ) = IS_STRING ;
1999-04-22 02:48:28 +00:00
break ;
}
/*case SYBFLT8:*/
case SYBREAL : {
2001-09-25 21:58:48 +00:00
Z_DVAL_P ( result ) = ( double ) floatcol ( offset ) ;
Z_TYPE_P ( result ) = IS_DOUBLE ;
1999-04-22 02:48:28 +00:00
break ;
}
default : {
if ( dbwillconvert ( coltype ( offset ) , SYBCHAR ) ) {
char * res_buf ;
int res_length = dbdatlen ( sybase_ptr - > link , offset ) ;
2002-12-10 15:18:01 +00:00
int src_length = res_length ;
1999-04-22 02:48:28 +00:00
register char * p ;
switch ( coltype ( offset ) ) {
case SYBBINARY :
case SYBVARBINARY :
2002-12-20 19:37:07 +00:00
case SYBIMAGE :
2002-12-10 15:18:01 +00:00
res_length * = 2 ;
break ;
1999-04-22 02:48:28 +00:00
case SYBCHAR :
case SYBVARCHAR :
case SYBTEXT :
break ;
default :
/* take no chances, no telling how big the result would really be */
res_length + = 20 ;
break ;
}
res_buf = ( char * ) emalloc ( res_length + 1 ) ;
1999-11-05 08:48:09 +00:00
memset ( res_buf , ' ' , res_length + 1 ) ; /* XXX i'm sure there's a better way
but i don ' t have sybase here to test
2000-10-29 09:14:55 +00:00
991105 thies @ thieso . net */
2002-12-10 20:18:25 +00:00
dbconvert ( NULL , coltype ( offset ) , dbdata ( sybase_ptr - > link , offset ) , src_length , SYBCHAR , res_buf , res_length ) ;
2003-10-16 04:24:07 +00:00
# if ilia_0
1999-04-22 02:48:28 +00:00
/* get rid of trailing spaces */
p = res_buf + res_length ;
2002-12-12 21:30:39 +00:00
while ( p > = res_buf & & ( * p = = ' ' | | * p = = ' \0 ' ) ) {
1999-04-22 02:48:28 +00:00
p - - ;
}
* ( + + p ) = 0 ; /* put a trailing NULL */
2002-12-10 15:18:01 +00:00
res_length = p - res_buf ;
2003-10-16 04:24:07 +00:00
# endif
2001-09-25 21:58:48 +00:00
Z_STRLEN_P ( result ) = res_length ;
Z_STRVAL_P ( result ) = res_buf ;
Z_TYPE_P ( result ) = IS_STRING ;
1999-04-22 02:48:28 +00:00
} else {
2003-10-03 18:05:42 +00:00
TSRMLS_FETCH ( ) ;
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: column %d has unknown data type (%d) " , offset , coltype ( offset ) ) ;
2001-07-11 12:42:25 +00:00
ZVAL_FALSE ( result ) ;
1999-04-22 02:48:28 +00:00
}
}
}
}
2000-05-24 16:28:16 +00:00
/* {{{ proto int sybase_query(string query [, int link_id])
Send Sybase query */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_query )
1999-04-22 02:48:28 +00:00
{
2003-01-15 13:55:34 +00:00
zval * * query , * * sybase_link_index ;
1999-04-22 02:48:28 +00:00
int id , type , retvalue ;
sybase_link * sybase_ptr ;
sybase_result * result ;
int num_fields ;
int blocks_initialized = 1 ;
int i , j ;
int * column_types ;
2003-07-11 02:08:14 +00:00
RETCODE dbresults_retval ;
1999-04-22 02:48:28 +00:00
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-22 02:48:28 +00:00
case 1 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 1 , & query ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
1999-12-18 03:20:53 +00:00
id = php_sybase_module . default_link ;
1999-04-22 02:48:28 +00:00
break ;
case 2 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 2 , & query , & sybase_link_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_link_index ) ;
id = Z_LVAL_PP ( sybase_link_index ) ;
1999-04-22 02:48:28 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
1999-12-17 19:51:39 +00:00
sybase_ptr = ( sybase_link * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_link & & type ! = php_sybase_module . le_plink ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase link index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_string_ex ( query ) ;
if ( dbcmd ( sybase_ptr - > link , Z_STRVAL_PP ( query ) ) = = FAIL ) {
/*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Unable to set query");*/
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-07-11 02:08:14 +00:00
if ( dbsqlexec ( sybase_ptr - > link ) = = FAIL ) {
/*php_error(E_WARNING,"Sybase: Query failed");*/
RETURN_FALSE ;
}
dbresults_retval = dbresults ( sybase_ptr - > link ) ;
if ( dbresults_retval = = FAIL ) {
2003-01-15 03:01:17 +00:00
/*php_error_docref(NULL TSRMLS_CC, E_WARNING,"Sybase: Query failed");*/
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
/* The following is more or less the equivalent of mysql_store_result().
* fetch all rows from the server into the row buffer , thus :
* 1 ) Being able to fire up another query without explicitly reading all rows
* 2 ) Having numrows accessible
*/
retvalue = dbnextrow ( sybase_ptr - > link ) ;
if ( retvalue = = FAIL ) {
RETURN_FALSE ;
}
num_fields = dbnumcols ( sybase_ptr - > link ) ;
if ( num_fields < = 0 ) {
RETURN_TRUE ;
}
2003-08-12 00:58:52 +00:00
column_types = ( int * ) safe_emalloc ( sizeof ( int ) , num_fields , 0 ) ;
1999-04-22 02:48:28 +00:00
for ( i = 0 ; i < num_fields ; i + + ) {
column_types [ i ] = coltype ( i + 1 ) ;
}
result = ( sybase_result * ) emalloc ( sizeof ( sybase_result ) ) ;
2003-08-12 00:58:52 +00:00
result - > data = ( pval * * * ) safe_emalloc ( sizeof ( pval * * ) , SYBASE_ROWS_BLOCK , 0 ) ;
1999-04-22 02:48:28 +00:00
result - > sybase_ptr = sybase_ptr ;
result - > cur_field = result - > cur_row = result - > num_rows = 0 ;
result - > num_fields = num_fields ;
i = 0 ;
while ( retvalue ! = FAIL & & retvalue ! = NO_MORE_ROWS ) {
result - > num_rows + + ;
if ( result - > num_rows > blocks_initialized * SYBASE_ROWS_BLOCK ) {
2000-06-30 16:24:17 +00:00
result - > data = ( pval * * * ) erealloc ( result - > data , sizeof ( pval * * ) * SYBASE_ROWS_BLOCK * ( + + blocks_initialized ) ) ;
1999-04-22 02:48:28 +00:00
}
2003-08-12 00:58:52 +00:00
result - > data [ i ] = ( pval * * ) safe_emalloc ( sizeof ( pval * ) , num_fields , 0 ) ;
1999-04-22 02:48:28 +00:00
for ( j = 1 ; j < = num_fields ; j + + ) {
1999-12-18 03:20:53 +00:00
php_sybase_get_column_content ( sybase_ptr , j , & result - > data [ i ] [ j - 1 ] , column_types [ j - 1 ] ) ;
if ( ! php_sybase_module . compatability_mode ) {
2000-06-30 16:24:17 +00:00
zval * cur_value = result - > data [ i ] [ j - 1 ] ;
convert_to_string ( cur_value ) ;
if ( PG ( magic_quotes_runtime ) ) {
2001-09-25 21:58:48 +00:00
Z_STRVAL_P ( cur_value ) = php_addslashes ( Z_STRVAL_P ( cur_value ) , Z_STRLEN_P ( cur_value ) , & Z_STRLEN_P ( cur_value ) , 0 TSRMLS_CC ) ;
2000-06-30 16:24:17 +00:00
}
1999-04-22 02:48:28 +00:00
}
}
retvalue = dbnextrow ( sybase_ptr - > link ) ;
dbclrbuf ( sybase_ptr - > link , DBLASTROW ( sybase_ptr - > link ) - 1 ) ;
i + + ;
}
result - > num_rows = DBCOUNT ( sybase_ptr - > link ) ;
2003-08-12 00:58:52 +00:00
result - > fields = ( sybase_field * ) safe_emalloc ( sizeof ( sybase_field ) , num_fields , 0 ) ;
1999-04-22 02:48:28 +00:00
j = 0 ;
for ( i = 0 ; i < num_fields ; i + + ) {
char * fname = dbcolname ( sybase_ptr - > link , i + 1 ) ;
char computed_buf [ 16 ] ;
if ( * fname ) {
result - > fields [ i ] . name = estrdup ( fname ) ;
} else {
if ( j > 0 ) {
snprintf ( computed_buf , 16 , " computed%d " , j ) ;
} else {
strcpy ( computed_buf , " computed " ) ;
}
result - > fields [ i ] . name = estrdup ( computed_buf ) ;
j + + ;
}
result - > fields [ i ] . max_length = dbcollen ( sybase_ptr - > link , i + 1 ) ;
result - > fields [ i ] . column_source = estrdup ( dbcolsource ( sybase_ptr - > link , i + 1 ) ) ;
if ( ! result - > fields [ i ] . column_source ) {
2004-07-19 07:19:50 +00:00
result - > fields [ i ] . column_source = STR_EMPTY_ALLOC ( ) ;
1999-04-22 02:48:28 +00:00
}
2001-09-26 08:35:48 +00:00
Z_TYPE ( result - > fields [ i ] ) = column_types [ i ] ;
1999-04-22 02:48:28 +00:00
/* set numeric flag */
switch ( column_types [ i ] ) {
case SYBINT2 :
case SYBINT4 :
case SYBFLT8 :
case SYBREAL :
result - > fields [ i ] . numeric = 1 ;
break ;
case SYBCHAR :
case SYBTEXT :
default :
result - > fields [ i ] . numeric = 0 ;
break ;
}
}
efree ( column_types ) ;
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( return_value ) = zend_list_insert ( result , php_sybase_module . le_result ) ;
Z_TYPE_P ( return_value ) = IS_LONG ;
2003-07-11 02:08:14 +00:00
/**
* mgruetzner @ rw3 . com - - 3 - 6 / 2003
*
* If you do a query that calls a stored procedure which returns
* data AND calls raiserror to generated a user - defined error message ,
* then after retrieving the first set of results above , the call
* to dbresults will have returned SUCCESS , instead of NO_MORE_RESULTS
* which indicates that the message generated by raiserror is still
* waiting to be read . If this is the case , then call dbresults
* again to force the reading of the message . This will ensure the
* get_last_message call will return the message properly if called
* after mssql_query , like it should .
*
* One thing to note , we are assuming that no more data should follow
* in this case - - only the message will be read . To make sure , I have
* added a call to dbnextrow . The assumption is that it will return
* NO_MORE_ROWS in this case . If the assumption is false , generate
* a PHP error so we can debug this case .
*
*/
if ( dbresults_retval ! = NO_MORE_RESULTS ) {
/* Call dbresults again to read message */
dbresults_retval = dbresults ( sybase_ptr - > link ) ;
/* Check assumption that dbnextrow returns NO_MORE_ROWS */
retvalue = dbnextrow ( sybase_ptr - > link ) ;
if ( retvalue ! = NO_MORE_ROWS ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Expected dbnextrow() to return NO_MORE_ROWS. " ) ;
}
}
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto bool sybase_free_result(int result)
Free result memory */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_free_result )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_result_index ;
1999-04-22 02:48:28 +00:00
sybase_result * result ;
int type ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & sybase_result_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-08-04 14:34:54 +00:00
convert_to_long_ex ( sybase_result_index ) ;
2003-01-15 03:01:17 +00:00
if ( Z_LVAL_PP ( sybase_result_index ) = = 0 ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
result = ( sybase_result * ) zend_list_find ( Z_LVAL_PP ( sybase_result_index ) , & type ) ;
1999-04-22 02:48:28 +00:00
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-08-28 21:00:24 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %ld is not a Sybase result index " , Z_LVAL_PP ( sybase_result_index ) ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-08-05 16:02:12 +00:00
zend_list_delete ( Z_LVAL_PP ( sybase_result_index ) ) ;
1999-04-22 02:48:28 +00:00
RETURN_TRUE ;
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto string sybase_get_last_message(void)
Returns the last message from server ( over min_message_severity ) */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_get_last_message )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
RETURN_STRING ( php_sybase_module . server_message , 1 ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto int sybase_num_rows(int result)
Get number of rows in result */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_num_rows )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * result_index ;
1999-04-22 02:48:28 +00:00
int type , id ;
sybase_result * result ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & result_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( result_index ) ;
id = Z_LVAL_PP ( result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( return_value ) = result - > num_rows ;
Z_TYPE_P ( return_value ) = IS_LONG ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto int sybase_num_fields(int result)
Get number of fields in result */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_num_fields )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * result_index ;
1999-04-22 02:48:28 +00:00
int type , id ;
sybase_result * result ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & result_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( result_index ) ;
id = Z_LVAL_PP ( result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2001-09-25 21:58:48 +00:00
Z_LVAL_P ( return_value ) = result - > num_fields ;
Z_TYPE_P ( return_value ) = IS_LONG ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto array sybase_fetch_row(int result)
Get row as enumerated array */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_fetch_row )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_result_index ;
1999-04-22 02:48:28 +00:00
int type , i , id ;
sybase_result * result ;
1999-07-16 22:00:53 +00:00
pval * field_content ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & sybase_result_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_result_index ) ;
id = Z_LVAL_PP ( sybase_result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
if ( result - > cur_row > = result - > num_rows ) {
RETURN_FALSE ;
}
array_init ( return_value ) ;
for ( i = 0 ; i < result - > num_fields ; i + + ) {
2000-06-30 16:24:17 +00:00
ZVAL_ADDREF ( result - > data [ result - > cur_row ] [ i ] ) ;
2001-09-25 21:58:48 +00:00
zend_hash_index_update ( Z_ARRVAL_P ( return_value ) , i , ( void * ) & result - > data [ result - > cur_row ] [ i ] , sizeof ( pval * ) , NULL ) ;
1999-04-22 02:48:28 +00:00
}
result - > cur_row + + ;
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-02-27 17:11:03 +00:00
static void php_sybase_fetch_hash ( INTERNAL_FUNCTION_PARAMETERS )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_result_index ;
1999-04-22 02:48:28 +00:00
sybase_result * result ;
int type ;
int i ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & sybase_result_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_result_index ) ;
result = ( sybase_result * ) zend_list_find ( Z_LVAL_PP ( sybase_result_index ) , & type ) ;
1999-04-22 02:48:28 +00:00
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-08-28 21:00:24 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %ld is not a Sybase result index " , Z_LVAL_PP ( sybase_result_index ) ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
if ( result - > cur_row > = result - > num_rows ) {
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
array_init ( return_value ) ;
1999-04-22 02:48:28 +00:00
for ( i = 0 ; i < result - > num_fields ; i + + ) {
2000-06-30 16:24:17 +00:00
ZVAL_ADDREF ( result - > data [ result - > cur_row ] [ i ] ) ;
2001-09-25 21:58:48 +00:00
zend_hash_index_update ( Z_ARRVAL_P ( return_value ) , i , ( void * ) & result - > data [ result - > cur_row ] [ i ] , sizeof ( pval * ) , NULL ) ;
2000-06-30 16:24:17 +00:00
ZVAL_ADDREF ( result - > data [ result - > cur_row ] [ i ] ) ;
2001-09-25 21:58:48 +00:00
zend_hash_update ( Z_ARRVAL_P ( return_value ) , result - > fields [ i ] . name , strlen ( result - > fields [ i ] . name ) + 1 , ( void * ) & result - > data [ result - > cur_row ] [ i ] , sizeof ( pval * ) , NULL ) ;
1999-04-22 02:48:28 +00:00
}
result - > cur_row + + ;
}
2000-05-24 16:28:16 +00:00
/* {{{ proto object sybase_fetch_object(int result)
Fetch row as object */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_fetch_object )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
php_sybase_fetch_hash ( INTERNAL_FUNCTION_PARAM_PASSTHRU ) ;
2001-09-25 21:58:48 +00:00
if ( Z_TYPE_P ( return_value ) = = IS_ARRAY ) {
2004-12-21 06:57:01 +00:00
object_and_properties_init (
return_value ,
ZEND_STANDARD_CLASS_DEF_PTR ,
Z_ARRVAL_P ( return_value )
) ;
1999-04-22 02:48:28 +00:00
}
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto array sybase_fetch_array(int result)
Fetch row as array */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_fetch_array )
1999-04-22 02:48:28 +00:00
{
1999-12-18 03:20:53 +00:00
php_sybase_fetch_hash ( INTERNAL_FUNCTION_PARAM_PASSTHRU ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto bool sybase_data_seek(int result, int offset)
Move internal row pointer */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_data_seek )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_result_index , * * offset ;
1999-04-22 02:48:28 +00:00
int type , id ;
sybase_result * result ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & sybase_result_index , & offset ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_result_index ) ;
id = Z_LVAL_PP ( sybase_result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( offset ) ;
if ( Z_LVAL_PP ( offset ) < 0 | | Z_LVAL_PP ( offset ) > = result - > num_rows ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Bad row offset " ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
result - > cur_row = Z_LVAL_PP ( offset ) ;
1999-04-22 02:48:28 +00:00
RETURN_TRUE ;
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
1999-12-18 03:20:53 +00:00
static char * php_sybase_get_field_name ( int type )
1999-04-22 02:48:28 +00:00
{
switch ( type ) {
case SYBBINARY :
case SYBVARBINARY :
return " blob " ;
break ;
case SYBCHAR :
case SYBVARCHAR :
case SYBTEXT :
return " string " ;
case SYBDATETIME :
case SYBDATETIME4 :
case SYBDATETIMN :
return " datetime " ;
break ;
case SYBDECIMAL :
case SYBFLT8 :
case SYBFLTN :
case SYBREAL :
case SYBNUMERIC :
return " real " ;
break ;
case SYBINT1 :
case SYBINT2 :
case SYBINT4 :
case SYBINTN :
return " int " ;
break ;
case SYBMONEY :
case SYBMONEY4 :
case SYBMONEYN :
return " money " ;
break ;
case SYBBIT :
return " bit " ;
break ;
case SYBIMAGE :
return " image " ;
break ;
default :
return " unknown " ;
break ;
}
}
2000-05-24 16:28:16 +00:00
/* {{{ proto object sybase_fetch_field(int result [, int offset])
Get field information */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_fetch_field )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_result_index , * * offset ;
1999-04-22 02:48:28 +00:00
int type , id , field_offset ;
sybase_result * result ;
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-22 02:48:28 +00:00
case 1 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 1 , & sybase_result_index ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
field_offset = - 1 ;
break ;
case 2 :
2003-01-15 03:01:17 +00:00
if ( zend_get_parameters_ex ( 2 , & sybase_result_index , & offset ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( offset ) ;
field_offset = Z_LVAL_PP ( offset ) ;
1999-04-22 02:48:28 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_result_index ) ;
id = Z_LVAL_PP ( sybase_result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
if ( field_offset = = - 1 ) {
field_offset = result - > cur_field ;
result - > cur_field + + ;
}
if ( field_offset < 0 | | field_offset > = result - > num_fields ) {
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) = = 2 ) { /* field specified explicitly */
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Bad column offset " ) ;
1999-04-22 02:48:28 +00:00
}
RETURN_FALSE ;
}
2003-01-30 02:33:16 +00:00
object_init ( return_value ) ;
1999-04-22 02:48:28 +00:00
add_property_string ( return_value , " name " , result - > fields [ field_offset ] . name , 1 ) ;
add_property_long ( return_value , " max_length " , result - > fields [ field_offset ] . max_length ) ;
add_property_string ( return_value , " column_source " , result - > fields [ field_offset ] . column_source , 1 ) ;
add_property_long ( return_value , " numeric " , result - > fields [ field_offset ] . numeric ) ;
2001-09-26 08:35:48 +00:00
add_property_string ( return_value , " type " , php_sybase_get_field_name ( Z_TYPE ( result - > fields [ field_offset ] ) ) , 1 ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto bool sybase_field_seek(int result, int offset)
Set field offset */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_field_seek )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * sybase_result_index , * * offset ;
1999-04-22 02:48:28 +00:00
int type , id , field_offset ;
sybase_result * result ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & sybase_result_index , & offset ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_result_index ) ;
id = Z_LVAL_PP ( sybase_result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( offset ) ;
field_offset = Z_LVAL_PP ( offset ) ;
1999-04-22 02:48:28 +00:00
if ( field_offset < 0 | | field_offset > = result - > num_fields ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Bad column offset " ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
result - > cur_field = field_offset ;
RETURN_TRUE ;
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto string sybase_result(int result, int row, mixed field)
Get result data */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_result )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * row , * * field , * * sybase_result_index ;
1999-04-22 02:48:28 +00:00
int id , type , field_offset = 0 ;
sybase_result * result ;
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 3 | | zend_get_parameters_ex ( 3 , & sybase_result_index , & row , & field ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( sybase_result_index ) ;
id = Z_LVAL_PP ( sybase_result_index ) ;
1999-04-22 02:48:28 +00:00
1999-12-17 19:51:39 +00:00
result = ( sybase_result * ) zend_list_find ( id , & type ) ;
1999-12-18 03:20:53 +00:00
if ( type ! = php_sybase_module . le_result ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase result index " , id ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( row ) ;
if ( Z_LVAL_PP ( row ) < 0 | | Z_LVAL_PP ( row ) > = result - > num_rows ) {
2003-08-28 21:00:24 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Bad row offset (%ld) " , Z_LVAL_PP ( row ) ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
2003-01-15 03:01:17 +00:00
switch ( Z_TYPE_PP ( field ) ) {
1999-04-22 02:48:28 +00:00
case IS_STRING : {
int i ;
for ( i = 0 ; i < result - > num_fields ; i + + ) {
2003-08-04 14:34:54 +00:00
if ( ! strcasecmp ( result - > fields [ i ] . name , Z_STRVAL_PP ( field ) ) ) {
1999-04-22 02:48:28 +00:00
field_offset = i ;
break ;
}
}
if ( i > = result - > num_fields ) { /* no match found */
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: %s field not found in result " , Z_STRVAL_PP ( field ) ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
break ;
}
default :
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( field ) ;
field_offset = Z_LVAL_PP ( field ) ;
1999-04-22 02:48:28 +00:00
if ( field_offset < 0 | | field_offset > = result - > num_fields ) {
2003-01-15 03:01:17 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Sybase: Bad column offset specified " ) ;
1999-04-22 02:48:28 +00:00
RETURN_FALSE ;
}
break ;
}
2003-01-15 03:01:17 +00:00
* return_value = * result - > data [ Z_LVAL_PP ( row ) ] [ field_offset ] ;
1999-04-22 02:48:28 +00:00
pval_copy_constructor ( return_value ) ;
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2001-03-16 01:21:32 +00:00
/* {{{ proto int sybase_affected_rows([int link_id])
Get number of affected rows in last query */
PHP_FUNCTION ( sybase_affected_rows )
{
2003-08-04 14:34:54 +00:00
zval * * sybase_link_index = NULL ;
2003-01-15 03:01:17 +00:00
sybase_link * sybase_ptr = NULL ;
int id = 0 ;
int type = 0 ;
switch ( ZEND_NUM_ARGS ( ) ) {
case 0 :
id = php_sybase_module . default_link ;
break ;
case 1 :
2003-08-04 14:34:54 +00:00
if ( zend_get_parameters_ex ( 1 , & sybase_link_index ) = = FAILURE ) {
2003-01-15 03:01:17 +00:00
RETURN_FALSE ;
}
convert_to_long_ex ( sybase_link_index ) ;
id = Z_LVAL_PP ( sybase_link_index ) ;
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
2001-03-16 01:21:32 +00:00
2003-01-15 03:01:17 +00:00
sybase_ptr = ( sybase_link * ) zend_list_find ( id , & type ) ;
2001-03-16 01:21:32 +00:00
2003-01-15 03:01:17 +00:00
if ( type ! = php_sybase_module . le_link & & type ! = php_sybase_module . le_plink ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " %d is not a Sybase link index " , id ) ;
RETURN_FALSE ;
}
2001-03-16 01:21:32 +00:00
2003-01-15 03:01:17 +00:00
Z_LVAL_P ( return_value ) = DBCOUNT ( sybase_ptr - > link ) ;
Z_TYPE_P ( return_value ) = IS_LONG ;
2001-03-16 01:21:32 +00:00
}
1999-12-18 03:20:53 +00:00
PHP_MINFO_FUNCTION ( sybase )
1999-04-22 02:48:28 +00:00
{
2001-07-30 06:18:13 +00:00
char maxp [ 32 ] , maxl [ 32 ] ;
1999-04-22 02:48:28 +00:00
1999-12-18 03:20:53 +00:00
if ( php_sybase_module . max_persistent = = - 1 ) {
2000-10-19 16:10:41 +00:00
snprintf ( maxp , 31 , " %ld/unlimited " , php_sybase_module . num_persistent ) ;
1999-04-22 02:48:28 +00:00
} else {
2000-10-19 16:10:41 +00:00
snprintf ( maxp , 31 , " %ld/%ld " , php_sybase_module . num_persistent , php_sybase_module . max_persistent ) ;
1999-04-22 02:48:28 +00:00
}
2000-04-06 21:07:44 +00:00
maxp [ 31 ] = 0 ;
1999-12-18 03:20:53 +00:00
if ( php_sybase_module . max_links = = - 1 ) {
2000-10-19 16:10:41 +00:00
snprintf ( maxl , 31 , " %ld/unlimited " , php_sybase_module . num_links ) ;
1999-04-22 02:48:28 +00:00
} else {
2000-10-19 16:10:41 +00:00
snprintf ( maxl , 31 , " %ld/%ld " , php_sybase_module . num_links , php_sybase_module . max_links ) ;
2000-04-06 21:07:44 +00:00
}
maxl [ 31 ] = 0 ;
php_info_print_table_start ( ) ;
php_info_print_table_row ( 2 , " Sybase Support " , " enabled " ) ;
php_info_print_table_row ( 2 , " Allow Persistent Links " , ( php_sybase_module . allow_persistent ? " Yes " : " No " ) ) ;
php_info_print_table_row ( 2 , " Persistent Links " , maxp ) ;
php_info_print_table_row ( 2 , " Total Links " , maxl ) ;
php_info_print_table_row ( 2 , " Application Name " , php_sybase_module . appname ) ;
php_info_print_table_row ( 2 , " Client API Version " , dbversion ( ) ) ;
php_info_print_table_end ( ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* {{{ proto void sybase_min_error_severity(int severity)
Sets the minimum error severity */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_min_error_severity )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * severity ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & severity ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( severity ) ;
php_sybase_module . min_error_severity = Z_LVAL_PP ( severity ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
2000-05-24 16:28:16 +00:00
/* {{{ proto void sybase_min_message_severity(int severity)
Sets the minimum message severity */
1999-05-21 10:06:25 +00:00
PHP_FUNCTION ( sybase_min_message_severity )
1999-04-22 02:48:28 +00:00
{
2003-01-15 03:01:17 +00:00
zval * * severity ;
1999-04-22 02:48:28 +00:00
2003-01-15 03:01:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & severity ) = = FAILURE ) {
1999-04-22 02:48:28 +00:00
WRONG_PARAM_COUNT ;
}
2003-01-15 03:01:17 +00:00
convert_to_long_ex ( severity ) ;
php_sybase_module . min_message_severity = Z_LVAL_PP ( severity ) ;
1999-04-22 02:48:28 +00:00
}
2000-05-24 16:28:16 +00:00
/* }}} */
1999-04-22 02:48:28 +00:00
# endif