OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2007 Apple Inc. All rights reserved. | |
3 * Copyright (C) 2010 Google Inc. All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * | |
9 * 1. Redistributions of source code must retain the above copyright | |
10 * notice, this list of conditions and the following disclaimer. | |
11 * 2. Redistributions in binary form must reproduce the above copyright | |
12 * notice, this list of conditions and the following disclaimer in the | |
13 * documentation and/or other materials provided with the distribution. | |
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
15 * its contributors may be used to endorse or promote products derived | |
16 * from this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
30 #include "config.h" | |
31 #include "modules/webdatabase/SQLStatementSync.h" | |
32 | |
33 #include "bindings/core/v8/ExceptionState.h" | |
34 #include "core/dom/ExceptionCode.h" | |
35 #include "modules/webdatabase/sqlite/SQLiteDatabase.h" | |
36 #include "modules/webdatabase/sqlite/SQLiteStatement.h" | |
37 #include "modules/webdatabase/DatabaseSync.h" | |
38 #include "modules/webdatabase/SQLError.h" | |
39 #include "modules/webdatabase/SQLResultSet.h" | |
40 #include "wtf/PassRefPtr.h" | |
41 #include "wtf/RefPtr.h" | |
42 | |
43 namespace blink { | |
44 | |
45 SQLStatementSync::SQLStatementSync(const String& statement, const Vector<SQLValu
e>& arguments, int permissions) | |
46 : m_statement(statement) | |
47 , m_arguments(arguments) | |
48 , m_permissions(permissions) | |
49 { | |
50 ASSERT(!m_statement.isEmpty()); | |
51 } | |
52 | |
53 PassRefPtrWillBeRawPtr<SQLResultSet> SQLStatementSync::execute(DatabaseSync* db,
ExceptionState& exceptionState) | |
54 { | |
55 db->setAuthorizerPermissions(m_permissions); | |
56 | |
57 SQLiteDatabase* database = &db->sqliteDatabase(); | |
58 | |
59 SQLiteStatement statement(*database, m_statement); | |
60 int result = statement.prepare(); | |
61 if (result != SQLResultOk) { | |
62 if (result == SQLResultInterrupt) | |
63 exceptionState.throwDOMException(SQLDatabaseError, "Connection to th
e database interrupted."); | |
64 else | |
65 exceptionState.throwDOMException(SyntaxError, "Could not prepare sta
tement."); | |
66 db->setLastErrorMessage("could not prepare statement", result, database-
>lastErrorMsg()); | |
67 return nullptr; | |
68 } | |
69 | |
70 if (statement.bindParameterCount() != m_arguments.size()) { | |
71 if (db->isInterrupted()) | |
72 exceptionState.throwDOMException(SQLDatabaseError, "Connection to th
e database interrupted."); | |
73 else | |
74 exceptionState.throwDOMException(SyntaxError, "Number of '?'s in sta
tement string (" + String::number(statement.bindParameterCount()) + ") does not
match the arguments provided (" + String::number(m_arguments.size()) + ")."); | |
75 db->setLastErrorMessage("number of '?'s in statement string does not mat
ch argument count"); | |
76 return nullptr; | |
77 } | |
78 | |
79 for (unsigned i = 0; i < m_arguments.size(); ++i) { | |
80 result = statement.bindValue(i + 1, m_arguments[i]); | |
81 if (result == SQLResultFull) { | |
82 exceptionState.throwDOMException(QuotaExceededError, SQLError::quota
ExceededErrorMessage); | |
83 db->setLastErrorMessage("there was not enough remaining storage spac
e"); | |
84 return nullptr; | |
85 } | |
86 | |
87 if (result != SQLResultOk) { | |
88 exceptionState.throwDOMException(SQLDatabaseError, "Could not bind v
alue."); | |
89 db->setLastErrorMessage("could not bind value", result, database->la
stErrorMsg()); | |
90 return nullptr; | |
91 } | |
92 } | |
93 | |
94 RefPtrWillBeRawPtr<SQLResultSet> resultSet = SQLResultSet::create(); | |
95 | |
96 // Step so we can fetch the column names. | |
97 result = statement.step(); | |
98 if (result == SQLResultRow) { | |
99 int columnCount = statement.columnCount(); | |
100 SQLResultSetRowList* rows = resultSet->rows(); | |
101 | |
102 for (int i = 0; i < columnCount; i++) | |
103 rows->addColumn(statement.getColumnName(i)); | |
104 | |
105 do { | |
106 for (int i = 0; i < columnCount; i++) | |
107 rows->addResult(statement.getColumnValue(i)); | |
108 | |
109 result = statement.step(); | |
110 } while (result == SQLResultRow); | |
111 | |
112 if (result != SQLResultDone) { | |
113 exceptionState.throwDOMException(SQLDatabaseError, "Could not iterat
e results."); | |
114 db->setLastErrorMessage("could not iterate results", result, databas
e->lastErrorMsg()); | |
115 return nullptr; | |
116 } | |
117 } else if (result == SQLResultDone) { | |
118 // Didn't find anything, or was an insert. | |
119 if (db->lastActionWasInsert()) | |
120 resultSet->setInsertId(database->lastInsertRowID()); | |
121 } else if (result == SQLResultFull) { | |
122 // Quota error, the delegate will be asked for more space and this state
ment might be re-run. | |
123 exceptionState.throwDOMException(QuotaExceededError, SQLError::quotaExce
ededErrorMessage); | |
124 db->setLastErrorMessage("there was not enough remaining storage space"); | |
125 return nullptr; | |
126 } else if (result == SQLResultConstraint) { | |
127 exceptionState.throwDOMException(ConstraintError, "A constraint was viol
ated."); | |
128 db->setLastErrorMessage("statement failed due to a constraint failure"); | |
129 return nullptr; | |
130 } else { | |
131 exceptionState.throwDOMException(SQLDatabaseError, "Could not execute st
atement."); | |
132 db->setLastErrorMessage("could not execute statement", result, database-
>lastErrorMsg()); | |
133 return nullptr; | |
134 } | |
135 | |
136 resultSet->setRowsAffected(database->lastChanges()); | |
137 return resultSet.release(); | |
138 } | |
139 | |
140 } // namespace blink | |
OLD | NEW |