1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include <unotools/tempfile.hxx>
27 #include "adabas/BDriver.hxx"
28 #include "adabas/BConnection.hxx"
29 #include "odbc/OFunctions.hxx"
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include "odbc/OTools.hxx"
32 #include "connectivity/dbexception.hxx"
33 #include "TConnection.hxx"
34 #include "diagnose_ex.h"
35 #include <vos/process.hxx>
36 #include <osl/process.h>
37 #include <unotools/ucbhelper.hxx>
38 #include <unotools/ucbstreamhelper.hxx>
39 #include <unotools/localfilehelper.hxx>
40 #include "resource/adabas_res.hrc"
41 #include "resource/sharedresources.hxx"
42 
43 
44 #include <memory>
45 #include <sys/stat.h>
46 
47 #if defined(UNX)
48 const char sNewLine = '\012';
49 #else
50 const char sNewLine[] = "\015\012"; // \015\012 and not \n
51 #endif
52 #define ADABAS_DB_11			"11.02.00"
53 #define ADABAS_KERNEL_11		"11.02"
54 #define ADABAS_DB_12			"12.01.00"
55 #define ADABAS_KERNEL_12		"12.01"
56 #define CURRENT_DB_VERSION		"13.01.00"
57 #define CURRENT_KERNEL_VERSION	"13.01"
58 
59 #define OPROCESS_ADABAS 	(OProcess::TOption_Hidden | OProcess::TOption_Wait | OProcess::TOption_SearchPath)
60 #define OPROCESS_ADABAS_DBG (OProcess::TOption_Wait | OProcess::TOption_SearchPath)
61 
62 
63 using namespace connectivity;
64 namespace connectivity
65 {
66 	namespace adabas
67 	{
68 		// extern declaration of the function pointer
69 	extern T3SQLAllocHandle pODBC3SQLAllocHandle;
70 	extern T3SQLConnect pODBC3SQLConnect;
71 	extern T3SQLDriverConnect pODBC3SQLDriverConnect;
72 	extern T3SQLBrowseConnect pODBC3SQLBrowseConnect;
73 	extern T3SQLDataSources pODBC3SQLDataSources;
74 	extern T3SQLDrivers pODBC3SQLDrivers;
75 	extern T3SQLGetInfo pODBC3SQLGetInfo;
76 	extern T3SQLGetFunctions pODBC3SQLGetFunctions;
77 	extern T3SQLGetTypeInfo pODBC3SQLGetTypeInfo;
78 	extern T3SQLSetConnectAttr pODBC3SQLSetConnectAttr;
79 	extern T3SQLGetConnectAttr pODBC3SQLGetConnectAttr;
80 	extern T3SQLSetEnvAttr pODBC3SQLSetEnvAttr;
81 	extern T3SQLGetEnvAttr pODBC3SQLGetEnvAttr;
82 	extern T3SQLSetStmtAttr pODBC3SQLSetStmtAttr;
83 	extern T3SQLGetStmtAttr pODBC3SQLGetStmtAttr;
84 	//extern T3SQLSetDescField pODBC3SQLSetDescField;
85 	//extern T3SQLGetDescField pODBC3SQLGetDescField;
86 	//extern T3SQLGetDescRec pODBC3SQLGetDescRec;
87 	//extern T3SQLSetDescRec pODBC3SQLSetDescRec;
88 	extern T3SQLPrepare pODBC3SQLPrepare;
89 	extern T3SQLBindParameter pODBC3SQLBindParameter;
90 	//extern T3SQLGetCursorName pODBC3SQLGetCursorName;
91 	extern T3SQLSetCursorName pODBC3SQLSetCursorName;
92 	extern T3SQLExecute pODBC3SQLExecute;
93 	extern T3SQLExecDirect pODBC3SQLExecDirect;
94 	//extern T3SQLNativeSql pODBC3SQLNativeSql;
95 	extern T3SQLDescribeParam pODBC3SQLDescribeParam;
96 	extern T3SQLNumParams pODBC3SQLNumParams;
97 	extern T3SQLParamData pODBC3SQLParamData;
98 	extern T3SQLPutData pODBC3SQLPutData;
99 	extern T3SQLRowCount pODBC3SQLRowCount;
100 	extern T3SQLNumResultCols pODBC3SQLNumResultCols;
101 	extern T3SQLDescribeCol pODBC3SQLDescribeCol;
102 	extern T3SQLColAttribute pODBC3SQLColAttribute;
103 	extern T3SQLBindCol pODBC3SQLBindCol;
104 	extern T3SQLFetch pODBC3SQLFetch;
105 	extern T3SQLFetchScroll pODBC3SQLFetchScroll;
106 	extern T3SQLGetData pODBC3SQLGetData;
107 	extern T3SQLSetPos pODBC3SQLSetPos;
108 	extern T3SQLBulkOperations pODBC3SQLBulkOperations;
109 	extern T3SQLMoreResults pODBC3SQLMoreResults;
110 	//extern T3SQLGetDiagField pODBC3SQLGetDiagField;
111 	extern T3SQLGetDiagRec pODBC3SQLGetDiagRec;
112 	extern T3SQLColumnPrivileges pODBC3SQLColumnPrivileges;
113 	extern T3SQLColumns pODBC3SQLColumns;
114 	extern T3SQLForeignKeys pODBC3SQLForeignKeys;
115 	extern T3SQLPrimaryKeys pODBC3SQLPrimaryKeys;
116 	extern T3SQLProcedureColumns pODBC3SQLProcedureColumns;
117 	extern T3SQLProcedures pODBC3SQLProcedures;
118 	extern T3SQLSpecialColumns pODBC3SQLSpecialColumns;
119 	extern T3SQLStatistics pODBC3SQLStatistics;
120 	extern T3SQLTablePrivileges pODBC3SQLTablePrivileges;
121 	extern T3SQLTables pODBC3SQLTables;
122 	extern T3SQLFreeStmt pODBC3SQLFreeStmt;
123 	extern T3SQLCloseCursor pODBC3SQLCloseCursor;
124 	extern T3SQLCancel pODBC3SQLCancel;
125 	extern T3SQLEndTran pODBC3SQLEndTran;
126 	extern T3SQLDisconnect pODBC3SQLDisconnect;
127 	extern T3SQLFreeHandle pODBC3SQLFreeHandle;
128 	extern T3SQLGetCursorName pODBC3SQLGetCursorName;
129 	extern T3SQLNativeSql pODBC3SQLNativeSql;
130 
131 using namespace ::com::sun::star::uno;
132 using namespace ::com::sun::star::beans;
133 using namespace ::com::sun::star::sdbcx;
134 using namespace ::com::sun::star::sdbc;
135 using namespace ::com::sun::star::container;
136 using namespace ::com::sun::star::lang;
137 using namespace utl;
138 using namespace osl;
139 using namespace vos;
140 using namespace ::dbtools;
141 
142 	sal_Bool LoadFunctions(oslModule pODBCso);
143 	sal_Bool LoadLibrary_ADABAS(::rtl::OUString &_rPath);
144     // --------------------------------------------------------------------------------
145 void ODriver::fillInfo(const Sequence< PropertyValue >& info, TDatabaseStruct& _rDBInfo)
146 {
147     const PropertyValue* pIter = info.getConstArray();
148 	const PropertyValue* pEnd = pIter + info.getLength();
149 	for(;pIter != pEnd;++pIter)
150 	{
151         if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DatabaseName"))))
152 		{
153 			pIter->Value >>= _rDBInfo.sDBName;
154 		}
155 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ControlUser"))))
156 		{
157 			pIter->Value >>= _rDBInfo.sControlUser;
158 		}
159 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ControlPassword"))))
160 		{
161 			pIter->Value >>= _rDBInfo.sControlPassword;
162 		}
163         else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataCacheSizeIncrement"))))
164 			pIter->Value >>= _rDBInfo.nDataIncrement;
165         else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShutdownDatabase"))))
166 			pIter->Value >>= _rDBInfo.bShutDown;
167 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("User"))))
168 		{
169 			pIter->Value >>= _rDBInfo.sSysUser;
170 		}
171 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Password"))))
172 		{
173 			pIter->Value >>= _rDBInfo.sSysPassword;
174 		}
175 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DomainPassword"))))
176 		{
177 			pIter->Value >>= _rDBInfo.sDomainPassword;
178 		}
179 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CacheSize"))))
180 		{
181 			pIter->Value >>= _rDBInfo.sCacheSize;
182 		}
183 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("RestoreDatabase"))))
184 		{
185 			pIter->Value >>= _rDBInfo.bRestoreDatabase;
186 		}
187 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Backup"))))
188 		{
189 			pIter->Value >>= _rDBInfo.sBackupFile;
190 		}
191 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataDevSpace"))))
192 		{
193 			pIter->Value >>= _rDBInfo.sDataDevName;
194 		}
195 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SYSDEVSPACE"))))
196 		{
197 			pIter->Value >>= _rDBInfo.sSysDevSpace;
198 		}
199 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TRANSACTION_LOG"))))
200 		{
201 			pIter->Value >>= _rDBInfo.sTransLogName;
202 		}
203 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataDevSize"))))
204 		{
205 			pIter->Value >>= _rDBInfo.nDataSize;
206 		}
207 		else if(pIter->Name.equalsIgnoreAsciiCase(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("LogDevSize"))))
208 		{
209 			pIter->Value >>= _rDBInfo.nLogSize;
210 		}
211 	}
212 }
213 
214 
215 // --------------------------------------------------------------------------------
216 ODriver::ODriver(const Reference< XMultiServiceFactory >& _rxFactory) : ODBCDriver(_rxFactory)
217 {
218     osl_incrementInterlockedCount(&m_refCount);
219 	fillEnvironmentVariables();
220 	Reference< XComponent >  xComponent(m_xORB, UNO_QUERY);
221 	if (xComponent.is())
222 	{
223 		Reference< ::com::sun::star::lang::XEventListener> xEvtL((::cppu::OWeakObject*)this,UNO_QUERY);
224 		xComponent->addEventListener(xEvtL);
225 	}
226 	osl_decrementInterlockedCount(&m_refCount);
227 }
228 //------------------------------------------------------------------------------
229 void ODriver::disposing()
230 {
231 	::osl::MutexGuard aGuard(m_aMutex);
232 	ODriver_BASE::disposing();
233     Reference< XComponent >  xComponent(m_xORB, UNO_QUERY);
234 	if (xComponent.is())
235 	{
236 		Reference< XEventListener> xEvtL((::cppu::OWeakObject*)this,UNO_QUERY);
237 		xComponent->removeEventListener(xEvtL);
238 	}
239     m_xORB.clear();
240 }
241 
242 // static ServiceInfo
243 //------------------------------------------------------------------------------
244 rtl::OUString ODriver::getImplementationName_Static(  ) throw(RuntimeException)
245 {
246     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.sdbcx.adabas.ODriver"));
247 }
248 //------------------------------------------------------------------------------
249 Sequence< ::rtl::OUString > ODriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
250 {
251 	Sequence< ::rtl::OUString > aSNS( 2 );
252     aSNS[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdbc.Driver"));
253     aSNS[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdbcx.Driver"));
254 	return aSNS;
255 }
256 //------------------------------------------------------------------
257 ::rtl::OUString SAL_CALL ODriver::getImplementationName(  ) throw(RuntimeException)
258 {
259 	return getImplementationName_Static();
260 }
261 
262 //------------------------------------------------------------------
263 sal_Bool SAL_CALL ODriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
264 {
265 	const Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
266 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
267 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
268 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
269 		;
270 
271 	return pSupported != pEnd;
272 }
273 //------------------------------------------------------------------
274 Sequence< ::rtl::OUString > SAL_CALL ODriver::getSupportedServiceNames(  ) throw(RuntimeException)
275 {
276 	return getSupportedServiceNames_Static();
277 }
278 //------------------------------------------------------------------
279 Any SAL_CALL ODriver::queryInterface( const Type & rType ) throw(RuntimeException)
280 {
281 	Any aRet = ::cppu::queryInterface(rType, static_cast<XDataDefinitionSupplier*>(this));
282     if ( !aRet.hasValue() )
283         aRet = ODriver_BASE::queryInterface(rType);
284 	return aRet.hasValue() ? aRet : ODriver_BASE2::queryInterface(rType);
285 }
286 //------------------------------------------------------------------
287 Reference< XInterface >  SAL_CALL ODriver_CreateInstance(const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFac) throw( Exception )
288 {
289 	return *(new ODriver(_rxFac));
290 }
291 // -----------------------------------------------------------------------------
292 void SAL_CALL ODriver::disposing( const EventObject& Source ) throw(RuntimeException)
293 {
294     ::osl::MutexGuard aGuard( m_aMutex );
295 
296 	if(m_xORB.is() && Reference< XMultiServiceFactory >(Source.Source,UNO_QUERY) == m_xORB)
297 	{
298 		TDatabaseMap::iterator aIter = m_aDatabaseMap.begin();
299 		for(;aIter != m_aDatabaseMap.end();++aIter)
300 		{
301 			if(aIter->second.bShutDown)
302 			{
303 				::rtl::OUString sName;
304 				if(getDBName(aIter->first,sName))
305 				{
306 					XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SHUTDOWN QUICK")),sName,aIter->second.sControlUser,aIter->second.sControlPassword);
307 					X_STOP(sName);
308 				}
309 			}
310 		}
311 	m_xORB.clear();
312 	}
313 }
314 // --------------------------------------------------------------------------------
315 Reference< XConnection > SAL_CALL ODriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
316 {
317 	if ( ! acceptsURL(url) )
318 		return NULL;
319 
320     ::osl::MutexGuard aGuard( m_aMutex );
321 	if (ODriver_BASE::rBHelper.bDisposed)
322 		throw DisposedException();
323 
324     TDatabaseStruct aDBInfo;
325 	aDBInfo.bShutDown = sal_False;
326     fillInfo(info,aDBInfo);
327     aDBInfo.sControlUser = aDBInfo.sControlUser.toAsciiUpperCase();
328     aDBInfo.sControlPassword = aDBInfo.sControlPassword.toAsciiUpperCase();
329     aDBInfo.sSysUser = aDBInfo.sSysUser.toAsciiUpperCase();
330     aDBInfo.sSysPassword = aDBInfo.sSysPassword.toAsciiUpperCase();
331 
332 
333 	TDatabaseMap::iterator aFind = m_aDatabaseMap.find(url);
334 	if(aFind == m_aDatabaseMap.end()) // only when not found yet
335 		m_aDatabaseMap[url] = aDBInfo;
336 	else
337 	{
338 		if(aFind->second.bShutDown != aDBInfo.bShutDown)
339 			aFind->second.bShutDown &= aDBInfo.bShutDown;
340 	}
341 
342 	::rtl::OUString sName;
343 	if(aDBInfo.sControlPassword.getLength() && aDBInfo.sControlUser.getLength() && getDBName(url,sName))
344 	{
345 		// check if we have to add a new data dev space
346 		checkAndInsertNewDevSpace(sName,aDBInfo);
347 
348 		convertOldVersion(sName,aDBInfo);
349 		// check if we must restart the database
350 		checkAndRestart(sName,aDBInfo);
351 	}
352 
353 
354 	if(!m_pDriverHandle)
355 	{
356 		::rtl::OUString aPath;
357 		if(!EnvironmentHandle(aPath))
358 			throw SQLException(aPath,*this,::rtl::OUString(),1000,Any());
359 	}
360     OAdabasConnection* pCon = new OAdabasConnection(m_pDriverHandle,this);
361 	Reference< XConnection > xCon = pCon;
362 	SQLRETURN nSQLRETURN = pCon->Construct(url,info);
363 
364 	if (nSQLRETURN == SQL_ERROR || nSQLRETURN == SQL_NO_DATA)
365 	{
366 		odbc::OTools::ThrowException(pCon,nSQLRETURN,pCon->getConnection(),SQL_HANDLE_DBC,*this);
367 	}
368 
369 	m_xConnections.push_back(WeakReferenceHelper(*pCon));
370 
371 	return xCon;
372 }
373 // -----------------------------------------------------------------------------
374 sal_Bool ODriver::getDBName(const ::rtl::OUString& _rName,::rtl::OUString& sDBName) const
375 {
376 	sDBName = ::rtl::OUString();
377 	::rtl::OUString sName = _rName.copy(12);
378 	sal_Int32 nPos = sName.indexOf(':');
379 	if(nPos != -1 && nPos < 1)
380 		sDBName = sName.copy(1);
381 	return (nPos != -1 && nPos < 1);
382 }
383 // --------------------------------------------------------------------------------
384 sal_Bool SAL_CALL ODriver::acceptsURL( const ::rtl::OUString& url )
385 		throw(SQLException, RuntimeException)
386 {
387 	return (!url.compareTo(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:adabas:")),12));
388 }
389 // --------------------------------------------------------------------------------
390 Sequence< DriverPropertyInfo > SAL_CALL ODriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& /*info*/) throw(SQLException, RuntimeException)
391 {
392 	if ( acceptsURL(url) )
393 	{
394 		::std::vector< DriverPropertyInfo > aDriverInfo;
395 
396 		aDriverInfo.push_back(DriverPropertyInfo(
397 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShutdownDatabase"))
398 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Shut down service when closing."))
399 				,sal_False
400 				,::rtl::OUString()
401 				,Sequence< ::rtl::OUString >())
402 				);
403 		aDriverInfo.push_back(DriverPropertyInfo(
404 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ControlUser"))
405 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Control user name."))
406 				,sal_False
407 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"))
408 				,Sequence< ::rtl::OUString >())
409 				);
410 		aDriverInfo.push_back(DriverPropertyInfo(
411 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ControlPassword"))
412 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Control password."))
413 				,sal_False
414 				,::rtl::OUString()
415 				,Sequence< ::rtl::OUString >())
416 				);
417 		aDriverInfo.push_back(DriverPropertyInfo(
418 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataCacheSizeIncrement"))
419 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Data increment (MB)."))
420 				,sal_False
421 				,::rtl::OUString()
422 				,Sequence< ::rtl::OUString >())
423 				);
424 		aDriverInfo.push_back(DriverPropertyInfo(
425 				::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharSet"))
426 				,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharSet of the database."))
427 				,sal_False
428 				,::rtl::OUString()
429 				,Sequence< ::rtl::OUString >())
430 				);
431 		return Sequence< DriverPropertyInfo >(&aDriverInfo[0],aDriverInfo.size());
432 	}
433 
434 	SharedResources aResources;
435     const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
436 	::dbtools::throwGenericSQLException(sMessage ,*this);
437 	return Sequence< DriverPropertyInfo >();
438 }
439 // --------------------------------------------------------------------------------
440 sal_Int32 SAL_CALL ODriver::getMajorVersion(  ) throw(RuntimeException)
441 {
442 	return 1;
443 }
444 // --------------------------------------------------------------------------------
445 sal_Int32 SAL_CALL ODriver::getMinorVersion(  ) throw(RuntimeException)
446 {
447 	return 0;
448 }
449 // -----------------------------------------------------------------------------
450 // XCreateCatalog
451 void SAL_CALL ODriver::createCatalog( const Sequence< PropertyValue >& info ) throw(SQLException, ElementExistException, RuntimeException)
452 {
453 	::osl::MutexGuard aGuard( m_aMutex );
454 	if (ODriver_BASE::rBHelper.bDisposed)
455 		throw DisposedException();
456 
457 	try
458 	{
459 		TDatabaseStruct aDBInfo;
460         fillInfo(info,aDBInfo);
461 
462 		::rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("DBSERVICE"));
463 		::rtl::OUString envData(RTL_CONSTASCII_USTRINGPARAM("0"));
464 		osl_setEnvironment(envVar.pData, envData.pData);
465 
466 		m_sDbRunDir = m_sDbWorkURL + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/wrk/")) + aDBInfo.sDBName;
467 		String sTemp;
468 		LocalFileHelper::ConvertURLToPhysicalName(m_sDbRunDir,sTemp);
469 		m_sDbRunDir = sTemp;
470 
471 		createNeededDirs(aDBInfo.sDBName);
472 		if(CreateFiles(aDBInfo))
473         {
474             ::connectivity::SharedResources aResources;
475             const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
476                     STR_NO_DISK_SPACE,
477                     "$filename$",aDBInfo.sDBName
478                  ) );
479 	        ::dbtools::throwGenericSQLException(sError,*this);
480         } // if(CreateFiles(aDBInfo))
481 
482 		createDb(aDBInfo);
483 	}
484 	catch( SQLException&)
485 	{
486 		throw;
487 	}
488 	catch(Exception&)
489 	{
490 		throw SQLException();
491 	}
492 
493 }
494 // -----------------------------------------------------------------------------
495 // XDropCatalog
496 void SAL_CALL ODriver::dropCatalog( const ::rtl::OUString& /*catalogName*/, const Sequence< PropertyValue >& /*info*/ ) throw(SQLException, NoSuchElementException, RuntimeException)
497 {
498 	::osl::MutexGuard aGuard( m_aMutex );
499 	if (ODriver_BASE::rBHelper.bDisposed)
500 		throw DisposedException();
501 
502     ::dbtools::throwFeatureNotImplementedException( "!XDropCatalog::dropCatalog", *this );
503 }
504 //-----------------------------------------------------------------------------
505 // ODBC Environment (gemeinsam fuer alle Connections):
506 SQLHANDLE ODriver::EnvironmentHandle(::rtl::OUString &_rPath)
507 {
508 	// Ist (fuer diese Instanz) bereits ein Environment erzeugt worden?
509 	if (!m_pDriverHandle)
510 	{
511 		SQLHANDLE h = SQL_NULL_HANDLE;
512 		// Environment allozieren
513 
514 		// ODBC-DLL jetzt laden:
515 		if (! LoadLibrary_ADABAS(_rPath))
516 			return SQL_NULL_HANDLE;
517 
518 		if (N3SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&h) != SQL_SUCCESS)
519 			return SQL_NULL_HANDLE;
520 
521 		// In globaler Struktur merken ...
522 		m_pDriverHandle = h;
523 		SQLRETURN nError = N3SQLSetEnvAttr(h, SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
524         OSL_UNUSED( nError );
525 		//N3SQLSetEnvAttr(h, SQL_ATTR_CONNECTION_POOLING,(SQLPOINTER) SQL_CP_ONE_PER_HENV, SQL_IS_INTEGER);
526 	}
527 
528 	return m_pDriverHandle;
529 }
530 // --------------------------------------------------------------------------------
531 // XDataDefinitionSupplier
532 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByConnection( const Reference< ::com::sun::star::sdbc::XConnection >& connection ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
533 {
534 	::osl::MutexGuard aGuard( m_aMutex );
535 	if (ODriver_BASE::rBHelper.bDisposed)
536 		throw DisposedException();
537 
538 	Reference< XTablesSupplier > xTab;
539 	Reference< ::com::sun::star::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
540 	if ( xTunnel.is() )
541 	{
542 
543 		OAdabasConnection* pConnection = NULL;
544 		OAdabasConnection* pSearchConnection = reinterpret_cast< OAdabasConnection* >( xTunnel->getSomething(OAdabasConnection::getUnoTunnelImplementationId()) );
545 		for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
546 		{
547 			if ( (OAdabasConnection*) Reference< XConnection >::query(i->get().get()).get() == pSearchConnection )
548 			{
549 				pConnection = pSearchConnection;
550 				break;
551 			}
552 		}
553 
554 
555 		if ( pConnection )
556 			xTab = pConnection->createCatalog();
557 	}
558 	return xTab;
559 }
560 
561 // --------------------------------------------------------------------------------
562 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByURL( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
563 {
564 	if ( ! acceptsURL(url) )
565     {
566         SharedResources aResources;
567         const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
568 		::dbtools::throwGenericSQLException(sMessage ,*this);
569     } // if ( ! acceptsURL(url) )
570 
571 	return getDataDefinitionByConnection(connect(url,info));
572 }
573 // -----------------------------------------------------------------------------
574 void SAL_CALL ODriver::acquire() throw()
575 {
576 	ODriver_BASE::acquire();
577 }
578 // -----------------------------------------------------------------------------
579 void SAL_CALL ODriver::release() throw()
580 {
581 	ODriver_BASE::release();
582 }
583 ODriver::~ODriver()
584 {
585 }
586 // -----------------------------------------------------------------------------
587 oslGenericFunction ODriver::getOdbcFunction(sal_Int32 _nIndex) const
588 {
589 	oslGenericFunction pFunction = NULL;
590 	switch(_nIndex)
591 	{
592 		case ODBC3SQLAllocHandle:
593 			pFunction = (oslGenericFunction)pODBC3SQLAllocHandle;;
594 			break;
595 		case ODBC3SQLConnect:
596 			pFunction = (oslGenericFunction)pODBC3SQLConnect;
597 			break;
598 		case ODBC3SQLDriverConnect:
599 			pFunction = (oslGenericFunction)pODBC3SQLDriverConnect;
600 			break;
601 		case ODBC3SQLBrowseConnect:
602 			pFunction = (oslGenericFunction)pODBC3SQLBrowseConnect;
603 			break;
604 		case ODBC3SQLDataSources:
605 			pFunction = (oslGenericFunction)pODBC3SQLDataSources;
606 			break;
607 		case ODBC3SQLDrivers:
608 			pFunction = (oslGenericFunction)pODBC3SQLDrivers;
609 			break;
610 		case ODBC3SQLGetInfo:
611 
612 			pFunction = (oslGenericFunction)pODBC3SQLGetInfo;
613 			break;
614 		case ODBC3SQLGetFunctions:
615 
616 			pFunction = (oslGenericFunction)pODBC3SQLGetFunctions;
617 			break;
618 		case ODBC3SQLGetTypeInfo:
619 
620 			pFunction = (oslGenericFunction)pODBC3SQLGetTypeInfo;
621 			break;
622 		case ODBC3SQLSetConnectAttr:
623 
624 			pFunction = (oslGenericFunction)pODBC3SQLSetConnectAttr;
625 			break;
626 		case ODBC3SQLGetConnectAttr:
627 
628 			pFunction = (oslGenericFunction)pODBC3SQLGetConnectAttr;
629 			break;
630 		case ODBC3SQLSetEnvAttr:
631 
632 			pFunction = (oslGenericFunction)pODBC3SQLSetEnvAttr;
633 			break;
634 		case ODBC3SQLGetEnvAttr:
635 
636 			pFunction = (oslGenericFunction)pODBC3SQLGetEnvAttr;
637 			break;
638 		case ODBC3SQLSetStmtAttr:
639 
640 			pFunction = (oslGenericFunction)pODBC3SQLSetStmtAttr;
641 			break;
642 		case ODBC3SQLGetStmtAttr:
643 
644 			pFunction = (oslGenericFunction)pODBC3SQLGetStmtAttr;
645 			break;
646 		case ODBC3SQLPrepare:
647 
648 			pFunction = (oslGenericFunction)pODBC3SQLPrepare;
649 			break;
650 		case ODBC3SQLBindParameter:
651 
652 			pFunction = (oslGenericFunction)pODBC3SQLBindParameter;
653 			break;
654 		case ODBC3SQLSetCursorName:
655 
656 			pFunction = (oslGenericFunction)pODBC3SQLSetCursorName;
657 			break;
658 		case ODBC3SQLExecute:
659 
660 			pFunction = (oslGenericFunction)pODBC3SQLExecute;
661 			break;
662 		case ODBC3SQLExecDirect:
663 
664 			pFunction = (oslGenericFunction)pODBC3SQLExecDirect;
665 			break;
666 		case ODBC3SQLDescribeParam:
667 
668 			pFunction = (oslGenericFunction)pODBC3SQLDescribeParam;
669 			break;
670 		case ODBC3SQLNumParams:
671 
672 			pFunction = (oslGenericFunction)pODBC3SQLNumParams;
673 			break;
674 		case ODBC3SQLParamData:
675 
676 			pFunction = (oslGenericFunction)pODBC3SQLParamData;
677 			break;
678 		case ODBC3SQLPutData:
679 
680 			pFunction = (oslGenericFunction)pODBC3SQLPutData;
681 			break;
682 		case ODBC3SQLRowCount:
683 
684 			pFunction = (oslGenericFunction)pODBC3SQLRowCount;
685 			break;
686 		case ODBC3SQLNumResultCols:
687 
688 			pFunction = (oslGenericFunction)pODBC3SQLNumResultCols;
689 			break;
690 		case ODBC3SQLDescribeCol:
691 
692 			pFunction = (oslGenericFunction)pODBC3SQLDescribeCol;
693 			break;
694 		case ODBC3SQLColAttribute:
695 
696 			pFunction = (oslGenericFunction)pODBC3SQLColAttribute;
697 			break;
698 		case ODBC3SQLBindCol:
699 
700 			pFunction = (oslGenericFunction)pODBC3SQLBindCol;
701 			break;
702 		case ODBC3SQLFetch:
703 
704 			pFunction = (oslGenericFunction)pODBC3SQLFetch;
705 			break;
706 		case ODBC3SQLFetchScroll:
707 
708 			pFunction = (oslGenericFunction)pODBC3SQLFetchScroll;
709 			break;
710 		case ODBC3SQLGetData:
711 
712 			pFunction = (oslGenericFunction)pODBC3SQLGetData;
713 			break;
714 		case ODBC3SQLSetPos:
715 
716 			pFunction = (oslGenericFunction)pODBC3SQLSetPos;
717 			break;
718 		case ODBC3SQLBulkOperations:
719 
720 			pFunction = (oslGenericFunction)pODBC3SQLBulkOperations;
721 			break;
722 		case ODBC3SQLMoreResults:
723 
724 			pFunction = (oslGenericFunction)pODBC3SQLMoreResults;
725 			break;
726 		case ODBC3SQLGetDiagRec:
727 
728 			pFunction = (oslGenericFunction)pODBC3SQLGetDiagRec;
729 			break;
730 		case ODBC3SQLColumnPrivileges:
731 
732 			pFunction = (oslGenericFunction)pODBC3SQLColumnPrivileges;
733 			break;
734 		case ODBC3SQLColumns:
735 
736 			pFunction = (oslGenericFunction)pODBC3SQLColumns;
737 			break;
738 		case ODBC3SQLForeignKeys:
739 
740 			pFunction = (oslGenericFunction)pODBC3SQLForeignKeys;
741 			break;
742 		case ODBC3SQLPrimaryKeys:
743 
744 			pFunction = (oslGenericFunction)pODBC3SQLPrimaryKeys;
745 			break;
746 		case ODBC3SQLProcedureColumns:
747 
748 			pFunction = (oslGenericFunction)pODBC3SQLProcedureColumns;
749 			break;
750 		case ODBC3SQLProcedures:
751 
752 			pFunction = (oslGenericFunction)pODBC3SQLProcedures;
753 			break;
754 		case ODBC3SQLSpecialColumns:
755 
756 			pFunction = (oslGenericFunction)pODBC3SQLSpecialColumns;
757 			break;
758 		case ODBC3SQLStatistics:
759 
760 			pFunction = (oslGenericFunction)pODBC3SQLStatistics;
761 			break;
762 		case ODBC3SQLTablePrivileges:
763 
764 			pFunction = (oslGenericFunction)pODBC3SQLTablePrivileges;
765 			break;
766 		case ODBC3SQLTables:
767 
768 			pFunction = (oslGenericFunction)pODBC3SQLTables;
769 			break;
770 		case ODBC3SQLFreeStmt:
771 
772 			pFunction = (oslGenericFunction)pODBC3SQLFreeStmt;
773 			break;
774 		case ODBC3SQLCloseCursor:
775 
776 			pFunction = (oslGenericFunction)pODBC3SQLCloseCursor;
777 			break;
778 		case ODBC3SQLCancel:
779 
780 			pFunction = (oslGenericFunction)pODBC3SQLCancel;
781 			break;
782 		case ODBC3SQLEndTran:
783 
784 			pFunction = (oslGenericFunction)pODBC3SQLEndTran;
785 			break;
786 		case ODBC3SQLDisconnect:
787 
788 			pFunction = (oslGenericFunction)pODBC3SQLDisconnect;
789 			break;
790 		case ODBC3SQLFreeHandle:
791 
792 			pFunction = (oslGenericFunction)pODBC3SQLFreeHandle;
793 			break;
794 		case ODBC3SQLGetCursorName:
795 
796 			pFunction = (oslGenericFunction)pODBC3SQLGetCursorName;
797 			break;
798 		case ODBC3SQLNativeSql:
799 
800 			pFunction = (oslGenericFunction)pODBC3SQLNativeSql;
801 			break;
802 		default:
803 			OSL_ENSURE(0,"Function unknown!");
804 	}
805 	return pFunction;
806 }
807 // -----------------------------------------------------------------------------
808 void ODriver::createNeededDirs(const ::rtl::OUString& sDBName)
809 {
810 	::rtl::OUString sDbWork,sDBConfig,sTemp;
811 
812 	if(m_sDbWork.getLength())
813 	{
814 		sDbWork = m_sDbWorkURL;
815 		if(!UCBContentHelper::IsFolder(m_sDbWorkURL))
816 			UCBContentHelper::MakeFolder(m_sDbWorkURL);
817 
818 		sDbWork += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
819 		sDbWork += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrk"));
820 		if(!UCBContentHelper::IsFolder(sDbWork))
821 			UCBContentHelper::MakeFolder(sDbWork);
822 
823 		sDbWork += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
824 
825 		sTemp = sDbWork;
826 		sTemp += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("config"));
827 		if(!UCBContentHelper::IsFolder(sTemp))
828 			UCBContentHelper::MakeFolder(sTemp);
829 
830 		sTemp = sDbWork;
831 		sTemp += sDBName;
832 		if(!UCBContentHelper::IsFolder(sTemp))
833 			UCBContentHelper::MakeFolder(sTemp);
834 	}
835 
836 	if(m_sDbConfig.getLength())
837 	{
838 		sDBConfig = m_sDbConfigURL;
839 		if(!UCBContentHelper::IsFolder(sDBConfig))
840 			UCBContentHelper::MakeFolder(sDBConfig);
841 
842 		sDBConfig += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
843 		sTemp = sDBConfig;
844 		sTemp += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("config"));
845 		if(!UCBContentHelper::IsFolder(sTemp))
846 			UCBContentHelper::MakeFolder(sTemp);
847 
848 		sTemp += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
849 		sTemp += sDBName;
850 		if(UCBContentHelper::Exists(sTemp))
851 			UCBContentHelper::Kill(sTemp);
852 
853 #if !(defined(WNT))
854 		sTemp = sDBConfig;
855 		sTemp += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("diag"));
856 		if(!UCBContentHelper::IsFolder(sTemp))
857 			UCBContentHelper::MakeFolder(sTemp);
858 
859 		sTemp = sDBConfig;
860 		sTemp += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ipc"));
861 		if(!UCBContentHelper::IsFolder(sTemp))
862 			UCBContentHelper::MakeFolder(sTemp);
863 
864 		sTemp = sDBConfig;
865 		sTemp += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("spool"));
866 		if(!UCBContentHelper::IsFolder(sTemp))
867 			UCBContentHelper::MakeFolder(sTemp);
868 #endif
869 	}
870 }
871 // -----------------------------------------------------------------------------
872 void ODriver::clearDatabase(const ::rtl::OUString& sDBName)
873 { // stop the database
874 	::rtl::OUString sCommand;
875 #if defined(WNT)
876 	::rtl::OUString sStop = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("stop"));
877 	OArgumentList aArgs(2,&sDBName,&sStop);
878 	sCommand =	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("x_cons.exe"));
879 #else
880 	OArgumentList aArgs(1,&sDBName);
881 	sCommand =	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("x_clear"));
882 #endif
883 
884 	OProcess aApp( sCommand,m_sDbWorkURL);
885 #if OSL_DEBUG_LEVEL > 0
886 	OProcess::TProcessError eError =
887 #endif
888         aApp.execute( (OProcess::TProcessOption) OPROCESS_ADABAS, aArgs );
889     OSL_ENSURE( eError == OProcess::E_None, "ODriver::clearDatabase: calling the executable failed!" );
890 }
891 // -----------------------------------------------------------------------------
892 void ODriver::createDb( const TDatabaseStruct& _aInfo)
893 {
894 
895 	clearDatabase(_aInfo.sDBName);
896 
897 	X_PARAM(_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword,String::CreateFromAscii("BINIT"));
898 
899 	String sTemp;
900 	LocalFileHelper::ConvertURLToPhysicalName(_aInfo.sSysDevSpace,sTemp);
901 
902 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SYSDEVSPACE")),sTemp);
903 
904 	sTemp.Erase();
905 	LocalFileHelper::ConvertURLToPhysicalName(_aInfo.sTransLogName,sTemp);
906     PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TRANSACTION_LOG")),sTemp);
907 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAXUSERTASKS")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("3")));
908 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAXDEVSPACES")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("7")));
909 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAXDATADEVSPACES")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("5")));
910 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAXDATAPAGES")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("25599")));
911 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAXBACKUPDEVS")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("1")));
912 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MAXSERVERDB")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("1")));
913 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DATA_CACHE_PAGES")),_aInfo.sCacheSize);
914 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CONV_CACHE_PAGES")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("23")));
915 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("PROC_DATA_PAGES")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("40")));
916 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("RUNDIRECTORY")),m_sDbRunDir);
917 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("KERNELTRACESIZE")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("100")));
918 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("LOG_QUEUE_PAGES")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("10")));
919 
920 #if !defined(WNT)
921 	PutParam(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OPMSG1")),::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/dev/null")));
922 #endif
923 
924 	X_PARAM(_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BCHECK")));
925 
926 	X_START(_aInfo.sDBName);
927 
928 	//	SHOW_STATE()
929 	//	%m_sDbRoot%\bin\xutil -d %_aInfo.sDBName% -u %CONUSR%,%CONPWD% -b %INITCMD%
930 	::rtl::OUString aBatch2 =  ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-b "));
931 
932 //	if(!bBsp && INITCMD.Len() >= 40)
933 //	{
934 //		DirEntry aTmp(INITCMD);
935 //		aTmp.CopyTo(aInitFile, FSYS_ACTION_COPYFILE);
936 //		INITCMD = aInitFile.GetFull();
937 //	}
938 	// generate the init file for the database
939 	String sInitFile = getDatabaseInitFile(_aInfo);
940 
941 	LocalFileHelper::ConvertURLToPhysicalName(sInitFile,sTemp);
942 	aBatch2 += sTemp;
943 	XUTIL(aBatch2,_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
944 #if OSL_DEBUG_LEVEL < 2
945 	if(UCBContentHelper::Exists(sInitFile))
946 		UCBContentHelper::Kill(sInitFile);
947 #endif
948 
949 	// install system tables
950 	installSystemTables(_aInfo);
951 	// now we have to make our SYSDBA user "NOT EXCLUSIVE"
952 	{
953 		String sExt;
954 		sExt.AssignAscii(".sql");
955 
956 		String sWorkUrl(m_sDbWorkURL);
957 		::utl::TempFile aInitFile(String::CreateFromAscii("Init"),&sExt,&sWorkUrl);
958 		aInitFile.EnableKillingFile();
959 		{
960 			::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(aInitFile.GetURL(),STREAM_WRITE) );
961 			(*pFileStream)	<< "ALTER USER \""
962 							<< ::rtl::OString(_aInfo.sSysUser,_aInfo.sSysUser.getLength(),gsl_getSystemTextEncoding())
963 							<< "\" NOT EXCLUSIVE "
964 							<< sNewLine;
965 			pFileStream->Flush();
966 		}
967 		{ // just to get sure that the tempfile still lives
968 			sTemp.Erase();
969 			LocalFileHelper::ConvertURLToPhysicalName(aInitFile.GetURL(),sTemp);
970 			LoadBatch(_aInfo.sDBName,_aInfo.sSysUser,_aInfo.sSysPassword,sTemp);
971 		}
972 	}
973 }
974 
975 
976 //-------------------------------------------------------------------------------------------------
977 int ODriver::X_PARAM(const ::rtl::OUString& _DBNAME,
978 			const ::rtl::OUString& _USR,
979 			const ::rtl::OUString& _PWD,
980 			const ::rtl::OUString& _CMD)
981 {
982 	//	%XPARAM% -u %CONUSR%,%CONPWD% BINIT
983 	String sCommandFile = generateInitFile();
984 	{
985 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
986 		pFileStream->Seek(STREAM_SEEK_TO_END);
987 		(*pFileStream)	<< "x_param"
988 #if defined(WNT)
989 						<< ".exe"
990 #endif
991 						<< " -d "
992 						<< ::rtl::OString(_DBNAME,_DBNAME.getLength(),gsl_getSystemTextEncoding())
993 						<< " -u "
994 						<< ::rtl::OString(_USR,_USR.getLength(),gsl_getSystemTextEncoding())
995 						<< ","
996 						<< ::rtl::OString(_PWD,_PWD.getLength(),gsl_getSystemTextEncoding())
997 						<< " "
998 						<< ::rtl::OString(_CMD,_CMD.getLength(),gsl_getSystemTextEncoding())
999 #if defined(WNT)
1000 #if (OSL_DEBUG_LEVEL > 1) || defined(DBG_UTIL)
1001 						<< " >> %DBWORK%\\create.log 2>&1"
1002 #endif
1003 #else
1004 #if (OSL_DEBUG_LEVEL > 1) || defined(DBG_UTIL)
1005 						<< " >> /tmp/kstart.log"
1006 #else
1007 						<< " > /dev/null"
1008 #endif
1009 #endif
1010 						<< " "
1011 						<< sNewLine
1012 						<< sNewLine;
1013 
1014 		pFileStream->Flush();
1015 	}
1016 
1017 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1018 #if OSL_DEBUG_LEVEL > 0
1019 	OProcess::TProcessError eError =
1020 #endif
1021 	    aApp.execute( (OProcess::TProcessOption)(OProcess::TOption_Hidden | OProcess::TOption_Wait));
1022     OSL_ENSURE( eError == OProcess::E_None, "ODriver::X_PARAM: calling the executable failed!" );
1023 #if OSL_DEBUG_LEVEL < 2
1024 	if(UCBContentHelper::Exists(sCommandFile))
1025 		UCBContentHelper::Kill(sCommandFile);
1026 #endif
1027 
1028 	return 0;
1029 }
1030 // -----------------------------------------------------------------------------
1031 sal_Int32 ODriver::CreateFiles(const TDatabaseStruct& _aInfo)
1032 {
1033 	int nRet = CreateFile(_aInfo.sSysDevSpace,_aInfo.nDataSize/50) ? 0 : -9;
1034 	if(!nRet)
1035 		nRet = CreateFile(_aInfo.sTransLogName,_aInfo.nLogSize) ? 0 : -10;
1036 	if(!nRet)
1037 		nRet = CreateFile(_aInfo.sDataDevName,_aInfo.nDataSize) ? 0 : -11;
1038 
1039 	return nRet;
1040 
1041 }
1042 // -----------------------------------------------------------------------------
1043 void ODriver::PutParam(const ::rtl::OUString& sDBName,
1044 					  const ::rtl::OUString& rWhat,
1045 					  const ::rtl::OUString& rHow)
1046 {
1047 	OArgumentList aArgs(3,&sDBName,&rWhat,&rHow);
1048 	::rtl::OUString sCommand = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("putparam"));
1049 #if defined(WNT)
1050 	sCommand += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".exe"));
1051 #endif
1052 
1053 	OProcess aApp(sCommand,m_sDbWorkURL);
1054 #if OSL_DEBUG_LEVEL > 0
1055 	OProcess::TProcessError eError =
1056 #endif
1057 	    aApp.execute( (OProcess::TProcessOption)OPROCESS_ADABAS,aArgs );
1058     OSL_ENSURE( eError == OProcess::E_None, "ODriver::PutParam: calling the executable failed!" );
1059 }
1060 // -----------------------------------------------------------------------------
1061 sal_Bool ODriver::CreateFile(const ::rtl::OUString &_FileName,
1062 				sal_Int32 _nSize)
1063 {
1064 OSL_TRACE("CreateFile %d",_nSize);
1065 	sal_Bool bOK = sal_True;
1066 	try
1067 	{
1068 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(_FileName,STREAM_WRITE));
1069 		if( !pFileStream.get())
1070 		{
1071             ::connectivity::SharedResources aResources;
1072             const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
1073                     STR_NO_DISK_SPACE,
1074                     "$filename$",_FileName
1075                  ) );
1076 	        ::dbtools::throwGenericSQLException(sError,*this);
1077 		}
1078 		(*pFileStream).SetFiller('\0');
1079 		sal_Int32 nNewSize = 0;
1080 		sal_Int32 nCount = _nSize /2;
1081 		for(sal_Int32 i=0; bOK && i < nCount; ++i)
1082 		{
1083 			nNewSize += 8192;//4096;
1084 			bOK = (*pFileStream).SetStreamSize(nNewSize);
1085 			pFileStream->Flush();
1086 		}
1087 
1088 		bOK = bOK && static_cast<sal_Int32>(pFileStream->Seek(STREAM_SEEK_TO_END)) == nNewSize;
1089 	}
1090 	catch(Exception&)
1091 	{
1092 	OSL_TRACE("Exception");
1093 	}
1094 	if(!bOK)
1095 	{
1096         ::connectivity::SharedResources aResources;
1097         const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
1098                 STR_NO_DISK_SPACE,
1099                 "$filename$",_FileName
1100              ) );
1101         ::dbtools::throwGenericSQLException(sError,*this);
1102 	}
1103 
1104 	return bOK;
1105 	// dd if=/dev/zero bs=4k of=$DEV_NAME count=$2
1106 }
1107 // -----------------------------------------------------------------------------
1108 int ODriver::X_START(const ::rtl::OUString& sDBName)
1109 {
1110 	::rtl::OUString sCommand;
1111 #if defined(WNT)
1112 
1113 	::rtl::OUString sArg1 = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-d"));
1114 	::rtl::OUString sArg3 = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-NoDBService"));
1115 	::rtl::OUString sArg4 = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-NoDBWindow"));
1116 
1117 	OArgumentList aArgs(4,&sArg1,&sDBName,&sArg3,&sArg4);
1118 	sCommand =	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("strt.exe"));
1119 #else
1120 	OArgumentList aArgs(1,&sDBName);
1121 	sCommand =	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("x_start"));
1122 #endif
1123 
1124 	OProcess aApp( sCommand ,m_sDbWorkURL);
1125 	OProcess::TProcessError eError = aApp.execute((OProcess::TProcessOption)OPROCESS_ADABAS,aArgs);
1126 
1127 	if(eError == OProcess::E_NotFound)
1128 	{
1129         ::connectivity::SharedResources aResources;
1130         const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
1131                 STR_COMMAND_NOT_FOUND,
1132                 "$databasename$",sDBName,
1133                 "$progname$",sCommand
1134              ) );
1135         ::dbtools::throwGenericSQLException(sError,*this);
1136 	}
1137 	OSL_ASSERT(eError == OProcess::E_None);
1138 
1139 	OProcess::TProcessInfo aInfo;
1140 	if(aApp.getInfo(OProcess::TData_ExitCode,&aInfo) == OProcess::E_None && aInfo.Code)
1141 		return aInfo.Code;
1142 
1143 	return 0;
1144 }
1145 // -----------------------------------------------------------------------------
1146 int ODriver::X_STOP(const ::rtl::OUString& sDBName)
1147 {
1148 	::rtl::OUString sCommand;
1149 #if defined(WNT)
1150 
1151 	::rtl::OUString sArg1 = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-d"));
1152 	::rtl::OUString sArg2 = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-NoDBService"));
1153 
1154 	OArgumentList aArgs(3,&sArg1,&sDBName,&sArg2);
1155 	sCommand =	::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("stp.exe"));
1156 #else
1157 	OArgumentList aArgs(1,&sDBName);
1158 	sCommand = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("x_stop"));
1159 #endif
1160 	OProcess aApp( sCommand ,m_sDbWorkURL);
1161 
1162 	OProcess::TProcessError eError = aApp.execute((OProcess::TProcessOption)OPROCESS_ADABAS,aArgs);
1163 
1164 
1165 	OSL_ASSERT(eError == OProcess::E_None);
1166 	if(eError != OProcess::E_None)
1167 		return 1;
1168 		OProcess::TProcessInfo aInfo;
1169 	if(aApp.getInfo(OProcess::TData_ExitCode,&aInfo) == OProcess::E_None && aInfo.Code)
1170 		return aInfo.Code;
1171 
1172 	return 0;
1173 }
1174 // -----------------------------------------------------------------------------
1175 void ODriver::XUTIL(const ::rtl::OUString& _rParam,
1176 		   const ::rtl::OUString& _DBNAME,
1177 		   const ::rtl::OUString& _USRNAME,
1178 		   const ::rtl::OUString& _USRPWD)
1179 {
1180 	String sWorkUrl(m_sDbWorkURL);
1181 	String sExt = String::CreateFromAscii(".log");
1182 	::utl::TempFile aCmdFile(String::CreateFromAscii("xutil"),&sExt,&sWorkUrl);
1183 	aCmdFile.EnableKillingFile();
1184 
1185 	String sPhysicalPath;
1186 	LocalFileHelper::ConvertURLToPhysicalName(aCmdFile.GetURL(),sPhysicalPath);
1187 
1188 	String sCommandFile = generateInitFile();
1189 
1190 	{
1191 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1192 		pFileStream->Seek(STREAM_SEEK_TO_END);
1193 		(*pFileStream)	<<
1194 #if defined(WNT)
1195 							"xutil.exe"
1196 #else
1197 							"utility"
1198 #endif
1199 						<< " -u "
1200 						<< ::rtl::OString(_USRNAME,_USRNAME.getLength(),gsl_getSystemTextEncoding())
1201 						<< ","
1202 						<< ::rtl::OString(_USRPWD,_USRPWD.getLength(),gsl_getSystemTextEncoding())
1203 						<< " -d "
1204 						<< ::rtl::OString(_DBNAME,_DBNAME.getLength(),gsl_getSystemTextEncoding())
1205 						<< " "
1206 						<< ::rtl::OString(_rParam,_rParam.getLength(),gsl_getSystemTextEncoding())
1207 						<< " > "
1208 						<< ::rtl::OString(sPhysicalPath.GetBuffer(),sPhysicalPath.Len(),gsl_getSystemTextEncoding())
1209 						<< " 2>&1"
1210 						<< sNewLine;
1211 		pFileStream->Flush();
1212 	}
1213 
1214 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1215 #if OSL_DEBUG_LEVEL > 0
1216 	OProcess::TProcessError eError =
1217 #endif
1218         aApp.execute( (OProcess::TProcessOption)(OProcess::TOption_Hidden | OProcess::TOption_Wait));
1219     OSL_ENSURE( eError == OProcess::E_None, "ODriver::XUTIL: calling the executable failed!" );
1220 #if OSL_DEBUG_LEVEL < 2
1221 	if(UCBContentHelper::Exists(sCommandFile))
1222 		UCBContentHelper::Kill(sCommandFile);
1223 #endif
1224 }
1225 // -----------------------------------------------------------------------------
1226 void ODriver::LoadBatch(const ::rtl::OUString& sDBName,
1227 			   const ::rtl::OUString& _rUSR,
1228 			   const ::rtl::OUString& _rPWD,
1229 			   const ::rtl::OUString& _rBatch)
1230 {
1231 	OSL_ENSURE(_rBatch.getLength(),"No batch file given!");
1232 	String sWorkUrl(m_sDbWorkURL);
1233 	String sExt = String::CreateFromAscii(".log");
1234 	::utl::TempFile aCmdFile(String::CreateFromAscii("LoadBatch"),&sExt,&sWorkUrl);
1235 #if OSL_DEBUG_LEVEL < 2
1236 	aCmdFile.EnableKillingFile();
1237 #endif
1238 
1239 	String sPhysicalPath;
1240 	LocalFileHelper::ConvertURLToPhysicalName(aCmdFile.GetURL(),sPhysicalPath);
1241 
1242 	String sCommandFile = generateInitFile();
1243 	{
1244 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1245 		pFileStream->Seek(STREAM_SEEK_TO_END);
1246 		(*pFileStream)	<< "xload"
1247 #if defined(WNT)
1248 						<< ".exe"
1249 #endif
1250 						<< " -d "
1251 						<< ::rtl::OString(sDBName,sDBName.getLength(),gsl_getSystemTextEncoding())
1252 						<< " -u "
1253 						<< ::rtl::OString(_rUSR,_rUSR.getLength(),gsl_getSystemTextEncoding())
1254 						<< ","
1255 						<< ::rtl::OString(_rPWD,_rPWD.getLength(),gsl_getSystemTextEncoding());
1256 
1257 		if ( !isKernelVersion(CURRENT_DB_VERSION) )
1258 			(*pFileStream) << " -S adabas -b ";
1259 		else
1260 			(*pFileStream) << " -S NATIVE -b ";
1261 
1262 		(*pFileStream)	<< ::rtl::OString(_rBatch,_rBatch.getLength(),gsl_getSystemTextEncoding())
1263 						<< " > "
1264 						<< ::rtl::OString(sPhysicalPath.GetBuffer(),sPhysicalPath.Len(),gsl_getSystemTextEncoding())
1265 						<< " 2>&1"
1266 						<< sNewLine;
1267 
1268 		pFileStream->Flush();
1269 	}
1270 
1271 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1272 #if OSL_DEBUG_LEVEL > 0
1273 	OProcess::TProcessError eError =
1274 #endif
1275         aApp.execute( (OProcess::TProcessOption)(OProcess::TOption_Hidden | OProcess::TOption_Wait));
1276     OSL_ENSURE( eError == OProcess::E_None, "ODriver::LoadBatch: calling the executable failed!" );
1277 #if OSL_DEBUG_LEVEL < 2
1278 	if(UCBContentHelper::Exists(sCommandFile))
1279 		UCBContentHelper::Kill(sCommandFile);
1280 #endif
1281 }
1282 // -----------------------------------------------------------------------------
1283 void ODriver::fillEnvironmentVariables()
1284 {
1285 	// read the environment vars
1286     struct env_data
1287     {
1288         const sal_Char*     pAsciiEnvName;
1289         ::rtl::OUString*    pValue;
1290         ::rtl::OUString*    pValueURL;
1291     } EnvData[] = {
1292         { "DBWORK",     &m_sDbWork,     &m_sDbWorkURL },
1293         { "DBCONFIG",   &m_sDbConfig,   &m_sDbConfigURL },
1294         { "DBROOT",     &m_sDbRoot,     &m_sDbRootURL }
1295     };
1296 
1297     for ( size_t i = 0; i < sizeof( EnvData ) / sizeof( EnvData[0] ); ++i )
1298     {
1299 	    ::rtl::OUString sVarName = ::rtl::OUString::createFromAscii( EnvData[i].pAsciiEnvName );
1300         ::rtl::OUString sEnvValue;
1301 	    if(osl_getEnvironment( sVarName.pData, &sEnvValue.pData ) == osl_Process_E_None )
1302 	    {
1303 		    *EnvData[i].pValue = sEnvValue;
1304 		    String sURL;
1305 		    LocalFileHelper::ConvertPhysicalNameToURL( *EnvData[i].pValue, sURL );
1306 		    *EnvData[i].pValueURL = sURL;
1307 	    }
1308     }
1309 
1310 	m_sDelimit =  ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
1311 }
1312 // -----------------------------------------------------------------------------
1313 ::rtl::OUString ODriver::generateInitFile() const
1314 {
1315 	String sExt;
1316 #if !defined(WNT)
1317 	sExt = String::CreateFromAscii(".sh");
1318 #else
1319 	sExt = String::CreateFromAscii(".bat");
1320 #endif
1321 
1322 	String sWorkUrl(m_sDbWorkURL);
1323 	::utl::TempFile aCmdFile(String::CreateFromAscii("Init"),&sExt,&sWorkUrl);
1324 #if !defined(WNT)
1325 	String sPhysicalPath;
1326 	LocalFileHelper::ConvertURLToPhysicalName(aCmdFile.GetURL(),sPhysicalPath);
1327 	chmod(ByteString(sPhysicalPath,gsl_getSystemTextEncoding()).GetBuffer(),S_IRUSR|S_IWUSR|S_IXUSR);
1328 #endif
1329 
1330 #if !defined(WNT)
1331 	SvStream* pFileStream = aCmdFile.GetStream(STREAM_WRITE);
1332 	(*pFileStream)	<< "#!/bin/sh"
1333 					<< sNewLine
1334 					<< "cd \"$DBWORK\""
1335 					<< sNewLine
1336 					<< sNewLine;
1337 	pFileStream->Flush();
1338 #endif
1339 
1340 	return aCmdFile.GetURL();
1341 }
1342 // -----------------------------------------------------------------------------
1343 ::rtl::OUString ODriver::getDatabaseInitFile(  const TDatabaseStruct& _aDBInfo)
1344 {
1345 	String sExt;
1346 	sExt.AssignAscii(".ins");
1347 
1348 
1349 	String sWorkUrl(m_sDbWorkURL);
1350 	::utl::TempFile aInitFile(String::CreateFromAscii("Init"),&sExt,&sWorkUrl);
1351 	{
1352 		SvStream* pFileStream = aInitFile.GetStream(STREAM_WRITE);
1353 		(*pFileStream) << "* @(#)init.cmd  6.1.1   1994-11-10\n";
1354 		(*pFileStream) << "init config\n";
1355 		(*pFileStream) << "* default code:\n";
1356 		(*pFileStream) << "ascii\n";
1357 		(*pFileStream) << "* date time format\n";
1358 		(*pFileStream) << "internal\n";
1359 		(*pFileStream) << "* command timeout:\n";
1360 		(*pFileStream) << "900\n";
1361 		(*pFileStream) << "* lock timeout:\n";
1362 		(*pFileStream) << "360\n";
1363 		(*pFileStream) << "* request timeout:\n";
1364 		(*pFileStream) << "180\n";
1365 		(*pFileStream) << "* log mode:\n";
1366 		(*pFileStream) << "demo\n";
1367 		(*pFileStream) << "* log segment size:\n";
1368 		(*pFileStream) << "0\n";
1369 		(*pFileStream) << "* no of archive logs:\n";
1370 		(*pFileStream) << "0\n";
1371 		(*pFileStream) << "* no of data devspaces:\n";
1372 		(*pFileStream) << "1\n";
1373 		(*pFileStream) << "* mirror devspaces:\n";
1374 		(*pFileStream) << "n\n";
1375 		(*pFileStream) << "if $rc <> 0 then stop\n";
1376 		(*pFileStream) << "*---  device description ---\n";
1377 		(*pFileStream) << "* sys devspace name:\n";
1378 		{
1379 			String sTemp;
1380 			LocalFileHelper::ConvertURLToPhysicalName(_aDBInfo.sSysDevSpace,sTemp);
1381 			(*pFileStream) << ::rtl::OString(sTemp.GetBuffer(),sTemp.Len(),gsl_getSystemTextEncoding());
1382 		}
1383 		(*pFileStream) << "\n* log devspace size:\n";
1384 		(*pFileStream) << ::rtl::OString::valueOf(_aDBInfo.nLogSize);
1385 		(*pFileStream) << "\n* log devspace name:\n";
1386 		{
1387 			String sTemp;
1388 			LocalFileHelper::ConvertURLToPhysicalName(_aDBInfo.sTransLogName,sTemp);
1389 			(*pFileStream) << ::rtl::OString(sTemp.GetBuffer(),sTemp.Len(),gsl_getSystemTextEncoding());
1390 		}
1391 		(*pFileStream) << "\n* data devspace size:\n";
1392 		(*pFileStream) << ::rtl::OString::valueOf(_aDBInfo.nDataSize);
1393 		(*pFileStream) << "\n* data devspace name:\n";
1394 		{
1395 			String sTemp;
1396 			LocalFileHelper::ConvertURLToPhysicalName(_aDBInfo.sDataDevName,sTemp);
1397 			(*pFileStream) << ::rtl::OString(sTemp.GetBuffer(),sTemp.Len(),gsl_getSystemTextEncoding());
1398 		}
1399 
1400 		(*pFileStream) << "\n* END INIT CONFIG\n";
1401 		(*pFileStream) << "if $rc <> 0 then stop\n";
1402 		if(_aDBInfo.bRestoreDatabase)
1403 		{
1404 			(*pFileStream) << "RESTORE DATA QUICK FROM '";
1405 			{
1406 				String sTemp;
1407 				LocalFileHelper::ConvertURLToPhysicalName(_aDBInfo.sBackupFile,sTemp);
1408 				(*pFileStream) << ::rtl::OString(sTemp.GetBuffer(),sTemp.Len(),gsl_getSystemTextEncoding());
1409 			}
1410 			(*pFileStream) << "' BLOCKSIZE 8\n";
1411 			(*pFileStream) << "if $rc <> 0 then stop\n";
1412 			(*pFileStream) << "RESTART\n";
1413 
1414 		}
1415 		else
1416 		{
1417 			(*pFileStream) << "ACTIVATE SERVERDB SYSDBA \"";
1418 			(*pFileStream) << ::rtl::OString(_aDBInfo.sSysUser,_aDBInfo.sSysUser.getLength(),gsl_getSystemTextEncoding());
1419 			(*pFileStream) << "\" PASSWORD \"";
1420 			(*pFileStream) << ::rtl::OString(_aDBInfo.sSysPassword,_aDBInfo.sSysPassword.getLength(),gsl_getSystemTextEncoding());
1421 			(*pFileStream) << "\"\n";
1422 		}
1423 		(*pFileStream) << "if $rc <> 0 then stop\n";
1424 		(*pFileStream) << "exit\n";
1425 	}
1426 	return aInitFile.GetURL();
1427 }
1428 // -----------------------------------------------------------------------------
1429 void ODriver::X_CONS(const ::rtl::OUString& sDBName,const ::rtl::OString& _ACTION,const ::rtl::OUString& _FILENAME)
1430 {
1431 	String sPhysicalPath;
1432 	LocalFileHelper::ConvertURLToPhysicalName(_FILENAME,sPhysicalPath);
1433 
1434 	String sCommandFile = generateInitFile();
1435 	{
1436 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1437 		pFileStream->Seek(STREAM_SEEK_TO_END);
1438 
1439 		(*pFileStream)	<< "x_cons"
1440 #if defined(WNT)
1441 						<< ".exe"
1442 #endif
1443 						<< " "
1444 						<< ::rtl::OString(sDBName,sDBName.getLength(),gsl_getSystemTextEncoding())
1445 						<< " SHOW "
1446 						<< _ACTION
1447 						<< " > "
1448 						<< ::rtl::OString(sPhysicalPath.GetBuffer(),sPhysicalPath.Len(),gsl_getSystemTextEncoding())
1449 						<< sNewLine;
1450 		pFileStream->Flush();
1451 	}
1452 
1453 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1454 	aApp.execute( (OProcess::TProcessOption)(OProcess::TOption_Hidden | OProcess::TOption_Wait));
1455 #if OSL_DEBUG_LEVEL < 2
1456 	if(UCBContentHelper::Exists(sCommandFile))
1457 		UCBContentHelper::Kill(sCommandFile);
1458 #endif
1459 }
1460 // -----------------------------------------------------------------------------
1461 void ODriver::checkAndRestart(const ::rtl::OUString& sDBName,const TDatabaseStruct& _rDbInfo)
1462 {
1463 	String sWorkUrl(m_sDbWorkURL);
1464 	String sExt = String::CreateFromAscii(".st");
1465 	::utl::TempFile aCmdFile(String::CreateFromAscii("State"),&sExt,&sWorkUrl);
1466 	aCmdFile.EnableKillingFile();
1467 
1468 	X_CONS(sDBName,"STATE",aCmdFile.GetURL());
1469 	SvStream* pFileStream = aCmdFile.GetStream(STREAM_SHARE_DENYALL);
1470 	if ( pFileStream )
1471 	{
1472 		ByteString sStateLine;
1473 		sal_Bool bRead = sal_True;
1474 		sal_Int32 nStart = 2;
1475 		while(bRead && !pFileStream->IsEof())
1476 		{
1477 			String aLine;
1478 			bRead = pFileStream->ReadLine(sStateLine);
1479 			if(bRead)
1480 			{
1481 				if(sStateLine.Search("WARM") != STRING_NOTFOUND)
1482 				{	// nothing to do
1483 					nStart = 0;
1484 					break;
1485 				}
1486 				else if(sStateLine.Search("COLD") != STRING_NOTFOUND)
1487 				{
1488 					nStart = 1;
1489 					break;
1490 				}
1491 			}
1492 		}
1493 		switch(nStart)
1494 		{
1495 			case 2:
1496                 clearDatabase(sDBName);
1497 				X_START(sDBName);
1498 				// don't break here
1499 			case 1:
1500 				XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("RESTART")),sDBName,_rDbInfo.sControlUser,_rDbInfo.sControlPassword);
1501 			case 0:
1502 				break;
1503 		}
1504 	}
1505 }
1506 // -----------------------------------------------------------------------------
1507 sal_Bool ODriver::isVersion(const ::rtl::OUString& sDBName, const char* _pVersion)
1508 {
1509 	String sWorkUrl(m_sDbWorkURL);
1510 	String sExt = String::CreateFromAscii(".st");
1511 	::utl::TempFile aCmdFile(String::CreateFromAscii("DevSpaces"),&sExt,&sWorkUrl);
1512 	aCmdFile.EnableKillingFile();
1513 
1514 	String sPhysicalPath;
1515 	LocalFileHelper::ConvertURLToPhysicalName(aCmdFile.GetURL(),sPhysicalPath);
1516 
1517 	String sCommandFile = generateInitFile();
1518 	{
1519 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1520 		pFileStream->Seek(STREAM_SEEK_TO_END);
1521 
1522 		(*pFileStream)	<< "getparam"
1523 #if defined(WNT)
1524 						<< ".exe"
1525 #endif
1526 						<< " "
1527 						<< ::rtl::OString(sDBName,sDBName.getLength(),gsl_getSystemTextEncoding())
1528 						<< " KERNELVERSION > "
1529 						<< ::rtl::OString(sPhysicalPath.GetBuffer(),sPhysicalPath.Len(),gsl_getSystemTextEncoding())
1530 						<< sNewLine;
1531 	}
1532 
1533 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1534 	aApp.execute( (OProcess::TProcessOption)OPROCESS_ADABAS);
1535 #if OSL_DEBUG_LEVEL < 2
1536 	if(UCBContentHelper::Exists(sCommandFile))
1537 		UCBContentHelper::Kill(sCommandFile);
1538 #endif
1539 	SvStream* pFileStream = aCmdFile.GetStream(STREAM_STD_READWRITE);
1540 	ByteString sStateLine;
1541 	sal_Bool bRead = sal_True;
1542 	sal_Bool bIsVersion = sal_False;
1543 	while ( pFileStream && bRead && !pFileStream->IsEof() )
1544 	{
1545 		bRead = pFileStream->ReadLine(sStateLine);
1546 		if ( bRead )
1547 		{
1548 			bIsVersion = sStateLine.GetToken(1,' ').Equals(_pVersion) != 0;
1549 			break;
1550 		}
1551 	}
1552 	return bIsVersion;
1553 }
1554 // -----------------------------------------------------------------------------
1555 void ODriver::checkAndInsertNewDevSpace(const ::rtl::OUString& sDBName,
1556 										const TDatabaseStruct& _rDBInfo)
1557 {
1558 	//	%DBROOT%\pgm\getparam %2 DATA_CACHE_PAGES > %3
1559 	String sWorkUrl(m_sDbWorkURL);
1560 	String sExt = String::CreateFromAscii(".st");
1561 	::utl::TempFile aCmdFile(String::CreateFromAscii("DevSpaces"),&sExt,&sWorkUrl);
1562 	aCmdFile.EnableKillingFile();
1563 
1564 	String sPhysicalPath;
1565 	LocalFileHelper::ConvertURLToPhysicalName(aCmdFile.GetURL(),sPhysicalPath);
1566 
1567 	String sCommandFile = generateInitFile();
1568 	{
1569 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1570 		pFileStream->Seek(STREAM_SEEK_TO_END);
1571 
1572 		(*pFileStream)	<< "getparam"
1573 #if defined(WNT)
1574 						<< ".exe"
1575 #endif
1576 						<< " "
1577 						<< ::rtl::OString(sDBName,sDBName.getLength(),gsl_getSystemTextEncoding())
1578 						<< " DATA_CACHE_PAGES > "
1579 						<< ::rtl::OString(sPhysicalPath.GetBuffer(),sPhysicalPath.Len(),gsl_getSystemTextEncoding())
1580 						<< sNewLine;
1581 	}
1582 
1583 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1584 	aApp.execute( (OProcess::TProcessOption)OPROCESS_ADABAS);
1585 #if OSL_DEBUG_LEVEL < 2
1586 	if(UCBContentHelper::Exists(sCommandFile))
1587 		UCBContentHelper::Kill(sCommandFile);
1588 #endif
1589 	SvStream* pFileStream = aCmdFile.GetStream(STREAM_STD_READWRITE);
1590 	ByteString sStateLine;
1591 	sal_Bool bRead = sal_True;
1592 	sal_Int32 nDataPages = 0;
1593 	while(pFileStream && bRead && !pFileStream->IsEof())
1594 	{
1595 		bRead = pFileStream->ReadLine(sStateLine);
1596 		if(bRead)
1597 		{
1598 			nDataPages = sStateLine.ToInt32();
1599 			if(nDataPages && nDataPages < 100)
1600 			{
1601 				// the space isn't big enough anymore so we increment it
1602 				PutParam(sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DATA_CACHE_PAGES")),::rtl::OUString::valueOf(nDataPages));
1603 				X_PARAM(sDBName,_rDBInfo.sControlUser,_rDBInfo.sControlPassword,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BCHECK")));
1604 			}
1605 		}
1606 	}
1607 }
1608 // -----------------------------------------------------------------------------
1609 sal_Bool ODriver::isKernelVersion(const char* _pVersion)
1610 {
1611 	::utl::TempFile aCmdFile(String::CreateFromAscii("KernelVersion"));
1612 	aCmdFile.EnableKillingFile();
1613 
1614 	String sPhysicalPath;
1615 	LocalFileHelper::ConvertURLToPhysicalName(aCmdFile.GetURL(),sPhysicalPath);
1616 
1617 	String sCommandFile = generateInitFile();
1618 	{
1619 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1620 		pFileStream->Seek(STREAM_SEEK_TO_END);
1621 
1622 		(*pFileStream)	<< "dbversion"
1623 						<< " > "
1624 						<< ::rtl::OString(sPhysicalPath.GetBuffer(),sPhysicalPath.Len(),gsl_getSystemTextEncoding())
1625 						<< sNewLine;
1626 	}
1627 
1628 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1629 	aApp.execute( (OProcess::TProcessOption)OPROCESS_ADABAS);
1630 #if OSL_DEBUG_LEVEL < 2
1631 	if(UCBContentHelper::Exists(sCommandFile))
1632 		UCBContentHelper::Kill(sCommandFile);
1633 #endif
1634 	SvStream* pFileStream = aCmdFile.GetStream(STREAM_STD_READWRITE);
1635 	ByteString sStateLine;
1636 	sal_Bool bRead = sal_True;
1637 	sal_Bool bIsVersion = sal_True;
1638 	while ( pFileStream && bRead && !pFileStream->IsEof() )
1639 	{
1640 		bRead = pFileStream->ReadLine(sStateLine);
1641 		if ( bRead )
1642 		{
1643 			// convert a 11.02.00 to a 12.01.30 version
1644 			bIsVersion = sStateLine.GetToken(0).Equals(_pVersion) != 0;
1645 			break;
1646 		}
1647 	}
1648 	return bIsVersion;
1649 }
1650 // -----------------------------------------------------------------------------
1651 void ODriver::installSystemTables(	const TDatabaseStruct& _aInfo)
1652 {
1653 #if defined(WNT)
1654 	//	xutil -d %_DBNAME% -u %_CONTROL_USER%,%_CONTROL_PWD% -b %m_sDbRoot%\env\TERMCHAR.ind
1655 	::rtl::OUString aBatch =  ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-b "));
1656 	::rtl::OUString sTemp2 = m_sDbRootURL	+ m_sDelimit
1657 											+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env"))
1658 											+ m_sDelimit
1659 											+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TERMCHAR.ind"));
1660 	String sTemp;
1661 	sal_Bool bOk = LocalFileHelper::ConvertURLToPhysicalName(sTemp2,sTemp);
1662 	aBatch += sTemp;
1663 
1664 	XUTIL(aBatch,_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
1665 
1666 	//	xutil -d %_DBNAME% -u %_CONTROL_USER%,%_CONTROL_PWD% DIAGNOSE TRIGGER OFF
1667 	XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DIAGNOSE TRIGGER OFF")),_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
1668 	//	xload -d %_DBNAME% -u %_SYSDBA_USER%,%_SYSDBA_PWD% -S NATIVE -b %m_sDbRoot%\env\DBS.ins %_DOMAINPWD%
1669 	{
1670 		sTemp2 = m_sDbRootURL
1671 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env"))
1672 								+ m_sDelimit
1673 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DBS.ins"));
1674 		sTemp.Erase();
1675 		bOk = LocalFileHelper::ConvertURLToPhysicalName(sTemp2,sTemp);
1676 		OSL_ENSURE(bOk,"File could be converted into file system path!");
1677 		sTemp.AppendAscii(" ");
1678 		sTemp += String(_aInfo.sDomainPassword);
1679 
1680 		LoadBatch(_aInfo.sDBName,_aInfo.sSysUser,_aInfo.sSysPassword,sTemp);
1681 	}
1682 	//	xload -d %_DBNAME% -u DOMAIN,%_DOMAINPWD% -S NATIVE -b %m_sDbRoot%\env\XDD.ins
1683 	{
1684 		sTemp2 = m_sDbRootURL
1685 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env"))
1686 								+ m_sDelimit
1687 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("XDD.ins"));
1688 		sTemp.Erase();
1689 		bOk = LocalFileHelper::ConvertURLToPhysicalName(sTemp2,sTemp);
1690 		OSL_ENSURE(bOk,"File could be converted into file system path!");
1691 
1692 		LoadBatch(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DOMAIN")),_aInfo.sDomainPassword,sTemp);
1693 	}
1694 	//	xload -d %_DBNAME% -u %_SYSDBA_USER%,%_SYSDBA_PWD% -S NATIVE -b %m_sDbRoot%\env\QP.ins
1695 	{
1696 		sTemp2 = m_sDbRootURL
1697 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env"))
1698 								+ m_sDelimit
1699 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("QP.ins"));
1700 		sTemp.Erase();
1701 		bOk = LocalFileHelper::ConvertURLToPhysicalName(sTemp2,sTemp);
1702 		OSL_ENSURE(bOk,"File could be converted into file system path!");
1703 		LoadBatch(_aInfo.sDBName,_aInfo.sSysUser,_aInfo.sSysPassword,sTemp);
1704 	}
1705 	//	xload  -d %_DBNAME% -u DOMAIN,%_DOMAINPWD% -S NATIVE -b %m_sDbRoot%\env\SPROC.ins
1706 	{
1707 		sTemp2 = m_sDbRootURL
1708 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env"))
1709 								+ m_sDelimit
1710 								+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SPROC.ins"));
1711 		sTemp.Erase();
1712 		bOk = LocalFileHelper::ConvertURLToPhysicalName(sTemp2,sTemp);
1713 		OSL_ENSURE(bOk,"File could be converted into file system path!");
1714 
1715 		LoadBatch(_aInfo.sDBName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DOMAIN")),_aInfo.sDomainPassword,sTemp);
1716 	}
1717 
1718 	//	xutil -d %_DBNAME% -u %_CONTROL_USER%,%_CONTROL_PWD%  DIAGNOSE TRIGGER ON
1719 	XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DIAGNOSE TRIGGER ON")),_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
1720 	//	xutil -d %_DBNAME% -u %_CONTROL_USER%,%_CONTROL_PWD%  SET NOLOG OFF
1721 	XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SET NOLOG OFF")),_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
1722 	//	xutil -d %_DBNAME% -u %_CONTROL_USER%,%_CONTROL_PWD%  SHUTDOWN QUICK
1723 	XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SHUTDOWN QUICK")),_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
1724 	//	xutil -d %_DBNAME% -u %_CONTROL_USER%,%_CONTROL_PWD%  RESTART
1725 	XUTIL(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("RESTART")),_aInfo.sDBName,_aInfo.sControlUser,_aInfo.sControlPassword);
1726 
1727 #else // UNX
1728 	String sCommandFile = generateInitFile();
1729 	{
1730 		::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READWRITE));
1731 		pFileStream->Seek(STREAM_SEEK_TO_END);
1732 		(*pFileStream)	<< "x_dbinst"
1733 						<< " -d "
1734 						<< ::rtl::OString(_aInfo.sDBName,_aInfo.sDBName.getLength(),gsl_getSystemTextEncoding())
1735 						<< " -u "
1736 						<< ::rtl::OString(_aInfo.sSysUser,_aInfo.sSysUser.getLength(),gsl_getSystemTextEncoding())
1737 						<< ","
1738 						<< ::rtl::OString(_aInfo.sSysPassword,_aInfo.sSysPassword.getLength(),gsl_getSystemTextEncoding())
1739 						<< " -w "
1740 						<< ::rtl::OString(_aInfo.sDomainPassword,_aInfo.sDomainPassword.getLength(),gsl_getSystemTextEncoding())
1741 						<< " -b ";
1742 
1743 		if ( isKernelVersion(ADABAS_KERNEL_11) )
1744 			(*pFileStream) << "-i all";
1745 		(*pFileStream)
1746 #if (OSL_DEBUG_LEVEL > 1) || defined(DBG_UTIL)
1747 					<< " >> /tmp/kstart.log"
1748 #else
1749 					<< " > /dev/null"
1750 #endif
1751 						<< sNewLine
1752 						<< sNewLine;
1753 		pFileStream->Flush();
1754 	}
1755 	// now execute the command
1756 	OProcess aApp(sCommandFile ,m_sDbWorkURL);
1757 	aApp.execute( (OProcess::TProcessOption)(OProcess::TOption_Hidden | OProcess::TOption_Wait));
1758 #if OSL_DEBUG_LEVEL < 2
1759 	if(UCBContentHelper::Exists(sCommandFile))
1760 		UCBContentHelper::Kill(sCommandFile);
1761 #endif
1762 
1763 #endif //WNT,UNX
1764 }
1765 // -----------------------------------------------------------------------------
1766 void ODriver::convertOldVersion(const ::rtl::OUString& sDBName,const TDatabaseStruct& _rDbInfo)
1767 {
1768 	// first we have to check if this databse is a old version and we have to update the system tables
1769 	if ( !isVersion(sDBName,CURRENT_DB_VERSION) && isKernelVersion(CURRENT_DB_VERSION) )
1770 	{
1771 		if (	!_rDbInfo.sControlUser.getLength()
1772 			||	!_rDbInfo.sControlPassword.getLength())
1773 		{
1774             ::connectivity::SharedResources aResources;
1775             const ::rtl::OUString sError( aResources.getResourceString(STR_DATABASE_NEEDS_CONVERTING) );
1776             ::dbtools::throwGenericSQLException(sError,*this);
1777 		}
1778 		String sCommandFile = m_sDbWorkURL;
1779 		sCommandFile += String::CreateFromAscii("/xparam.prt");
1780 		if ( UCBContentHelper::Exists(sCommandFile) )
1781 			UCBContentHelper::Kill(sCommandFile);
1782 		X_PARAM(sDBName,_rDbInfo.sControlUser,_rDbInfo.sControlPassword,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BCHECK")));
1783 
1784 		if ( UCBContentHelper::Exists(sCommandFile) )
1785 		{
1786 			{
1787 				::std::auto_ptr<SvStream> pFileStream( UcbStreamHelper::CreateStream(sCommandFile,STREAM_STD_READ) );
1788 				ByteString sStateLine;
1789 				sal_Bool bRead = sal_True;
1790 				static ByteString s_ErrorId("-21100");
1791 				while ( pFileStream.get() && bRead && !pFileStream->IsEof() )
1792 				{
1793 					bRead = pFileStream->ReadLine(sStateLine);
1794 					if ( bRead && s_ErrorId == sStateLine.GetToken(0,' ') )
1795 					{
1796 						UCBContentHelper::Kill(sCommandFile);
1797 						::rtl::OUString sError(::rtl::OUString::createFromAscii(sStateLine.GetBuffer()));
1798 						throw SQLException(sError,*this,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")),1000,Any());
1799 					}
1800 				}
1801 			}
1802 
1803 			UCBContentHelper::Kill(sCommandFile);
1804 		}
1805 	}
1806 }
1807 // -----------------------------------------------------------------------------
1808 // -----------------------------------------------------------------------------
1809 	} // namespace adabas
1810 }// namespace connectivity
1811 // -----------------------------------------------------------------------------
1812 
1813 
1814 
1815 
1816