1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mozthread.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include <connectivity/sqlparse.hxx>
34 #include "connectivity/sqliterator.hxx"
35 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
36 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/beans/PropertyState.hpp>
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #include <unotools/processfactory.hxx>
45 #include <cppuhelper/servicefactory.hxx>
46 #include <com/sun/star/sdbc/XConnection.hpp>
47 #include <com/sun/star/sdbc/XDriver.hpp>
48 #include <com/sun/star/sdbc/XDriverAccess.hpp>
49 #include <com/sun/star/sdbcx/XRowLocate.hpp>
50 #include <com/sun/star/sdbc/XRowUpdate.hpp>
51 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
52 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
53 #include <com/sun/star/sdbc/XCloseable.hpp>
54 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
55 #include <com/sun/star/uno/Any.hxx>
57 #include "connectivity/sqlnode.hxx"
58 #include <ucbhelper/contentbroker.hxx>
59 #include <comphelper/regpathhelper.hxx>
60 #include <rtl/ustring.hxx>
61 #include <rtl/ustrbuf.hxx>
62 #include <osl/process.h>
64 #include <cppuhelper/bootstrap.hxx>
65 #include <cppuhelper/servicefactory.hxx>
66 #include <com/sun/star/lang/XComponent.hpp>
67 #include <com/sun/star/registry/XImplementationRegistration.hpp>
68 #include <com/sun/star/ucb/XContentProviderManager.hpp>
70 #include <ucbhelper/content.hxx>
71 #include <osl/module.h>
72 #include <tools/config.hxx>
75 #include <osl/thread.hxx>
76 #include <osl/diagnose.h>
77 #include <osl/conditn.hxx>
81 using namespace comphelper
;
84 using namespace com::sun::star::uno
;
85 using namespace com::sun::star::lang
;
86 using namespace com::sun::star::registry
;
87 using namespace com::sun::star::ucb
;
88 using namespace com::sun::star::beans
;
90 //using namespace com::sun::star;
91 using namespace connectivity
;
92 using namespace com::sun::star::sdb
;
93 using namespace com::sun::star::sdbc
;
94 using namespace com::sun::star::sdbcx
;
95 using namespace ::com::sun::star::container
;
96 using namespace com::sun::star::registry
;
98 extern Reference
< XMultiServiceFactory
> InitializeFac( void );
99 Reference
< XMultiServiceFactory
> mMgr
;
101 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
102 #define PRINTSTR(x) printf("%s",x);
103 #define PRINTLN(x) printf("%s\n",x);
105 const int testLDAP
=0;
106 const int testMozilla
=1;
110 static int testCount
= 0;
111 static int testList
[4] = {0,0,0,0};
113 int autoTest(Reference
<XResultSet
> &xRes
);
115 void printColumns( Reference
<XResultSet
> &xRes
)
119 char* aPat
= " %-22s ";
120 char* aPat_Short
= " %-12s ";
121 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
122 OSL_TRACE( "ColumnCount = %d\n", xMeta
->getColumnCount());
123 for(sal_Int32 i
=1;i
<=xMeta
->getColumnCount();++i
)
125 const char *str
= OUtoCStr(xMeta
->getColumnName(i
));
126 OSL_TRACE( aPat
, str
);
129 OSL_TRACE("------------------------------------------------------------------------------------------\n");
133 OSL_TRACE(": FAILED to get a ResultSet \n");
136 void printXResultSet( Reference
<XResultSet
> &xRes
)
140 char* aPat
= " %-22s ";
141 char* aPat_Short
= " %-12s ";
142 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
143 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
144 for(sal_Int32 j
=1;j
<=xMeta
->getColumnCount();++j
)
148 const char *str
= OUtoCStr(xRow
->getString(j
));
149 OSL_TRACE( aPat_Short
, str
);
157 OSL_TRACE("FAILED to get a ResultSet \n");
160 void printXResultSets( Reference
<XResultSet
> &xRes
)
166 while( xRes
.is() && xRes
->next())
168 printXResultSet(xRes
);
171 OSL_TRACE( "%d Row(s)\n", nRows
);
173 OSL_TRACE("FAILED to get a ResultSet \n");
178 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
180 // Test some metadata
181 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
184 OSL_TRACE(": got DatabaseMetaData \n");
186 OUString sQuoteStr
= xDmd
->getIdentifierQuoteString();
187 OSL_TRACE( "Quote String : '%s'\n", OUtoCStr( sQuoteStr
) );
189 OUString sSQLCmds
= xDmd
->getSQLKeywords();
190 OSL_TRACE( "SQL Commands : '%s'\n", OUtoCStr( sSQLCmds
) );
192 OSL_TRACE("Testing getColumns() : START\n");
194 Reference
<XResultSet
> xRes
= xDmd
->getColumns(
195 makeAny(OUString::createFromAscii("")), // Catalog
196 OUString::createFromAscii("%"), // Schema
197 OUString::createFromAscii("%"), // TabName
198 OUString::createFromAscii("%")
200 printXResultSets( xRes
);
202 OSL_TRACE("Testing getColumns() : END\n");
204 OSL_TRACE("Testing getTypeInfo() : START\n");
206 Reference
<XResultSet
> xRes
= xDmd
-> getTypeInfo();
207 printXResultSets( xRes
);
209 OSL_TRACE("Testing getTypeInfo() : END\n");
211 OSL_TRACE("Testing getTables() : START\n");
213 Reference
<XResultSet
> xRes
= xDmd
->getTables(
214 makeAny(OUString::createFromAscii("")), // Catalog
215 OUString::createFromAscii("%"), // Schema
216 OUString::createFromAscii("%"), // TabName
217 Sequence
<rtl::OUString
>() );
218 printXResultSets( xRes
);
220 OSL_TRACE("Testing getTables() : END\n");
224 OSL_TRACE(": FAILED to get DatabaseMetaData \n");
228 void TestQuery(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
231 OSL_TRACE("Testing createStatement() & executeQuery() : START\n");
232 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
233 Reference
<XResultSet
> xRes
;
236 OSL_TRACE(": got statement\n");
237 OSL_TRACE(": excuteQuery() : START \n");
238 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
239 OUString sqlPrefix
= OUString::createFromAscii("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
243 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
246 OSL_TRACE("getTables() : START\n");
247 OUString qut
= xDmd
->getIdentifierQuoteString();
249 Reference
<XResultSet
> xRes
= xDmd
->getTables(
250 makeAny(OUString::createFromAscii("")), // Catalog
251 OUString::createFromAscii("%"), // Schema
252 OUString::createFromAscii("%"), // TabName
253 Sequence
<rtl::OUString
>() );
254 sal_Int32 nTables
= 0;
255 while( xRes
.is() && xRes
->next())
257 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
258 const char *strTableName
= OUtoCStr(xRow
->getString(3));
259 OSL_TRACE("Testing Table:%s\n",strTableName
);
261 Reference
<XResultSet
> tmpRes
=
262 xStmt
->executeQuery(sqlPrefix
+ qut
+ xRow
->getString(3) + qut
);
264 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
268 OSL_TRACE("Tested Tables:%d\n",nTables
);
270 } catch ( Exception
&e
) {
271 OSL_TRACE( "Exception caught : %s\n", OUtoCStr( e
.Message
) );
274 // OSL_TRACE( "Non-UNO Exception caught\n" );
276 OSL_TRACE("excuteQuery() : END \n");
280 OSL_TRACE(": FAILED to get statement\n");
282 OSL_TRACE("Testing createStatement() & executeQuery() : END\n");
284 Reference
< ::com::sun::star::sdbc::XConnection
> TestConnected
285 (Reference
< ::com::sun::star::sdbc::XDriver
> &pDriver
)
287 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> pConnection
;
288 OSL_TRACE("Begin Connect!\n");
290 Sequence
<PropertyValue
> aValue
;
292 srand( (unsigned)time( NULL
) );
293 nType
= rand() % testCount
+ 1;
294 int nIndex
=0,nCount
=0;
295 for ( nIndex
= 0; nIndex
< 4;nIndex
++)
297 if (testList
[nIndex
])
305 url
=OUString::createFromAscii("sdbc:address:ldap://");
307 aValue
[0].Name
= ::rtl::OUString::createFromAscii("HostName");
308 aValue
[0].Value
<<= rtl::OUString::createFromAscii("sun-ds");
309 aValue
[1].Name
= ::rtl::OUString::createFromAscii("BaseDN");
310 aValue
[1].Value
<<= rtl::OUString::createFromAscii("dc=sun,dc=com");
313 url
=OUString::createFromAscii("sdbc:address:mozilla://");
316 url
=OUString::createFromAscii("sdbc:address:outlook://");
319 url
=OUString::createFromAscii("sdbc:address:outlookexp://");
322 url
=OUString::createFromAscii("sdbc:address:mozilla://");
326 pDriver
->connect(url
,aValue
);
330 int autoTest(Reference
<XResultSet
> &xRes
)
336 while( xRes
.is() && xRes
->next())
340 OSL_TRACE( "%d Row(s)\n", nRows
);
345 for(times
= 1;times
< 10; times
++)
347 pos
= rand() % nRows
+1;
348 OSL_TRACE("pos:%d\n",pos
);
350 printXResultSet(xRes
);
356 OSL_TRACE(": FAILED to get a ResultSet \n");
358 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
359 osl_waitThread(&timeValue
);
362 void SAL_CALL
mozThread(void*)
364 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
367 Reference
< ::com::sun::star::sdbc::XDriver
>
368 m_xDriver(mMgr
->createInstance(
369 OUString::createFromAscii("com.sun.star.comp.sdbc.MozabDriver")),
374 m_xConnection
= TestConnected(m_xDriver
);
375 if(m_xConnection
.is())
377 if (!TestMetaData(m_xConnection
))
379 TestQuery(m_xConnection
);
383 OSL_TRACE("Can't connected!\n");
388 OSL_TRACE("No driver!\n");
390 } catch ( Exception
&e
) {
391 OSL_TRACE( "Exception caught : %s\n", OUtoCStr( e
.Message
) );
394 // OSL_TRACE( "Non-UNO Exception caught\n" );
397 const int THREAD_COUNT
=100;
402 PRINTLN("mozThread [opts] threadcount");
404 PRINTLN("-l test ldap");
405 PRINTLN("-m test mozilla");
406 PRINTLN("-p test outlook");
407 PRINTLN("-e test outlook express");
408 PRINTLN("0 < threadcount <= 100, default 100");
410 #if (defined UNX) || (defined OS2)
411 int main( int argc
, char * argv
[] )
413 int _cdecl
main( int argc
, char * argv
[] )
417 OSL_TRACE("Init UNO\n");
418 Reference
< XMultiServiceFactory
> xMgr
=InitializeFac();
419 int threadCount
=THREAD_COUNT
;
421 for (nAc
= 1; nAc
< argc
; nAc
++)
423 if (strcmp(argv
[nAc
],"-l") ==0)
425 testList
[testLDAP
] = 1;
426 }else if(strcmp(argv
[nAc
],"-m") ==0)
428 testList
[testMozilla
]=1;
429 }else if(strcmp(argv
[nAc
],"-p") ==0)
432 }else if(strcmp(argv
[nAc
],"-e") ==0)
435 }else if(strcmp(argv
[nAc
],"-h") ==0 || strcmp(argv
[nAc
],"--help") ==0)
441 int tmpCount
= atoi(argv
[nAc
]);
442 if (tmpCount
> 0 && tmpCount
< threadCount
)
444 threadCount
= tmpCount
;
448 testCount
= testList
[testLDAP
] + testList
[testMozilla
] + testList
[testOp
] + testList
[testOe
];
451 testList
[testLDAP
] = 1;
457 OSL_TRACE("Error init UNO\n");
461 OSL_TRACE("UNO initted\n");
464 oslThread xThreads
[THREAD_COUNT
];
466 for(index
=0;index
< threadCount
; index
++)
468 xThreads
[index
] = osl_createThread(mozThread
,(void*)NULL
);
469 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
470 osl_waitThread(&timeValue
);
472 for(index
=0;index
< threadCount
; index
++)
474 if (osl_isThreadRunning(xThreads
[index
]))
475 osl_joinWithThread(xThreads
[index
]);
477 OSL_TRACE("Exiting...\n");