[go: up one dir, main page]

File: tooracletable.cpp

package info (click to toggle)
tora 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 27,560 kB
  • ctags: 21,885
  • sloc: cpp: 189,278; sh: 17,561; makefile: 692; python: 159; xml: 69; ansic: 56
file content (425 lines) | stat: -rw-r--r-- 15,089 bytes parent folder | download | duplicates (4)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <QDebug>
#include "tooracleextract.h"

// the same function as in tooracleextract.cpp should probably be moved to toextract.cpp?
static bool SameContext(const QString &str, const QString &str2)
{
    if (str.startsWith(str2))
    {
        if (str.length() == str2.length())
            return true;
        if (str.length() > str2.length() &&
                str.at(str2.length()) == QChar('\001'))
            return true;
    }
    return false;
} // SameContext

/***********************************************************************************
Purpose: Create new table
TODO: Maybe toExtract::parseColumnDescription should be used to extract columns?
************************************************************************************/
QString toOracleExtract::createTable(std::list<QString> &destin) const
{
    QString context; // grouping properties of one entity (for example one column)

    QString schema_name;
    QString table_name;
    QString tablespace;
    std::list<toExtract::columnInfo> table_columns;

    QString tmp;
    QString end_statements; // statements going after create table one (f.e. comments)
    bool first_column = true;

    // Part I - go through given rows and extract table information into local structures
    std::list<QString>::iterator i = destin.begin();

    while (i != destin.end())
    {
        tmp = toExtract::partDescribe(*i, 3);
        if (tmp.isNull())
        {
            // part 3 (starting from 0) is null only for table name description
            if (!table_name.isNull())
            {
                // if table_name already filled in - error in data or this code
                throw qApp->translate("toExtract", "Too many table definitions");
            }
            schema_name = toExtract::partDescribe(*i, 0);
            tmp         = toExtract::partDescribe(*i, 1);
            if (tmp != "TABLE")
            {
                throw qApp->translate("toExtract", "Unkown string %1").arg(*i);
            }
            table_name  = toExtract::partDescribe(*i, 2);
        }
        else
        {
            if (tmp == "COLUMN")
            {
            }
            else if (tmp == "TABLESPACE")
            {
                tablespace = toExtract::partDescribe(*i, 4);
            }
            else if (tmp == "COMMENT" )
            {
                end_statements += ("comment on table " + table_name +
                                   " is '" + toExtract::partDescribe(*i, 4) + "';\n");
            }
            else
            {
                throw qApp->translate("toExtract", "Unkown string %1").arg(*i);
            }
        } // if context.isNull
        i++;
    } // while

    table_columns = toExtract::parseColumnDescription(destin);

    // Part II - generate oracle create table statement from data in local structures
    // table header
    tmp = "create table " + schema_name + "." + table_name + "(";

    // table body - loop through columns
    for (std::list<toExtract::columnInfo>::iterator i = table_columns.begin(); i != table_columns.end(); i++)
    {
        if (!first_column)
            tmp += ",";
        else
            first_column = false;
        tmp += ("\n" + (*i).Name + " " + (*i).Definition);

        if (!(*i).DefaultValue.isEmpty())
            tmp += (" DEFAULT '" + (*i).DefaultValue + "'");

        if ((*i).bNotNull)
            tmp += " NOT NULL";

        if (!(*i).Comment.isEmpty())
            end_statements +=
                ("comment on column " + table_name + "." + (*i).Name + " is '" + (*i).Comment + "';\n");
    } // for loop through columns

    // table footer
    tmp += ")";
    if (!tablespace.isNull())
        tmp += ("\ntablespace " + tablespace + ";");
    else
        tmp += ";";

    if (!end_statements.isEmpty())
        tmp += ("\n" + end_statements);

    return tmp;
} // createTable

/***********************************************************************************
Purpose: Checks if specified property(-ies) is mentioned in given list.
  This function is used when altering tables. It can for example say that column
mentioned in drop list should not actually be dropped because it is being renamed.
************************************************************************************/
bool propertyMentioned(std::list<QString> &list,
                       const QString property1,
                       const QString property2)
{
    QStringList split;
    bool bProp1;

    std::list<QString>::iterator i = list.begin();
    while (i != list.end())
    {
        split = (*i).split("\01");
        bProp1 = false;
        for (int j = 0; j < split.count(); j++)
        {
            if (!bProp1)
            {
                if (split[j] == property1)
                {
                    bProp1 = true;
                }
            }
            else if (split[j] == property2)
            {
                return true;
            }
        }
        i++;
    }
    return false;
} // columnMentioned

/***********************************************************************************
Purpose: Checks if column_type has default value. If it does not - it checks if
         default value was present before. If so - it returns true - meaning that
         altering operation should also remove default value
************************************************************************************/
bool defaultValueRemoved(std::list<QString> &list,
                         const QString column_name,
                         const QString column_type)
{
    QStringList split;

    if (column_type.indexOf("default", 0, Qt::CaseInsensitive) > -1)
        // there is default value specified, nothing else to check
        return false;

    std::list<QString>::iterator i = list.begin();
    while (i != list.end())
    {
        split = (*i).split("\01");
        if (split.count() == 6)
            if ((split[3] == "COLUMN") &&
                    (split[4] == column_name) &&
                    (split[5].indexOf(" default '", 0, Qt::CaseInsensitive) > -1))
                return true; // yes, default value WAS removed
        i++;
    }
    return false;
} // defaultValueRemoved
/***********************************************************************************
Purpose: Alter existing table
************************************************************************************/
QString toOracleExtract::alterTable(std::list<QString> &source,
                                    std::list<QString> &destin) const
{
    std::list<QString> drop; // will hold table properties being removed
    std::list<QString> create; // will hold table properties being added
    toExtract::srcDst2DropCreate(source, destin, drop, create);

    QString statements;
    QString context = "";
    QString table_name;
    QString new_table_name;
    QString column_name = "", column_type;
    QString tmp, tmp1;
    QString end_statements; // statements to be added to the end
    QString operation; // used to differentiate between add and modify column
    QString not_null;
    bool add_modify_column = false;

    // drop statements
    for (std::list<QString>::iterator i = drop.begin(); i != drop.end(); i++)
    {
#ifdef DEBUG
        qDebug() << "drop>>" + *i;
#endif
        table_name = toExtract::partDescribe(*i, 2);
        if (!SameContext(*i, context))
        {
            if (context != "")
            {
                if (column_name != "")
                    // check if property is not mentioned in create list,
                    // if it is, this is not dropping of column but rather renaming
                    if (!propertyMentioned(destin, "COLUMN", column_name))
                        statements += ("alter table " + table_name + " drop column " + column_name + ";\n");
                column_name = "";
            }
            context = toExtract::contextDescribe(*i, 5);
        }

        std::list<QString> row = toExtract::splitDescribe(*i);
        // read through "context" - skip general tablename information
        for (int j = 0; j < 3; j++)
            toShift(row);
        tmp = toShift(row);
        if (tmp == "COLUMN")
        {
            column_name = toShift(row);
            tmp = toShift(row);
            if (tmp.isNull())
            {
                // nothing to be done here, column name was defined
            }
            else if (tmp == "ORDER")
            {
                // nothing to do with ORDER
            }
            else if (tmp == "COMMENT")
            {
                if (propertyMentioned(destin, "COLUMN", column_name)) // if column is not dropped...
                    if (!propertyMentioned(destin, column_name, "COMMENT")) // ...and comment is not changed
                        // then drop comment
                        statements += ("comment on column " + table_name + "." + column_name + " is '';\n");
            }
            else if (tmp == "EXTRA")
            {
                if (propertyMentioned(destin, "COLUMN", column_name))
                    // if column does not have to be dropped - just remove "not null" flag
                    statements += ("alter table " + table_name + " modify (" + column_name + " null);\n");
            }
            else
            {
                // nothing to do with column type
            }
        }
        else if (tmp == "PARAMETERS")
        {
            // TODO Check if anything should/could be done with parameters being dropped
            tmp = toExtract::partDescribe(*i, 4);
#ifdef DEBUG
            qDebug() << "Dropping parameters is NOT currently supported (" + tmp + ").";
#endif
        }
        else if (tmp == "STORAGE")
        {
            // TODO Check if anything should/could be done with sotrage attributes being dropped
            tmp = toExtract::partDescribe(*i, 4);
#ifdef DEBUG
            qDebug() << "Dropping storage attributes is NOT currently supported (" + tmp + ").";
#endif
        }
        else
        {
            throw qApp->translate("toExtract", "Unkown string %1 for drop statements").arg(tmp);
        }
    }
    if (column_name != "")
        if (!propertyMentioned(destin, "COLUMN", column_name))
            statements += ("alter table " + table_name + " drop column " + column_name + ";\n");

    // add/modify/rename statements
    column_name = "";
    context = "";
    for (std::list<QString>::iterator i = create.begin(); i != create.end(); i++)
    {
#ifdef DEBUG
        qDebug() << "add>>" + *i;
#endif

        table_name = toExtract::partDescribe(*i, 2);
        tmp = toExtract::partDescribe(*i, 5);
        if (!SameContext(*i, context))
        {
            if (context != "")
            {
                if (add_modify_column)
                {
                    if (propertyMentioned(source, "COLUMN", column_name))
                    {
                        operation = "modify";
                        if (defaultValueRemoved(drop, column_name, column_type))
                            column_type += " default null"; // Note! It is not possible to remove default value in Oracle!!!
                    }
                    else
                    {
                        operation = "add";
                    }
                    statements += ("alter table " + table_name + " " + operation +
                                   " (" + column_name + " " + column_type + not_null + ");\n");
                    add_modify_column = false;
                }
                column_name == "";
            }
            context = toExtract::contextDescribe(*i, 5);
            not_null = "";
        }

        std::list<QString> row = toExtract::splitDescribe(*i);
        // read through "context" - skip general tablename information
        for (int j = 0; j < 3; j++)
            toShift(row);
        tmp = toShift(row);
        if (tmp == "COLUMN")
        {
            column_name = toShift(row);
            tmp = toShift(row);  // value AFTER column name
            if (tmp.isNull())
            {
                column_name = tmp1;
            }
            else if (tmp == "RENAME")
            {
                tmp = toShift(row);
                statements += ("alter table " + table_name + " rename column " + column_name + " to " + tmp + ";\n");
            }
            else if (tmp == "ORDER")
            {
                // nothing to do with ORDER
            }
            else if (tmp == "COMMENT")
            {
                tmp = toShift(row);
                end_statements += ("comment on column " + table_name + "." + column_name + " is '" + tmp + "';\n");
            }
            else if (tmp == "EXTRA")
            {
                not_null = " NOT NULL"; // set "not null" flag on column
                add_modify_column = true;
            }
            else
            {
                column_type = tmp;
                add_modify_column = true;
            }
        }
        else if (tmp == "RENAME")   // rename table
        {
            new_table_name = toShift(row);
        }
        else
        {
            throw qApp->translate("toExtract", "Unkown string %1 for alter/add statements.").arg(tmp);
        }
    }
    if (add_modify_column)
    {
        if (propertyMentioned(source, "COLUMN", column_name))
        {
            operation = "modify";
            if (defaultValueRemoved(drop, column_name, column_type))
                column_type += " default null"; // Note! It is not possible to remove default value in Oracle!!!
        }
        else
        {
            operation = "add";
        }
        statements += ("alter table " + table_name + " " + operation +
                       " (" + column_name + " " + column_type + not_null + ");\n");
    }

    if (!new_table_name.isNull())
        statements += ("alter table " + table_name + " rename to " + new_table_name + ";");

    return statements + end_statements;
} // alterTable

/***********************************************************************************
   Main function for table creation/altering called from tobrowsertable.cpp
   It will call either createTable either alterTable
************************************************************************************/
QString toOracleExtract::migrateTable(toExtract &ext, std::list<QString> &source,
                                      std::list<QString> &destin) const
{
#ifdef DEBUG
    qDebug() << "toOracleExtract::migrateTable source=";
    for (std::list<QString>::iterator i = source.begin(); i != source.end(); i++)
    {
        qDebug() << *i;
    }
    qDebug() << "toOracleExtract::migrateTable destin=";
    for (std::list<QString>::iterator i = destin.begin(); i != destin.end(); i++)
    {
        qDebug() << *i;
    }
#endif


    if (source.empty())
    {
#ifdef DEBUG
        qDebug() << "New table has to be created.";
#endif
        return createTable(destin);
    }
    else
    {
#ifdef DEBUG
        qDebug() << "Existing table is to be modified.";
#endif
        return alterTable(source, destin);
    }
} // migrateTable