Skip to content

Commit

Permalink
Merge pull request #24 from php/bool
Browse files Browse the repository at this point in the history
Boolean support
  • Loading branch information
NattyNarwhal authored Oct 18, 2023
2 parents dcbe0bc + b196160 commit d760f98
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ibm_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ static int stmt_get_parameter_info(pdo_stmt_t * stmt, struct pdo_bound_param_dat
* Numeric forms we can map directly to a long
* int value
*/
case SQL_BOOLEAN:
case SQL_BIT:
case SQL_SMALLINT:
case SQL_INTEGER:
param_res->ctype = SQL_C_LONG;
Expand All @@ -371,6 +373,8 @@ static int stmt_get_parameter_info(pdo_stmt_t * stmt, struct pdo_bound_param_dat
static int db2_inout_parm_numeric(param_node *param_res) {
/* need character to number padding */
switch ( param_res->data_type ) {
case SQL_BOOLEAN:
case SQL_BIT:
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_REAL:
Expand Down Expand Up @@ -446,6 +450,8 @@ static void db2_inout_parm_pad_data(param_node *param_res, struct pdo_bound_para
* '2' becomes '000000000000000000002'
*/
switch ( param_res->data_type ) {
case SQL_BOOLEAN:
case SQL_BIT:
case SQL_SMALLINT:
case SQL_INTEGER:
case SQL_REAL:
Expand Down Expand Up @@ -591,6 +597,7 @@ int stmt_bind_parameter(pdo_stmt_t *stmt, struct pdo_bound_param_data *curr)
return FALSE;

/* this is a long value for PHP (or user forced) */
case PDO_PARAM_BOOL:
case PDO_PARAM_INT:
/*
* If the parameter type is a numeric type,
Expand Down Expand Up @@ -634,7 +641,6 @@ int stmt_bind_parameter(pdo_stmt_t *stmt, struct pdo_bound_param_data *curr)
*/

/* a string value (very common) */
case PDO_PARAM_BOOL:
case PDO_PARAM_STR:
/*
* SQL_PARAM_INPUT -- null remain untouched (read)
Expand Down Expand Up @@ -1481,6 +1487,8 @@ static int ibm_stmt_describer(
#ifdef PASE /* i5/OS size changes for "common" converts to string */
switch (col_res->data_type) {
/* BIGINT 9223372036854775807 (2^63-1) string convert */
case SQL_BOOLEAN:
case SQL_BIT:
case SQL_BIGINT:
case SQL_SMALLINT:
case SQL_INTEGER:
Expand Down Expand Up @@ -1780,6 +1788,10 @@ static int ibm_stmt_get_column_meta(
* XXX: Also safe for pre-8.1?
*/
switch (col_res->data_type) {
/* Booleans */
case SQL_BOOLEAN:
case SQL_BIT:
add_assoc_long(return_value, "pdo_type", PDO_PARAM_BOOL);
/* LOBs */
#ifndef PASE /* i5/OS - not LOBs */
case SQL_LONGVARBINARY:
Expand Down Expand Up @@ -1815,7 +1827,6 @@ static int ibm_stmt_get_column_meta(
default:
add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
break;
/* XXX: PARAM_(INT|BOOL)? */
}
#endif
return SUCCESS;
Expand Down
8 changes: 8 additions & 0 deletions php_pdo_ibm_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
#define MAX_ERR_MSG_LEN (SQL_MAX_MESSAGE_LENGTH + SQL_SQLSTATE_SIZE + 1)
#define CDTIMETYPE 112

/*
* Added in IBM i 7.5, also in LUW 11.1 MP1/FP1
* see: https://www.ibm.com/docs/en/db2/11.1?topic=database-mod-pack-fix-pack-updates#c0061179__FP1
*/
#ifndef SQL_BOOLEAN
#define SQL_BOOLEAN 16
#endif

#ifndef SQL_XML
#define SQL_XML -370
#endif
Expand Down
45 changes: 45 additions & 0 deletions tests/fvt_boolean.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
pdo_ibm: Boolean data type
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
require_once('fvt.inc');
class Test extends FVTTest
{
public function runTest()
{
// XXX: Skip on older LUW/i
$this->connect();

// Tests insert and select
try {
$this->db->exec("drop table booltest");
} catch (Exception $e) {}
$this->db->exec("create table booltest (id integer not null, enabled boolean, primary key(id))");

$s = $this->db->prepare("insert into booltest (id, enabled) values (1, ?)");
$x = true;
$s->bindParam(1, $x, PDO::PARAM_BOOL);
$r = $s->execute();;

$s = $this->db->prepare("insert into booltest (id, enabled) values (2, ?)");
$x = false;
$s->bindParam(1, $x, PDO::PARAM_BOOL);
$r = $s->execute();

$s = $this->db->prepare("select * from booltest");
$s->execute();
while ($r = $s->fetch(PDO::FETCH_ASSOC)) {
// This can return 0/1 on LUW or TRUE/FALSE on i
echo $r["ID"] . ": " . $r["ENABLED"] . "\n";
}
}
}

$testcase = new Test();
$testcase->runTest();
?>
--EXPECTREGEX--
1: (1|TRUE)
2: (0|FALSE)

0 comments on commit d760f98

Please sign in to comment.