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 "file/FDriver.hxx"
31 #include "file/FConnection.hxx"
32 #include "file/fcode.hxx"
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <comphelper/types.hxx>
35 #include "connectivity/dbexception.hxx"
36 #include "resource/common_res.hrc"
37 #include "resource/sharedresources.hxx"
38 #include <rtl/logfile.hxx>
39 
40 
41 using namespace connectivity::file;
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::beans;
45 using namespace com::sun::star::sdbc;
46 using namespace com::sun::star::sdbcx;
47 using namespace com::sun::star::container;
48 // --------------------------------------------------------------------------------
49 OFileDriver::OFileDriver(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory)
50 	: ODriver_BASE(m_aMutex)
51 	,m_xFactory(_rxFactory)
52 {
53     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::OFileDriver" );
54 }
55 // --------------------------------------------------------------------------------
56 void OFileDriver::disposing()
57 {
58     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::disposing" );
59 	::osl::MutexGuard aGuard(m_aMutex);
60 
61 
62 	for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
63 	{
64         Reference< XComponent > xComp(i->get(), UNO_QUERY);
65 		if (xComp.is())
66 			xComp->dispose();
67 	}
68 	m_xConnections.clear();
69 
70 	ODriver_BASE::disposing();
71 }
72 
73 // static ServiceInfo
74 //------------------------------------------------------------------------------
75 rtl::OUString OFileDriver::getImplementationName_Static(  ) throw(RuntimeException)
76 {
77 	return rtl::OUString::createFromAscii("com.sun.star.sdbc.driver.file.Driver");
78 }
79 //------------------------------------------------------------------------------
80 Sequence< ::rtl::OUString > OFileDriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
81 {
82     Sequence< ::rtl::OUString > aSNS( 2 );
83 	aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
84 	aSNS[1] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Driver");
85 	return aSNS;
86 }
87 
88 //------------------------------------------------------------------
89 ::rtl::OUString SAL_CALL OFileDriver::getImplementationName(  ) throw(RuntimeException)
90 {
91 	return getImplementationName_Static();
92 }
93 
94 //------------------------------------------------------------------
95 sal_Bool SAL_CALL OFileDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
96 {
97     Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
98 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
99 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
100 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
101 		;
102 
103 	return pSupported != pEnd;
104 }
105 
106 //------------------------------------------------------------------
107 Sequence< ::rtl::OUString > SAL_CALL OFileDriver::getSupportedServiceNames(  ) throw(RuntimeException)
108 {
109 	return getSupportedServiceNames_Static();
110 }
111 
112 // --------------------------------------------------------------------------------
113 Reference< XConnection > SAL_CALL OFileDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
114 {
115     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::connect" );
116 	::osl::MutexGuard aGuard( m_aMutex );
117 	checkDisposed(ODriver_BASE::rBHelper.bDisposed);
118 
119 	OConnection* pCon = new OConnection(this);
120 	Reference< XConnection > xCon = pCon;
121 	pCon->construct(url,info);
122     m_xConnections.push_back(WeakReferenceHelper(*pCon));
123 
124 	return xCon;
125 }
126 // --------------------------------------------------------------------------------
127 sal_Bool SAL_CALL OFileDriver::acceptsURL( const ::rtl::OUString& url )
128                 throw(SQLException, RuntimeException)
129 {
130     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::acceptsURL" );
131 	return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:file:"),10));
132 }
133 // --------------------------------------------------------------------------------
134 Sequence< DriverPropertyInfo > SAL_CALL OFileDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& /*info*/ ) throw(SQLException, RuntimeException)
135 {
136     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::getPropertyInfo" );
137 	if ( acceptsURL(url) )
138 	{
139 		::std::vector< DriverPropertyInfo > aDriverInfo;
140 
141 		Sequence< ::rtl::OUString > aBoolean(2);
142 		aBoolean[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"));
143 		aBoolean[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("1"));
144 
145 		aDriverInfo.push_back(DriverPropertyInfo(
146 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharSet"))
147 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharSet of the database."))
148 				,sal_False
149 				,::rtl::OUString()
150 				,Sequence< ::rtl::OUString >())
151 				);
152 		aDriverInfo.push_back(DriverPropertyInfo(
153 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Extension"))
154 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Extension of the file format."))
155 				,sal_False
156 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".*"))
157 				,Sequence< ::rtl::OUString >())
158 				);
159 		aDriverInfo.push_back(DriverPropertyInfo(
160 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowDeleted"))
161 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Display inactive records."))
162 				,sal_False
163 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"))
164 				,aBoolean)
165 				);
166 		aDriverInfo.push_back(DriverPropertyInfo(
167 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EnableSQL92Check"))
168 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Use SQL92 naming constraints."))
169 				,sal_False
170 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"))
171 				,aBoolean)
172 				);
173         aDriverInfo.push_back(DriverPropertyInfo(
174 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("UseRelativePath"))
175 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Handle the connection url as relative path."))
176 				,sal_False
177 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"))
178 				,aBoolean)
179 				);
180         aDriverInfo.push_back(DriverPropertyInfo(
181 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("URL"))
182 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("The URL of the database document which is used to create an absolute path."))
183 				,sal_False
184 				,::rtl::OUString()
185 				,Sequence< ::rtl::OUString >())
186 				);
187 		return Sequence< DriverPropertyInfo >(&(aDriverInfo[0]),aDriverInfo.size());
188 	} // if ( acceptsURL(url) )
189     {
190         ::connectivity::SharedResources aResources;
191         const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
192 		::dbtools::throwGenericSQLException(sMessage ,*this);
193     } // if ( ! acceptsURL(url) )
194 	return Sequence< DriverPropertyInfo >();
195 }
196 // --------------------------------------------------------------------------------
197 sal_Int32 SAL_CALL OFileDriver::getMajorVersion(  ) throw(RuntimeException)
198 {
199     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::getMajorVersion" );
200 	return 1;
201 }
202 // --------------------------------------------------------------------------------
203 sal_Int32 SAL_CALL OFileDriver::getMinorVersion(  ) throw(RuntimeException)
204 {
205     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::getMinorVersion" );
206 	return 0;
207 }
208 // --------------------------------------------------------------------------------
209 // --------------------------------------------------------------------------------
210 // XDataDefinitionSupplier
211 Reference< XTablesSupplier > SAL_CALL OFileDriver::getDataDefinitionByConnection( const Reference< ::com::sun::star::sdbc::XConnection >& connection ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
212 {
213     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::getDataDefinitionByConnection" );
214 	::osl::MutexGuard aGuard( m_aMutex );
215 	checkDisposed(ODriver_BASE::rBHelper.bDisposed);
216 
217 	Reference< XTablesSupplier > xTab = NULL;
218 	Reference< ::com::sun::star::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
219 	if(xTunnel.is())
220 	{
221 		OConnection* pSearchConnection = reinterpret_cast< OConnection* >( xTunnel->getSomething(OConnection::getUnoTunnelImplementationId()) );
222 		OConnection* pConnection = NULL;
223 		for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
224 		{
225 			if ((OConnection*) Reference< XConnection >::query(i->get().get()).get() == pSearchConnection)
226 			{
227 				pConnection = pSearchConnection;
228 				break;
229 			}
230 		}
231 
232 		if(pConnection)
233 			xTab = pConnection->createCatalog();
234 	}
235 	return xTab;
236 }
237 
238 // --------------------------------------------------------------------------------
239 Reference< XTablesSupplier > SAL_CALL OFileDriver::getDataDefinitionByURL( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
240 {
241     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OFileDriver::getDataDefinitionByURL" );
242 	if ( ! acceptsURL(url) )
243     {
244 		::connectivity::SharedResources aResources;
245         const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
246 		::dbtools::throwGenericSQLException(sMessage ,*this);
247     }
248 	return getDataDefinitionByConnection(connect(url,info));
249 }
250 // -----------------------------------------------------------------------------
251 void OOperandParam::describe(const Reference< XPropertySet>& rColumn, ::vos::ORef<connectivity::OSQLColumns> rParameterColumns)
252 {
253 	// den alten namen beibehalten
254 
255 	OSL_ENSURE(getRowPos() < rParameterColumns->get().size(),"Invalid index for orderkey values!");
256 
257 	Reference< XPropertySet> xColumn = (rParameterColumns->get())[getRowPos()];
258 
259 	try
260 	{
261 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME)));
262 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE)));
263 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)));
264 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)));
265 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE)));
266 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE)));
267 		xColumn->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT),rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)));
268 	}
269 	catch(const Exception&)
270 	{
271 	}
272 
273 	m_eDBType = ::comphelper::getINT32(rColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)));
274 }
275 // -----------------------------------------------------------------------------
276 OOperandAttr::OOperandAttr(sal_uInt16 _nPos,const Reference< XPropertySet>& _xColumn)
277 	: OOperandRow(_nPos,::comphelper::getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))))
278 	, m_xColumn(_xColumn)
279 {
280 }
281 // -----------------------------------------------------------------------------
282 
283 
284 
285 
286