File: ItemIdMapper.cpp

package info (click to toggle)
buteo-sync-plugins 0.8.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,076 kB
  • sloc: cpp: 8,130; xml: 755; sh: 207; perl: 82; makefile: 11
file content (211 lines) | stat: -rw-r--r-- 5,856 bytes parent folder | download
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
/*
 * This file is part of buteo-sync-plugins package
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "ItemIdMapper.h"

#include "SyncMLPluginLogging.h"

const QString CONNECTIONNAME( "idmapper" );

ItemIdMapper::ItemIdMapper() :
    iNextValue(1)
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}

ItemIdMapper::~ItemIdMapper()
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}

bool ItemIdMapper::init( const QString& aDbFile, const QString& aStorageId )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    static unsigned connectionNumber = 0;

    qCDebug(lcSyncMLPlugin) << "Initiating ID mapper...";

    if( !iDb.isOpen() ) {
        iConnectionName = CONNECTIONNAME + QString::number( connectionNumber++ );
        iDb = QSqlDatabase::addDatabase( "QSQLITE", iConnectionName );
        iDb.setDatabaseName( aDbFile );
        if(!iDb.open()) { //CID 29154
            qCCritical(lcSyncMLPlugin) << "Could open ID database file:" << aDbFile;
            return false;
        }
    }

    iStorageId = aStorageId;

    QString queryString;
    QSqlQuery query;

    queryString.append( "CREATE TABLE if not exists " );
    queryString.append( iStorageId );
    queryString.append(" (value integer primary key, key varchar(512))" );

    query = QSqlQuery( queryString, iDb );
    if( !query.exec() ) {
        qCCritical(lcSyncMLPlugin) << "Create Query failed: " << query.lastError();
        return false;
    }


    // Load the key,value pairs in memory
    queryString.clear();
    queryString.append( "SELECT key, value FROM " );
    queryString.append( iStorageId );
    query = QSqlQuery( queryString, iDb );
    if( query.exec() )
    {
        while( query.next() )
        {
            iKeyToValueMap[query.value(0).toString()] = query.value(1).toUInt();
            iValueToKeyMap[query.value(1).toUInt()] = query.value(0).toString();
            qCDebug(lcSyncMLPlugin) << "Mapped key " << query.value(0).toString() << " and value " << query.value(1).toString();
        }
        iNextValue = iKeyToValueMap.count() + 1;
    }

    qCDebug(lcSyncMLPlugin) << "ID mapper initiated";
    return true;

}


void ItemIdMapper::uninit()
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Uninitiating ID mapper...";

    {
        QString queryString;
        QSqlQuery query;

        bool supportsTransaction = iDb.transaction();
        if( !supportsTransaction )
        {
            qCDebug(lcSyncMLPlugin) << "Db doesn't support transactions";
        }

        queryString.append( "DELETE FROM " );
        queryString.append( iStorageId );
        query = QSqlQuery( queryString, iDb );
        if( !query.exec() )
        {
            qCWarning(lcSyncMLPlugin) << "Delete Query failed: " << query.lastError();
        }

        queryString.clear();
        queryString.append( "INSERT INTO " );
        queryString.append( iStorageId );
        queryString.append( " (value, key) values(:values, :key)" );
        query = QSqlQuery( queryString, iDb );
        QVariantList keys, values;
        for( int i = 0; i < iValueToKeyMap.count(); ++i )
        {
            values << (i + 1);
            keys << iValueToKeyMap[i+1];
        }
        query.addBindValue( values );
        query.addBindValue( keys );
        if( !query.execBatch() )
        {
            qCCritical(lcSyncMLPlugin) << "Save Query failed: " << query.lastError();
        }

        if( supportsTransaction )
        {
            if( !iDb.commit() )
            {
                qCCritical(lcSyncMLPlugin) << "Commit failed";
            }
        }
    }
    iDb.close();
    iDb = QSqlDatabase();
    QSqlDatabase::removeDatabase( iConnectionName );

    iNextValue = 1;

    qCDebug(lcSyncMLPlugin) << "ID mapper uninitiated";

}


QString ItemIdMapper::key( const QString& aValue )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    // NB#153991:In case SyncML stack asks for empty key, we shouldn't treat
    // it as an error situation, rather just not do mapping in that case.

    QString key = aValue;

    if( !iValueToKeyMap.contains( aValue.toUInt() ) ) {
        qCDebug(lcSyncMLPlugin) << "Value is empty, mapping not done";
    }
    else {
        key = iValueToKeyMap.value( aValue.toUInt() );
    }

    return key;
}


QString ItemIdMapper::value( const QString& aKey )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QString value = aKey;

    // If the key is already an integer, no mapping is needed.
    bool keyIsInt;
    int id = aKey.toInt(&keyIsInt);
    Q_UNUSED(id);

    if (aKey.isEmpty()) {
        qCWarning(lcSyncMLPlugin) << "Key is empty. Not trying to do mapping";
    }
    else if( !keyIsInt ) {
        if( !iKeyToValueMap.contains( aKey ) )
        {
           value = add( aKey );
        }
        else
        {
            value = QString::number( iKeyToValueMap.value( aKey ) );
        }
    }

    return value;
}

QString ItemIdMapper::add( const QString &aKey )
{
   iKeyToValueMap[aKey] = iNextValue;
   iValueToKeyMap[iNextValue] = aKey;
   return QString::number( iNextValue++ );
}