Skip to content

Commit

Permalink
feat: Add bind-value to statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
smeghead committed May 20, 2024
1 parent 5a3a4bf commit 19d15f8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Features

* Implemented statement bind-value method.

## v0.0.2 (2024-05-17)

### Features
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ Set an attribute

Represents a prepared statement and, after the statement is executed, an associated result set.

#### bind-value

Binds a value to a parameter

```clojure
(bind-value statement column value & [type])
```

#### execute

Executes a prepared statement
Expand Down
31 changes: 24 additions & 7 deletions src/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,38 @@
(defn execute
"Executes a prepared statement"
[statement & [params]]
(let [params (map-key-to-string params)]
(php/->
(statement :stmt)
(execute params))
statement))
(if (empty? params)
(php/-> (statement :stmt) (execute))
(let [params (map-key-to-string params)]
(php/->
(statement :stmt)
(execute params))))
statement)

(defn column-count
"Returns the number of columns in the result set"
[statement]
(php/-> (statement :stmt) (columnCount)))

(defn row-count
"Returns the number of rows affected by the last SQL statement"
[statement]
(php/-> (statement :stmt) (rowCount)))

(defn bind-value
"Binds a value to a parameter"
[statement column value & [type]]
(php/->
(statement :stmt)
(bindValue (str column) value (or type (php/:: \PDO PARAM_STR))))
statement)

# not implement yet.

#PDOStatement::bindColumn — Bind a column to a PHP variable
#PDOStatement::bindParam — Binds a parameter to the specified variable name
#PDOStatement::bindValue — Binds a value to a parameter
#PDOStatement::closeCursor — Closes the cursor, enabling the statement to be executed again
#PDOStatement::columnCount — Returns the number of columns in the result set
#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
Expand All @@ -69,6 +87,5 @@
#PDOStatement::getColumnMeta — Returns metadata for a column in a result set
#PDOStatement::getIterator — Gets result set iterator
#PDOStatement::nextRowset — Advances to the next rowset in a multi-rowset statement handle
#PDOStatement::rowCount — Returns the number of rows affected by the last SQL statement
#PDOStatement::setAttribute — Set a statement attribute
#PDOStatement::setFetchMode — Set the default fetch mode for this statement
50 changes: 50 additions & 0 deletions tests/feature/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,53 @@
"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))))))

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

(deftest test-row-count
(let [conn (pdo/connect "sqlite::memory:")]
(is (= 0 (pdo/exec
conn
"create table t1 (id integer primary key autoincrement, name varchr(10))")))
(let [stmt (pdo/query conn "insert into t1 (name) values ('phel'), ('php')")]
(is (= 2 (statement/row-count stmt))))))

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

(deftest test-bind-value-with-type
(let [conn (pdo/connect "sqlite::memory:")]
(is (= 0 (pdo/exec
conn
"create table t1 (id integer primary key autoincrement, name varchr(10))")))
(is (= 1 (pdo/exec
conn
"insert into t1 (name) values ('phel')")))
(let [stmt (pdo/prepare conn "select * from t1 where id = :id")
stmt (statement/bind-value stmt :id 1 (php/:: \PDO PARAM_INT))
stmt (statement/execute stmt)]
(is (= {:id 1 :name "phel"} (statement/fetch stmt))))
(let [stmt (pdo/prepare conn "select * from t1 where name = :name")
stmt (statement/bind-value stmt :name "phel" (php/:: \PDO PARAM_STR))
stmt (statement/execute stmt)]
(is (= {:id 1 :name "phel"} (statement/fetch stmt))))))

0 comments on commit 19d15f8

Please sign in to comment.