bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / firebird / Table.cxx
blobf88b371d5705c3423d9e912ae73ea9dac211532a
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 "Columns.hxx"
11 #include "Indexes.hxx"
12 #include "Keys.hxx"
13 #include "Table.hxx"
15 #include <TConnection.hxx>
17 #include <sal/log.hxx>
18 #include <comphelper/sequence.hxx>
19 #include <connectivity/dbtools.hxx>
21 #include <com/sun/star/sdbc/ColumnValue.hpp>
22 #include <com/sun/star/sdbcx/Privilege.hpp>
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 using namespace ::connectivity;
26 using namespace ::connectivity::firebird;
27 using namespace ::connectivity::sdbcx;
29 using namespace ::osl;
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::beans;
33 using namespace ::com::sun::star::container;
34 using namespace ::com::sun::star::sdbc;
35 using namespace ::com::sun::star::sdbcx;
36 using namespace ::com::sun::star::uno;
38 Table::Table(Tables* pTables,
39 Mutex& rMutex,
40 const uno::Reference< XConnection >& rConnection):
41 OTableHelper(pTables,
42 rConnection,
43 true),
44 m_rMutex(rMutex),
45 m_nPrivileges(0)
47 construct();
50 Table::Table(Tables* pTables,
51 Mutex& rMutex,
52 const uno::Reference< XConnection >& rConnection,
53 const OUString& rName,
54 const OUString& rType,
55 const OUString& rDescription):
56 OTableHelper(pTables,
57 rConnection,
58 true,
59 rName,
60 rType,
61 rDescription,
62 "",
63 ""),
64 m_rMutex(rMutex),
65 m_nPrivileges(0)
67 construct();
70 void Table::construct()
72 OTableHelper::construct();
73 if (isNew())
74 return;
76 // TODO: get privileges when in non-embedded mode.
77 m_nPrivileges = Privilege::DROP |
78 Privilege::REFERENCE |
79 Privilege::ALTER |
80 Privilege::CREATE |
81 Privilege::READ |
82 Privilege::DELETE |
83 Privilege::UPDATE |
84 Privilege::INSERT |
85 Privilege::SELECT;
86 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRIVILEGES),
87 PROPERTY_ID_PRIVILEGES,
88 PropertyAttribute::READONLY,
89 &m_nPrivileges,
90 cppu::UnoType<decltype(m_nPrivileges)>::get());
92 //----- OTableHelper ---------------------------------------------------------
93 OCollection* Table::createColumns(const ::std::vector< OUString>& rNames)
95 return new Columns(*this,
96 m_rMutex,
97 rNames);
100 OCollection* Table::createKeys(const ::std::vector< OUString>& rNames)
102 return new Keys(this,
103 m_rMutex,
104 rNames);
107 OCollection* Table::createIndexes(const ::std::vector< OUString>& rNames)
109 return new Indexes(this,
110 m_rMutex,
111 rNames);
114 //----- XAlterTable -----------------------------------------------------------
115 void SAL_CALL Table::alterColumnByName(const OUString& rColName,
116 const uno::Reference< XPropertySet >& rDescriptor)
118 MutexGuard aGuard(m_rMutex);
119 checkDisposed(WeakComponentImplHelperBase::rBHelper.bDisposed);
121 uno::Reference< XPropertySet > xColumn(m_xColumns->getByName(rColName), UNO_QUERY);
123 // sdbcx::Descriptor
124 const bool bNameChanged = xColumn->getPropertyValue("Name") != rDescriptor->getPropertyValue("Name");
125 // sdbcx::ColumnDescriptor
126 const bool bTypeChanged = xColumn->getPropertyValue("Type") != rDescriptor->getPropertyValue("Type");
127 const bool bTypeNameChanged = xColumn->getPropertyValue("TypeName") != rDescriptor->getPropertyValue("TypeName");
128 const bool bPrecisionChanged = xColumn->getPropertyValue("Precision") != rDescriptor->getPropertyValue("Precision");
129 const bool bScaleChanged = xColumn->getPropertyValue("Scale") != rDescriptor->getPropertyValue("Scale");
130 const bool bIsNullableChanged = xColumn->getPropertyValue("IsNullable") != rDescriptor->getPropertyValue("IsNullable");
131 const bool bIsAutoIncrementChanged = xColumn->getPropertyValue("IsAutoIncrement") != rDescriptor->getPropertyValue("IsAutoIncrement");
133 // TODO: remainder -- these are all "optional" so have to detect presence and change.
135 bool bDefaultChanged = xColumn->getPropertyValue("DefaultValue")
136 != rDescriptor->getPropertyValue("DefaultValue");
138 if (bTypeChanged || bTypeNameChanged || bPrecisionChanged || bScaleChanged)
140 // If bPrecisionChanged this will only succeed if we have increased the
141 // precision, otherwise an exception is thrown -- however the base
142 // gui then offers to delete and recreate the column.
143 OUString sSql(getAlterTableColumn(rColName) + "TYPE " +
144 ::dbtools::createStandardTypePart(rDescriptor, getConnection()));
145 getConnection()->createStatement()->execute(sSql);
146 // TODO: could cause errors e.g. if incompatible types, deal with them here as appropriate.
147 // possibly we have to wrap things in Util::evaluateStatusVector.
150 if (bIsNullableChanged)
152 sal_Int32 nNullable = 0;
153 rDescriptor->getPropertyValue("IsNullable") >>= nNullable;
155 if (nNullable != ColumnValue::NULLABLE_UNKNOWN)
158 OUString sSql;
159 // Dirty hack: can't change null directly in sql, we have to fiddle
160 // the system tables manually.
161 if (nNullable == ColumnValue::NULLABLE)
163 sSql = "UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = NULL "
164 "WHERE RDB$FIELD_NAME = '" + rColName + "' "
165 "AND RDB$RELATION_NAME = '" + getName() + "'";
167 else if (nNullable == ColumnValue::NO_NULLS)
169 // And if we are making NOT NULL then we have to make sure we have
170 // no nulls left in the column.
171 OUString sFillNulls("UPDATE \"" + getName() + "\" SET \""
172 + rColName + "\" = 0 "
173 "WHERE \"" + rColName + "\" IS NULL");
174 getConnection()->createStatement()->execute(sFillNulls);
176 sSql = "UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = 1 "
177 "WHERE RDB$FIELD_NAME = '" + rColName + "' "
178 "AND RDB$RELATION_NAME = '" + getName() + "'";
180 getConnection()->createStatement()->execute(sSql);
182 else
184 SAL_WARN("connectivity.firebird", "Attempting to set Nullable to NULLABLE_UNKNOWN");
188 if (bIsAutoIncrementChanged)
190 ::dbtools::throwSQLException(
191 "Changing autoincrement property of existing column is not supported",
192 ::dbtools::StandardSQLState::FUNCTION_NOT_SUPPORTED,
193 *this);
197 if (bDefaultChanged)
199 OUString sNewDefault;
200 rDescriptor->getPropertyValue("DefaultValue") >>= sNewDefault;
202 OUString sSql;
203 if (sNewDefault.isEmpty())
204 sSql = getAlterTableColumn(rColName) + "DROP DEFAULT";
205 else
206 sSql = getAlterTableColumn(rColName) + "SET DEFAULT " + sNewDefault;
208 getConnection()->createStatement()->execute(sSql);
210 // TODO: quote identifiers as needed.
211 if (bNameChanged)
213 OUString sNewColName;
214 rDescriptor->getPropertyValue("Name") >>= sNewColName;
215 OUString sSql(getAlterTableColumn(rColName)
216 + " TO \"" + sNewColName + "\"");
218 getConnection()->createStatement()->execute(sSql);
222 m_xColumns->refresh();
225 // ----- XRename --------------------------------------------------------------
226 void SAL_CALL Table::rename(const OUString&)
228 throw RuntimeException("Table renaming not supported by Firebird.");
231 // ----- XInterface -----------------------------------------------------------
232 Any SAL_CALL Table::queryInterface(const Type& rType)
234 if (rType.getTypeName() == "com.sun.star.sdbcx.XRename")
235 return Any();
237 return OTableHelper::queryInterface(rType);
240 // ----- XTypeProvider --------------------------------------------------------
241 uno::Sequence< Type > SAL_CALL Table::getTypes()
243 uno::Sequence< Type > aTypes = OTableHelper::getTypes();
245 for (int i = 0; i < aTypes.getLength(); i++)
247 if (aTypes[i].getTypeName() == "com.sun.star.sdbcx.XRename")
249 ::comphelper::removeElementAt(aTypes, i);
250 break;
254 return OTableHelper::getTypes();
257 OUString Table::getAlterTableColumn(std::u16string_view rColumn)
259 return ("ALTER TABLE \"" + getName() + "\" ALTER COLUMN \"" + rColumn + "\" ");
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */