1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <string_view>
#include <test/unoapi_test.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
#include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
#include <com/sun/star/sdbc/XConnection.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>
#include <com/sun/star/sdbc/XStatement.hpp>
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::uno;
class DBTestBase
: public UnoApiTest
{
public:
DBTestBase() : UnoApiTest(u"dbaccess/qa/unit/data"_ustr) {};
uno::Reference<XOfficeDatabaseDocument> getDocumentForUrl(OUString const & url);
uno::Reference< XConnection >
getConnectionForDocument(
uno::Reference< XOfficeDatabaseDocument > const & xDocument);
void createDBDocument(const OUString& rDriverURL);
void createTables(Reference< XConnection > xConnection);
void createQueries(Reference< XDataSource > xDataSource);
void createQuery(OUString sQuery, bool bEscapeProcessing,
OUString sQueryName, Reference<XDataSource> xDataSource);
};
uno::Reference<XOfficeDatabaseDocument> DBTestBase::getDocumentForUrl(OUString const & url) {
mxComponent = loadFromDesktop(url);
uno::Reference< XOfficeDatabaseDocument > xDocument(mxComponent, UNO_QUERY_THROW);
return xDocument;
}
uno::Reference< XConnection > DBTestBase::getConnectionForDocument(
uno::Reference< XOfficeDatabaseDocument > const & xDocument)
{
uno::Reference< XDataSource > xDataSource = xDocument->getDataSource();
CPPUNIT_ASSERT(xDataSource.is());
uno::Reference< XConnection > xConnection = xDataSource->getConnection(u""_ustr,u""_ustr);
CPPUNIT_ASSERT(xConnection.is());
return xConnection;
}
void DBTestBase::createDBDocument(const OUString& rDriverURL)
{
uno::Reference< XOfficeDatabaseDocument > xDocument(
m_xSFactory->createInstance(u"com.sun.star.sdb.OfficeDatabaseDocument"_ustr),
UNO_QUERY_THROW);
uno::Reference< com::sun::star::frame::XStorable > xStorable(xDocument, UNO_QUERY_THROW);
uno::Reference< XDataSource > xDataSource = xDocument->getDataSource();
uno::Reference< XPropertySet > xPropertySet(xDataSource, UNO_QUERY_THROW);
xPropertySet->setPropertyValue(u"URL"_ustr, Any(rDriverURL));
xStorable->storeAsURL(maTempFile.GetURL(), uno::Sequence< beans::PropertyValue >());
mxComponent = loadFromDesktop(maTempFile.GetURL());
CPPUNIT_ASSERT(mxComponent);
}
void DBTestBase::createTables(Reference<XConnection> xConnection)
{
uno::Reference<XStatement> xStatement = xConnection->createStatement();
xStatement->execute(
u"CREATE TABLE \"CATEGORIES\" ( \"ID\" INTEGER NOT NULL PRIMARY KEY "
", \"NAME\" VARCHAR (50) , \"DESCRIPTION\" VARCHAR (1024), \"IMAGE\" LONGVARBINARY"
")"_ustr);
xStatement->execute(u"INSERT INTO \"CATEGORIES\" (\"ID\", \"NAME\") VALUES ( 1, 'Food' )"_ustr);
xStatement->execute(u"INSERT INTO \"CATEGORIES\" (\"ID\", \"NAME\") VALUES ( 2, 'Furniture' )"_ustr);
xStatement->execute(
u"CREATE TABLE \"PRODUCTS\" ( \"ID\" INTEGER NOT NULL PRIMARY KEY "
", \"NAME\" VARCHAR (50) , \"CATEGORYID\" INTEGER NOT NULL, FOREIGN KEY (\"CATEGORYID\")"
"REFERENCES \"CATEGORIES\" (\"ID\") )"_ustr);
xStatement->execute(u"INSERT INTO \"PRODUCTS\" VALUES ( 1, 'Orange', 1 )"_ustr);
xStatement->execute(u"INSERT INTO \"PRODUCTS\" VALUES ( 2, 'Apples', 1 )"_ustr);
xStatement->execute(u"INSERT INTO \"PRODUCTS\" VALUES ( 3, 'Pears', 1 )"_ustr);
xStatement->execute(u"INSERT INTO \"PRODUCTS\" VALUES ( 4, 'Strawberries', 1 )"_ustr);
xStatement->execute(
u"CREATE TABLE \"CUSTOMERS\" ( \"ID\" INTEGER NOT NULL PRIMARY KEY "
", \"NAME\" VARCHAR (50) , \"ADDRESS\" VARCHAR (50), \"CITY\" VARCHAR (50), \"POSTAL\" VARCHAR (50)"
", \"COMMENT\" LONGVARCHAR )"_ustr);
xStatement->execute(u"INSERT INTO \"CUSTOMERS\" VALUES(1,'Food, Inc.','Down Under','Melbourne','509','Preferred')"_ustr);
xStatement->execute(u"INSERT INTO \"CUSTOMERS\" VALUES(2,'Simply Delicious','Down Under','Melbourne','518',null)"_ustr);
xStatement->execute(u"INSERT INTO \"CUSTOMERS\" VALUES(3,'Pure Health','10 Fish St.','San Francisco','94107',null)"_ustr);
xStatement->execute(u"INSERT INTO \"CUSTOMERS\" VALUES(4,'Milk And More','Arlington Road 21','Dublin','31021','Good one.')"_ustr);
xStatement->execute(
u"CREATE TABLE \"ORDERS\" ( \"ID\" INTEGER NOT NULL PRIMARY KEY, \"CUSTOMERID\" INTEGER NOT NULL "
", \"ORDERDATE\" DATE, \"SHIPDATE\" DATE, FOREIGN KEY (\"CUSTOMERID\")"
"REFERENCES \"CUSTOMERS\" (\"ID\") )"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS\" (\"ID\", \"CUSTOMERID\", \"ORDERDATE\") VALUES(1, 1, {D '2009-01-01'})"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS\" VALUES(2, 2, {D '2009-01-01'}, {D '2009-01-23'})"_ustr);
xStatement->execute(
u"CREATE TABLE \"ORDERS_DETAILS\" ( \"ORDERID\" INTEGER NOT NULL, \"PRODUCTID\" INTEGER NOT NULL, \"QUANTITY\" INTEGER"
", FOREIGN KEY (\"ORDERID\") REFERENCES \"ORDERS\" (\"ID\") "
", FOREIGN KEY (\"PRODUCTID\") REFERENCES \"PRODUCTS\" (\"ID\"), PRIMARY KEY(\"ORDERID\", \"PRODUCTID\") )"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS_DETAILS\" VALUES(1, 1, 100)"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS_DETAILS\" VALUES(1, 2, 100)"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS_DETAILS\" VALUES(2, 2, 2000)"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS_DETAILS\" VALUES(2, 3, 2000)"_ustr);
xStatement->execute(u"INSERT INTO \"ORDERS_DETAILS\" VALUES(2, 4, 2000)"_ustr);
xConnection->commit();
}
void DBTestBase::createQueries(Reference<XDataSource> xDataSource)
{
createQuery(
u"SELECT \"ORDERS\".\"ID\" AS \"Order No.\", "
"\"CUSTOMERS\".\"NAME\" AS \"Customer Name\", "
"\"ORDERS\".\"ORDERDATE\" AS \"Order Date\", "
"\"ORDERS\".\"SHIPDATE\" AS \"Ship Date\", "
"\"ORDERS_DETAILS\".\"QUANTITY\", "
"\"PRODUCTS\".\"NAME\" AS \"Product Name\" "
"FROM \"ORDERS_DETAILS\" AS \"ORDERS_DETAILS\", "
"\"ORDERS\" AS \"ORDERS\", "
"\"PRODUCTS\" AS \"PRODUCTS\", "
"\"CUSTOMERS\" AS \"CUSTOMERS\" "
"WHERE ( \"ORDERS_DETAILS\".\"ORDERID\" = \"ORDERS\".\"ID\" "
"AND \"ORDERS_DETAILS\".\"PRODUCTID\" = \"PRODUCTS\".\"ID\" "
"AND \"ORDERS\".\"CUSTOMERID\" = \"CUSTOMERS\".\"ID\" )"_ustr,
true, u"all orders"_ustr, xDataSource);
createQuery(
u"SELECT *"
"FROM \"all orders\""
"WHERE ( \"SHIPDATE\" IS NULL )"_ustr, true, u"unshipped orders"_ustr, xDataSource);
createQuery(u"SELECT * FROM \"CUSTOMERS\""_ustr, true, u"parseable"_ustr, xDataSource);
createQuery(u"SELECT * FROM INFORMATION_SCHEMA.SYSTEM_VIEWS"_ustr, false, u"parseable native"_ustr, xDataSource);
}
void DBTestBase::createQuery(OUString sQuery, bool bEscapeProcessing, OUString sQueryName, Reference<XDataSource> xDataSource)
{
Reference<XQueryDefinitionsSupplier> xQuerySupplier(xDataSource, UNO_QUERY_THROW);
Reference<container::XNameAccess> xQueryAccess = xQuerySupplier->getQueryDefinitions();
CPPUNIT_ASSERT(xQueryAccess.is());
Reference<lang::XSingleServiceFactory> xSingleServiceFactory(xQueryAccess, UNO_QUERY_THROW);
Reference<beans::XPropertySet> xQueryProp(xSingleServiceFactory->createInstance(), UNO_QUERY_THROW);
xQueryProp->setPropertyValue(u"Command"_ustr, Any(sQuery));
xQueryProp->setPropertyValue(u"EscapeProcessing"_ustr, Any(bEscapeProcessing));
Reference<container::XNameContainer> xNameContainer(xQueryAccess, UNO_QUERY_THROW);
xNameContainer->insertByName(sQueryName, Any(xQueryProp));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|