// File Size Extension static void reserve_size(FILE* fp, unsigned int size){ if( ::fseek(fp, size-1, SEEK_SET) != 0 ) throw db_exception("File Seek Error."); if( ::fputc(0, fp) != 0 ) throw db_exception("File Resize Error."); }
static FILE* open(const char* filepath, const char* desc_str, _Header& m_header){ FILE* m_fp = NULL; /* Create new DB File if not exist. */ if ( !::roast::file::is_exist(filepath) ) { //::roast::file::touch(filepath); FILE *fp_initial = ::fopen(filepath, "w+b"); if ( fp_initial == NULL ) throw db_exception(::std::string("Initial DB File Create Error. ")+filepath); /* Create Initial Header Info */ _Header header_initial; ::memset(&header_initial, 0, sizeof(header_initial)); //::memcpy(header_initial.desc_str, desc_str, strlen(desc_str)); ::strcpy(header_initial.desc_str, desc_str); /* Write Initial Header */ if ( fwrite(&header_initial, 1, sizeof(header_initial), fp_initial) != sizeof(header_initial) ) throw db_exception(::std::string("Initial DB File Create Error. ")+filepath); fclose(fp_initial); } /* Open DB File */ m_fp = ::fopen(filepath, "r+b"); if ( m_fp == NULL ) throw db_exception(::std::string("DB File Open Error. ")+filepath); /* Read Header */ if ( ::fread(&m_header, 1, sizeof(m_header), m_fp) != sizeof(m_header) ) throw db_exception(::std::string("Invalid or broken DB File. ")+filepath); /* Check Header */ if ( ::memcmp(m_header.desc_str, desc_str, strlen(desc_str)) != 0 ) throw db_exception(::std::string("Invalid or broken DB File. ")+filepath); /* Seek to Tail */ ::fseek(m_fp, 0, SEEK_END); return m_fp; }
/** * * @param tablename */ sqlite(std::string database = schemadb) : zErrMsg(0) , rc(0) , db_open(0) { rc = sqlite3_open(database.c_str(), &db); if (rc) { throw db_exception(1, sqlite3_errmsg(db)); sqlite3_close(db); } else { db_open = 1; LOG_DEBUG("Database open: [" << database << "]"); } execute("PRAGMA foreign_keys = ON;"); // enable support for foreign keys constraints }
/** * * @param s_exe */ int execute(const std::string& s_exe) { LOG_DEBUG("sqlite query: [" << s_exe << "]"); rc = sqlite3_get_table(db, /* An open database */ s_exe.c_str(), /* SQL to be executed */ &result, /* Result written to a char *[] that this points to */ &nrow, /* Number of result rows written here */ &ncol, /* Number of result columns written here */ &zErrMsg /* Error msg written here */ ); if (vcol_head.size() > 0) vcol_head.clear(); if (vdata.size() > 0) vdata.clear(); if (m_data.size() > 0) m_data.clear(); if (rc == SQLITE_OK) { for (int i = 0; i < ncol; i++) { vcol_head.push_back(result[i]); /* First row heading */ } for (int i = 0; i < ncol * nrow; i++) vdata.push_back(result[ncol+i]); for (int i = 0; i < nrow; i++) { std::vector<std::string > row; for (int j = 0; j < ncol; j++) row.push_back( result[ (i + 1) * ncol + j] ); m_data.push_back(row); } } else throw db_exception(sqlite3_errmsg(db)); sqlite3_free_table(result); return rc; }
// File Seek To Tail static void seek_to_last(FILE* fp){ if( ::fseek(fp, 0, SEEK_END) != 0 ) throw db_exception("File Seek Error."); }
static void read(T* p, FILE* fp){ if ( ::fread(p, sizeof(T), 1, fp) != 1 ) throw db_exception("File Read Error."); }
static void write(const T* p, FILE* fp){ // if ( ::fwrite((void*)p, sizeof(T), 1, fp) != 1 ) if ( ::fwrite(p, sizeof(T), 1, fp) != 1 ) throw db_exception("File Write Error."); }