LibreOffice Module connectivity (master) 1
Tables.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 */
9
10#include "Table.hxx"
11#include "Tables.hxx"
12#include "Views.hxx"
13#include "Catalog.hxx"
14
15#include <TConnection.hxx>
16
18
19#include <com/sun/star/sdbc/XRow.hpp>
20#include <com/sun/star/sdbc/ColumnValue.hpp>
21#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
22#include <comphelper/types.hxx>
23
24using namespace ::connectivity;
25using namespace ::connectivity::firebird;
26using namespace ::connectivity::sdbcx;
27using namespace ::cppu;
28using namespace ::osl;
29
30using namespace ::com::sun::star;
31using namespace ::com::sun::star::beans;
32using namespace ::com::sun::star::container;
33using namespace ::com::sun::star::lang;
34using namespace ::com::sun::star::sdbc;
35using namespace ::com::sun::star::sdbcx;
36using namespace ::com::sun::star::uno;
37
38
39//----- OCollection -----------------------------------------------------------
40void Tables::impl_refresh()
41{
42 static_cast<Catalog&>(m_rParent).refreshTables();
43}
44
45ObjectType Tables::createObject(const OUString& rName)
46{
47 // Only retrieving a single table, so table type is irrelevant (param 4)
48 uno::Reference< XResultSet > xTables = m_xMetaData->getTables(Any(),
49 OUString(),
50 rName,
51 uno::Sequence< OUString >());
52
53 if (!xTables.is())
54 throw RuntimeException("Could not acquire table.");
55
56 uno::Reference< XRow > xRow(xTables,UNO_QUERY_THROW);
57
58 if (!xTables->next())
59 throw RuntimeException();
60
61 ObjectType xRet(new Table(this,
62 m_rMutex,
63 m_xMetaData->getConnection(),
64 xRow->getString(3), // Name
65 xRow->getString(4), // Type
66 xRow->getString(5))); // Description / Remarks / Comments
67
68 if (xTables->next())
69 throw RuntimeException("Found more tables than expected.");
70
71 return xRet;
72}
73
74OUString Tables::createStandardColumnPart(const Reference< XPropertySet >& xColProp,const Reference< XConnection>& _xConnection)
75{
76 Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
77
78 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
79
80 bool bIsAutoIncrement = false;
81 xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bIsAutoIncrement;
82
83 const OUString sQuoteString = xMetaData->getIdentifierQuoteString();
84 OUStringBuffer aSql(::dbtools::quoteName(sQuoteString,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
85
86 // check if the user enter a specific string to create autoincrement values
87 OUString sAutoIncrementValue;
88 Reference<XPropertySetInfo> xPropInfo = xColProp->getPropertySetInfo();
89
90 if ( xPropInfo.is() && xPropInfo->hasPropertyByName(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) )
91 xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) >>= sAutoIncrementValue;
92
93 aSql.append(" "
94 + dbtools::createStandardTypePart(xColProp, _xConnection));
95 // Add character set for (VAR)BINARY (fix) types:
96 // (VAR) BINARY is distinguished from other CHAR types by its character set.
97 // Octets is a special character set for binary data.
98 if ( xPropInfo.is() && xPropInfo->hasPropertyByName(rPropMap.getNameByIndex(
100 {
101 sal_Int32 aType = 0;
102 xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE))
103 >>= aType;
104 if(aType == DataType::BINARY || aType == DataType::VARBINARY)
105 {
106 aSql.append(" CHARACTER SET OCTETS");
107 }
108 }
109
110 if ( bIsAutoIncrement && !sAutoIncrementValue.isEmpty())
111 {
112 aSql.append(" " + sAutoIncrementValue);
113 }
114 // AutoIncrement "IDENTITY" is implicitly "NOT NULL"
115 else if(::comphelper::getINT32(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISNULLABLE))) == ColumnValue::NO_NULLS)
116 aSql.append(" NOT NULL");
117
118 return aSql.makeStringAndClear();
119}
120
121uno::Reference< XPropertySet > Tables::createDescriptor()
122{
123 // There is some internal magic so that the same class can be used as either
124 // a descriptor or as a normal table. See VTable.cxx for the details. In our
125 // case we just need to ensure we use the correct constructor.
126 return new Table(this, m_rMutex, m_xMetaData->getConnection());
127}
128
129//----- XAppend ---------------------------------------------------------------
130ObjectType Tables::appendObject(const OUString& rName,
131 const uno::Reference< XPropertySet >& rDescriptor)
132{
133 /* OUString sSql(::dbtools::createSqlCreateTableStatement(rDescriptor,
134 m_xMetaData->getConnection())); */
135 OUStringBuffer aSqlBuffer("CREATE TABLE ");
136 OUString sCatalog, sSchema, sComposedName, sTable;
137 const Reference< XConnection>& xConnection = m_xMetaData->getConnection();
138
139 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
140
141 rDescriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) >>= sCatalog;
142 rDescriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
143 rDescriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= sTable;
144
145 sComposedName = ::dbtools::composeTableName(m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
146 if ( sComposedName.isEmpty() )
148
149 aSqlBuffer.append(sComposedName
150 + " (");
151
152 // columns
153 Reference<XColumnsSupplier> xColumnSup(rDescriptor,UNO_QUERY);
154 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
155 // check if there are columns
156 if(!xColumns.is() || !xColumns->getCount())
158
159 Reference< XPropertySet > xColProp;
160
161 sal_Int32 nCount = xColumns->getCount();
162 for(sal_Int32 i=0;i<nCount;++i)
163 {
164 if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
165 {
166 aSqlBuffer.append(createStandardColumnPart(xColProp,xConnection)
167 + ",");
168 }
169 }
170 OUString sSql = aSqlBuffer.makeStringAndClear();
171
172 const OUString sKeyStmt = ::dbtools::createStandardKeyStatement(rDescriptor,xConnection);
173 if ( !sKeyStmt.isEmpty() )
174 sSql += sKeyStmt;
175 else
176 {
177 if ( sSql.endsWith(",") )
178 sSql = sSql.replaceAt(sSql.getLength()-1, 1, u")");
179 else
180 sSql += ")";
181 }
182
183 m_xMetaData->getConnection()->createStatement()->execute(sSql);
184
185 return createObject(rName);
186}
187
188//----- XDrop -----------------------------------------------------------------
189void Tables::dropObject(sal_Int32 nPosition, const OUString& sName)
190{
191 uno::Reference< XPropertySet > xTable(getObject(nPosition));
192
193 if (ODescriptor::isNew(xTable))
194 return;
195
196 OUString sType;
197 xTable->getPropertyValue("Type") >>= sType;
198
199 const OUString sQuoteString = m_xMetaData->getIdentifierQuoteString();
200
201 m_xMetaData->getConnection()->createStatement()->execute(
202 "DROP " + sType + " " + ::dbtools::quoteName(sQuoteString,sName));
203
204 if (sType == "VIEW")
205 {
206 Views* pViews = static_cast<Views*>(static_cast<Catalog&>(m_rParent).getPrivateViews());
207 if ( pViews && pViews->hasByName(sName) )
208 pViews->dropByNameImpl(sName);
209 }
210}
211
212void connectivity::firebird::Tables::appendNew(const OUString& _rsNewTable)
213{
214 insertElement(_rsNewTable, nullptr);
215
216 // notify our container listeners
217 css::container::ContainerEvent aEvent(static_cast<XContainer*>(this),
218 css::uno::Any(_rsNewTable), css::uno::Any(),
219 css::uno::Any());
221 while (aListenerLoop.hasMoreElements())
222 aListenerLoop.next()->elementInserted(aEvent);
223}
224
225
226/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
struct _ADOTable Table
Definition: Awrapadox.hxx:55
OptionalString sSchema
OptionalString sCatalog
OptionalString sComposedName
OptionalString sType
XSLTFilter & m_rParent
AnyEventRef aEvent
css::uno::Reference< ListenerT > const & next()
void appendNew(const OUString &_rsNewTable)
Definition: Tables.cxx:212
void insertElement(const OUString &_sElementName, const ObjectType &_xElement)
insert a new element into the collection
::comphelper::OInterfaceContainerHelper3< css::container::XContainerListener > m_aContainerListeners
Definition: VCollection.hxx:93
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
int nCount
css::uno::Reference< css::beans::XPropertySet > ObjectType
Definition: VCollection.hxx:59
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
void throwFunctionSequenceException(const Reference< XInterface > &Context, const Any &Next)
OUString createStandardTypePart(const Reference< XPropertySet > &xColProp, const Reference< XConnection > &_xConnection, std::u16string_view _sCreatePattern)
Definition: dbtools2.cxx:67
OUString createStandardColumnPart(const Reference< XPropertySet > &xColProp, const Reference< XConnection > &_xConnection, ISQLStatementHelper *_pHelper, std::u16string_view _sCreatePattern)
Definition: dbtools2.cxx:169
OUString createStandardKeyStatement(const Reference< XPropertySet > &descriptor, const Reference< XConnection > &_xConnection)
Definition: dbtools2.cxx:267
OUString quoteName(std::u16string_view _rQuote, const OUString &_rName)
quote the given name with the given quote string.
int i
ObjectType
#define PROPERTY_ID_NAME
Definition: propertyids.hxx:50
#define PROPERTY_ID_AUTOINCREMENTCREATION
Definition: propertyids.hxx:92
#define PROPERTY_ID_TYPE
Definition: propertyids.hxx:51
#define PROPERTY_ID_CATALOGNAME
Definition: propertyids.hxx:70
#define PROPERTY_ID_ISNULLABLE
Definition: propertyids.hxx:55
#define PROPERTY_ID_SCHEMANAME
Definition: propertyids.hxx:69
#define PROPERTY_ID_ISAUTOINCREMENT
Definition: propertyids.hxx:56