summaryrefslogtreecommitdiff
path: root/ext/sybase
diff options
context:
space:
mode:
authorStig Bakken <ssb@php.net>1999-04-22 02:48:28 +0000
committerStig Bakken <ssb@php.net>1999-04-22 02:48:28 +0000
commit2c0ad3ee25cd8e449b02c4668a39f48998507739 (patch)
tree8064c7ee59309ffed3d9c5b8fd5c329ede193561 /ext/sybase
parent2a78f92804ae6f3e32cd278afbb9aa8ec3d2f52d (diff)
downloadphp-git-2c0ad3ee25cd8e449b02c4668a39f48998507739.tar.gz
last bunch of extensions moving to ext/
fhttpd module taken out of functions, functions is ready to go. The only extensions I have tested are gd+freetype and odbc(solid). Please try compiling in your favourite extensions and let me know how it works.
Diffstat (limited to 'ext/sybase')
-rw-r--r--ext/sybase/Makefile.am6
-rw-r--r--ext/sybase/Makefile.in270
-rw-r--r--ext/sybase/config.h.stub6
-rw-r--r--ext/sybase/config.m473
-rw-r--r--ext/sybase/php3_sybase-ct.h123
-rw-r--r--ext/sybase/php3_sybase.h141
-rw-r--r--ext/sybase/setup.stub11
-rw-r--r--ext/sybase/sybase-ct.c1652
-rw-r--r--ext/sybase/sybase.c1258
9 files changed, 3540 insertions, 0 deletions
diff --git a/ext/sybase/Makefile.am b/ext/sybase/Makefile.am
new file mode 100644
index 0000000000..97c3e06f8f
--- /dev/null
+++ b/ext/sybase/Makefile.am
@@ -0,0 +1,6 @@
+# $Id$
+
+INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/libzend
+noinst_LIBRARIES=libphpext_sybase.a
+libphpext_sybase_a_SOURCES=sybase.c sybase-ct.c
+
diff --git a/ext/sybase/Makefile.in b/ext/sybase/Makefile.in
new file mode 100644
index 0000000000..224e6b570d
--- /dev/null
+++ b/ext/sybase/Makefile.in
@@ -0,0 +1,270 @@
+# Makefile.in generated automatically by automake 1.3 from Makefile.am
+
+# Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+# $Id$
+
+
+SHELL = /bin/sh
+
+srcdir = @srcdir@
+top_srcdir = @top_srcdir@
+VPATH = @srcdir@
+prefix = @prefix@
+exec_prefix = @exec_prefix@
+
+bindir = @bindir@
+sbindir = @sbindir@
+libexecdir = @libexecdir@
+datadir = @datadir@
+sysconfdir = @sysconfdir@
+sharedstatedir = @sharedstatedir@
+localstatedir = @localstatedir@
+libdir = @libdir@
+infodir = @infodir@
+mandir = @mandir@
+includedir = @includedir@
+oldincludedir = /usr/include
+
+DISTDIR =
+
+pkgdatadir = $(datadir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+
+top_builddir = ../..
+
+ACLOCAL = @ACLOCAL@
+AUTOCONF = @AUTOCONF@
+AUTOMAKE = @AUTOMAKE@
+AUTOHEADER = @AUTOHEADER@
+
+INSTALL = @INSTALL@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+transform = @program_transform_name@
+
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+APACHE_INCLUDE = @APACHE_INCLUDE@
+APACHE_TARGET = @APACHE_TARGET@
+APXS = @APXS@
+APXS_LDFLAGS = @APXS_LDFLAGS@
+BINNAME = @BINNAME@
+CC = @CC@
+CFLAGS = @CFLAGS@
+CFLAGS_SHLIB = @CFLAGS_SHLIB@
+DEBUG_CFLAGS = @DEBUG_CFLAGS@
+EXTINFO_DEPS = @EXTINFO_DEPS@
+EXTRA_LIBS = @EXTRA_LIBS@
+EXT_LIBS = @EXT_LIBS@
+EXT_SUBDIRS = @EXT_SUBDIRS@
+FHTTPD_LIB = @FHTTPD_LIB@
+FHTTPD_TARGET = @FHTTPD_TARGET@
+HSREGEX = @HSREGEX@
+INSTALL_IT = @INSTALL_IT@
+LDFLAGS_SHLIB = @LDFLAGS_SHLIB@
+LDFLAGS_SHLIB_EXPORT = @LDFLAGS_SHLIB_EXPORT@
+MAINT = @MAINT@
+MAKEINFO = @MAKEINFO@
+PACKAGE = @PACKAGE@
+PERL_PATH = @PERL_PATH@
+PHP_BUILD_DATE = @PHP_BUILD_DATE@
+PHP_DEBUG = @PHP_DEBUG@
+PHP_LIBS = @PHP_LIBS@
+PHP_VERSION = @PHP_VERSION@
+PROG_SENDMAIL = @PROG_SENDMAIL@
+RANLIB = @RANLIB@
+RDYNAMIC_LFLAGS = @RDYNAMIC_LFLAGS@
+REGEX_LIB = @REGEX_LIB@
+STRONGHOLD = @STRONGHOLD@
+VERSION = @VERSION@
+WARNING_LEVEL = @WARNING_LEVEL@
+YACC = @YACC@
+
+INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/libzend
+noinst_LIBRARIES=libphpext_sybase.a
+libphpext_sybase_a_SOURCES=sybase.c sybase-ct.c
+mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
+CONFIG_HEADER = ../../config.h
+CONFIG_CLEAN_FILES =
+LIBRARIES = $(noinst_LIBRARIES)
+
+
+DEFS = @DEFS@ -I. -I$(srcdir) -I../..
+CPPFLAGS = @CPPFLAGS@
+LDFLAGS = @LDFLAGS@
+LIBS = @LIBS@
+libphpext_sybase_a_LIBADD =
+libphpext_sybase_a_OBJECTS = sybase.o sybase-ct.o
+AR = ar
+COMPILE = $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
+LINK = $(CC) $(CFLAGS) $(LDFLAGS) -o $@
+DIST_COMMON = Makefile.am Makefile.in
+
+
+DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
+
+TAR = tar
+GZIP = --best
+SOURCES = $(libphpext_sybase_a_SOURCES)
+OBJECTS = $(libphpext_sybase_a_OBJECTS)
+
+all: Makefile $(LIBRARIES)
+
+.SUFFIXES:
+.SUFFIXES: .S .c .o .s
+$(srcdir)/Makefile.in: @MAINT@ Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4)
+ cd $(top_srcdir) && $(AUTOMAKE) --gnu --include-deps ext/sybase/Makefile
+
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+ cd $(top_builddir) \
+ && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
+
+
+mostlyclean-noinstLIBRARIES:
+
+clean-noinstLIBRARIES:
+ -test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES)
+
+distclean-noinstLIBRARIES:
+
+maintainer-clean-noinstLIBRARIES:
+
+.c.o:
+ $(COMPILE) -c $<
+
+.s.o:
+ $(COMPILE) -c $<
+
+.S.o:
+ $(COMPILE) -c $<
+
+mostlyclean-compile:
+ -rm -f *.o core *.core
+
+clean-compile:
+
+distclean-compile:
+ -rm -f *.tab.c
+
+maintainer-clean-compile:
+
+libphpext_sybase.a: $(libphpext_sybase_a_OBJECTS) $(libphpext_sybase_a_DEPENDENCIES)
+ -rm -f libphpext_sybase.a
+ $(AR) cru libphpext_sybase.a $(libphpext_sybase_a_OBJECTS) $(libphpext_sybase_a_LIBADD)
+ $(RANLIB) libphpext_sybase.a
+
+tags: TAGS
+
+ID: $(HEADERS) $(SOURCES) $(LISP)
+ here=`pwd` && cd $(srcdir) \
+ && mkid -f$$here/ID $(SOURCES) $(HEADERS) $(LISP)
+
+TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) $(LISP)
+ tags=; \
+ here=`pwd`; \
+ list='$(SOURCES) $(HEADERS)'; \
+ unique=`for i in $$list; do echo $$i; done | \
+ awk ' { files[$$0] = 1; } \
+ END { for (i in files) print i; }'`; \
+ test -z "$(ETAGS_ARGS)$$unique$(LISP)$$tags" \
+ || (cd $(srcdir) && etags $(ETAGS_ARGS) $$tags $$unique $(LISP) -o $$here/TAGS)
+
+mostlyclean-tags:
+
+clean-tags:
+
+distclean-tags:
+ -rm -f TAGS ID
+
+maintainer-clean-tags:
+
+distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
+
+subdir = ext/sybase
+
+distdir: $(DISTFILES)
+ @for file in $(DISTFILES); do \
+ d=$(srcdir); \
+ test -f $(distdir)/$$file \
+ || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
+ || cp -p $$d/$$file $(distdir)/$$file; \
+ done
+info:
+dvi:
+check: all
+ $(MAKE)
+installcheck:
+install-exec:
+ @$(NORMAL_INSTALL)
+
+install-data:
+ @$(NORMAL_INSTALL)
+
+install: install-exec install-data all
+ @:
+
+uninstall:
+
+install-strip:
+ $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' INSTALL_SCRIPT='$(INSTALL_PROGRAM)' install
+installdirs:
+
+
+mostlyclean-generic:
+ -test -z "$(MOSTLYCLEANFILES)" || rm -f $(MOSTLYCLEANFILES)
+
+clean-generic:
+ -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
+
+distclean-generic:
+ -rm -f Makefile $(DISTCLEANFILES)
+ -rm -f config.cache config.log stamp-h stamp-h[0-9]*
+ -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
+
+maintainer-clean-generic:
+ -test -z "$(MAINTAINERCLEANFILES)" || rm -f $(MAINTAINERCLEANFILES)
+ -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES)
+mostlyclean: mostlyclean-noinstLIBRARIES mostlyclean-compile \
+ mostlyclean-tags mostlyclean-generic
+
+clean: clean-noinstLIBRARIES clean-compile clean-tags clean-generic \
+ mostlyclean
+
+distclean: distclean-noinstLIBRARIES distclean-compile distclean-tags \
+ distclean-generic clean
+ -rm -f config.status
+
+maintainer-clean: maintainer-clean-noinstLIBRARIES \
+ maintainer-clean-compile maintainer-clean-tags \
+ maintainer-clean-generic distclean
+ @echo "This command is intended for maintainers to use;"
+ @echo "it deletes files that may require special tools to rebuild."
+
+.PHONY: mostlyclean-noinstLIBRARIES distclean-noinstLIBRARIES \
+clean-noinstLIBRARIES maintainer-clean-noinstLIBRARIES \
+mostlyclean-compile distclean-compile clean-compile \
+maintainer-clean-compile tags mostlyclean-tags distclean-tags \
+clean-tags maintainer-clean-tags distdir info dvi installcheck \
+install-exec install-data install uninstall all installdirs \
+mostlyclean-generic distclean-generic clean-generic \
+maintainer-clean-generic clean mostlyclean distclean maintainer-clean
+
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/ext/sybase/config.h.stub b/ext/sybase/config.h.stub
new file mode 100644
index 0000000000..15ce4496bc
--- /dev/null
+++ b/ext/sybase/config.h.stub
@@ -0,0 +1,6 @@
+/* Define if you have libdnet_stub (used for Sybase support) */
+#define HAVE_LIBDNET_STUB 0
+
+#define HAVE_SYBASE 0
+
+#define HAVE_SYBASE_CT 0
diff --git a/ext/sybase/config.m4 b/ext/sybase/config.m4
new file mode 100644
index 0000000000..34de48373b
--- /dev/null
+++ b/ext/sybase/config.m4
@@ -0,0 +1,73 @@
+dnl $Id$
+
+AC_MSG_CHECKING(for Sybase support)
+AC_ARG_WITH(sybase,
+[ --with-sybase[=DIR] Include Sybase-DB support. DIR is the Sybase home
+ directory, defaults to /home/sybase.],
+[
+ if test "$withval" != "no"; then
+ if test "$withval" = "yes"; then
+ SYBASE_INCDIR=/home/sybase/include
+ SYBASE_LIBDIR=/home/sybase/lib
+ else
+ SYBASE_INCDIR=$withval/include
+ SYBASE_LIBDIR=$withval/lib
+ fi
+ SYBASE_INCLUDE=-I$SYBASE_INCDIR
+ SYBASE_LFLAGS="-L$SYBASE_LIBDIR -L$SYBASE_LIBDIR"
+ SYBASE_LIBS=-lsybdb
+ AC_MSG_RESULT(yes)
+ PHP_EXTENSION(sybase)
+ AC_CHECK_LIB(dnet_stub, dnet_addr,
+ [ SYBASE_LIBS="$SYBASE_LIBS -ldnet_stub"
+ AC_DEFINE(HAVE_LIBDNET_STUB)
+ ])
+ AC_DEFINE(HAVE_SYBASE)
+ EXTRA_LIBS="$EXTRA_LIBS $SYBASE_LFLAGS $SYBASE_LIBS"
+ INCLUDES="$INCLUDES $SYBASE_INCLUDE"
+ else
+ AC_MSG_RESULT(no)
+ fi
+],[
+ AC_MSG_RESULT(no)
+])
+
+
+AC_MSG_CHECKING(for Sybase-CT support)
+AC_ARG_WITH(sybase-ct,
+[ --with-sybase-ct[=DIR] Include Sybase-CT support. DIR is the Sybase home
+ directory, defaults to /home/sybase.],
+[
+ if test "$withval" != "no"; then
+ AC_MSG_RESULT(yes)
+ if test -z "$SYBASE_LIBS"; then
+ PHP_EXTENSION(sybase)
+ fi
+ AC_DEFINE(HAVE_SYBASE_CT)
+ if test "$withval" = "yes"; then
+ SYBASE_CT_INCDIR=/home/sybase/include
+ SYBASE_CT_LIBDIR=/home/sybase/lib
+ else
+ SYBASE_CT_INCDIR=$withval/include
+ SYBASE_CT_LIBDIR=$withval/lib
+ fi
+ SYBASE_CT_INCLUDE=-I$SYBASE_CT_INCDIR
+ SYBASE_CT_LFLAGS="-L$SYBASE_CT_LIBDIR"
+ SYBASE_CT_LIBS="-lcs -lct -lcomn -lintl"
+ old_LDFLAGS=$LDFLAGS
+ LDFLAGS="$LDFLAGS -L$SYBASE_CT_LIBDIR"
+ AC_CHECK_LIB(tcl, netg_errstr,
+ [ SYBASE_CT_LIBS="$SYBASE_CT_LIBS -ltcl" ],
+ [ SYBASE_CT_LIBS="$SYBASE_CT_LIBS -lsybtcl" ],
+ [ $SYBASE_CT_LIBS ])
+ AC_CHECK_LIB(insck, insck__getVdate,
+ [ SYBASE_CT_LIBS="$SYBASE_CT_LIBS -linsck" ])
+ LDFLAGS=$old_LDFLAGS
+ EXTRA_LIBS="$EXTRA_LIBS $SYBASE_CT_LFLAGS $SYBASE_CT_LIBS"
+ INCLUDES="$INCLUDES $SYBASE_CT_INCLUDE"
+ else
+ AC_MSG_RESULT(no)
+ fi
+],[
+ AC_MSG_RESULT(no)
+])
diff --git a/ext/sybase/php3_sybase-ct.h b/ext/sybase/php3_sybase-ct.h
new file mode 100644
index 0000000000..1ce0a30f0c
--- /dev/null
+++ b/ext/sybase/php3_sybase-ct.h
@@ -0,0 +1,123 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Authors: Zeev Suraski <zeev@zend.com> |
+ +----------------------------------------------------------------------+
+ */
+
+
+/* $Id$ */
+
+#ifndef _PHP3_SYBASE_CT_H
+#define _PHP3_SYBASE_CT_H
+
+#if COMPILE_DL
+#undef HAVE_SYBASE_CT
+#define HAVE_SYBASE_CT 1
+#endif
+
+#if HAVE_SYBASE_CT
+
+#define CTLIB_VERSION CS_VERSION_100
+
+extern php3_module_entry sybct_module_entry;
+#define sybct_module_ptr &sybct_module_entry
+
+extern int php3_minit_sybct(INIT_FUNC_ARGS);
+extern int php3_rinit_sybct(INIT_FUNC_ARGS);
+extern int php3_mshutdown_sybct(SHUTDOWN_FUNC_ARGS);
+extern int php3_rshutdown_sybct(SHUTDOWN_FUNC_ARGS);
+extern void php3_info_sybct(void);
+extern void php3_sybct_connect(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_pconnect(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_close(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_select_db(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_query(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_free_result(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_get_last_message(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_num_rows(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_num_fields(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_fetch_row(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_fetch_array(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_fetch_object(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_data_seek(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_result(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_affected_rows(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_field_seek(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_min_client_severity(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_min_server_severity(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybct_fetch_field(INTERNAL_FUNCTION_PARAMETERS);
+
+
+#include <ctpublic.h>
+
+typedef struct {
+ long default_link;
+ long num_links,num_persistent;
+ long max_links,max_persistent;
+ long allow_persistent;
+ char *appname;
+ char *hostname;
+ char *server_message;
+ int le_link,le_plink,le_result;
+ long min_server_severity, min_client_severity;
+ long cfg_min_server_severity, cfg_min_client_severity;
+} sybct_module;
+
+typedef struct {
+ CS_CONNECTION *connection;
+ CS_COMMAND *cmd;
+ int valid;
+ int deadlock;
+ int dead;
+ long affected_rows;
+} sybct_link;
+
+#define SYBASE_ROWS_BLOCK 128
+
+typedef struct {
+ char *name,*column_source;
+ int max_length, numeric;
+ CS_INT type;
+} sybct_field;
+
+typedef struct {
+ pval **data;
+ sybct_field *fields;
+ sybct_link *sybct_ptr;
+ int cur_row,cur_field;
+ int num_rows,num_fields;
+} sybct_result;
+
+
+extern sybct_module php3_sybct_module;
+
+#else
+
+#define sybct_module_ptr NULL
+
+#endif
+
+#endif /* _PHP3_SYBASE_CT_H */
diff --git a/ext/sybase/php3_sybase.h b/ext/sybase/php3_sybase.h
new file mode 100644
index 0000000000..dbfcdd767a
--- /dev/null
+++ b/ext/sybase/php3_sybase.h
@@ -0,0 +1,141 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Authors: Zeev Suraski <zeev@zend.com> |
+ +----------------------------------------------------------------------+
+ */
+
+
+/* $Id$ */
+
+#ifndef _PHP3_SYBASE_H
+#define _PHP3_SYBASE_H
+
+#if COMPILE_DL
+#undef HAVE_SYBASE
+#define HAVE_SYBASE 1
+#endif
+
+#if HAVE_SYBASE
+
+extern php3_module_entry sybase_module_entry;
+#define sybase_module_ptr &sybase_module_entry
+
+extern int php3_minit_sybase(INIT_FUNC_ARGS);
+extern int php3_rinit_sybase(INIT_FUNC_ARGS);
+extern int php3_mshutdown_sybase(SHUTDOWN_FUNC_ARGS);
+extern int php3_rshutdown_sybase(SHUTDOWN_FUNC_ARGS);
+extern void php3_info_sybase(void);
+extern void php3_sybase_connect(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_pconnect(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_close(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_select_db(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_query(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_free_result(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_get_last_message(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_num_rows(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_num_fields(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_fetch_row(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_fetch_array(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_fetch_object(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_data_seek(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_result(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_seek(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_min_error_severity(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_min_message_severity(INTERNAL_FUNCTION_PARAMETERS);
+
+extern void php3_sybase_db_query(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_list_fields(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_fetch_lengths(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_fetch_field(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_seek(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_free_result(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_name(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_table(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_len(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_type(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sybase_field_flags(INTERNAL_FUNCTION_PARAMETERS);
+
+
+
+#include <sybfront.h>
+#include <sybdb.h>
+#include <syberror.h>
+
+#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;
+
+typedef struct {
+ long default_link;
+ long num_links,num_persistent;
+ long max_links,max_persistent;
+ long allow_persistent;
+ char *appname;
+ char *server_message;
+ int le_link,le_plink,le_result;
+ long min_error_severity,min_message_severity;
+ long cfg_min_error_severity,cfg_min_message_severity;
+ long compatability_mode;
+} sybase_module;
+
+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;
+
+
+extern sybase_module php3_sybase_module;
+
+#else
+
+#define sybase_module_ptr NULL
+
+#endif
+
+#endif /* _PHP3_SYBASE_H */
diff --git a/ext/sybase/setup.stub b/ext/sybase/setup.stub
new file mode 100644
index 0000000000..58e0b789c6
--- /dev/null
+++ b/ext/sybase/setup.stub
@@ -0,0 +1,11 @@
+# $Source$
+# $Id$
+
+define_option with-sybase 'Sybase support?' yesnodir \
+ 'no /home/sybase Sybase install' \
+' Whether to build PHP with Sybase support (DBLib only).\n
+ More info about Sybase can be found at http://www.sybase.com/.'
+
+define_option with-sybase-ct 'Sybase-CT support?' yesnodir \
+ 'no /home/sybase Sybase-CT install' \
+' Whether to build PHP with Sybase-CT support.'
diff --git a/ext/sybase/sybase-ct.c b/ext/sybase/sybase-ct.c
new file mode 100644
index 0000000000..7f45658d6b
--- /dev/null
+++ b/ext/sybase/sybase-ct.c
@@ -0,0 +1,1652 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Authors: Zeev Suraski <zeev@zend.com> |
+ | Tom May <tom@go2net.com> |
+ +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+
+#ifndef MSVC5
+#include "config.h"
+#endif
+#include "php.h"
+#include "php3_sybase-ct.h"
+#include "ext/standard/php3_standard.h"
+#include "php_globals.h"
+
+#if HAVE_SYBASE_CT
+
+
+
+function_entry sybct_functions[] = {
+ {"sybase_connect", php3_sybct_connect, NULL},
+ {"sybase_pconnect", php3_sybct_pconnect, NULL},
+ {"sybase_close", php3_sybct_close, NULL},
+ {"sybase_select_db", php3_sybct_select_db, NULL},
+ {"sybase_query", php3_sybct_query, NULL},
+ {"sybase_free_result", php3_sybct_free_result, NULL},
+ {"sybase_num_rows", php3_sybct_num_rows, NULL},
+ {"sybase_num_fields", php3_sybct_num_fields, NULL},
+ {"sybase_fetch_row", php3_sybct_fetch_row, NULL},
+ {"sybase_fetch_array", php3_sybct_fetch_array, NULL},
+ {"sybase_fetch_object", php3_sybct_fetch_object, NULL},
+ {"sybase_data_seek", php3_sybct_data_seek, NULL},
+ {"sybase_fetch_field", php3_sybct_fetch_field, NULL},
+ {"sybase_field_seek", php3_sybct_field_seek, NULL},
+ {"sybase_result", php3_sybct_result, NULL},
+ {"sybase_affected_rows",php3_sybct_affected_rows, NULL},
+ {"sybase_min_client_severity", php3_sybct_min_client_severity, NULL},
+ {"sybase_min_server_severity", php3_sybct_min_server_severity, NULL},
+ {"mssql_connect", php3_sybct_connect, NULL},
+ {"mssql_pconnect", php3_sybct_pconnect, NULL},
+ {"mssql_close", php3_sybct_close, NULL},
+ {"mssql_select_db", php3_sybct_select_db, NULL},
+ {"mssql_query", php3_sybct_query, NULL},
+ {"mssql_free_result", php3_sybct_free_result, NULL},
+ {"mssql_num_rows", php3_sybct_num_rows, NULL},
+ {"mssql_num_fields", php3_sybct_num_fields, NULL},
+ {"mssql_fetch_row", php3_sybct_fetch_row, NULL},
+ {"mssql_fetch_array", php3_sybct_fetch_array, NULL},
+ {"mssql_fetch_object", php3_sybct_fetch_object, NULL},
+ {"mssql_data_seek", php3_sybct_data_seek, NULL},
+ {"mssql_fetch_field", php3_sybct_fetch_field, NULL},
+ {"mssql_field_seek", php3_sybct_field_seek, NULL},
+ {"mssql_result", php3_sybct_result, NULL},
+ {"mssql_affected_rows", php3_sybct_affected_rows, NULL},
+ {"mssql_min_client_severity", php3_sybct_min_client_severity, NULL},
+ {"mssql_min_server_severity", php3_sybct_min_server_severity, NULL},
+ {NULL, NULL, NULL}
+};
+
+php3_module_entry sybct_module_entry = {
+ "Sybase SQL - CT", sybct_functions, php3_minit_sybct, php3_mshutdown_sybct, php3_rinit_sybct, php3_rshutdown_sybct, php3_info_sybct, STANDARD_MODULE_PROPERTIES
+};
+
+#if COMPILE_DL
+php3_module_entry *get_module() { return &sybct_module_entry; }
+#endif
+
+THREAD_LS sybct_module php3_sybct_module;
+THREAD_LS static CS_CONTEXT *context;
+THREAD_LS static HashTable *resource_list,*resource_plist;
+
+#define CHECK_LINK(link) { if (link==-1) { php3_error(E_WARNING,"Sybase: A link to the server could not be established"); RETURN_FALSE; } }
+
+
+static int _clean_invalid_results(list_entry *le)
+{
+ if (le->type == php3_sybct_module.le_result) {
+ sybct_link *sybct_ptr = ((sybct_result *) le->ptr)->sybct_ptr;
+
+ if (!sybct_ptr->valid) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+
+static void _free_sybct_result(sybct_result *result)
+{
+ int i,j;
+
+ if (result->data) {
+ for (i=0; i<result->num_rows; i++) {
+ for (j=0; j<result->num_fields; j++) {
+ pval_destructor(&result->data[i][j]);
+ }
+ 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);
+}
+
+
+static void _close_sybct_link(sybct_link *sybct_ptr)
+{
+ CS_INT con_status;
+
+ sybct_ptr->valid = 0;
+ _php3_hash_apply(resource_list,(int (*)(void *))_clean_invalid_results);
+
+ /* Non-persistent connections will always be connected or we wouldn't
+ * get here, but since we want to check the death status anyway
+ * we might as well double-check the connect status.
+ */
+ if (ct_con_props(sybct_ptr->connection, CS_GET, CS_CON_STATUS,
+ &con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to get connection status on close");
+ /* Assume the worst. */
+ con_status = CS_CONSTAT_CONNECTED | CS_CONSTAT_DEAD;
+ }
+ if (con_status & CS_CONSTAT_CONNECTED) {
+ if ((con_status & CS_CONSTAT_DEAD) || ct_close(sybct_ptr->connection, CS_UNUSED)!=CS_SUCCEED) {
+ ct_close(sybct_ptr->connection, CS_FORCE_CLOSE);
+ }
+ }
+
+ ct_con_drop(sybct_ptr->connection);
+ efree(sybct_ptr);
+ php3_sybct_module.num_links--;
+}
+
+
+static void _close_sybct_plink(sybct_link *sybct_ptr)
+{
+ CS_INT con_status;
+
+ /* Persistent connections may have been closed before a failed
+ * reopen attempt.
+ */
+ if (ct_con_props(sybct_ptr->connection, CS_GET, CS_CON_STATUS,
+ &con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to get connection status on close");
+ /* Assume the worst. */
+ con_status = CS_CONSTAT_CONNECTED | CS_CONSTAT_DEAD;
+ }
+ if (con_status & CS_CONSTAT_CONNECTED) {
+ if ((con_status & CS_CONSTAT_DEAD) || ct_close(sybct_ptr->connection, CS_UNUSED)!=CS_SUCCEED) {
+ ct_close(sybct_ptr->connection, CS_FORCE_CLOSE);
+ }
+ }
+
+ ct_con_drop(sybct_ptr->connection);
+ free(sybct_ptr);
+ php3_sybct_module.num_persistent--;
+ php3_sybct_module.num_links--;
+}
+
+
+static CS_RETCODE _client_message_handler(CS_CONTEXT *context, CS_CONNECTION *connection, CS_CLIENTMSG *errmsg)
+{
+ if (CS_SEVERITY(errmsg->msgnumber) >= php3_sybct_module.min_client_severity) {
+ php3_error(E_WARNING,"Sybase: Client message: %s (severity %d)",errmsg->msgstring, CS_SEVERITY(errmsg->msgnumber));
+ }
+
+ /* If this is a timeout message, return CS_FAIL to cancel the
+ * operation and mark the connection as dead.
+ */
+ if (CS_SEVERITY(errmsg->msgnumber) == CS_SV_RETRY_FAIL &&
+ CS_NUMBER(errmsg->msgnumber) == 63 &&
+ CS_ORIGIN(errmsg->msgnumber) == 2 &&
+ CS_LAYER(errmsg->msgnumber) == 1)
+ {
+ return CS_FAIL;
+ }
+
+ return CS_SUCCEED;
+}
+
+
+static CS_RETCODE _server_message_handler(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *srvmsg)
+{
+ if (srvmsg->severity >= php3_sybct_module.min_server_severity) {
+ php3_error(E_WARNING,"Sybase: Server message: %s (severity %d, procedure %s)",
+ srvmsg->text, srvmsg->severity, ((srvmsg->proclen>0) ? srvmsg->proc : "N/A"));
+ }
+
+ /* If this is a deadlock message, set the connection's deadlock flag
+ * so we will retry the request. Sorry about the bare constant here,
+ * but it's not defined anywhere and it's a "well-known" number.
+ */
+ if (srvmsg->msgnumber == 1205) {
+ sybct_link *sybct;
+
+ if (ct_con_props(connection, CS_GET, CS_USERDATA, &sybct, CS_SIZEOF(sybct), NULL)==CS_SUCCEED) {
+ sybct->deadlock = 1;
+ }
+ else {
+ /* oh well */
+ }
+ }
+
+ return CS_SUCCEED;
+}
+
+
+int php3_minit_sybct(INIT_FUNC_ARGS)
+{
+ long timeout;
+
+ if (cs_ctx_alloc(CTLIB_VERSION, &context)!=CS_SUCCEED || ct_init(context,CTLIB_VERSION)!=CS_SUCCEED) {
+ return FAILURE;
+ }
+ /* Initialize message handlers */
+
+ if (ct_callback(context, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)_server_message_handler)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to set server message handler");
+ }
+ if (ct_callback(context, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)_client_message_handler)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to set client message handler");
+ }
+
+ /* Set datetime conversion format to "Nov 3 1998 8:06PM".
+ * This is the default format for the ct-lib that comes with
+ * Sybase ASE 11.5.1 for Solaris, but the Linux libraries that
+ * come with 11.0.3.3 default to "03/11/98" which is singularly
+ * useless. This levels the playing field for all platforms.
+ */
+ {
+ CS_INT dt_convfmt = CS_DATES_SHORT;
+ if (cs_dt_info(context, CS_SET, NULL, CS_DT_CONVFMT, CS_UNUSED, &dt_convfmt, sizeof(dt_convfmt), NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to set datetime conversion format");
+ }
+ }
+
+ /* Set the login and command timeouts. These are per-context and
+ * can't be set with ct_con_props(), so set them globally from
+ * their config values if requested. The defaults are 1 minute
+ * for CS_LOGIN_TIMEOUT and CS_NO_LIMIT for CS_TIMEOUT. The latter
+ * especially is fairly useless for web applications.
+ * Note that depite some noise in the documentation about using
+ * signals to implement timeouts, they are actually implemented
+ * by using poll() or select() on Solaris and Linux.
+ */
+ if (cfg_get_long("sybct.login_timeout",&timeout)==SUCCESS) {
+ CS_INT cs_login_timeout = timeout;
+ if (ct_config(context, CS_SET, CS_LOGIN_TIMEOUT, &cs_login_timeout, CS_UNUSED, NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to set login timeoutt");
+ }
+ }
+ if (cfg_get_long("sybct.timeout",&timeout)==SUCCESS) {
+ CS_INT cs_timeout = timeout;
+ if (ct_config(context, CS_SET, CS_TIMEOUT, &cs_timeout, CS_UNUSED, NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to set timeout");
+ }
+ }
+
+ if (cfg_get_long("sybct.allow_persistent",&php3_sybct_module.allow_persistent)==FAILURE) {
+ php3_sybct_module.allow_persistent=1;
+ }
+ if (cfg_get_long("sybct.max_persistent",&php3_sybct_module.max_persistent)==FAILURE) {
+ php3_sybct_module.max_persistent=-1;
+ }
+ if (cfg_get_long("sybct.max_links",&php3_sybct_module.max_links)==FAILURE) {
+ php3_sybct_module.max_links=-1;
+ }
+ if (cfg_get_long("sybct.min_server_severity",&php3_sybct_module.cfg_min_server_severity)==FAILURE) {
+ php3_sybct_module.cfg_min_server_severity=10;
+ }
+ if (cfg_get_long("sybct.min_client_severity",&php3_sybct_module.cfg_min_client_severity)==FAILURE) {
+ php3_sybct_module.cfg_min_client_severity=10;
+ }
+
+ if (cfg_get_string("sybct.hostname",&php3_sybct_module.hostname)==FAILURE
+ || php3_sybct_module.hostname[0]==0) {
+ php3_sybct_module.hostname=NULL;
+ }
+
+ php3_sybct_module.num_persistent=0;
+ php3_sybct_module.le_link = register_list_destructors(_close_sybct_link,NULL);
+ php3_sybct_module.le_plink = register_list_destructors(NULL,_close_sybct_plink);
+ php3_sybct_module.le_result = register_list_destructors(_free_sybct_result,NULL);
+
+ return SUCCESS;
+}
+
+
+int php3_rinit_sybct(INIT_FUNC_ARGS)
+{
+ php3_sybct_module.default_link=-1;
+ php3_sybct_module.num_links = php3_sybct_module.num_persistent;
+ php3_sybct_module.appname = estrndup("PHP 3.0",7);
+ php3_sybct_module.server_message = NULL;
+ php3_sybct_module.min_server_severity = php3_sybct_module.cfg_min_server_severity;
+ php3_sybct_module.min_client_severity = php3_sybct_module.cfg_min_client_severity;
+ return SUCCESS;
+}
+
+int php3_mshutdown_sybct(SHUTDOWN_FUNC_ARGS)
+{
+/* ct_exit(context,CS_UNUSED);
+ cs_ctx_drop(context); */
+ return SUCCESS;
+}
+
+int php3_rshutdown_sybct(SHUTDOWN_FUNC_ARGS)
+{
+ efree(php3_sybct_module.appname);
+ if (php3_sybct_module.server_message) {
+ efree(php3_sybct_module.server_message);
+ }
+ return SUCCESS;
+}
+
+
+static int _php3_sybct_really_connect(sybct_link *sybct, char *host, char *user, char *passwd)
+{
+ /* set a CS_CONNECTION record */
+ if (ct_con_alloc(context, &sybct->connection)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to allocate connection record");
+ return 0;
+ }
+
+ /* Note - this saves a copy of sybct, not a pointer to it. */
+ if (ct_con_props(sybct->connection, CS_SET, CS_USERDATA, &sybct, CS_SIZEOF(sybct), NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to set userdata");
+ ct_con_drop(sybct->connection);
+ return 0;
+ }
+
+ if (user) {
+ ct_con_props(sybct->connection, CS_SET, CS_USERNAME, user, CS_NULLTERM, NULL);
+ }
+ if (passwd) {
+ ct_con_props(sybct->connection, CS_SET, CS_PASSWORD, passwd, CS_NULLTERM, NULL);
+ }
+ ct_con_props(sybct->connection, CS_SET, CS_APPNAME, php3_sybct_module.appname, CS_NULLTERM, NULL);
+
+ if (php3_sybct_module.hostname) {
+ ct_con_props(sybct->connection, CS_SET, CS_HOSTNAME, php3_sybct_module.hostname, CS_NULLTERM, NULL);
+ }
+
+ sybct->valid = 1;
+ sybct->dead = 0;
+
+ /* create the link */
+ if (ct_connect(sybct->connection, host, CS_NULLTERM)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to connect");
+ ct_con_drop(sybct->connection);
+ return 0;
+ }
+
+ if (ct_cmd_alloc(sybct->connection,&sybct->cmd)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to allocate command record");
+ ct_close(sybct->connection,CS_UNUSED);
+ ct_con_drop(sybct->connection);
+ return 0;
+ }
+
+ return 1;
+}
+
+
+static void php3_sybct_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent)
+{
+ char *user,*passwd,*host;
+ char *hashed_details;
+ int hashed_details_length;
+ sybct_link *sybct_ptr;
+
+ resource_list = list;
+ resource_plist = plist;
+
+ switch(ARG_COUNT(ht)) {
+ case 0: /* defaults */
+ host=user=passwd=NULL;
+ hashed_details_length=5+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ strcpy(hashed_details,"sybct___");
+ break;
+ case 1: {
+ pval *yyhost;
+
+ if (getParameters(ht, 1, &yyhost)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_string(yyhost);
+ host = yyhost->value.str.val;
+ user=passwd=NULL;
+ hashed_details_length = yyhost->value.str.len+5+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ sprintf(hashed_details,"sybct_%s__",yyhost->value.str.val);
+ }
+ break;
+ case 2: {
+ pval *yyhost,*yyuser;
+
+ if (getParameters(ht, 2, &yyhost, &yyuser)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_string(yyhost);
+ convert_to_string(yyuser);
+ host = yyhost->value.str.val;
+ user = yyuser->value.str.val;
+ passwd=NULL;
+ hashed_details_length = yyhost->value.str.len+yyuser->value.str.len+5+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ sprintf(hashed_details,"sybct_%s_%s_",yyhost->value.str.val,yyuser->value.str.val);
+ }
+ break;
+ case 3: {
+ pval *yyhost,*yyuser,*yypasswd;
+
+ if (getParameters(ht, 3, &yyhost, &yyuser, &yypasswd) == FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_string(yyhost);
+ convert_to_string(yyuser);
+ convert_to_string(yypasswd);
+ host = yyhost->value.str.val;
+ user = yyuser->value.str.val;
+ passwd = yypasswd->value.str.val;
+ hashed_details_length = yyhost->value.str.len+yyuser->value.str.len+yypasswd->value.str.len+5+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ sprintf(hashed_details,"sybct_%s_%s_%s",yyhost->value.str.val,yyuser->value.str.val,yypasswd->value.str.val); /* SAFE */
+ }
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+
+ if (!php3_sybct_module.allow_persistent) {
+ persistent=0;
+ }
+ if (persistent) {
+ list_entry *le;
+
+ /* try to find if we already have this link in our persistent list */
+ if (_php3_hash_find(plist, hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
+ list_entry new_le;
+
+ if (php3_sybct_module.max_links!=-1 && php3_sybct_module.num_links>=php3_sybct_module.max_links) {
+ php3_error(E_WARNING,"Sybase: Too many open links (%d)",php3_sybct_module.num_links);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ if (php3_sybct_module.max_persistent!=-1 && php3_sybct_module.num_persistent>=php3_sybct_module.max_persistent) {
+ php3_error(E_WARNING,"Sybase: Too many open persistent links (%d)",php3_sybct_module.num_persistent);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ sybct_ptr = (sybct_link *) malloc(sizeof(sybct_link));
+ if (!_php3_sybct_really_connect(sybct_ptr, host, user, passwd)) {
+ free(sybct_ptr);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ /* hash it up */
+ new_le.type = php3_sybct_module.le_plink;
+ new_le.ptr = sybct_ptr;
+ if (_php3_hash_update(plist, hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry),NULL)==FAILURE) {
+ ct_close(sybct_ptr->connection, CS_UNUSED);
+ ct_con_drop(sybct_ptr->connection);
+ free(sybct_ptr);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ php3_sybct_module.num_persistent++;
+ php3_sybct_module.num_links++;
+ } else { /* we do */
+ CS_INT con_status;
+
+ if (le->type != php3_sybct_module.le_plink) {
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ sybct_ptr = (sybct_link *) le->ptr;
+
+ /* If the link has died, close it and overwrite it with a new one. */
+
+ if (ct_con_props(sybct_ptr->connection, CS_GET, CS_CON_STATUS,
+ &con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
+ php3_error(E_WARNING,"Sybase: Unable to get connection status");
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ if (!(con_status & CS_CONSTAT_CONNECTED) || (con_status & CS_CONSTAT_DEAD) || sybct_ptr->dead) {
+ sybct_link sybct;
+
+ if (con_status & CS_CONSTAT_CONNECTED) {
+ ct_close(sybct_ptr->connection, CS_FORCE_CLOSE);
+ }
+ /* Create a new connection, then replace the old
+ * connection. If we fail to create a new connection,
+ * put the old one back so there will be a connection,
+ * even if it is a non-functional one. This is because
+ * code may still be holding an id for this connection
+ * so we can't free the CS_CONNECTION.
+ * (This is actually totally hokey, it would be better
+ * to just ct_con_drop() the connection and set
+ * sybct_ptr->connection to NULL, then test it for
+ * NULL before trying to use it elsewhere . . .)
+ */
+ memcpy(&sybct,sybct_ptr,sizeof(sybct_link));
+ if (!_php3_sybct_really_connect(sybct_ptr, host, user, passwd)) {
+ memcpy(sybct_ptr,&sybct,sizeof(sybct_link));
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ ct_con_drop(sybct.connection); /* drop old connection */
+ }
+ }
+ return_value->value.lval = php3_list_insert(sybct_ptr,php3_sybct_module.le_plink);
+ return_value->type = IS_LONG;
+ } 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 sybct link sits.
+ * if it doesn't, open a new sybct link, add it to the resource list,
+ * and add a pointer to it with hashed_details as the key.
+ */
+ if (_php3_hash_find(list,hashed_details,hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
+ int type,link;
+ void *ptr;
+
+ if (index_ptr->type != le_index_ptr) {
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ link = (int) index_ptr->ptr;
+ ptr = php3_list_find(link,&type); /* check if the link is still there */
+ if (ptr && (type==php3_sybct_module.le_link || type==php3_sybct_module.le_plink)) {
+ return_value->value.lval = php3_sybct_module.default_link = link;
+ return_value->type = IS_LONG;
+ efree(hashed_details);
+ return;
+ } else {
+ _php3_hash_del(list,hashed_details,hashed_details_length+1);
+ }
+ }
+ if (php3_sybct_module.max_links!=-1 && php3_sybct_module.num_links>=php3_sybct_module.max_links) {
+ php3_error(E_WARNING,"Sybase: Too many open links (%d)",php3_sybct_module.num_links);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ sybct_ptr = (sybct_link *) emalloc(sizeof(sybct_link));
+ if (!_php3_sybct_really_connect(sybct_ptr, host, user, passwd)) {
+ efree(sybct_ptr);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ /* add it to the list */
+ return_value->value.lval = php3_list_insert(sybct_ptr,php3_sybct_module.le_link);
+ return_value->type = IS_LONG;
+
+ /* add it to the hash */
+ new_index_ptr.ptr = (void *) return_value->value.lval;
+ new_index_ptr.type = le_index_ptr;
+ if (_php3_hash_update(list,hashed_details,hashed_details_length+1,(void *) &new_index_ptr, sizeof(list_entry),NULL)==FAILURE) {
+ ct_close(sybct_ptr->connection, CS_UNUSED);
+ ct_con_drop(sybct_ptr->connection);
+ efree(sybct_ptr);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ php3_sybct_module.num_links++;
+ }
+ efree(hashed_details);
+ php3_sybct_module.default_link=return_value->value.lval;
+}
+
+
+static int php3_sybct_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
+{
+ if (php3_sybct_module.default_link==-1) { /* no link opened yet, implicitly open one */
+ HashTable dummy;
+
+ _php3_hash_init(&dummy,0,NULL,NULL,0);
+ php3_sybct_do_connect(&dummy,return_value,list,plist,0);
+ _php3_hash_destroy(&dummy);
+ }
+ return php3_sybct_module.default_link;
+}
+
+
+void php3_sybct_connect(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybct_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
+}
+
+void php3_sybct_pconnect(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybct_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
+}
+
+
+void php3_sybct_close(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_link_index;
+ int id,type;
+
+ switch (ARG_COUNT(ht)) {
+ case 0:
+ id = php3_sybct_module.default_link;
+ break;
+ case 1:
+ if (getParameters(ht, 1, &sybct_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybct_link_index);
+ id = sybct_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_link && type!=php3_sybct_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ php3_list_delete(id);
+ RETURN_TRUE;
+}
+
+
+static int exec_cmd(sybct_link *sybct_ptr,char *cmdbuf)
+{
+ CS_RETCODE retcode;
+ CS_INT restype;
+ int failure=0;
+
+ /* Fail if we already marked this connection dead. */
+
+ if (sybct_ptr->dead) {
+ return FAILURE;
+ }
+
+ /*
+ ** Get a command handle, store the command string in it, and
+ ** send it to the server.
+ */
+
+ if (ct_command(sybct_ptr->cmd, CS_LANG_CMD, cmdbuf, CS_NULLTERM, CS_UNUSED)!=CS_SUCCEED) {
+ sybct_ptr->dead = 1;
+ return FAILURE;
+ }
+ if (ct_send(sybct_ptr->cmd)!=CS_SUCCEED) {
+ sybct_ptr->dead = 1;
+ return FAILURE;
+ }
+
+ while ((retcode = ct_results(sybct_ptr->cmd, &restype))==CS_SUCCEED) {
+ switch ((int) restype) {
+ case CS_CMD_SUCCEED:
+ case CS_CMD_DONE:
+ break;
+
+ case CS_CMD_FAIL:
+ failure=1;
+ break;
+
+ case CS_STATUS_RESULT:
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_CURRENT);
+ break;
+
+ default:
+ failure=1;
+ break;
+ }
+ if (failure) {
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ return FAILURE;
+ }
+ }
+
+ switch (retcode) {
+ case CS_END_RESULTS:
+ return SUCCESS;
+ break;
+
+ case CS_FAIL:
+ /* Hopefully this either cleans up the connection, or the
+ * connection ends up marked dead so it will be reopened
+ * if it is persistent. We may want to do
+ * ct_close(CS_FORCE_CLOSE) if ct_cancel() fails; see the
+ * doc for ct_results()==CS_FAIL.
+ */
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ /* Don't take chances with the vagaries of ct-lib. Mark it
+ * dead ourselves.
+ */
+ sybct_ptr->dead = 1;
+ return FAILURE;
+
+ default:
+ return FAILURE;
+ }
+}
+
+
+void php3_sybct_select_db(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *db,*sybct_link_index;
+ int id,type;
+ char *cmdbuf;
+ sybct_link *sybct_ptr;
+
+ switch(ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &db)==FAILURE) {
+ RETURN_FALSE;
+ }
+ id = php3_sybct_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ break;
+ case 2:
+ if (getParameters(ht, 2, &db, &sybct_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybct_link_index);
+ id = sybct_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ CHECK_LINK(id);
+
+ sybct_ptr = (sybct_link *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_link && type!=php3_sybct_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_string(db);
+ cmdbuf = (char *) emalloc(sizeof("use ")+db->value.str.len+1);
+ sprintf(cmdbuf,"use %s",db->value.str.val); /* SAFE */
+
+ if (exec_cmd(sybct_ptr,cmdbuf)==FAILURE) {
+ efree(cmdbuf);
+ RETURN_FALSE;
+ } else {
+ efree(cmdbuf);
+ RETURN_TRUE;
+ }
+}
+
+
+static sybct_result * _php3_sybct_fetch_result_set (sybct_link *sybct_ptr)
+{
+ int num_fields;
+ sybct_result *result;
+ char **tmp_buffer;
+ CS_INT *lengths;
+ CS_SMALLINT *indicators;
+ unsigned char *numerics;
+ CS_INT *types;
+ CS_DATAFMT *datafmt;
+ int i,j,retcode;
+ int blocks_initialized=1;
+
+ /* 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
+ */
+
+ if (ct_res_info(sybct_ptr->cmd, CS_NUMDATA, &num_fields, CS_UNUSED, NULL)!=CS_SUCCEED) {
+ return NULL;
+ }
+
+ result = (sybct_result *) emalloc(sizeof(sybct_result));
+ result->data = (pval **) emalloc(sizeof(pval *)*SYBASE_ROWS_BLOCK);
+ result->fields = NULL;
+ result->sybct_ptr = sybct_ptr;
+ result->cur_field=result->cur_row=result->num_rows=0;
+ result->num_fields = num_fields;
+
+ tmp_buffer = (char **) emalloc(sizeof(char *)*num_fields);
+ lengths = (CS_INT *) emalloc(sizeof(CS_INT)*num_fields);
+ indicators = (CS_SMALLINT *) emalloc(sizeof(CS_INT)*num_fields);
+ datafmt = (CS_DATAFMT *) emalloc(sizeof(CS_DATAFMT)*num_fields);
+ numerics = (unsigned char *) emalloc(sizeof(unsigned char)*num_fields);
+ types = (CS_INT *) emalloc(sizeof(CS_INT)*num_fields);
+
+
+ for (i=0; i<num_fields; i++) {
+ ct_describe(sybct_ptr->cmd,i+1,&datafmt[i]);
+ types[i] = datafmt[i].datatype;
+ switch (datafmt[i].datatype) {
+ case CS_CHAR_TYPE:
+ case CS_VARCHAR_TYPE:
+ case CS_TEXT_TYPE:
+ case CS_IMAGE_TYPE:
+ datafmt[i].maxlength++;
+ numerics[i] = 0;
+ break;
+ case CS_BINARY_TYPE:
+ case CS_VARBINARY_TYPE:
+ datafmt[i].maxlength *= 2;
+ datafmt[i].maxlength++;
+ numerics[i] = 0;
+ break;
+ case CS_BIT_TYPE:
+ case CS_TINYINT_TYPE:
+ datafmt[i].maxlength = 4;
+ numerics[i] = 1;
+ break;
+ case CS_SMALLINT_TYPE:
+ datafmt[i].maxlength = 7;
+ numerics[i] = 1;
+ break;
+ case CS_INT_TYPE:
+ datafmt[i].maxlength = 12;
+ numerics[i] = 1;
+ break;
+ case CS_REAL_TYPE:
+ case CS_FLOAT_TYPE:
+ datafmt[i].maxlength = 24;
+ numerics[i] = 1;
+ break;
+ case CS_MONEY_TYPE:
+ case CS_MONEY4_TYPE:
+ datafmt[i].maxlength = 24;
+ numerics[i] = 0;
+ break;
+ case CS_DATETIME_TYPE:
+ case CS_DATETIME4_TYPE:
+ datafmt[i].maxlength = 30;
+ numerics[i] = 0;
+ break;
+ case CS_NUMERIC_TYPE:
+ case CS_DECIMAL_TYPE:
+ datafmt[i].maxlength = datafmt[i].precision + 3;
+ numerics[i] = 1;
+ break;
+ default:
+ datafmt[i].maxlength++;
+ numerics[i] = 0;
+ break;
+ }
+ tmp_buffer[i] = (char *)emalloc(datafmt[i].maxlength);
+ datafmt[i].datatype = CS_CHAR_TYPE;
+ datafmt[i].format = CS_FMT_NULLTERM;
+ ct_bind(sybct_ptr->cmd,i+1,&datafmt[i],tmp_buffer[i],&lengths[i],&indicators[i]);
+ }
+
+ while ((retcode=ct_fetch(sybct_ptr->cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,NULL))==CS_SUCCEED
+ || retcode==CS_ROW_FAIL) {
+ /*
+ if (retcode==CS_ROW_FAIL) {
+ php3_error(E_WARNING,"Sybase: Error reading row %d",result->num_rows);
+ }
+ */
+ i = result->num_rows++;
+ if (result->num_rows > blocks_initialized*SYBASE_ROWS_BLOCK) {
+ result->data = (pval **) erealloc(result->data,sizeof(pval *)*SYBASE_ROWS_BLOCK*(++blocks_initialized));
+ }
+ result->data[i] = (pval *) emalloc(sizeof(pval)*num_fields);
+ for (j=0; j<num_fields; j++) {
+ if (indicators[j] == -1) { /* null value */
+ var_reset(&result->data[i][j]);
+ } else {
+ result->data[i][j].value.str.len = lengths[j]-1; /* we don't need the NULL in the length */
+ result->data[i][j].value.str.val = estrndup(tmp_buffer[j],lengths[j]);
+ result->data[i][j].type = IS_STRING;
+ }
+ }
+ }
+
+ if (retcode != CS_END_DATA) {
+ _free_sybct_result(result);
+ result = NULL;
+ } else {
+ result->fields = (sybct_field *) emalloc(sizeof(sybct_field)*num_fields);
+ j=0;
+ for (i=0; i<num_fields; i++) {
+ char computed_buf[16];
+
+ if (datafmt[i].namelen>0) {
+ result->fields[i].name = estrndup(datafmt[i].name,datafmt[i].namelen);
+ } 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].column_source = empty_string;
+ result->fields[i].max_length = datafmt[i].maxlength-1;
+ result->fields[i].numeric = numerics[i];
+ result->fields[i].type = types[i];
+ }
+ }
+
+ efree(datafmt);
+ efree(lengths);
+ efree(indicators);
+ efree(numerics);
+ efree(types);
+ for (i=0; i<num_fields; i++) {
+ efree(tmp_buffer[i]);
+ }
+ efree(tmp_buffer);
+
+ return result;
+}
+
+
+void php3_sybct_query(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *query,*sybct_link_index;
+ int id,type;
+ sybct_link *sybct_ptr;
+ sybct_result *result;
+ CS_INT restype;
+ CS_RETCODE retcode;
+ enum {
+ Q_RESULT, /* Success with results. */
+ Q_SUCCESS, /* Success but no results. */
+ Q_FAILURE, /* Failure, no results. */
+ } status;
+
+ switch(ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &query)==FAILURE) {
+ RETURN_FALSE;
+ }
+ id = php3_sybct_module.default_link;
+ break;
+ case 2:
+ if (getParameters(ht, 2, &query, &sybct_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybct_link_index);
+ id = sybct_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ sybct_ptr = (sybct_link *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_link && type!=php3_sybct_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_string(query);
+
+ /* Fail if we already marked this connection dead. */
+
+ if (sybct_ptr->dead) {
+ RETURN_FALSE;
+ }
+
+ /* Repeat until we don't deadlock. */
+
+ for (;;) {
+ result = NULL;
+ sybct_ptr->deadlock = 0;
+ sybct_ptr->affected_rows = 0;
+
+ /* On Solaris 11.5, ct_command() can be moved outside the
+ * loop, but not on Linux 11.0.
+ */
+ if (ct_command(sybct_ptr->cmd, CS_LANG_CMD, query->value.str.val, CS_NULLTERM, CS_UNUSED)!=CS_SUCCEED) {
+ /* If this didn't work, the connection is screwed but
+ * ct-lib might not set CS_CONSTAT_DEAD. So set our own
+ * flag. This happens sometimes when the database is restarted
+ * and/or its machine is rebooted, and ct_command() returns
+ * CS_BUSY for some reason.
+ */
+ sybct_ptr->dead = 1;
+ RETURN_FALSE;
+ }
+
+ if (ct_send(sybct_ptr->cmd)!=CS_SUCCEED) {
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ sybct_ptr->dead = 1;
+ RETURN_FALSE;
+ }
+
+ /* Use the first result set or succeed/fail status and discard the
+ * others. Applications really shouldn't be making calls that
+ * return multiple result sets, but if they do then we need to
+ * properly read or cancel them or the connection will become
+ * unusable.
+ */
+ if (ct_results(sybct_ptr->cmd, &restype)!=CS_SUCCEED) {
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ sybct_ptr->dead = 1;
+ RETURN_FALSE;
+ }
+
+ switch ((int) restype) {
+ case CS_CMD_FAIL:
+ default:
+ status = Q_FAILURE;
+ break;
+ case CS_CMD_SUCCEED:
+ case CS_CMD_DONE: {
+ CS_INT row_count;
+ if (ct_res_info(sybct_ptr->cmd, CS_ROW_COUNT, &row_count, CS_UNUSED, NULL)==CS_SUCCEED) {
+ sybct_ptr->affected_rows = (long)row_count;
+ }
+ }
+ /* Fall through */
+ case CS_COMPUTEFMT_RESULT:
+ case CS_ROWFMT_RESULT:
+ case CS_DESCRIBE_RESULT:
+ case CS_MSG_RESULT:
+ status = Q_SUCCESS;
+ break;
+ case CS_COMPUTE_RESULT:
+ case CS_CURSOR_RESULT:
+ case CS_PARAM_RESULT:
+ case CS_ROW_RESULT:
+ case CS_STATUS_RESULT:
+ result = _php3_sybct_fetch_result_set(sybct_ptr);
+ if (result == NULL) {
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ sybct_ptr->dead = 1;
+ RETURN_FALSE;
+ }
+ status = Q_RESULT;
+ break;
+ }
+
+ /* The only restype we should get now is CS_CMD_DONE, possibly
+ * followed by a CS_STATUS_RESULT/CS_CMD_SUCCEED/CS_CMD_DONE
+ * sequence if the command was a stored procedure call. But we
+ * still need to read and discard unexpected results. We might
+ * want to return a failure in this case because the application
+ * won't be getting all the results it asked for.
+ */
+ while ((retcode = ct_results(sybct_ptr->cmd, &restype))==CS_SUCCEED) {
+ switch ((int) restype) {
+ case CS_CMD_SUCCEED:
+ case CS_CMD_DONE:
+ break;
+
+ case CS_CMD_FAIL:
+ status = Q_FAILURE;
+ break;
+
+ case CS_COMPUTE_RESULT:
+ case CS_CURSOR_RESULT:
+ case CS_PARAM_RESULT:
+ case CS_ROW_RESULT:
+ /* Unexpected results, cancel them. */
+ case CS_STATUS_RESULT:
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_CURRENT);
+ break;
+
+ default:
+ status = Q_FAILURE;
+ break;
+ }
+ if (status == Q_FAILURE) {
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ }
+ }
+
+ switch (retcode) {
+ case CS_END_RESULTS:
+ /* Normal. */
+ break;
+
+ case CS_FAIL:
+ /* Hopefully this either cleans up the connection, or the
+ * connection ends up marked dead so it will be reopened
+ * if it is persistent. We may want to do
+ * ct_close(CS_FORCE_CLOSE) if ct_cancel() fails; see the
+ * doc for ct_results()==CS_FAIL.
+ */
+ ct_cancel(NULL, sybct_ptr->cmd, CS_CANCEL_ALL);
+ /* Don't take chances with the vagaries of ct-lib. Mark it
+ * dead ourselves.
+ */
+ sybct_ptr->dead = 1;
+ case CS_CANCELED:
+ default:
+ status = Q_FAILURE;
+ break;
+ }
+
+ /* If query completed without deadlock, break out of the loop.
+ * Sometimes deadlock results in failures and sometimes not,
+ * it seems to depend on the server flavor. But we want to
+ * retry all deadlocks.
+ */
+ if (sybct_ptr->dead || sybct_ptr->deadlock == 0) {
+ break;
+ }
+
+ /* Get rid of any results we may have fetched. This happens:
+ * e.g., our result set may be a stored procedure status which
+ * is returned even if the stored procedure deadlocks. As an
+ * optimization, we could try not to fetch results in known
+ * deadlock conditions, but deadlock is (should be) rare.
+ */
+ if (result != NULL) {
+ _free_sybct_result(result);
+ }
+ }
+
+ if (status == Q_SUCCESS) {
+ RETURN_TRUE;
+ }
+
+ if (status == Q_FAILURE) {
+ if (result != NULL) {
+ _free_sybct_result(result);
+ }
+ RETURN_FALSE;
+ }
+
+ return_value->value.lval = php3_list_insert(result,php3_sybct_module.le_result);
+ return_value->type = IS_LONG;
+}
+
+
+void php3_sybct_free_result(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_result_index;
+ sybct_result *result;
+ int type;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &sybct_result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybct_result_index);
+ if (sybct_result_index->value.lval==0) {
+ RETURN_FALSE;
+ }
+ result = (sybct_result *) php3_list_find(sybct_result_index->value.lval,&type);
+
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",sybct_result_index->value.lval);
+ RETURN_FALSE;
+ }
+ php3_list_delete(sybct_result_index->value.lval);
+ RETURN_TRUE;
+}
+
+
+#if 0
+void php3_sybct_get_last_message(INTERNAL_FUNCTION_PARAMETERS)
+{
+ if (php3_sybct_module.server_message) {
+ RETURN_STRING(php3_sybct_module.server_message,1);
+ }
+}
+#endif
+
+
+void php3_sybct_num_rows(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *result_index;
+ int type,id;
+ sybct_result *result;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(result_index);
+ id = result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ return_value->value.lval = result->num_rows;
+ return_value->type = IS_LONG;
+}
+
+
+void php3_sybct_num_fields(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *result_index;
+ int type,id;
+ sybct_result *result;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(result_index);
+ id = result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ return_value->value.lval = result->num_fields;
+ return_value->type = IS_LONG;
+}
+
+
+void php3_sybct_fetch_row(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_result_index;
+ int type,i,id;
+ sybct_result *result;
+ pval field_content;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &sybct_result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybct_result_index);
+ id = sybct_result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ if (result->cur_row >= result->num_rows) {
+ RETURN_FALSE;
+ }
+
+ array_init(return_value);
+ for (i=0; i<result->num_fields; i++) {
+ field_content = result->data[result->cur_row][i];
+ pval_copy_constructor(&field_content);
+ _php3_hash_index_update(return_value->value.ht, i, (void *) &field_content, sizeof(pval),NULL);
+ }
+ result->cur_row++;
+}
+
+
+static void php3_sybct_fetch_hash(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_result_index;
+ sybct_result *result;
+ int type;
+ int i;
+ pval *pval_ptr,tmp;
+ PLS_FETCH();
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &sybct_result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybct_result_index);
+ result = (sybct_result *) php3_list_find(sybct_result_index->value.lval,&type);
+
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",sybct_result_index->value.lval);
+ RETURN_FALSE;
+ }
+
+ if (result->cur_row >= result->num_rows) {
+ RETURN_FALSE;
+ }
+
+ if (array_init(return_value)==FAILURE) {
+ RETURN_FALSE;
+ }
+
+ for (i=0; i<result->num_fields; i++) {
+ tmp = result->data[result->cur_row][i];
+ pval_copy_constructor(&tmp);
+ if (PG(magic_quotes_runtime) && tmp.type == IS_STRING) {
+ tmp.value.str.val = _php3_addslashes(tmp.value.str.val,tmp.value.str.len,&tmp.value.str.len,1);
+ }
+ _php3_hash_index_update(return_value->value.ht, i, (void *) &tmp, sizeof(pval), (void **) &pval_ptr);
+ _php3_hash_pointer_update(return_value->value.ht, result->fields[i].name, strlen(result->fields[i].name)+1, pval_ptr);
+ }
+ result->cur_row++;
+}
+
+
+void php3_sybct_fetch_object(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybct_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ if (return_value->type==IS_ARRAY) {
+ return_value->type=IS_OBJECT;
+ }
+}
+
+
+void php3_sybct_fetch_array(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybct_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+}
+
+void php3_sybct_data_seek(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_result_index,*offset;
+ int type,id;
+ sybct_result *result;
+
+ if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &sybct_result_index, &offset)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybct_result_index);
+ id = sybct_result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_long(offset);
+ if (offset->value.lval<0 || offset->value.lval>=result->num_rows) {
+ php3_error(E_WARNING,"Sybase: Bad row offset");
+ RETURN_FALSE;
+ }
+
+ result->cur_row = offset->value.lval;
+ RETURN_TRUE;
+}
+
+
+static char *php3_sybct_get_field_name(CS_INT type)
+{
+ switch (type) {
+ case CS_CHAR_TYPE:
+ case CS_VARCHAR_TYPE:
+ case CS_TEXT_TYPE:
+ return "string";
+ break;
+ case CS_IMAGE_TYPE:
+ return "image";
+ break;
+ case CS_BINARY_TYPE:
+ case CS_VARBINARY_TYPE:
+ return "blob";
+ break;
+ case CS_BIT_TYPE:
+ return "bit";
+ break;
+ case CS_TINYINT_TYPE:
+ case CS_SMALLINT_TYPE:
+ case CS_INT_TYPE:
+ return "int";
+ break;
+ case CS_REAL_TYPE:
+ case CS_FLOAT_TYPE:
+ case CS_NUMERIC_TYPE:
+ case CS_DECIMAL_TYPE:
+ return "real";
+ break;
+ case CS_MONEY_TYPE:
+ case CS_MONEY4_TYPE:
+ return "money";
+ break;
+ case CS_DATETIME_TYPE:
+ case CS_DATETIME4_TYPE:
+ return "datetime";
+ break;
+ default:
+ return "unknown";
+ break;
+ }
+}
+
+
+void php3_sybct_fetch_field(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_result_index,*offset;
+ int type,id,field_offset;
+ sybct_result *result;
+
+ switch (ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &sybct_result_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ field_offset=-1;
+ break;
+ case 2:
+ if (getParameters(ht, 2, &sybct_result_index, &offset)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(offset);
+ field_offset = offset->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ convert_to_long(sybct_result_index);
+ id = sybct_result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ if (field_offset==-1) {
+ field_offset = result->cur_field;
+ result->cur_field++;
+ }
+
+ if (field_offset<0 || field_offset >= result->num_fields) {
+ if (ARG_COUNT(ht)==2) { /* field specified explicitly */
+ php3_error(E_WARNING,"Sybase: Bad column offset");
+ }
+ RETURN_FALSE;
+ }
+
+ if (object_init(return_value)==FAILURE) {
+ RETURN_FALSE;
+ }
+ 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);
+ add_property_string(return_value, "type", php3_sybct_get_field_name(result->fields[field_offset].type), 1);
+}
+
+
+void php3_sybct_field_seek(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_result_index,*offset;
+ int type,id,field_offset;
+ sybct_result *result;
+
+ if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &sybct_result_index, &offset)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybct_result_index);
+ id = sybct_result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_long(offset);
+ field_offset = offset->value.lval;
+
+ if (field_offset<0 || field_offset >= result->num_fields) {
+ php3_error(E_WARNING,"Sybase: Bad column offset");
+ RETURN_FALSE;
+ }
+
+ result->cur_field = field_offset;
+ RETURN_TRUE;
+}
+
+
+void php3_sybct_result(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *row, *field, *sybct_result_index;
+ int id,type,field_offset=0;
+ sybct_result *result;
+
+
+ if (ARG_COUNT(ht)!=3 || getParameters(ht, 3, &sybct_result_index, &row, &field)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybct_result_index);
+ id = sybct_result_index->value.lval;
+
+ result = (sybct_result *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_long(row);
+ if (row->value.lval<0 || row->value.lval>=result->num_rows) {
+ php3_error(E_WARNING,"Sybase: Bad row offset (%d)",row->value.lval);
+ RETURN_FALSE;
+ }
+
+ switch(field->type) {
+ case IS_STRING: {
+ int i;
+
+ for (i=0; i<result->num_fields; i++) {
+ if (!strcasecmp(result->fields[i].name,field->value.str.val)) {
+ field_offset = i;
+ break;
+ }
+ }
+ if (i>=result->num_fields) { /* no match found */
+ php3_error(E_WARNING,"Sybase: %s field not found in result",field->value.str.val);
+ RETURN_FALSE;
+ }
+ break;
+ }
+ default:
+ convert_to_long(field);
+ field_offset = field->value.lval;
+ if (field_offset<0 || field_offset>=result->num_fields) {
+ php3_error(E_WARNING,"Sybase: Bad column offset specified");
+ RETURN_FALSE;
+ }
+ break;
+ }
+
+ *return_value = result->data[row->value.lval][field_offset];
+ pval_copy_constructor(return_value);
+}
+
+
+void php3_sybct_affected_rows(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybct_link_index;
+ int id,type;
+ sybct_link *sybct_ptr;
+ CS_INT row_count;
+
+ switch(ARG_COUNT(ht)) {
+ case 0:
+ id = php3_sybct_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ break;
+ case 1:
+ if (getParameters(ht, 1, &sybct_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybct_link_index);
+ id = sybct_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ sybct_ptr = (sybct_link *) php3_list_find(id,&type);
+ if (type!=php3_sybct_module.le_link && type!=php3_sybct_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ return_value->value.lval = sybct_ptr->affected_rows;
+ return_value->type = IS_LONG;
+}
+
+
+void php3_info_sybct(void)
+{
+ char maxp[16],maxl[16];
+
+ if (php3_sybct_module.max_persistent==-1) {
+ strcpy(maxp,"Unlimited");
+ } else {
+ snprintf(maxp,15,"%ld",php3_sybct_module.max_persistent);
+ maxp[15]=0;
+ }
+ if (php3_sybct_module.max_links==-1) {
+ strcpy(maxl,"Unlimited");
+ } else {
+ snprintf(maxl,15,"%ld",php3_sybct_module.max_links);
+ maxl[15]=0;
+ }
+ php3_printf("<table cellpadding=5>"
+ "<tr><td>Allow persistent links:</td><td>%s</td></tr>\n"
+ "<tr><td>Persistent links:</td><td>%d/%s</td></tr>\n"
+ "<tr><td>Total links:</td><td>%d/%s</td></tr>\n"
+ "<tr><td>Application name:</td><td>%s</td></tr>\n"
+ "</table>\n",
+ (php3_sybct_module.allow_persistent?"Yes":"No"),
+ php3_sybct_module.num_persistent,maxp,
+ php3_sybct_module.num_links,maxl,
+ php3_sybct_module.appname);
+}
+
+
+void php3_sybct_min_client_severity(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *severity;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &severity)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_long(severity);
+ php3_sybct_module.min_client_severity = severity->value.lval;
+}
+
+
+void php3_sybct_min_server_severity(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *severity;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &severity)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_long(severity);
+ php3_sybct_module.min_server_severity = severity->value.lval;
+}
+
+
+#endif
diff --git a/ext/sybase/sybase.c b/ext/sybase/sybase.c
new file mode 100644
index 0000000000..11d5fcd768
--- /dev/null
+++ b/ext/sybase/sybase.c
@@ -0,0 +1,1258 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Authors: Zeev Suraski <zeev@zend.com> |
+ +----------------------------------------------------------------------+
+ | php3_sybase_get_column_content_with_type() based on code by: |
+ | Muhammad A Muquit <MA_Muquit@fccc.edu> |
+ | Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
+ +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+
+#ifndef MSVC5
+#include "config.h"
+#endif
+#include "php.h"
+#include "php3_sybase.h"
+#include "ext/standard/php3_standard.h"
+#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
+
+function_entry sybase_functions[] = {
+ {"sybase_connect", php3_sybase_connect, NULL},
+ {"sybase_pconnect", php3_sybase_pconnect, NULL},
+ {"sybase_close", php3_sybase_close, NULL},
+ {"sybase_select_db", php3_sybase_select_db, NULL},
+ {"sybase_query", php3_sybase_query, NULL},
+ {"sybase_free_result", php3_sybase_free_result, NULL},
+ {"sybase_get_last_message", php3_sybase_get_last_message, NULL},
+ {"sybase_num_rows", php3_sybase_num_rows, NULL},
+ {"sybase_num_fields", php3_sybase_num_fields, NULL},
+ {"sybase_fetch_row", php3_sybase_fetch_row, NULL},
+ {"sybase_fetch_array", php3_sybase_fetch_array, NULL},
+ {"sybase_fetch_object", php3_sybase_fetch_object, NULL},
+ {"sybase_data_seek", php3_sybase_data_seek, NULL},
+ {"sybase_fetch_field", php3_sybase_fetch_field, NULL},
+ {"sybase_field_seek", php3_sybase_field_seek, NULL},
+ {"sybase_result", php3_sybase_result, NULL},
+ {"sybase_min_error_severity", php3_sybase_min_error_severity, NULL},
+ {"sybase_min_message_severity", php3_sybase_min_message_severity, NULL},
+ {"mssql_connect", php3_sybase_connect, NULL},
+ {"mssql_pconnect", php3_sybase_pconnect, NULL},
+ {"mssql_close", php3_sybase_close, NULL},
+ {"mssql_select_db", php3_sybase_select_db, NULL},
+ {"mssql_query", php3_sybase_query, NULL},
+ {"mssql_free_result", php3_sybase_free_result, NULL},
+ {"mssql_get_last_message", php3_sybase_get_last_message, NULL},
+ {"mssql_num_rows", php3_sybase_num_rows, NULL},
+ {"mssql_num_fields", php3_sybase_num_fields, NULL},
+ {"mssql_fetch_row", php3_sybase_fetch_row, NULL},
+ {"mssql_fetch_array", php3_sybase_fetch_array, NULL},
+ {"mssql_fetch_object", php3_sybase_fetch_object, NULL},
+ {"mssql_data_seek", php3_sybase_data_seek, NULL},
+ {"mssql_fetch_field", php3_sybase_fetch_field, NULL},
+ {"mssql_field_seek", php3_sybase_field_seek, NULL},
+ {"mssql_result", php3_sybase_result, NULL},
+ {"mssql_min_error_severity", php3_sybase_min_error_severity, NULL},
+ {"mssql_min_message_severity", php3_sybase_min_message_severity, NULL},
+ {NULL, NULL, NULL}
+};
+
+php3_module_entry sybase_module_entry = {
+ "Sybase SQL", sybase_functions, php3_minit_sybase, php3_mshutdown_sybase, php3_rinit_sybase, php3_rshutdown_sybase, php3_info_sybase, STANDARD_MODULE_PROPERTIES
+};
+
+#if COMPILE_DL
+php3_module_entry *get_module() { return &sybase_module_entry; }
+#endif
+
+THREAD_LS sybase_module php3_sybase_module;
+THREAD_LS static HashTable *resource_list, *resource_plist;
+
+
+#define CHECK_LINK(link) { if (link==-1) { php3_error(E_WARNING,"Sybase: A link to the server could not be established"); RETURN_FALSE; } }
+
+
+static void php3_sybase_get_column_content(sybase_link *sybase_ptr,int offset,pval *result, int column_type);
+
+/* error handler */
+static int php3_sybase_error_handler(DBPROCESS *dbproc,int severity,int dberr,
+ int oserr,char *dberrstr,char *oserrstr)
+{
+ if (severity >= php3_sybase_module.min_error_severity) {
+ php3_error(E_WARNING,"Sybase error: %s (severity %d)",dberrstr,severity);
+ }
+ return INT_CANCEL;
+}
+
+/* message handler */
+static int php3_sybase_message_handler(DBPROCESS *dbproc,DBINT msgno,int msgstate,
+ int severity,char *msgtext,char *srvname,
+ char *procname,DBUSMALLINT line)
+{
+ if (severity >= php3_sybase_module.min_message_severity) {
+ php3_error(E_WARNING,"Sybase message: %s (severity %d)",msgtext,severity);
+ }
+ STR_FREE(php3_sybase_module.server_message);
+ php3_sybase_module.server_message = estrdup(msgtext);
+ return 0;
+}
+
+
+static int _clean_invalid_results(list_entry *le)
+{
+ if (le->type == php3_sybase_module.le_result) {
+ sybase_link *sybase_ptr = ((sybase_result *) le->ptr)->sybase_ptr;
+
+ if (!sybase_ptr->valid) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+
+static void _free_sybase_result(sybase_result *result)
+{
+ int i,j;
+
+ if (result->data) {
+ for (i=0; i<result->num_rows; i++) {
+ for (j=0; j<result->num_fields; j++) {
+ pval_destructor(&result->data[i][j]);
+ }
+ 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);
+}
+
+
+static void _close_sybase_link(sybase_link *sybase_ptr)
+{
+ sybase_ptr->valid = 0;
+ _php3_hash_apply(resource_list,(int (*)(void *))_clean_invalid_results);
+ dbclose(sybase_ptr->link);
+ dbloginfree(sybase_ptr->login);
+ efree(sybase_ptr);
+ php3_sybase_module.num_links--;
+}
+
+
+static void _close_sybase_plink(sybase_link *sybase_ptr)
+{
+ dbclose(sybase_ptr->link);
+ dbloginfree(sybase_ptr->login);
+ free(sybase_ptr);
+ php3_sybase_module.num_persistent--;
+ php3_sybase_module.num_links--;
+}
+
+
+int php3_minit_sybase(INIT_FUNC_ARGS)
+{
+ char *interface_file;
+
+ if (dbinit()==FAIL) {
+ return FAILURE;
+ }
+ dberrhandle((EHANDLEFUNC) php3_sybase_error_handler);
+ dbmsghandle((MHANDLEFUNC) php3_sybase_message_handler);
+
+ if (cfg_get_string("sybase.interface_file",&interface_file)==SUCCESS) {
+ dbsetifile(interface_file);
+ }
+ if (cfg_get_long("sybase.allow_persistent",&php3_sybase_module.allow_persistent)==FAILURE) {
+ php3_sybase_module.allow_persistent=1;
+ }
+ if (cfg_get_long("sybase.max_persistent",&php3_sybase_module.max_persistent)==FAILURE) {
+ php3_sybase_module.max_persistent=-1;
+ }
+ if (cfg_get_long("sybase.max_links",&php3_sybase_module.max_links)==FAILURE) {
+ php3_sybase_module.max_links=-1;
+ }
+ if (cfg_get_long("sybase.min_error_severity",&php3_sybase_module.cfg_min_error_severity)==FAILURE) {
+ php3_sybase_module.cfg_min_error_severity=10;
+ }
+ if (cfg_get_long("sybase.min_message_severity",&php3_sybase_module.cfg_min_message_severity)==FAILURE) {
+ php3_sybase_module.cfg_min_message_severity=10;
+ }
+ if (cfg_get_long("sybase.compatability_mode",&php3_sybase_module.compatability_mode)==FAILURE) {
+ php3_sybase_module.compatability_mode = 0;
+ }
+
+ php3_sybase_module.num_persistent=0;
+ php3_sybase_module.le_link = register_list_destructors(_close_sybase_link,NULL);
+ php3_sybase_module.le_plink = register_list_destructors(NULL,_close_sybase_plink);
+ php3_sybase_module.le_result = register_list_destructors(_free_sybase_result,NULL);
+
+ return SUCCESS;
+}
+
+
+int php3_rinit_sybase(INIT_FUNC_ARGS)
+{
+ php3_sybase_module.default_link=-1;
+ php3_sybase_module.num_links = php3_sybase_module.num_persistent;
+ php3_sybase_module.appname = estrndup("PHP 3.0",7);
+ php3_sybase_module.server_message = empty_string;
+ php3_sybase_module.min_error_severity = php3_sybase_module.cfg_min_error_severity;
+ php3_sybase_module.min_message_severity = php3_sybase_module.cfg_min_message_severity;
+ return SUCCESS;
+}
+
+int php3_mshutdown_sybase(SHUTDOWN_FUNC_ARGS)
+{
+ dbexit();
+ return SUCCESS;
+}
+
+int php3_rshutdown_sybase(SHUTDOWN_FUNC_ARGS)
+{
+ efree(php3_sybase_module.appname);
+ STR_FREE(php3_sybase_module.server_message);
+ return SUCCESS;
+}
+
+static void php3_sybase_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent)
+{
+ char *user,*passwd,*host;
+ char *hashed_details;
+ int hashed_details_length;
+ sybase_link sybase,*sybase_ptr;
+
+ resource_list = list;
+ resource_plist = plist;
+
+ switch(ARG_COUNT(ht)) {
+ case 0: /* defaults */
+ host=user=passwd=NULL;
+ hashed_details_length=6+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ strcpy(hashed_details,"sybase___");
+ break;
+ case 1: {
+ pval *yyhost;
+
+ if (getParameters(ht, 1, &yyhost)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_string(yyhost);
+ host = yyhost->value.str.val;
+ user=passwd=NULL;
+ hashed_details_length = yyhost->value.str.len+6+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ sprintf(hashed_details,"sybase_%s__",yyhost->value.str.val);
+ }
+ break;
+ case 2: {
+ pval *yyhost,*yyuser;
+
+ if (getParameters(ht, 2, &yyhost, &yyuser)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_string(yyhost);
+ convert_to_string(yyuser);
+ host = yyhost->value.str.val;
+ user = yyuser->value.str.val;
+ passwd=NULL;
+ hashed_details_length = yyhost->value.str.len+yyuser->value.str.len+6+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ sprintf(hashed_details,"sybase_%s_%s_",yyhost->value.str.val,yyuser->value.str.val);
+ }
+ break;
+ case 3: {
+ pval *yyhost,*yyuser,*yypasswd;
+
+ if (getParameters(ht, 3, &yyhost, &yyuser, &yypasswd) == FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_string(yyhost);
+ convert_to_string(yyuser);
+ convert_to_string(yypasswd);
+ host = yyhost->value.str.val;
+ user = yyuser->value.str.val;
+ passwd = yypasswd->value.str.val;
+ hashed_details_length = yyhost->value.str.len+yyuser->value.str.len+yypasswd->value.str.len+6+3;
+ hashed_details = (char *) emalloc(hashed_details_length+1);
+ sprintf(hashed_details,"sybase_%s_%s_%s",yyhost->value.str.val,yyuser->value.str.val,yypasswd->value.str.val); /* SAFE */
+ }
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+
+ /* set a DBLOGIN record */
+ if ((sybase.login=dblogin())==NULL) {
+ php3_error(E_WARNING,"Sybase: Unable to allocate login record");
+ RETURN_FALSE;
+ }
+
+ if (user) {
+ DBSETLUSER(sybase.login,user);
+ }
+ if (passwd) {
+ DBSETLPWD(sybase.login,passwd);
+ }
+ DBSETLAPP(sybase.login,php3_sybase_module.appname);
+ sybase.valid = 1;
+
+ if (!php3_sybase_module.allow_persistent) {
+ persistent=0;
+ }
+ if (persistent) {
+ list_entry *le;
+
+ /* try to find if we already have this link in our persistent list */
+ if (_php3_hash_find(plist, hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
+ list_entry new_le;
+
+ if (php3_sybase_module.max_links!=-1 && php3_sybase_module.num_links>=php3_sybase_module.max_links) {
+ php3_error(E_WARNING,"Sybase: Too many open links (%d)",php3_sybase_module.num_links);
+ efree(hashed_details);
+ dbloginfree(sybase.login);
+ RETURN_FALSE;
+ }
+ if (php3_sybase_module.max_persistent!=-1 && php3_sybase_module.num_persistent>=php3_sybase_module.max_persistent) {
+ php3_error(E_WARNING,"Sybase: Too many open persistent links (%d)",php3_sybase_module.num_persistent);
+ efree(hashed_details);
+ dbloginfree(sybase.login);
+ RETURN_FALSE;
+ }
+ /* create the link */
+ if ((sybase.link=dbopen(sybase.login,host))==FAIL) {
+ /*php3_error(E_WARNING,"Sybase: Unable to connect to server: %s",sybase_error(sybase));*/
+ efree(hashed_details);
+ dbloginfree(sybase.login);
+ RETURN_FALSE;
+ }
+
+ if (dbsetopt(sybase.link,DBBUFFER,"2",-1)==FAIL) {
+ efree(hashed_details);
+ dbloginfree(sybase.login);
+ dbclose(sybase.link);
+ RETURN_FALSE;
+ }
+
+ /* hash it up */
+ sybase_ptr = (sybase_link *) malloc(sizeof(sybase_link));
+ memcpy(sybase_ptr,&sybase,sizeof(sybase_link));
+ new_le.type = php3_sybase_module.le_plink;
+ new_le.ptr = sybase_ptr;
+ if (_php3_hash_update(plist, hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry),NULL)==FAILURE) {
+ free(sybase_ptr);
+ efree(hashed_details);
+ dbloginfree(sybase.login);
+ RETURN_FALSE;
+ }
+ php3_sybase_module.num_persistent++;
+ php3_sybase_module.num_links++;
+ } else { /* we do */
+ if (le->type != php3_sybase_module.le_plink) {
+#if BROKEN_SYBASE_PCONNECTS
+ log_error("PHP/Sybase: Hashed persistent link is not a Sybase link!",php3_rqst->server);
+#endif
+ php3_error(E_WARNING,"Sybase: Hashed persistent link is not a Sybase link!");
+ RETURN_FALSE;
+ }
+
+ sybase_ptr = (sybase_link *) le->ptr;
+ /* test that the link hasn't died */
+ if (DBDEAD(sybase_ptr->link)==TRUE) {
+#if BROKEN_SYBASE_PCONNECTS
+ log_error("PHP/Sybase: Persistent link died, trying to reconnect...",php3_rqst->server);
+#endif
+ if ((sybase_ptr->link=dbopen(sybase_ptr->login,host))==FAIL) {
+#if BROKEN_SYBASE_PCONNECTS
+ log_error("PHP/Sybase: Unable to reconnect!",php3_rqst->server);
+#endif
+ /*php3_error(E_WARNING,"Sybase: Link to server lost, unable to reconnect");*/
+ _php3_hash_del(plist, hashed_details, hashed_details_length+1);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+#if BROKEN_SYBASE_PCONNECTS
+ log_error("PHP/Sybase: Reconnect successful!",php3_rqst->server);
+#endif
+ if (dbsetopt(sybase_ptr->link,DBBUFFER,"2",-1)==FAIL) {
+#if BROKEN_SYBASE_PCONNECTS
+ log_error("PHP/Sybase: Unable to set required options",php3_rqst->server);
+#endif
+ _php3_hash_del(plist, hashed_details, hashed_details_length+1);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ }
+ }
+ return_value->value.lval = php3_list_insert(sybase_ptr,php3_sybase_module.le_plink);
+ return_value->type = IS_LONG;
+ } 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.
+ */
+ if (_php3_hash_find(list,hashed_details,hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
+ int type,link;
+ void *ptr;
+
+ if (index_ptr->type != le_index_ptr) {
+ RETURN_FALSE;
+ }
+ link = (int) index_ptr->ptr;
+ ptr = php3_list_find(link,&type); /* check if the link is still there */
+ if (ptr && (type==php3_sybase_module.le_link || type==php3_sybase_module.le_plink)) {
+ return_value->value.lval = php3_sybase_module.default_link = link;
+ return_value->type = IS_LONG;
+ efree(hashed_details);
+ return;
+ } else {
+ _php3_hash_del(list,hashed_details,hashed_details_length+1);
+ }
+ }
+ if (php3_sybase_module.max_links!=-1 && php3_sybase_module.num_links>=php3_sybase_module.max_links) {
+ php3_error(E_WARNING,"Sybase: Too many open links (%d)",php3_sybase_module.num_links);
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ if ((sybase.link=dbopen(sybase.login,host))==NULL) {
+ /*php3_error(E_WARNING,"Sybase: Unable to connect to server: %s",sybase_error(sybase));*/
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+
+ if (dbsetopt(sybase.link,DBBUFFER,"2",-1)==FAIL) {
+ efree(hashed_details);
+ dbloginfree(sybase.login);
+ dbclose(sybase.link);
+ RETURN_FALSE;
+ }
+
+ /* add it to the list */
+ sybase_ptr = (sybase_link *) emalloc(sizeof(sybase_link));
+ memcpy(sybase_ptr,&sybase,sizeof(sybase_link));
+ return_value->value.lval = php3_list_insert(sybase_ptr,php3_sybase_module.le_link);
+ return_value->type = IS_LONG;
+
+ /* add it to the hash */
+ new_index_ptr.ptr = (void *) return_value->value.lval;
+ new_index_ptr.type = le_index_ptr;
+ if (_php3_hash_update(list,hashed_details,hashed_details_length+1,(void *) &new_index_ptr, sizeof(list_entry),NULL)==FAILURE) {
+ efree(hashed_details);
+ RETURN_FALSE;
+ }
+ php3_sybase_module.num_links++;
+ }
+ efree(hashed_details);
+ php3_sybase_module.default_link=return_value->value.lval;
+}
+
+
+static int php3_sybase_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
+{
+ if (php3_sybase_module.default_link==-1) { /* no link opened yet, implicitly open one */
+ HashTable dummy;
+
+ _php3_hash_init(&dummy,0,NULL,NULL,0);
+ php3_sybase_do_connect(&dummy,return_value,list,plist,0);
+ _php3_hash_destroy(&dummy);
+ }
+ return php3_sybase_module.default_link;
+}
+
+
+void php3_sybase_connect(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
+}
+
+void php3_sybase_pconnect(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
+}
+
+
+void php3_sybase_close(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_link_index;
+ int id,type;
+
+ switch (ARG_COUNT(ht)) {
+ case 0:
+ id = php3_sybase_module.default_link;
+ break;
+ case 1:
+ if (getParameters(ht, 1, &sybase_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybase_link_index);
+ id = sybase_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_link && type!=php3_sybase_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ php3_list_delete(id);
+ RETURN_TRUE;
+}
+
+
+void php3_sybase_select_db(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *db,*sybase_link_index;
+ int id,type;
+ sybase_link *sybase_ptr;
+
+ switch(ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &db)==FAILURE) {
+ RETURN_FALSE;
+ }
+ id = php3_sybase_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ break;
+ case 2:
+ if (getParameters(ht, 2, &db, &sybase_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybase_link_index);
+ id = sybase_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ CHECK_LINK(id);
+
+ sybase_ptr = (sybase_link *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_link && type!=php3_sybase_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_string(db);
+
+ if (dbuse(sybase_ptr->link,db->value.str.val)==FAIL) {
+ /*php3_error(E_WARNING,"Sybase: Unable to select database: %s",sybase_error(sybase));*/
+ RETURN_FALSE;
+ } else {
+ RETURN_TRUE;
+ }
+}
+
+
+static void php3_sybase_get_column_content(sybase_link *sybase_ptr,int offset,pval *result, int column_type)
+{
+ if (dbdatlen(sybase_ptr->link,offset) == 0) {
+ var_reset(result);
+ return;
+ }
+
+ switch (column_type)
+ {
+ case SYBINT2:
+ case SYBINT4: {
+ result->value.lval = (long) anyintcol(offset);
+ result->type = IS_LONG;
+ 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--;
+ }
+ result->value.str.val = estrndup(data,length);
+ result->value.str.len = length;
+ result->type = IS_STRING;
+ break;
+ }
+ /*case SYBFLT8:*/
+ case SYBREAL: {
+ result->value.dval = (double) floatcol(offset);
+ result->type = IS_DOUBLE;
+ break;
+ }
+ default: {
+ if (dbwillconvert(coltype(offset),SYBCHAR)) {
+ char *res_buf;
+ int res_length = dbdatlen(sybase_ptr->link,offset);
+ register char *p;
+
+ switch (coltype(offset)) {
+ case SYBBINARY:
+ case SYBVARBINARY:
+ case SYBCHAR:
+ case SYBVARCHAR:
+ case SYBTEXT:
+ case SYBIMAGE:
+ 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);
+ dbconvert(NULL,coltype(offset),dbdata(sybase_ptr->link,offset), res_length,SYBCHAR,res_buf,-1);
+
+ /* get rid of trailing spaces */
+ p = res_buf + res_length;
+ while (*p == ' ') {
+ p--;
+ res_length--;
+ }
+ *(++p) = 0; /* put a trailing NULL */
+
+ result->value.str.len = res_length;
+ result->value.str.val = res_buf;
+ result->type = IS_STRING;
+ } else {
+ php3_error(E_WARNING,"Sybase: column %d has unknown data type (%d)", offset, coltype(offset));
+ var_reset(result);
+ }
+ }
+ }
+}
+
+
+void php3_sybase_query(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *query,*sybase_link_index;
+ int id,type,retvalue;
+ sybase_link *sybase_ptr;
+ sybase_result *result;
+ int num_fields;
+ int blocks_initialized=1;
+ int i,j;
+ int *column_types;
+
+ switch(ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &query)==FAILURE) {
+ RETURN_FALSE;
+ }
+ id = php3_sybase_module.default_link;
+ break;
+ case 2:
+ if (getParameters(ht, 2, &query, &sybase_link_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(sybase_link_index);
+ id = sybase_link_index->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ sybase_ptr = (sybase_link *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_link && type!=php3_sybase_module.le_plink) {
+ php3_error(E_WARNING,"%d is not a Sybase link index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_string(query);
+ if (dbcmd(sybase_ptr->link,query->value.str.val)==FAIL) {
+ /*php3_error(E_WARNING,"Sybase: Unable to set query");*/
+ RETURN_FALSE;
+ }
+ if (dbsqlexec(sybase_ptr->link)==FAIL || dbresults(sybase_ptr->link)==FAIL) {
+ /*php3_error(E_WARNING,"Sybase: Query failed");*/
+ 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;
+ }
+
+ column_types = (int *) emalloc(sizeof(int) * num_fields);
+ for (i=0; i<num_fields; i++) {
+ column_types[i] = coltype(i+1);
+ }
+
+ result = (sybase_result *) emalloc(sizeof(sybase_result));
+ result->data = (pval **) emalloc(sizeof(pval *)*SYBASE_ROWS_BLOCK);
+ 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) {
+ result->data = (pval **) erealloc(result->data,sizeof(pval *)*SYBASE_ROWS_BLOCK*(++blocks_initialized));
+ }
+ result->data[i] = (pval *) emalloc(sizeof(pval)*num_fields);
+ for (j=1; j<=num_fields; j++) {
+ php3_sybase_get_column_content(sybase_ptr, j, &result->data[i][j-1], column_types[j-1]);
+ if (!php3_sybase_module.compatability_mode) {
+ convert_to_string(&result->data[i][j-1]);
+ }
+ }
+ retvalue=dbnextrow(sybase_ptr->link);
+ dbclrbuf(sybase_ptr->link,DBLASTROW(sybase_ptr->link)-1);
+ i++;
+ }
+ result->num_rows = DBCOUNT(sybase_ptr->link);
+
+ result->fields = (sybase_field *) emalloc(sizeof(sybase_field)*num_fields);
+ 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) {
+ result->fields[i].column_source = empty_string;
+ }
+ result->fields[i].type = column_types[i];
+ /* 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);
+ return_value->value.lval = php3_list_insert(result,php3_sybase_module.le_result);
+ return_value->type = IS_LONG;
+}
+
+
+void php3_sybase_free_result(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_result_index;
+ sybase_result *result;
+ int type;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &sybase_result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybase_result_index);
+ if (sybase_result_index->value.lval==0) {
+ RETURN_FALSE;
+ }
+ result = (sybase_result *) php3_list_find(sybase_result_index->value.lval,&type);
+
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",sybase_result_index->value.lval);
+ RETURN_FALSE;
+ }
+ php3_list_delete(sybase_result_index->value.lval);
+ RETURN_TRUE;
+}
+
+
+void php3_sybase_get_last_message(INTERNAL_FUNCTION_PARAMETERS)
+{
+ RETURN_STRING(php3_sybase_module.server_message,1);
+}
+
+
+void php3_sybase_num_rows(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *result_index;
+ int type,id;
+ sybase_result *result;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(result_index);
+ id = result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ return_value->value.lval = result->num_rows;
+ return_value->type = IS_LONG;
+}
+
+
+void php3_sybase_num_fields(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *result_index;
+ int type,id;
+ sybase_result *result;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(result_index);
+ id = result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ return_value->value.lval = result->num_fields;
+ return_value->type = IS_LONG;
+}
+
+
+void php3_sybase_fetch_row(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_result_index;
+ int type,i,id;
+ sybase_result *result;
+ pval field_content;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &sybase_result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybase_result_index);
+ id = sybase_result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ if (result->cur_row >= result->num_rows) {
+ RETURN_FALSE;
+ }
+
+ array_init(return_value);
+ for (i=0; i<result->num_fields; i++) {
+ field_content = result->data[result->cur_row][i];
+ pval_copy_constructor(&field_content);
+ _php3_hash_index_update(return_value->value.ht, i, (void *) &field_content, sizeof(pval),NULL);
+ }
+ result->cur_row++;
+}
+
+
+static void php3_sybase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_result_index;
+ sybase_result *result;
+ int type;
+ int i;
+ pval *pval_ptr,tmp;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &sybase_result_index)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybase_result_index);
+ result = (sybase_result *) php3_list_find(sybase_result_index->value.lval,&type);
+
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",sybase_result_index->value.lval);
+ RETURN_FALSE;
+ }
+
+ if (result->cur_row >= result->num_rows) {
+ RETURN_FALSE;
+ }
+
+ if (array_init(return_value)==FAILURE) {
+ RETURN_FALSE;
+ }
+
+ for (i=0; i<result->num_fields; i++) {
+ tmp = result->data[result->cur_row][i];
+ pval_copy_constructor(&tmp);
+ if (PG(magic_quotes_runtime) && tmp.type == IS_STRING) {
+ tmp.value.str.val = _php3_addslashes(tmp.value.str.val,tmp.value.str.len,&tmp.value.str.len,1);
+ }
+ _php3_hash_index_update(return_value->value.ht, i, (void *) &tmp, sizeof(pval), (void **) &pval_ptr);
+ _php3_hash_pointer_update(return_value->value.ht, result->fields[i].name, strlen(result->fields[i].name)+1, pval_ptr);
+ }
+ result->cur_row++;
+}
+
+
+void php3_sybase_fetch_object(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ if (return_value->type==IS_ARRAY) {
+ return_value->type=IS_OBJECT;
+ }
+}
+
+
+void php3_sybase_fetch_array(INTERNAL_FUNCTION_PARAMETERS)
+{
+ php3_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+}
+
+void php3_sybase_data_seek(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_result_index,*offset;
+ int type,id;
+ sybase_result *result;
+
+ if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &sybase_result_index, &offset)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybase_result_index);
+ id = sybase_result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_long(offset);
+ if (offset->value.lval<0 || offset->value.lval>=result->num_rows) {
+ php3_error(E_WARNING,"Sybase: Bad row offset");
+ RETURN_FALSE;
+ }
+
+ result->cur_row = offset->value.lval;
+ RETURN_TRUE;
+}
+
+
+static char *php3_sybase_get_field_name(int type)
+{
+ 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;
+ }
+}
+
+
+void php3_sybase_fetch_field(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_result_index,*offset;
+ int type,id,field_offset;
+ sybase_result *result;
+
+ switch (ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &sybase_result_index)==FAILURE) {
+ RETURN_FALSE;
+ }
+ field_offset=-1;
+ break;
+ case 2:
+ if (getParameters(ht, 2, &sybase_result_index, &offset)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(offset);
+ field_offset = offset->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ convert_to_long(sybase_result_index);
+ id = sybase_result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ if (field_offset==-1) {
+ field_offset = result->cur_field;
+ result->cur_field++;
+ }
+
+ if (field_offset<0 || field_offset >= result->num_fields) {
+ if (ARG_COUNT(ht)==2) { /* field specified explicitly */
+ php3_error(E_WARNING,"Sybase: Bad column offset");
+ }
+ RETURN_FALSE;
+ }
+
+ if (object_init(return_value)==FAILURE) {
+ RETURN_FALSE;
+ }
+ 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);
+ add_property_string(return_value, "type", php3_sybase_get_field_name(result->fields[field_offset].type), 1);
+}
+
+void php3_sybase_field_seek(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *sybase_result_index,*offset;
+ int type,id,field_offset;
+ sybase_result *result;
+
+ if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &sybase_result_index, &offset)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybase_result_index);
+ id = sybase_result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_long(offset);
+ field_offset = offset->value.lval;
+
+ if (field_offset<0 || field_offset >= result->num_fields) {
+ php3_error(E_WARNING,"Sybase: Bad column offset");
+ RETURN_FALSE;
+ }
+
+ result->cur_field = field_offset;
+ RETURN_TRUE;
+}
+
+
+void php3_sybase_result(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *row, *field, *sybase_result_index;
+ int id,type,field_offset=0;
+ sybase_result *result;
+
+
+ if (ARG_COUNT(ht)!=3 || getParameters(ht, 3, &sybase_result_index, &row, &field)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ convert_to_long(sybase_result_index);
+ id = sybase_result_index->value.lval;
+
+ result = (sybase_result *) php3_list_find(id,&type);
+ if (type!=php3_sybase_module.le_result) {
+ php3_error(E_WARNING,"%d is not a Sybase result index",id);
+ RETURN_FALSE;
+ }
+
+ convert_to_long(row);
+ if (row->value.lval<0 || row->value.lval>=result->num_rows) {
+ php3_error(E_WARNING,"Sybase: Bad row offset (%d)",row->value.lval);
+ RETURN_FALSE;
+ }
+
+ switch(field->type) {
+ case IS_STRING: {
+ int i;
+
+ for (i=0; i<result->num_fields; i++) {
+ if (!strcasecmp(result->fields[i].name,field->value.str.val)) {
+ field_offset = i;
+ break;
+ }
+ }
+ if (i>=result->num_fields) { /* no match found */
+ php3_error(E_WARNING,"Sybase: %s field not found in result",field->value.str.val);
+ RETURN_FALSE;
+ }
+ break;
+ }
+ default:
+ convert_to_long(field);
+ field_offset = field->value.lval;
+ if (field_offset<0 || field_offset>=result->num_fields) {
+ php3_error(E_WARNING,"Sybase: Bad column offset specified");
+ RETURN_FALSE;
+ }
+ break;
+ }
+
+ *return_value = result->data[row->value.lval][field_offset];
+ pval_copy_constructor(return_value);
+}
+
+
+void php3_info_sybase(void)
+{
+ char maxp[16],maxl[16];
+
+ if (php3_sybase_module.max_persistent==-1) {
+ strcpy(maxp,"Unlimited");
+ } else {
+ snprintf(maxp,15,"%ld",php3_sybase_module.max_persistent);
+ maxp[15]=0;
+ }
+ if (php3_sybase_module.max_links==-1) {
+ strcpy(maxl,"Unlimited");
+ } else {
+ snprintf(maxl,15,"%ld",php3_sybase_module.max_links);
+ maxl[15]=0;
+ }
+ php3_printf("<table cellpadding=5>"
+ "<tr><td>Allow persistent links:</td><td>%s</td></tr>\n"
+ "<tr><td>Persistent links:</td><td>%d/%s</td></tr>\n"
+ "<tr><td>Total links:</td><td>%d/%s</td></tr>\n"
+ "<tr><td>Application name:</td><td>%s</td></tr>\n"
+ "<tr><td valign=\"top\" width=\"20%%\">Client API information:</td><td><pre>%s</pre></td></tr>\n"
+ "</table>\n",
+ (php3_sybase_module.allow_persistent?"Yes":"No"),
+ php3_sybase_module.num_persistent,maxp,
+ php3_sybase_module.num_links,maxl,
+ php3_sybase_module.appname,
+ dbversion());
+}
+
+
+void php3_sybase_min_error_severity(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *severity;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &severity)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_long(severity);
+ php3_sybase_module.min_error_severity = severity->value.lval;
+}
+
+
+void php3_sybase_min_message_severity(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *severity;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &severity)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_long(severity);
+ php3_sybase_module.min_message_severity = severity->value.lval;
+}
+
+#endif