phel-lang/phel-pdo
Composer 安装命令:
composer require phel-lang/phel-pdo
包简介
phel-lang pdo wrapper library.
README 文档
README
PDO wrapper for Phel. Talk to relational databases from Phel without dropping into PHP interop.
Install
composer require phel-lang/phel-pdo
Requires PHP >=8.4 and phel-lang/phel-lang ^0.41.
Quick start
(require phel.pdo) (def conn (pdo/connect "sqlite::memory:")) (pdo/exec conn "create table t1 (id integer primary key autoincrement, name varchar(10))") (pdo/exec conn "insert into t1 (name) values ('phel'), ('php')") ;; Raw query (-> (pdo/query conn "select * from t1 where id = 1") (pdo/fetch)) ;; => {:id 1, :name "phel"} ;; Prepared statement (-> (pdo/prepare conn "select * from t1 where id = :id") (pdo/execute {:id 1}) (pdo/fetch)) ;; => {:id 1, :name "phel"} ;; Insert a row from a map (pdo/insert conn :t1 {:name "lisp"}) ;; => "3" ; new last-insert-id (string, as PDO reports it)
pdo/fetch returns the row as a map keyed by column keyword, or nil when no rows remain.
With phel-sql
phel-sql is an optional data-driven SQL DSL. It returns [sql params] you feed straight into pdo/prepare + pdo/execute:
(let [[query params] (sql/format {:select [:id :name], :from [:users], :where [:= :id 1]})] (-> (pdo/prepare conn query) (pdo/execute params) (pdo/fetch))) ;; => {:id 1, :name "phel"}
API
All functions live in the phel.pdo namespace.
Connection
| Function | Signature | Description |
|---|---|---|
connect |
(connect dsn & [username password options]) |
Open a connection. Throws PDOException on failure. Sets ERRMODE_EXCEPTION by default. |
from-connection |
(from-connection pdo & [options]) |
Wrap an already-open \PDO (e.g. a framework/DBAL connection) as-is. {:apply-defaults true} sets ERRMODE_EXCEPTION. |
exec |
(exec conn sql) |
Execute SQL, return number of affected rows. |
query |
(query conn sql & [fetch-mode]) |
Run SQL without placeholders, return a statement. |
prepare |
(prepare conn sql & [options]) |
Prepare a statement for later execute. |
insert |
(insert conn table row) |
Insert a non-empty row map into table via a prepared statement and return the new last-insert-id (string). Identifiers must match [A-Za-z_][A-Za-z0-9_]*. |
quote |
(quote conn string & [type]) |
Quote a string for safe embedding in SQL. |
last-insert-id |
(last-insert-id conn) |
ID of the last inserted row, as a string (as PDO reports it). |
begin / commit / rollback |
(begin conn) … |
Transaction control. |
in-transaction |
(in-transaction conn) |
true if a transaction is active. |
with-transaction |
(with-transaction conn & body) |
Run body in a transaction: commit + return last value, or rollback + re-throw. Runs inline if already in a transaction. |
get-attribute / set-attribute |
(get-attribute handle attr) / (set-attribute handle attr value) |
PDO attribute access; handle is a connection or a statement. |
get-available-drivers |
(get-available-drivers) |
Vector of installed PDO drivers (static; no connection needed). |
error-code |
(error-code handle) |
SQLSTATE string of the last operation; handle is a connection or a statement. |
error-info |
(error-info handle) |
[sqlstate driver-code driver-message]; handle is a connection or a statement. |
Statement
Returned by pdo/query and pdo/prepare.
| Function | Signature | Description |
|---|---|---|
execute |
(execute stmt & [params]) |
Run a prepared statement. Returns the statement so it threads through -> / let. |
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. |
statement-seq |
(statement-seq stmt) |
Lazy seq of the remaining rows as maps, fetched one at a time. |
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. |
row-count |
(row-count stmt) |
Rows affected by the last DML. |
column-meta |
(column-meta stmt column) |
Metadata map for a 0-indexed column, or nil if unavailable. |
close-cursor |
(close-cursor stmt) |
Free the cursor so the statement can be re-executed. Returns the statement. |
set-fetch-mode |
(set-fetch-mode stmt mode & args) |
Set the statement's default fetch mode (extra args match the mode). Returns the statement. |
next-rowset |
(next-rowset stmt) |
Advance to the next rowset of a multi-rowset statement; false when none remain. |
debug-dump-params |
(debug-dump-params stmt) |
Dump prepared statement info as a string. |
Note
Unlike PDOStatement::execute() (returns bool), pdo/execute returns the statement itself so it composes with ->.
Development
composer install
vendor/bin/phel test
Docs
Deeper docs live in docs/:
- Getting started - install, first query, run tests.
- Architecture -
connection/statementdesign and conventions. - Recipes - transactions, prepared statements, bind types, phel-sql.
- Troubleshooting - common errors and fixes.
- Contributing - adding wrappers, commits, PRs, releases.
phel-lang/phel-pdo 适用场景与选型建议
phel-lang/phel-pdo 是一款 基于 Shell 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdo」 「phel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phel-lang/phel-pdo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phel-lang/phel-pdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phel-lang/phel-pdo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
VV database abstraction layer with query builder and DB structure models
A Phel router based on symfony routing component
VV DB Driver over PDO extension
Useful Query Builder, PDO Class for PHP. A simple access to your SQL records.
PostgreSQL
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-24