1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "public/platform/WebIDBMetadata.h"
28
29#include "modules/indexeddb/IDBMetadata.h"
30#include "public/platform/WebIDBKeyPath.h"
31#include "public/platform/WebString.h"
32#include "public/platform/WebVector.h"
33
34namespace blink {
35
36WebIDBMetadata::WebIDBMetadata(const IDBDatabaseMetadata& metadata)
37{
38    name = metadata.name;
39    version = metadata.version;
40    intVersion = metadata.intVersion;
41    objectStores = WebVector<ObjectStore>(static_cast<size_t>(metadata.objectStores.size()));
42    maxObjectStoreId = metadata.maxObjectStoreId;
43
44    size_t i = 0;
45    for (IDBDatabaseMetadata::ObjectStoreMap::const_iterator storeIterator = metadata.objectStores.begin(); storeIterator != metadata.objectStores.end(); ++storeIterator) {
46        const IDBObjectStoreMetadata& objectStore = storeIterator->value;
47        ObjectStore webObjectStore;
48        webObjectStore.id = objectStore.id;
49        webObjectStore.name = objectStore.name;
50        webObjectStore.keyPath = objectStore.keyPath;
51        webObjectStore.autoIncrement = objectStore.autoIncrement;
52        webObjectStore.indexes = WebVector<Index>(static_cast<size_t>(objectStore.indexes.size()));
53        webObjectStore.maxIndexId = objectStore.maxIndexId;
54
55        size_t j = 0;
56        for (IDBObjectStoreMetadata::IndexMap::const_iterator indexIterator = objectStore.indexes.begin(); indexIterator != objectStore.indexes.end(); ++indexIterator) {
57            const IDBIndexMetadata& index = indexIterator->value;
58            Index webIndex;
59            webIndex.id = index.id;
60            webIndex.name = index.name;
61            webIndex.keyPath = index.keyPath;
62            webIndex.unique = index.unique;
63            webIndex.multiEntry = index.multiEntry;
64            webObjectStore.indexes[j++] = webIndex;
65        }
66        objectStores[i++] = webObjectStore;
67    }
68}
69
70WebIDBMetadata::operator IDBDatabaseMetadata() const
71{
72    IDBDatabaseMetadata db(name, id, version, intVersion, maxObjectStoreId);
73    for (size_t i = 0; i < objectStores.size(); ++i) {
74        const ObjectStore webObjectStore = objectStores[i];
75        IDBObjectStoreMetadata objectStore(webObjectStore.name, webObjectStore.id, webObjectStore.keyPath, webObjectStore.autoIncrement, webObjectStore.maxIndexId);
76
77        for (size_t j = 0; j < webObjectStore.indexes.size(); ++j) {
78            const Index webIndex = webObjectStore.indexes[j];
79            IDBIndexMetadata index(webIndex.name, webIndex.id, webIndex.keyPath, webIndex.unique, webIndex.multiEntry);
80            objectStore.indexes.set(index.id, index);
81        }
82        db.objectStores.set(objectStore.id, objectStore);
83    }
84    return db;
85}
86
87} // namespace blink
88