1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <connectivity/sqlparse.hxx>
30 #include "connectivity/sqliterator.hxx"
31 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
32 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/beans/PropertyState.hpp>
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <com/sun/star/sdbc/XConnection.hpp>
42 #include <com/sun/star/sdbc/XDriver.hpp>
43 #include <com/sun/star/sdbc/XDriverAccess.hpp>
44 #include <com/sun/star/sdbcx/XRowLocate.hpp>
45 #include <com/sun/star/sdbc/XRowUpdate.hpp>
46 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
47 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
48 #include <com/sun/star/sdbc/XCloseable.hpp>
49 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
50 #include <com/sun/star/uno/Any.hxx>
52 #include "connectivity/sqlnode.hxx"
53 #include <ucbhelper/contentbroker.hxx>
54 #include <rtl/ustring.hxx>
55 #include <rtl/ustrbuf.hxx>
56 #include <osl/process.h>
58 #include <cppuhelper/bootstrap.hxx>
59 #include <com/sun/star/lang/XComponent.hpp>
60 #include <com/sun/star/registry/XImplementationRegistration.hpp>
61 #include <com/sun/star/ucb/XContentProviderManager.hpp>
63 #include <ucbhelper/content.hxx>
64 #include <osl/module.h>
67 #include <osl/thread.hxx>
68 #include <osl/diagnose.h>
69 #include <osl/conditn.hxx>
73 using namespace comphelper
;
75 using namespace com::sun::star::uno
;
76 using namespace com::sun::star::lang
;
77 using namespace com::sun::star::registry
;
78 using namespace com::sun::star::ucb
;
79 using namespace com::sun::star::beans
;
81 using namespace connectivity
;
82 using namespace com::sun::star::sdb
;
83 using namespace com::sun::star::sdbc
;
84 using namespace com::sun::star::sdbcx
;
85 using namespace ::com::sun::star::container
;
86 using namespace com::sun::star::registry
;
88 using ::rtl::OUString
;
89 using ::rtl::OUStringToOString
;
91 extern Reference
< XMultiServiceFactory
> InitializeFac( void );
92 Reference
< XMultiServiceFactory
> mMgr
;
94 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
95 #define PRINTSTR(x) printf("%s",x);
96 #define PRINTLN(x) printf("%s\n",x);
99 const int testMozilla
=1;
103 static int testCount
= 0;
104 static int testList
[4] = {0,0,0,0};
106 int autoTest(Reference
<XResultSet
> &xRes
);
108 void printColumns( Reference
<XResultSet
> &xRes
)
112 char* aPat
= " %-22s ";
113 char* aPat_Short
= " %-12s ";
114 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
115 OSL_TRACE( "ColumnCount = %d", xMeta
->getColumnCount());
116 for(sal_Int32 i
=1;i
<=xMeta
->getColumnCount();++i
)
118 const char *str
= OUtoCStr(xMeta
->getColumnName(i
));
119 OSL_TRACE( aPat
, str
);
122 OSL_TRACE("------------------------------------------------------------------------------------------");
126 OSL_TRACE(": FAILED to get a ResultSet");
129 void printXResultSet( Reference
<XResultSet
> &xRes
)
133 char* aPat
= " %-22s ";
134 char* aPat_Short
= " %-12s ";
135 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
136 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
137 for(sal_Int32 j
=1;j
<=xMeta
->getColumnCount();++j
)
141 const char *str
= OUtoCStr(xRow
->getString(j
));
142 OSL_TRACE( aPat_Short
, str
);
150 OSL_TRACE("FAILED to get a ResultSet");
153 void printXResultSets( Reference
<XResultSet
> &xRes
)
159 while( xRes
.is() && xRes
->next())
161 printXResultSet(xRes
);
164 OSL_TRACE( "%d Row(s)", nRows
);
166 OSL_TRACE("FAILED to get a ResultSet");
171 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
173 // Test some metadata
174 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
177 OSL_TRACE(": got DatabaseMetaData");
179 OUString sQuoteStr
= xDmd
->getIdentifierQuoteString();
180 OSL_TRACE( "Quote String : '%s'", OUtoCStr( sQuoteStr
) );
182 OUString sSQLCmds
= xDmd
->getSQLKeywords();
183 OSL_TRACE( "SQL Commands : '%s'", OUtoCStr( sSQLCmds
) );
185 OSL_TRACE("Testing getColumns() : START");
187 Reference
<XResultSet
> xRes
= xDmd
->getColumns(
188 makeAny(OUString("")), // Catalog
189 OUString("%"), // Schema
190 OUString("%"), // TabName
193 printXResultSets( xRes
);
195 OSL_TRACE("Testing getColumns() : END");
197 OSL_TRACE("Testing getTypeInfo() : START");
199 Reference
<XResultSet
> xRes
= xDmd
-> getTypeInfo();
200 printXResultSets( xRes
);
202 OSL_TRACE("Testing getTypeInfo() : END");
204 OSL_TRACE("Testing getTables() : START");
206 Reference
<XResultSet
> xRes
= xDmd
->getTables(
207 makeAny(OUString("")), // Catalog
208 OUString("%"), // Schema
209 OUString("%"), // TabName
210 Sequence
<rtl::OUString
>() );
211 printXResultSets( xRes
);
213 OSL_TRACE("Testing getTables() : END");
217 OSL_TRACE(": FAILED to get DatabaseMetaData");
221 void TestQuery(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
224 OSL_TRACE("Testing createStatement() & executeQuery() : START");
225 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
226 Reference
<XResultSet
> xRes
;
229 OSL_TRACE(": got statement");
230 OSL_TRACE(": excuteQuery() : START \n");
231 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
232 OUString
sqlPrefix("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
236 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
239 OSL_TRACE("getTables() : START");
240 OUString qut
= xDmd
->getIdentifierQuoteString();
242 Reference
<XResultSet
> xRes
= xDmd
->getTables(
243 makeAny(OUString("")), // Catalog
244 OUString("%"), // Schema
245 OUString("%"), // TabName
246 Sequence
<rtl::OUString
>() );
247 sal_Int32 nTables
= 0;
248 while( xRes
.is() && xRes
->next())
250 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
251 const char *strTableName
= OUtoCStr(xRow
->getString(3));
252 OSL_TRACE("Testing Table:%s",strTableName
);
254 Reference
<XResultSet
> tmpRes
=
255 xStmt
->executeQuery(sqlPrefix
+ qut
+ xRow
->getString(3) + qut
);
257 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
261 OSL_TRACE("Tested Tables:%d",nTables
);
263 } catch ( Exception
&e
) {
264 OSL_TRACE( "Exception caught : %s", OUtoCStr( e
.Message
) );
267 // OSL_TRACE( "Non-UNO Exception caught\n" );
269 OSL_TRACE("excuteQuery() : END");
273 OSL_TRACE(": FAILED to get statement");
275 OSL_TRACE("Testing createStatement() & executeQuery() : END");
277 Reference
< ::com::sun::star::sdbc::XConnection
> TestConnected
278 (Reference
< ::com::sun::star::sdbc::XDriver
> &pDriver
)
280 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> pConnection
;
281 OSL_TRACE("Begin Connect!");
283 Sequence
<PropertyValue
> aValue
;
285 srand( (unsigned)time( NULL
) );
286 nType
= rand() % testCount
+ 1;
287 int nIndex
=0,nCount
=0;
288 for ( nIndex
= 0; nIndex
< 4;nIndex
++)
290 if (testList
[nIndex
])
298 url
=OUString("sdbc:address:ldap://");
300 aValue
[0].Name
= ::rtl::OUString("HostName");
301 aValue
[0].Value
<<= rtl::OUString("sun-ds");
302 aValue
[1].Name
= ::rtl::OUString("BaseDN");
303 aValue
[1].Value
<<= rtl::OUString("dc=sun,dc=com");
306 url
=OUString("sdbc:address:mozilla://");
309 url
=OUString("sdbc:address:outlook://");
312 url
=OUString("sdbc:address:outlookexp://");
315 url
=OUString("sdbc:address:mozilla://");
319 pDriver
->connect(url
,aValue
);
323 int autoTest(Reference
<XResultSet
> &xRes
)
329 while( xRes
.is() && xRes
->next())
333 OSL_TRACE( "%d Row(s)", nRows
);
338 for(times
= 1;times
< 10; times
++)
340 pos
= rand() % nRows
+1;
341 OSL_TRACE("pos:%d",pos
);
343 printXResultSet(xRes
);
349 OSL_TRACE(": FAILED to get a ResultSet");
351 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
352 osl_waitThread(&timeValue
);
355 void SAL_CALL
mozThread(void*)
357 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
360 Reference
< ::com::sun::star::sdbc::XDriver
>
361 m_xDriver(mMgr
->createInstance(
362 OUString("com.sun.star.comp.sdbc.MozabDriver")),
367 m_xConnection
= TestConnected(m_xDriver
);
368 if(m_xConnection
.is())
370 if (!TestMetaData(m_xConnection
))
372 TestQuery(m_xConnection
);
376 OSL_TRACE("Can't connected!");
381 OSL_TRACE("No driver!");
383 } catch ( Exception
&e
) {
384 OSL_TRACE( "Exception caught : %s", OUtoCStr( e
.Message
) );
387 // OSL_TRACE( "Non-UNO Exception caught\n" );
390 const int THREAD_COUNT
=100;
395 PRINTLN("mozThread [opts] threadcount");
397 PRINTLN("-l test ldap");
398 PRINTLN("-m test mozilla");
399 PRINTLN("-p test outlook");
400 PRINTLN("-e test outlook express");
401 PRINTLN("0 < threadcount <= 100, default 100");
404 int main( int argc
, char * argv
[] )
406 int _cdecl
main( int argc
, char * argv
[] )
410 OSL_TRACE("Init UNO");
411 Reference
< XMultiServiceFactory
> xMgr
=InitializeFac();
412 int threadCount
=THREAD_COUNT
;
414 for (nAc
= 1; nAc
< argc
; nAc
++)
416 if (strcmp(argv
[nAc
],"-l") ==0)
418 testList
[testLDAP
] = 1;
419 }else if(strcmp(argv
[nAc
],"-m") ==0)
421 testList
[testMozilla
]=1;
422 }else if(strcmp(argv
[nAc
],"-p") ==0)
425 }else if(strcmp(argv
[nAc
],"-e") ==0)
428 }else if(strcmp(argv
[nAc
],"-h") ==0 || strcmp(argv
[nAc
],"--help") ==0)
434 int tmpCount
= atoi(argv
[nAc
]);
435 if (tmpCount
> 0 && tmpCount
< threadCount
)
437 threadCount
= tmpCount
;
441 testCount
= testList
[testLDAP
] + testList
[testMozilla
] + testList
[testOp
] + testList
[testOe
];
444 testList
[testLDAP
] = 1;
450 OSL_TRACE("Error init UNO");
454 OSL_TRACE("UNO initted");
457 oslThread xThreads
[THREAD_COUNT
];
459 for(index
=0;index
< threadCount
; index
++)
461 xThreads
[index
] = osl_createThread(mozThread
,(void*)NULL
);
462 TimeValue timeValue
= { 1, 0 }; //sleep 1 Seconds to give time to other threads
463 osl_waitThread(&timeValue
);
465 for(index
=0;index
< threadCount
; index
++)
467 if (osl_isThreadRunning(xThreads
[index
]))
468 osl_joinWithThread(xThreads
[index
]);
470 OSL_TRACE("Exiting...");
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */