Skip to content

Commit

Permalink
feat stmt execute with params.
Browse files Browse the repository at this point in the history
  • Loading branch information
smeghead committed Apr 23, 2024
1 parent 48df6ec commit c5b1469
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
"Connect database and return connection object.
Throws a PDOException if the attempt to connect to the requested database fails "
[dsn & [username password options]]
(connection (php/new \PDO dsn username password options)))
(let [conn (connection (php/new \PDO dsn username password options))]
(php/-> (conn :pdo) (setAttribute (php/:: \PDO ATTR_ERRMODE) (php/:: \PDO ERRMODE_EXCEPTION)))
conn))

(defn exec
"Execute an SQL statement and return the number of affected rows"
[conn stmt]
(php/-> (conn :pdo) (exec stmt)))

(defn prepare
"Prepares a statement for execution and returns a statement object"
[conn stmt & [options]]
(statement/statement (php/-> (conn :pdo) (prepare stmt (to-php-array (or options {}))))))

(defn query
"Prepares and executes an SQL statement without placeholders"
[conn stmt & [fetch-mode]]
(statement/statement (php/-> (conn :pdo) (query stmt))))
(statement/statement (php/-> (conn :pdo) (query stmt (or fetch-mode (php/:: \PDO FETCH_ASSOC))))))

(defn begin
"Initiates a transaction"
Expand All @@ -34,6 +41,11 @@
[conn]
(php/-> (conn :pdo) (commit)))

(defn quote
"Quotes a string for use in a query"
[conn string & [type]]
(php/-> (conn :pdo) (quote string (or type (php/:: \PDO PARAM_STR)))))

# not implement yet.

#PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
Expand Down
20 changes: 19 additions & 1 deletion src/statement/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@
(statement :stmt)
(fetchAll (or fetch-mode (php/:: \PDO FETCH_ASSOC))))))

(defn- map-key-to-string [params]
(loop [ps (keys params)
acc {}]
(if (empty? ps)
(to-php-array acc)
(recur
(rest ps)
(put acc (format "%s" (first ps)) (params (first ps)))))))

(defn execute
"Executes a prepared statement"
[statement & [params]]
(let [params (map-key-to-string params)]
(php/->
(statement :stmt)
(execute params))
statement))


# not implement yet.

#PDOStatement::bindColumn — Bind a column to a PHP variable
Expand All @@ -35,7 +54,6 @@
#PDOStatement::debugDumpParams — Dump an SQL prepared command
#PDOStatement::errorCode — Fetch the SQLSTATE associated with the last operation on the statement handle
#PDOStatement::errorInfo — Fetch extended error information associated with the last operation on the statement handle
#PDOStatement::execute — Executes a prepared statement
#PDOStatement::fetchObject — Fetches the next row and returns it as an object
#PDOStatement::getAttribute — Retrieve a statement attribute
#PDOStatement::getColumnMeta — Returns metadata for a column in a result set
Expand Down
35 changes: 34 additions & 1 deletion tests/feature/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(is (struct? conn))
(is (not= nil conn))))

(deftest test-simple-table-definition
(deftest test-simple-select
(let [conn (pdo/connect "sqlite::memory:")]
(is (= 0 (pdo/exec
conn
Expand All @@ -19,6 +19,33 @@
(let [stmt (pdo/query conn "select * from t1 where id = 1")]
(is (= {"id" 1 "name" "phel"} (statement/fetch stmt))))))

(deftest test-simple-prepare-select
(let [conn (pdo/connect "sqlite::memory:")]
(is (= 0 (pdo/exec
conn
"create table t1 (id integer primary key autoincrement, name varchr(10))")))
(is (pdo/begin conn))
(is (= 1 (pdo/exec
conn
"insert into t1 (name) values ('phel')")))
(is (pdo/commit conn))
(let [stmt (pdo/query conn "select count(*) from t1 where id = 1")]
(is (= 1 (statement/fetch-column stmt))))))

(deftest test-simple-prepare-select-with-params
(let [conn (pdo/connect "sqlite::memory:")]
(is (= 0 (pdo/exec
conn
"create table t1 (id integer primary key autoincrement, name varchr(10))")))
(is (pdo/begin conn))
(is (= 1 (pdo/exec
conn
"insert into t1 (name) values ('phel')")))
(is (pdo/commit conn))
(let [stmt (pdo/prepare conn "select count(*) from t1 where id = :id")
stmt (statement/execute stmt {:id 1})]
(is (= 1 (statement/fetch-column stmt))))))

(deftest test-simple-rollback
(let [conn (pdo/connect "sqlite::memory:")]
(is (= 0 (pdo/exec
Expand All @@ -44,3 +71,9 @@
(is (pdo/commit conn))
(let [stmt (pdo/query conn "select count(*) from t1 where id = 1")]
(is (= 1 (statement/fetch-column stmt))))))

(deftest test-quote
(let [conn (pdo/connect "sqlite::memory:")]
(is (= "'I''m fine.'" (pdo/quote conn "I'm fine.")))))


0 comments on commit c5b1469

Please sign in to comment.