5 Commits
0.0.1 ... 0.0.3

Author SHA1 Message Date
262aacc0a9 ownership bugfix and vbump 2020-01-27 09:05:11 +01:00
2386599913 examples: remove fsanitize 2020-01-26 22:23:16 +01:00
21e74f82e3 docs: update for 0.0.2 2020-01-26 22:15:22 +01:00
9743ac15d9 memory bugfix, vbump 2020-01-26 22:14:25 +01:00
e239ac2b7f docs: better example 2020-01-26 16:06:06 +01:00
4 changed files with 17 additions and 15 deletions

View File

@@ -6,7 +6,7 @@ everything, but it tries to be useful.
## Installation ## Installation
```clojure ```clojure
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.1") (load "https://veitheller.de/git/carpentry/sqlite3@0.0.3")
``` ```
## Usage ## Usage

View File

@@ -32,7 +32,7 @@
<p>is a simple high-level wrapper around SQLite3. It doesnt intend <p>is a simple high-level wrapper around SQLite3. It doesnt intend
to wrap everything, but it tries to be useful.</p> to wrap everything, but it tries to be useful.</p>
<h2>Installation</h2> <h2>Installation</h2>
<pre><code class="language-clojure">(load &quot;https://veitheller.de/git/carpentry/sqlite3@0.0.1&quot;) <pre><code class="language-clojure">(load &quot;https://veitheller.de/git/carpentry/sqlite3@0.0.3&quot;)
</code></pre> </code></pre>
<h2>Usage</h2> <h2>Usage</h2>
<p>The module <code>SQLite3</code> provides facilities for opening, closing, and querying <p>The module <code>SQLite3</code> provides facilities for opening, closing, and querying
@@ -43,7 +43,8 @@ databases.</p>
; ignore that ; ignore that
(let-do [db (Result.unsafe-from-success (SQLite3.open &quot;db&quot;))] (let-do [db (Result.unsafe-from-success (SQLite3.open &quot;db&quot;))]
; we can prepare statements ; we can prepare statements
(println* &amp;(SQLite3.query &amp;db &quot;INSERT INTO mytable VALUES (?1, ?2);&quot; (ignore
(SQLite3.query &amp;db &quot;INSERT INTO mytable VALUES (?1, ?2);&quot;
&amp;[(to-sqlite3 @&quot;hello&quot;) (to-sqlite3 100)])) &amp;[(to-sqlite3 @&quot;hello&quot;) (to-sqlite3 100)]))
; and query things ; and query things
(println* &amp;(SQLite3.query &amp;db &quot;SELECT * from mytable;&quot; &amp;[])) (println* &amp;(SQLite3.query &amp;db &quot;SELECT * from mytable;&quot; &amp;[]))

View File

@@ -7,7 +7,7 @@ to wrap everything, but it tries to be useful.
## Installation ## Installation
```clojure ```clojure
(load \"https://veitheller.de/git/carpentry/sqlite3@0.0.1\") (load \"https://veitheller.de/git/carpentry/sqlite3@0.0.3\")
``` ```
## Usage ## Usage
@@ -22,7 +22,8 @@ databases.
; ignore that ; ignore that
(let-do [db (Result.unsafe-from-success (SQLite3.open \"db\"))] (let-do [db (Result.unsafe-from-success (SQLite3.open \"db\"))]
; we can prepare statements ; we can prepare statements
(println* &(SQLite3.query &db \"INSERT INTO mytable VALUES (?1, ?2);\" (ignore
(SQLite3.query &db \"INSERT INTO mytable VALUES (?1, ?2);\"
&[(to-sqlite3 @\"hello\") (to-sqlite3 100)])) &[(to-sqlite3 @\"hello\") (to-sqlite3 100)]))
; and query things ; and query things
(println* &(SQLite3.query &db \"SELECT * from mytable;\" &[])) (println* &(SQLite3.query &db \"SELECT * from mytable;\" &[]))
@@ -144,7 +145,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
(register open- (Fn [&SQLite (Ptr Char)] Int) "SQLite3_open_c") (register open- (Fn [&SQLite (Ptr Char)] Int) "SQLite3_open_c")
(private exec-) (private exec-)
(hidden exec-) (hidden exec-)
(register exec- (Fn [&SQLite (Ptr Char) (Array SQLiteColumn)] SQLiteRes) "SQLite3_exec_c") (register exec- (Fn [&SQLite (Ptr Char) &(Array SQLiteColumn)] SQLiteRes) "SQLite3_exec_c")
(private error-) (private error-)
(hidden error-) (hidden error-)
(register error- (Fn [SQLite] (Ptr Char)) "SQLite3_error") (register error- (Fn [SQLite] (Ptr Char)) "SQLite3_error")
@@ -164,7 +165,7 @@ If it fails, we return an error message using `Result.Error`.")
If it fails, we return an error message using `Result.Error`.") If it fails, we return an error message using `Result.Error`.")
(defn query [db s p] (defn query [db s p]
(let [r (exec- db (cstr s) (Array.copy-map &(fn [x] (Type.to-sqlite3-internal @x)) p))] (let [r (exec- db (cstr s) &(Array.copy-map &(fn [x] (Type.to-sqlite3-internal @x)) p))]
(if (SQLiteRes.ok? &r) (if (SQLiteRes.ok? &r)
(Result.Success (SQLiteRes.to-array r)) (Result.Success (SQLiteRes.to-array r))
(Result.Error (from-cstr (SQLiteRes.error r)))))) (Result.Error (from-cstr (SQLiteRes.error r))))))

View File

@@ -176,14 +176,14 @@ const char* SQLite3_exec_internal(sqlite3_stmt* s, SQLiteRows* rows) {
break; break;
case SQLITE_TEXT: { case SQLITE_TEXT: {
len = sqlite3_column_bytes(s, i); len = sqlite3_column_bytes(s, i);
c->s = CARP_MALLOC(len); c->s = CARP_MALLOC(len+1);
memcpy(c->s, sqlite3_column_text(s, i), len); memcpy(c->s, sqlite3_column_text(s, i), len);
c->s[len] = '\0'; c->s[len] = '\0';
break; break;
} }
case SQLITE_BLOB: { case SQLITE_BLOB: {
len = sqlite3_column_bytes(s, i); len = sqlite3_column_bytes(s, i);
c->s = CARP_MALLOC(len); c->s = CARP_MALLOC(len+1);
memcpy(c->s, sqlite3_column_blob(s, i), len); memcpy(c->s, sqlite3_column_blob(s, i), len);
c->s[len] = '\0'; c->s[len] = '\0';
break; break;
@@ -218,12 +218,12 @@ static const char* SQLite3_exec_ignore(sqlite3_stmt* s) {
return ret; return ret;
} }
const char* SQLite3_bind(sqlite3_stmt* s, Array p) { const char* SQLite3_bind(sqlite3_stmt* s, Array* p) {
int res; int res;
const char* err = NULL; const char* err = NULL;
for (int i = 0; i < p.len; i++) { for (int i = 0; i < p->len; i++) {
SQLiteColumn val = ((SQLiteColumn*)p.data)[i]; SQLiteColumn val = ((SQLiteColumn*)p->data)[i];
switch (val.tag) { switch (val.tag) {
case SQLITE_NULL: case SQLITE_NULL:
@@ -252,7 +252,7 @@ const char* SQLite3_bind(sqlite3_stmt* s, Array p) {
return err; return err;
} }
SQLiteRes SQLite3_exec_c(SQLite* db, const char* stmt, Array p) { SQLiteRes SQLite3_exec_c(SQLite* db, const char* stmt, Array* p) {
sqlite3_stmt* s = NULL; sqlite3_stmt* s = NULL;
sqlite3_stmt* n = NULL; sqlite3_stmt* n = NULL;
const char* err; const char* err;