1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LConnection.cxx,v $
10 * $Revision: 1.12.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "LConnection.hxx"
34 #include "LDatabaseMetaData.hxx"
35 #include "LCatalog.hxx"
36 #include <com/sun/star/lang/DisposedException.hpp>
37 #include <tools/urlobj.hxx>
38 #include "LPreparedStatement.hxx"
39 #include "LStatement.hxx"
40 #include <comphelper/extract.hxx>
41 #include <connectivity/dbcharset.hxx>
42 #include <connectivity/dbexception.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include <vos/process.hxx>
45 #include <tools/debug.hxx>
47 #include "diagnose_ex.h"
48 #include <comphelper/sequence.hxx>
49 #include <connectivity/dbexception.hxx>
50 #include "resource/common_res.hrc"
52 using namespace connectivity::evoab
;
53 using namespace connectivity::file
;
56 typedef connectivity::file::OConnection OConnection_B
;
58 //------------------------------------------------------------------------------
59 using namespace ::com::sun::star::uno
;
60 using namespace ::com::sun::star::beans
;
61 using namespace ::com::sun::star::sdbcx
;
62 using namespace ::com::sun::star::sdbc
;
63 using namespace ::com::sun::star::lang
;
65 ::rtl::OUString
implGetExceptionMsg( Exception
& e
, const ::rtl::OUString
& aExceptionType_
)
67 ::rtl::OUString aExceptionType
= aExceptionType_
;
68 if( aExceptionType
.getLength() == 0 )
69 aExceptionType
= ::rtl::OUString( ::rtl::OUString::createFromAscii("Unknown" ) );
71 ::rtl::OUString
aTypeLine( ::rtl::OUString::createFromAscii("\nType: " ) );
72 aTypeLine
+= aExceptionType
;
74 ::rtl::OUString
aMessageLine( ::rtl::OUString::createFromAscii("\nMessage: " ) );
75 aMessageLine
+= ::rtl::OUString( e
.Message
);
77 ::rtl::OUString
aMsg(aTypeLine
);
82 // Exception type unknown
83 ::rtl::OUString
implGetExceptionMsg( Exception
& e
)
85 ::rtl::OUString aMsg
= implGetExceptionMsg( e
, ::rtl::OUString() );
89 // --------------------------------------------------------------------------------
90 OEvoabConnection::OEvoabConnection(OEvoabDriver
* _pDriver
) : OConnection(_pDriver
)
91 ,m_bHeaderLine(sal_True
)
92 ,m_cFieldDelimiter(',')
93 ,m_cStringDelimiter('"')
94 ,m_cDecimalDelimiter('.')
95 ,m_cThousandDelimiter(' ')
97 // Initialise m_aColumnAlias.
98 m_aColumnAlias
.setAlias(_pDriver
->getFactory());
100 //-----------------------------------------------------------------------------
101 OEvoabConnection::~OEvoabConnection()
106 // --------------------------------------------------------------------------------
107 IMPLEMENT_SERVICE_INFO(OEvoabConnection
, "com.sun.star.sdbc.drivers.evoab.Connection", "com.sun.star.sdbc.Connection")
109 //-----------------------------------------------------------------------------
110 void OEvoabConnection::construct(const ::rtl::OUString
& url
,const Sequence
< PropertyValue
>& info
) throw(SQLException
)
112 osl_incrementInterlockedCount( &m_refCount
);
113 EVO_TRACE_STRING("OEvoabConnection::construct()::url = %s\n", url
);
116 ::rtl::OUString aCLICommand
= getDriver()->getEvoab_CLI_EffectiveCommand();
117 ::rtl::OUString aWorkingDirPath
= getDriver()->getWorkingDirPath();
118 ::rtl::OUString aArg1
= ::rtl::OUString::createFromAscii(OEvoabDriver::getEVOAB_CLI_ARG_LIST_FOLDERS());
119 ::rtl::OUString aArg2
= ::rtl::OUString::createFromAscii(OEvoabDriver::getEVOAB_CLI_ARG_OUTPUT_FILE_PREFIX());
120 aArg2
+= aWorkingDirPath
;
121 aArg2
+= getDriver()->getEvoFolderListFileName();
122 OArgumentList
aArgs(2,&aArg1
,&aArg2
);
124 EVO_TRACE_STRING("OEvoabConnection::construct()::aCLICommand = %s\n", aCLICommand
);
125 EVO_TRACE_STRING("OEvoabConnection::construct()::aWorkingDirPath = %s\n", aWorkingDirPath
);
126 EVO_TRACE_STRING("OEvoabConnection::construct()::aArg1 = %s\n", aArg1
);
127 EVO_TRACE_STRING("OEvoabConnection::construct()::aArg2 = %s\n", aArg2
);
128 OProcess
aApp( aCLICommand
,aWorkingDirPath
);
130 aApp
.execute( (OProcess::TProcessOption
)(OProcess::TOption_Hidden
| OProcess::TOption_Wait
| OProcess::TOption_SearchPath
),aArgs
),
132 "Error at execute evolution-addressbook-export to get VCards");
135 Sequence
<PropertyValue
> aDriverParam
;
136 ::std::vector
<PropertyValue
> aParam
;
138 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("EnableSQL92Check"), 0, Any(), PropertyState_DIRECT_VALUE
));
139 ::dbtools::OCharsetMap aLookupIanaName
;
140 ::dbtools::OCharsetMap::const_iterator aLookup
= aLookupIanaName
.find(RTL_TEXTENCODING_UTF8
);
141 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("CharSet"), 0,
142 makeAny((*aLookup
).getIanaName()), PropertyState_DIRECT_VALUE
));
143 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("Extension"), 0, makeAny(getDriver()->getFileExt()), PropertyState_DIRECT_VALUE
));
144 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("HeaderLine"), 0, makeAny(m_bHeaderLine
), PropertyState_DIRECT_VALUE
));
145 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("FieldDelimiter"), 0, makeAny(::rtl::OUString(&m_cFieldDelimiter
,1)), PropertyState_DIRECT_VALUE
));
146 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("StringDelimiter"), 0, makeAny(::rtl::OUString(&m_cStringDelimiter
,1)), PropertyState_DIRECT_VALUE
));
147 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("DecimalDelimiter"), 0, makeAny(::rtl::OUString(&m_cDecimalDelimiter
,1)), PropertyState_DIRECT_VALUE
));
148 aParam
.push_back(PropertyValue(::rtl::OUString::createFromAscii("ThousandDelimiter"), 0, makeAny(::rtl::OUString(&m_cThousandDelimiter
,1)), PropertyState_DIRECT_VALUE
));
150 // build a new parameter sequence from the original parameters, appended by the new parameters from above
151 PropertyValue
*pParams
= aParam
.empty() ? 0 : &aParam
[0];
152 aDriverParam
= ::comphelper::concatSequences(
154 Sequence
< PropertyValue
>( pParams
, aParam
.size() )
157 // transform "sdbc:address:evolution" part of URL to "sdbc:flat:file:///..."
159 sal_Int32 nLen
= url
.indexOf(':');
160 nLen
= url
.indexOf(':',nLen
+1);
161 ::rtl::OUString
aAddrbookURI(url
.copy(nLen
+1));
163 nLen
= aAddrbookURI
.indexOf(':');
164 ::rtl::OUString aAddrbookScheme
;
167 // There isn't any subschema: - but could be just subschema
168 if ( aAddrbookURI
.getLength() > 0 )
170 aAddrbookScheme
= aAddrbookURI
;
174 OSL_TRACE( "No subschema given!!!\n");
175 throwGenericSQLException(STR_URI_SYNTAX_ERROR
,*this);
180 aAddrbookScheme
= aAddrbookURI
.copy(0, nLen
);
183 EVO_TRACE_STRING("OEvoabConnection::construct()::URI = %s\n", aAddrbookURI
);
184 EVO_TRACE_STRING("OEvoabConnection::construct()::Scheme = %s\n", aAddrbookScheme
);
187 // Now we have a URI convert it to a Evolution CLI flat file URI
189 // The Mapping being used is:
192 // "sdbc:address:evolution:" -> "sdbc:flat:file:///(file path generated)
194 rtl::OUString aEvoFlatURI
;
195 if ( aAddrbookScheme
.compareToAscii( OEvoabDriver::getSDBC_SCHEME_EVOLUTION() ) == 0 )
197 aEvoFlatURI
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "sdbc:flat:" ));
201 aEvoFlatURI
+= getDriver()->getWorkingDirURL();
202 EVO_TRACE_STRING("OEvoabConnection::construct()::m_aEvoFlatURI = %s\n", aEvoFlatURI
);
203 //setURL(aEvoFlatURI);
204 m_aEvoFlatURI
= aEvoFlatURI
;
206 osl_decrementInterlockedCount( &m_refCount
);
207 OConnection::construct(aEvoFlatURI
,aDriverParam
);
209 // --------------------------------------------------------------------------------
210 Reference
< XDatabaseMetaData
> SAL_CALL
OEvoabConnection::getMetaData( ) throw(SQLException
, RuntimeException
)
212 ::osl::MutexGuard
aGuard( m_aMutex
);
213 checkDisposed(OConnection_B::rBHelper
.bDisposed
);
216 Reference
< XDatabaseMetaData
> xMetaData
= m_xMetaData
;
219 xMetaData
= new OEvoabDatabaseMetaData(this);
220 m_xMetaData
= xMetaData
;
225 //------------------------------------------------------------------------------
226 ::com::sun::star::uno::Reference
< XTablesSupplier
> OEvoabConnection::createCatalog()
228 ::osl::MutexGuard
aGuard( m_aMutex
);
229 Reference
< XTablesSupplier
> xTab
= m_xCatalog
;
232 OEvoabCatalog
*pCat
= new OEvoabCatalog(this);
238 // --------------------------------------------------------------------------------
239 Reference
< XStatement
> SAL_CALL
OEvoabConnection::createStatement( ) throw(SQLException
, RuntimeException
)
241 ::osl::MutexGuard
aGuard( m_aMutex
);
242 checkDisposed(OConnection_B::rBHelper
.bDisposed
);
244 OEvoabStatement
* pStmt
= new OEvoabStatement(this);
246 Reference
< XStatement
> xStmt
= pStmt
;
247 m_aStatements
.push_back(WeakReferenceHelper(*pStmt
));
250 // --------------------------------------------------------------------------------
251 Reference
< XPreparedStatement
> SAL_CALL
OEvoabConnection::prepareStatement( const ::rtl::OUString
& sql
) throw(SQLException
, RuntimeException
)
253 ::osl::MutexGuard
aGuard( m_aMutex
);
254 checkDisposed(OConnection_B::rBHelper
.bDisposed
);
257 OEvoabPreparedStatement
* pStmt
= new OEvoabPreparedStatement(this);
258 Reference
< XPreparedStatement
> xStmt
= pStmt
;
259 pStmt
->construct(sql
);
261 m_aStatements
.push_back(WeakReferenceHelper(*pStmt
));
264 // --------------------------------------------------------------------------------
265 Reference
< XPreparedStatement
> SAL_CALL
OEvoabConnection::prepareCall( const ::rtl::OUString
& /*sql*/ ) throw(SQLException
, RuntimeException
)
267 ::dbtools::throwFeatureNotImplementedException( "XConnection::prepareCall", *this );
270 // -------------------------------------------------------------------------