Permalink
Browse files

Add support for SQL Server Native driver

Since PHP 5.3 has been released, as per PHP documentation the mssql
driver is no longer available on Windows. Microsoft has published a
new SQL Server Native Driver.

This commits adds support for this new driver (sqlsrv) to MantisBT
as mssqlnative db type.

Thanks to genius_p and tomkraw1 for the original patch and testing.

Fixes #11776
  • Loading branch information...
1 parent eacd840 commit 88274c93944b1ebd88e5ad8f489d9cf784019929 @dregad dregad committed Jan 21, 2012
Showing with 66 additions and 48 deletions.
  1. +8 −0 admin/check.php
  2. +38 −42 admin/install.php
  3. +20 −6 core/database_api.php
View
@@ -277,13 +277,21 @@ function test_database_utf8() {
}
if ( db_is_mssql() ) {
+ if( 'mssql' == config_get_global( 'db_type' ) ) {
+ print_test_warn_row( 'Checking PHP support for Microsoft SQL Server driver',
+ version_compare( phpversion(), '5.3' ) < 0,
+ "'mssql' driver is no longer supported in PHP >= 5.3, please use 'mssqlnative' instead"
+ );
+ }
+
if ( print_test_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textsize' ) != 4096, ini_get( 'mssql.textsize' ) ) ) {
print_test_warn_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textsize' ) == 2147483647, ini_get( 'mssql.textsize' ) );
}
if ( print_test_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textlimit' ) != 4096 , ini_get( 'mssql.textlimit' ) ) ) {
print_test_warn_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textsize' ) == 2147483647, ini_get( 'mssql.textsize' ) );
}
}
+
print_test_row( 'check variables_order includes GPCS', stristr( ini_get( 'variables_order' ), 'G' ) && stristr( ini_get( 'variables_order' ), 'P' ) && stristr( ini_get( 'variables_order' ), 'C' ) && stristr( ini_get( 'variables_order' ), 'S' ), ini_get( 'variables_order' ) );
View
@@ -166,7 +166,16 @@ function InsertData( $p_table, $p_data ) {
if( 0 == $t_install_state ) {
print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' );
print_test( 'Checking Database connection settings exist', ( $f_dsn !== '' || ( $f_database_name !== '' && $f_db_username !== '' && $f_hostname !== '' ) ), true, 'database connection settings do not exist?' );
- print_test( 'Checking PHP support for database type', db_check_database_support( $f_db_type ), true, 'database is not supported by PHP. Check that it has been compiled into your server.' );
+ print_test( 'Checking PHP support for database type',
+ db_check_database_support( $f_db_type ), true,
+ 'database is not supported by PHP. Check that it has been compiled into your server.'
+ );
+ if( $f_db_type == 'mssql' ) {
+ print_test( 'Checking PHP support for Microsoft SQL Server driver',
+ version_compare( phpversion(), '5.3' ) < 0, true,
+ 'mssql driver is no longer supported in PHP >= 5.3, please use mssqlnative instead'
+ );
+ }
}
$g_db = ADONewConnection( $f_db_type );
@@ -241,16 +250,17 @@ function InsertData( $p_table, $p_data ) {
<?php print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' )?>
<!-- Checking DB support-->
-<?php print_test( 'Checking PHP support for database type', db_check_database_support( $f_db_type ), true, 'database is not supported by PHP. Check that it has been compiled into your server.' )?>
-
-<?php print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' )?>
-<?php print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' )?>
-<?php print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' )?>
<?php
+ print_test( 'Checking PHP support for database type', db_check_database_support( $f_db_type ), true, 'database is not supported by PHP. Check that it has been compiled into your server.' );
+
+ print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' );
+ print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' );
+ print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' );
+
if( $f_db_type == 'db2' ) {
print_test( 'Setting Database Schema', !is_blank( $f_db_schema ), true, 'must have a schema name for AS400 in the form of DBNAME/SCHEMA' );
}
- ?>
+?>
<tr>
<td bgcolor="#ffffff">
Setting Admin Username
@@ -367,6 +377,7 @@ function InsertData( $p_table, $p_data ) {
break;
case 'pgsql':
case 'mssql':
+ case 'mssqlnative':
case 'db2':
default:
break;
@@ -394,7 +405,6 @@ function InsertData( $p_table, $p_data ) {
<span class="title"><?php echo $g_database_upgrade ? 'Upgrade Options' : 'Installation Options'?></span>
</td>
</tr>
-
<?php if( !$g_database_upgrade ) {?>
<tr>
<td>
@@ -403,41 +413,27 @@ function InsertData( $p_table, $p_data ) {
<td>
<select name="db_type">
<?php
- if( $f_db_type == 'mysql' ) {
- echo '<option value="mysql" selected="selected">MySQL (default)</option>';
- } else {
- echo '<option value="mysql">MySQL (default)</option>';
- }
-
- if( $f_db_type == 'mysqli' ) {
- echo '<option value="mysqli" selected="selected">MySQLi</option>';
- } else {
- echo '<option value="mysqli">MySQLi</option>';
- }
-
- if( $f_db_type == 'mssql' ) {
- echo '<option value="mssql" selected="selected">Microsoft SQL Server</option>';
- } else {
- echo '<option value="mssql">Microsoft SQL Server</option>';
- }
-
- if( $f_db_type == 'pgsql' ) {
- echo '<option value="pgsql" selected="selected">PostgreSQL</option>';
- } else {
- echo '<option value="pgsql">PostgreSQL</option>';
- }
-
- if( $f_db_type == 'oci8' ) {
- echo '<option value="oci8" selected="selected">Oracle</option>';
- } else {
- echo '<option value="oci8">Oracle</option>';
- }
+ // Build selection list of available DB types
+ $t_db_list = array(
+ 'mysql' => 'MySQL (default)',
+ 'mysqli' => 'MySQLi',
+ 'mssql' => 'Microsoft SQL Server',
+ 'mssqlnative' => 'Microsoft SQL Server Native Driver',
+ 'pgsql' => 'PostgreSQL',
+ 'oci8' => 'Oracle',
+ 'db2' => 'IBM DB2',
+ );
+
+ // mssql is not supported with PHP >= 5.3
+ if( version_compare( phpversion(), '5.3' ) >= 0 ) {
+ unset( $t_db_list['mssql']);
+ }
- if( $f_db_type == 'db2' ) {
- echo '<option value="db2" selected="selected">IBM DB2</option>';
- } else {
- echo '<option value="db2">IBM DB2</option>';
- }
+ foreach( $t_db_list as $t_db => $t_db_descr ) {
+ echo '<option value="' . $t_db . '"' .
+ ( $t_db == $f_db_type ? ' selected="selected"' : '' ) . '>' .
+ $t_db_descr . '</option>';
+ }
?>
</select>
</td>
View
@@ -134,7 +134,7 @@ function db_is_connected() {
}
/**
- * Returns whether php supprot for a database is enabled
+ * Returns whether php support for a database is enabled
* @return bool indicating if php current supports the given database type
*/
function db_check_database_support( $p_db_type ) {
@@ -152,6 +152,9 @@ function db_check_database_support( $p_db_type ) {
case 'mssql':
$t_support = function_exists( 'mssql_connect' );
break;
+ case 'mssqlnative':
+ $t_support = function_exists( 'sqlsrv_connect' );
+ break;
case 'oci8':
$t_support = function_exists( 'OCILogon' );
break;
@@ -203,13 +206,14 @@ function db_is_pgsql() {
/**
* Checks if the database driver is MS SQL
- * @return bool true if postgres
+ * @return bool true if mssql
*/
function db_is_mssql() {
$t_db_type = config_get_global( 'db_type' );
switch( $t_db_type ) {
case 'mssql':
+ case 'mssqlnative':
case 'odbc_mssql':
return true;
}
@@ -302,14 +306,14 @@ function db_query( $p_query, $p_limit = -1, $p_offset = -1 ) {
function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = -1 ) {
global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param_count;
+ $t_db_type = config_get_global( 'db_type' );
+
static $s_check_params;
if( $s_check_params === null ) {
- $s_check_params = ( db_is_pgsql() || config_get_global( 'db_type' ) == 'odbc_mssql' );
+ $s_check_params = ( db_is_pgsql() || $t_db_type == 'odbc_mssql' || $t_db_type == 'mssqlnative');
}
if( ON == $g_db_log_queries ) {
- $t_db_type = config_get_global( 'db_type' );
-
$t_start = microtime(true);
$t_backtrace = debug_backtrace();
@@ -331,6 +335,9 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
if( $arr_parms[$i] === false ) {
$arr_parms[$i] = 0;
}
+ elseif( $arr_parms[$i] === true && $t_db_type == 'mssqlnative' ) {
+ $arr_parms[$i] = 1;
+ }
}
}
@@ -448,7 +455,7 @@ function db_fetch_array( &$p_result ) {
}
# mysql obeys FETCH_MODE_BOTH, hence ->fields works, other drivers do not support this
- if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' ) {
+ if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' || $g_db_type == 'mssqlnative' ) {
$t_array = $p_result->fields;
$p_result->MoveNext();
return $t_array;
@@ -542,6 +549,11 @@ function db_insert_id( $p_table = null, $p_field = "id" ) {
$result = db_query_bound( $query );
return db_result( $result );
}
+ if( db_is_mssql() ) {
+ $query = "SELECT IDENT_CURRENT('$p_table')";
+ $result = db_query_bound( $query );
+ return db_result( $result );
+ }
return $g_db->Insert_ID();
}
@@ -682,6 +694,7 @@ function db_prepare_string( $p_string ) {
switch( $t_db_type ) {
case 'mssql':
+ case 'mssqlnative':
case 'odbc_mssql':
case 'ado_mssql':
if( ini_get( 'magic_quotes_sybase' ) ) {
@@ -729,6 +742,7 @@ function db_prepare_binary_string( $p_string ) {
switch( $t_db_type ) {
case 'mssql':
+ case 'mssqlnative':
case 'odbc_mssql':
case 'ado_mssql':
$content = unpack( "H*hex", $p_string );

0 comments on commit 88274c9

Please sign in to comment.