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: main.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>
76 using namespace comphelper
;
79 using namespace com::sun::star::uno
;
80 using namespace com::sun::star::lang
;
81 using namespace com::sun::star::registry
;
82 using namespace com::sun::star::ucb
;
83 using namespace com::sun::star::beans
;
85 //using namespace com::sun::star;
86 using namespace connectivity
;
87 using namespace com::sun::star::sdb
;
88 using namespace com::sun::star::sdbc
;
89 using namespace com::sun::star::sdbcx
;
90 using namespace ::com::sun::star::container
;
91 using namespace com::sun::star::registry
;
93 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
94 Reference
< XContentProviderManager
> globalUcb
;
95 #define PRINTSTR(x) printf("%s",x);
97 int autoTest(Reference
<XResultSet
> &xRes
);
99 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 printf( "ColumnCount = %d\n", xMeta
->getColumnCount());
106 for(sal_Int32 i
=1;i
<=xMeta
->getColumnCount();++i
)
108 // printf(aPat.getStr(), xMeta->getColumnName(i).getStr());
109 const char *str
= OUtoCStr(xMeta
->getColumnName(i
));
111 // printf( aPat_Short, str );
117 printf("------------------------------------------------------------------------------------------\n");
119 printf(": FAILED to get a ResultSet \n");
122 void printXResultSet( Reference
<XResultSet
> &xRes
)
125 char* aPat
= " %-22s ";
126 char* aPat_Short
= " %-12s ";
127 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
128 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(xRes
,UNO_QUERY
)->getMetaData();
129 for(sal_Int32 j
=1;j
<=xMeta
->getColumnCount();++j
)
132 const char *str
= OUtoCStr(xRow
->getString(j
));
134 // printf( aPat_Short, str );
136 printf( aPat_Short
, str
);
144 printf(": FAILED to get a ResultSet \n");
148 void printXResultSets( Reference
<XResultSet
> &xRes
)
153 while( xRes
.is() && xRes
->next())
155 printXResultSet(xRes
);
158 printf( "%d Row(s)\n", nRows
);
160 printf(": FAILED to get a ResultSet \n");
164 //#define OUtoCStr( x ) ( ::rtl::OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
165 static const char * const components
[] =
167 SAL_MODULENAME( "ucb1" ) // KSO, ABI
168 , SAL_MODULENAME( "ucpfile1" )
169 , SAL_MODULENAME( "cfgmgr2" )
170 , "sax.uno" SAL_DLLEXTENSION
171 , "stocservices.uno" SAL_DLLEXTENSION
172 , SAL_MODULENAME( "fileacc" )
173 , SAL_MODULENAME( "mcnttype" ) //Clipboard Ask Oliver Braun
174 , "i18npool.uno" SAL_DLLEXTENSION
175 // Reading of files in specific encodings like UTF-8 using
176 // createUnoService( "com.sun.star.io.TextInputStream" ) and such
177 , "textinstream.uno" SAL_DLLEXTENSION
178 , "textoutstream.uno" SAL_DLLEXTENSION
179 , "introspection.uno" SAL_DLLEXTENSION
180 , "corereflection.uno" SAL_DLLEXTENSION
182 , "connector.uno" SAL_DLLEXTENSION
183 , "bridgefac.uno" SAL_DLLEXTENSION
184 , "remotebridge.uno" SAL_DLLEXTENSION
185 , "dbtools2" SAL_DLLEXTENSION
186 , "mozab2" SAL_DLLEXTENSION
187 , "mozabdrv2" SAL_DLLEXTENSION
188 , "sdbc2" SAL_DLLEXTENSION
189 , "dbpool2" SAL_DLLEXTENSION
191 , SVLIBRARY( "dtransX11" ) // OBR
194 , SAL_MODULENAME( "sysdtrans" )
195 , SAL_MODULENAME( "ftransl" )
196 , SAL_MODULENAME( "dnd" )
201 Reference
< XMultiServiceFactory
> InitializeFac( void )
204 if( osl_Process_E_None
!= osl_getExecutableFile( (rtl_uString
**)&path
) )
209 OSL_ASSERT( path
.lastIndexOf( '/' ) >= 0 );
212 ::rtl::OUStringBuffer
bufServices( path
.copy( 0, path
.lastIndexOf( '/' )+1 ) );
213 bufServices
.appendAscii("services.rdb");
214 OUString services
= bufServices
.makeStringAndClear();
216 ::rtl::OUStringBuffer
bufTypes( path
.copy( 0, path
.lastIndexOf( '/' )+1 ) );
217 bufTypes
.appendAscii("types.rdb");
218 OUString types
= bufTypes
.makeStringAndClear();
220 printf("Create Registry.\n");
222 Reference
< XMultiServiceFactory
> xSMgr
;
225 xSMgr
= createRegistryServiceFactory( types
, services
, sal_True
);
227 catch( com::sun::star::uno::Exception
& )
232 Reference
< XMultiServiceFactory
> interimSmgr
=
233 createRegistryServiceFactory( types
, sal_True
);
234 Reference
< XImplementationRegistration
> xIR(
235 interimSmgr
->createInstance(
236 OUString::createFromAscii(
237 "com.sun.star.registry.ImplementationRegistration" ) ), UNO_QUERY
);
239 Reference
< XSimpleRegistry
> xReg(
240 interimSmgr
->createInstance(
241 OUString::createFromAscii(
242 "com.sun.star.registry.SimpleRegistry" ) ), UNO_QUERY
);
245 xReg
->open(services
, sal_False
, sal_True
);
246 if ( xReg
->isValid() )
248 OUString
loader( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.SharedLibrary" ));
249 for( sal_Int32 i
= 0; components
[i
] ; i
++ )
251 printf("Registering %s ... ", components
[i
]);
252 xIR
->registerImplementation(
253 loader
, OUString::createFromAscii(components
[i
]),xReg
);
259 printf("Cannot open Registry. Terminating Program\n");
264 Reference
< XComponent
> xComp( interimSmgr
, UNO_QUERY
);
269 // now try it again readonly
270 printf("Opening Registry readonly\n");
271 xSMgr
= createRegistryServiceFactory( types
, services
, sal_True
);
273 catch( com::sun::star::uno::Exception
& exc
)
275 fprintf( stderr
, "Couldn't bootstrap uno servicemanager for reason : %s\n" ,
276 OUStringToOString( exc
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
280 printf("set global factory.\n");
282 //////////////////////////////////////////////////////////////////////
283 // set global factory
284 setProcessServiceFactory( xSMgr
);
286 /* // Create simple ConfigManager
287 Sequence< Any > aConfArgs(3);
288 aConfArgs[0] <<= PropertyValue( OUString::createFromAscii("servertype"), 0, makeAny( OUString::createFromAscii("local") ), ::com::sun::star::beans::PropertyState_DIRECT_VALUE );
289 aConfArgs[1] <<= PropertyValue( OUString::createFromAscii("sourcepath"), 0, makeAny( OUString::createFromAscii("g:\\") ), ::com::sun::star::beans::PropertyState_DIRECT_VALUE );
290 aConfArgs[2] <<= PropertyValue( OUString::createFromAscii("updatepath"), 0, makeAny( OUString::createFromAscii("g:\\") ), ::com::sun::star::beans::PropertyState_DIRECT_VALUE );
292 Reference< XContentProvider > xConfProvider
293 ( xSMgr->createInstanceWithArguments( OUString::createFromAscii( "com.sun.star.configuration.ConfigurationProvider" ), aConfArgs), UNO_QUERY );
297 // Create unconfigured Ucb:
298 /* Sequence< Any > aArgs(1);
299 aArgs[1] = makeAny ( xConfProvider );*/
300 Sequence
< Any
> aArgs
;
301 ::ucb::ContentBroker::initialize( xSMgr
, aArgs
);
302 Reference
< XContentProviderManager
> xUcb
=
303 ucb::ContentBroker::get()->getContentProviderManagerInterface();
305 Reference
< XContentProvider
> xFileProvider
306 ( xSMgr
->createInstance( OUString::createFromAscii( "com.sun.star.ucb.FileContentProvider" ) ), UNO_QUERY
);
307 xUcb
->registerContentProvider( xFileProvider
, OUString::createFromAscii( "file" ), sal_True
);
310 /* Reference< XContentProvider > xPackageProvider
311 ( xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.ucb.PackageContentProvider" ) ), UNO_QUERY );
312 xUcb->registerContentProvider( xPackageProvider, OUString::createFromAscii( "vnd.sun.star.pkg" ), sal_True );
318 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
320 // Test some metadata
321 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
323 printf(": got DatabaseMetaData \n");
325 OUString sQuoteStr
= xDmd
->getIdentifierQuoteString();
326 printf( "Quote String : '%s'\n", OUtoCStr( sQuoteStr
) );
328 OUString sSQLCmds
= xDmd
->getSQLKeywords();
329 printf( "SQL Commands : '%s'\n", OUtoCStr( sSQLCmds
) );
331 printf("Testing getColumns() : START\n");
333 Reference
<XResultSet
> xRes
= xDmd
->getColumns(
334 makeAny(OUString::createFromAscii("")), // Catalog
335 OUString::createFromAscii("%"), // Schema
336 OUString::createFromAscii("%"), // TabName
337 OUString::createFromAscii("%")
339 printXResultSets( xRes
);
341 printf("Testing getColumns() : END\n");
343 printf("Testing getTypeInfo() : START\n");
345 Reference
<XResultSet
> xRes
= xDmd
-> getTypeInfo();
346 printXResultSets( xRes
);
348 printf("Testing getTypeInfo() : END\n");
350 printf("Testing getTables() : START\n");
352 Reference
<XResultSet
> xRes
= xDmd
->getTables(
353 makeAny(OUString::createFromAscii("")), // Catalog
354 OUString::createFromAscii("%"), // Schema
355 OUString::createFromAscii("%"), // TabName
356 Sequence
<rtl::OUString
>()
358 printXResultSets( xRes
);
360 printf("Testing getTables() : END\n");
363 printf(": FAILED to get DatabaseMetaData \n");
368 int TestBookMark(Reference
<XResultSet
> &xRes
);
369 int TestRowUpdate(Reference
<XResultSet
> &xRes
);
371 Reference
<XResultSet
> TestQuery(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
,sal_Int32 choice
)
374 printf("Testing createStatement() & executeQuery() : START\n");
375 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
376 Reference
<XResultSet
> xRes
;
379 printf(": got statement\n");
380 printf(": excuteQuery() : START \n");
381 // SELECT "First Name", "Display Name", "E-mail" FROM "addr" "addr"
382 char sql
[256]="SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM \"addr\"";
385 printf("Please input a query,end by \";\" and less then 256 char plz:\n");
394 printf("SQL:%s\n",sql
);
399 for (times
= 0;times
< 100;times
++)
401 Reference
<XResultSet
> tmpRes
=
402 //xStmt->executeQuery(OUString::createFromAscii("SELECT * FROM \"addr\""));
403 xStmt
->executeQuery(OUString::createFromAscii(sql
));
404 // xStmt->executeQuery(OUString::createFromAscii("SELECT * FROM \"Personal Address Book\" WHERE ( PrimaryEmail IS NULL )"));
405 // xStmt->executeQuery(OUString::createFromAscii("SELECT * FROM \"Personal Address Book\" WHERE ( PrimaryEmail LIKE \"Darren\" )"));
406 // xStmt->executeQuery(OUString::createFromAscii("SELECT * FROM \"Personal Address Book\""));
407 // xStmt->executeQuery(OUString::createFromAscii("SELECT * FROM \"myldap\" WHERE ( PrimaryEmail LIKE \"%Darren%\" OR DisplayName LIKE \"%Darren%\" )"));
410 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
412 printf("Current Times:%d\n",times
);
416 } catch ( Exception
&e
) {
417 printf( "Exception caught : %s\n", OUtoCStr( e
.Message
) );
419 printf( "Non-UNO Exception caught\n" );
421 printf(": excuteQuery() : END \n");
425 printf(": FAILED to get statement\n");
427 printf("Testing createStatement() & executeQuery() : END\n");
430 Reference
< ::com::sun::star::sdbc::XConnection
> TestConnected
431 (Reference
< ::com::sun::star::sdbc::XDriver
> &pDriver
,sal_Int32 choice
)
433 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> pConnection
;
434 printf("Begin Connect!\n");
436 Sequence
<PropertyValue
> aValue
;
442 url
=OUString::createFromAscii("sdbc:address:mozilla://");
445 url
=OUString::createFromAscii("sdbc:address:ldap://");
446 char hostname
[40],basedn
[40];
447 scanf("%s %s",hostname
,basedn
);
449 aValue
[0].Name
= ::rtl::OUString::createFromAscii("HostName");
450 aValue
[0].Value
<<= rtl::OUString::createFromAscii(hostname
);
451 aValue
[1].Name
= ::rtl::OUString::createFromAscii("BaseDN");
452 aValue
[1].Value
<<= rtl::OUString::createFromAscii(basedn
);
459 url
=OUString::createFromAscii("sdbc:address:ldap://");
461 aValue
[0].Name
= ::rtl::OUString::createFromAscii("HostName");
462 aValue
[0].Value
<<= rtl::OUString::createFromAscii("sun-ds");
463 aValue
[1].Name
= ::rtl::OUString::createFromAscii("BaseDN");
464 aValue
[1].Value
<<= rtl::OUString::createFromAscii("dc=sun,dc=com");
471 pDriver
->connect(url
,aValue
);
478 PRINTSTR("1 Show Max rowcount\n")
479 PRINTSTR("2 Move First\n")
480 PRINTSTR("3 Record bookmark 1\n")
481 PRINTSTR("4 Record bookmark 2\n")
482 PRINTSTR("5 Move to bookmark. Usage: 5 bookid\n")
483 PRINTSTR("6 moveRelativeToBookmark, follow bookid rows plz\n")
484 PRINTSTR("7 compareBookmark\n")
485 PRINTSTR("8 print boomark hash. Usage: 8 bookid\n")
486 PRINTSTR("9 print current row\n")
487 PRINTSTR("10 Auto test\n")
489 PRINTSTR("Please Input you choice:")
491 int autoTest(Reference
<XResultSet
> &xRes
)
496 while( xRes
.is() && xRes
->next())
500 printf( "%d Row(s)\n", nRows
);
503 for(times
= 1;times
< 100; times
++)
505 pos
= rand() % nRows
+1;
506 printf("pos:%d\n",pos
);
508 printXResultSet(xRes
);
511 printf(": FAILED to get a ResultSet \n");
515 int TestBookMark(Reference
<XResultSet
> &xRes
)
517 Reference
<XResultSet
> mRes(xRes
);
518 if (!mRes
.is() || !mRes
->first())
523 Reference
<XRow
> mRow(xRes
,UNO_QUERY
);
524 Reference
<XResultSetMetaData
> xMeta
= Reference
<XResultSetMetaDataSupplier
>(mRes
,UNO_QUERY
)->getMetaData();
525 printXResultSet(mRes
);
526 Reference
< ::com::sun::star::sdbcx::XRowLocate
> xRowLocate(xRes
, UNO_QUERY
);
527 ::com::sun::star::uno::Any xBookMark
[2];
537 printf("Your choice is:%d\n",choice
);
541 printf("Rowcount:Current don't know\n");
544 if (!mRes
->isFirst()) mRes
->first();
547 xBookMark
[0] = xRowLocate
->getBookmark();
550 xBookMark
[1] = xRowLocate
->getBookmark();
554 if (index
== 1 || index
== 2)
555 xRowLocate
->moveToBookmark(xBookMark
[index
-1]);
559 scanf("%d %d",&index
,&rows
);
560 if (index
== 1 || index
== 2)
561 xRowLocate
->moveRelativeToBookmark(xBookMark
[index
-1],rows
);
564 printf("compareBookmarks:%d\n",xRowLocate
->compareBookmarks(xBookMark
[0],xBookMark
[1]));
568 printf("HashBookmark[%d]:%d\n",index
,xRowLocate
->hashBookmark(xBookMark
[index
-1]));
571 printXResultSet(mRes
);
586 PRINTSTR("1 Print Columns\n")
587 PRINTSTR("2 Move to row. Usage:2 rowid\n")
588 PRINTSTR("3 Print Row values\n")
589 PRINTSTR("4 Change Column Value: 4 columnid newvalue\n")
590 PRINTSTR("5 Commit changes\n")
591 PRINTSTR("6 Delete Current Row\n")
592 PRINTSTR("Please Input Your choice:")
594 int TestRowUpdate(Reference
<XResultSet
> &xRes
)
596 if (!xRes
.is() || !xRes
->first())
600 printf("Test XRowUpdate START\n");
601 Reference
< ::com::sun::star::sdbc::XRowUpdate
> xRowUpdate(xRes
, UNO_QUERY
);
602 Reference
< ::com::sun::star::sdbc::XResultSetUpdate
> xResultSetUpdate(xRes
, UNO_QUERY
);
613 printf("Your choice is:%d\n",choice
);
621 xRes
->absolute(index
);
624 printXResultSet(xRes
);
627 scanf("%d %s",&index
,newString
);
628 xRowUpdate
->updateString(index
,OUString::createFromAscii(newString
));
629 printXResultSet(xRes
);
632 if (xResultSetUpdate
.is())
634 xResultSetUpdate
->updateRow();
635 printXResultSet(xRes
);
638 printf("Can't update!\n");
641 if (xResultSetUpdate
.is())
643 xResultSetUpdate
->deleteRow();
644 printXResultSet(xRes
);
647 printf("Can't update!\n");
655 printf("Test XRowUpdate END\n");
662 PRINTSTR("1 Open Mozilla Address Book\n")
663 PRINTSTR("2 Open LDAP. Pleae follow hostname and base dn.\n")
664 PRINTSTR("3 Open Outlook Address Book\n")
665 PRINTSTR("4 Open OE Address Book\n")
666 PRINTSTR("Please Input your choice:")
668 #if (defined UNX) || (defined OS2)
669 int main( int argc
, char * argv
[] )
671 int _cdecl
main( int argc
, char * argv
[] )
675 Reference
< XMultiServiceFactory
> xMgr
= InitializeFac();
676 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
679 Reference
< ::com::sun::star::sdbc::XDriver
>
680 m_xDriver(xMgr
->createInstance(
681 OUString::createFromAscii("com.sun.star.comp.sdbc.MozabDriver")),
695 m_xConnection
= TestConnected(m_xDriver
,choice
);
696 if(m_xConnection
.is())
698 if (!TestMetaData(m_xConnection
))
700 Reference
<XResultSet
> xRes
=TestQuery(m_xConnection
,choice
);
705 }else printf("Can't connected!\n");
717 printf("No driver!\n");
722 printf("Exception thrown!\n");
724 printf("Exiting...\n");