// Truck Computer Dooom! // // Douglas Thrift // // $Id$ #ifndef _sqlite3_hpp_ #define _sqlite3_hpp_ #include #include namespace SQLite3 { class Error : public std::exception { std::string message; public: Error(::sqlite3 *connection) : message(::sqlite3_errmsg(connection)) {} virtual ~Error() throw() {} virtual const char *what() const throw() { return message.c_str(); } }; inline int CheckError(int code, ::sqlite3 *connection) { if (code != SQLITE_OK) throw Error(connection); return code; } struct Statement; class Connection { ::sqlite3 *connection; public: Connection(const std::string &db); ~Connection(); Statement Prepare(const std::string &statement) const; }; struct Null {}; class Statement; class Results { ::sqlite3 *connection; ::sqlite3_stmt *statement; enum { Begin = -1, False = 0, True = 1, Ignore = 2 } next; Results(::sqlite3 *connection, ::sqlite3_stmt *statement); public: bool MoveNext(); template Type Get(int index); friend class Statement; }; class Statement { ::sqlite3 *connection; ::sqlite3_stmt *statement; Statement(::sqlite3 *connection, const std::string &statement); public: ~Statement(); void Bind(int index, double value) const; void Bind(int index, int value) const; void Bind(int index, int64_t value) const; void Bind(int index, const Null &value) const; void Bind(int index, const std::string &value) const; Results Execute() const; template inline Results Execute(const Arg0 &arg0) const { Bind(0, arg0); return Execute(); } template inline Results Execute(const Arg0 &arg0, const Arg1 &arg1) const { Bind(0, arg0); Bind(1, arg1); return Execute(); } template inline Results Execute(const Arg0 &arg0, const Arg1 &arg1, const Arg2 &arg2) const { Bind(0, arg0); Bind(1, arg1); Bind(2, arg2); return Execute(); } template inline Results Execute(const Arg0 &arg0, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) const { Bind(0, arg0); Bind(1, arg1); Bind(2, arg2); Bind(3, arg3); return Execute(); } template Results Execute(const Arg0 &arg0, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4) const { Bind(0, arg0); Bind(1, arg1); Bind(2, arg2); Bind(3, arg3); Bind(4, arg4); return Execute(); } template inline Results Execute(const Arg0 &arg0, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5) const { Bind(0, arg0); Bind(1, arg1); Bind(2, arg2); Bind(3, arg3); Bind(4, arg4); Bind(5, arg5); return Execute(); } template inline Results Execute(const Arg0 &arg0, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5, const Arg6 &arg6) const { Bind(0, arg0); Bind(1, arg1); Bind(2, arg2); Bind(3, arg3); Bind(4, arg4); Bind(5, arg5); Bind(6, arg6); return Execute(); } void Clear() const; friend Statement Connection::Prepare(const std::string &statement) const; }; } #endif//_sqlite3_hpp_