diff options
author | Lionel Elie Mamane <lionel@mamane.lu> | 2011-11-30 15:57:08 +0100 |
---|---|---|
committer | Lionel Elie Mamane <lionel@mamane.lu> | 2011-11-30 16:00:13 +0100 |
commit | 5f3bd5bed3f5d677208dc1b897b2f21eb5f622bb (patch) | |
tree | 7cdece771c2aabbf07616e0b3c7928782e365975 /connectivity | |
parent | c75e15c980714c746a0b4b023cecf486cbe81bdc (diff) |
pgsql-sdbc: Robustify against empty arrays, etc
Diffstat (limited to 'connectivity')
11 files changed, 113 insertions, 74 deletions
diff --git a/connectivity/source/drivers/postgresql/pq_connection.cxx b/connectivity/source/drivers/postgresql/pq_connection.cxx index bf30c779ecc5..ef6ec787db1b 100644 --- a/connectivity/source/drivers/postgresql/pq_connection.cxx +++ b/connectivity/source/drivers/postgresql/pq_connection.cxx @@ -492,6 +492,7 @@ static void properties2arrays( const Sequence< PropertyValue > & args, for( int i = 0; i < args.getLength() ; ++i ) { bool append = true; + // TODO: rewrite this as a static table of keywords, and a loop over these keywords. if( args[i].Name.matchIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "password" ) ) ) { keywords.push_back( "password", SAL_NO_ACQUIRE ); diff --git a/connectivity/source/drivers/postgresql/pq_databasemetadata.cxx b/connectivity/source/drivers/postgresql/pq_databasemetadata.cxx index 7bca58290dc7..28aad9cbcb6a 100644 --- a/connectivity/source/drivers/postgresql/pq_databasemetadata.cxx +++ b/connectivity/source/drivers/postgresql/pq_databasemetadata.cxx @@ -1250,8 +1250,7 @@ sal_Bool DatabaseMetaData::dataDefinitionIgnoredInTransactions( ) throw (SQLExc closeable->close(); return new SequenceResultSet( - m_refMutex, *this, statics.tablesRowNames, - Sequence< Sequence< Any > > ( &vec[0],vec.size() ), m_pSettings->tc ); + m_refMutex, *this, statics.tablesRowNames, sequence_of_vector(vec), m_pSettings->tc ); } struct SortInternalSchemasLastAndPublicFirst @@ -1325,8 +1324,7 @@ struct SortInternalSchemasLastAndPublicFirst if( closeable.is() ) closeable->close(); return new SequenceResultSet( - m_refMutex, *this, getStatics().schemaNames, - Sequence< Sequence< Any > > ( &vec[0], vec.size() ), m_pSettings->tc ); + m_refMutex, *this, getStatics().schemaNames, sequence_of_vector(vec), m_pSettings->tc ); } ::com::sun::star::uno::Reference< XResultSet > DatabaseMetaData::getCatalogs( ) @@ -1670,8 +1668,7 @@ static void columnMetaData2DatabaseTypeDescription( closeable->close(); return new SequenceResultSet( - m_refMutex, *this, statics.columnRowNames, - Sequence< Sequence< Any > > ( &vec[0],vec.size() ), m_pSettings->tc ); + m_refMutex, *this, statics.columnRowNames, sequence_of_vector(vec), m_pSettings->tc ); } ::com::sun::star::uno::Reference< XResultSet > DatabaseMetaData::getColumnPrivileges( @@ -2348,7 +2345,7 @@ static void pgTypeInfo2ResultSet( m_refMutex, *this, getStatics().typeinfoColumnNames, - Sequence< Sequence< Any > > ( &vec[0] , vec.size() ), + sequence_of_vector(vec), m_pSettings->tc, &( getStatics().typeInfoMetaData )); } @@ -2494,7 +2491,7 @@ static sal_Int32 seqContains( const Sequence< sal_Int32 > &seq, sal_Int32 value } return new SequenceResultSet( m_refMutex, *this, getStatics().indexinfoColumnNames, - Sequence< Sequence< Any > > ( &vec[0] , vec.size() ), + sequence_of_vector(vec), m_pSettings->tc ); } diff --git a/connectivity/source/drivers/postgresql/pq_tools.cxx b/connectivity/source/drivers/postgresql/pq_tools.cxx index ac9c66f45a11..c27bf154e0db 100644 --- a/connectivity/source/drivers/postgresql/pq_tools.cxx +++ b/connectivity/source/drivers/postgresql/pq_tools.cxx @@ -829,8 +829,7 @@ com::sun::star::uno::Sequence< Any > parseArray( const rtl::OUString & str ) thr } i++; } - ret = Sequence< Any > ( &elements[0] , elements.size() ); - return ret; + return sequence_of_vector(elements); } com::sun::star::uno::Sequence< sal_Int32 > parseIntArray( const ::rtl::OUString & str ) @@ -846,7 +845,7 @@ com::sun::star::uno::Sequence< sal_Int32 > parseIntArray( const ::rtl::OUString } vec.push_back( (sal_Int32)rtl_ustr_toInt32( &str.pData->buffer[start], 10 ) ); // printf( "found %d\n" , rtl_ustr_toInt32( &str.pData->buffer[start], 10 )); - return Sequence< sal_Int32 > ( &vec[0], vec.size() ); + return sequence_of_vector(vec); } void fillAttnum2attnameMap( @@ -987,16 +986,44 @@ void fillAttnum2attnameMap( com::sun::star::uno::Sequence< sal_Int32 > string2intarray( const ::rtl::OUString & str ) { com::sun::star::uno::Sequence< sal_Int32 > ret; - if( str.getLength() > 1 && '{' == str[0] ) + const sal_Int32 strlen = str.getLength(); + if( str.getLength() > 1 ) { - std::vector< sal_Int32, Allocator< sal_Int32 > > vec; sal_Int32 start = 0; + while ( iswspace( str.iterateCodePoints(&start) ) ) + if ( start == strlen) + return ret; + if ( str.iterateCodePoints(&start) != L'{' ) + return ret; + while ( iswspace( str.iterateCodePoints(&start) ) ) + if ( start == strlen) + return ret; + if ( str.iterateCodePoints(&start, 0) == L'}' ) + return ret; + + std::vector< sal_Int32, Allocator< sal_Int32 > > vec; do { - start ++; - vec.push_back( (sal_Int32)rtl_ustr_toInt32( &str.getStr()[start], 10 ) ); - start = str.indexOf( ',' , start ); - } while( start != -1 ); + ::rtl::OUString digits; + sal_Int32 c; + while ( isdigit( c = str.iterateCodePoints(&start) ) ) + { + if ( start == strlen) + return ret; + digits += OUString(c); + } + vec.push_back( digits.toInt32() ); + while ( iswspace( str.iterateCodePoints(&start) ) ) + if ( start == strlen) + return ret; + if ( str.iterateCodePoints(&start, 0) == L'}' ) + break; + if ( str.iterateCodePoints(&start) != L',' ) + return ret; + if ( start == strlen) + return ret; + } while( true ); + // vec is guaranteed non-empty ret = com::sun::star::uno::Sequence< sal_Int32 > ( &vec[0] , vec.size() ); } return ret; @@ -1346,5 +1373,4 @@ bool implSetObject( const Reference< XParameters >& _rxParameters, return bSuccessfullyReRouted; } - } diff --git a/connectivity/source/drivers/postgresql/pq_tools.hxx b/connectivity/source/drivers/postgresql/pq_tools.hxx index a5b7128c45c0..2f6880297ebc 100644 --- a/connectivity/source/drivers/postgresql/pq_tools.hxx +++ b/connectivity/source/drivers/postgresql/pq_tools.hxx @@ -197,6 +197,14 @@ public: void executeUpdate( const rtl::OUString & sql ); }; +template < typename T, typename Allocator > com::sun::star::uno::Sequence<T> sequence_of_vector ( const std::vector<T, Allocator> &vec ) +{ + if ( vec.size() == 0 ) + return com::sun::star::uno::Sequence<T>(); + else + return com::sun::star::uno::Sequence<T>( &vec[0], vec.size()); +} + } #endif diff --git a/connectivity/source/drivers/postgresql/pq_xcolumns.cxx b/connectivity/source/drivers/postgresql/pq_xcolumns.cxx index 1e1bfd315165..18f2965d288d 100644 --- a/connectivity/source/drivers/postgresql/pq_xcolumns.cxx +++ b/connectivity/source/drivers/postgresql/pq_xcolumns.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <rtl/strbuf.hxx> @@ -340,8 +338,8 @@ void Columns::refresh() String2IntMap map; - std::vector< Any, Allocator< Any> > vec; - sal_Int32 columnIndex = 0; + m_values = Sequence< com::sun::star::uno::Any > (); + int columnIndex = 0; while( rs->next() ) { Column * pColumn = @@ -359,11 +357,14 @@ void Columns::refresh() // m_tableName, // name ) ); - vec.push_back( makeAny( prop ) ); - map[ name ] = columnIndex; - columnIndex ++; + { + const int currentColumnIndex = columnIndex++; + assert(currentColumnIndex == m_values.getLength()); + m_values.realloc( columnIndex ); + m_values[currentColumnIndex] = makeAny( prop ); + map[ name ] = currentColumnIndex; + } } - m_values = Sequence< com::sun::star::uno::Any > ( & vec[0] , vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) diff --git a/connectivity/source/drivers/postgresql/pq_xindexes.cxx b/connectivity/source/drivers/postgresql/pq_xindexes.cxx index c965bace1fa0..523bc65d255c 100644 --- a/connectivity/source/drivers/postgresql/pq_xindexes.cxx +++ b/connectivity/source/drivers/postgresql/pq_xindexes.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <rtl/strbuf.hxx> @@ -165,7 +163,7 @@ void Indexes::refresh() Reference< XRow > row( rs, UNO_QUERY ); String2IntMap map; - std::vector< Any, Allocator< Any> > vec; + m_values = Sequence< com::sun::star::uno::Any > (); sal_Int32 index = 0; while( rs->next() ) { @@ -205,11 +203,14 @@ void Indexes::refresh() pIndex->setPropertyValue_NoBroadcast_public( st.PRIVATE_COLUMN_INDEXES, makeAny( columnNames )); - vec.push_back( makeAny( prop ) ); - map[ currentIndexName ] = index; - index ++; + { + const int currentIndex = index++; + assert(currentIndex == m_values.getLength()); + m_values.realloc( index ); + m_values[currentIndex] = makeAny( prop ); + map[ currentIndexName ] = currentIndex; + } } - m_values = Sequence< com::sun::star::uno::Any > ( & vec[0] , vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) diff --git a/connectivity/source/drivers/postgresql/pq_xkeycolumns.cxx b/connectivity/source/drivers/postgresql/pq_xkeycolumns.cxx index ac0d65ec9669..bbee0fa3b1c6 100644 --- a/connectivity/source/drivers/postgresql/pq_xkeycolumns.cxx +++ b/connectivity/source/drivers/postgresql/pq_xkeycolumns.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <rtl/strbuf.hxx> @@ -151,7 +149,7 @@ void KeyColumns::refresh() String2IntMap map; - std::vector< Any, Allocator< Any> > vec; + m_values = Sequence< com::sun::star::uno::Any > (); sal_Int32 columnIndex = 0; while( rs->next() ) { @@ -177,11 +175,14 @@ void KeyColumns::refresh() st.RELATED_COLUMN, makeAny( m_foreignColumnNames[keyindex]) ); } - vec.push_back( makeAny( prop ) ); - map[ name ] = columnIndex; - columnIndex ++; + { + const int currentColumnIndex = columnIndex++; + assert(currentColumnIndex == m_values.getLength()); + m_values.realloc( columnIndex ); + m_values[currentColumnIndex] = makeAny( prop ); + map[ name ] = currentColumnIndex; + } } - m_values = Sequence< com::sun::star::uno::Any > ( & vec[0] , vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) diff --git a/connectivity/source/drivers/postgresql/pq_xkeys.cxx b/connectivity/source/drivers/postgresql/pq_xkeys.cxx index 4db692e7b5b5..9ad71b10c82c 100644 --- a/connectivity/source/drivers/postgresql/pq_xkeys.cxx +++ b/connectivity/source/drivers/postgresql/pq_xkeys.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <rtl/strbuf.hxx> @@ -226,8 +224,8 @@ void Keys::refresh() Reference< XRow > xRow( rs , UNO_QUERY ); String2IntMap map; - std::vector< Any, Allocator< Any> > vec; - sal_Int32 keyIndex = 0; + m_values = Sequence< com::sun::star::uno::Any > (); + int keyIndex = 0; while( rs->next() ) { Key * pKey = @@ -268,11 +266,15 @@ void Keys::refresh() string2intarray( xRow->getString(8) ) ) ) ); } - vec.push_back( makeAny( prop ) ); - map[ xRow->getString( 1 ) ] = keyIndex; - keyIndex ++; + + { + const int currentKeyIndex = keyIndex++; + map[ xRow->getString( 1 ) ] = currentKeyIndex; + assert(currentKeyIndex == m_values.getLength()); + m_values.realloc( keyIndex ); + m_values[currentKeyIndex] = makeAny( prop ); + } } - m_values = Sequence< com::sun::star::uno::Any > ( & vec[0] , vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) diff --git a/connectivity/source/drivers/postgresql/pq_xtables.cxx b/connectivity/source/drivers/postgresql/pq_xtables.cxx index 8e7207d3fc9b..483cc3fd88b7 100644 --- a/connectivity/source/drivers/postgresql/pq_xtables.cxx +++ b/connectivity/source/drivers/postgresql/pq_xtables.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <com/sun/star/sdbc/XRow.hpp> @@ -137,7 +135,7 @@ void Tables::refresh() String2IntMap map; - std::vector< Any, Allocator< Any> > vec; + m_values = Sequence< com::sun::star::uno::Any > (); sal_Int32 tableIndex = 0; while( rs->next() ) { @@ -170,13 +168,16 @@ void Tables::refresh() com::sun::star::sdbcx::Privilege::REFERENCE | com::sun::star::sdbcx::Privilege::DROP ) ) ); - vec.push_back( makeAny( prop ) ); - OUStringBuffer buf( name.getLength() + schema.getLength() + 1); - buf.append( schema ).appendAscii( "." ).append( name ); - map[ buf.makeStringAndClear() ] = tableIndex; - tableIndex ++; + { + const int currentTableIndex = tableIndex++; + assert(currentTableIndex == m_values.getLength()); + m_values.realloc( tableIndex ); + m_values[currentTableIndex] = makeAny( prop ); + OUStringBuffer buf( name.getLength() + schema.getLength() + 1); + buf.append( schema ).appendAscii( "." ).append( name ); + map[ buf.makeStringAndClear() ] = currentTableIndex; + } } - m_values = Sequence< com::sun::star::uno::Any > ( & vec[0] , vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) diff --git a/connectivity/source/drivers/postgresql/pq_xusers.cxx b/connectivity/source/drivers/postgresql/pq_xusers.cxx index b41bd3af01e7..390bc47bf171 100644 --- a/connectivity/source/drivers/postgresql/pq_xusers.cxx +++ b/connectivity/source/drivers/postgresql/pq_xusers.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <com/sun/star/sdbc/XRow.hpp> @@ -126,7 +124,7 @@ void Users::refresh() String2IntMap map; - std::vector< Any, Allocator< Any> > vec; + m_values = Sequence< com::sun::star::uno::Any > ( ); sal_Int32 tableIndex = 0; while( rs->next() ) { @@ -138,11 +136,14 @@ void Users::refresh() pUser->setPropertyValue_NoBroadcast_public( st.NAME , makeAny(xRow->getString( TABLE_INDEX_CATALOG+1) ) ); - vec.push_back( makeAny(prop ) ); - map[ name ] = tableIndex; - tableIndex ++; + { + const int currentTableIndex = tableIndex++; + assert(currentTableIndex == m_values.getLength()); + m_values.realloc( tableIndex ); + m_values[currentTableIndex] = makeAny( prop ); + map[ name ] = currentTableIndex; + } } - m_values = Sequence< com::sun::star::uno::Any > ( & vec[0] , vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) diff --git a/connectivity/source/drivers/postgresql/pq_xviews.cxx b/connectivity/source/drivers/postgresql/pq_xviews.cxx index b39bb4284996..b949c1a7b3f4 100644 --- a/connectivity/source/drivers/postgresql/pq_xviews.cxx +++ b/connectivity/source/drivers/postgresql/pq_xviews.cxx @@ -55,8 +55,6 @@ * ************************************************************************/ -#include <vector> - #include <rtl/ustrbuf.hxx> #include <com/sun/star/sdbc/XRow.hpp> @@ -135,7 +133,7 @@ void Views::refresh() Reference< XRow > xRow( rs , UNO_QUERY ); - std::vector< Any, Allocator< Any> > vec; + m_values = Sequence< com::sun::star::uno::Any > (); String2IntMap map; sal_Int32 viewIndex = 0; @@ -152,15 +150,17 @@ void Views::refresh() pView->setPropertyValue_NoBroadcast_public(st.NAME , makeAny(table) ); pView->setPropertyValue_NoBroadcast_public(st.SCHEMA_NAME, makeAny(schema) ); pView->setPropertyValue_NoBroadcast_public(st.COMMAND, makeAny(command) ); - vec.push_back( makeAny( prop ) ); - - OUStringBuffer buf( table.getLength() + schema.getLength() + 1); - buf.append( schema ).appendAscii( "." ).append( table ); - map[ buf.makeStringAndClear() ] = viewIndex; - viewIndex ++; + { + const int currentViewIndex = viewIndex++; + assert(currentViewIndex == m_values.getLength()); + m_values.realloc( viewIndex ); + m_values[currentViewIndex] = makeAny( prop ); + OUStringBuffer buf( table.getLength() + schema.getLength() + 1); + buf.append( schema ).appendAscii( "." ).append( table ); + map[ buf.makeStringAndClear() ] = currentViewIndex; + } } - m_values = Sequence< com::sun::star::uno::Any > ( &vec[0], vec.size() ); m_name2index.swap( map ); } catch ( com::sun::star::sdbc::SQLException & e ) |