Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `pdo/insert` - build an `INSERT` from a map (`(pdo/insert conn :table {:col v ...})`), execute it as a prepared statement, and return the new `last-insert-id`. Identifiers must match `[A-Za-z_][A-Za-z0-9_]*` ([#4]).
- `pdo/bind-param` - wrap `PDOStatement::bindParam`; binds a parameter applied at execution time ([#8]).
- `pdo/close-cursor` - wrap `PDOStatement::closeCursor`; frees the cursor so the statement can be re-executed ([#9]).
- `pdo/fetch-object` - wrap `PDOStatement::fetchObject`; returns the next row as an object (`stdClass` or a named class), or `nil` when exhausted ([#11]).

## [0.1.0] - 2026-05-13

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Returned by `pdo/query` and `pdo/prepare`.
| `fetch` | `(fetch stmt)` | Next row as a map, or `nil` if exhausted. |
| `fetch-all` | `(fetch-all stmt)` | Remaining rows as a vector of maps. |
| `fetch-column` | `(fetch-column stmt & [column])` | Single column from the next row. |
| `fetch-object` | `(fetch-object stmt & [class-name ctor-args])` | Next row as an object (`stdClass` by default, or an instance of `class-name`), or `nil` if exhausted. |
| `bind-value` | `(bind-value stmt column value & [type])` | Bind a value to a placeholder. Returns the statement. |
| `bind-param` | `(bind-param stmt column value & [type])` | Bind a parameter, applied at execution time. Returns the statement. |
| `column-count` | `(column-count stmt)` | Number of columns in the result set. |
Expand Down
9 changes: 8 additions & 1 deletion src/pdo/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
[stmt]
(map row->map (php/-> (stmt :stmt) (fetchAll \PDO/FETCH_ASSOC))))

(defn fetch-object
"Fetches the next row as an object (stdClass by default, or an instance of class-name), or nil if no more rows"
[stmt & [class-name ctor-args]]
(when-let [obj (php/-> (stmt :stmt)
(fetchObject (or class-name "stdClass")
(phel->php (or ctor-args []))))]
obj))

(defn execute
"Executes a prepared statement"
[stmt & [params]]
Expand Down Expand Up @@ -71,7 +79,6 @@
;; PDOStatement::bindColumn — Bind a column to a PHP variable
;; 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::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
;; PDOStatement::getIterator — Gets result set iterator
Expand Down
12 changes: 12 additions & 0 deletions tests/pdo_test.phel
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@
(let [stmt (pdo/query *conn* "select * from t1")]
(is (= [{:id 1 :name "phel"} {:id 2 :name "php"}] (pdo/fetch-all stmt)))))

(deftest fetch-object-returns-stdclass-row
(seed-t1 *conn*)
(let [stmt (pdo/query *conn* "select * from t1 where id = 1")
obj (pdo/fetch-object stmt)]
(is (= "stdClass" (php/get_class obj)))
(is (= "phel" (php/-> obj name)))))

(deftest fetch-object-returns-nil-when-no-rows
(seed-t1 *conn*)
(let [stmt (pdo/query *conn* "select * from t1 where id = 999")]
(is (nil? (pdo/fetch-object stmt)))))

(deftest column-count-on-select
(seed-t1 *conn*)
(let [stmt (pdo/query *conn* "select * from t1")]
Expand Down
Loading