Skip to content

Commit e7af8fd

Browse files
authored
Merge pull request #57 from php/boolean
Boolean support
2 parents 7c1458c + 2f82edb commit e7af8fd

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

ibm_db2.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,16 @@ static int _php_db2_bind_column_helper(stmt_handle *stmt_res)
21222122
}
21232123
break;
21242124

2125+
case SQL_BOOLEAN:
2126+
case SQL_BIT:
2127+
rc = SQLBindCol((SQLHSTMT)stmt_res->hstmt, (SQLUSMALLINT)(i + 1),
2128+
SQL_C_LONG, &row_data->i_val, sizeof(row_data->i_val),
2129+
(SQLINTEGER *)(&stmt_res->row_data[i].out_length));
2130+
if ( rc == SQL_ERROR ) {
2131+
_php_db2_check_sql_errors((SQLHSTMT)stmt_res->hstmt, SQL_HANDLE_STMT, rc, 1, NULL, -1, 1);
2132+
}
2133+
break;
2134+
21252135
case SQL_SMALLINT:
21262136
rc = SQLBindCol((SQLHSTMT)stmt_res->hstmt, (SQLUSMALLINT)(i + 1),
21272137
SQL_C_DEFAULT, &row_data->s_val, sizeof(row_data->s_val),
@@ -4444,6 +4454,8 @@ static int _php_db2_bind_data( stmt_handle *stmt_res, param_node *curr, zval **b
44444454
}
44454455

44464456
switch ( curr->data_type ) {
4457+
case SQL_BOOLEAN:
4458+
case SQL_BIT:
44474459
case SQL_SMALLINT:
44484460
case SQL_INTEGER:
44494461
case SQL_REAL:
@@ -5481,6 +5493,10 @@ PHP_FUNCTION(db2_field_type)
54815493
RETURN_FALSE;
54825494
}
54835495
switch (stmt_res->column_info[col].type) {
5496+
case SQL_BOOLEAN:
5497+
case SQL_BIT:
5498+
str_val = "boolean";
5499+
break;
54845500
case SQL_SMALLINT:
54855501
case SQL_INTEGER:
54865502
case SQL_BIGINT:
@@ -5931,6 +5947,9 @@ PHP_FUNCTION(db2_result)
59315947
}
59325948
break;
59335949

5950+
/* BOOLEAN can't be represented as true/false because false is considered an error */
5951+
case SQL_BOOLEAN:
5952+
case SQL_BIT:
59345953
case SQL_SMALLINT:
59355954
case SQL_INTEGER:
59365955
rc = _php_db2_get_data(stmt_res, col_num+1, SQL_C_LONG, (SQLPOINTER)&long_val, sizeof(long_val), &out_length);
@@ -6290,6 +6309,15 @@ static void _php_db2_bind_fetch_helper(INTERNAL_FUNCTION_PARAMETERS, int op)
62906309
strlen((char *)row_data->str_val));
62916310
}
62926311
break;
6312+
case SQL_BOOLEAN:
6313+
case SQL_BIT:
6314+
if ( op & DB2_FETCH_ASSOC ) {
6315+
add_assoc_bool(return_value, (char *)stmt_res->column_info[i].name, row_data->i_val);
6316+
}
6317+
if ( op & DB2_FETCH_INDEX ) {
6318+
add_index_bool(return_value, i, row_data->i_val);
6319+
}
6320+
break;
62936321
case SQL_SMALLINT:
62946322
if ( op & DB2_FETCH_ASSOC ) {
62956323
add_assoc_long(return_value, (char *)stmt_res->column_info[i].name, row_data->s_val);

php_ibm_db2_int.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ typedef uint32_t uintptr_t;
1515
#endif
1616
#endif
1717

18+
/*
19+
* Added in IBM i 7.5, also in LUW 11.1 MP1/FP1
20+
* see: https://www.ibm.com/docs/en/db2/11.1?topic=database-mod-pack-fix-pack-updates#c0061179__FP1
21+
*/
22+
#ifndef SQL_BOOLEAN
23+
#define SQL_BOOLEAN 16
24+
#endif
25+
1826
/* Needed for backward compatibility (SQL_ATTR_DBC_SYS_NAMING not defined prior to DB2 10.1.0.2) */
1927
#ifndef SQL_ATTR_DBC_SYS_NAMING
2028
#define SQL_ATTR_DBC_SYS_NAMING 3017

tests/test_boolean.phpt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
IBM-DB2: Boolean data type test.
3+
--SKIPIF--
4+
<?php
5+
6+
require_once('skipif.inc');
7+
8+
// This test requires IBM i 7.5 or DB2/LUW 9.7 for the boolean type
9+
require_once('connection.inc');
10+
$conn = db2_connect($database, $user, $password);
11+
if (!$conn) {
12+
die("skip can't connect to DB:" . db2_conn_errormsg());
13+
}
14+
15+
$info = db2_server_info($conn);
16+
if ($info->DBMS_NAME == "AS") { // IBM i
17+
// DBMS_VER is VVRRM string
18+
$major = $info->DBMS_VER[1];
19+
$minor = $info->DBMS_VER[3];
20+
$mod = $info->DBMS_VER[4];
21+
if (!version_compare("$major.$minor.$mod", "7.5.0", ">=")) {
22+
die("skip IBM i version too old");
23+
}
24+
} else { // Should cover i.e. DB2/LINUX
25+
// DBMS_VER is VV.RR.MMMM, version_compore should work directly
26+
if (!version_compare($info->DBMS_VER, "9.7.0", ">=")) {
27+
die("skip DB2 version too old");
28+
}
29+
}
30+
// XXX: z
31+
32+
?>
33+
--FILE--
34+
<?php
35+
36+
require_once('connection.inc');
37+
$conn = db2_connect($database, $user, $password);
38+
39+
if (!$conn) {
40+
echo "Error connecting to database " . db2_conn_errormsg() ."\n";
41+
exit;
42+
}
43+
44+
$s = db2_exec($conn, "values (true, false)");
45+
$r = db2_fetch_array($s);
46+
var_dump($r);
47+
48+
// db2_result can't return true/false due to API limitations, so these will be ints
49+
$s = db2_exec($conn, "values (true, false)");
50+
db2_fetch_row($s);
51+
var_dump(db2_result($s, 0));
52+
var_dump(db2_result($s, 1));
53+
echo db2_field_type($s, 1) . "\n";
54+
55+
db2_close($conn);
56+
57+
?>
58+
--EXPECT--
59+
array(2) {
60+
[0]=>
61+
bool(true)
62+
[1]=>
63+
bool(false)
64+
}
65+
int(1)
66+
int(0)
67+
boolean

0 commit comments

Comments
 (0)