From 3ec084d61e9c8aa0199b52de43fe288001f47e6d Mon Sep 17 00:00:00 2001 From: Wastack Date: Thu, 17 Nov 2016 14:00:06 +0100 Subject: get table name from resultset simply There is a much simpler way to access table name in ResultSetMetadata in Firebird sdbc which I didn't notice before. Change-Id: Ib306c04acf38ec86475d8d1a22a67012fe48539e Reviewed-on: https://gerrit.libreoffice.org/30931 Reviewed-by: Lionel Elie Mamane Tested-by: Lionel Elie Mamane --- .../source/drivers/firebird/PreparedStatement.cxx | 13 +- .../source/drivers/firebird/PreparedStatement.hxx | 1 - connectivity/source/drivers/firebird/ResultSet.cxx | 7 +- connectivity/source/drivers/firebird/ResultSet.hxx | 6 +- .../source/drivers/firebird/ResultSetMetaData.cxx | 8 +- .../source/drivers/firebird/ResultSetMetaData.hxx | 5 +- connectivity/source/drivers/firebird/Statement.cxx | 9 +- connectivity/source/drivers/firebird/Util.cxx | 218 --------------------- connectivity/source/drivers/firebird/Util.hxx | 4 - 9 files changed, 12 insertions(+), 259 deletions(-) (limited to 'connectivity') diff --git a/connectivity/source/drivers/firebird/PreparedStatement.cxx b/connectivity/source/drivers/firebird/PreparedStatement.cxx index 35da8ff3c8c4..7af0976b4962 100644 --- a/connectivity/source/drivers/firebird/PreparedStatement.cxx +++ b/connectivity/source/drivers/firebird/PreparedStatement.cxx @@ -57,7 +57,6 @@ OPreparedStatement::OPreparedStatement( Connection* _pConnection, ,m_sSqlStatement(sql) ,m_pOutSqlda(nullptr) ,m_pInSqlda(nullptr) - ,m_sTableName() { SAL_INFO("connectivity.firebird", "OPreparedStatement(). " "sql: " << sql); @@ -85,12 +84,6 @@ void OPreparedStatement::ensurePrepared() m_pOutSqlda, m_pInSqlda); - OStringVector vec; - tokenizeSQL( OUStringToOString(m_sSqlStatement, RTL_TEXTENCODING_UTF8), vec ); - m_sTableName = - OStringToOUString( - extractSingleTableFromSelect( vec ), RTL_TEXTENCODING_UTF8); - aErr = isc_dsql_describe_bind(m_statusVector, &m_aStatementHandle, 1, @@ -159,8 +152,7 @@ Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData() if(!m_xMetaData.is()) m_xMetaData = new OResultSetMetaData(m_pConnection.get() - , m_pOutSqlda - , m_sTableName); + , m_pOutSqlda); return m_xMetaData; } @@ -294,8 +286,7 @@ sal_Bool SAL_CALL OPreparedStatement::execute() m_aMutex, uno::Reference< XInterface >(*this), m_aStatementHandle, - m_pOutSqlda, - m_sTableName); + m_pOutSqlda); if (getStatementChangeCount() > 0) m_pConnection->notifyDatabaseModified(); diff --git a/connectivity/source/drivers/firebird/PreparedStatement.hxx b/connectivity/source/drivers/firebird/PreparedStatement.hxx index d98e46d2950c..21f1d3b3183a 100644 --- a/connectivity/source/drivers/firebird/PreparedStatement.hxx +++ b/connectivity/source/drivers/firebird/PreparedStatement.hxx @@ -53,7 +53,6 @@ namespace connectivity XSQLDA* m_pOutSqlda; XSQLDA* m_pInSqlda; - ::rtl::OUString m_sTableName; void checkParameterIndex(sal_Int32 nParameterIndex) throw(css::sdbc::SQLException, css::uno::RuntimeException); diff --git a/connectivity/source/drivers/firebird/ResultSet.cxx b/connectivity/source/drivers/firebird/ResultSet.cxx index 4449d5e2ad9f..f48046524364 100644 --- a/connectivity/source/drivers/firebird/ResultSet.cxx +++ b/connectivity/source/drivers/firebird/ResultSet.cxx @@ -57,8 +57,7 @@ OResultSet::OResultSet(Connection* pConnection, ::osl::Mutex& rMutex, const uno::Reference< XInterface >& xStatement, isc_stmt_handle& aStatementHandle, - XSQLDA* pSqlda, - const OUString& rTableName) + XSQLDA* pSqlda ) : OResultSet_BASE(rMutex) , OPropertyContainer(OResultSet_BASE::rBHelper) , m_bIsBookmarkable(false) @@ -76,7 +75,6 @@ OResultSet::OResultSet(Connection* pConnection, , m_currentRow(0) , m_bIsAfterLastRow(false) , m_fieldCount(pSqlda? pSqlda->sqld : 0) - , m_sTableName(rTableName) { SAL_INFO("connectivity.firebird", "OResultSet()."); registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISBOOKMARKABLE), @@ -725,8 +723,7 @@ uno::Reference< XResultSetMetaData > SAL_CALL OResultSet::getMetaData( ) throw( if(!m_xMetaData.is()) m_xMetaData = new OResultSetMetaData(m_pConnection - , m_pSqlda - , m_sTableName); + , m_pSqlda); return m_xMetaData; } diff --git a/connectivity/source/drivers/firebird/ResultSet.hxx b/connectivity/source/drivers/firebird/ResultSet.hxx index 347c7ea80f71..834e25e7b886 100644 --- a/connectivity/source/drivers/firebird/ResultSet.hxx +++ b/connectivity/source/drivers/firebird/ResultSet.hxx @@ -97,8 +97,6 @@ namespace connectivity const sal_Int32 m_fieldCount; ISC_STATUS_ARRAY m_statusVector; - OUString m_sTableName; - bool isNull(const sal_Int32 nColumnIndex); template OUString makeNumericString( @@ -131,8 +129,8 @@ namespace connectivity ::osl::Mutex& rMutex, const css::uno::Reference< css::uno::XInterface >& xStatement, isc_stmt_handle& aStatementHandle, - XSQLDA* aSqlda, - const OUString & rTableName); + XSQLDA* aSqlda + ); // XInterface virtual css::uno::Any SAL_CALL queryInterface( diff --git a/connectivity/source/drivers/firebird/ResultSetMetaData.cxx b/connectivity/source/drivers/firebird/ResultSetMetaData.cxx index c36148c6c2fa..6b6d9849e6af 100644 --- a/connectivity/source/drivers/firebird/ResultSetMetaData.cxx +++ b/connectivity/source/drivers/firebird/ResultSetMetaData.cxx @@ -150,13 +150,14 @@ sal_Bool SAL_CALL OResultSetMetaData::isCurrency(sal_Int32 column) sal_Bool SAL_CALL OResultSetMetaData::isAutoIncrement(sal_Int32 column) throw(SQLException, RuntimeException, std::exception) { - if( !m_sTableName.isEmpty() ) + OUString sTable = getTableName(column); + if( !sTable.isEmpty() ) { OUString sColumnName = getColumnName( column ); OUString sSql = "SELECT RDB$IDENTITY_TYPE FROM RDB$RELATION_FIELDS " "WHERE RDB$RELATION_NAME = '" - + escapeWith(m_sTableName, '\'', '\'') + "' AND " + + escapeWith(sTable, '\'', '\'') + "' AND " "RDB$FIELD_NAME = '"+ escapeWith(sColumnName, '\'', '\'') +"'"; Reference xStmt =m_pConnection ->createStatement(); @@ -195,8 +196,7 @@ sal_Int32 SAL_CALL OResultSetMetaData::getPrecision(sal_Int32 column) throw(SQLException, RuntimeException, std::exception) { sal_Int32 nType = getColumnType(column); - if( (nType == DataType::NUMERIC || nType == DataType::DECIMAL) - && !m_sTableName.isEmpty() ) + if( nType == DataType::NUMERIC || nType == DataType::DECIMAL ) { OUString sColumnName = getColumnName( column ); diff --git a/connectivity/source/drivers/firebird/ResultSetMetaData.hxx b/connectivity/source/drivers/firebird/ResultSetMetaData.hxx index 3c984e6d158f..17539bdf01bc 100644 --- a/connectivity/source/drivers/firebird/ResultSetMetaData.hxx +++ b/connectivity/source/drivers/firebird/ResultSetMetaData.hxx @@ -40,7 +40,6 @@ namespace connectivity protected: ::rtl::Reference m_pConnection; XSQLDA* m_pSqlda; - OUString m_sTableName; virtual ~OResultSetMetaData() override; @@ -48,11 +47,9 @@ namespace connectivity public: // a constructor, which is required for returning objects: OResultSetMetaData(Connection* pConnection, - XSQLDA* pSqlda, - const OUString & rTableName) + XSQLDA* pSqlda) : m_pConnection(pConnection) , m_pSqlda(pSqlda) - , m_sTableName(rTableName) {} virtual sal_Int32 SAL_CALL getColumnCount() diff --git a/connectivity/source/drivers/firebird/Statement.cxx b/connectivity/source/drivers/firebird/Statement.cxx index f69e3ef52cf7..a13414ca268e 100644 --- a/connectivity/source/drivers/firebird/Statement.cxx +++ b/connectivity/source/drivers/firebird/Statement.cxx @@ -125,18 +125,11 @@ uno::Reference< XResultSet > SAL_CALL OStatement::executeQuery(const OUString& s if (aErr) SAL_WARN("connectivity.firebird", "isc_dsql_execute failed"); - OStringVector vec; - tokenizeSQL( OUStringToOString(sql, RTL_TEXTENCODING_UTF8), vec ); - OUString sourceTable = - OStringToOUString( - extractSingleTableFromSelect( vec ), RTL_TEXTENCODING_UTF8); - m_xResultSet = new OResultSet(m_pConnection.get(), m_aMutex, uno::Reference< XInterface >(*this), m_aStatementHandle, - m_pSqlda, - sourceTable); + m_pSqlda ); // TODO: deal with cleanup diff --git a/connectivity/source/drivers/firebird/Util.cxx b/connectivity/source/drivers/firebird/Util.cxx index 114c4c811bd9..1502e60c5286 100644 --- a/connectivity/source/drivers/firebird/Util.cxx +++ b/connectivity/source/drivers/firebird/Util.cxx @@ -350,224 +350,6 @@ void firebird::freeSQLVAR(XSQLDA* pSqlda) } } -static bool isWhitespace( sal_Unicode c ) -{ - return ' ' == c || 9 == c || 10 == c || 13 == c; -} - -static bool isOperator( char c ) -{ - bool ret; - switch(c) - { - case '+': - case '-': - case '*': - case '/': - case '<': - case '>': - case '=': - case '~': - case '!': - case '@': - case '#': - case '%': - case '^': - case '&': - case '|': - case '`': - case '?': - case '$': - ret = true; - break; - default: - ret = false; - } - return ret; -} - -void firebird::tokenizeSQL( const OString & sql, OStringVector &vec ) -{ - int length = sql.getLength(); - - int i = 0; - bool singleQuote = false; - bool doubleQuote = false; - int start = 0; - for( ; i < length ; i ++ ) - { - char c = sql[i]; - if( doubleQuote ) - { - if( '"' == c ) - { - vec.push_back( OString( &sql.getStr()[start], i-start ) ); - start = i + 1; - doubleQuote = false; - } - } - else if( singleQuote ) - { - if( '\'' == c ) - { - vec.push_back( OString( &sql.getStr()[start], i - start +1 ) ); - start = i + 1; // leave single quotes ! - singleQuote = false; - } - } - else - { - if( '"' == c ) - { - doubleQuote = true; - start = i +1; // skip double quotes ! - } - else if( '\'' == c ) - { - singleQuote = true; - start = i; // leave single quotes - } - else if( isWhitespace( c ) ) - { - if( i == start ) - start ++; // skip additional whitespace - else - { - vec.push_back( OString( &sql.getStr()[start], i - start ) ); - start = i +1; - } - } - else if( ',' == c || isOperator( c ) || '(' == c || ')' == c ) - { - if( i - start ) - vec.push_back( OString( &sql.getStr()[start], i - start ) ); - vec.push_back( OString( &sql.getStr()[i], 1 ) ); - start = i + 1; - } - else if( '.' == c ) - { - if( ( i > start && sql[start] >= '0' && sql[start] <= '9' ) || - ( i == start && i > 1 && isWhitespace( sql[i-1] ) ) ) - { - // ignore, is a literal - } - else - { - if( i - start ) - vec.push_back( OString( &sql.getStr()[start], i - start ) ); - vec.push_back( OString( "." ) ); - start = i + 1; - } - } - } - } - if( start < i ) - vec.push_back( OString( &sql.getStr()[start] , i - start ) ); -} - -OString firebird::extractSingleTableFromSelect( const OStringVector &vec ) -{ - OString ret; - - if( 0 == rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( - vec[0].pData->buffer, vec[0].pData->length, "select" , 6 , 6 ) ) - { - size_t token = 0; - - for( token = 1; token < vec.size() ; token ++ ) - { - if( 0 == rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( - vec[token].getStr(), vec[token].getLength(), "from" , 4 , 4 ) ) - { - // found from - break; - } - } - token ++; - - if( token < vec.size() && 0 == rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( - vec[token].pData->buffer, vec[token].pData->length, "only " , 4 , 4 ) ) - { - token ++; - } - - if( token < vec.size() && vec[token] != "(" ) - { - // it is a table or a function name - OStringBuffer buf(128); - if( '"' == vec[token][0] ) - buf.append( &(vec[token].getStr()[1]) , vec[token].getLength() -2 ); - else - buf.append( vec[token] ); - token ++; - - if( token < vec.size() ) - { - if( vec[token] == "." ) - { - buf.append( vec[token] ); - token ++; - if( token < vec.size() ) - { - if( '"' == vec[token][0] ) - buf.append( &(vec[token].getStr()[1]) , vec[token].getLength() -2 ); - else - buf.append( vec[token] ); - token ++; - } - } - } - - ret = buf.makeStringAndClear(); - // now got my table candidate - - if( token < vec.size() && vec[token] == "(" ) - { - // whoops, it is a function - ret.clear(); - } - else - { - if( token < vec.size() ) - { - if( 0 == rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( - vec[token].pData->buffer, vec[token].pData->length, "as" , 2, 2 ) ) - { - token += 2; // skip alias - } - } - - if( token < vec.size() ) - { - if( vec[token] == "," ) - { - // whoops, multiple tables are used - ret.clear(); - } - else - { - static const char * forbiddenKeywords[] = - { "join", "natural", "outer", "inner", "left", "right", "full" , nullptr }; - for( int i = 0 ; forbiddenKeywords[i] ; i ++ ) - { - size_t nKeywordLen = strlen(forbiddenKeywords[i]); - if( 0 == rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( - vec[token].pData->buffer, vec[token].pData->length, - forbiddenKeywords[i], nKeywordLen, - nKeywordLen ) ) - { - // whoops, it is a join - ret.clear(); - } - } - } - } - } - } - } - return ret; - -} OUString firebird::escapeWith( const OUString& sText, const char aKey, const char aEscapeChar) { diff --git a/connectivity/source/drivers/firebird/Util.hxx b/connectivity/source/drivers/firebird/Util.hxx index e8077e65596a..04d96d435414 100644 --- a/connectivity/source/drivers/firebird/Util.hxx +++ b/connectivity/source/drivers/firebird/Util.hxx @@ -76,10 +76,6 @@ namespace connectivity void freeSQLVAR(XSQLDA* pSqlda); - void tokenizeSQL( const OString & sql, OStringVector &vec ); - - OString extractSingleTableFromSelect( const OStringVector &vec ); - OUString escapeWith( const OUString& sText, const char aKey, const char aEscapeChar); sal_Int64 pow10Integer( int nDecimalCount ); } -- cgit