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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/* -*- 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 "Columns.hxx"
#include "Indexes.hxx"
#include "Keys.hxx"
#include "Table.hxx"
#include <TConnection.hxx>
#include <sal/log.hxx>
#include <comphelper/sequence.hxx>
#include <connectivity/dbtools.hxx>
#include <com/sun/star/sdbc/ColumnValue.hpp>
#include <com/sun/star/sdbcx/Privilege.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
using namespace ::connectivity;
using namespace ::connectivity::firebird;
using namespace ::connectivity::sdbcx;
using namespace ::osl;
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::uno;
Table::Table(Tables* pTables,
Mutex& rMutex,
const uno::Reference< XConnection >& rConnection):
OTableHelper(pTables,
rConnection,
true),
m_rMutex(rMutex),
m_nPrivileges(0)
{
construct();
}
Table::Table(Tables* pTables,
Mutex& rMutex,
const uno::Reference< XConnection >& rConnection,
const OUString& rName,
const OUString& rType,
const OUString& rDescription):
OTableHelper(pTables,
rConnection,
true,
rName,
rType,
rDescription,
"",
""),
m_rMutex(rMutex),
m_nPrivileges(0)
{
construct();
}
void Table::construct()
{
OTableHelper::construct();
if (isNew())
return;
// TODO: get privileges when in non-embedded mode.
m_nPrivileges = Privilege::DROP |
Privilege::REFERENCE |
Privilege::ALTER |
Privilege::CREATE |
Privilege::READ |
Privilege::DELETE |
Privilege::UPDATE |
Privilege::INSERT |
Privilege::SELECT;
registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRIVILEGES),
PROPERTY_ID_PRIVILEGES,
PropertyAttribute::READONLY,
&m_nPrivileges,
cppu::UnoType<decltype(m_nPrivileges)>::get());
}
//----- OTableHelper ---------------------------------------------------------
OCollection* Table::createColumns(const ::std::vector< OUString>& rNames)
{
return new Columns(*this,
m_rMutex,
rNames);
}
OCollection* Table::createKeys(const ::std::vector< OUString>& rNames)
{
return new Keys(this,
m_rMutex,
rNames);
}
OCollection* Table::createIndexes(const ::std::vector< OUString>& rNames)
{
return new Indexes(this,
m_rMutex,
rNames);
}
//----- XAlterTable -----------------------------------------------------------
void SAL_CALL Table::alterColumnByName(const OUString& rColName,
const uno::Reference< XPropertySet >& rDescriptor)
{
MutexGuard aGuard(m_rMutex);
checkDisposed(WeakComponentImplHelperBase::rBHelper.bDisposed);
uno::Reference< XPropertySet > xColumn(m_xColumns->getByName(rColName), UNO_QUERY);
// sdbcx::Descriptor
const bool bNameChanged = xColumn->getPropertyValue("Name") != rDescriptor->getPropertyValue("Name");
// sdbcx::ColumnDescriptor
const bool bTypeChanged = xColumn->getPropertyValue("Type") != rDescriptor->getPropertyValue("Type");
const bool bTypeNameChanged = xColumn->getPropertyValue("TypeName") != rDescriptor->getPropertyValue("TypeName");
const bool bPrecisionChanged = xColumn->getPropertyValue("Precision") != rDescriptor->getPropertyValue("Precision");
const bool bScaleChanged = xColumn->getPropertyValue("Scale") != rDescriptor->getPropertyValue("Scale");
const bool bIsNullableChanged = xColumn->getPropertyValue("IsNullable") != rDescriptor->getPropertyValue("IsNullable");
const bool bIsAutoIncrementChanged = xColumn->getPropertyValue("IsAutoIncrement") != rDescriptor->getPropertyValue("IsAutoIncrement");
// TODO: remainder -- these are all "optional" so have to detect presence and change.
bool bDefaultChanged = xColumn->getPropertyValue("DefaultValue")
!= rDescriptor->getPropertyValue("DefaultValue");
if (bTypeChanged || bTypeNameChanged || bPrecisionChanged || bScaleChanged)
{
// If bPrecisionChanged this will only succeed if we have increased the
// precision, otherwise an exception is thrown -- however the base
// gui then offers to delete and recreate the column.
OUString sSql(getAlterTableColumn(rColName) + "TYPE " +
::dbtools::createStandardTypePart(rDescriptor, getConnection()));
getConnection()->createStatement()->execute(sSql);
// TODO: could cause errors e.g. if incompatible types, deal with them here as appropriate.
// possibly we have to wrap things in Util::evaluateStatusVector.
}
if (bIsNullableChanged)
{
sal_Int32 nNullable = 0;
rDescriptor->getPropertyValue("IsNullable") >>= nNullable;
if (nNullable != ColumnValue::NULLABLE_UNKNOWN)
{
OUString sSql;
// Dirty hack: can't change null directly in sql, we have to fiddle
// the system tables manually.
if (nNullable == ColumnValue::NULLABLE)
{
sSql = "UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = NULL "
"WHERE RDB$FIELD_NAME = '" + rColName + "' "
"AND RDB$RELATION_NAME = '" + getName() + "'";
}
else if (nNullable == ColumnValue::NO_NULLS)
{
// And if we are making NOT NULL then we have to make sure we have
// no nulls left in the column.
OUString sFillNulls("UPDATE \"" + getName() + "\" SET \""
+ rColName + "\" = 0 "
"WHERE \"" + rColName + "\" IS NULL");
getConnection()->createStatement()->execute(sFillNulls);
sSql = "UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = 1 "
"WHERE RDB$FIELD_NAME = '" + rColName + "' "
"AND RDB$RELATION_NAME = '" + getName() + "'";
}
getConnection()->createStatement()->execute(sSql);
}
else
{
SAL_WARN("connectivity.firebird", "Attempting to set Nullable to NULLABLE_UNKNOWN");
}
}
if (bIsAutoIncrementChanged)
{
::dbtools::throwSQLException(
"Changing autoincrement property of existing column is not supported",
::dbtools::StandardSQLState::FUNCTION_NOT_SUPPORTED,
*this);
}
if (bDefaultChanged)
{
OUString sNewDefault;
rDescriptor->getPropertyValue("DefaultValue") >>= sNewDefault;
OUString sSql;
if (sNewDefault.isEmpty())
sSql = getAlterTableColumn(rColName) + "DROP DEFAULT";
else
sSql = getAlterTableColumn(rColName) + "SET DEFAULT " + sNewDefault;
getConnection()->createStatement()->execute(sSql);
}
// TODO: quote identifiers as needed.
if (bNameChanged)
{
OUString sNewColName;
rDescriptor->getPropertyValue("Name") >>= sNewColName;
OUString sSql(getAlterTableColumn(rColName)
+ " TO \"" + sNewColName + "\"");
getConnection()->createStatement()->execute(sSql);
}
m_xColumns->refresh();
}
// ----- XRename --------------------------------------------------------------
void SAL_CALL Table::rename(const OUString&)
{
throw RuntimeException("Table renaming not supported by Firebird.");
}
// ----- XInterface -----------------------------------------------------------
Any SAL_CALL Table::queryInterface(const Type& rType)
{
if (rType.getTypeName() == "com.sun.star.sdbcx.XRename")
return Any();
return OTableHelper::queryInterface(rType);
}
// ----- XTypeProvider --------------------------------------------------------
uno::Sequence< Type > SAL_CALL Table::getTypes()
{
uno::Sequence< Type > aTypes = OTableHelper::getTypes();
for (int i = 0; i < aTypes.getLength(); i++)
{
if (aTypes[i].getTypeName() == "com.sun.star.sdbcx.XRename")
{
::comphelper::removeElementAt(aTypes, i);
break;
}
}
return OTableHelper::getTypes();
}
OUString Table::getAlterTableColumn(const OUString& rColumn)
{
return ("ALTER TABLE \"" + getName() + "\" ALTER COLUMN \"" + rColumn + "\" ");
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|