1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
57 using namespace comphelper
;
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
)
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
));
93 printf("------------------------------------------------------------------------------------------\n");
95 printf(": FAILED to get a ResultSet \n");
98 void printXResultSet( Reference
<XResultSet
> &xRes
)
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
)
107 const char *str
= OUtoCStr(xRow
->getString(j
));
108 printf( aPat_Short
, str
);
115 printf(": FAILED to get a ResultSet \n");
119 void printXResultSets( Reference
<XResultSet
> &xRes
)
124 while( xRes
.is() && xRes
->next())
126 printXResultSet(xRes
);
129 printf( "%d Row(s)\n", nRows
);
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
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
161 , SVLIBRARY( "dtransX11" ) // OBR
164 , SAL_MODULENAME( "sysdtrans" )
165 , SAL_MODULENAME( "ftransl" )
166 , SAL_MODULENAME( "dnd" )
171 Reference
< XMultiServiceFactory
> InitializeFac( void )
174 if( osl_Process_E_None
!= osl_getExecutableFile( (rtl_uString
**)&path
) )
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(
207 "com.sun.star.registry.ImplementationRegistration" ) ), UNO_QUERY
);
209 Reference
< XSimpleRegistry
> xReg(
210 interimSmgr
->createInstance(
212 "com.sun.star.registry.SimpleRegistry" ) ), UNO_QUERY
);
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
);
229 printf("Cannot open Registry. Terminating Program\n");
234 Reference
< XComponent
> xComp( interimSmgr
, UNO_QUERY
);
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
);
268 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
270 // Test some metadata
271 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
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
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");
313 printf(": FAILED to get DatabaseMetaData \n");
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
)
324 printf("Testing createStatement() & executeQuery() : START\n");
325 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
326 Reference
<XResultSet
> xRes
;
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\"";
335 printf("Please input a query,end by \";\" and less then 256 char plz:\n");
344 printf("SQL:%s\n",sql
);
349 for (times
= 0;times
< 100;times
++)
351 Reference
<XResultSet
> tmpRes
=
352 xStmt
->executeQuery(OUString::createFromAscii(sql
));
355 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
357 printf("Current Times:%d\n",times
);
361 } catch ( Exception
&e
) {
362 printf( "Exception caught : %s\n", OUtoCStr( e
.Message
) );
364 printf( "Non-UNO Exception caught\n" );
366 printf(": excuteQuery() : END \n");
370 printf(": FAILED to get statement\n");
372 printf("Testing createStatement() & executeQuery() : END\n");
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");
381 Sequence
<PropertyValue
> aValue
;
387 url
=OUString("sdbc:address:mozilla://");
390 url
=OUString("sdbc:address:ldap://");
391 char hostname
[40],basedn
[40];
392 scanf("%s %s",hostname
,basedn
);
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
);
404 url
=OUString("sdbc:address:ldap://");
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");
416 pDriver
->connect(url
,aValue
);
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
)
442 while( xRes
.is() && xRes
->next())
446 printf( "%d Row(s)\n", nRows
);
449 for(times
= 1;times
< 100; times
++)
451 pos
= rand() % nRows
+1;
452 printf("pos:%d\n",pos
);
454 printXResultSet(xRes
);
457 printf(": FAILED to get a ResultSet \n");
461 int TestBookMark(Reference
<XResultSet
> &xRes
)
463 Reference
<XResultSet
> mRes(xRes
);
464 if (!mRes
.is() || !mRes
->first())
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];
483 printf("Your choice is:%d\n",choice
);
487 printf("Rowcount:Current don't know\n");
490 if (!mRes
->isFirst()) mRes
->first();
493 xBookMark
[0] = xRowLocate
->getBookmark();
496 xBookMark
[1] = xRowLocate
->getBookmark();
500 if (index
== 1 || index
== 2)
501 xRowLocate
->moveToBookmark(xBookMark
[index
-1]);
505 scanf("%d %d",&index
,&rows
);
506 if (index
== 1 || index
== 2)
507 xRowLocate
->moveRelativeToBookmark(xBookMark
[index
-1],rows
);
510 printf("compareBookmarks:%d\n",xRowLocate
->compareBookmarks(xBookMark
[0],xBookMark
[1]));
514 printf("HashBookmark[%d]:%d\n",index
,xRowLocate
->hashBookmark(xBookMark
[index
-1]));
517 printXResultSet(mRes
);
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())
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
);
559 printf("Your choice is:%d\n",choice
);
567 xRes
->absolute(index
);
570 printXResultSet(xRes
);
573 scanf("%d %s",&index
,newString
);
574 xRowUpdate
->updateString(index
,OUString::createFromAscii(newString
));
575 printXResultSet(xRes
);
578 if (xResultSetUpdate
.is())
580 xResultSetUpdate
->updateRow();
581 printXResultSet(xRes
);
584 printf("Can't update!\n");
587 if (xResultSetUpdate
.is())
589 xResultSetUpdate
->deleteRow();
590 printXResultSet(xRes
);
593 printf("Can't update!\n");
601 printf("Test XRowUpdate END\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:")
615 int main( int argc
, char * argv
[] )
617 int _cdecl
main( int argc
, char * argv
[] )
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")),
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
);
651 }else printf("Can't connected!\n");
663 printf("No driver!\n");
668 printf("Exception thrown!\n");
670 printf("Exiting...\n");
674 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */