Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / firebird / Tables.cxx
blob8b69044bef79baa4224e8174dcc98048e38584ee
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.
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 */
10 #include "Table.hxx"
11 #include "Tables.hxx"
12 #include "Views.hxx"
13 #include "Catalog.hxx"
15 #include <TConnection.hxx>
17 #include <connectivity/dbtools.hxx>
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>
24 using namespace ::connectivity;
25 using namespace ::connectivity::firebird;
26 using namespace ::connectivity::sdbcx;
27 using namespace ::cppu;
28 using namespace ::osl;
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::beans;
32 using namespace ::com::sun::star::container;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::sdbc;
35 using namespace ::com::sun::star::sdbcx;
36 using namespace ::com::sun::star::uno;
39 //----- OCollection -----------------------------------------------------------
40 void Tables::impl_refresh()
42 static_cast<Catalog&>(m_rParent).refreshTables();
45 ObjectType Tables::createObject(const OUString& rName)
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 >());
53 if (!xTables.is())
54 throw RuntimeException("Could not acquire table.");
56 uno::Reference< XRow > xRow(xTables,UNO_QUERY_THROW);
58 if (!xTables->next())
59 throw RuntimeException();
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
68 if (xTables->next())
69 throw RuntimeException("Found more tables than expected.");
71 return xRet;
74 OUString Tables::createStandardColumnPart(const Reference< XPropertySet >& xColProp,const Reference< XConnection>& _xConnection)
76 Reference<XDatabaseMetaData> xMetaData = _xConnection->getMetaData();
78 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
80 bool bIsAutoIncrement = false;
81 xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bIsAutoIncrement;
83 const OUString sQuoteString = xMetaData->getIdentifierQuoteString();
84 OUStringBuffer aSql(::dbtools::quoteName(sQuoteString,::comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
86 // check if the user enter a specific string to create autoincrement values
87 OUString sAutoIncrementValue;
88 Reference<XPropertySetInfo> xPropInfo = xColProp->getPropertySetInfo();
90 if ( xPropInfo.is() && xPropInfo->hasPropertyByName(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) )
91 xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION)) >>= sAutoIncrementValue;
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(
99 PROPERTY_ID_TYPE)) )
101 sal_Int32 aType = 0;
102 xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE))
103 >>= aType;
104 if(aType == DataType::BINARY || aType == DataType::VARBINARY)
106 aSql.append(" CHARACTER SET OCTETS");
110 if ( bIsAutoIncrement && !sAutoIncrementValue.isEmpty())
112 aSql.append(" " + sAutoIncrementValue);
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");
118 return aSql.makeStringAndClear();
121 uno::Reference< XPropertySet > Tables::createDescriptor()
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());
129 //----- XAppend ---------------------------------------------------------------
130 ObjectType Tables::appendObject(const OUString& rName,
131 const uno::Reference< XPropertySet >& rDescriptor)
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();
139 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
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;
145 sComposedName = ::dbtools::composeTableName(m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
146 if ( sComposedName.isEmpty() )
147 ::dbtools::throwFunctionSequenceException(xConnection);
149 aSqlBuffer.append(sComposedName
150 + " (");
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())
157 ::dbtools::throwFunctionSequenceException(xConnection);
159 Reference< XPropertySet > xColProp;
161 sal_Int32 nCount = xColumns->getCount();
162 for(sal_Int32 i=0;i<nCount;++i)
164 if ( (xColumns->getByIndex(i) >>= xColProp) && xColProp.is() )
166 aSqlBuffer.append(createStandardColumnPart(xColProp,xConnection)
167 + ",");
170 OUString sSql = aSqlBuffer.makeStringAndClear();
172 const OUString sKeyStmt = ::dbtools::createStandardKeyStatement(rDescriptor,xConnection);
173 if ( !sKeyStmt.isEmpty() )
174 sSql += sKeyStmt;
175 else
177 if ( sSql.endsWith(",") )
178 sSql = sSql.replaceAt(sSql.getLength()-1, 1, u")");
179 else
180 sSql += ")";
183 m_xMetaData->getConnection()->createStatement()->execute(sSql);
185 return createObject(rName);
188 //----- XDrop -----------------------------------------------------------------
189 void Tables::dropObject(sal_Int32 nPosition, const OUString& sName)
191 uno::Reference< XPropertySet > xTable(getObject(nPosition));
193 if (ODescriptor::isNew(xTable))
194 return;
196 OUString sType;
197 xTable->getPropertyValue("Type") >>= sType;
199 const OUString sQuoteString = m_xMetaData->getIdentifierQuoteString();
201 m_xMetaData->getConnection()->createStatement()->execute(
202 "DROP " + sType + " " + ::dbtools::quoteName(sQuoteString,sName));
204 if (sType == "VIEW")
206 Views* pViews = static_cast<Views*>(static_cast<Catalog&>(m_rParent).getPrivateViews());
207 if ( pViews && pViews->hasByName(sName) )
208 pViews->dropByNameImpl(sName);
212 void connectivity::firebird::Tables::appendNew(const OUString& _rsNewTable)
214 insertElement(_rsNewTable, nullptr);
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());
220 comphelper::OInterfaceIteratorHelper3 aListenerLoop(m_aContainerListeners);
221 while (aListenerLoop.hasMoreElements())
222 aListenerLoop.next()->elementInserted(aEvent);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */