Archive / / / / / Echo.Database.SQL.DatabaseConnection.vala
2008-11-27 23:48:48 UTC
previous next
/* vim: set noexpandtab tabstop=4 shiftwidth=4 nowrap textwidth=100 * * Echo Media Player * Copyright (C) 2008 Shane O'Connell * * [ The original file includes a copyright header in this location describing * the file as being released under the terms of the GNU General Public * License. It has been removed in order to display the file as part of the * archive. ] */ public class Echo.Database.SQL.DatabaseConnection : GLib.Object { private Sqlite.Database sqlite_db; public DatabaseConnection(string filename) throws SQLError { int rc = Sqlite.Database.open(filename, out this.sqlite_db); if (rc != Sqlite.OK) throw new SQLError.CANT_OPEN(this.sqlite_db.errmsg()); } public void run(string sql) throws SQLError { Sqlite.Statement stmt; int rc = sqlite_db.prepare(sql, -1, out stmt, null); if (rc != Sqlite.OK) throw new SQLError.CANT_PREPARE(this.sqlite_db.errmsg()); rc = stmt.step(); if (rc != Sqlite.DONE && rc != Sqlite.ROW) throw new SQLError.CANT_STEP(this.sqlite_db.errmsg()); } public Statement prepare(string sql) throws SQLError { Sqlite.Statement stmt; int rc = sqlite_db.prepare(sql, -1, out stmt, null); if (rc != Sqlite.OK) throw new SQLError.CANT_PREPARE(this.sqlite_db.errmsg()); return new Statement(#stmt); } public int64 last_rowid() { return sqlite_db.last_insert_rowid(); } public int64 get_row_count(string table) throws SQLError { var stmt = this.prepare("SELECT COUNT(*) FROM %s".printf(table)); if (!stmt.step()) throw new SQLError.CANT_GET_ROW_COUNT(this.sqlite_db.errmsg()); return stmt.get_int64_column(0); } }