Skip to content

Commit

Permalink
Add support for SQL Server Native driver
Browse files Browse the repository at this point in the history
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
dregad committed Jan 21, 2012
1 parent 1ea9202 commit 29f85ad
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
7 changes: 6 additions & 1 deletion admin/check/check_database_inc.php
Expand Up @@ -83,6 +83,12 @@

if ( db_is_mssql() ) {

check_print_test_warn_row(
'PHP support for Microsoft SQL Server driver',
'mssql' != config_get_global( 'db_type' ),
array( false => "'mssql' driver is no longer supported in PHP >= 5.3, please use 'mssqlnative' instead" )
);

$t_mssql_textsize = ini_get_number( 'mssql.textsize' );
check_print_info_row(
'php.ini directive: mssql.textsize',
Expand All @@ -106,7 +112,6 @@
$t_mssql_textlimit == -1,
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.' )
);

}

$t_database_hostname = config_get_global( 'hostname' );
Expand Down
80 changes: 38 additions & 42 deletions admin/install.php
Expand Up @@ -169,7 +169,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 );
Expand Down Expand Up @@ -244,16 +253,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
Expand Down Expand Up @@ -370,6 +380,7 @@ function InsertData( $p_table, $p_data ) {
break;
case 'pgsql':
case 'mssql':
case 'mssqlnative':
case 'db2':
default:
break;
Expand Down Expand Up @@ -399,7 +410,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>
Expand All @@ -408,41 +418,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>
Expand Down
25 changes: 20 additions & 5 deletions core/database_api.php
Expand Up @@ -140,7 +140,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 ) {
Expand All @@ -158,6 +158,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;
Expand Down Expand Up @@ -208,13 +211,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;
}
Expand Down Expand Up @@ -293,9 +297,11 @@ 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');
}

$t_start = microtime(true);
Expand All @@ -306,6 +312,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;
}
}
}

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

if( ON == $g_db_log_queries ) {
$t_db_type = config_get_global( 'db_type' );
$lastoffset = 0;
$i = 1;
if( !( is_null( $arr_parms ) || empty( $arr_parms ) ) ) {
Expand Down Expand Up @@ -424,7 +432,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;
Expand Down Expand Up @@ -518,6 +526,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();
}

Expand Down Expand Up @@ -651,6 +664,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' ) ) {
Expand Down Expand Up @@ -698,6 +712,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 );
Expand Down

0 comments on commit 29f85ad

Please sign in to comment.