//template <typename LTYPE> void pDB::list_query(pStringRef query, RowList &result) const { sqlite3_stmt *stmt; if (trace_) { std::cerr << "TRACE: " << query.str() << "\n"; } int rc = sqlite3_prepare_v2(db_, query.str().c_str(), -1, &stmt, NULL); if (rc != SQLITE_OK) { std::cerr << "sqlite error: " << query.str() << "\n"; std::cerr << sqlite3_errmsg(db_) << "\n"; exit(1); } while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { //typename LTYPE::value_type f; RowList::value_type f; for (int i = 0; i < sqlite3_column_count(stmt); i++) { pStringRef key = sqlite3_column_name(stmt, i); pStringRef val; char *cVal = (char*)sqlite3_column_text(stmt, i); if (cVal) val = cVal; f.set(key, val); } result.push_back(f); } if (trace_) { std::cerr << "TRACE: " << result.size() << " rows returned\n"; } sqlite3_finalize(stmt); }
std::string pDB::sql_select_single_string(pStringRef query) const { sqlite3_stmt *stmt; std::string result; int rc = sqlite3_prepare_v2(db_, query.str().c_str(), -1, &stmt, NULL); if (rc != SQLITE_OK) { std::cerr << "sqlite error: " << query.str() << "\n"; std::cerr << sqlite3_errmsg(db_) << "\n"; exit(1); } else if (trace_) { std::cerr << "TRACE: " << query.str() << std::endl; } rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) { result = (char*)sqlite3_column_text(stmt, 0); if (trace_) std::cerr << "TRACE: found 1 row\n"; } else if (trace_) { std::cerr << "TRACE: no rows found\n"; } sqlite3_finalize(stmt); return result; }
// handles escape quotes std::string pDB::sql_string(pStringRef val, bool allowNull) { if (!val.empty()) { std::string conv; int quotes = val.count('\''); // get room for doubling up the quotes conv.reserve(val.size()+quotes+2); // +2 is outer quotes conv.push_back('\''); for (pStringRef::iterator i = val.begin(); i != val.end(); ++i) { if (*i == '\'') { conv.push_back('\''); conv.push_back('\''); } else { conv.push_back(*i); } } conv.push_back('\''); return conv; } else { if (allowNull) return "NULL"; else return "''"; } }
void pModel::sql_execute(pStringRef ref, pStringRef query) { char *errMsg; if (trace_) { std::cerr << "TRACE: " << query.str() << std::endl; } int rc = sqlite3_exec(db_, query.begin(), NULL, NULL, &errMsg); if (rc != SQLITE_OK) { std::cerr << "sqlite error in " << ref.str() << ": " << query.str() << "\n" << errMsg << "\n"; exit(1); } }
void pDB::sql_execute(pStringRef query) const { char *errMsg; int rc = sqlite3_exec(db_, query.begin(), NULL, NULL, &errMsg); if (rc != SQLITE_OK) { std::cerr << "sqlite error: " << query.str() << "\n" << errMsg << "\n"; sqlite3_free(errMsg); exit(1); } else if (trace_) { std::cerr << "TRACE: " << query.str() << std::endl; } }
pModel::oid pModel::getNamespace(pStringRef ns) { std::stringstream ins; ins << "INSERT INTO namespace VALUES ('" << ns.str() << "')"; return sql_insert("getNamespace", ins.str().c_str()); }
pModel::oid pModel::getSourceModule(pStringRef realPath) { std::stringstream ins; ins << "INSERT INTO sourceModule VALUES ('" << realPath.str() << "', '')"; return sql_insert("getSourceModule", ins.str().c_str()); }