From 5f2a14c8e92cd14d9c062cd8094da82acdd63e7a Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Tue, 18 May 2021 16:56:49 -0700 Subject: [PATCH] Cleaned up redundant code (#1260) --- source/pdo_sqlsrv/pdo_dbh.cpp | 8 +++---- source/pdo_sqlsrv/pdo_stmt.cpp | 14 +++++------ source/shared/core_stmt.cpp | 21 +++-------------- source/sqlsrv/conn.cpp | 43 ++++++++++------------------------ 4 files changed, 26 insertions(+), 60 deletions(-) diff --git a/source/pdo_sqlsrv/pdo_dbh.cpp b/source/pdo_sqlsrv/pdo_dbh.cpp index f88b2f53..e5cbe0a3 100644 --- a/source/pdo_sqlsrv/pdo_dbh.cpp +++ b/source/pdo_sqlsrv/pdo_dbh.cpp @@ -1049,7 +1049,7 @@ int pdo_sqlsrv_dbh_set_attr( _Inout_ pdo_dbh_t *dbh, _In_ zend_long attr, _Inout break; case SQLSRV_ATTR_DIRECT_QUERY: - driver_dbh->direct_query = ( zend_is_true( val ) ) ? true : false; + driver_dbh->direct_query = zend_is_true(val); break; case SQLSRV_ATTR_QUERY_TIMEOUT: @@ -1069,15 +1069,15 @@ int pdo_sqlsrv_dbh_set_attr( _Inout_ pdo_dbh_t *dbh, _In_ zend_long attr, _Inout break; case SQLSRV_ATTR_FETCHES_NUMERIC_TYPE: - driver_dbh->fetch_numeric = (zend_is_true(val)) ? true : false; + driver_dbh->fetch_numeric = zend_is_true(val); break; case SQLSRV_ATTR_FETCHES_DATETIME_TYPE: - driver_dbh->fetch_datetime = (zend_is_true(val)) ? true : false; + driver_dbh->fetch_datetime = zend_is_true(val); break; case SQLSRV_ATTR_FORMAT_DECIMALS: - driver_dbh->format_decimals = (zend_is_true(val)) ? true : false; + driver_dbh->format_decimals = zend_is_true(val); break; case SQLSRV_ATTR_DECIMAL_PLACES: diff --git a/source/pdo_sqlsrv/pdo_stmt.cpp b/source/pdo_sqlsrv/pdo_stmt.cpp index b2a30798..36d9856d 100644 --- a/source/pdo_sqlsrv/pdo_stmt.cpp +++ b/source/pdo_sqlsrv/pdo_stmt.cpp @@ -315,7 +315,7 @@ void stmt_option_encoding:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option c void stmt_option_direct_query:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /*opt*/, _In_ zval* value_z ) { pdo_sqlsrv_stmt *pdo_stmt = static_cast( stmt ); - pdo_stmt->direct_query = ( zend_is_true( value_z )) ? true : false; + pdo_stmt->direct_query = zend_is_true(value_z); } void stmt_option_cursor_scroll_type:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /*opt*/, _In_ zval* value_z ) @@ -332,13 +332,13 @@ void stmt_option_emulate_prepares:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_ void stmt_option_fetch_numeric:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /*opt*/, _In_ zval* value_z ) { pdo_sqlsrv_stmt *pdo_stmt = static_cast( stmt ); - pdo_stmt->fetch_numeric = ( zend_is_true( value_z )) ? true : false; + pdo_stmt->fetch_numeric = zend_is_true(value_z); } void stmt_option_fetch_datetime:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /*opt*/, _In_ zval* value_z ) { pdo_sqlsrv_stmt *pdo_stmt = static_cast( stmt ); - pdo_stmt->fetch_datetime = ( zend_is_true( value_z )) ? true : false; + pdo_stmt->fetch_datetime = zend_is_true(value_z); } // log a function entry point @@ -880,15 +880,15 @@ int pdo_sqlsrv_stmt_set_attr( _Inout_ pdo_stmt_t *stmt, _In_ zend_long attr, _In break; case SQLSRV_ATTR_FETCHES_NUMERIC_TYPE: - driver_stmt->fetch_numeric = ( zend_is_true( val )) ? true : false; + driver_stmt->fetch_numeric = zend_is_true(val); break; case SQLSRV_ATTR_FETCHES_DATETIME_TYPE: - driver_stmt->fetch_datetime = ( zend_is_true( val )) ? true : false; + driver_stmt->fetch_datetime = zend_is_true(val); break; case SQLSRV_ATTR_FORMAT_DECIMALS: - driver_stmt->format_decimals = ( zend_is_true( val )) ? true : false; + driver_stmt->format_decimals = zend_is_true(val); break; case SQLSRV_ATTR_DECIMAL_PLACES: @@ -896,7 +896,7 @@ int pdo_sqlsrv_stmt_set_attr( _Inout_ pdo_stmt_t *stmt, _In_ zend_long attr, _In break; case SQLSRV_ATTR_DATA_CLASSIFICATION: - driver_stmt->data_classification = (zend_is_true(val)) ? true : false; + driver_stmt->data_classification = zend_is_true(val); break; default: diff --git a/source/shared/core_stmt.cpp b/source/shared/core_stmt.cpp index 6331a114..723868b9 100644 --- a/source/shared/core_stmt.cpp +++ b/source/shared/core_stmt.cpp @@ -1152,22 +1152,12 @@ void stmt_option_buffered_query_limit:: operator()( _Inout_ sqlsrv_stmt* stmt, s void stmt_option_date_as_string:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /**/, _In_ zval* value_z ) { - if (zend_is_true(value_z)) { - stmt->date_as_string = true; - } - else { - stmt->date_as_string = false; - } + stmt->date_as_string = zend_is_true(value_z); } void stmt_option_format_decimals:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /**/, _In_ zval* value_z ) { - if (zend_is_true(value_z)) { - stmt->format_decimals = true; - } - else { - stmt->format_decimals = false; - } + stmt->format_decimals = zend_is_true(value_z); } void stmt_option_decimal_places:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /**/, _In_ zval* value_z ) @@ -1177,12 +1167,7 @@ void stmt_option_decimal_places:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_op void stmt_option_data_classification:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_option const* /**/, _In_ zval* value_z ) { - if (zend_is_true(value_z)) { - stmt->data_classification = true; - } - else { - stmt->data_classification = false; - } + stmt->data_classification = zend_is_true(value_z); } // internal function to release the active stream. Called by each main API function diff --git a/source/sqlsrv/conn.cpp b/source/sqlsrv/conn.cpp index 0c2680cb..a6ae7a57 100644 --- a/source/sqlsrv/conn.cpp +++ b/source/sqlsrv/conn.cpp @@ -30,6 +30,8 @@ extern "C" { namespace { +const int MAX_CONN_VALSTRING_LEN = 256; + // current subsytem. defined for the CHECK_SQL_{ERROR|WARNING} macros unsigned int current_log_subsystem = LOG_CONN; @@ -38,13 +40,7 @@ struct date_as_string_func { static void func( connection_option const* /*option*/, _In_ zval* value, _Inout_ sqlsrv_conn* conn, std::string& /*conn_str*/ ) { ss_sqlsrv_conn* ss_conn = static_cast( conn ); - - if( zend_is_true( value )) { - ss_conn->date_as_string = true; - } - else { - ss_conn->date_as_string = false; - } + ss_conn->date_as_string = zend_is_true(value); } }; @@ -53,13 +49,7 @@ struct format_decimals_func static void func(connection_option const* /*option*/, _In_ zval* value, _Inout_ sqlsrv_conn* conn, std::string& /*conn_str*/) { ss_sqlsrv_conn* ss_conn = static_cast(conn); - - if (zend_is_true(value)) { - ss_conn->format_decimals = true; - } - else { - ss_conn->format_decimals = false; - } + ss_conn->format_decimals = zend_is_true(value); } }; @@ -117,17 +107,10 @@ struct bool_conn_str_func { static void func( _In_ connection_option const* option, _In_ zval* value, sqlsrv_conn* /*conn*/, _Out_ std::string& conn_str ) { - char const* val_str; - if( zend_is_true( value )) { - val_str = "yes"; - } - else { - val_str = "no"; - } - conn_str += option->odbc_name; - conn_str += "={"; - conn_str += val_str; - conn_str += "};"; + char temp_str[MAX_CONN_VALSTRING_LEN]; + + snprintf(temp_str, MAX_CONN_VALSTRING_LEN, "%s={%s};", option->odbc_name, (zend_is_true(value) ? "yes" : "no")); + conn_str += temp_str; } }; @@ -137,12 +120,10 @@ struct int_conn_str_func { { SQLSRV_ASSERT( Z_TYPE_P( value ) == IS_LONG, "An integer is expected for this keyword" ) - std::string val_str = std::to_string( Z_LVAL_P( value )); - - conn_str += option->odbc_name; - conn_str += "={"; - conn_str += val_str; - conn_str += "};"; + char temp_str[MAX_CONN_VALSTRING_LEN]; + + snprintf(temp_str, MAX_CONN_VALSTRING_LEN, "%s={%d};", option->odbc_name, Z_LVAL_P(value)); + conn_str += temp_str; } };