diff options
author | Javier Fernandez <jfernandez@igalia.com> | 2013-06-03 23:25:45 +0000 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2013-07-10 14:08:45 +0100 |
commit | 3e1638aae16b448562f78eec2a7ee5542b98671f (patch) | |
tree | 2a7f71acbceb107aa1532f46e57b262d444df33d | |
parent | cc228a3fcdbcdd476cdbe8b699d9b7bb3b0445d2 (diff) |
Dealing with parameters in Prepared Statement.
Change-Id: I1f94b7b4795c4a72ef8bf78bb032097094cda589
-rw-r--r-- | connectivity/source/drivers/firebird/FDatabaseMetaData.cxx | 7 | ||||
-rw-r--r-- | connectivity/source/drivers/firebird/FPreparedStatement.cxx | 83 |
2 files changed, 77 insertions, 13 deletions
diff --git a/connectivity/source/drivers/firebird/FDatabaseMetaData.cxx b/connectivity/source/drivers/firebird/FDatabaseMetaData.cxx index 367a4cde7e9e..b20ec5463ab4 100644 --- a/connectivity/source/drivers/firebird/FDatabaseMetaData.cxx +++ b/connectivity/source/drivers/firebird/FDatabaseMetaData.cxx @@ -865,7 +865,6 @@ Reference< XResultSet > SAL_CALL ODatabaseMetaData::getTables( ODatabaseMetaDataResultSet* pResultSet = new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables); Reference< XResultSet > xResultSet = pResultSet; - /* Reference< XPreparedStatement > statement = m_pConnection->prepareStatement( "SELECT " "'schema' as schema, RDB$RELATION_NAME, RDB$SYSTEM_FLAG, RDB$RELATION_TYPE, 'description' as description " // avoid duplicates @@ -873,12 +872,6 @@ Reference< XResultSet > SAL_CALL ODatabaseMetaData::getTables( "WHERE (RDB$RELATION_TYPE = 0 OR RDB$RELATION_TYPE = 1) " "AND 'schema' LIKE ? " "AND RDB$RELATION_NAME LIKE ? "); - */ - Reference< XPreparedStatement > statement = m_pConnection->prepareStatement( - "SELECT " - "'schema' as schema, RDB$RELATION_NAME, RDB$SYSTEM_FLAG, RDB$RELATION_TYPE, 'description' as description " // avoid duplicates - "FROM RDB$RELATIONS " - "WHERE (RDB$RELATION_TYPE = 0 OR RDB$RELATION_TYPE = 1) "); printf("DEBUG !!! connectivity.firebird => ODatabaseMetaData::getTables() Setting query parameters. \n"); diff --git a/connectivity/source/drivers/firebird/FPreparedStatement.cxx b/connectivity/source/drivers/firebird/FPreparedStatement.cxx index bdd20ab66043..3e058aa46b24 100644 --- a/connectivity/source/drivers/firebird/FPreparedStatement.cxx +++ b/connectivity/source/drivers/firebird/FPreparedStatement.cxx @@ -93,6 +93,11 @@ OPreparedStatement::OPreparedStatement( OConnection* _pConnection,const TTypeInf m_OUTsqlda->version = SQLDA_VERSION1; m_OUTsqlda->sqln = 10; + // enabling the XSQLDA to accommodate up to 10 parameter items (DEFAULT) + m_INsqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(10)); + m_INsqlda->version = SQLDA_VERSION1; + m_INsqlda->sqln = 10; + m_STMTHandler = NULL; // Set handle to NULL before allocation. if (isc_dsql_allocate_statement(status, &db, &m_STMTHandler)) if (pr_error(status, "allocate statement")) @@ -105,11 +110,42 @@ OPreparedStatement::OPreparedStatement( OConnection* _pConnection,const TTypeInf // sets the statement handle (stmt) to refer to the parsed format. char *sqlStr = strdup(OUStringToOString( m_sSqlStatement, RTL_TEXTENCODING_UTF8 ).getStr()); - if (isc_dsql_prepare(status, &m_TRANSHandler, &m_STMTHandler, 0, sqlStr, 1, NULL)) + if (isc_dsql_prepare(status, &m_TRANSHandler, &m_STMTHandler, 0, sqlStr, 1, m_OUTsqlda)) if (pr_error(status, "prepare statement")) return; free(sqlStr); + + // fill the input XSQLDA with information about the parameters + if (isc_dsql_describe_bind(status, &m_STMTHandler, 1, m_INsqlda)) + if (pr_error(status, "bind statement")) + return; + + XSQLVAR *var = NULL; + int i, dtype; + + // determine if the input descriptor can accommodate the number of parameters + // contained in the statement. + if (0 == m_INsqlda->sqld) + { + free(m_INsqlda); + m_INsqlda = NULL; + } + else + { + if (m_INsqlda->sqld > m_INsqlda->sqln) + { + int n = m_INsqlda->sqld; + free(m_INsqlda); + m_INsqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(n)); + m_INsqlda->sqln = n; + m_INsqlda->version = SQLDA_VERSION1; + if (isc_dsql_describe_bind(status, &m_STMTHandler, 1, m_INsqlda)) + if (pr_error(status, "bind statement 2")) + return; + } + } + // fill the output XSQLDA with information about the select-list items. if (isc_dsql_describe(status, &m_STMTHandler, 1, m_OUTsqlda)) if (pr_error(status, "describe statement")) @@ -129,8 +165,7 @@ OPreparedStatement::OPreparedStatement( OConnection* _pConnection,const TTypeInf return; } - XSQLVAR *var = NULL; - int i, dtype; + // Process each XSQLVAR parameter structure in the output XSQLDA for (i=0, var = m_OUTsqlda->sqlvar; i < m_OUTsqlda->sqld; i++, var++) { dtype = (var->sqltype & ~1); /* drop flag bit for now */ @@ -264,6 +299,33 @@ void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const ::r { ::osl::MutexGuard aGuard( m_aMutex ); checkDisposed(OStatement_BASE::rBHelper.bDisposed); + + if (NULL == m_INsqlda) + { + printf ("DEBUG !!! OPreparedStatement::setString => The query has not input parameters \n."); + return; + } + + OString str = OUStringToOString(x , RTL_TEXTENCODING_UTF8 ); + printf ("DEBUG !!! OPreparedStatement::setString => Setting parameter %i as: %s \n.", + parameterIndex, str.getStr()); + + XSQLVAR *var = m_INsqlda->sqlvar + (parameterIndex - 1); + + int dtype = (var->sqltype & ~1); // drop flag bit for now + switch(dtype) { + case SQL_VARYING: + var->sqltype = SQL_TEXT; + case SQL_TEXT: + var->sqllen = str.getLength(); + var->sqldata = (char *)malloc(sizeof(char)*var->sqllen); + sprintf(var->sqldata , "%s", str.getStr()); + break; + default: + OSL_ASSERT( false ); + } + + sprintf(var->sqldata , "%s", OUStringToOString(x , RTL_TEXTENCODING_UTF8 ).getStr()); } // ------------------------------------------------------------------------- @@ -286,9 +348,18 @@ Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery( ) throw(SQLE checkDisposed(OStatement_BASE::rBHelper.bDisposed); ISC_STATUS_ARRAY status; /* status vector */ - if (isc_dsql_execute(status, &m_TRANSHandler, &m_STMTHandler, 1, NULL)) - if (pr_error(status, "execute query")) - return NULL; + if (NULL != m_INsqlda) + { + if (isc_dsql_execute2(status, &m_TRANSHandler, &m_STMTHandler, 1, m_INsqlda, NULL)) + if (pr_error(status, "execute2 query")) + return NULL; + } + else + { + if (isc_dsql_execute(status, &m_TRANSHandler, &m_STMTHandler, 1, NULL)) + if (pr_error(status, "execute query")) + return NULL; + } Reference< OResultSet > pResult( new OResultSet( this) ); //initializeResultSet( pResult.get() ); |