fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / connectivity / workben / testmoz / main.cxx
bloba5d251c1756ea4e83cec3b7004f4b617fcabc355
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;
72 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
73 #define PRINTSTR(x) printf("%s",x);
75 int autoTest(Reference<XResultSet> &xRes);
77 void printColumns( Reference<XResultSet> &xRes )
79 if(xRes.is()) {
80 char* aPat = " %-22s ";
81 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
82 printf( "ColumnCount = %d\n", xMeta->getColumnCount());
83 for(sal_Int32 i=1;i<=xMeta->getColumnCount();++i)
85 const char *str = OUtoCStr(xMeta->getColumnName(i));
86 printf( aPat, str );
88 printf("\n");
89 printf("------------------------------------------------------------------------------------------\n");
90 } else {
91 printf(": FAILED to get a ResultSet \n");
94 void printXResultSet( Reference<XResultSet> &xRes )
96 if(xRes.is()) {
97 char* aPat_Short = " %-12s ";
98 Reference<XRow> xRow(xRes,UNO_QUERY);
99 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
100 for(sal_Int32 j=1;j<=xMeta->getColumnCount();++j)
102 try {
103 const char *str = OUtoCStr(xRow->getString(j));
104 printf( aPat_Short, str );
105 } catch (...) {
106 printf(" Ex ");
109 printf("\n");
110 } else {
111 printf(": FAILED to get a ResultSet \n");
115 void printXResultSets( Reference<XResultSet> &xRes )
117 if(xRes.is()) {
118 printColumns(xRes);
119 sal_Int32 nRows = 0;
120 while( xRes.is() && xRes->next())
122 printXResultSet(xRes);
123 nRows++;
125 printf( "%d Row(s)\n", nRows);
126 } else {
127 printf(": FAILED to get a ResultSet \n");
131 static const char * const components[] =
133 SAL_MODULENAME( "ucb1" ) // KSO, ABI
134 , SAL_MODULENAME( "ucpfile1" )
135 , SAL_MODULENAME( "cfgmgr2" )
136 , "sax.uno" SAL_DLLEXTENSION
137 , "stocservices.uno" SAL_DLLEXTENSION
138 , SAL_MODULENAME( "fileacc" )
139 , SAL_MODULENAME( "mcnttype" ) //Clipboard Ask Oliver Braun
140 , "i18npool.uno" SAL_DLLEXTENSION
141 // Reading of files in specific encodings like UTF-8 using
142 // createUnoService( "com.sun.star.io.TextInputStream" ) and such
143 , "textinstream.uno" SAL_DLLEXTENSION
144 , "textoutstream.uno" SAL_DLLEXTENSION
145 , "introspection.uno" SAL_DLLEXTENSION
146 , "corereflection.uno" SAL_DLLEXTENSION
147 // RemoteUno
148 , "connector.uno" SAL_DLLEXTENSION
149 , "bridgefac.uno" SAL_DLLEXTENSION
150 , "remotebridge.uno" SAL_DLLEXTENSION
151 , "dbtools2" SAL_DLLEXTENSION
152 , "mozab2" SAL_DLLEXTENSION
153 , "mozabdrv2" SAL_DLLEXTENSION
154 , "sdbc2" SAL_DLLEXTENSION
155 , "dbpool2" SAL_DLLEXTENSION
156 #ifdef SAL_UNX
157 , SVLIBRARY( "dtransX11" ) // OBR
158 #endif
159 #ifdef SAL_W32
160 , SAL_MODULENAME( "sysdtrans" )
161 , SAL_MODULENAME( "ftransl" )
162 , SAL_MODULENAME( "dnd" )
163 #endif
167 Reference< XMultiServiceFactory > InitializeFac( void )
169 OUString path;
170 if( osl_Process_E_None != osl_getExecutableFile( (rtl_uString**)&path ) )
172 printf("Exit.\n");
173 exit( 1 );
175 OSL_ASSERT( path.lastIndexOf( '/' ) >= 0 );
178 OUStringBuffer bufServices( path.copy( 0, path.lastIndexOf( '/' )+1 ) );
179 bufServices.appendAscii("services.rdb");
180 OUString services = bufServices.makeStringAndClear();
182 OUStringBuffer bufTypes( path.copy( 0, path.lastIndexOf( '/' )+1 ) );
183 bufTypes.appendAscii("types.rdb");
184 OUString types = bufTypes.makeStringAndClear();
186 printf("Create Registry.\n");
188 Reference< XMultiServiceFactory > xSMgr;
191 xSMgr = createRegistryServiceFactory( types, services, sal_True );
193 catch( com::sun::star::uno::Exception & )
198 Reference< XMultiServiceFactory > interimSmgr =
199 createRegistryServiceFactory( types, sal_True );
200 Reference< XImplementationRegistration > xIR(
201 interimSmgr->createInstance(
202 OUString::(
203 "com.sun.star.registry.ImplementationRegistration" ) ), UNO_QUERY );
205 Reference< XSimpleRegistry > xReg(
206 interimSmgr->createInstance(
207 OUString::(
208 "com.sun.star.registry.SimpleRegistry" ) ), UNO_QUERY );
209 if ( xReg.is() )
211 xReg->open(services, sal_False, sal_True);
212 if ( xReg->isValid() )
214 OUString loader( "com.sun.star.loader.SharedLibrary" );
215 for( sal_Int32 i = 0; components[i] ; i ++ )
217 printf("Registering %s ... ", components[i]);
218 xIR->registerImplementation(
219 loader, OUString::createFromAscii(components[i]),xReg);
220 printf("done\n");
222 xReg->close();
223 } else
225 printf("Cannot open Registry. Terminating Program\n");
226 exit (1);
230 Reference< XComponent > xComp( interimSmgr, UNO_QUERY );
231 if( xComp.is() )
232 xComp->dispose();
235 // now try it again readonly
236 printf("Opening Registry readonly\n");
237 xSMgr = createRegistryServiceFactory( types, services, sal_True );
239 catch( com::sun::star::uno::Exception & exc )
241 fprintf( stderr, "Couldn't bootstrap uno servicemanager for reason : %s\n" ,
242 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
246 printf("set global factory.\n");
248 //////////////////////////////////////////////////////////////////////
249 // set global factory
250 setProcessServiceFactory( xSMgr );
253 // Create unconfigured Ucb:
254 Reference< XUniversalContentBroker > xUcb
255 ( xSMgr->createInstance( OUString("com.sun.star.ucb.UniversalContentBroker") ), UNO_QUERY_THROW );
257 Reference< XContentProvider > xFileProvider
258 ( xSMgr->createInstance( OUString("com.sun.star.ucb.FileContentProvider") ), UNO_QUERY_THROW );
259 xUcb->registerContentProvider( xFileProvider, OUString("file"), sal_True );
261 return xSMgr;
264 int TestMetaData(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
266 // Test some metadata
267 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
268 if ( xDmd.is() ) {
269 printf(": got DatabaseMetaData \n");
271 OUString sQuoteStr = xDmd->getIdentifierQuoteString();
272 printf( "Quote String : '%s'\n", OUtoCStr( sQuoteStr ) );
274 OUString sSQLCmds = xDmd->getSQLKeywords();
275 printf( "SQL Commands : '%s'\n", OUtoCStr( sSQLCmds ) );
277 printf("Testing getColumns() : START\n");
279 Reference<XResultSet> xRes = xDmd->getColumns(
280 makeAny(OUString("")), // Catalog
281 OUString("%"), // Schema
282 OUString("%"), // TabName
283 OUString("%")
285 printXResultSets( xRes );
287 printf("Testing getColumns() : END\n");
289 printf("Testing getTypeInfo() : START\n");
291 Reference<XResultSet> xRes = xDmd-> getTypeInfo();
292 printXResultSets( xRes );
294 printf("Testing getTypeInfo() : END\n");
296 printf("Testing getTables() : START\n");
298 Reference<XResultSet> xRes = xDmd->getTables(
299 makeAny(OUString("")), // Catalog
300 OUString("%"), // Schema
301 OUString("%"), // TabName
302 Sequence<OUString>()
304 printXResultSets( xRes );
306 printf("Testing getTables() : END\n");
308 } else {
309 printf(": FAILED to get DatabaseMetaData \n");
312 return 0;
314 int TestBookMark(Reference<XResultSet> &xRes);
315 int TestRowUpdate(Reference<XResultSet> &xRes);
317 Reference<XResultSet> TestQuery(Reference< ::com::sun::star::sdbc::XConnection> &pConnection,sal_Int32 choice)
319 // Try a query
320 printf("Testing createStatement() & executeQuery() : START\n");
321 Reference<XStatement> xStmt = pConnection->createStatement();
322 Reference<XResultSet> xRes;
323 if(xStmt.is())
325 printf(": got statement\n");
326 printf(": excuteQuery() : START \n");
327 // SELECT "First Name", "Display Name", "E-mail" FROM "addr" "addr"
328 char sql[256]="SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM \"addr\"";
329 if (choice!=-1)
331 printf("Please input a query,end by \";\" and less then 256 char plz:\n");
332 char ch=0;
333 int index=0;
334 while (ch != ';')
336 scanf("%c",&ch);
337 sql[index++] = ch;
339 sql[index]= 0;
340 printf("SQL:%s\n",sql);
344 sal_Int32 times=0;
345 for (times = 0;times< 100;times ++)
347 Reference<XResultSet> tmpRes =
348 xStmt->executeQuery(OUString::createFromAscii(sql));
350 autoTest( tmpRes );
351 Reference<XCloseable> clsRes(tmpRes,UNO_QUERY);
352 clsRes->close();
353 printf("Current Times:%d\n",times);
355 TestBookMark(xRes);
356 TestRowUpdate(xRes);
357 } catch ( Exception &e ) {
358 printf( "Exception caught : %s\n", OUtoCStr( e.Message) );
359 } catch (...) {
360 printf( "Non-UNO Exception caught\n" );
362 printf(": excuteQuery() : END \n");
364 else
366 printf(": FAILED to get statement\n");
368 printf("Testing createStatement() & executeQuery() : END\n");
369 return xRes;
371 Reference< ::com::sun::star::sdbc::XConnection> TestConnected
372 (Reference< ::com::sun::star::sdbc::XDriver> &pDriver,sal_Int32 choice)
374 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> pConnection;
375 printf("Begin Connect!\n");
376 OUString url;
377 Sequence<PropertyValue> aValue;
379 switch (choice)
381 case -1:
382 case 1: //mozilla
383 url=OUString("sdbc:address:mozilla://");
384 break;
385 case 2:
386 url=OUString("sdbc:address:ldap://");
387 char hostname[40],basedn[40];
388 scanf("%s %s",hostname,basedn);
389 aValue.realloc(2);
390 aValue[0].Name = OUString("HostName");
391 aValue[0].Value <<= OUString::createFromAscii(hostname);
392 aValue[1].Name = OUString("BaseDN");
393 aValue[1].Value <<= OUString::createFromAscii(basedn);
394 break;
395 case 3:
396 case 4:
397 break;
398 case 5:
399 //Default LDAP AB
400 url=OUString("sdbc:address:ldap://");
401 aValue.realloc(2);
402 aValue[0].Name = OUString("HostName");
403 aValue[0].Value <<= OUString("sun-ds");
404 aValue[1].Name = OUString("BaseDN");
405 aValue[1].Value <<= OUString("dc=sun,dc=com");
406 break;
407 default:
408 return pConnection;
410 pConnection =
411 pDriver->connect(url,aValue);
412 return pConnection;
415 void menuBookmark()
417 PRINTSTR("0 Exit\n")
418 PRINTSTR("1 Show Max rowcount\n")
419 PRINTSTR("2 Move First\n")
420 PRINTSTR("3 Record bookmark 1\n")
421 PRINTSTR("4 Record bookmark 2\n")
422 PRINTSTR("5 Move to bookmark. Usage: 5 bookid\n")
423 PRINTSTR("6 moveRelativeToBookmark, follow bookid rows plz\n")
424 PRINTSTR("7 compareBookmark\n")
425 PRINTSTR("8 print boomark hash. Usage: 8 bookid\n")
426 PRINTSTR("9 print current row\n")
427 PRINTSTR("10 Auto test\n")
429 PRINTSTR("Please Input you choice:")
431 int autoTest(Reference<XResultSet> &xRes)
433 printColumns(xRes);
434 if(xRes.is())
436 sal_Int32 nRows = 0;
437 while( xRes.is() && xRes->next())
439 nRows++;
441 printf( "%d Row(s)\n", nRows);
442 sal_Int32 times;
443 sal_Int32 pos;
444 for(times = 1;times < 100; times ++)
446 pos= rand() % nRows+1;
447 printf("pos:%d\n",pos);
448 xRes->absolute(pos);
449 printXResultSet(xRes);
451 } else {
452 printf(": FAILED to get a ResultSet \n");
454 return 0;
456 int TestBookMark(Reference<XResultSet> &xRes)
458 Reference<XResultSet> mRes(xRes);
459 if (!mRes.is() || !mRes->first())
461 return sal_False;
464 Reference<XRow> mRow(xRes,UNO_QUERY);
465 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(mRes,UNO_QUERY)->getMetaData();
466 printXResultSet(mRes);
467 Reference< ::com::sun::star::sdbcx::XRowLocate> xRowLocate(xRes, UNO_QUERY);
468 ::com::sun::star::uno::Any xBookMark[2];
469 int index;
470 if (xRowLocate.is())
472 int choice=-1;
473 while (choice)
475 menuBookmark();
477 scanf("%d",&choice);
478 printf("Your choice is:%d\n",choice);
479 switch (choice)
481 case 1:
482 printf("Rowcount:Current don't know\n");
483 break;
484 case 2:
485 if (!mRes->isFirst()) mRes->first();
486 break;
487 case 3:
488 xBookMark[0] = xRowLocate->getBookmark();
489 break;
490 case 4:
491 xBookMark[1] = xRowLocate->getBookmark();
492 break;
493 case 5:
494 scanf("%d",&index);
495 if (index == 1 || index == 2)
496 xRowLocate->moveToBookmark(xBookMark[index-1]);
497 break;
498 case 6:
499 int rows;
500 scanf("%d %d",&index,&rows);
501 if (index == 1 || index == 2)
502 xRowLocate->moveRelativeToBookmark(xBookMark[index-1],rows);
503 break;
504 case 7:
505 printf("compareBookmarks:%d\n",xRowLocate->compareBookmarks(xBookMark[0],xBookMark[1]));
506 break;
507 case 8:
508 scanf("%d",&index);
509 printf("HashBookmark[%d]:%d\n",index,xRowLocate->hashBookmark(xBookMark[index-1]));
510 break;
511 case 9:
512 printXResultSet(mRes);
513 break;
514 case 10:
515 autoTest(mRes);
516 break;
517 default:
518 break;
522 return 0;
524 void menuRowUpdate()
526 PRINTSTR("0 Exit\n")
527 PRINTSTR("1 Print Columns\n")
528 PRINTSTR("2 Move to row. Usage:2 rowid\n")
529 PRINTSTR("3 Print Row values\n")
530 PRINTSTR("4 Change Column Value: 4 columnid newvalue\n")
531 PRINTSTR("5 Commit changes\n")
532 PRINTSTR("6 Delete Current Row\n")
533 PRINTSTR("Please Input Your choice:")
535 int TestRowUpdate(Reference<XResultSet> &xRes)
537 if (!xRes.is() || !xRes->first())
539 return sal_False;
541 printf("Test XRowUpdate START\n");
542 Reference< ::com::sun::star::sdbc::XRowUpdate> xRowUpdate(xRes, UNO_QUERY);
543 Reference< ::com::sun::star::sdbc::XResultSetUpdate> xResultSetUpdate(xRes, UNO_QUERY);
544 int index;
545 if (xRowUpdate.is())
547 int choice=-1;
548 char newString[256];
549 while (choice)
551 menuRowUpdate();
553 scanf("%d",&choice);
554 printf("Your choice is:%d\n",choice);
555 switch (choice)
557 case 1:
558 printColumns(xRes);
559 break;
560 case 2:
561 scanf("%d",&index);
562 xRes->absolute(index);
563 break;
564 case 3:
565 printXResultSet(xRes);
566 break;
567 case 4:
568 scanf("%d %s",&index,newString);
569 xRowUpdate->updateString(index,OUString::createFromAscii(newString));
570 printXResultSet(xRes);
571 break;
572 case 5:
573 if (xResultSetUpdate.is())
575 xResultSetUpdate->updateRow();
576 printXResultSet(xRes);
578 else
579 printf("Can't update!\n");
580 break;
581 case 6:
582 if (xResultSetUpdate.is())
584 xResultSetUpdate->deleteRow();
585 printXResultSet(xRes);
587 else
588 printf("Can't update!\n");
589 break;
591 default:
592 break;
596 printf("Test XRowUpdate END\n");
597 return 0;
600 void menuMain()
602 PRINTSTR("0 Exit\n")
603 PRINTSTR("1 Open Mozilla Address Book\n")
604 PRINTSTR("2 Open LDAP. Pleae follow hostname and base dn.\n")
605 PRINTSTR("3 Open Outlook Address Book\n")
606 PRINTSTR("4 Open OE Address Book\n")
607 PRINTSTR("Please Input your choice:")
609 #if (defined UNX)
610 int main( int argc, char * argv[] )
611 #else
612 int _cdecl main( int argc, char * argv[] )
613 #endif
616 Reference< XMultiServiceFactory > xMgr = InitializeFac();
617 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
620 Reference< ::com::sun::star::sdbc::XDriver>
621 m_xDriver(xMgr->createInstance(
622 OUString("com.sun.star.comp.sdbc.MozabDriver")),
623 UNO_QUERY);
624 if(m_xDriver.is())
626 int choice=-1;
627 if (argc > 1 )
629 menuMain();
630 scanf("%d",&choice);
633 while (choice)
636 m_xConnection = TestConnected(m_xDriver,choice);
637 if(m_xConnection.is())
639 if (!TestMetaData(m_xConnection))
641 Reference<XResultSet> xRes=TestQuery(m_xConnection,choice);
642 if (xRes.is())
646 }else printf("Can't connected!\n");
648 if (argc > 1)
650 menuMain();
651 scanf("%d",&choice);
653 else
654 choice = 0;
657 } else {
658 printf("No driver!\n");
661 catch(...)
663 printf("Exception thrown!\n");
665 printf("Exiting...\n");
666 return 0;
669 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */