1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <connectivity/sqlparse.hxx>
21 #include "connectivity/sqliterator.hxx"
22 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
23 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
24 #include <com/sun/star/sdbc/XResultSet.hpp>
25 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
26 #include <com/sun/star/sdbc/XRow.hpp>
27 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/beans/PropertyState.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <cppuhelper/servicefactory.hxx>
32 #include <com/sun/star/sdbc/XConnection.hpp>
33 #include <com/sun/star/sdbc/XDriver.hpp>
34 #include <com/sun/star/sdbc/XDriverAccess.hpp>
35 #include <com/sun/star/sdbcx/XRowLocate.hpp>
36 #include <com/sun/star/sdbc/XRowUpdate.hpp>
37 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
38 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
39 #include <com/sun/star/sdbc/XCloseable.hpp>
40 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
41 #include <com/sun/star/uno/Any.hxx>
43 #include "connectivity/sqlnode.hxx"
44 #include <rtl/ustring.hxx>
45 #include <rtl/ustrbuf.hxx>
46 #include <osl/process.h>
48 #include <cppuhelper/bootstrap.hxx>
49 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/registry/XImplementationRegistration.hpp>
51 #include <com/sun/star/ucb/XContentProviderManager.hpp>
53 #include <ucbhelper/content.hxx>
54 #include <osl/module.h>
57 #include <osl/thread.hxx>
58 #include <osl/diagnose.h>
59 #include <osl/conditn.hxx>
63 using namespace comphelper
;
65 using namespace com::sun::star::uno
;
66 using namespace com::sun::star::lang
;
67 using namespace com::sun::star::registry
;
68 using namespace com::sun::star::ucb
;
69 using namespace com::sun::star::beans
;
71 using namespace connectivity
;
72 using namespace com::sun::star::sdb
;
73 using namespace com::sun::star::sdbc
;
74 using namespace com::sun::star::sdbcx
;
75 using namespace ::com::sun::star::container
;
78 extern Reference
< XMultiServiceFactory
> InitializeFac( void );
79 Reference
< XMultiServiceFactory
> mMgr
;
81 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
82 #define PRINTSTR(x) printf("%s",x);
83 #define PRINTLN(x) printf("%s\n",x);
86 const int testMozilla
=1;
90 static int testCount
= 0;
91 static int testList
[4] = {0,0,0,0};
93 int autoTest(Reference
<XResultSet
> &xRes
);
95 void printColumns( Reference
<XResultSet
> &xRes
)
99 char* aPat
= " %-22s ";
100 char* aPat_Short
= " %-12s ";
101 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
102 OSL_TRACE( "ColumnCount = %d", xMeta
->getColumnCount());
103 for(sal_Int32 i
=1;i
<=xMeta
->getColumnCount();++i
)
105 const char *str
= OUtoCStr(xMeta
->getColumnName(i
));
106 OSL_TRACE( aPat
, str
);
109 OSL_TRACE("------------------------------------------------------------------------------------------");
113 OSL_TRACE(": FAILED to get a ResultSet");
116 void printXResultSet( Reference
<XResultSet
> &xRes
)
120 char* aPat
= " %-22s ";
121 char* aPat_Short
= " %-12s ";
122 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
123 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
124 for(sal_Int32 j
=1;j
<=xMeta
->getColumnCount();++j
)
128 const char *str
= OUtoCStr(xRow
->getString(j
));
129 OSL_TRACE( aPat_Short
, str
);
137 OSL_TRACE("FAILED to get a ResultSet");
140 void printXResultSets( Reference
<XResultSet
> &xRes
)
146 while( xRes
.is() && xRes
->next())
148 printXResultSet(xRes
);
151 OSL_TRACE( "%d Row(s)", nRows
);
153 OSL_TRACE("FAILED to get a ResultSet");
158 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
160 // Test some metadata
161 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
164 OSL_TRACE(": got DatabaseMetaData");
166 OUString sQuoteStr
= xDmd
->getIdentifierQuoteString();
167 OSL_TRACE( "Quote String : '%s'", OUtoCStr( sQuoteStr
) );
169 OUString sSQLCmds
= xDmd
->getSQLKeywords();
170 OSL_TRACE( "SQL Commands : '%s'", OUtoCStr( sSQLCmds
) );
172 OSL_TRACE("Testing getColumns() : START");
174 Reference
<XResultSet
> xRes
= xDmd
->getColumns(
175 makeAny(OUString("")), // Catalog
176 OUString("%"), // Schema
177 OUString("%"), // TabName
180 printXResultSets( xRes
);
182 OSL_TRACE("Testing getColumns() : END");
184 OSL_TRACE("Testing getTypeInfo() : START");
186 Reference
<XResultSet
> xRes
= xDmd
-> getTypeInfo();
187 printXResultSets( xRes
);
189 OSL_TRACE("Testing getTypeInfo() : END");
191 OSL_TRACE("Testing getTables() : START");
193 Reference
<XResultSet
> xRes
= xDmd
->getTables(
194 makeAny(OUString("")), // Catalog
195 OUString("%"), // Schema
196 OUString("%"), // TabName
197 Sequence
<OUString
>() );
198 printXResultSets( xRes
);
200 OSL_TRACE("Testing getTables() : END");
204 OSL_TRACE(": FAILED to get DatabaseMetaData");
208 void TestQuery(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
211 OSL_TRACE("Testing createStatement() & executeQuery() : START");
212 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
213 Reference
<XResultSet
> xRes
;
216 OSL_TRACE(": got statement");
217 OSL_TRACE(": excuteQuery() : START \n");
218 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
219 OUString
sqlPrefix("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
223 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
226 OSL_TRACE("getTables() : START");
227 OUString qut
= xDmd
->getIdentifierQuoteString();
229 Reference
<XResultSet
> xRes
= xDmd
->getTables(
230 makeAny(OUString("")), // Catalog
231 OUString("%"), // Schema
232 OUString("%"), // TabName
233 Sequence
<OUString
>() );
234 sal_Int32 nTables
= 0;
235 while( xRes
.is() && xRes
->next())
237 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
238 const char *strTableName
= OUtoCStr(xRow
->getString(3));
239 OSL_TRACE("Testing Table:%s",strTableName
);
241 Reference
<XResultSet
> tmpRes
=
242 xStmt
->executeQuery(sqlPrefix
+ qut
+ xRow
->getString(3) + qut
);
244 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
248 OSL_TRACE("Tested Tables:%d",nTables
);
250 } catch ( Exception
&e
) {
251 OSL_TRACE( "Exception caught : %s", OUtoCStr( e
.Message
) );
254 // OSL_TRACE( "Non-UNO Exception caught\n" );
256 OSL_TRACE("excuteQuery() : END");
260 OSL_TRACE(": FAILED to get statement");
262 OSL_TRACE("Testing createStatement() & executeQuery() : END");
264 Reference
< ::com::sun::star::sdbc::XConnection
> TestConnected
265 (Reference
< ::com::sun::star::sdbc::XDriver
> &pDriver
)
267 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> pConnection
;
268 OSL_TRACE("Begin Connect!");
270 Sequence
<PropertyValue
> aValue
;
272 srand( (unsigned)time( NULL
) );
273 nType
= rand() % testCount
+ 1;
274 int nIndex
=0,nCount
=0;
275 for ( nIndex
= 0; nIndex
< 4;nIndex
++)
277 if (testList
[nIndex
])
285 url
=OUString("sdbc:address:ldap://");
287 aValue
[0].Name
= OUString("HostName");
288 aValue
[0].Value
<<= OUString("sun-ds");
289 aValue
[1].Name
= OUString("BaseDN");
290 aValue
[1].Value
<<= OUString("dc=sun,dc=com");
293 url
=OUString("sdbc:address:mozilla://");
296 url
=OUString("sdbc:address:outlook://");
299 url
=OUString("sdbc:address:outlookexp://");
302 url
=OUString("sdbc:address:mozilla://");
306 pDriver
->connect(url
,aValue
);
310 int autoTest(Reference
<XResultSet
> &xRes
)
316 while( xRes
.is() && xRes
->next())
320 OSL_TRACE( "%d Row(s)", nRows
);
325 for(times
= 1;times
< 10; times
++)
327 pos
= rand() % nRows
+1;
328 OSL_TRACE("pos:%d",pos
);
330 printXResultSet(xRes
);
336 OSL_TRACE(": FAILED to get a ResultSet");
338 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
339 osl_waitThread(&timeValue
);
342 void SAL_CALL
mozThread(void*)
344 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
347 Reference
< ::com::sun::star::sdbc::XDriver
>
348 m_xDriver(mMgr
->createInstance(
349 OUString("com.sun.star.comp.sdbc.MozabDriver")),
354 m_xConnection
= TestConnected(m_xDriver
);
355 if(m_xConnection
.is())
357 if (!TestMetaData(m_xConnection
))
359 TestQuery(m_xConnection
);
363 OSL_TRACE("Can't connected!");
368 OSL_TRACE("No driver!");
370 } catch ( Exception
&e
) {
371 OSL_TRACE( "Exception caught : %s", OUtoCStr( e
.Message
) );
374 // OSL_TRACE( "Non-UNO Exception caught\n" );
377 const int THREAD_COUNT
=100;
382 PRINTLN("mozThread [opts] threadcount");
384 PRINTLN("-l test ldap");
385 PRINTLN("-m test mozilla");
386 PRINTLN("-p test outlook");
387 PRINTLN("-e test outlook express");
388 PRINTLN("0 < threadcount <= 100, default 100");
391 int main( int argc
, char * argv
[] )
393 int _cdecl
main( int argc
, char * argv
[] )
397 OSL_TRACE("Init UNO");
398 Reference
< XMultiServiceFactory
> xMgr
=InitializeFac();
399 int threadCount
=THREAD_COUNT
;
401 for (nAc
= 1; nAc
< argc
; nAc
++)
403 if (strcmp(argv
[nAc
],"-l") ==0)
405 testList
[testLDAP
] = 1;
406 }else if(strcmp(argv
[nAc
],"-m") ==0)
408 testList
[testMozilla
]=1;
409 }else if(strcmp(argv
[nAc
],"-p") ==0)
412 }else if(strcmp(argv
[nAc
],"-e") ==0)
415 }else if(strcmp(argv
[nAc
],"-h") ==0 || strcmp(argv
[nAc
],"--help") ==0)
421 int tmpCount
= atoi(argv
[nAc
]);
422 if (tmpCount
> 0 && tmpCount
< threadCount
)
424 threadCount
= tmpCount
;
428 testCount
= testList
[testLDAP
] + testList
[testMozilla
] + testList
[testOp
] + testList
[testOe
];
431 testList
[testLDAP
] = 1;
437 OSL_TRACE("Error init UNO");
441 OSL_TRACE("UNO initted");
444 oslThread xThreads
[THREAD_COUNT
];
446 for(index
=0;index
< threadCount
; index
++)
448 xThreads
[index
] = osl_createThread(mozThread
,(void*)NULL
);
449 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
450 osl_waitThread(&timeValue
);
452 for(index
=0;index
< threadCount
; index
++)
454 if (osl_isThreadRunning(xThreads
[index
]))
455 osl_joinWithThread(xThreads
[index
]);
457 OSL_TRACE("Exiting...");
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */