blob: adef7aed41eff687fe89e09d60519a05dd243fc3 [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
Mike Lockwoodb14e5882010-06-29 18:11:52 -040017#define LOG_TAG "SqliteStatement"
18
Mike Lockwood16864ba2010-05-11 17:16:59 -040019#include "SqliteStatement.h"
20#include "SqliteDatabase.h"
21
22#include <stdio.h>
23#include <sqlite3.h>
24
Mike Lockwood7850ef92010-05-14 10:10:36 -040025namespace android {
26
Mike Lockwood16864ba2010-05-11 17:16:59 -040027SqliteStatement::SqliteStatement(SqliteDatabase* db)
28 : mDatabaseHandle(db->getDatabaseHandle()),
29 mStatement(NULL),
30 mDone(false)
31{
32}
33
34SqliteStatement::~SqliteStatement() {
35 finalize();
36}
37
38bool SqliteStatement::prepare(const char* sql) {
39 return (sqlite3_prepare_v2(mDatabaseHandle, sql, -1, &mStatement, NULL) == 0);
40}
41
42bool SqliteStatement::step() {
43 int ret = sqlite3_step(mStatement);
44 if (ret == SQLITE_DONE) {
45 mDone = true;
46 return true;
47 }
48 return (ret == SQLITE_OK || ret == SQLITE_ROW);
49}
50
51void SqliteStatement::reset() {
52 sqlite3_reset(mStatement);
53 mDone = false;
54}
55
56void SqliteStatement::finalize() {
57 if (mStatement) {
58 sqlite3_finalize(mStatement);
59 mStatement = NULL;
60 }
61}
62
63void SqliteStatement::bind(int column, int value) {
64 sqlite3_bind_int(mStatement, column, value);
65}
66
67void SqliteStatement::bind(int column, const char* value) {
68 sqlite3_bind_text(mStatement, column, value, -1, SQLITE_TRANSIENT);
69}
70
71int SqliteStatement::getColumnInt(int column) {
72 return sqlite3_column_int(mStatement, column);
73}
74
75int64_t SqliteStatement::getColumnInt64(int column) {
76 return sqlite3_column_int64(mStatement, column);
77}
78
79const char* SqliteStatement::getColumnString(int column) {
80 return (const char *)sqlite3_column_text(mStatement, column);
81}
Mike Lockwood7850ef92010-05-14 10:10:36 -040082
83} // namespace android