| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 #include "modules/webdatabase/sqlite/SQLLog.h" | 28 #include "modules/webdatabase/sqlite/SQLLog.h" |
| 29 #include "modules/webdatabase/sqlite/SQLValue.h" | 29 #include "modules/webdatabase/sqlite/SQLValue.h" |
| 30 #include "platform/heap/SafePoint.h" | 30 #include "platform/heap/SafePoint.h" |
| 31 #include "third_party/sqlite/sqlite3.h" | 31 #include "third_party/sqlite/sqlite3.h" |
| 32 #include "wtf/Assertions.h" | 32 #include "wtf/Assertions.h" |
| 33 #include "wtf/PtrUtil.h" | 33 #include "wtf/PtrUtil.h" |
| 34 #include "wtf/text/CString.h" | 34 #include "wtf/text/CString.h" |
| 35 #include <memory> | 35 #include <memory> |
| 36 | 36 |
| 37 // SQLite 3.6.16 makes sqlite3_prepare_v2 automatically retry preparing the stat
ement | 37 // SQLite 3.6.16 makes sqlite3_prepare_v2 automatically retry preparing the |
| 38 // once if the database scheme has changed. We rely on this behavior. | 38 // statement once if the database scheme has changed. We rely on this behavior. |
| 39 #if SQLITE_VERSION_NUMBER < 3006016 | 39 #if SQLITE_VERSION_NUMBER < 3006016 |
| 40 #error SQLite version 3.6.16 or newer is required | 40 #error SQLite version 3.6.16 or newer is required |
| 41 #endif | 41 #endif |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 // Only return error codes consistent with 3.7.6.3. | 45 // Only return error codes consistent with 3.7.6.3. |
| 46 int restrictError(int error) { | 46 int restrictError(int error) { |
| 47 switch (error) { | 47 switch (error) { |
| 48 case SQLITE_IOERR_READ: | 48 case SQLITE_IOERR_READ: |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 std::unique_ptr<const char*> tail = wrapUnique(new const char*); | 103 std::unique_ptr<const char*> tail = wrapUnique(new const char*); |
| 104 std::unique_ptr<sqlite3_stmt*> statement = wrapUnique(new sqlite3_stmt*); | 104 std::unique_ptr<sqlite3_stmt*> statement = wrapUnique(new sqlite3_stmt*); |
| 105 *tail = nullptr; | 105 *tail = nullptr; |
| 106 *statement = nullptr; | 106 *statement = nullptr; |
| 107 int error; | 107 int error; |
| 108 { | 108 { |
| 109 SafePointScope scope(BlinkGC::HeapPointersOnStack); | 109 SafePointScope scope(BlinkGC::HeapPointersOnStack); |
| 110 | 110 |
| 111 SQL_DVLOG(1) << "SQL - prepare - " << query.data(); | 111 SQL_DVLOG(1) << "SQL - prepare - " << query.data(); |
| 112 | 112 |
| 113 // Pass the length of the string including the null character to sqlite3_pre
pare_v2; | 113 // Pass the length of the string including the null character to |
| 114 // this lets SQLite avoid an extra string copy. | 114 // sqlite3_prepare_v2; this lets SQLite avoid an extra string copy. |
| 115 size_t lengthIncludingNullCharacter = query.length() + 1; | 115 size_t lengthIncludingNullCharacter = query.length() + 1; |
| 116 | 116 |
| 117 error = sqlite3_prepare_v2(m_database.sqlite3Handle(), query.data(), | 117 error = sqlite3_prepare_v2(m_database.sqlite3Handle(), query.data(), |
| 118 lengthIncludingNullCharacter, statement.get(), | 118 lengthIncludingNullCharacter, statement.get(), |
| 119 tail.get()); | 119 tail.get()); |
| 120 } | 120 } |
| 121 m_statement = *statement; | 121 m_statement = *statement; |
| 122 | 122 |
| 123 if (error != SQLITE_OK) | 123 if (error != SQLITE_OK) |
| 124 SQL_DVLOG(1) << "sqlite3_prepare16 failed (" << error << ")\n" | 124 SQL_DVLOG(1) << "sqlite3_prepare16 failed (" << error << ")\n" |
| 125 << query.data() << "\n" | 125 << query.data() << "\n" |
| 126 << sqlite3_errmsg(m_database.sqlite3Handle()); | 126 << sqlite3_errmsg(m_database.sqlite3Handle()); |
| 127 else if (*tail && **tail) | 127 else if (*tail && **tail) |
| 128 error = SQLITE_ERROR; | 128 error = SQLITE_ERROR; |
| 129 | 129 |
| 130 #if ENABLE(ASSERT) | 130 #if ENABLE(ASSERT) |
| 131 m_isPrepared = error == SQLITE_OK; | 131 m_isPrepared = error == SQLITE_OK; |
| 132 #endif | 132 #endif |
| 133 return restrictError(error); | 133 return restrictError(error); |
| 134 } | 134 } |
| 135 | 135 |
| 136 int SQLiteStatement::step() { | 136 int SQLiteStatement::step() { |
| 137 SafePointScope scope(BlinkGC::HeapPointersOnStack); | 137 SafePointScope scope(BlinkGC::HeapPointersOnStack); |
| 138 //ASSERT(m_isPrepared); | 138 // ASSERT(m_isPrepared); |
| 139 | 139 |
| 140 if (!m_statement) | 140 if (!m_statement) |
| 141 return SQLITE_OK; | 141 return SQLITE_OK; |
| 142 | 142 |
| 143 // The database needs to update its last changes count before each statement | 143 // The database needs to update its last changes count before each statement |
| 144 // in order to compute properly the lastChanges() return value. | 144 // in order to compute properly the lastChanges() return value. |
| 145 m_database.updateLastChangesCount(); | 145 m_database.updateLastChangesCount(); |
| 146 | 146 |
| 147 SQL_DVLOG(1) << "SQL - step - " << m_query; | 147 SQL_DVLOG(1) << "SQL - step - " << m_query; |
| 148 int error = sqlite3_step(m_statement); | 148 int error = sqlite3_step(m_statement); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 if (!m_statement) | 251 if (!m_statement) |
| 252 if (prepareAndStep() != SQLITE_ROW) | 252 if (prepareAndStep() != SQLITE_ROW) |
| 253 return SQLValue(); | 253 return SQLValue(); |
| 254 if (columnCount() <= col) | 254 if (columnCount() <= col) |
| 255 return SQLValue(); | 255 return SQLValue(); |
| 256 | 256 |
| 257 // SQLite is typed per value. optional column types are | 257 // SQLite is typed per value. optional column types are |
| 258 // "(mostly) ignored" | 258 // "(mostly) ignored" |
| 259 sqlite3_value* value = sqlite3_column_value(m_statement, col); | 259 sqlite3_value* value = sqlite3_column_value(m_statement, col); |
| 260 switch (sqlite3_value_type(value)) { | 260 switch (sqlite3_value_type(value)) { |
| 261 case SQLITE_INTEGER: // SQLValue and JS don't represent integers, so use FL
OAT -case | 261 case SQLITE_INTEGER: // SQLValue and JS don't represent integers, so use |
| 262 // FLOAT -case |
| 262 case SQLITE_FLOAT: | 263 case SQLITE_FLOAT: |
| 263 return SQLValue(sqlite3_value_double(value)); | 264 return SQLValue(sqlite3_value_double(value)); |
| 264 case SQLITE_BLOB: // SQLValue and JS don't represent blobs, so use TEXT -ca
se | 265 case SQLITE_BLOB: // SQLValue and JS don't represent blobs, so use TEXT |
| 266 // -case |
| 265 case SQLITE_TEXT: { | 267 case SQLITE_TEXT: { |
| 266 const UChar* string = | 268 const UChar* string = |
| 267 reinterpret_cast<const UChar*>(sqlite3_value_text16(value)); | 269 reinterpret_cast<const UChar*>(sqlite3_value_text16(value)); |
| 268 unsigned length = sqlite3_value_bytes16(value) / sizeof(UChar); | 270 unsigned length = sqlite3_value_bytes16(value) / sizeof(UChar); |
| 269 return SQLValue(StringImpl::create8BitIfPossible(string, length)); | 271 return SQLValue(StringImpl::create8BitIfPossible(string, length)); |
| 270 } | 272 } |
| 271 case SQLITE_NULL: | 273 case SQLITE_NULL: |
| 272 return SQLValue(); | 274 return SQLValue(); |
| 273 default: | 275 default: |
| 274 break; | 276 break; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 304 ASSERT(col >= 0); | 306 ASSERT(col >= 0); |
| 305 if (!m_statement) | 307 if (!m_statement) |
| 306 if (prepareAndStep() != SQLITE_ROW) | 308 if (prepareAndStep() != SQLITE_ROW) |
| 307 return 0; | 309 return 0; |
| 308 if (columnCount() <= col) | 310 if (columnCount() <= col) |
| 309 return 0; | 311 return 0; |
| 310 return sqlite3_column_int64(m_statement, col); | 312 return sqlite3_column_int64(m_statement, col); |
| 311 } | 313 } |
| 312 | 314 |
| 313 } // namespace blink | 315 } // namespace blink |
| OLD | NEW |