tdf#67302 Resolving tablesSupplier name clash for postgresql

Change-Id: Ic9bedc678699e136c09a51575f1d27131c5ffd29
Reviewed-on: https://gerrit.libreoffice.org/25069
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: jan iversen <jani@documentfoundation.org>
diff --git a/connectivity/source/drivers/postgresql/pq_resultsetmetadata.cxx b/connectivity/source/drivers/postgresql/pq_resultsetmetadata.cxx
index c2e64df..cec6070 100644
--- a/connectivity/source/drivers/postgresql/pq_resultsetmetadata.cxx
+++ b/connectivity/source/drivers/postgresql/pq_resultsetmetadata.cxx
@@ -212,7 +212,9 @@ void ResultSetMetaData::checkTable()
            {
                const OUString name   (getTableName ( 1 ));
                const OUString schema (getSchemaName( 1 ));
                const OUString composedName( schema.isEmpty() ? name : (schema + "." + name) );
                const OUString EscapedName (name.replaceAll("\"","\"\"" ));
                const OUString EscapedSchema ( schema.replaceAll("\"","\"\""));
                const OUString composedName( schema.isEmpty() ? name : ("\"" + EscapedSchema + "\".\"" + EscapedName + "\"") );
                tables->getByName( composedName ) >>= m_table;
            }
        }
diff --git a/connectivity/source/drivers/postgresql/pq_tools.cxx b/connectivity/source/drivers/postgresql/pq_tools.cxx
index 4e6df8b..0312dde 100644
--- a/connectivity/source/drivers/postgresql/pq_tools.cxx
+++ b/connectivity/source/drivers/postgresql/pq_tools.cxx
@@ -463,6 +463,21 @@ void splitSQL( const OString & sql, OStringVector &vec )

}

void splitDoubleQuoteEscapedIdentifiers( const OUString & source, OUString *first, OUString *second)
{
    OStringVector vec;
    int a = source.indexOf("\".\"");
    OString tempstring = OUStringToOString(source , RTL_TEXTENCODING_UTF8);
    if(a > 0)
    {
        //remove start and end double quote as well as escaped double quotes
        *first =OStringToOUString(OString(&tempstring.getStr()[1],a-2) , RTL_TEXTENCODING_UTF8);
        *first=first->replaceAll("\"\"","\"");
        *second =OStringToOUString(OString(&tempstring.getStr()[a+2],source.getLength()-a-2) , RTL_TEXTENCODING_UTF8);
        *second=second->replaceAll("\"\"","\"");
    }
}

void tokenizeSQL( const OString & sql, OStringVector &vec  )
{
    int length = sql.getLength();
diff --git a/connectivity/source/drivers/postgresql/pq_tools.hxx b/connectivity/source/drivers/postgresql/pq_tools.hxx
index 6ab147c..8c17c28 100644
--- a/connectivity/source/drivers/postgresql/pq_tools.hxx
+++ b/connectivity/source/drivers/postgresql/pq_tools.hxx
@@ -111,6 +111,7 @@ OUString array2String( const css::uno::Sequence< css::uno::Any > &seq );
css::uno::Reference< css::sdbc::XConnection > extractConnectionFromStatement(
    const css::uno::Reference< css::uno::XInterface > & stmt );

void splitDoubleQuoteEscapedIdentifiers( const OUString & source, OUString *first, OUString *second);
void splitConcatenatedIdentifier( const OUString & source, OUString *first, OUString *second);


diff --git a/connectivity/source/drivers/postgresql/pq_xtable.cxx b/connectivity/source/drivers/postgresql/pq_xtable.cxx
index 4b8d451..8e0ae92 100644
--- a/connectivity/source/drivers/postgresql/pq_xtable.cxx
+++ b/connectivity/source/drivers/postgresql/pq_xtable.cxx
@@ -154,12 +154,13 @@ void Table::rename( const OUString& newName )

    OUString newTableName;
    OUString newSchemaName;
    //changing schema + dot + table-name to "schema"."table-name"
    // OOo2.0 passes schema + dot + new-table-name while
    // OO1.1.x passes new Name without schema
    // in case name contains a dot, it is interpreted as schema.tablename
    if( newName.indexOf( '.' ) >= 0 )
    if( newName.indexOf( "\".\"" ) >= 0 )
    {
        splitConcatenatedIdentifier( newName, &newSchemaName, &newTableName );
        splitDoubleQuoteEscapedIdentifiers( newName, &newSchemaName, &newTableName );
    }
    else
    {
diff --git a/connectivity/source/drivers/postgresql/pq_xtables.cxx b/connectivity/source/drivers/postgresql/pq_xtables.cxx
index 97a9b7f2..9ec5c02 100644
--- a/connectivity/source/drivers/postgresql/pq_xtables.cxx
+++ b/connectivity/source/drivers/postgresql/pq_xtables.cxx
@@ -133,8 +133,10 @@ void Tables::refresh()

            {
                m_values.push_back( makeAny( prop ) );
                OUStringBuffer buf( name.getLength() + schema.getLength() + 1);
                buf.append( schema + "." + name );
                OUString EscapedName = /* ::dbtools::composeTableNameForSelect(); */ name.replaceAll("\"","\"\"");
                OUString EscapedSchema = schema.replaceAll("\"","\"\"");
                OUStringBuffer buf( EscapedName.getLength() + EscapedSchema.getLength() + 1);
                buf.append("\"" + EscapedSchema + "\".\"" + EscapedName + "\"");
                map[ buf.makeStringAndClear() ] = tableIndex;
                ++tableIndex;
            }
diff --git a/connectivity/source/drivers/postgresql/pq_xtables.hxx b/connectivity/source/drivers/postgresql/pq_xtables.hxx
index d46ec04..17bb5fa 100644
--- a/connectivity/source/drivers/postgresql/pq_xtables.hxx
+++ b/connectivity/source/drivers/postgresql/pq_xtables.hxx
@@ -38,7 +38,7 @@
#define INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_POSTGRESQL_PQ_XTABLES_HXX

#include "pq_xcontainer.hxx"

#include "connectivity/dbtools.hxx"
namespace pq_sdbc_driver
{

diff --git a/connectivity/source/drivers/postgresql/pq_xview.cxx b/connectivity/source/drivers/postgresql/pq_xview.cxx
index c42f533..2ed4ece 100644
--- a/connectivity/source/drivers/postgresql/pq_xview.cxx
+++ b/connectivity/source/drivers/postgresql/pq_xview.cxx
@@ -102,12 +102,13 @@ void View::rename( const OUString& newName )

    OUString newTableName;
    OUString newSchemaName;
    //changing schema + dot + table-name to "schema"."table-name"
    // OOo2.0 passes schema + dot + new-table-name while
    // OO1.1.x passes new Name without schema
    // in case name contains a dot, it is interpreted as schema.tablename
    if( newName.indexOf( '.' ) >= 0 )
    if( newName.indexOf( "\".\"" ) >= 0 )
    {
        splitConcatenatedIdentifier( newName, &newSchemaName, &newTableName );
        splitDoubleQuoteEscapedIdentifiers( newName, &newSchemaName, &newTableName );
    }
    else
    {
diff --git a/connectivity/source/drivers/postgresql/pq_xviews.cxx b/connectivity/source/drivers/postgresql/pq_xviews.cxx
index 4948907..2e97f17 100644
--- a/connectivity/source/drivers/postgresql/pq_xviews.cxx
+++ b/connectivity/source/drivers/postgresql/pq_xviews.cxx
@@ -114,8 +114,10 @@ void Views::refresh()

            {
                m_values.push_back( makeAny( prop ) );
                OUStringBuffer buf( table.getLength() + schema.getLength() + 1);
                buf.append( schema + "." + table );
                OUString EscapedTable = table.replaceAll("\"","\"\"");
                OUString EscapedSchema = schema.replaceAll("\"","\"\"");
                OUStringBuffer buf( EscapedTable.getLength() + EscapedSchema.getLength() + 1);
                buf.append("\"" + EscapedSchema + "\".\"" + EscapedTable + "\"");
                map[ buf.makeStringAndClear() ] = viewIndex;
                ++viewIndex;
            }