1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "flat/EConnection.hxx"
31 #include "flat/EDatabaseMetaData.hxx"
32 #include "flat/ECatalog.hxx"
33 #ifndef _CONNECTIVITY_FLAT_ODRIVER_HXX_
34 #include "flat/EDriver.hxx"
35 #endif
36 #include <com/sun/star/lang/DisposedException.hpp>
37 #include <tools/urlobj.hxx>
38 #include "flat/EPreparedStatement.hxx"
39 #ifndef _CONNECTIVITY_FLAT_DSTATEMENT_HXX_
40 #include "flat/EStatement.hxx"
41 #endif
42 #include <comphelper/extract.hxx>
43 #include <connectivity/dbexception.hxx>
44 
45 using namespace connectivity::flat;
46 using namespace connectivity::file;
47 
48 typedef connectivity::file::OConnection  OConnection_B;
49 
50 //------------------------------------------------------------------------------
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::sdbcx;
54 using namespace ::com::sun::star::sdbc;
55 using namespace ::com::sun::star::lang;
56 
57 // --------------------------------------------------------------------------------
58 OFlatConnection::OFlatConnection(ODriver*	_pDriver) : OConnection(_pDriver)
59     ,m_nMaxRowsToScan(50)
60 	,m_bHeaderLine(sal_True)
61 	,m_cFieldDelimiter(';')
62 	,m_cStringDelimiter('"')
63 	,m_cDecimalDelimiter(',')
64 	,m_cThousandDelimiter('.')
65 {
66 }
67 //-----------------------------------------------------------------------------
68 OFlatConnection::~OFlatConnection()
69 {
70 }
71 
72 // XServiceInfo
73 // --------------------------------------------------------------------------------
74 IMPLEMENT_SERVICE_INFO(OFlatConnection, "com.sun.star.sdbc.drivers.flat.Connection", "com.sun.star.sdbc.Connection")
75 
76 //-----------------------------------------------------------------------------
77 void OFlatConnection::construct(const ::rtl::OUString& url,const Sequence< PropertyValue >& info)  throw(SQLException)
78 {
79 	osl_incrementInterlockedCount( &m_refCount );
80 
81 	::rtl::OUString aExt;
82     const PropertyValue *pBegin  = info.getConstArray();
83     const PropertyValue *pEnd    = pBegin + info.getLength();
84 	for(;pBegin != pEnd;++pBegin)
85 	{
86         if(!pBegin->Name.compareToAscii("HeaderLine"))
87             OSL_VERIFY( pBegin->Value >>= m_bHeaderLine );
88 		else if(!pBegin->Name.compareToAscii("FieldDelimiter"))
89 		{
90 			::rtl::OUString aVal;
91 			OSL_VERIFY( pBegin->Value >>= aVal );
92             m_cFieldDelimiter = aVal.toChar();
93 		}
94 		else if(!pBegin->Name.compareToAscii("StringDelimiter"))
95 		{
96 			::rtl::OUString aVal;
97 			OSL_VERIFY( pBegin->Value >>= aVal );
98             m_cStringDelimiter = aVal.toChar();
99 		}
100 		else if(!pBegin->Name.compareToAscii("DecimalDelimiter"))
101 		{
102 			::rtl::OUString aVal;
103 			OSL_VERIFY( pBegin->Value >>= aVal );
104             m_cDecimalDelimiter = aVal.toChar();
105 		}
106 		else if(!pBegin->Name.compareToAscii("ThousandDelimiter"))
107 		{
108 			::rtl::OUString aVal;
109 			OSL_VERIFY( pBegin->Value >>= aVal );
110             m_cThousandDelimiter = aVal.toChar();
111 		}
112         else if ( !pBegin->Name.compareToAscii("MaxRowScan") )
113 		{
114             pBegin->Value >>= m_nMaxRowsToScan;
115         }
116 	}
117 
118 	osl_decrementInterlockedCount( &m_refCount );
119 	OConnection::construct(url,info);
120     m_bShowDeleted = sal_True; // we do not supported rows for this type
121 }
122 // --------------------------------------------------------------------------------
123 Reference< XDatabaseMetaData > SAL_CALL OFlatConnection::getMetaData(  ) throw(SQLException, RuntimeException)
124 {
125 	::osl::MutexGuard aGuard( m_aMutex );
126 	checkDisposed(OConnection_B::rBHelper.bDisposed);
127 
128 
129 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
130 	if(!xMetaData.is())
131 	{
132 		xMetaData = new OFlatDatabaseMetaData(this);
133 		m_xMetaData = xMetaData;
134 	}
135 
136 	return xMetaData;
137 }
138 //------------------------------------------------------------------------------
139 ::com::sun::star::uno::Reference< XTablesSupplier > OFlatConnection::createCatalog()
140 {
141 	::osl::MutexGuard aGuard( m_aMutex );
142 	Reference< XTablesSupplier > xTab = m_xCatalog;
143 	if(!xTab.is())
144 	{
145 		OFlatCatalog *pCat = new OFlatCatalog(this);
146 		xTab = pCat;
147 		m_xCatalog = xTab;
148 	}
149 	return xTab;
150 }
151 // --------------------------------------------------------------------------------
152 Reference< XStatement > SAL_CALL OFlatConnection::createStatement(  ) throw(SQLException, RuntimeException)
153 {
154 	::osl::MutexGuard aGuard( m_aMutex );
155 	checkDisposed(OConnection_B::rBHelper.bDisposed);
156 
157 	OFlatStatement* pStmt = new OFlatStatement(this);
158 
159 	Reference< XStatement > xStmt = pStmt;
160     m_aStatements.push_back(WeakReferenceHelper(*pStmt));
161 	return xStmt;
162 }
163 // --------------------------------------------------------------------------------
164 Reference< XPreparedStatement > SAL_CALL OFlatConnection::prepareStatement( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
165 {
166 	::osl::MutexGuard aGuard( m_aMutex );
167 	checkDisposed(OConnection_B::rBHelper.bDisposed);
168 
169 
170 	OFlatPreparedStatement* pStmt = new OFlatPreparedStatement(this);
171 	Reference< XPreparedStatement > xStmt = pStmt;
172 	pStmt->construct(sql);
173 
174     m_aStatements.push_back(WeakReferenceHelper(*pStmt));
175 	return xStmt;
176 }
177 // --------------------------------------------------------------------------------
178 Reference< XPreparedStatement > SAL_CALL OFlatConnection::prepareCall( const ::rtl::OUString& /*sql*/ ) throw(SQLException, RuntimeException)
179 {
180 	::osl::MutexGuard aGuard( m_aMutex );
181 	checkDisposed(OConnection_B::rBHelper.bDisposed);
182 
183     ::dbtools::throwFeatureNotImplementedException( "XConnection::prepareCall", *this );
184 	return NULL;
185 }
186 
187 
188