Created
January 18, 2011 20:27
-
-
Save sw17ch/785074 to your computer and use it in GitHub Desktop.
Test writing a bunch of blobs at once.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -Wall -O3 c_sqlite_test.c -lsqlite3 -o c_sqlite */ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <sqlite3.h> | |
/* Macros to simplify things. */ | |
#define BEGIN_TRANSACTION() do { int e; char em[1024]; \ | |
if((e = sqlite3_exec(hdl, BEGIN_TX, 0, 0, (char**)&em))) \ | |
hdl_err(e, em); \ | |
} while(0) | |
#define END_TRANSACTION() do { int e; char em[1024]; \ | |
if((e = sqlite3_exec(hdl, END_TX, 0, 0, (char**)&em))) \ | |
hdl_err(e, em); \ | |
} while(0) | |
/* If it's not defined already, define it. */ | |
#ifndef CHUNK_DIMENSION | |
#define CHUNK_DIMENSION 32 | |
#endif | |
/* SQL Statements */ | |
const char * TABLE_DROP = "DROP TABLE IF EXISTS chunks;"; | |
const char * TABLE_DEF = "CREATE TABLE chunks (x INTEGER, " | |
"y INTEGER, " | |
"z INTEGER, " | |
"d BLOB);"; | |
const char * BEGIN_TX = "BEGIN TRANSACTION;"; | |
const char * TABLE_INSERT = "INSERT INTO chunks VALUES( ?1, ?2, ?3, ?4);"; | |
const char * END_TX = "END TRANSACTION;"; | |
/* Generic error printing function. */ | |
void hdl_err(int e, char * m); | |
/* Setup the tables in the database. Drops table and then recreates. */ | |
void setup_tables(sqlite3 * hdl); | |
/* Insert a blob with the specified coordinates. */ | |
void insert(sqlite3 * hdl, int64_t x, int64_t y, int64_t z, void * blob, int size); | |
int main(int argc, char * argv[]) { | |
int i; | |
sqlite3 * hdl; | |
int count = 16; | |
/* Check if a count has been defined. */ | |
if (argc > 1) { | |
count = atoi(argv[1]); | |
} | |
sqlite3_open("c_test_db.sqlite3", &hdl); | |
setup_tables(hdl); | |
BEGIN_TRANSACTION(); | |
{ | |
uint64_t blob[CHUNK_DIMENSION][CHUNK_DIMENSION][CHUNK_DIMENSION]; | |
/* Insert a bunch of blobs. */ | |
for (i = 0; i < count; i++) { | |
int a,b,c; | |
/* Update each block. */ | |
for (a = 0; a < CHUNK_DIMENSION; a++) { | |
for (b = 0; b < CHUNK_DIMENSION; b++) { | |
for (c = 0; c < CHUNK_DIMENSION; c++) { | |
blob[a][b][c] = i; | |
} | |
} | |
} | |
insert(hdl, i, i + 1, i + 2, blob, sizeof(blob)); | |
} | |
} | |
END_TRANSACTION(); | |
sqlite3_close(hdl); | |
return 0; | |
} | |
void hdl_err(int e, char * m) { | |
fprintf(stderr, "[%d] %s\n", e, m); | |
} | |
void setup_tables(sqlite3 * hdl) { | |
int e; /* Error Value */ | |
char em[1024]; /* Error Message */ | |
char * ep = em; | |
bool failed = false; | |
if( (e = sqlite3_exec(hdl, TABLE_DROP, 0, 0, &ep))) { | |
hdl_err(e, ep); | |
failed = true; | |
} | |
else if((e = sqlite3_exec(hdl, TABLE_DEF, 0, 0, &ep))) { | |
hdl_err(e, ep); | |
failed = true; | |
} | |
if (failed) { | |
fprintf(stderr, "Unable to setup tables.\n"); | |
} | |
} | |
void insert(sqlite3 * hdl, int64_t x, int64_t y, int64_t z, void * blob, int size) { | |
int e; | |
sqlite3_stmt * stmt; | |
if ((e = sqlite3_prepare_v2(hdl, TABLE_INSERT, strlen(TABLE_INSERT) + 1, &stmt, 0))) | |
fprintf(stderr, "sqlite_3_prepare_v2: %d\n", e); | |
if ((e = sqlite3_bind_int64(stmt, 1, x))) | |
fprintf(stderr, "sqlite3_bind_int64: %d\n", e); | |
if ((e = sqlite3_bind_int64(stmt, 2, y))) | |
fprintf(stderr, "sqlite3_bind_int64: %d\n", e); | |
if ((e = sqlite3_bind_int64(stmt, 3, z))) | |
fprintf(stderr, "sqlite3_bind_int64: %d\n", e); | |
if ((e = sqlite3_bind_blob(stmt, 4, (void*) blob, size, 0))) | |
fprintf(stderr, "sqlite3_bind_blob: %d\n", e); | |
if (SQLITE_DONE != (e = sqlite3_step(stmt))) | |
fprintf(stderr, "sqlite3_step: %d\n", e); | |
if ((e = sqlite3_finalize(stmt))) | |
fprintf(stderr, "sqlite3_finalize: %d\n", e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment