Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
1b87e7fec1 | |||
a689c3ec85 | |||
f99c5dc329 | |||
|
f037790d07 | ||
|
8818ec83ab |
@@ -6,7 +6,7 @@ everything, but it tries to be useful.
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.3")
|
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.5")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -15,7 +15,7 @@ The module `SQLite3` provides facilities for opening, closing, and querying
|
|||||||
databases.
|
databases.
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.1")
|
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.5")
|
||||||
|
|
||||||
; opening DBs can fail, for the purposes of this example we
|
; opening DBs can fail, for the purposes of this example we
|
||||||
; ignore that
|
; ignore that
|
||||||
|
@@ -9,8 +9,8 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<div class="logo">
|
<div class="logo">
|
||||||
<a href="http://github.com/carp-lang/Carp">
|
<a href="">
|
||||||
<img src="logo.png">
|
<img src="">
|
||||||
</a>
|
</a>
|
||||||
<div class="title">
|
<div class="title">
|
||||||
sqlite3
|
sqlite3
|
||||||
@@ -32,23 +32,32 @@
|
|||||||
<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.3")
|
<pre><code class="language-clojure">(load "https://veitheller.de/git/carpentry/sqlite3@0.0.5")
|
||||||
</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
|
||||||
databases.</p>
|
databases.</p>
|
||||||
<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.5")
|
||||||
|
|
||||||
; opening DBs can fail, for the purposes of this example we
|
; opening DBs can fail, for the purposes of this example we
|
||||||
; ignore that
|
; ignore that
|
||||||
|
(defn main []
|
||||||
(let-do [db (Result.unsafe-from-success (SQLite3.open "db"))]
|
(let-do [db (Result.unsafe-from-success (SQLite3.open "db"))]
|
||||||
|
; Let's make sure our table is there
|
||||||
|
(ignore
|
||||||
|
(SQLite3.query &db
|
||||||
|
"CREATE TABLE IF NOT EXISTS mytable (name TEXT, age INT)"
|
||||||
|
&[]))
|
||||||
|
|
||||||
; we can prepare statements
|
; we can prepare statements
|
||||||
(ignore
|
(ignore
|
||||||
(SQLite3.query &db "INSERT INTO mytable VALUES (?1, ?2);"
|
(SQLite3.query &db
|
||||||
&[(to-sqlite3 @"hello") (to-sqlite3 100)]))
|
"INSERT INTO mytable VALUES (?1, ?2);"
|
||||||
|
&[(to-sqlite3 @"Carp") (to-sqlite3 4)]))
|
||||||
|
|
||||||
; 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)))
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>Because <code>open</code> and <code>query</code> return <code>Result</code> types, we could also use
|
<p>Because <code>open</code> and <code>query</code> return <code>Result</code> types, we could also use
|
||||||
combinators!</p>
|
combinators!</p>
|
||||||
@@ -61,7 +70,7 @@ combinators!</p>
|
|||||||
</h3>
|
</h3>
|
||||||
</a>
|
</a>
|
||||||
<div class="description">
|
<div class="description">
|
||||||
doc-stub
|
meta-stub
|
||||||
</div>
|
</div>
|
||||||
<p class="sig">
|
<p class="sig">
|
||||||
a
|
a
|
||||||
@@ -92,10 +101,6 @@ anything.</p>
|
|||||||
|
|
||||||
</span>
|
</span>
|
||||||
<p class="doc">
|
<p class="doc">
|
||||||
<p>represent all the SQLite types we can represent.</p>
|
|
||||||
<p>The constructors are <code>Null</code>, <code>Integer</code>, <code>Floating</code>, <code>Text</code>, and <code>Blob</code>. Most
|
|
||||||
primitive Carp types can be casted to appropriate SQLite types by using the
|
|
||||||
<code>to-sqlite3</code> interface.</p>
|
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -109,7 +114,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
external
|
external
|
||||||
</div>
|
</div>
|
||||||
<p class="sig">
|
<p class="sig">
|
||||||
(λ [SQLite] ())
|
(Fn [SQLite] ())
|
||||||
</p>
|
</p>
|
||||||
<span>
|
<span>
|
||||||
|
|
||||||
@@ -129,7 +134,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
defn
|
defn
|
||||||
</div>
|
</div>
|
||||||
<p class="sig">
|
<p class="sig">
|
||||||
(λ [&String] (Result SQLite String))
|
(Fn [(Ref String a)] (Result SQLite String))
|
||||||
</p>
|
</p>
|
||||||
<pre class="args">
|
<pre class="args">
|
||||||
(open s)
|
(open s)
|
||||||
@@ -150,7 +155,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
defn
|
defn
|
||||||
</div>
|
</div>
|
||||||
<p class="sig">
|
<p class="sig">
|
||||||
(λ [(Ref SQLite), &String, (Ref (Array Type))] (Result (Array (Array Type)) String))
|
(Fn [(Ref SQLite a), (Ref String b), (Ref (Array SQLite3.Type) c)] (Result (Array (Array SQLite3.Type)) String))
|
||||||
</p>
|
</p>
|
||||||
<pre class="args">
|
<pre class="args">
|
||||||
(query db s p)
|
(query db s p)
|
||||||
|
@@ -1,13 +1,10 @@
|
|||||||
(load "sqlite3.carp")
|
(load "sqlite3.carp")
|
||||||
|
|
||||||
(defndynamic gendocs []
|
|
||||||
(do
|
|
||||||
(Project.config "title" "sqlite3")
|
(Project.config "title" "sqlite3")
|
||||||
(Project.config "docs-directory" "./docs/")
|
(Project.config "docs-directory" "./docs/")
|
||||||
(Project.config "docs-logo" "")
|
(Project.config "docs-logo" "")
|
||||||
(Project.config "docs-styling" "style.css")
|
(Project.config "docs-styling" "style.css")
|
||||||
(Project.config "docs-generate-index" false)
|
(Project.config "docs-generate-index" false)
|
||||||
(save-docs SQLite3)))
|
(save-docs SQLite3)
|
||||||
|
|
||||||
(gendocs)
|
|
||||||
(quit)
|
(quit)
|
||||||
|
66
sqlite3.carp
66
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.3\")
|
(load \"https://veitheller.de/git/carpentry/sqlite3@0.0.5\")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -16,18 +16,27 @@ The module `SQLite3` provides facilities for opening, closing, and querying
|
|||||||
databases.
|
databases.
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(load \"https://veitheller.de/git/carpentry/sqlite3@0.0.1\")
|
(load \"https://veitheller.de/git/carpentry/sqlite3@0.0.5\")
|
||||||
|
|
||||||
; opening DBs can fail, for the purposes of this example we
|
; opening DBs can fail, for the purposes of this example we
|
||||||
; ignore that
|
; ignore that
|
||||||
|
(defn main []
|
||||||
(let-do [db (Result.unsafe-from-success (SQLite3.open \"db\"))]
|
(let-do [db (Result.unsafe-from-success (SQLite3.open \"db\"))]
|
||||||
|
; Let's make sure our table is there
|
||||||
|
(ignore
|
||||||
|
(SQLite3.query &db
|
||||||
|
\"CREATE TABLE IF NOT EXISTS mytable (name TEXT, age INT)\"
|
||||||
|
&[]))
|
||||||
|
|
||||||
; we can prepare statements
|
; we can prepare statements
|
||||||
(ignore
|
(ignore
|
||||||
(SQLite3.query &db \"INSERT INTO mytable VALUES (?1, ?2);\"
|
(SQLite3.query &db
|
||||||
&[(to-sqlite3 @\"hello\") (to-sqlite3 100)]))
|
\"INSERT INTO mytable VALUES (?1, ?2);\"
|
||||||
|
&[(to-sqlite3 @\"Carp\") (to-sqlite3 4)]))
|
||||||
|
|
||||||
; 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)))
|
||||||
```
|
```
|
||||||
|
|
||||||
Because `open` and `query` return `Result` types, we could also use
|
Because `open` and `query` return `Result` types, we could also use
|
||||||
@@ -65,8 +74,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
(Integer [Int])
|
(Integer [Int])
|
||||||
(Floating [Double])
|
(Floating [Double])
|
||||||
(Text [String])
|
(Text [String])
|
||||||
(Blob [String])
|
(Blob [String]))
|
||||||
)
|
|
||||||
|
|
||||||
(private SQLiteColumn)
|
(private SQLiteColumn)
|
||||||
(hidden SQLiteColumn)
|
(hidden SQLiteColumn)
|
||||||
@@ -78,8 +86,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
(register int (Fn [Int] SQLiteColumn) "SQLiteColumn_int")
|
(register int (Fn [Int] SQLiteColumn) "SQLiteColumn_int")
|
||||||
(register float (Fn [Double] SQLiteColumn) "SQLiteColumn_float")
|
(register float (Fn [Double] SQLiteColumn) "SQLiteColumn_float")
|
||||||
(register text (Fn [String] SQLiteColumn) "SQLiteColumn_text")
|
(register text (Fn [String] SQLiteColumn) "SQLiteColumn_text")
|
||||||
(register blob (Fn [String] SQLiteColumn) "SQLiteColumn_blob")
|
(register blob (Fn [String] SQLiteColumn) "SQLiteColumn_blob"))
|
||||||
)
|
|
||||||
|
|
||||||
(defn to-sqlite3-internal [x]
|
(defn to-sqlite3-internal [x]
|
||||||
(match x
|
(match x
|
||||||
@@ -87,8 +94,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
(Integer i) (SQLiteColumn.int i)
|
(Integer i) (SQLiteColumn.int i)
|
||||||
(Floating f) (SQLiteColumn.float f)
|
(Floating f) (SQLiteColumn.float f)
|
||||||
(Text s) (SQLiteColumn.text s)
|
(Text s) (SQLiteColumn.text s)
|
||||||
(Blob s) (SQLiteColumn.blob s)))
|
(Blob s) (SQLiteColumn.blob s))))
|
||||||
)
|
|
||||||
|
|
||||||
(defmodule SQLiteColumn
|
(defmodule SQLiteColumn
|
||||||
(register tag (Fn [&SQLiteColumn] Int) "SQLiteColumn_tag")
|
(register tag (Fn [&SQLiteColumn] Int) "SQLiteColumn_tag")
|
||||||
@@ -117,8 +123,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
a (Array.allocate l)]
|
a (Array.allocate l)]
|
||||||
(for [i 0 l]
|
(for [i 0 l]
|
||||||
(Array.aset-uninitialized! &a i (SQLiteColumn.to-carp (nth &r i))))
|
(Array.aset-uninitialized! &a i (SQLiteColumn.to-carp (nth &r i))))
|
||||||
a))
|
a)))
|
||||||
)
|
|
||||||
|
|
||||||
(private SQLiteRes)
|
(private SQLiteRes)
|
||||||
(hidden SQLiteRes)
|
(hidden SQLiteRes)
|
||||||
@@ -127,28 +132,27 @@ primitive Carp types can be casted to appropriate SQLite types by using the
|
|||||||
(register ok? (Fn [&SQLiteRes] Bool) "SQLiteRes_is_ok")
|
(register ok? (Fn [&SQLiteRes] Bool) "SQLiteRes_is_ok")
|
||||||
(register length (Fn [&SQLiteRes] Int) "SQLiteRes_length")
|
(register length (Fn [&SQLiteRes] Int) "SQLiteRes_length")
|
||||||
(register nth (Fn [&SQLiteRes Int] SQLiteRow) "SQLiteRes_nth")
|
(register nth (Fn [&SQLiteRes Int] SQLiteRow) "SQLiteRes_nth")
|
||||||
(register error (Fn [SQLiteRes] (Ptr Char)) "SQLiteRes_error")
|
(register error (Fn [SQLiteRes] (Ptr CChar)) "SQLiteRes_error")
|
||||||
|
|
||||||
(defn to-array [r]
|
(defn to-array [r]
|
||||||
(let-do [l (length &r)
|
(let-do [l (length &r)
|
||||||
a (Array.allocate l)]
|
a (Array.allocate l)]
|
||||||
(for [i 0 l]
|
(for [i 0 l]
|
||||||
(Array.aset-uninitialized! &a i (SQLiteRow.to-carp (nth &r i))))
|
(Array.aset-uninitialized! &a i (SQLiteRow.to-carp (nth &r i))))
|
||||||
a))
|
a)))
|
||||||
)
|
|
||||||
|
|
||||||
(private init)
|
(private init)
|
||||||
(hidden init)
|
(hidden init)
|
||||||
(register init (Fn [] SQLite))
|
(register init (Fn [] SQLite))
|
||||||
(private open-)
|
(private open-)
|
||||||
(hidden open-)
|
(hidden open-)
|
||||||
(register open- (Fn [&SQLite (Ptr Char)] Int) "SQLite3_open_c")
|
(register open- (Fn [&SQLite (Ptr CChar)] 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 CChar) &(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 CChar)) "SQLite3_error")
|
||||||
|
|
||||||
(doc open "opens a database with the filename `s`.
|
(doc open "opens a database with the filename `s`.
|
||||||
|
|
||||||
@@ -171,25 +175,31 @@ If it fails, we return an error message using `Result.Error`.")
|
|||||||
(Result.Error (from-cstr (SQLiteRes.error r))))))
|
(Result.Error (from-cstr (SQLiteRes.error r))))))
|
||||||
|
|
||||||
(doc close "closes a database.")
|
(doc close "closes a database.")
|
||||||
(register close (Fn [SQLite] ()) "SQLite3_close_c")
|
(register close (Fn [SQLite] ()) "SQLite3_close_c"))
|
||||||
)
|
|
||||||
|
|
||||||
(definterface to-sqlite3 (Fn [a] SQLIte3.Type))
|
(definterface to-sqlite3 (Fn [a] SQLite3.Type))
|
||||||
|
|
||||||
(defmodule Bool
|
(defmodule Bool
|
||||||
(defn to-sqlite3 [b] (SQLite3.Type.Integer (if b 1 0))))
|
(defn to-sqlite3 [b] (SQLite3.Type.Integer (if b 1 0)))
|
||||||
|
(implements to-sqlite3 Bool.to-sqlite3))
|
||||||
|
|
||||||
(defmodule Int
|
(defmodule Int
|
||||||
(defn to-sqlite3 [i] (SQLite3.Type.Integer i)))
|
(defn to-sqlite3 [i] (SQLite3.Type.Integer i))
|
||||||
|
(implements to-sqlite3 Int.to-sqlite3))
|
||||||
|
|
||||||
(defmodule Long
|
(defmodule Long
|
||||||
(defn to-sqlite3 [l] (SQLite3.Type.Integer (to-int (the Long l)))))
|
(defn to-sqlite3 [l] (SQLite3.Type.Integer (to-int (the Long l))))
|
||||||
|
(implements to-sqlite3 Long.to-sqlite3))
|
||||||
|
|
||||||
(defmodule Float
|
(defmodule Float
|
||||||
(defn to-sqlite3 [f] (SQLite3.Type.Floating (Double.from-float f))))
|
(defn to-sqlite3 [f] (SQLite3.Type.Floating (Double.from-float f)))
|
||||||
|
(implements to-sqlite3 Float.to-sqlite3))
|
||||||
|
|
||||||
(defmodule Double
|
(defmodule Double
|
||||||
(defn to-sqlite3 [d] (SQLite3.Type.Floating d)))
|
(defn to-sqlite3 [d] (SQLite3.Type.Floating d))
|
||||||
|
(implements to-sqlite3 Double.to-sqlite3))
|
||||||
|
|
||||||
(defmodule String
|
(defmodule String
|
||||||
(defn to-sqlite3 [s] (SQLite3.Type.Text s)))
|
(defn to-sqlite3 [s] (SQLite3.Type.Text s))
|
||||||
|
(implements to-sqlite3 String.to-sqlite3))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user