~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/database/databasecopymanager.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer, Rohan Garg, Philip Muškovac, Felix Geyer
  • Date: 2011-09-23 18:18:55 UTC
  • mfrom: (1.2.36 upstream)
  • Revision ID: package-import@ubuntu.com-20110923181855-ifs67wxkugshev9k
Tags: 2:2.1.1-0ubuntu1
[ Rohan Garg ]
* New upstream release (LP: #834190)
  - debian/control
    + Build with libqtwebkit-dev
 - debian/kipi-plugins-common
    + Install libkvkontakte required by kipi-plugins
 - debian/digikam
    + Install panoramagui

[ Philip Muškovac ]
* New upstream release
  - debian/control:
    + Add libcv-dev, libcvaux-dev, libhighgui-dev, libboost-graph1.46-dev,
      libksane-dev, libxml2-dev, libxslt-dev, libqt4-opengl-dev, libqjson-dev,
      libgpod-dev and libqca2-dev to build-deps
    + Add packages for kipi-plugins, libmediawiki, libkface, libkgeomap and
      libkvkontakte
  - debian/rules:
    + Don't build with gphoto2 since it doesn't build with it.
  - Add kubuntu_fix_test_linking.diff to fix linking of the dngconverter test
  - update install files
  - update kubuntu_01_mysqld_executable_name.diff for new cmake layout
    and rename to kubuntu_mysqld_executable_name.diff
* Fix typo in digikam-data description (LP: #804894)
* Fix Vcs links

[ Felix Geyer ]
* Move library data files to the new packages libkface-data, libkgeomap-data
  and libkvkontakte-data.
* Override version of the embedded library packages to 1.0~digikam<version>.
* Exclude the library packages from digikam-dbg to prevent file conflicts in
  the future.
* Call dh_install with --list-missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ============================================================
2
 
 *
3
 
 * This file is a part of digiKam project
4
 
 * http://www.digikam.org
5
 
 *
6
 
 * Date        : 2009-11-14
7
 
 * Description : database migration dialog
8
 
 *
9
 
 * Copyright (C) 2009-2010 by Holger Foerster <Hamsi2k at freenet dot de>
10
 
 *
11
 
 * This program is free software; you can redistribute it
12
 
 * and/or modify it under the terms of the GNU General
13
 
 * Public License as published by the Free Software Foundation;
14
 
 * either version 2, or (at your option)
15
 
 * any later version.
16
 
 *
17
 
 * This program is distributed in the hope that it will be useful,
18
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
 * GNU General Public License for more details.
21
 
 *
22
 
 * ============================================================ */
23
 
 
24
 
#include "databasecopymanager.moc"
25
 
 
26
 
// QT includes
27
 
 
28
 
#include <QSqlError>
29
 
#include <QSqlDriver>
30
 
 
31
 
// KDE Includes
32
 
 
33
 
#include "kdebug.h"
34
 
#include "klocale.h"
35
 
 
36
 
// Local includes
37
 
 
38
 
#include "databasecorebackend.h"
39
 
#include "databaseparameters.h"
40
 
#include "albumdb.h"
41
 
#include "schemaupdater.h"
42
 
 
43
 
namespace Digikam
44
 
{
45
 
 
46
 
DatabaseCopyManager::DatabaseCopyManager()
47
 
{
48
 
}
49
 
 
50
 
DatabaseCopyManager::~DatabaseCopyManager()
51
 
{
52
 
}
53
 
 
54
 
void DatabaseCopyManager::stopProcessing()
55
 
{
56
 
    isStopProcessing=true;
57
 
}
58
 
 
59
 
void DatabaseCopyManager::copyDatabases(DatabaseParameters fromDBParameters, DatabaseParameters toDBParameters)
60
 
{
61
 
    isStopProcessing = false;
62
 
    DatabaseLocking fromLocking;
63
 
    DatabaseBackend fromDBbackend(&fromLocking, "MigrationFromDatabase");
64
 
 
65
 
    if (!fromDBbackend.open(fromDBParameters))
66
 
    {
67
 
        emit finished(DatabaseCopyManager::failed, i18n("Error while opening the source database."));
68
 
        return;
69
 
    }
70
 
 
71
 
    DatabaseLocking toLocking;
72
 
    DatabaseBackend toDBbackend(&toLocking, "MigrationToDatabase");
73
 
 
74
 
    if (!toDBbackend.open(toDBParameters))
75
 
    {
76
 
        emit finished(DatabaseCopyManager::failed, i18n("Error while opening the target database."));
77
 
        fromDBbackend.close();
78
 
        return;
79
 
    }
80
 
 
81
 
    QMap<QString, QVariant> bindingMap;
82
 
 
83
 
    // Delete all tables
84
 
    if (toDBbackend.execDBAction(toDBbackend.getDBAction("Migrate_Cleanup_DB"), bindingMap) != DatabaseCoreBackend::NoErrors)
85
 
    {
86
 
        emit finished(DatabaseCopyManager::failed, i18n("Error while scrubbing the target database."));
87
 
        fromDBbackend.close();
88
 
        toDBbackend.close();
89
 
        return;
90
 
    }
91
 
 
92
 
    // then create the schema
93
 
    AlbumDB       albumDB(&toDBbackend);
94
 
    SchemaUpdater updater(&albumDB, &toDBbackend, toDBParameters);
95
 
 
96
 
    emit stepStarted(i18n("Create Schema..."));
97
 
 
98
 
    if (!updater.update())
99
 
    {
100
 
        emit finished(DatabaseCopyManager::failed, i18n("Error while creating the database schema."));
101
 
        fromDBbackend.close();
102
 
        toDBbackend.close();
103
 
        return;
104
 
    }
105
 
 
106
 
    emit stepStarted(i18n("Copy AlbumRoots..."));
107
 
 
108
 
    // now perform the copy action
109
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_AlbumRoots"), toDBbackend, QString("Migrate_Write_AlbumRoots")))
110
 
    {
111
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
112
 
        return;
113
 
    }
114
 
 
115
 
    emit stepStarted(i18n("Copy Albums..."));
116
 
 
117
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_Albums"), toDBbackend, QString("Migrate_Write_Albums")))
118
 
    {
119
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
120
 
        return;
121
 
    }
122
 
 
123
 
    emit stepStarted(i18n("Copy Images..."));
124
 
 
125
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_Images"), toDBbackend, QString("Migrate_Write_Images")))
126
 
    {
127
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
128
 
        return;
129
 
    }
130
 
 
131
 
    emit stepStarted(i18n("Copy ImageHaarMatrix..."));
132
 
 
133
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageHaarMatrix"), toDBbackend, QString("Migrate_Write_ImageHaarMatrix")))
134
 
    {
135
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
136
 
        return;
137
 
    }
138
 
 
139
 
    emit stepStarted(i18n("Copy ImageInformation..."));
140
 
 
141
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageInformation"), toDBbackend, QString("Migrate_Write_ImageInformation")))
142
 
    {
143
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
144
 
        return;
145
 
    }
146
 
 
147
 
    emit stepStarted(i18n("Copy ImageMetadata..."));
148
 
 
149
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageMetadata"), toDBbackend, QString("Migrate_Write_ImageMetadata")))
150
 
    {
151
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
152
 
        return;
153
 
    }
154
 
 
155
 
    emit stepStarted(i18n("Copy ImagePositions..."));
156
 
 
157
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImagePositions"), toDBbackend, QString("Migrate_Write_ImagePositions")))
158
 
    {
159
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
160
 
        return;
161
 
    }
162
 
 
163
 
    emit stepStarted(i18n("Copy ImageComments..."));
164
 
 
165
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageComments"), toDBbackend, QString("Migrate_Write_ImageComments")))
166
 
    {
167
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
168
 
        return;
169
 
    }
170
 
 
171
 
    emit stepStarted(i18n("Copy ImageCopyright..."));
172
 
 
173
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageCopyright"), toDBbackend, QString("Migrate_Write_ImageCopyright")))
174
 
    {
175
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
176
 
        return;
177
 
    }
178
 
 
179
 
    emit stepStarted(i18n("Copy Tags..."));
180
 
 
181
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_Tags"), toDBbackend, QString("Migrate_Write_Tags")))
182
 
    {
183
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
184
 
        return;
185
 
    }
186
 
 
187
 
    emit stepStarted(i18n("Copy ImageTags..."));
188
 
 
189
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageTags"), toDBbackend, QString("Migrate_Write_ImageTags")))
190
 
    {
191
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
192
 
        return;
193
 
    }
194
 
 
195
 
    emit stepStarted(i18n("Copy ImageProperties..."));
196
 
 
197
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_ImageProperties"), toDBbackend, QString("Migrate_Write_ImageProperties")))
198
 
    {
199
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
200
 
        return;
201
 
    }
202
 
 
203
 
    emit stepStarted(i18n("Copy Searches..."));
204
 
 
205
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_Searches"), toDBbackend, QString("Migrate_Write_Searches")))
206
 
    {
207
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
208
 
        return;
209
 
    }
210
 
 
211
 
    emit stepStarted(i18n("Copy DownloadHistory..."));
212
 
 
213
 
    if (isStopProcessing || !copyTable(fromDBbackend, QString("Migrate_Read_DownloadHistory"), toDBbackend, QString("Migrate_Write_DownloadHistory")))
214
 
    {
215
 
        handleClosing(isStopProcessing, fromDBbackend, toDBbackend);
216
 
        return;
217
 
    }
218
 
 
219
 
    /*
220
 
        if (isStopThread || !copyTable(fromDBbackend, QString("Migrate_Read_Settings"), toDBbackend, QString("Migrate_Write_Settings")))
221
 
        {
222
 
            handleClosing(isStopThread, fromDBbackend, toDBbackend);
223
 
            return;
224
 
        }
225
 
    */
226
 
    fromDBbackend.close();
227
 
    toDBbackend.close();
228
 
 
229
 
    emit finished(DatabaseCopyManager::success, QString());
230
 
}
231
 
 
232
 
bool DatabaseCopyManager::copyTable(DatabaseBackend& fromDBbackend, QString fromActionName, DatabaseBackend& toDBbackend, QString toActionName)
233
 
{
234
 
    kDebug(50003) << "Trying to copy contents from DB with ActionName: [" << fromActionName
235
 
                  << "] to DB with ActionName [" << toActionName << "]";
236
 
 
237
 
    QMap<QString, QVariant> bindingMap;
238
 
    // now perform the copy action
239
 
    QList<QString> columnNames;
240
 
    QSqlQuery result = fromDBbackend.execDBActionQuery(fromDBbackend.getDBAction(fromActionName), bindingMap) ;
241
 
 
242
 
    int resultSize = -1;
243
 
 
244
 
    if (result.driver()->hasFeature(QSqlDriver::QuerySize))
245
 
    {
246
 
        resultSize=result.size();
247
 
    }
248
 
    else
249
 
    {
250
 
        kDebug(50003) << "Driver doesn't support query size. We try to go to the last row and back to the current.";
251
 
        result.last();
252
 
        /*
253
 
         * Now get the current row. If this is not possible, a value lower than 0 will be returned.
254
 
         * To not confuse the log reading user, we reset this value to 0.
255
 
         */
256
 
        resultSize = result.at()<0 ? 0 : result.at();
257
 
        result.first();
258
 
    }
259
 
 
260
 
    kDebug(50003) << "Result size: ["<< resultSize << "]";
261
 
 
262
 
    /*
263
 
     * If the sql query is forward only - perform the query again.
264
 
     * This is not atomic, so it can be tend to different results between
265
 
     * real database entries copied and shown at the progressbar.
266
 
     */
267
 
    if (result.isForwardOnly())
268
 
    {
269
 
        result.finish();
270
 
        result = fromDBbackend.execDBActionQuery(fromDBbackend.getDBAction(fromActionName), bindingMap) ;
271
 
    }
272
 
 
273
 
    int columnCount = result.record().count();
274
 
 
275
 
    for (int i=0; i<columnCount; i++)
276
 
    {
277
 
        //            kDebug(50003) << "Column: ["<< result.record().fieldName(i) << "]";
278
 
        columnNames.append(result.record().fieldName(i));
279
 
    }
280
 
 
281
 
    int resultCounter=0;
282
 
 
283
 
    while (result.next())
284
 
    {
285
 
        kDebug(50003) << "Query isOnValidRow ["<< result.isValid() <<"] isActive ["<< result.isActive() <<"] result size: ["<< result.size() << "]";
286
 
 
287
 
        if (isStopProcessing==true)
288
 
        {
289
 
            return false;
290
 
        }
291
 
 
292
 
        // Send a signal to the GUI to entertain the user
293
 
        emit smallStepStarted(++resultCounter, resultSize);
294
 
 
295
 
        // read the values from the fromDB into a hash
296
 
        QMap<QString, QVariant> tempBindingMap;
297
 
        int i=0;
298
 
 
299
 
        foreach (QString columnName, columnNames)
300
 
        {
301
 
            kDebug(50003) << "Column: ["<< columnName << "] value ["<<result.value(i)<<"]";
302
 
            tempBindingMap.insert(columnName.insert(0, ':'), result.value(i));
303
 
            i++;
304
 
        }
305
 
 
306
 
        // insert the previous requested values to the toDB
307
 
        DatabaseAction action = toDBbackend.getDBAction(toActionName);
308
 
        DatabaseCoreBackend::QueryState queryStateResult = toDBbackend.execDBAction(action, tempBindingMap);
309
 
 
310
 
        if (queryStateResult != DatabaseCoreBackend::NoErrors && toDBbackend.lastSQLError().isValid() && toDBbackend.lastSQLError().number()!=0)
311
 
        {
312
 
            kDebug(50003) << "Error while converting table data. Details: " << toDBbackend.lastSQLError();
313
 
            QString errorMsg = i18n("Error while converting the database. \n Details: %1", toDBbackend.lastSQLError().databaseText());
314
 
            emit finished(DatabaseCopyManager::failed, errorMsg);
315
 
            return false;
316
 
        }
317
 
 
318
 
    }
319
 
 
320
 
    return true;
321
 
}
322
 
 
323
 
void DatabaseCopyManager::handleClosing(bool isStopThread, DatabaseBackend& fromDBbackend, DatabaseBackend& toDBbackend)
324
 
{
325
 
    if (isStopThread)
326
 
    {
327
 
        emit finished(DatabaseCopyManager::canceled, "");
328
 
    }
329
 
 
330
 
    fromDBbackend.close();
331
 
    toDBbackend.close();
332
 
}
333
 
 
334
 
} // namespace Digikam