Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 88274c9

Browse files
committedJan 21, 2012
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
1 parent eacd840 commit 88274c9

File tree

3 files changed

+66
-48
lines changed

3 files changed

+66
-48
lines changed
 

‎admin/check.php

+8
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,21 @@ function test_database_utf8() {
277277
}
278278

279279
if ( db_is_mssql() ) {
280+
if( 'mssql' == config_get_global( 'db_type' ) ) {
281+
print_test_warn_row( 'Checking PHP support for Microsoft SQL Server driver',
282+
version_compare( phpversion(), '5.3' ) < 0,
283+
"'mssql' driver is no longer supported in PHP >= 5.3, please use 'mssqlnative' instead"
284+
);
285+
}
286+
280287
if ( print_test_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textsize' ) != 4096, ini_get( 'mssql.textsize' ) ) ) {
281288
print_test_warn_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textsize' ) == 2147483647, ini_get( 'mssql.textsize' ) );
282289
}
283290
if ( print_test_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textlimit' ) != 4096 , ini_get( 'mssql.textlimit' ) ) ) {
284291
print_test_warn_row( 'check mssql textsize in php.ini...', ini_get( 'mssql.textsize' ) == 2147483647, ini_get( 'mssql.textsize' ) );
285292
}
286293
}
294+
287295
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' ) );
288296

289297

‎admin/install.php

+38-42
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,16 @@ function InsertData( $p_table, $p_data ) {
166166
if( 0 == $t_install_state ) {
167167
print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' );
168168
print_test( 'Checking Database connection settings exist', ( $f_dsn !== '' || ( $f_database_name !== '' && $f_db_username !== '' && $f_hostname !== '' ) ), true, 'database connection settings do not exist?' );
169-
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.' );
169+
print_test( 'Checking PHP support for database type',
170+
db_check_database_support( $f_db_type ), true,
171+
'database is not supported by PHP. Check that it has been compiled into your server.'
172+
);
173+
if( $f_db_type == 'mssql' ) {
174+
print_test( 'Checking PHP support for Microsoft SQL Server driver',
175+
version_compare( phpversion(), '5.3' ) < 0, true,
176+
'mssql driver is no longer supported in PHP >= 5.3, please use mssqlnative instead'
177+
);
178+
}
170179
}
171180

172181
$g_db = ADONewConnection( $f_db_type );
@@ -241,16 +250,17 @@ function InsertData( $p_table, $p_data ) {
241250
<?php print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' )?>
242251

243252
<!-- Checking DB support-->
244-
<?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.' )?>
245-
246-
<?php print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' )?>
247-
<?php print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' )?>
248-
<?php print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' )?>
249253
<?php
254+
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.' );
255+
256+
print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' );
257+
print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' );
258+
print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' );
259+
250260
if( $f_db_type == 'db2' ) {
251261
print_test( 'Setting Database Schema', !is_blank( $f_db_schema ), true, 'must have a schema name for AS400 in the form of DBNAME/SCHEMA' );
252262
}
253-
?>
263+
?>
254264
<tr>
255265
<td bgcolor="#ffffff">
256266
Setting Admin Username
@@ -367,6 +377,7 @@ function InsertData( $p_table, $p_data ) {
367377
break;
368378
case 'pgsql':
369379
case 'mssql':
380+
case 'mssqlnative':
370381
case 'db2':
371382
default:
372383
break;
@@ -394,7 +405,6 @@ function InsertData( $p_table, $p_data ) {
394405
<span class="title"><?php echo $g_database_upgrade ? 'Upgrade Options' : 'Installation Options'?></span>
395406
</td>
396407
</tr>
397-
398408
<?php if( !$g_database_upgrade ) {?>
399409
<tr>
400410
<td>
@@ -403,41 +413,27 @@ function InsertData( $p_table, $p_data ) {
403413
<td>
404414
<select name="db_type">
405415
<?php
406-
if( $f_db_type == 'mysql' ) {
407-
echo '<option value="mysql" selected="selected">MySQL (default)</option>';
408-
} else {
409-
echo '<option value="mysql">MySQL (default)</option>';
410-
}
411-
412-
if( $f_db_type == 'mysqli' ) {
413-
echo '<option value="mysqli" selected="selected">MySQLi</option>';
414-
} else {
415-
echo '<option value="mysqli">MySQLi</option>';
416-
}
417-
418-
if( $f_db_type == 'mssql' ) {
419-
echo '<option value="mssql" selected="selected">Microsoft SQL Server</option>';
420-
} else {
421-
echo '<option value="mssql">Microsoft SQL Server</option>';
422-
}
423-
424-
if( $f_db_type == 'pgsql' ) {
425-
echo '<option value="pgsql" selected="selected">PostgreSQL</option>';
426-
} else {
427-
echo '<option value="pgsql">PostgreSQL</option>';
428-
}
429-
430-
if( $f_db_type == 'oci8' ) {
431-
echo '<option value="oci8" selected="selected">Oracle</option>';
432-
} else {
433-
echo '<option value="oci8">Oracle</option>';
434-
}
416+
// Build selection list of available DB types
417+
$t_db_list = array(
418+
'mysql' => 'MySQL (default)',
419+
'mysqli' => 'MySQLi',
420+
'mssql' => 'Microsoft SQL Server',
421+
'mssqlnative' => 'Microsoft SQL Server Native Driver',
422+
'pgsql' => 'PostgreSQL',
423+
'oci8' => 'Oracle',
424+
'db2' => 'IBM DB2',
425+
);
426+
427+
// mssql is not supported with PHP >= 5.3
428+
if( version_compare( phpversion(), '5.3' ) >= 0 ) {
429+
unset( $t_db_list['mssql']);
430+
}
435431

436-
if( $f_db_type == 'db2' ) {
437-
echo '<option value="db2" selected="selected">IBM DB2</option>';
438-
} else {
439-
echo '<option value="db2">IBM DB2</option>';
440-
}
432+
foreach( $t_db_list as $t_db => $t_db_descr ) {
433+
echo '<option value="' . $t_db . '"' .
434+
( $t_db == $f_db_type ? ' selected="selected"' : '' ) . '>' .
435+
$t_db_descr . '</option>';
436+
}
441437
?>
442438
</select>
443439
</td>

‎core/database_api.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function db_is_connected() {
134134
}
135135

136136
/**
137-
* Returns whether php supprot for a database is enabled
137+
* Returns whether php support for a database is enabled
138138
* @return bool indicating if php current supports the given database type
139139
*/
140140
function db_check_database_support( $p_db_type ) {
@@ -152,6 +152,9 @@ function db_check_database_support( $p_db_type ) {
152152
case 'mssql':
153153
$t_support = function_exists( 'mssql_connect' );
154154
break;
155+
case 'mssqlnative':
156+
$t_support = function_exists( 'sqlsrv_connect' );
157+
break;
155158
case 'oci8':
156159
$t_support = function_exists( 'OCILogon' );
157160
break;
@@ -203,13 +206,14 @@ function db_is_pgsql() {
203206

204207
/**
205208
* Checks if the database driver is MS SQL
206-
* @return bool true if postgres
209+
* @return bool true if mssql
207210
*/
208211
function db_is_mssql() {
209212
$t_db_type = config_get_global( 'db_type' );
210213

211214
switch( $t_db_type ) {
212215
case 'mssql':
216+
case 'mssqlnative':
213217
case 'odbc_mssql':
214218
return true;
215219
}
@@ -302,14 +306,14 @@ function db_query( $p_query, $p_limit = -1, $p_offset = -1 ) {
302306
function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = -1 ) {
303307
global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param_count;
304308

309+
$t_db_type = config_get_global( 'db_type' );
310+
305311
static $s_check_params;
306312
if( $s_check_params === null ) {
307-
$s_check_params = ( db_is_pgsql() || config_get_global( 'db_type' ) == 'odbc_mssql' );
313+
$s_check_params = ( db_is_pgsql() || $t_db_type == 'odbc_mssql' || $t_db_type == 'mssqlnative');
308314
}
309315

310316
if( ON == $g_db_log_queries ) {
311-
$t_db_type = config_get_global( 'db_type' );
312-
313317
$t_start = microtime(true);
314318

315319
$t_backtrace = debug_backtrace();
@@ -331,6 +335,9 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
331335
if( $arr_parms[$i] === false ) {
332336
$arr_parms[$i] = 0;
333337
}
338+
elseif( $arr_parms[$i] === true && $t_db_type == 'mssqlnative' ) {
339+
$arr_parms[$i] = 1;
340+
}
334341
}
335342
}
336343

@@ -448,7 +455,7 @@ function db_fetch_array( &$p_result ) {
448455
}
449456

450457
# mysql obeys FETCH_MODE_BOTH, hence ->fields works, other drivers do not support this
451-
if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' ) {
458+
if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' || $g_db_type == 'mssqlnative' ) {
452459
$t_array = $p_result->fields;
453460
$p_result->MoveNext();
454461
return $t_array;
@@ -542,6 +549,11 @@ function db_insert_id( $p_table = null, $p_field = "id" ) {
542549
$result = db_query_bound( $query );
543550
return db_result( $result );
544551
}
552+
if( db_is_mssql() ) {
553+
$query = "SELECT IDENT_CURRENT('$p_table')";
554+
$result = db_query_bound( $query );
555+
return db_result( $result );
556+
}
545557
return $g_db->Insert_ID();
546558
}
547559

@@ -682,6 +694,7 @@ function db_prepare_string( $p_string ) {
682694

683695
switch( $t_db_type ) {
684696
case 'mssql':
697+
case 'mssqlnative':
685698
case 'odbc_mssql':
686699
case 'ado_mssql':
687700
if( ini_get( 'magic_quotes_sybase' ) ) {
@@ -729,6 +742,7 @@ function db_prepare_binary_string( $p_string ) {
729742

730743
switch( $t_db_type ) {
731744
case 'mssql':
745+
case 'mssqlnative':
732746
case 'odbc_mssql':
733747
case 'ado_mssql':
734748
$content = unpack( "H*hex", $p_string );

0 commit comments

Comments
 (0)
Please sign in to comment.