Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
262aacc0a9 | |||
2386599913 | |||
21e74f82e3 | |||
9743ac15d9 | |||
e239ac2b7f |
@@ -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
|
||||||
|
@@ -32,7 +32,7 @@
|
|||||||
<p>is a simple high-level wrapper around SQLite3. It doesn’t intend
|
<p>is a simple high-level wrapper around SQLite3. It doesn’t 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 "https://veitheller.de/git/carpentry/sqlite3@0.0.1")
|
<pre><code class="language-clojure">(load "https://veitheller.de/git/carpentry/sqlite3@0.0.3")
|
||||||
</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,8 +43,9 @@ databases.</p>
|
|||||||
; 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
|
||||||
&[(to-sqlite3 @"hello") (to-sqlite3 100)]))
|
(SQLite3.query &db "INSERT INTO mytable VALUES (?1, ?2);"
|
||||||
|
&[(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;" &[]))
|
||||||
(SQLite3.close db)
|
(SQLite3.close db)
|
||||||
|
11
sqlite3.carp
11
sqlite3.carp
@@ -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,8 +22,9 @@ 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
|
||||||
&[(to-sqlite3 @\"hello\") (to-sqlite3 100)]))
|
(SQLite3.query &db \"INSERT INTO mytable VALUES (?1, ?2);\"
|
||||||
|
&[(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;\" &[]))
|
||||||
(SQLite3.close db)
|
(SQLite3.close db)
|
||||||
@@ -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))))))
|
||||||
|
@@ -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;
|
||||||
|
Reference in New Issue
Block a user