diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2013-09-18 20:55:26 +0100 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2013-09-18 20:57:08 +0100 |
commit | 936cce1110233504307c9e95baafd1fe67d0fab1 (patch) | |
tree | cdacbb309cd992e1b5e71b1a048f4210112c07ac /connectivity | |
parent | e808cae99e009b1e1814b83ea2c34b09e25dc2cc (diff) |
Add some type checking to ResultSet.
This is in preparation for complying with the API by allowing
implicit type conversion.
Change-Id: I5e52fdc2f3562f806d7f9fba5d558ece4bd182ac
Diffstat (limited to 'connectivity')
-rw-r--r-- | connectivity/source/drivers/firebird/ResultSet.cxx | 83 | ||||
-rw-r--r-- | connectivity/source/drivers/firebird/ResultSet.hxx | 19 |
2 files changed, 61 insertions, 41 deletions
diff --git a/connectivity/source/drivers/firebird/ResultSet.cxx b/connectivity/source/drivers/firebird/ResultSet.cxx index 99051fe423c6..8d8e3a73f9d6 100644 --- a/connectivity/source/drivers/firebird/ResultSet.cxx +++ b/connectivity/source/drivers/firebird/ResultSet.cxx @@ -355,79 +355,86 @@ uno::Reference< XInputStream > SAL_CALL OResultSet::getCharacterStream( sal_Int3 } // ---- Internal Utilities --------------------------------------------------- -bool OResultSet::isNull(sal_Int32 columnIndex) +bool OResultSet::isNull(const sal_Int32 nColumnIndex) { - assert(columnIndex <= m_fieldCount); + assert(nColumnIndex <= m_fieldCount); XSQLVAR* pVar = m_pSqlda->sqlvar; - if (pVar[columnIndex-1].sqltype & 1) // Indicates column may contain null + if (pVar[nColumnIndex-1].sqltype & 1) // Indicates column may contain null { - if (*pVar[columnIndex-1].sqlind == -1) + if (*pVar[nColumnIndex-1].sqlind == -1) return true; } return false; } template <typename T> -T OResultSet::retrieveValue(sal_Int32 columnIndex) +T OResultSet::retrieveValue(const sal_Int32 nColumnIndex, const ISC_SHORT nType) { // TODO: check we have the right type. - if ((m_bWasNull = isNull(columnIndex))) + if ((m_bWasNull = isNull(nColumnIndex))) return T(); - return *((T*) m_pSqlda->sqlvar[columnIndex-1].sqldata); + if (m_pSqlda->sqlvar[nColumnIndex-1].sqltype == nType) + return *((T*) m_pSqlda->sqlvar[nColumnIndex-1].sqldata); + else + return T(); + // TODO: fix } template <> -OUString OResultSet::retrieveValue(sal_Int32 columnIndex) +OUString OResultSet::retrieveValue(const sal_Int32 nColumnIndex, const ISC_SHORT nType) { - if ((m_bWasNull = isNull(columnIndex))) + if ((m_bWasNull = isNull(nColumnIndex))) return OUString(); // &~1 to remove the "can contain NULL" indicator - int aSqlType = m_pSqlda->sqlvar[columnIndex-1].sqltype & ~1; + int aSqlType = m_pSqlda->sqlvar[nColumnIndex-1].sqltype & ~1; if (aSqlType == SQL_TEXT ) { - return OUString(m_pSqlda->sqlvar[columnIndex-1].sqldata, - m_pSqlda->sqlvar[columnIndex-1].sqllen, + return OUString(m_pSqlda->sqlvar[nColumnIndex-1].sqldata, + m_pSqlda->sqlvar[nColumnIndex-1].sqllen, RTL_TEXTENCODING_UTF8); } else if (aSqlType == SQL_VARYING) { // First 2 bytes are a short containing the length of the string // No idea if sqllen is still valid here? - short aLength = *((short*) m_pSqlda->sqlvar[columnIndex-1].sqldata); - return OUString(m_pSqlda->sqlvar[columnIndex-1].sqldata + 2, + short aLength = *((short*) m_pSqlda->sqlvar[nColumnIndex-1].sqldata); + return OUString(m_pSqlda->sqlvar[nColumnIndex-1].sqldata + 2, aLength, RTL_TEXTENCODING_UTF8); } else { + (void) nType; return OUString(); // TODO: Possibly do some sort of type conversion? } } template <> -ISC_QUAD* OResultSet::retrieveValue(sal_Int32 columnIndex) +ISC_QUAD* OResultSet::retrieveValue(const sal_Int32 nColumnIndex, const ISC_SHORT nType) { // TODO: this is probably wrong - if ((m_bWasNull = isNull(columnIndex))) + if ((m_bWasNull = isNull(nColumnIndex))) return 0; - return (ISC_QUAD*) m_pSqlda->sqlvar[columnIndex-1].sqldata; + if (m_pSqlda->sqlvar[nColumnIndex-1].sqltype == nType) + return (ISC_QUAD*) m_pSqlda->sqlvar[nColumnIndex-1].sqldata; + else + throw SQLException(); // TODO: better exception (can't convert Blob) } template <typename T> -T OResultSet::safelyRetrieveValue(sal_Int32 columnIndex) - throw (SQLException, RuntimeException) +T OResultSet::safelyRetrieveValue(const sal_Int32 nColumnIndex, const ISC_SHORT nType) { MutexGuard aGuard(m_rMutex); checkDisposed(OResultSet_BASE::rBHelper.bDisposed); - checkColumnIndex(columnIndex); + checkColumnIndex(nColumnIndex); checkRowIndex(); - return retrieveValue< T >(columnIndex); + return retrieveValue< T >(nColumnIndex, nType); } // ---- XRow ----------------------------------------------------------------- @@ -443,14 +450,19 @@ sal_Bool SAL_CALL OResultSet::wasNull() throw(SQLException, RuntimeException) sal_Bool SAL_CALL OResultSet::getBoolean(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - // TODO: maybe retrieve as string and test for "true", "t", "1" etc. instead? - return safelyRetrieveValue< bool >(columnIndex); +// // TODO: maybe retrieve as string and test for "true", "t", "1" etc. instead? +// return safelyRetrieveValue< bool >(columnIndex); + (void) columnIndex; + return sal_False; } sal_Int8 SAL_CALL OResultSet::getByte( sal_Int32 columnIndex ) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< sal_Int8 >(columnIndex); + // TODO: this doesn't exist in firebird, we have to always convert. +// return safelyRetrieveValue< sal_Int8 >(columnIndex); + (void) columnIndex; + return 0; } Sequence< sal_Int8 > SAL_CALL OResultSet::getBytes(sal_Int32 columnIndex) @@ -464,44 +476,45 @@ Sequence< sal_Int8 > SAL_CALL OResultSet::getBytes(sal_Int32 columnIndex) sal_Int16 SAL_CALL OResultSet::getShort(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< sal_Int16 >(columnIndex); + return safelyRetrieveValue< sal_Int16 >(columnIndex, SQL_SHORT); } sal_Int32 SAL_CALL OResultSet::getInt(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< sal_Int32 >(columnIndex); + return safelyRetrieveValue< sal_Int32 >(columnIndex, SQL_LONG); } sal_Int64 SAL_CALL OResultSet::getLong(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< sal_Int64 >(columnIndex); + return safelyRetrieveValue< sal_Int64 >(columnIndex, SQL_INT64); } float SAL_CALL OResultSet::getFloat(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< float >(columnIndex); + return safelyRetrieveValue< float >(columnIndex, SQL_FLOAT); } double SAL_CALL OResultSet::getDouble(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< double >(columnIndex); + return safelyRetrieveValue< double >(columnIndex, SQL_DOUBLE); } // ---- XRow: More complex types ---------------------------------------------- OUString SAL_CALL OResultSet::getString(sal_Int32 columnIndex) throw(SQLException, RuntimeException) { - return safelyRetrieveValue< OUString >(columnIndex); + // TODO: special handling for char type? + return safelyRetrieveValue< OUString >(columnIndex, 0); } Date SAL_CALL OResultSet::getDate(sal_Int32 nIndex) throw(SQLException, RuntimeException) { - ISC_DATE aISCDate = safelyRetrieveValue< ISC_DATE >(nIndex); + ISC_DATE aISCDate = safelyRetrieveValue< ISC_DATE >(nIndex, SQL_TYPE_DATE); struct tm aCTime; isc_decode_sql_date(&aISCDate, &aCTime); @@ -512,7 +525,7 @@ Date SAL_CALL OResultSet::getDate(sal_Int32 nIndex) Time SAL_CALL OResultSet::getTime(sal_Int32 nIndex) throw(SQLException, RuntimeException) { - ISC_TIME aISCTime = safelyRetrieveValue< ISC_TIME >(nIndex); + ISC_TIME aISCTime = safelyRetrieveValue< ISC_TIME >(nIndex, SQL_TYPE_TIME); struct tm aCTime; isc_decode_sql_time(&aISCTime, &aCTime); @@ -525,7 +538,7 @@ Time SAL_CALL OResultSet::getTime(sal_Int32 nIndex) DateTime SAL_CALL OResultSet::getTimestamp(sal_Int32 nIndex) throw(SQLException, RuntimeException) { - ISC_TIMESTAMP aISCTimestamp = safelyRetrieveValue< ISC_TIMESTAMP >(nIndex); + ISC_TIMESTAMP aISCTimestamp = safelyRetrieveValue< ISC_TIMESTAMP >(nIndex, SQL_TIMESTAMP); struct tm aCTime; isc_decode_timestamp(&aISCTimestamp, &aCTime); @@ -573,7 +586,9 @@ uno::Reference< XBlob > SAL_CALL OResultSet::getBlob(sal_Int32 columnIndex) MutexGuard aGuard(m_rMutex); checkDisposed(OResultSet_BASE::rBHelper.bDisposed); - ISC_QUAD* pBlobID = safelyRetrieveValue< ISC_QUAD* >(columnIndex); + // TODO: CLOB etc. should be valid here too, but we probably want some more + // cleverness around this. + ISC_QUAD* pBlobID = safelyRetrieveValue< ISC_QUAD* >(columnIndex, SQL_BLOB); if (!pBlobID) return 0; return m_pConnection->createBlob(pBlobID); diff --git a/connectivity/source/drivers/firebird/ResultSet.hxx b/connectivity/source/drivers/firebird/ResultSet.hxx index af608757799a..c1dd9049a76a 100644 --- a/connectivity/source/drivers/firebird/ResultSet.hxx +++ b/connectivity/source/drivers/firebird/ResultSet.hxx @@ -91,12 +91,13 @@ namespace connectivity const sal_Int32 m_fieldCount; ISC_STATUS_ARRAY m_statusVector; - bool isNull(sal_Int32 columnIndex); - template <typename T> T retrieveValue(sal_Int32 columnIndex); + bool isNull(const sal_Int32 nColumnIndex); + template <typename T> T retrieveValue(const sal_Int32 nColumnIndex, + const ISC_SHORT nType); - template <typename T> T safelyRetrieveValue(sal_Int32 columnIndex) - throw(::com::sun::star::sdbc::SQLException, - ::com::sun::star::uno::RuntimeException); + template <typename T> T safelyRetrieveValue( + const sal_Int32 nColumnIndex, + const ISC_SHORT nType); // OIdPropertyArrayUsageHelper virtual ::cppu::IPropertyArrayHelper* createArrayHelper() const; @@ -194,9 +195,13 @@ namespace connectivity // Specialisations have to be in the namespace and can't be within the class. template <> ::rtl::OUString - OResultSet::retrieveValue(sal_Int32 columnIndex); + OResultSet::retrieveValue( + const sal_Int32 nColumnIndex, + const ISC_SHORT nType); template <> ISC_QUAD* - OResultSet::retrieveValue(sal_Int32 columnIndex); + OResultSet::retrieveValue( + const sal_Int32 nColumnIndex, + const ISC_SHORT nType); } } #endif // CONNECTIVITY_FIREBIRD_RESULTSET_HXX |