1/*
2 * Copyright (C) 2006, 2007, 2008 Apple 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "SQLValue.h"
29#include "SQLiteDatabase.h"
30
31struct sqlite3_stmt;
32
33namespace WebCore {
34
35class SQLiteStatement {
36 WTF_MAKE_NONCOPYABLE(SQLiteStatement); WTF_MAKE_FAST_ALLOCATED;
37public:
38 WEBCORE_EXPORT SQLiteStatement(SQLiteDatabase&, const String&);
39 WEBCORE_EXPORT ~SQLiteStatement();
40
41 WEBCORE_EXPORT int prepare();
42 WEBCORE_EXPORT int bindBlob(int index, const void* blob, int size);
43 WEBCORE_EXPORT int bindBlob(int index, const String&);
44 WEBCORE_EXPORT int bindText(int index, const String&);
45 WEBCORE_EXPORT int bindInt(int index, int);
46 WEBCORE_EXPORT int bindInt64(int index, int64_t);
47 WEBCORE_EXPORT int bindDouble(int index, double);
48 WEBCORE_EXPORT int bindNull(int index);
49 WEBCORE_EXPORT int bindValue(int index, const SQLValue&);
50 WEBCORE_EXPORT unsigned bindParameterCount() const;
51
52 WEBCORE_EXPORT int step();
53 WEBCORE_EXPORT int finalize();
54 WEBCORE_EXPORT int reset();
55
56 int prepareAndStep() { if (int error = prepare()) return error; return step(); }
57
58 // prepares, steps, and finalizes the query.
59 // returns true if all 3 steps succeed with step() returning SQLITE_DONE
60 // returns false otherwise
61 WEBCORE_EXPORT bool executeCommand();
62
63 // prepares, steps, and finalizes.
64 // returns true is step() returns SQLITE_ROW
65 // returns false otherwise
66 bool returnsAtLeastOneResult();
67
68 bool isExpired();
69
70 // Returns -1 on last-step failing. Otherwise, returns number of rows
71 // returned in the last step()
72 int columnCount();
73
74 WEBCORE_EXPORT bool isColumnNull(int col);
75 WEBCORE_EXPORT bool isColumnDeclaredAsBlob(int col);
76 String getColumnName(int col);
77 SQLValue getColumnValue(int col);
78 WEBCORE_EXPORT String getColumnText(int col);
79 WEBCORE_EXPORT double getColumnDouble(int col);
80 WEBCORE_EXPORT int getColumnInt(int col);
81 WEBCORE_EXPORT int64_t getColumnInt64(int col);
82 WEBCORE_EXPORT String getColumnBlobAsString(int col);
83 WEBCORE_EXPORT void getColumnBlobAsVector(int col, Vector<char>&);
84 WEBCORE_EXPORT void getColumnBlobAsVector(int col, Vector<uint8_t>&);
85
86 bool returnTextResults(int col, Vector<String>&);
87 bool returnIntResults(int col, Vector<int>&);
88 bool returnInt64Results(int col, Vector<int64_t>&);
89 bool returnDoubleResults(int col, Vector<double>&);
90
91 SQLiteDatabase& database() { return m_database; }
92
93 const String& query() const { return m_query; }
94
95private:
96 SQLiteDatabase& m_database;
97 String m_query;
98 sqlite3_stmt* m_statement;
99#ifndef NDEBUG
100 bool m_isPrepared;
101#endif
102};
103
104} // namespace WebCore
105