Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed May 13, 2020
1 parent 70288b9 commit d4a0cb2
Showing 1 changed file with 83 additions and 60 deletions.
143 changes: 83 additions & 60 deletions swoole_postgresql_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -915,64 +915,82 @@ static void _php_pgsql_free_params(char **params, int num_params)
}

/* {{{ void php_pgsql_get_field_value */
static inline void php_pgsql_get_field_value(zval *value, PGresult *pgsql_result, zend_long result_type, int row, int column)
static inline void php_pgsql_get_field_value(zval *value, PGresult *pgsql_result, zend_long result_type, int row,
int column)
{
if (PQgetisnull(pgsql_result, row, column)) {
ZVAL_NULL(value);
} else {
char *element = PQgetvalue(pgsql_result, row, column);
if (element) {
const size_t element_len = PQgetlength(pgsql_result, row, column);
Oid pgsql_type = PQftype(pgsql_result, column);

switch (pgsql_type) {
case BOOLOID:
ZVAL_BOOL(value, *element == 't');
break;
case FLOAT4OID:
case FLOAT8OID:
if (element_len == sizeof("Infinity") - 1 && strcmp(element, "Infinity") == 0) {
ZVAL_DOUBLE(value, ZEND_INFINITY);
} else if (element_len == sizeof("-Infinity") - 1 && strcmp(element, "-Infinity") == 0) {
ZVAL_DOUBLE(value, -ZEND_INFINITY);
} else if (element_len == sizeof("NaN") - 1 && strcmp(element, "NaN") == 0) {
ZVAL_DOUBLE(value, ZEND_NAN);
} else {
ZVAL_DOUBLE(value, zend_strtod(element, NULL));
}
break;
case OIDOID:
case INT2OID:
case INT4OID:
if (PQgetisnull(pgsql_result, row, column))
{
ZVAL_NULL(value);
}
else
{
char *element = PQgetvalue(pgsql_result, row, column);
if (element)
{
const size_t element_len = PQgetlength(pgsql_result, row, column);
Oid pgsql_type = PQftype(pgsql_result, column);

switch (pgsql_type)
{
case BOOLOID:
ZVAL_BOOL(value, *element == 't');
break;
case FLOAT4OID:
case FLOAT8OID:
if (element_len == sizeof("Infinity") - 1 && strcmp(element, "Infinity") == 0)
{
ZVAL_DOUBLE(value, ZEND_INFINITY);
}
else if (element_len == sizeof("-Infinity") - 1 && strcmp(element, "-Infinity") == 0)
{
ZVAL_DOUBLE(value, -ZEND_INFINITY);
}
else if (element_len == sizeof("NaN") - 1 && strcmp(element, "NaN") == 0)
{
ZVAL_DOUBLE(value, ZEND_NAN);
}
else
{
ZVAL_DOUBLE(value, zend_strtod(element, NULL));
}
break;
case OIDOID:
case INT2OID:
case INT4OID:
#if SIZEOF_ZEND_LONG >= 8
case INT8OID:
case INT8OID:
#endif
{
zend_long long_value;
ZEND_ATOL(long_value, element);
ZVAL_LONG(value, long_value);
break;
}
case BYTEAOID:
{
size_t tmp_len;
char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *)element, &tmp_len);
if (!tmp_ptr) {
/* PQunescapeBytea returned an error */
ZVAL_NULL(value);
} else {
ZVAL_STRINGL(value, tmp_ptr, tmp_len);
PQfreemem(tmp_ptr);
}
break;
}
default:
ZVAL_STRINGL(value, element, element_len);
}
} else {
ZVAL_NULL(value);
}
}
{
zend_long long_value;
ZEND_ATOL(long_value, element);
ZVAL_LONG(value, long_value);
break;
}
case BYTEAOID:
{
size_t tmp_len;
char *tmp_ptr = (char *) PQunescapeBytea((unsigned char *) element, &tmp_len);
if (!tmp_ptr)
{
/* PQunescapeBytea returned an error */
ZVAL_NULL(value);
}
else
{
ZVAL_STRINGL(value, tmp_ptr, tmp_len);
PQfreemem(tmp_ptr);
}
break;
}
default:
ZVAL_STRINGL(value, element, element_len);
}
}
else
{
ZVAL_NULL(value);
}
}
}
/* }}} */

Expand All @@ -987,19 +1005,24 @@ int swoole_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_
uint32_t i;
assert(Z_TYPE_P(ret_array) == IS_ARRAY);

if ((pg_numrows = PQntuples(pg_result)) <= 0) {
if ((pg_numrows = PQntuples(pg_result)) <= 0)
{
return FAILURE;
}
for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
for (pg_row = 0; pg_row < pg_numrows; pg_row++)
{
array_init(&row);
for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
if (result_type & PGSQL_ASSOC) {
for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++)
{
if (result_type & PGSQL_ASSOC)
{
zval value;
php_pgsql_get_field_value(&value, pg_result, result_type, pg_row, i);
field_name = PQfname(pg_result, i);
add_assoc_zval(&row, field_name, &value);
}
if (result_type & PGSQL_NUM) {
if (result_type & PGSQL_NUM)
{
zval value;
php_pgsql_get_field_value(&value, pg_result, result_type, pg_row, i);
add_next_index_zval(&row, &value);
Expand Down

0 comments on commit d4a0cb2

Please sign in to comment.