blob: e568928f577859f06bfe700ef4c0d4350ce72196 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SqliteStatement.h"
18#include "SqliteDatabase.h"
19
20#include <stdio.h>
21#include <sqlite3.h>
22
23SqliteStatement::SqliteStatement(SqliteDatabase* db)
24 : mDatabaseHandle(db->getDatabaseHandle()),
25 mStatement(NULL),
26 mDone(false)
27{
28}
29
30SqliteStatement::~SqliteStatement() {
31 finalize();
32}
33
34bool SqliteStatement::prepare(const char* sql) {
35 return (sqlite3_prepare_v2(mDatabaseHandle, sql, -1, &mStatement, NULL) == 0);
36}
37
38bool SqliteStatement::step() {
39 int ret = sqlite3_step(mStatement);
40 if (ret == SQLITE_DONE) {
41 mDone = true;
42 return true;
43 }
44 return (ret == SQLITE_OK || ret == SQLITE_ROW);
45}
46
47void SqliteStatement::reset() {
48 sqlite3_reset(mStatement);
49 mDone = false;
50}
51
52void SqliteStatement::finalize() {
53 if (mStatement) {
54 sqlite3_finalize(mStatement);
55 mStatement = NULL;
56 }
57}
58
59void SqliteStatement::bind(int column, int value) {
60 sqlite3_bind_int(mStatement, column, value);
61}
62
63void SqliteStatement::bind(int column, const char* value) {
64 sqlite3_bind_text(mStatement, column, value, -1, SQLITE_TRANSIENT);
65}
66
67int SqliteStatement::getColumnInt(int column) {
68 return sqlite3_column_int(mStatement, column);
69}
70
71int64_t SqliteStatement::getColumnInt64(int column) {
72 return sqlite3_column_int64(mStatement, column);
73}
74
75const char* SqliteStatement::getColumnString(int column) {
76 return (const char *)sqlite3_column_text(mStatement, column);
77}