Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 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 Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 17 | #define LOG_TAG "SqliteStatement" |
| 18 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 19 | #include "SqliteStatement.h" |
| 20 | #include "SqliteDatabase.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <sqlite3.h> |
| 24 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 27 | SqliteStatement::SqliteStatement(SqliteDatabase* db) |
| 28 | : mDatabaseHandle(db->getDatabaseHandle()), |
| 29 | mStatement(NULL), |
| 30 | mDone(false) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | SqliteStatement::~SqliteStatement() { |
| 35 | finalize(); |
| 36 | } |
| 37 | |
| 38 | bool SqliteStatement::prepare(const char* sql) { |
| 39 | return (sqlite3_prepare_v2(mDatabaseHandle, sql, -1, &mStatement, NULL) == 0); |
| 40 | } |
| 41 | |
| 42 | bool 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 | |
| 51 | void SqliteStatement::reset() { |
| 52 | sqlite3_reset(mStatement); |
| 53 | mDone = false; |
| 54 | } |
| 55 | |
| 56 | void SqliteStatement::finalize() { |
| 57 | if (mStatement) { |
| 58 | sqlite3_finalize(mStatement); |
| 59 | mStatement = NULL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void SqliteStatement::bind(int column, int value) { |
| 64 | sqlite3_bind_int(mStatement, column, value); |
| 65 | } |
| 66 | |
| 67 | void SqliteStatement::bind(int column, const char* value) { |
| 68 | sqlite3_bind_text(mStatement, column, value, -1, SQLITE_TRANSIENT); |
| 69 | } |
| 70 | |
| 71 | int SqliteStatement::getColumnInt(int column) { |
| 72 | return sqlite3_column_int(mStatement, column); |
| 73 | } |
| 74 | |
| 75 | int64_t SqliteStatement::getColumnInt64(int column) { |
| 76 | return sqlite3_column_int64(mStatement, column); |
| 77 | } |
| 78 | |
| 79 | const char* SqliteStatement::getColumnString(int column) { |
| 80 | return (const char *)sqlite3_column_text(mStatement, column); |
| 81 | } |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 82 | |
| 83 | } // namespace android |