From f037790d07e2d30267a5d8737c58baa947be08db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=A9v=C3=A9?= Date: Fri, 19 Jun 2020 21:32:32 +0100 Subject: [PATCH] Updates the docs so that the example work straight away & Updates gendocs.carp so it behaved incorectly on latest carp --- docs/SQLite3.html | 33 +++++++++++++++++++++------------ gendocs.carp | 15 ++++++--------- sqlite3.carp | 25 +++++++++++++++++-------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/docs/SQLite3.html b/docs/SQLite3.html index 1b5e035..d7bd998 100644 --- a/docs/SQLite3.html +++ b/docs/SQLite3.html @@ -37,18 +37,27 @@ to wrap everything, but it tries to be useful.

Usage

The module SQLite3 provides facilities for opening, closing, and querying databases.

-
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.1")
+
(load "https://veitheller.de/git/carpentry/sqlite3@0.0.3")
 
 ; opening DBs can fail, for the purposes of this example we
 ; ignore that
-(let-do [db (Result.unsafe-from-success (SQLite3.open "db"))]
-  ; we can prepare statements
-  (ignore
-    (SQLite3.query &db "INSERT INTO mytable VALUES (?1, ?2);"
-                   &[(to-sqlite3 @"hello") (to-sqlite3 100)]))
-  ; and query things
-  (println* &(SQLite3.query &db "SELECT * from mytable;" &[]))
-  (SQLite3.close db)
+(defn main []
+  (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
+    (ignore
+       (SQLite3.query &db
+                      "INSERT INTO mytable VALUES (?1, ?2);"
+                      &[(to-sqlite3 @"Carp") (to-sqlite3 4)]))
+
+    ; and query things
+    (println* &(SQLite3.query &db "SELECT * from mytable;" &[]))
+    (SQLite3.close db)))
 

Because open and query return Result types, we could also use combinators!

@@ -109,7 +118,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the external

- (λ [SQLite] ()) + (Fn [SQLite] ())

@@ -129,7 +138,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the defn

- (λ [&String] (Result SQLite String)) + (Fn [(Ref String a)] (Result SQLite String))

                     (open s)
@@ -150,7 +159,7 @@ primitive Carp types can be casted to appropriate SQLite types by using the
                     defn
                 
                 

- (λ [(Ref SQLite), &String, (Ref (Array Type))] (Result (Array (Array Type)) String)) + (Fn [(Ref SQLite a), (Ref String b), (Ref (Array Type) c)] (Result (Array (Array Type)) String))

                     (query db s p)
diff --git a/gendocs.carp b/gendocs.carp
index 3e688e5..6fa34b4 100644
--- a/gendocs.carp
+++ b/gendocs.carp
@@ -1,13 +1,10 @@
 (load "sqlite3.carp")
 
-(defndynamic gendocs []
-       (do
-         (Project.config "title" "sqlite3")
-         (Project.config "docs-directory" "./docs/")
-         (Project.config "docs-logo" "")
-         (Project.config "docs-styling" "style.css")
-         (Project.config "docs-generate-index" false)
-         (save-docs SQLite3)))
+(Project.config "title" "sqlite3")
+(Project.config "docs-directory" "./docs/")
+(Project.config "docs-logo" "")
+(Project.config "docs-styling" "style.css")
+(Project.config "docs-generate-index" false)
+(save-docs SQLite3)
 
-(gendocs)
 (quit)
diff --git a/sqlite3.carp b/sqlite3.carp
index e90508a..245972f 100644
--- a/sqlite3.carp
+++ b/sqlite3.carp
@@ -20,14 +20,23 @@ databases.
 
 ; opening DBs can fail, for the purposes of this example we
 ; ignore that
-(let-do [db (Result.unsafe-from-success (SQLite3.open \"db\"))]
-  ; we can prepare statements
-  (ignore
-    (SQLite3.query &db \"INSERT INTO mytable VALUES (?1, ?2);\"
-                   &[(to-sqlite3 @\"hello\") (to-sqlite3 100)]))
-  ; and query things
-  (println* &(SQLite3.query &db \"SELECT * from mytable;\" &[]))
-  (SQLite3.close db)
+(defn main []
+  (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
+    (ignore
+       (SQLite3.query &db
+                      \"INSERT INTO mytable VALUES (?1, ?2);\"
+                      &[(to-sqlite3 @\"Carp\") (to-sqlite3 4)]))
+
+    ; and query things
+    (println* &(SQLite3.query &db \"SELECT * from mytable;\" &[]))
+    (SQLite3.close db)))
 ```
 
 Because `open` and `query` return `Result` types, we could also use