2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "public/platform/WebIDBKey.h"
31 #include "modules/indexeddb/IDBKey.h"
35 WebIDBKey WebIDBKey::createArray(const WebVector<WebIDBKey>& array)
38 key.assignArray(array);
42 WebIDBKey WebIDBKey::createBinary(const WebData& binary)
45 key.assignBinary(binary);
49 WebIDBKey WebIDBKey::createString(const WebString& string)
52 key.assignString(string);
56 WebIDBKey WebIDBKey::createDate(double date)
63 WebIDBKey WebIDBKey::createNumber(double number)
66 key.assignNumber(number);
70 WebIDBKey WebIDBKey::createInvalid()
77 WebIDBKey WebIDBKey::createNull()
84 void WebIDBKey::assign(const WebIDBKey& value)
86 m_private = value.m_private;
89 static IDBKey* convertFromWebIDBKeyArray(const WebVector<WebIDBKey>& array)
91 IDBKey::KeyArray keys;
92 keys.reserveCapacity(array.size());
93 for (size_t i = 0; i < array.size(); ++i) {
94 switch (array[i].keyType()) {
95 case WebIDBKeyTypeArray:
96 keys.append(convertFromWebIDBKeyArray(array[i].array()));
98 case WebIDBKeyTypeBinary:
99 keys.append(IDBKey::createBinary(array[i].binary()));
101 case WebIDBKeyTypeString:
102 keys.append(IDBKey::createString(array[i].string()));
104 case WebIDBKeyTypeDate:
105 keys.append(IDBKey::createDate(array[i].date()));
107 case WebIDBKeyTypeNumber:
108 keys.append(IDBKey::createNumber(array[i].number()));
110 case WebIDBKeyTypeInvalid:
111 keys.append(IDBKey::createInvalid());
113 case WebIDBKeyTypeNull:
114 case WebIDBKeyTypeMin:
115 ASSERT_NOT_REACHED();
119 return IDBKey::createArray(keys);
122 static void convertToWebIDBKeyArray(const IDBKey::KeyArray& array, WebVector<WebIDBKey>& result)
124 WebVector<WebIDBKey> keys(array.size());
125 WebVector<WebIDBKey> subkeys;
126 for (size_t i = 0; i < array.size(); ++i) {
127 IDBKey* key = array[i];
128 switch (key->type()) {
129 case IDBKey::ArrayType:
130 convertToWebIDBKeyArray(key->array(), subkeys);
131 keys[i] = WebIDBKey::createArray(subkeys);
133 case IDBKey::BinaryType:
134 keys[i] = WebIDBKey::createBinary(key->binary());
136 case IDBKey::StringType:
137 keys[i] = WebIDBKey::createString(key->string());
139 case IDBKey::DateType:
140 keys[i] = WebIDBKey::createDate(key->date());
142 case IDBKey::NumberType:
143 keys[i] = WebIDBKey::createNumber(key->number());
145 case IDBKey::InvalidType:
146 keys[i] = WebIDBKey::createInvalid();
148 case IDBKey::MinType:
149 ASSERT_NOT_REACHED();
156 void WebIDBKey::assignArray(const WebVector<WebIDBKey>& array)
158 m_private = convertFromWebIDBKeyArray(array);
161 void WebIDBKey::assignBinary(const WebData& binary)
163 m_private = IDBKey::createBinary(binary);
166 void WebIDBKey::assignString(const WebString& string)
168 m_private = IDBKey::createString(string);
171 void WebIDBKey::assignDate(double date)
173 m_private = IDBKey::createDate(date);
176 void WebIDBKey::assignNumber(double number)
178 m_private = IDBKey::createNumber(number);
181 void WebIDBKey::assignInvalid()
183 m_private = IDBKey::createInvalid();
186 void WebIDBKey::assignNull()
191 void WebIDBKey::reset()
196 WebIDBKeyType WebIDBKey::keyType() const
198 if (!m_private.get())
199 return WebIDBKeyTypeNull;
200 return static_cast<WebIDBKeyType>(m_private->type());
203 bool WebIDBKey::isValid() const
205 if (!m_private.get())
207 return m_private->isValid();
210 WebVector<WebIDBKey> WebIDBKey::array() const
212 WebVector<WebIDBKey> keys;
213 convertToWebIDBKeyArray(m_private->array(), keys);
217 WebData WebIDBKey::binary() const
219 return m_private->binary();
222 WebString WebIDBKey::string() const
224 return m_private->string();
227 double WebIDBKey::date() const
229 return m_private->date();
232 double WebIDBKey::number() const
234 return m_private->number();
237 WebIDBKey::WebIDBKey(IDBKey* value)
242 WebIDBKey& WebIDBKey::operator=(IDBKey* value)
248 WebIDBKey::operator IDBKey*() const
250 return m_private.get();