Skip to content

Commit

Permalink
Merge pull request #240 from yitam/sqlsrv-linux
Browse files Browse the repository at this point in the history
Merging Sqlsrv source code from linux to windows
  • Loading branch information
Hadis Kakanejadi Fard authored Jan 26, 2017
2 parents dcc4ff2 + 1b38d24 commit 88a2b05
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 79 deletions.
41 changes: 41 additions & 0 deletions source/sqlsrv/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
PHP_ARG_ENABLE(sqlsrv, whether to enable sqlsrv functions,
[ --disable-sqlsrv Disable sqlsrv functions], yes)

if test "$PHP_SQLSRV" != "no"; then
sqlsrv_src_class="\
conn.cpp \
util.cpp \
init.cpp \
stmt.cpp \
"
shared_src_class="\
shared/core_conn.cpp \
shared/core_results.cpp \
shared/core_stream.cpp \
shared/core_init.cpp \
shared/core_stmt.cpp \
shared/core_util.cpp \
shared/FormattedPrint.cpp \
shared/localizationimpl.cpp \
shared/StringFunctions.cpp \
"
AC_MSG_CHECKING([for SQLSRV headers])
if test -f $srcdir/ext/sqlsrv/shared/core_sqlsrv.h; then
sqlsrv_inc_path=$srcdir/ext/sqlsrv/shared/
elif test -f $srcdir/shared/core_sqlsrv.h; then
sqlsrv_inc_path=$srcdir/shared/
else
AC_MSG_ERROR([Cannot find SQLSRV headers])
fi
AC_MSG_RESULT($sqlsrv_inc_path)

CXXFLAGS="$CXXFLAGS -std=c++11"
PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, SQLSRV_SHARED_LIBADD)
PHP_ADD_LIBRARY(odbc, 1, SQLSRV_SHARED_LIBADD)
PHP_SUBST(SQLSRV_SHARED_LIBADD)
AC_DEFINE(HAVE_SQLSRV, 1, [ ])
PHP_ADD_INCLUDE([$sqlsrv_inc_path])
PHP_NEW_EXTENSION(sqlsrv, $sqlsrv_src_class $shared_src_class, $ext_shared,,-std=c++11)

fi
32 changes: 14 additions & 18 deletions source/sqlsrv/conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
//---------------------------------------------------------------------------------------------------------------------------------

#include "php_sqlsrv.h"
#include <psapi.h>
#include <windows.h>
#include <winver.h>

#include <string>
#include <sstream>
Expand Down Expand Up @@ -146,12 +143,12 @@ int get_stmt_option_key( zend_string* key, size_t key_len TSRMLS_DC );

// constants for parameters used by process_params function(s)
int ss_sqlsrv_conn::descriptor;
char* ss_sqlsrv_conn::resource_name = "ss_sqlsrv_conn";
char* ss_sqlsrv_conn::resource_name = static_cast<char *>("ss_sqlsrv_conn");

// connection specific parameter proccessing. Use the generic function specialised to return a connection
// resource.
#define PROCESS_PARAMS( rsrc, param_spec, calling_func, param_count, ... ) \
rsrc = process_params<ss_sqlsrv_conn>( INTERNAL_FUNCTION_PARAM_PASSTHRU, param_spec, calling_func, param_count, __VA_ARGS__ ); \
rsrc = process_params<ss_sqlsrv_conn>( INTERNAL_FUNCTION_PARAM_PASSTHRU, param_spec, calling_func, param_count, ##__VA_ARGS__ );\
if( rsrc == NULL ) { \
RETURN_FALSE; \
}
Expand Down Expand Up @@ -765,7 +762,10 @@ PHP_FUNCTION( sqlsrv_client_info )
core_sqlsrv_get_client_info( conn, return_value TSRMLS_CC );

// Add the sqlsrv driver's file version
core::sqlsrv_add_assoc_string( *conn, return_value, "ExtensionVer", VER_FILEVERSION_STR, 1 /*duplicate*/ TSRMLS_CC );
//Declarations below eliminate compiler warnings about string constant to char* conversions
const char* extver = "ExtensionVer";
std::string filever = VER_FILEVERSION_STR;
core::sqlsrv_add_assoc_string( *conn, return_value, extver, &filever[0], 1 /*duplicate*/ TSRMLS_CC );
}

catch( core::CoreException& ) {
Expand Down Expand Up @@ -978,7 +978,7 @@ PHP_FUNCTION( sqlsrv_query )
sqlsrv_malloc_auto_ptr<ss_sqlsrv_stmt> stmt;
char* sql = NULL;
hash_auto_ptr ss_stmt_options_ht;
int sql_len = 0;
size_t sql_len = 0;
zval* options_z = NULL;
zval* params_z = NULL;
zval stmt_z;
Expand Down Expand Up @@ -1026,7 +1026,7 @@ PHP_FUNCTION( sqlsrv_query )
bind_params( stmt TSRMLS_CC );

// execute the statement
core_sqlsrv_execute( stmt TSRMLS_CC, sql, sql_len );
core_sqlsrv_execute( stmt TSRMLS_CC, sql, static_cast<int>( sql_len ) );

// register the statement with the PHP runtime
ss::zend_register_resource(stmt_z, stmt, ss_sqlsrv_stmt::descriptor, ss_sqlsrv_stmt::resource_name TSRMLS_CC);
Expand Down Expand Up @@ -1112,14 +1112,12 @@ void sqlsrv_conn_close_stmts( ss_sqlsrv_conn* conn TSRMLS_DC )
// delete the statement by deleting it from Zend's resource list, which will force its destruction
stmt->conn = NULL;

try {
// this would call the destructor on the statement.
int zr = zend_list_close(Z_RES_P(rsrc_ptr));
}
catch( core::CoreException& ) {
LOG(SEV_ERROR, "Failed to remove statement resource %1!d! when closing the connection", Z_RES_HANDLE_P(rsrc_ptr));
}
} ZEND_HASH_FOREACH_END();
// this would call the destructor on the statement.
// There's nothing we can do if the removal fails, so we just log it and move on.
if( zend_list_close( Z_RES_P(rsrc_ptr) ) == FAILURE ) {
LOG(SEV_ERROR, "Failed to remove statement resource %1!d! when closing the connection", Z_RES_HANDLE_P(rsrc_ptr));
}
} ZEND_HASH_FOREACH_END();

zend_hash_destroy( conn->stmts );
FREE_HASHTABLE( conn->stmts );
Expand Down Expand Up @@ -1230,8 +1228,6 @@ void validate_stmt_options( sqlsrv_context& ctx, zval* stmt_options, _Inout_ Has
ZEND_HASH_FOREACH_KEY_VAL( options_ht, int_key, key, data ) {
int type = HASH_KEY_NON_EXISTENT;
size_t key_len = 0;
zval* conn_opt = NULL;
int result = 0;

type = key ? HASH_KEY_IS_STRING : HASH_KEY_IS_LONG;

Expand Down
23 changes: 16 additions & 7 deletions source/sqlsrv/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,11 @@ PHP_MINIT_FUNCTION(sqlsrv)
REGISTER_LONG_CONSTANT( "SQLSRV_PHPTYPE_FLOAT", SQLSRV_PHPTYPE_FLOAT, CONST_PERSISTENT | CONST_CS );
REGISTER_LONG_CONSTANT( "SQLSRV_PHPTYPE_DATETIME", SQLSRV_PHPTYPE_DATETIME, CONST_PERSISTENT | CONST_CS );

REGISTER_STRING_CONSTANT( "SQLSRV_ENC_BINARY", "binary", CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_ENC_CHAR", "char", CONST_PERSISTENT | CONST_CS );
std::string bin = "binary";
std::string chr = "char";

REGISTER_STRING_CONSTANT( "SQLSRV_ENC_BINARY", &bin[0], CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_ENC_CHAR", &chr[0], CONST_PERSISTENT | CONST_CS );

REGISTER_LONG_CONSTANT( "SQLSRV_NULLABLE_NO", 0, CONST_PERSISTENT | CONST_CS );
REGISTER_LONG_CONSTANT( "SQLSRV_NULLABLE_YES", 1, CONST_PERSISTENT | CONST_CS );
Expand Down Expand Up @@ -413,11 +416,17 @@ PHP_MINIT_FUNCTION(sqlsrv)
REGISTER_LONG_CONSTANT( "SQLSRV_SCROLL_ABSOLUTE", SQL_FETCH_ABSOLUTE, CONST_PERSISTENT | CONST_CS );
REGISTER_LONG_CONSTANT( "SQLSRV_SCROLL_RELATIVE", SQL_FETCH_RELATIVE, CONST_PERSISTENT | CONST_CS );

REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_FORWARD", "forward", CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_STATIC", "static", CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_DYNAMIC", "dynamic", CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_KEYSET", "keyset", CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_CLIENT_BUFFERED", "buffered", CONST_PERSISTENT | CONST_CS );
std::string fwd = "forward";
std::string stc = "static";
std::string dyn = "dynamic";
std::string key = "keyset";
std::string buf = "buffered";

REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_FORWARD", &fwd[0], CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_STATIC", &stc[0], CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_DYNAMIC", &dyn[0], CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_KEYSET", &key[0], CONST_PERSISTENT | CONST_CS );
REGISTER_STRING_CONSTANT( "SQLSRV_CURSOR_CLIENT_BUFFERED", &buf[0], CONST_PERSISTENT | CONST_CS );

try {

Expand Down
66 changes: 34 additions & 32 deletions source/sqlsrv/php_sqlsrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ OACR_WARNING_DISABLE( REALLOCLEAK, "Third party code." )

extern "C" {

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning( disable: 4005 4100 4127 4142 4244 4505 4530 )
#endif

#ifdef ZTS
#include "TSRM.h"
Expand All @@ -65,13 +67,9 @@ typedef int socklen_t;
#define HAVE_SOCKLEN_T
#endif

#include "php.h"
#include "php_globals.h"
#include "php_ini.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/info.h"

#if defined(_MSC_VER)
#pragma warning(pop)
#endif

#if PHP_MAJOR_VERSION < 7
#error Trying to compile "Microsoft Drivers for PHP for SQL Server (SQLSRV Driver)" with an unsupported version of PHP
Expand Down Expand Up @@ -103,6 +101,8 @@ extern sqlsrv_context* g_henv_ncp;

extern OSVERSIONINFO g_osversion; // used to determine which OS we're running in

#define phpext_sqlsrv_ptr &g_sqlsrv_module_entry

// module initialization
PHP_MINIT_FUNCTION(sqlsrv);
// module shutdown function
Expand Down Expand Up @@ -373,7 +373,7 @@ unsigned int convert_string_from_default_encoding( unsigned int php_encoding, ch
// create a wide char string from the passed in mbcs string. NULL is returned if the string
// could not be created. No error is posted by this function. utf16_len is the number of
// wchar_t characters, not the number of bytes.
wchar_t* utf16_string_from_mbcs_string( unsigned int php_encoding, const char* mbcs_string,
SQLWCHAR* utf16_string_from_mbcs_string( unsigned int php_encoding, const char* mbcs_string,
unsigned int mbcs_len, _Out_ unsigned int* utf16_len );

// *** internal error macros and functions ***
Expand Down Expand Up @@ -407,7 +407,7 @@ inline void reset_errors( TSRMLS_D )
}

#define THROW_SS_ERROR( ctx, error_code, ... ) \
(void)call_error_handler( ctx, error_code TSRMLS_CC, false /*warning*/, __VA_ARGS__ ); \
(void)call_error_handler( ctx, error_code TSRMLS_CC, false /*warning*/, ## __VA_ARGS__ ); \
throw ss::SSException();


Expand Down Expand Up @@ -475,6 +475,31 @@ enum logging_subsystems {
LOG_ALL = -1,
};

//*********************************************************************************************************************************
// Common function wrappers
// have to place this namespace before the utility functions
// otherwise can't compile in Linux because 'ss' not defined
//*********************************************************************************************************************************
namespace ss {

// an error which occurred in our SQLSRV driver
struct SSException : public core::CoreException {

SSException()
{
}
};

inline void zend_register_resource(_Out_ zval& rsrc_result, void* rsrc_pointer, int rsrc_type, char* rsrc_name TSRMLS_DC)
{
int zr = (NULL != (Z_RES(rsrc_result) = ::zend_register_resource(rsrc_pointer, rsrc_type)) ? SUCCESS : FAILURE);
CHECK_CUSTOM_ERROR(( zr == FAILURE ), reinterpret_cast<sqlsrv_context*>( rsrc_pointer ), SS_SQLSRV_ERROR_REGISTER_RESOURCE,
rsrc_name ) {
throw ss::SSException();
}
Z_TYPE_INFO(rsrc_result) = IS_RESOURCE_EX;
}
} // namespace ss

//*********************************************************************************************************************************
// Utility Functions
Expand Down Expand Up @@ -519,7 +544,7 @@ inline H* process_params( INTERNAL_FUNCTION_PARAMETERS, char const* param_spec,
int result = SUCCESS;

// dummy context to pass to the error handler
sqlsrv_context error_ctx( 0, ss_error_handler, NULL );;
sqlsrv_context error_ctx( 0, ss_error_handler, NULL );
error_ctx.set_func( calling_func );

switch( param_count ) {
Expand Down Expand Up @@ -591,28 +616,5 @@ inline H* process_params( INTERNAL_FUNCTION_PARAMETERS, char const* param_spec,
DIE( "%1!s!: Unknown exception caught in process_params.", calling_func );
}
}
//*********************************************************************************************************************************
// Common function wrappers
//*********************************************************************************************************************************
namespace ss {

// an error which occurred in our SQLSRV driver
struct SSException : public core::CoreException {

SSException()
{
}
};

inline void zend_register_resource(_Out_ zval& rsrc_result, void* rsrc_pointer, int rsrc_type, char* rsrc_name TSRMLS_DC)
{
int zr = (NULL != (Z_RES(rsrc_result) = ::zend_register_resource(rsrc_pointer, rsrc_type)) ? SUCCESS : FAILURE);
CHECK_CUSTOM_ERROR(( zr == FAILURE ), reinterpret_cast<sqlsrv_context*>( rsrc_pointer ), SS_SQLSRV_ERROR_REGISTER_RESOURCE,
rsrc_name ) {
throw ss::SSException();
}
Z_TYPE_INFO(rsrc_result) = IS_RESOURCE_EX;
}
} // namespace ss

#endif /* PHP_SQLSRV_H */
Loading

0 comments on commit 88a2b05

Please sign in to comment.