blob: e1300b6d76f05de4b098ddcb660002d75f2c9e2e [file] [log] [blame]
Mike Lockwood56118b52010-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
Mike Lockwood8d3257a2010-05-14 10:10:36 -040023namespace android {
24
Mike Lockwood56118b52010-05-11 17:16:59 -040025SqliteStatement::SqliteStatement(SqliteDatabase* db)
26 : mDatabaseHandle(db->getDatabaseHandle()),
27 mStatement(NULL),
28 mDone(false)
29{
30}
31
32SqliteStatement::~SqliteStatement() {
33 finalize();
34}
35
36bool SqliteStatement::prepare(const char* sql) {
37 return (sqlite3_prepare_v2(mDatabaseHandle, sql, -1, &mStatement, NULL) == 0);
38}
39
40bool SqliteStatement::step() {
41 int ret = sqlite3_step(mStatement);
42 if (ret == SQLITE_DONE) {
43 mDone = true;
44 return true;
45 }
46 return (ret == SQLITE_OK || ret == SQLITE_ROW);
47}
48
49void SqliteStatement::reset() {
50 sqlite3_reset(mStatement);
51 mDone = false;
52}
53
54void SqliteStatement::finalize() {
55 if (mStatement) {
56 sqlite3_finalize(mStatement);
57 mStatement = NULL;
58 }
59}
60
61void SqliteStatement::bind(int column, int value) {
62 sqlite3_bind_int(mStatement, column, value);
63}
64
65void SqliteStatement::bind(int column, const char* value) {
66 sqlite3_bind_text(mStatement, column, value, -1, SQLITE_TRANSIENT);
67}
68
69int SqliteStatement::getColumnInt(int column) {
70 return sqlite3_column_int(mStatement, column);
71}
72
73int64_t SqliteStatement::getColumnInt64(int column) {
74 return sqlite3_column_int64(mStatement, column);
75}
76
77const char* SqliteStatement::getColumnString(int column) {
78 return (const char *)sqlite3_column_text(mStatement, column);
79}
Mike Lockwood8d3257a2010-05-14 10:10:36 -040080
81} // namespace android