diff --git a/CHANGELOG.md b/CHANGELOG.md index ab5a5d9..a8e235e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +### Features + + * Implemented statement bind-value method. + ## v0.0.2 (2024-05-17) ### Features diff --git a/README.md b/README.md index 431a771..8f5a467 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/statement.phel b/src/statement.phel index b16a6ad..f8014a3 100644 --- a/src/statement.phel +++ b/src/statement.phel @@ -47,12 +47,31 @@ (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. @@ -60,7 +79,6 @@ #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 @@ -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 diff --git a/tests/feature/statement.phel b/tests/feature/statement.phel index 016e230..fdce3f7 100644 --- a/tests/feature/statement.phel +++ b/tests/feature/statement.phel @@ -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)))))) +