Skip to content

Commit 29f85ad

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 1ea9202 commit 29f85ad

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed
 

‎admin/check/check_database_inc.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383

8484
if ( db_is_mssql() ) {
8585

86+
check_print_test_warn_row(
87+
'PHP support for Microsoft SQL Server driver',
88+
'mssql' != config_get_global( 'db_type' ),
89+
array( false => "'mssql' driver is no longer supported in PHP >= 5.3, please use 'mssqlnative' instead" )
90+
);
91+
8692
$t_mssql_textsize = ini_get_number( 'mssql.textsize' );
8793
check_print_info_row(
8894
'php.ini directive: mssql.textsize',
@@ -106,7 +112,6 @@
106112
$t_mssql_textlimit == -1,
107113
array( false => 'The value of the mssql.textlimit directive is currently ' . htmlentities( $t_mssql_textlimit ) . '. You should set this value to -1 to prevent large text fields being truncated upon being read from the database.' )
108114
);
109-
110115
}
111116

112117
$t_database_hostname = config_get_global( 'hostname' );

‎admin/install.php

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

175184
$g_db = ADONewConnection( $f_db_type );
@@ -244,16 +253,17 @@ function InsertData( $p_table, $p_data ) {
244253
<?php print_test( 'Setting Database Type', '' !== $f_db_type, true, 'database type is blank?' )?>
245254

246255
<!-- Checking DB support-->
247-
<?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.' )?>
248-
249-
<?php print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' )?>
250-
<?php print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' )?>
251-
<?php print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' )?>
252256
<?php
257+
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.' );
258+
259+
print_test( 'Setting Database Username', '' !== $f_db_username, true, 'database username is blank' );
260+
print_test( 'Setting Database Password', '' !== $f_db_password, false, 'database password is blank' );
261+
print_test( 'Setting Database Name', '' !== $f_database_name, true, 'database name is blank' );
262+
253263
if( $f_db_type == 'db2' ) {
254264
print_test( 'Setting Database Schema', !is_blank( $f_db_schema ), true, 'must have a schema name for AS400 in the form of DBNAME/SCHEMA' );
255265
}
256-
?>
266+
?>
257267
<tr>
258268
<td bgcolor="#ffffff">
259269
Setting Admin Username
@@ -370,6 +380,7 @@ function InsertData( $p_table, $p_data ) {
370380
break;
371381
case 'pgsql':
372382
case 'mssql':
383+
case 'mssqlnative':
373384
case 'db2':
374385
default:
375386
break;
@@ -399,7 +410,6 @@ function InsertData( $p_table, $p_data ) {
399410
<span class="title"><?php echo $g_database_upgrade ? 'Upgrade Options' : 'Installation Options'?></span>
400411
</td>
401412
</tr>
402-
403413
<?php if( !$g_database_upgrade ) {?>
404414
<tr>
405415
<td>
@@ -408,41 +418,27 @@ function InsertData( $p_table, $p_data ) {
408418
<td>
409419
<select name="db_type">
410420
<?php
411-
if( $f_db_type == 'mysql' ) {
412-
echo '<option value="mysql" selected="selected">MySQL (default)</option>';
413-
} else {
414-
echo '<option value="mysql">MySQL (default)</option>';
415-
}
416-
417-
if( $f_db_type == 'mysqli' ) {
418-
echo '<option value="mysqli" selected="selected">MySQLi</option>';
419-
} else {
420-
echo '<option value="mysqli">MySQLi</option>';
421-
}
422-
423-
if( $f_db_type == 'mssql' ) {
424-
echo '<option value="mssql" selected="selected">Microsoft SQL Server</option>';
425-
} else {
426-
echo '<option value="mssql">Microsoft SQL Server</option>';
427-
}
428-
429-
if( $f_db_type == 'pgsql' ) {
430-
echo '<option value="pgsql" selected="selected">PostgreSQL</option>';
431-
} else {
432-
echo '<option value="pgsql">PostgreSQL</option>';
433-
}
434-
435-
if( $f_db_type == 'oci8' ) {
436-
echo '<option value="oci8" selected="selected">Oracle</option>';
437-
} else {
438-
echo '<option value="oci8">Oracle</option>';
439-
}
421+
// Build selection list of available DB types
422+
$t_db_list = array(
423+
'mysql' => 'MySQL (default)',
424+
'mysqli' => 'MySQLi',
425+
'mssql' => 'Microsoft SQL Server',
426+
'mssqlnative' => 'Microsoft SQL Server Native Driver',
427+
'pgsql' => 'PostgreSQL',
428+
'oci8' => 'Oracle',
429+
'db2' => 'IBM DB2',
430+
);
431+
432+
// mssql is not supported with PHP >= 5.3
433+
if( version_compare( phpversion(), '5.3' ) >= 0 ) {
434+
unset( $t_db_list['mssql']);
435+
}
440436

441-
if( $f_db_type == 'db2' ) {
442-
echo '<option value="db2" selected="selected">IBM DB2</option>';
443-
} else {
444-
echo '<option value="db2">IBM DB2</option>';
445-
}
437+
foreach( $t_db_list as $t_db => $t_db_descr ) {
438+
echo '<option value="' . $t_db . '"' .
439+
( $t_db == $f_db_type ? ' selected="selected"' : '' ) . '>' .
440+
$t_db_descr . '</option>';
441+
}
446442
?>
447443
</select>
448444
</td>

‎core/database_api.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function db_is_connected() {
140140
}
141141

142142
/**
143-
* Returns whether php supprot for a database is enabled
143+
* Returns whether php support for a database is enabled
144144
* @return bool indicating if php current supports the given database type
145145
*/
146146
function db_check_database_support( $p_db_type ) {
@@ -158,6 +158,9 @@ function db_check_database_support( $p_db_type ) {
158158
case 'mssql':
159159
$t_support = function_exists( 'mssql_connect' );
160160
break;
161+
case 'mssqlnative':
162+
$t_support = function_exists( 'sqlsrv_connect' );
163+
break;
161164
case 'oci8':
162165
$t_support = function_exists( 'OCILogon' );
163166
break;
@@ -208,13 +211,14 @@ function db_is_pgsql() {
208211

209212
/**
210213
* Checks if the database driver is MS SQL
211-
* @return bool true if postgres
214+
* @return bool true if mssql
212215
*/
213216
function db_is_mssql() {
214217
$t_db_type = config_get_global( 'db_type' );
215218

216219
switch( $t_db_type ) {
217220
case 'mssql':
221+
case 'mssqlnative':
218222
case 'odbc_mssql':
219223
return true;
220224
}
@@ -293,9 +297,11 @@ function db_query( $p_query, $p_limit = -1, $p_offset = -1 ) {
293297
function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = -1 ) {
294298
global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param_count;
295299

300+
$t_db_type = config_get_global( 'db_type' );
301+
296302
static $s_check_params;
297303
if( $s_check_params === null ) {
298-
$s_check_params = ( db_is_pgsql() || config_get_global( 'db_type' ) == 'odbc_mssql' );
304+
$s_check_params = ( db_is_pgsql() || $t_db_type == 'odbc_mssql' || $t_db_type == 'mssqlnative');
299305
}
300306

301307
$t_start = microtime(true);
@@ -306,6 +312,9 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
306312
if( $arr_parms[$i] === false ) {
307313
$arr_parms[$i] = 0;
308314
}
315+
elseif( $arr_parms[$i] === true && $t_db_type == 'mssqlnative' ) {
316+
$arr_parms[$i] = 1;
317+
}
309318
}
310319
}
311320

@@ -318,7 +327,6 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
318327
$t_elapsed = number_format( microtime(true) - $t_start, 4 );
319328

320329
if( ON == $g_db_log_queries ) {
321-
$t_db_type = config_get_global( 'db_type' );
322330
$lastoffset = 0;
323331
$i = 1;
324332
if( !( is_null( $arr_parms ) || empty( $arr_parms ) ) ) {
@@ -424,7 +432,7 @@ function db_fetch_array( &$p_result ) {
424432
}
425433

426434
# mysql obeys FETCH_MODE_BOTH, hence ->fields works, other drivers do not support this
427-
if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' ) {
435+
if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' || $g_db_type == 'mssqlnative' ) {
428436
$t_array = $p_result->fields;
429437
$p_result->MoveNext();
430438
return $t_array;
@@ -518,6 +526,11 @@ function db_insert_id( $p_table = null, $p_field = "id" ) {
518526
$result = db_query_bound( $query );
519527
return db_result( $result );
520528
}
529+
if( db_is_mssql() ) {
530+
$query = "SELECT IDENT_CURRENT('$p_table')";
531+
$result = db_query_bound( $query );
532+
return db_result( $result );
533+
}
521534
return $g_db->Insert_ID();
522535
}
523536

@@ -651,6 +664,7 @@ function db_prepare_string( $p_string ) {
651664

652665
switch( $t_db_type ) {
653666
case 'mssql':
667+
case 'mssqlnative':
654668
case 'odbc_mssql':
655669
case 'ado_mssql':
656670
if( ini_get( 'magic_quotes_sybase' ) ) {
@@ -698,6 +712,7 @@ function db_prepare_binary_string( $p_string ) {
698712

699713
switch( $t_db_type ) {
700714
case 'mssql':
715+
case 'mssqlnative':
701716
case 'odbc_mssql':
702717
case 'ado_mssql':
703718
$content = unpack( "H*hex", $p_string );

0 commit comments

Comments
 (0)
Please sign in to comment.