Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / connectivity / workben / testmoz / main.cxx
blob10e9bc8af060c795388a4607b87a7c57e62b0d1c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/XUniversalContentBroker.hpp>
53 #include <osl/module.h>
55 #include <stdio.h>
57 using namespace comphelper;
58 using namespace cppu;
59 using namespace com::sun::star::uno;
60 using namespace com::sun::star::lang;
61 using namespace com::sun::star::registry;
62 using namespace com::sun::star::ucb;
63 using namespace com::sun::star::beans;
65 using namespace connectivity;
66 using namespace com::sun::star::sdb;
67 using namespace com::sun::star::sdbc;
68 using namespace com::sun::star::sdbcx;
69 using namespace ::com::sun::star::container;
70 using namespace com::sun::star::registry;
72 using ::rtl::OUString;
73 using ::rtl::OUStringBuffer;
74 using ::rtl::OUStringToOString;
76 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
77 #define PRINTSTR(x) printf("%s",x);
79 int autoTest(Reference<XResultSet> &xRes);
81 void printColumns( Reference<XResultSet> &xRes )
83 if(xRes.is()) {
84 char* aPat = " %-22s ";
85 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
86 printf( "ColumnCount = %d\n", xMeta->getColumnCount());
87 for(sal_Int32 i=1;i<=xMeta->getColumnCount();++i)
89 const char *str = OUtoCStr(xMeta->getColumnName(i));
90 printf( aPat, str );
92 printf("\n");
93 printf("------------------------------------------------------------------------------------------\n");
94 } else {
95 printf(": FAILED to get a ResultSet \n");
98 void printXResultSet( Reference<XResultSet> &xRes )
100 if(xRes.is()) {
101 char* aPat_Short = " %-12s ";
102 Reference<XRow> xRow(xRes,UNO_QUERY);
103 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
104 for(sal_Int32 j=1;j<=xMeta->getColumnCount();++j)
106 try {
107 const char *str = OUtoCStr(xRow->getString(j));
108 printf( aPat_Short, str );
109 } catch (...) {
110 printf(" Ex ");
113 printf("\n");
114 } else {
115 printf(": FAILED to get a ResultSet \n");
119 void printXResultSets( Reference<XResultSet> &xRes )
121 if(xRes.is()) {
122 printColumns(xRes);
123 sal_Int32 nRows = 0;
124 while( xRes.is() && xRes->next())
126 printXResultSet(xRes);
127 nRows++;
129 printf( "%d Row(s)\n", nRows);
130 } else {
131 printf(": FAILED to get a ResultSet \n");
135 static const char * const components[] =
137 SAL_MODULENAME( "ucb1" ) // KSO, ABI
138 , SAL_MODULENAME( "ucpfile1" )
139 , SAL_MODULENAME( "cfgmgr2" )
140 , "sax.uno" SAL_DLLEXTENSION
141 , "stocservices.uno" SAL_DLLEXTENSION
142 , SAL_MODULENAME( "fileacc" )
143 , SAL_MODULENAME( "mcnttype" ) //Clipboard Ask Oliver Braun
144 , "i18npool.uno" SAL_DLLEXTENSION
145 // Reading of files in specific encodings like UTF-8 using
146 // createUnoService( "com.sun.star.io.TextInputStream" ) and such
147 , "textinstream.uno" SAL_DLLEXTENSION
148 , "textoutstream.uno" SAL_DLLEXTENSION
149 , "introspection.uno" SAL_DLLEXTENSION
150 , "corereflection.uno" SAL_DLLEXTENSION
151 // RemoteUno
152 , "connector.uno" SAL_DLLEXTENSION
153 , "bridgefac.uno" SAL_DLLEXTENSION
154 , "remotebridge.uno" SAL_DLLEXTENSION
155 , "dbtools2" SAL_DLLEXTENSION
156 , "mozab2" SAL_DLLEXTENSION
157 , "mozabdrv2" SAL_DLLEXTENSION
158 , "sdbc2" SAL_DLLEXTENSION
159 , "dbpool2" SAL_DLLEXTENSION
160 #ifdef SAL_UNX
161 , SVLIBRARY( "dtransX11" ) // OBR
162 #endif
163 #ifdef SAL_W32
164 , SAL_MODULENAME( "sysdtrans" )
165 , SAL_MODULENAME( "ftransl" )
166 , SAL_MODULENAME( "dnd" )
167 #endif
171 Reference< XMultiServiceFactory > InitializeFac( void )
173 OUString path;
174 if( osl_Process_E_None != osl_getExecutableFile( (rtl_uString**)&path ) )
176 printf("Exit.\n");
177 exit( 1 );
179 OSL_ASSERT( path.lastIndexOf( '/' ) >= 0 );
182 ::rtl::OUStringBuffer bufServices( path.copy( 0, path.lastIndexOf( '/' )+1 ) );
183 bufServices.appendAscii("services.rdb");
184 OUString services = bufServices.makeStringAndClear();
186 ::rtl::OUStringBuffer bufTypes( path.copy( 0, path.lastIndexOf( '/' )+1 ) );
187 bufTypes.appendAscii("types.rdb");
188 OUString types = bufTypes.makeStringAndClear();
190 printf("Create Registry.\n");
192 Reference< XMultiServiceFactory > xSMgr;
195 xSMgr = createRegistryServiceFactory( types, services, sal_True );
197 catch( com::sun::star::uno::Exception & )
202 Reference< XMultiServiceFactory > interimSmgr =
203 createRegistryServiceFactory( types, sal_True );
204 Reference< XImplementationRegistration > xIR(
205 interimSmgr->createInstance(
206 OUString::(
207 "com.sun.star.registry.ImplementationRegistration" ) ), UNO_QUERY );
209 Reference< XSimpleRegistry > xReg(
210 interimSmgr->createInstance(
211 OUString::(
212 "com.sun.star.registry.SimpleRegistry" ) ), UNO_QUERY );
213 if ( xReg.is() )
215 xReg->open(services, sal_False, sal_True);
216 if ( xReg->isValid() )
218 OUString loader( "com.sun.star.loader.SharedLibrary" );
219 for( sal_Int32 i = 0; components[i] ; i ++ )
221 printf("Registering %s ... ", components[i]);
222 xIR->registerImplementation(
223 loader, OUString::createFromAscii(components[i]),xReg);
224 printf("done\n");
226 xReg->close();
227 } else
229 printf("Cannot open Registry. Terminating Program\n");
230 exit (1);
234 Reference< XComponent > xComp( interimSmgr, UNO_QUERY );
235 if( xComp.is() )
236 xComp->dispose();
239 // now try it again readonly
240 printf("Opening Registry readonly\n");
241 xSMgr = createRegistryServiceFactory( types, services, sal_True );
243 catch( com::sun::star::uno::Exception & exc )
245 fprintf( stderr, "Couldn't bootstrap uno servicemanager for reason : %s\n" ,
246 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
250 printf("set global factory.\n");
252 //////////////////////////////////////////////////////////////////////
253 // set global factory
254 setProcessServiceFactory( xSMgr );
257 // Create unconfigured Ucb:
258 Reference< XUniversalContentBroker > xUcb
259 ( xSMgr->createInstance( OUString("com.sun.star.ucb.UniversalContentBroker") ), UNO_QUERY_THROW );
261 Reference< XContentProvider > xFileProvider
262 ( xSMgr->createInstance( OUString("com.sun.star.ucb.FileContentProvider") ), UNO_QUERY_THROW );
263 xUcb->registerContentProvider( xFileProvider, OUString("file"), sal_True );
265 return xSMgr;
268 int TestMetaData(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
270 // Test some metadata
271 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
272 if ( xDmd.is() ) {
273 printf(": got DatabaseMetaData \n");
275 OUString sQuoteStr = xDmd->getIdentifierQuoteString();
276 printf( "Quote String : '%s'\n", OUtoCStr( sQuoteStr ) );
278 OUString sSQLCmds = xDmd->getSQLKeywords();
279 printf( "SQL Commands : '%s'\n", OUtoCStr( sSQLCmds ) );
281 printf("Testing getColumns() : START\n");
283 Reference<XResultSet> xRes = xDmd->getColumns(
284 makeAny(OUString("")), // Catalog
285 OUString("%"), // Schema
286 OUString("%"), // TabName
287 OUString("%")
289 printXResultSets( xRes );
291 printf("Testing getColumns() : END\n");
293 printf("Testing getTypeInfo() : START\n");
295 Reference<XResultSet> xRes = xDmd-> getTypeInfo();
296 printXResultSets( xRes );
298 printf("Testing getTypeInfo() : END\n");
300 printf("Testing getTables() : START\n");
302 Reference<XResultSet> xRes = xDmd->getTables(
303 makeAny(OUString("")), // Catalog
304 OUString("%"), // Schema
305 OUString("%"), // TabName
306 Sequence<rtl::OUString>()
308 printXResultSets( xRes );
310 printf("Testing getTables() : END\n");
312 } else {
313 printf(": FAILED to get DatabaseMetaData \n");
316 return 0;
318 int TestBookMark(Reference<XResultSet> &xRes);
319 int TestRowUpdate(Reference<XResultSet> &xRes);
321 Reference<XResultSet> TestQuery(Reference< ::com::sun::star::sdbc::XConnection> &pConnection,sal_Int32 choice)
323 // Try a query
324 printf("Testing createStatement() & executeQuery() : START\n");
325 Reference<XStatement> xStmt = pConnection->createStatement();
326 Reference<XResultSet> xRes;
327 if(xStmt.is())
329 printf(": got statement\n");
330 printf(": excuteQuery() : START \n");
331 // SELECT "First Name", "Display Name", "E-mail" FROM "addr" "addr"
332 char sql[256]="SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM \"addr\"";
333 if (choice!=-1)
335 printf("Please input a query,end by \";\" and less then 256 char plz:\n");
336 char ch=0;
337 int index=0;
338 while (ch != ';')
340 scanf("%c",&ch);
341 sql[index++] = ch;
343 sql[index]= 0;
344 printf("SQL:%s\n",sql);
348 sal_Int32 times=0;
349 for (times = 0;times< 100;times ++)
351 Reference<XResultSet> tmpRes =
352 xStmt->executeQuery(OUString::createFromAscii(sql));
354 autoTest( tmpRes );
355 Reference<XCloseable> clsRes(tmpRes,UNO_QUERY);
356 clsRes->close();
357 printf("Current Times:%d\n",times);
359 TestBookMark(xRes);
360 TestRowUpdate(xRes);
361 } catch ( Exception &e ) {
362 printf( "Exception caught : %s\n", OUtoCStr( e.Message) );
363 } catch (...) {
364 printf( "Non-UNO Exception caught\n" );
366 printf(": excuteQuery() : END \n");
368 else
370 printf(": FAILED to get statement\n");
372 printf("Testing createStatement() & executeQuery() : END\n");
373 return xRes;
375 Reference< ::com::sun::star::sdbc::XConnection> TestConnected
376 (Reference< ::com::sun::star::sdbc::XDriver> &pDriver,sal_Int32 choice)
378 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> pConnection;
379 printf("Begin Connect!\n");
380 OUString url;
381 Sequence<PropertyValue> aValue;
383 switch (choice)
385 case -1:
386 case 1: //mozilla
387 url=OUString("sdbc:address:mozilla://");
388 break;
389 case 2:
390 url=OUString("sdbc:address:ldap://");
391 char hostname[40],basedn[40];
392 scanf("%s %s",hostname,basedn);
393 aValue.realloc(2);
394 aValue[0].Name = ::rtl::OUString("HostName");
395 aValue[0].Value <<= rtl::OUString::createFromAscii(hostname);
396 aValue[1].Name = ::rtl::OUString("BaseDN");
397 aValue[1].Value <<= rtl::OUString::createFromAscii(basedn);
398 break;
399 case 3:
400 case 4:
401 break;
402 case 5:
403 //Default LDAP AB
404 url=OUString("sdbc:address:ldap://");
405 aValue.realloc(2);
406 aValue[0].Name = ::rtl::OUString("HostName");
407 aValue[0].Value <<= rtl::OUString("sun-ds");
408 aValue[1].Name = ::rtl::OUString("BaseDN");
409 aValue[1].Value <<= rtl::OUString("dc=sun,dc=com");
410 break;
411 default:
412 return pConnection;
413 break;
415 pConnection =
416 pDriver->connect(url,aValue);
417 return pConnection;
420 void menuBookmark()
422 PRINTSTR("0 Exit\n")
423 PRINTSTR("1 Show Max rowcount\n")
424 PRINTSTR("2 Move First\n")
425 PRINTSTR("3 Record bookmark 1\n")
426 PRINTSTR("4 Record bookmark 2\n")
427 PRINTSTR("5 Move to bookmark. Usage: 5 bookid\n")
428 PRINTSTR("6 moveRelativeToBookmark, follow bookid rows plz\n")
429 PRINTSTR("7 compareBookmark\n")
430 PRINTSTR("8 print boomark hash. Usage: 8 bookid\n")
431 PRINTSTR("9 print current row\n")
432 PRINTSTR("10 Auto test\n")
434 PRINTSTR("Please Input you choice:")
436 int autoTest(Reference<XResultSet> &xRes)
438 printColumns(xRes);
439 if(xRes.is())
441 sal_Int32 nRows = 0;
442 while( xRes.is() && xRes->next())
444 nRows++;
446 printf( "%d Row(s)\n", nRows);
447 sal_Int32 times;
448 sal_Int32 pos;
449 for(times = 1;times < 100; times ++)
451 pos= rand() % nRows+1;
452 printf("pos:%d\n",pos);
453 xRes->absolute(pos);
454 printXResultSet(xRes);
456 } else {
457 printf(": FAILED to get a ResultSet \n");
459 return 0;
461 int TestBookMark(Reference<XResultSet> &xRes)
463 Reference<XResultSet> mRes(xRes);
464 if (!mRes.is() || !mRes->first())
466 return sal_False;
469 Reference<XRow> mRow(xRes,UNO_QUERY);
470 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(mRes,UNO_QUERY)->getMetaData();
471 printXResultSet(mRes);
472 Reference< ::com::sun::star::sdbcx::XRowLocate> xRowLocate(xRes, UNO_QUERY);
473 ::com::sun::star::uno::Any xBookMark[2];
474 int index;
475 if (xRowLocate.is())
477 int choice=-1;
478 while (choice)
480 menuBookmark();
482 scanf("%d",&choice);
483 printf("Your choice is:%d\n",choice);
484 switch (choice)
486 case 1:
487 printf("Rowcount:Current don't know\n");
488 break;
489 case 2:
490 if (!mRes->isFirst()) mRes->first();
491 break;
492 case 3:
493 xBookMark[0] = xRowLocate->getBookmark();
494 break;
495 case 4:
496 xBookMark[1] = xRowLocate->getBookmark();
497 break;
498 case 5:
499 scanf("%d",&index);
500 if (index == 1 || index == 2)
501 xRowLocate->moveToBookmark(xBookMark[index-1]);
502 break;
503 case 6:
504 int rows;
505 scanf("%d %d",&index,&rows);
506 if (index == 1 || index == 2)
507 xRowLocate->moveRelativeToBookmark(xBookMark[index-1],rows);
508 break;
509 case 7:
510 printf("compareBookmarks:%d\n",xRowLocate->compareBookmarks(xBookMark[0],xBookMark[1]));
511 break;
512 case 8:
513 scanf("%d",&index);
514 printf("HashBookmark[%d]:%d\n",index,xRowLocate->hashBookmark(xBookMark[index-1]));
515 break;
516 case 9:
517 printXResultSet(mRes);
518 break;
519 case 10:
520 autoTest(mRes);
521 break;
522 default:
523 break;
527 return 0;
529 void menuRowUpdate()
531 PRINTSTR("0 Exit\n")
532 PRINTSTR("1 Print Columns\n")
533 PRINTSTR("2 Move to row. Usage:2 rowid\n")
534 PRINTSTR("3 Print Row values\n")
535 PRINTSTR("4 Change Column Value: 4 columnid newvalue\n")
536 PRINTSTR("5 Commit changes\n")
537 PRINTSTR("6 Delete Current Row\n")
538 PRINTSTR("Please Input Your choice:")
540 int TestRowUpdate(Reference<XResultSet> &xRes)
542 if (!xRes.is() || !xRes->first())
544 return sal_False;
546 printf("Test XRowUpdate START\n");
547 Reference< ::com::sun::star::sdbc::XRowUpdate> xRowUpdate(xRes, UNO_QUERY);
548 Reference< ::com::sun::star::sdbc::XResultSetUpdate> xResultSetUpdate(xRes, UNO_QUERY);
549 int index;
550 if (xRowUpdate.is())
552 int choice=-1;
553 char newString[256];
554 while (choice)
556 menuRowUpdate();
558 scanf("%d",&choice);
559 printf("Your choice is:%d\n",choice);
560 switch (choice)
562 case 1:
563 printColumns(xRes);
564 break;
565 case 2:
566 scanf("%d",&index);
567 xRes->absolute(index);
568 break;
569 case 3:
570 printXResultSet(xRes);
571 break;
572 case 4:
573 scanf("%d %s",&index,newString);
574 xRowUpdate->updateString(index,OUString::createFromAscii(newString));
575 printXResultSet(xRes);
576 break;
577 case 5:
578 if (xResultSetUpdate.is())
580 xResultSetUpdate->updateRow();
581 printXResultSet(xRes);
583 else
584 printf("Can't update!\n");
585 break;
586 case 6:
587 if (xResultSetUpdate.is())
589 xResultSetUpdate->deleteRow();
590 printXResultSet(xRes);
592 else
593 printf("Can't update!\n");
594 break;
596 default:
597 break;
601 printf("Test XRowUpdate END\n");
602 return 0;
605 void menuMain()
607 PRINTSTR("0 Exit\n")
608 PRINTSTR("1 Open Mozilla Address Book\n")
609 PRINTSTR("2 Open LDAP. Pleae follow hostname and base dn.\n")
610 PRINTSTR("3 Open Outlook Address Book\n")
611 PRINTSTR("4 Open OE Address Book\n")
612 PRINTSTR("Please Input your choice:")
614 #if (defined UNX)
615 int main( int argc, char * argv[] )
616 #else
617 int _cdecl main( int argc, char * argv[] )
618 #endif
621 Reference< XMultiServiceFactory > xMgr = InitializeFac();
622 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
625 Reference< ::com::sun::star::sdbc::XDriver>
626 m_xDriver(xMgr->createInstance(
627 OUString("com.sun.star.comp.sdbc.MozabDriver")),
628 UNO_QUERY);
629 if(m_xDriver.is())
631 int choice=-1;
632 if (argc > 1 )
634 menuMain();
635 scanf("%d",&choice);
638 while (choice)
641 m_xConnection = TestConnected(m_xDriver,choice);
642 if(m_xConnection.is())
644 if (!TestMetaData(m_xConnection))
646 Reference<XResultSet> xRes=TestQuery(m_xConnection,choice);
647 if (xRes.is())
651 }else printf("Can't connected!\n");
653 if (argc > 1)
655 menuMain();
656 scanf("%d",&choice);
658 else
659 choice = 0;
662 } else {
663 printf("No driver!\n");
666 catch(...)
668 printf("Exception thrown!\n");
670 printf("Exiting...\n");
671 return 0;
674 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */