Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. /* gcc -Wall -O3 c_sqlite_test.c -lsqlite3 -o c_sqlite */
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7. #include <sqlite3.h>
  8.  
  9. /* Macros to simplify things. */
  10. #define BEGIN_TRANSACTION() do { int e; char em[1024]; \
  11. if((e = sqlite3_exec(hdl, BEGIN_TX, 0, 0, (char**)&em))) \
  12. hdl_err(e, em); \
  13. } while(0)
  14. #define END_TRANSACTION() do { int e; char em[1024]; \
  15. if((e = sqlite3_exec(hdl, END_TX, 0, 0, (char**)&em))) \
  16. hdl_err(e, em); \
  17. } while(0)
  18.  
  19. /* If it's not defined already, define it. */
  20. #ifndef CHUNK_DIMENSION
  21. #define CHUNK_DIMENSION 32
  22. #endif
  23.  
  24. /* SQL Statements */
  25. const char * TABLE_DROP = "DROP TABLE IF EXISTS chunks;";
  26. const char * TABLE_DEF = "CREATE TABLE chunks (x INTEGER, "
  27. "y INTEGER, "
  28. "z INTEGER, "
  29. "d BLOB);";
  30. const char * BEGIN_TX = "BEGIN TRANSACTION;";
  31. const char * TABLE_INSERT = "INSERT INTO chunks VALUES( ?1, ?2, ?3, ?4);";
  32. const char * END_TX = "END TRANSACTION;";
  33.  
  34. /* Generic error printing function. */
  35. void hdl_err(int e, char * m);
  36.  
  37. /* Setup the tables in the database. Drops table and then recreates. */
  38. void setup_tables(sqlite3 * hdl);
  39.  
  40. /* Insert a blob with the specified coordinates. */
  41. void insert(sqlite3 * hdl, int64_t x, int64_t y, int64_t z, void * blob, int size);
  42.  
  43. int main(int argc, char * argv[]) {
  44. int i;
  45. sqlite3 * hdl;
  46.  
  47. int count = 16;
  48.  
  49. /* Check if a count has been defined. */
  50. if (argc > 1) {
  51. count = atoi(argv[1]);
  52. }
  53.  
  54. sqlite3_open("c_test_db.sqlite3", &hdl);
  55. setup_tables(hdl);
  56.  
  57. BEGIN_TRANSACTION();
  58. {
  59. uint64_t blob[CHUNK_DIMENSION][CHUNK_DIMENSION][CHUNK_DIMENSION];
  60.  
  61. /* Insert a bunch of blobs. */
  62. for (i = 0; i < count; i++) {
  63. int a,b,c;
  64.  
  65. /* Update each block. */
  66. for (a = 0; a < CHUNK_DIMENSION; a++) {
  67. for (b = 0; b < CHUNK_DIMENSION; b++) {
  68. for (c = 0; c < CHUNK_DIMENSION; c++) {
  69. blob[a][b][c] = i;
  70. }
  71. }
  72. }
  73.  
  74. insert(hdl, i, i + 1, i + 2, blob, sizeof(blob));
  75. }
  76. }
  77. END_TRANSACTION();
  78.  
  79. sqlite3_close(hdl);
  80.  
  81. return 0;
  82. }
  83.  
  84. void hdl_err(int e, char * m) {
  85. fprintf(stderr, "[%d] %s\n", e, m);
  86. }
  87.  
  88. void setup_tables(sqlite3 * hdl) {
  89. int e; /* Error Value */
  90. char em[1024]; /* Error Message */
  91. char * ep = em;
  92.  
  93. bool failed = false;
  94.  
  95. if( (e = sqlite3_exec(hdl, TABLE_DROP, 0, 0, &ep))) {
  96. hdl_err(e, ep);
  97. failed = true;
  98. }
  99. else if((e = sqlite3_exec(hdl, TABLE_DEF, 0, 0, &ep))) {
  100. hdl_err(e, ep);
  101. failed = true;
  102. }
  103.  
  104. if (failed) {
  105. fprintf(stderr, "Unable to setup tables.\n");
  106. }
  107. }
  108.  
  109. void insert(sqlite3 * hdl, int64_t x, int64_t y, int64_t z, void * blob, int size) {
  110. int e;
  111. sqlite3_stmt * stmt;
  112.  
  113. if ((e = sqlite3_prepare_v2(hdl, TABLE_INSERT, strlen(TABLE_INSERT) + 1, &stmt, 0)))
  114. fprintf(stderr, "sqlite_3_prepare_v2: %d\n", e);
  115.  
  116. if ((e = sqlite3_bind_int64(stmt, 1, x)))
  117. fprintf(stderr, "sqlite3_bind_int64: %d\n", e);
  118.  
  119. if ((e = sqlite3_bind_int64(stmt, 2, y)))
  120. fprintf(stderr, "sqlite3_bind_int64: %d\n", e);
  121.  
  122. if ((e = sqlite3_bind_int64(stmt, 3, z)))
  123. fprintf(stderr, "sqlite3_bind_int64: %d\n", e);
  124.  
  125. if ((e = sqlite3_bind_blob(stmt, 4, (void*) blob, size, 0)))
  126. fprintf(stderr, "sqlite3_bind_blob: %d\n", e);
  127.  
  128. if (SQLITE_DONE != (e = sqlite3_step(stmt)))
  129. fprintf(stderr, "sqlite3_step: %d\n", e);
  130.  
  131. if ((e = sqlite3_finalize(stmt)))
  132. fprintf(stderr, "sqlite3_finalize: %d\n", e);
  133. }
Add Comment
Please, Sign In to add comment