From da9fc621b4e6f35fb6775f411127d0a1594ce7fa Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Mon, 9 Jan 2012 12:35:20 +0200 Subject: [PATCH] Use Convert.ToBoolean to parse returned scalar from database when expecting boolean value --- .../TestRoundhousE/custom_db_create.sql | 2 ++ .../roundhouse/databases/DefaultDatabase.cs | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/db/SQLServer/TestRoundhousE/custom_db_create.sql b/db/SQLServer/TestRoundhousE/custom_db_create.sql index 29c82405..adec2f60 100644 --- a/db/SQLServer/TestRoundhousE/custom_db_create.sql +++ b/db/SQLServer/TestRoundhousE/custom_db_create.sql @@ -2,4 +2,6 @@ USE master IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{{DatabaseName}}') BEGIN CREATE DATABASE {{DatabaseName}} + -- signal success + SELECT 1 END \ No newline at end of file diff --git a/product/roundhouse/databases/DefaultDatabase.cs b/product/roundhouse/databases/DefaultDatabase.cs index bd0858fc..d86dce8c 100644 --- a/product/roundhouse/databases/DefaultDatabase.cs +++ b/product/roundhouse/databases/DefaultDatabase.cs @@ -103,22 +103,19 @@ public virtual bool create_database_if_it_doesnt_exist(string custom_create_data { foreach (var sql_statement in StatementSplitter.split_sql_on_regex_and_remove_empty_statements(create_script, sql_statement_separator_regex_pattern)) { - var return_value = run_sql_scalar(sql_statement, ConnectionType.Admin); //should only receive a return value once + var return_value = run_sql_scalar_boolean(sql_statement, ConnectionType.Admin); if (return_value != null) { - database_was_created = (bool)return_value; + database_was_created = return_value.Value; } } } else { - var return_value = run_sql_scalar(create_script, ConnectionType.Admin); //should only receive a return value once - if (return_value != null) - { - database_was_created = (bool)return_value; - } + var return_value = run_sql_scalar_boolean(create_script, ConnectionType.Admin); + database_was_created = return_value.GetValueOrDefault(false); } } catch (Exception ex) @@ -131,6 +128,16 @@ public virtual bool create_database_if_it_doesnt_exist(string custom_create_data return database_was_created; } + private bool? run_sql_scalar_boolean(string sql_to_run, ConnectionType connection_type) + { + var return_value = run_sql_scalar(sql_to_run, connection_type); + if (return_value != null && return_value != DBNull.Value) + { + return Convert.ToBoolean(return_value); + } + return null; + } + public void set_recovery_mode(bool simple) { try