From cbc975be10807686f94b3d46b9ab0fdd476ae088 Mon Sep 17 00:00:00 2001 From: smeghead Date: Tue, 23 Apr 2024 23:38:21 +0900 Subject: [PATCH] doc: add example usage to README. --- README.md | 20 ++++++++++++++++++++ src/pdo.phel | 1 - src/statement/statement.phel | 22 ++++++++++++++++------ tests/feature/pdo.phel | 2 +- tests/feature/statement.phel | 2 +- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 785ad35..b4f9850 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/pdo.phel b/src/pdo.phel index 310c7fa..d759e3b 100644 --- a/src/pdo.phel +++ b/src/pdo.phel @@ -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 diff --git a/src/statement/statement.phel b/src/statement/statement.phel index 34766b4..b16a6ad 100644 --- a/src/statement/statement.phel +++ b/src/statement/statement.phel @@ -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" @@ -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) diff --git a/tests/feature/pdo.phel b/tests/feature/pdo.phel index 14cf7c6..c539d93 100644 --- a/tests/feature/pdo.phel +++ b/tests/feature/pdo.phel @@ -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:")] diff --git a/tests/feature/statement.phel b/tests/feature/statement.phel index b40dab9..016e230 100644 --- a/tests/feature/statement.phel +++ b/tests/feature/statement.phel @@ -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))))))