Skip to content

Commit

Permalink
doc: add example usage to README.
Browse files Browse the repository at this point in the history
  • Loading branch information
smeghead committed Apr 23, 2024
1 parent c5b1469 commit cbc975b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
# phel-pdo
phel-lang pdo wrapper library.

## Usage

This is an example of connecting to a file database, creating a table, inserting records, and searching on repl.

```
iphel:1> (require smeghead\pdo)
smeghead\pdo
phel:2> (require smeghead\pdo\statement)
smeghead\pdo\statement
phel:3> (def connection-string "sqlite:database.db")
1
phel:4> (def conn (pdo/connect connection-string))
1
phel:5> (pdo/exec conn "create table t1 (id integer primary key autoincrement, name varchr(10))")
0
phel:6> (pdo/exec conn "insert into t1 (name) values ('phel'), ('php')")
2
phel:7> (def stmt (pdo/query conn "select * from t1 where id = 1"))
1
phel:8> (statement/fetch stmt)
{:id 1 :name phel}
```


## Development
Expand Down
1 change: 0 additions & 1 deletion src/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,4 @@
#PDO::inTransaction — Checks if inside a transaction
#PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
#PDO::prepare — Prepares a statement for execution and returns a statement object
#PDO::quote — Quotes a string for use in a query
#PDO::setAttribute — Set an attribute
22 changes: 16 additions & 6 deletions src/statement/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

(defstruct statement [stmt])

(defn- map-key-to-keyowrd [row]
(let [row (php-array-to-map row)]
(loop [ps (keys row)
acc {}]
(if (empty? ps)
acc
(recur
(rest ps)
(put acc (keyword (first ps)) (row (first ps))))))))

(defn fetch
"Fetches the next row from a result set"
[statement & [fetch-mode cursor-orientation cursor-offset]]
(php-array-to-map
[statement]
(map-key-to-keyowrd
(php/->
(statement :stmt)
(fetch (or fetch-mode (php/:: \PDO FETCH_ASSOC))))))
(fetch (php/:: \PDO FETCH_ASSOC)))))

(defn fetch-column
"Returns a single column from the next row of a result set"
Expand All @@ -19,11 +29,11 @@

(defn fetch-all
"Fetches the remaining rows from a result set"
[statement & [fetch-mode cursor-orientation cursor-offset]]
(map php-array-to-map
[statement]
(map map-key-to-keyowrd
(php/->
(statement :stmt)
(fetchAll (or fetch-mode (php/:: \PDO FETCH_ASSOC))))))
(fetchAll (php/:: \PDO FETCH_ASSOC)))))

(defn- map-key-to-string [params]
(loop [ps (keys params)
Expand Down
2 changes: 1 addition & 1 deletion tests/feature/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
conn
"insert into t1 (name) values ('phel')")))
(let [stmt (pdo/query conn "select * from t1 where id = 1")]
(is (= {"id" 1 "name" "phel"} (statement/fetch stmt))))))
(is (= {:id 1 :name "phel"} (statement/fetch stmt))))))

(deftest test-simple-prepare-select
(let [conn (pdo/connect "sqlite::memory:")]
Expand Down
2 changes: 1 addition & 1 deletion tests/feature/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
conn
"insert into t1 (name) values ('php')")))
(let [stmt (pdo/query conn "select * from t1")]
(is (= [{"id" 1 "name" "phel"} {"id" 2 "name" "php"}] (statement/fetch-all stmt))))))
(is (= [{:id 1 :name "phel"} {:id 2 :name "php"}] (statement/fetch-all stmt))))))

0 comments on commit cbc975b

Please sign in to comment.