Skip to content

Commit bb15fc6

Browse files
committed
Introduce fb_insert_aliases
1 parent 6851f29 commit bb15fc6

4 files changed

Lines changed: 72 additions & 18 deletions

File tree

ibase_query.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,36 @@ static int _php_ibase_arr_zval(zval *ar_zval, char *data, zend_ulong data_size,
15501550
}
15511551
/* }}} */
15521552

1553+
void _php_ibase_insert_alias(HashTable *ht, const char *alias, size_t alias_len)
1554+
{
1555+
char buf[METADATALENGTH + 3 + 1]; // _00 + \0
1556+
zval t2;
1557+
int i = 0;
1558+
char const *base = "FIELD"; /* use 'FIELD' if name is empty */
1559+
size_t alias_len_w_suff = alias_len + 3;
1560+
1561+
switch (*alias) {
1562+
void *p;
1563+
1564+
default:
1565+
i = 1;
1566+
base = alias;
1567+
1568+
while ((p = zend_symtable_str_find_ptr(
1569+
ht, alias, alias_len)) != NULL) {
1570+
1571+
case '\0':
1572+
// TODO: i > 99?
1573+
snprintf(buf, sizeof(buf), "%s_%02d", base, i++);
1574+
alias = buf;
1575+
alias_len = alias_len_w_suff;
1576+
}
1577+
}
1578+
1579+
ZVAL_NULL(&t2);
1580+
zend_hash_str_add_new(ht, alias, alias_len, &t2);
1581+
}
1582+
15531583
static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type) /* {{{ */
15541584
{
15551585
zval *result_arg;

pdo_firebird_utils.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
#if FB_API_VER >= 40
2020

21-
#include "pdo_firebird_utils.h"
2221
#include <firebird/Interface.h>
2322
#include <cstring>
23+
#include "php.h"
24+
#include "pdo_firebird_utils.h"
25+
#include "php_ibase_includes.h"
2426

2527
/* Returns the client version. 0 bytes are minor version, 1 bytes are major version. */
2628
extern "C" unsigned fb_get_client_version(void)
@@ -81,24 +83,38 @@ extern "C" void fb_decode_timestamp_tz(const ISC_TIMESTAMP_TZ* timestampTz,
8183
timeZoneBufferLength, timeZoneBuffer);
8284
}
8385

84-
extern "C" int fb_get_sql_info(ISC_STATUS* st, isc_stmt_handle* stmt,
85-
unsigned itemsLength, const unsigned char* items,
86-
unsigned bufferLength, unsigned char* buffer)
86+
extern "C" int fb_insert_aliases(ISC_STATUS* st, ibase_query *ib_query)
8787
{
8888
Firebird::IMaster* master = Firebird::fb_get_master_interface();
8989
Firebird::ThrowStatusWrapper status(master->getStatus());
9090
Firebird::IStatement* statement = NULL;
91+
Firebird::IMessageMetadata* meta = NULL;
92+
ISC_STATUS res;
9193

92-
if (fb_get_statement_interface(st, &statement, stmt)){
93-
// if (sv[0] == 1 && sv[1] > 0)
94-
return st[1];
94+
if (res = fb_get_statement_interface(st, &statement, &ib_query->stmt)){
95+
return res;
9596
}
9697

97-
// TODO: check status;
98-
statement->getInfo(&status, itemsLength, items, bufferLength, buffer);
98+
try {
99+
meta = statement->getOutputMetadata(&status);
100+
unsigned cols = meta->getCount(&status);
99101

100-
statement->release();
101-
statement = NULL;
102+
assert(cols == ib_query->out_fields_count);
103+
104+
zval t;
105+
for (unsigned i = 0; i < cols; ++i)
106+
{
107+
_php_ibase_insert_alias(ib_query->ht_aliases,
108+
meta->getAlias(&status, i), strlen(meta->getAlias(&status, i)));
109+
}
110+
}
111+
catch (const Firebird::FbException& error)
112+
{
113+
if (status.hasData()) {
114+
fb_copy_status((const ISC_STATUS*)status.getErrors(), st, 20);
115+
return st[1];
116+
}
117+
}
102118

103119
return 0;
104120
}

pdo_firebird_utils.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
#ifndef PDO_FIREBIRD_UTILS_H
1818
#define PDO_FIREBIRD_UTILS_H
1919

20+
#if FB_API_VER >= 40
21+
2022
#include <ibase.h>
23+
#include "php_ibase_includes.h"
2124

2225
#ifdef __cplusplus
2326
extern "C" {
@@ -29,8 +32,6 @@ ISC_TIME fb_encode_time(unsigned hours, unsigned minutes, unsigned seconds, unsi
2932

3033
ISC_DATE fb_encode_date(unsigned year, unsigned month, unsigned day);
3134

32-
#if FB_API_VER >= 40
33-
3435
void fb_decode_time_tz(const ISC_TIME_TZ* timeTz, unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
3536
unsigned timeZoneBufferLength, char* timeZoneBuffer);
3637

@@ -39,14 +40,11 @@ void fb_decode_timestamp_tz(const ISC_TIMESTAMP_TZ* timestampTz,
3940
unsigned* hours, unsigned* minutes, unsigned* seconds, unsigned* fractions,
4041
unsigned timeZoneBufferLength, char* timeZoneBuffer);
4142

42-
int fb_get_sql_info(ISC_STATUS* st, isc_stmt_handle* stmt,
43-
unsigned itemsLength, const unsigned char* items,
44-
unsigned bufferLength, unsigned char* buffer);
45-
46-
#endif
43+
int fb_insert_aliases(ISC_STATUS* st, ibase_query *ib_query);
4744

4845
#ifdef __cplusplus
4946
}
5047
#endif
48+
#endif
5149

5250
#endif /* PDO_FIREBIRD_UTILS_H */

php_ibase_includes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ void _php_ibase_free_event(ibase_event *event);
249249
/* provided by ibase_service.c */
250250
void php_ibase_service_minit(INIT_FUNC_ARGS);
251251

252+
#ifdef __cplusplus
253+
extern "C" {
254+
#endif
255+
256+
void _php_ibase_insert_alias(HashTable *ht, const char *alias, size_t alias_len);
257+
258+
#ifdef __cplusplus
259+
}
260+
#endif
261+
252262
#ifndef max
253263
#define max(a,b) ((a)>(b)?(a):(b))
254264
#endif

0 commit comments

Comments
 (0)