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
;
76 using namespace com::sun::star::registry
;
78 using ::rtl::OUString
;
79 using ::rtl::OUStringToOString
;
81 extern Reference
< XMultiServiceFactory
> InitializeFac( void );
82 Reference
< XMultiServiceFactory
> mMgr
;
84 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
85 #define PRINTSTR(x) printf("%s",x);
86 #define PRINTLN(x) printf("%s\n",x);
89 const int testMozilla
=1;
93 static int testCount
= 0;
94 static int testList
[4] = {0,0,0,0};
96 int autoTest(Reference
<XResultSet
> &xRes
);
98 void printColumns( Reference
<XResultSet
> &xRes
)
102 char* aPat
= " %-22s ";
103 char* aPat_Short
= " %-12s ";
104 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
105 OSL_TRACE( "ColumnCount = %d", xMeta
->getColumnCount());
106 for(sal_Int32 i
=1;i
<=xMeta
->getColumnCount();++i
)
108 const char *str
= OUtoCStr(xMeta
->getColumnName(i
));
109 OSL_TRACE( aPat
, str
);
112 OSL_TRACE("------------------------------------------------------------------------------------------");
116 OSL_TRACE(": FAILED to get a ResultSet");
119 void printXResultSet( Reference
<XResultSet
> &xRes
)
123 char* aPat
= " %-22s ";
124 char* aPat_Short
= " %-12s ";
125 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
126 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
127 for(sal_Int32 j
=1;j
<=xMeta
->getColumnCount();++j
)
131 const char *str
= OUtoCStr(xRow
->getString(j
));
132 OSL_TRACE( aPat_Short
, str
);
140 OSL_TRACE("FAILED to get a ResultSet");
143 void printXResultSets( Reference
<XResultSet
> &xRes
)
149 while( xRes
.is() && xRes
->next())
151 printXResultSet(xRes
);
154 OSL_TRACE( "%d Row(s)", nRows
);
156 OSL_TRACE("FAILED to get a ResultSet");
161 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
163 // Test some metadata
164 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
167 OSL_TRACE(": got DatabaseMetaData");
169 OUString sQuoteStr
= xDmd
->getIdentifierQuoteString();
170 OSL_TRACE( "Quote String : '%s'", OUtoCStr( sQuoteStr
) );
172 OUString sSQLCmds
= xDmd
->getSQLKeywords();
173 OSL_TRACE( "SQL Commands : '%s'", OUtoCStr( sSQLCmds
) );
175 OSL_TRACE("Testing getColumns() : START");
177 Reference
<XResultSet
> xRes
= xDmd
->getColumns(
178 makeAny(OUString("")), // Catalog
179 OUString("%"), // Schema
180 OUString("%"), // TabName
183 printXResultSets( xRes
);
185 OSL_TRACE("Testing getColumns() : END");
187 OSL_TRACE("Testing getTypeInfo() : START");
189 Reference
<XResultSet
> xRes
= xDmd
-> getTypeInfo();
190 printXResultSets( xRes
);
192 OSL_TRACE("Testing getTypeInfo() : END");
194 OSL_TRACE("Testing getTables() : START");
196 Reference
<XResultSet
> xRes
= xDmd
->getTables(
197 makeAny(OUString("")), // Catalog
198 OUString("%"), // Schema
199 OUString("%"), // TabName
200 Sequence
<rtl::OUString
>() );
201 printXResultSets( xRes
);
203 OSL_TRACE("Testing getTables() : END");
207 OSL_TRACE(": FAILED to get DatabaseMetaData");
211 void TestQuery(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
214 OSL_TRACE("Testing createStatement() & executeQuery() : START");
215 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
216 Reference
<XResultSet
> xRes
;
219 OSL_TRACE(": got statement");
220 OSL_TRACE(": excuteQuery() : START \n");
221 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
222 OUString
sqlPrefix("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
226 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
229 OSL_TRACE("getTables() : START");
230 OUString qut
= xDmd
->getIdentifierQuoteString();
232 Reference
<XResultSet
> xRes
= xDmd
->getTables(
233 makeAny(OUString("")), // Catalog
234 OUString("%"), // Schema
235 OUString("%"), // TabName
236 Sequence
<rtl::OUString
>() );
237 sal_Int32 nTables
= 0;
238 while( xRes
.is() && xRes
->next())
240 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
241 const char *strTableName
= OUtoCStr(xRow
->getString(3));
242 OSL_TRACE("Testing Table:%s",strTableName
);
244 Reference
<XResultSet
> tmpRes
=
245 xStmt
->executeQuery(sqlPrefix
+ qut
+ xRow
->getString(3) + qut
);
247 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
251 OSL_TRACE("Tested Tables:%d",nTables
);
253 } catch ( Exception
&e
) {
254 OSL_TRACE( "Exception caught : %s", OUtoCStr( e
.Message
) );
257 // OSL_TRACE( "Non-UNO Exception caught\n" );
259 OSL_TRACE("excuteQuery() : END");
263 OSL_TRACE(": FAILED to get statement");
265 OSL_TRACE("Testing createStatement() & executeQuery() : END");
267 Reference
< ::com::sun::star::sdbc::XConnection
> TestConnected
268 (Reference
< ::com::sun::star::sdbc::XDriver
> &pDriver
)
270 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> pConnection
;
271 OSL_TRACE("Begin Connect!");
273 Sequence
<PropertyValue
> aValue
;
275 srand( (unsigned)time( NULL
) );
276 nType
= rand() % testCount
+ 1;
277 int nIndex
=0,nCount
=0;
278 for ( nIndex
= 0; nIndex
< 4;nIndex
++)
280 if (testList
[nIndex
])
288 url
=OUString("sdbc:address:ldap://");
290 aValue
[0].Name
= ::rtl::OUString("HostName");
291 aValue
[0].Value
<<= rtl::OUString("sun-ds");
292 aValue
[1].Name
= ::rtl::OUString("BaseDN");
293 aValue
[1].Value
<<= rtl::OUString("dc=sun,dc=com");
296 url
=OUString("sdbc:address:mozilla://");
299 url
=OUString("sdbc:address:outlook://");
302 url
=OUString("sdbc:address:outlookexp://");
305 url
=OUString("sdbc:address:mozilla://");
309 pDriver
->connect(url
,aValue
);
313 int autoTest(Reference
<XResultSet
> &xRes
)
319 while( xRes
.is() && xRes
->next())
323 OSL_TRACE( "%d Row(s)", nRows
);
328 for(times
= 1;times
< 10; times
++)
330 pos
= rand() % nRows
+1;
331 OSL_TRACE("pos:%d",pos
);
333 printXResultSet(xRes
);
339 OSL_TRACE(": FAILED to get a ResultSet");
341 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
342 osl_waitThread(&timeValue
);
345 void SAL_CALL
mozThread(void*)
347 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
350 Reference
< ::com::sun::star::sdbc::XDriver
>
351 m_xDriver(mMgr
->createInstance(
352 OUString("com.sun.star.comp.sdbc.MozabDriver")),
357 m_xConnection
= TestConnected(m_xDriver
);
358 if(m_xConnection
.is())
360 if (!TestMetaData(m_xConnection
))
362 TestQuery(m_xConnection
);
366 OSL_TRACE("Can't connected!");
371 OSL_TRACE("No driver!");
373 } catch ( Exception
&e
) {
374 OSL_TRACE( "Exception caught : %s", OUtoCStr( e
.Message
) );
377 // OSL_TRACE( "Non-UNO Exception caught\n" );
380 const int THREAD_COUNT
=100;
385 PRINTLN("mozThread [opts] threadcount");
387 PRINTLN("-l test ldap");
388 PRINTLN("-m test mozilla");
389 PRINTLN("-p test outlook");
390 PRINTLN("-e test outlook express");
391 PRINTLN("0 < threadcount <= 100, default 100");
394 int main( int argc
, char * argv
[] )
396 int _cdecl
main( int argc
, char * argv
[] )
400 OSL_TRACE("Init UNO");
401 Reference
< XMultiServiceFactory
> xMgr
=InitializeFac();
402 int threadCount
=THREAD_COUNT
;
404 for (nAc
= 1; nAc
< argc
; nAc
++)
406 if (strcmp(argv
[nAc
],"-l") ==0)
408 testList
[testLDAP
] = 1;
409 }else if(strcmp(argv
[nAc
],"-m") ==0)
411 testList
[testMozilla
]=1;
412 }else if(strcmp(argv
[nAc
],"-p") ==0)
415 }else if(strcmp(argv
[nAc
],"-e") ==0)
418 }else if(strcmp(argv
[nAc
],"-h") ==0 || strcmp(argv
[nAc
],"--help") ==0)
424 int tmpCount
= atoi(argv
[nAc
]);
425 if (tmpCount
> 0 && tmpCount
< threadCount
)
427 threadCount
= tmpCount
;
431 testCount
= testList
[testLDAP
] + testList
[testMozilla
] + testList
[testOp
] + testList
[testOe
];
434 testList
[testLDAP
] = 1;
440 OSL_TRACE("Error init UNO");
444 OSL_TRACE("UNO initted");
447 oslThread xThreads
[THREAD_COUNT
];
449 for(index
=0;index
< threadCount
; index
++)
451 xThreads
[index
] = osl_createThread(mozThread
,(void*)NULL
);
452 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
453 osl_waitThread(&timeValue
);
455 for(index
=0;index
< threadCount
; index
++)
457 if (osl_isThreadRunning(xThreads
[index
]))
458 osl_joinWithThread(xThreads
[index
]);
460 OSL_TRACE("Exiting...");
464 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */