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
;
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
)
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
));
89 printf("------------------------------------------------------------------------------------------\n");
91 printf(": FAILED to get a ResultSet \n");
94 void printXResultSet( Reference
<XResultSet
> &xRes
)
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
)
103 const char *str
= OUtoCStr(xRow
->getString(j
));
104 printf( aPat_Short
, str
);
111 printf(": FAILED to get a ResultSet \n");
115 void printXResultSets( Reference
<XResultSet
> &xRes
)
120 while( xRes
.is() && xRes
->next())
122 printXResultSet(xRes
);
125 printf( "%d Row(s)\n", nRows
);
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
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
157 , SVLIBRARY( "dtransX11" ) // OBR
160 , SAL_MODULENAME( "sysdtrans" )
161 , SAL_MODULENAME( "ftransl" )
162 , SAL_MODULENAME( "dnd" )
167 Reference
< XMultiServiceFactory
> InitializeFac( void )
170 if( osl_Process_E_None
!= osl_getExecutableFile( (rtl_uString
**)&path
) )
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(
203 "com.sun.star.registry.ImplementationRegistration" ) ), UNO_QUERY
);
205 Reference
< XSimpleRegistry
> xReg(
206 interimSmgr
->createInstance(
208 "com.sun.star.registry.SimpleRegistry" ) ), UNO_QUERY
);
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
);
225 printf("Cannot open Registry. Terminating Program\n");
230 Reference
< XComponent
> xComp( interimSmgr
, UNO_QUERY
);
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
);
264 int TestMetaData(Reference
< ::com::sun::star::sdbc::XConnection
> &pConnection
)
266 // Test some metadata
267 Reference
< XDatabaseMetaData
> xDmd
= pConnection
->getMetaData();
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
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
304 printXResultSets( xRes
);
306 printf("Testing getTables() : END\n");
309 printf(": FAILED to get DatabaseMetaData \n");
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
)
320 printf("Testing createStatement() & executeQuery() : START\n");
321 Reference
<XStatement
> xStmt
= pConnection
->createStatement();
322 Reference
<XResultSet
> xRes
;
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\"";
331 printf("Please input a query,end by \";\" and less then 256 char plz:\n");
340 printf("SQL:%s\n",sql
);
345 for (times
= 0;times
< 100;times
++)
347 Reference
<XResultSet
> tmpRes
=
348 xStmt
->executeQuery(OUString::createFromAscii(sql
));
351 Reference
<XCloseable
> clsRes(tmpRes
,UNO_QUERY
);
353 printf("Current Times:%d\n",times
);
357 } catch ( Exception
&e
) {
358 printf( "Exception caught : %s\n", OUtoCStr( e
.Message
) );
360 printf( "Non-UNO Exception caught\n" );
362 printf(": excuteQuery() : END \n");
366 printf(": FAILED to get statement\n");
368 printf("Testing createStatement() & executeQuery() : END\n");
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");
377 Sequence
<PropertyValue
> aValue
;
383 url
=OUString("sdbc:address:mozilla://");
386 url
=OUString("sdbc:address:ldap://");
387 char hostname
[40],basedn
[40];
388 scanf("%s %s",hostname
,basedn
);
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
);
400 url
=OUString("sdbc:address:ldap://");
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");
411 pDriver
->connect(url
,aValue
);
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
)
437 while( xRes
.is() && xRes
->next())
441 printf( "%d Row(s)\n", nRows
);
444 for(times
= 1;times
< 100; times
++)
446 pos
= rand() % nRows
+1;
447 printf("pos:%d\n",pos
);
449 printXResultSet(xRes
);
452 printf(": FAILED to get a ResultSet \n");
456 int TestBookMark(Reference
<XResultSet
> &xRes
)
458 Reference
<XResultSet
> mRes(xRes
);
459 if (!mRes
.is() || !mRes
->first())
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];
478 printf("Your choice is:%d\n",choice
);
482 printf("Rowcount:Current don't know\n");
485 if (!mRes
->isFirst()) mRes
->first();
488 xBookMark
[0] = xRowLocate
->getBookmark();
491 xBookMark
[1] = xRowLocate
->getBookmark();
495 if (index
== 1 || index
== 2)
496 xRowLocate
->moveToBookmark(xBookMark
[index
-1]);
500 scanf("%d %d",&index
,&rows
);
501 if (index
== 1 || index
== 2)
502 xRowLocate
->moveRelativeToBookmark(xBookMark
[index
-1],rows
);
505 printf("compareBookmarks:%d\n",xRowLocate
->compareBookmarks(xBookMark
[0],xBookMark
[1]));
509 printf("HashBookmark[%d]:%d\n",index
,xRowLocate
->hashBookmark(xBookMark
[index
-1]));
512 printXResultSet(mRes
);
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())
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
);
554 printf("Your choice is:%d\n",choice
);
562 xRes
->absolute(index
);
565 printXResultSet(xRes
);
568 scanf("%d %s",&index
,newString
);
569 xRowUpdate
->updateString(index
,OUString::createFromAscii(newString
));
570 printXResultSet(xRes
);
573 if (xResultSetUpdate
.is())
575 xResultSetUpdate
->updateRow();
576 printXResultSet(xRes
);
579 printf("Can't update!\n");
582 if (xResultSetUpdate
.is())
584 xResultSetUpdate
->deleteRow();
585 printXResultSet(xRes
);
588 printf("Can't update!\n");
596 printf("Test XRowUpdate END\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:")
610 int main( int argc
, char * argv
[] )
612 int _cdecl
main( int argc
, char * argv
[] )
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")),
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
);
646 }else printf("Can't connected!\n");
658 printf("No driver!\n");
663 printf("Exception thrown!\n");
665 printf("Exiting...\n");
669 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */