sqlite-lean

Sqlite3 bindings for lean4 (mirror)

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
#include <lean/lean.h>
#include <sqlite3.h>
#include <stdio.h>

typedef struct {
  sqlite3_stmt* stmt;
  int cols;
} cursor_t;

lean_external_class* g_sqlite_object_external_class = NULL;
lean_external_class* g_sqlite_cursor_external_class = NULL;

void noop_foreach(void* mod, b_lean_obj_arg fn) {}

lean_object* box_connection(sqlite3 *conn) {
  return lean_alloc_external(g_sqlite_object_external_class, conn);
}

sqlite3* unbox_connection(lean_object* o) {
  return (sqlite3*) lean_get_external_data(o);
}

lean_object* box_cursor(cursor_t* cursor) {
  return lean_alloc_external(g_sqlite_cursor_external_class, cursor);
}

cursor_t* unbox_cursor(lean_object* o) {
  return (cursor_t*) lean_get_external_data(o);
}

void connection_finalize(void* conn) {
  if (!conn) return;

  sqlite3_close(conn);
}

void cursor_finalize(void* cursor_ptr) {
  cursor_t* cursor = (cursor_t*) cursor_ptr;

  if (!cursor->stmt) return;
  sqlite3_finalize(cursor->stmt);

  if (!cursor) return;
  free(cursor);
}

lean_obj_res lean_sqlite_initialize() {
  g_sqlite_object_external_class = lean_register_external_class(connection_finalize, noop_foreach);
  g_sqlite_cursor_external_class = lean_register_external_class(cursor_finalize, noop_foreach);
  return lean_io_result_mk_ok(lean_box(0));
}

lean_obj_res lean_sqlite_open(b_lean_obj_arg path) {
  const char* path_str = lean_string_cstr(path);
  sqlite3* conn = malloc(sizeof(sqlite3*));

  int c = sqlite3_open_v2(path_str, &conn, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);

  if (c == SQLITE_OK)
    return lean_io_result_mk_ok(box_connection(conn));

  lean_object *err = lean_mk_string(sqlite3_errmsg(conn));

  sqlite3_close(conn);

  return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}

lean_obj_res lean_sqlite_prepare(b_lean_obj_arg conn_box, b_lean_obj_arg query_str) {
  sqlite3* conn = unbox_connection(conn_box);
  const char* query = lean_string_cstr(query_str);

  cursor_t* cursor = malloc(sizeof(cursor_t*));

  int c = sqlite3_prepare_v2(conn, query, -1, &cursor->stmt, NULL);

  if (c != SQLITE_OK) {
    lean_object* err = lean_mk_string(sqlite3_errmsg(conn));
    free(cursor);

    lean_object* res = lean_alloc_ctor(0, 1, 0);
    lean_ctor_set(res, 0, err);
    return lean_io_result_mk_ok(res);
  }

  cursor->cols = (uint32_t) sqlite3_column_count(cursor->stmt);

  lean_object* res = lean_alloc_ctor(1, 1, 0);
  lean_ctor_set(res, 0, box_cursor(cursor));
  return lean_io_result_mk_ok(res);
}

lean_obj_res lean_sqlite_cursor_column_text(b_lean_obj_arg cursor_box, uint32_t col) {
  cursor_t* cursor = unbox_cursor(cursor_box);

  const unsigned char* text = sqlite3_column_text(cursor->stmt, col);

  lean_object* s = lean_mk_string((const char*) text);

  return lean_io_result_mk_ok(s);
}

lean_obj_res lean_sqlite_cursor_column_int(b_lean_obj_arg cursor_box, uint32_t col) {
  cursor_t* cursor = unbox_cursor(cursor_box);

  const int integer = sqlite3_column_int(cursor->stmt, col);

  return lean_io_result_mk_ok(lean_box(integer));
}

lean_obj_res lean_sqlite_cursor_step(b_lean_obj_arg cursor_box) {
  cursor_t* cursor = unbox_cursor(cursor_box);

  int c = sqlite3_step(cursor->stmt);

  if (c == SQLITE_ROW) {
    return lean_io_result_mk_ok(lean_box(1));
  }

  if (c == SQLITE_DONE) {
    return lean_io_result_mk_ok(lean_box(0));
  }

  lean_object* err = lean_mk_string(sqlite3_errmsg(sqlite3_db_handle(cursor->stmt)));
  return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}

lean_obj_res lean_sqlite_cursor_reset(b_lean_obj_arg cursor_box) {
  cursor_t* cursor = unbox_cursor(cursor_box);

  int c = sqlite3_reset(cursor->stmt);

  if (c == SQLITE_OK)
    return lean_io_result_mk_ok(lean_box(0));

  lean_object *err = lean_mk_string(sqlite3_errmsg(sqlite3_db_handle(cursor->stmt)));
  return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}

uint32_t lean_sqlite_cursor_columns_count(b_lean_obj_arg cursor_box) {
  cursor_t* cursor = unbox_cursor(cursor_box);

  return cursor->cols;
}