[Gcblue-commits] gcb_wx/src/sqlite sqlite_connection.cpp,NONE,1.1 sqlite_internal.cpp,NONE,1.1 sqlit
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-08-08 00:31:44
|
Update of /cvsroot/gcblue/gcb_wx/src/sqlite In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21227/src/sqlite Added Files: sqlite_connection.cpp sqlite_internal.cpp sqlite_reader.cpp Log Message: --- NEW FILE: sqlite_internal.cpp --- /* Copyright (c) 2004 Cory Nelson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <cstdarg> #ifndef NULL #define NULL 0 #endif #include <sqlite.h> #include "sqlite/sqlite_internal.h" void *_sqlite_open(const char *db) { return (void*)sqlite_open(db, 0, NULL); } void _sqlite_close(void *db) { sqlite_close((sqlite*)db); } int _sqlite_exec_vprintf(void *db, const char *sql, int (*cb)(void*,int,char**, char**), void *arg, va_list args) { return sqlite_exec_vprintf((sqlite*)db, sql, cb, arg, NULL, args); } int _sqlite_compile(void *db, const char *sql, void **vm) { const char *tail; return sqlite_compile((sqlite*)db, sql, &tail, (sqlite_vm**)vm, NULL); } int _sqlite_step(void *vm, int *argc, const char ***argv) { const char **colnames; return sqlite_step((sqlite_vm*)vm, argc, argv, &colnames); } int _sqlite_finalize(void *vm) { return sqlite_finalize((sqlite_vm*)vm, NULL); } const char *_sqlite_error_string(int err) { return sqlite_error_string(err); } int _sqlite_last_insert_rowid(void *db) { return sqlite_last_insert_rowid((sqlite*)db); } int _sqlite_changes(void *db) { return sqlite_changes((sqlite*)db); } char *_sqlite_vmprintf(const char *fmt, va_list args) { return sqlite_vmprintf(fmt, args); } void _sqlite_freemem(char *str) { sqlite_freemem(str); } --- NEW FILE: sqlite_reader.cpp --- /* Copyright (c) 2004 Cory Nelson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <string> #include <stdexcept> #include <cstddef> using namespace std; #include "sqlite/sqlite_plus.h" #include "sqlite/sqlite_internal.h" namespace sqlite { reader::reader() { this->vm=NULL; } bool reader::read() { if(!this->vm) throw runtime_error("can't read from an unopened reader"); int ret=_sqlite_step(this->vm, &this->argc, &this->argv); switch(ret) { case SQLITE_ROW: return true; case SQLITE_DONE: return false; default: throw runtime_error(_sqlite_error_string(ret)); } } void reader::close() { if(this->vm) { int ret=_sqlite_finalize(this->vm); this->vm=NULL; if(ret!=SQLITE_OK) throw runtime_error(_sqlite_error_string(ret)); } } int reader::getint(int index) { string s=this->getstring(index); return atoi(s.c_str()); } double reader::getdouble(int index) { string s=this->getstring(index); return atof(s.c_str()); } std::string reader::getstring(int index) { if(index>(argc-1)) throw out_of_range("index is out of range"); return argv[index]?argv[index]:""; } std::string reader::operator[](int index) { return this->getstring(index); } }; --- NEW FILE: sqlite_connection.cpp --- /* Copyright (c) 2004 Cory Nelson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <string> #include <stdexcept> #include <sstream> #include <cstdarg> using namespace std; #include "sqlite/sqlite_plus.h" #include "sqlite/sqlite_internal.h" static int es_callback(void *arg, int argc, char *argv[], char **colnames) { if(argc>0 && argv[0]!=NULL) *((string*)arg)=argv[0]; return 0; } namespace sqlite { connection::connection() { this->internal=NULL; } connection::connection(const char *db) { this->internal=NULL; this->open(db); } connection::~connection() { this->close(); } void connection::open(const char *db) { this->internal=_sqlite_open(db); } void connection::close() { if(this->internal) { _sqlite_close(this->internal); this->internal=NULL; } } int connection::insertid() { if(!this->internal) throw runtime_error("must have an open connection to get insert id"); return _sqlite_last_insert_rowid(this->internal); } reader connection::executereader(const char *fmt, va_list args) { char *sql=_sqlite_vmprintf(fmt, args); reader r; int ret=_sqlite_compile(this->internal, sql, &r.vm); if(ret!=SQLITE_OK) throw runtime_error(_sqlite_error_string(ret)); _sqlite_freemem(sql); return r; } int connection::executenonquery(const char *fmt, ...) { va_list args; va_start(args, fmt); reader r=this->executereader(fmt, args); va_end(args); r.read(); r.close(); return _sqlite_changes(this->internal); } string connection::executescalar(const char *fmt, ...) { va_list args; va_start(args, fmt); reader r=this->executereader(fmt, args); va_end(args); string str; if(r.read()) str=r[0]; r.close(); return str; } reader connection::executereader(const char *fmt, ...) { va_list args; va_start(args, fmt); reader r=this->executereader(fmt, args); va_end(args); return r; } }; |