php / php/php-src

Binary type for PDO

未关闭
#11,462 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Extension: pdo (core) Feature Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

As part of supporting multiple users, I've found that they have difficulty with dealing with binary columns with PDO drivers.

  • One was using PDO_DBLIB; I've filed GH-10312 and its associated PR. (Details in there, but tl;dr binary data needs to be hex-encoded in a TDS stream to avoid damage as TDS doesn't have protocol level prepared statements.)
  • One was using PDO_ODBC, with an ODBC driver that had different semantics with SQL_C_CHAR shaped bindings on binary columns versus binding it as a binary on the C side. (Specifically, it would hex-encode the column. Useful perhaps, but unexpected for the user.)
    • As a workaround, the user ended up using procedural ODBC, which has odbc_result work by accident by binding it as a binary on the C side, even though I don;'t think the procedural ODBC binding interface can represent that.

Notably, these aren't necessarily LOBs - they can be (relatively) small, users don't (want to) deal with them in terms of streams on the PHP side (as PHP strings can represent binary data just fine), etc. Treating them as strings isn't right, because they can be subject to unwanted and inappropriate encoding conversions or an inefficient representation over the wire.

Proposal

Extend the PDO constants to have a PARAM_BINARY binding type, and use it in drivers. For things like ODBC (where I'm most familiar), this would map to an SQL_C_BINARY binding type, with the equivalent in other drivers when possible.

User code could look something like:

<?php
  
$SQL1 = "select id, my_blob from calvin.blob_stream";

$connstring = 'odbc:*LOCAL';
$dbconn = new PDO($connstring);
$stmt = $dbconn->prepare($SQL1);
$stmt->execute();
$lob = "";
$stmt->bindColumn(2, $lob, PDO::PARAM_BINARY);
$stmt->fetch(PDO::FETCH_BOUND);

echo "PDO: $lob\n";

Basically looks like using PARAM_STR, and would maintain a string interface unlike PARAM_LOB.

Possible problems/alternatives

I'm not committed to the proposed interface; I just know there is a problem dealing with binaries. If there's another way to solve this, I'd like to hear it.

  • Making it a flag: PARAM_STR_NATL or PARAM_STR_INTL could be set on a PARAM_STR, so why not PARAM_STR_BINARY?
  • Driver support: Binary columns are common on other databases (ODBC standardized it), but not necessarily always. If they do, they might not have the same problems. I don't know how the semantics would map in the case it lacks support - treat it as a string?
    • PARAM_STMT has even sketchier support (specifically, it's in none of them) and is also a constant, so there is precedent for types not supported by all drivers.
  • Overlap with PARAM_LOB: The intent of a LOB is somewhat similar, but with different semantics. Also note a LOB doesn't necessarily have to be binary data (blob), it can be a large string (clob), and (var)binary columns aren't blobs anyways. GH-10343 overrides the semantics of PARAM_LOB for binary non-stream content, but I'm not sure if this is appropriate.
  • Making it work with PARAM_STR: For the TDS case, the driver could automatically detect binary data, but the semantics issue exposed by i.e. ODBC remains.
  • RFC: This might be a big enough change to merit an RFC. Nothing would be changed/removed, just added, but it is public PDO surface.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查 PDO 现有的参数常量和绑定行为,然后比较 issue 中描述的 PDO_ODBC 和 PDO_DBLIB 情况。确定新的二进制绑定类型或替代接口是否合适,记录驱动程序支持情况和 fallback 语义,并确定实现前是否需要 RFC。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, php
领域
backend-api-design, databases
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。