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

Commit 2c87b4d

Browse files
committed
code format
1 parent 8aa7157 commit 2c87b4d

File tree

1 file changed

+112
-98
lines changed

1 file changed

+112
-98
lines changed

swoole_postgresql_coro.cc

Lines changed: 112 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ void swoole_postgresql_init(int module_number) {
249249
NULL,
250250
"Co\\PostgreSQL",
251251
swoole_postgresql_coro_methods);
252+
#ifdef SW_SET_CLASS_NOT_SERIALIZABLE
253+
SW_SET_CLASS_NOT_SERIALIZABLE(swoole_postgresql_coro);
254+
#else
252255
SW_SET_CLASS_SERIALIZABLE(swoole_postgresql_coro, zend_class_serialize_deny, zend_class_unserialize_deny);
256+
#endif
253257
SW_SET_CLASS_CLONEABLE(swoole_postgresql_coro, sw_zend_class_clone_deny);
254258
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_postgresql_coro, sw_zend_class_unset_property_deny);
255259
SW_SET_CLASS_CUSTOM_OBJECT(swoole_postgresql_coro,
@@ -270,7 +274,7 @@ void swoole_postgresql_init(int module_number) {
270274
SW_REGISTER_LONG_CONSTANT("SW_PGSQL_BOTH", PGSQL_BOTH);
271275
}
272276

273-
static char * _php_pgsql_trim_message(const char *message, size_t *len) {
277+
static char *_php_pgsql_trim_message(const char *message, size_t *len) {
274278
size_t i = strlen(message);
275279
if (i > 2 && (message[i - 2] == '\r' || message[i - 2] == '\n') && message[i - 1] == '.') {
276280
--i;
@@ -291,7 +295,8 @@ static void _php_pgsql_notice_handler(void *resource_id, const char *message) {
291295
pg_object *object = (pg_object *) resource_id;
292296

293297
if (!object->ignore_notices) {
294-
notices = sw_zend_read_and_convert_property_array(swoole_postgresql_coro_ce, &object->_object, ZEND_STRL("notices"), 0);
298+
notices = sw_zend_read_and_convert_property_array(
299+
swoole_postgresql_coro_ce, &object->_object, ZEND_STRL("notices"), 0);
295300

296301
trimed_message = _php_pgsql_trim_message(message, &trimed_message_len);
297302
if (object->log_notices) {
@@ -443,7 +448,8 @@ static void connect_callback(pg_object *object, Reactor *reactor, Event *event)
443448
case PGRES_POLLING_FAILED:
444449
events = 0;
445450
err_msg = PQerrorMessage(conn);
446-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), err_msg);
451+
zend_update_property_string(
452+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), err_msg);
447453
break;
448454
default:
449455
swWarn("PQconnectPoll unexpected status");
@@ -570,48 +576,44 @@ static int meta_data_result_parse(pg_object *object) {
570576
}
571577

572578
static void set_error_diag(const pg_object *object, const PGresult *pgsql_result) {
573-
const unsigned int error_codes[] = {
574-
PG_DIAG_SEVERITY,
575-
PG_DIAG_SQLSTATE,
576-
PG_DIAG_MESSAGE_PRIMARY,
577-
PG_DIAG_MESSAGE_DETAIL,
578-
PG_DIAG_MESSAGE_HINT,
579-
PG_DIAG_STATEMENT_POSITION,
580-
PG_DIAG_INTERNAL_POSITION,
581-
PG_DIAG_INTERNAL_QUERY,
582-
PG_DIAG_CONTEXT,
583-
PG_DIAG_SCHEMA_NAME,
584-
PG_DIAG_TABLE_NAME,
585-
PG_DIAG_COLUMN_NAME,
586-
PG_DIAG_DATATYPE_NAME,
587-
PG_DIAG_CONSTRAINT_NAME,
588-
PG_DIAG_SOURCE_FILE,
589-
PG_DIAG_SOURCE_LINE,
590-
PG_DIAG_SOURCE_FUNCTION
591-
};
592-
593-
const char* error_names[] = {
594-
"severity",
595-
"sqlstate",
596-
"message_primary",
597-
"message_detail",
598-
"message_hint",
599-
"statement_position",
600-
"internal_position",
601-
"internal_query",
602-
"content",
603-
"schema_name",
604-
"table_name",
605-
"column_name",
606-
"datatype_name",
607-
"constraint_name",
608-
"source_file",
609-
"source_line",
610-
"source_function"
611-
};
579+
const unsigned int error_codes[] = {PG_DIAG_SEVERITY,
580+
PG_DIAG_SQLSTATE,
581+
PG_DIAG_MESSAGE_PRIMARY,
582+
PG_DIAG_MESSAGE_DETAIL,
583+
PG_DIAG_MESSAGE_HINT,
584+
PG_DIAG_STATEMENT_POSITION,
585+
PG_DIAG_INTERNAL_POSITION,
586+
PG_DIAG_INTERNAL_QUERY,
587+
PG_DIAG_CONTEXT,
588+
PG_DIAG_SCHEMA_NAME,
589+
PG_DIAG_TABLE_NAME,
590+
PG_DIAG_COLUMN_NAME,
591+
PG_DIAG_DATATYPE_NAME,
592+
PG_DIAG_CONSTRAINT_NAME,
593+
PG_DIAG_SOURCE_FILE,
594+
PG_DIAG_SOURCE_LINE,
595+
PG_DIAG_SOURCE_FUNCTION};
596+
597+
const char *error_names[] = {"severity",
598+
"sqlstate",
599+
"message_primary",
600+
"message_detail",
601+
"message_hint",
602+
"statement_position",
603+
"internal_position",
604+
"internal_query",
605+
"content",
606+
"schema_name",
607+
"table_name",
608+
"column_name",
609+
"datatype_name",
610+
"constraint_name",
611+
"source_file",
612+
"source_line",
613+
"source_function"};
612614

613615
long unsigned int i;
614-
char* error_result;
616+
char *error_result;
615617

616618
zval result_diag;
617619
array_init_size(&result_diag, sizeof(error_codes) / sizeof(int));
@@ -643,10 +645,10 @@ static int query_result_parse(pg_object *object) {
643645
pgsql_result = PQgetResult(object->conn);
644646
status = PQresultStatus(pgsql_result);
645647

646-
zend_update_property_long(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultStatus"), status);
648+
zend_update_property_long(
649+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultStatus"), status);
647650

648-
switch (status)
649-
{
651+
switch (status) {
650652
case PGRES_EMPTY_QUERY:
651653
case PGRES_BAD_RESPONSE:
652654
case PGRES_NONFATAL_ERROR:
@@ -656,7 +658,8 @@ static int query_result_parse(pg_object *object) {
656658
PQclear(pgsql_result);
657659
ZVAL_FALSE(&return_value);
658660
swoole_event_del(object->socket);
659-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), err_msg);
661+
zend_update_property_string(
662+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), err_msg);
660663
PHPCoroutine::resume_m(context, &return_value);
661664
break;
662665
case PGRES_COMMAND_OK: /* successful command that did not return rows */
@@ -694,48 +697,52 @@ static int prepare_result_parse(pg_object *object) {
694697
pgsql_result = PQgetResult(object->conn);
695698
status = PQresultStatus(pgsql_result);
696699

697-
zend_update_property_long(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultStatus"), status);
698-
699-
switch (status)
700-
{
701-
case PGRES_EMPTY_QUERY:
702-
case PGRES_BAD_RESPONSE:
703-
case PGRES_NONFATAL_ERROR:
704-
case PGRES_FATAL_ERROR:
705-
err_msg = PQerrorMessage(object->conn);
706-
set_error_diag(object, pgsql_result);
707-
PQclear(pgsql_result);
708-
ZVAL_FALSE(&return_value);
709-
swoole_event_del(object->socket);
710-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), err_msg);
711-
PHPCoroutine::resume_m(context, &return_value);
712-
if (error != 0) {
713-
php_swoole_fatal_error(E_WARNING, "socket error. Error: %s [%d]", strerror(error), error);
714-
}
715-
break;
716-
case PGRES_COMMAND_OK: /* successful command that did not return rows */
717-
/* Wait to finish sending buffer */
718-
//res = PQflush(object->conn);
719-
PQclear(pgsql_result);
720-
swoole_event_del(object->socket);
721-
ZVAL_TRUE(&return_value);
722-
zend_update_property_null(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"));
723-
zend_update_property_null(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultDiag"));
724-
PHPCoroutine::resume_m(context, &return_value);
725-
if (error != 0) {
726-
php_swoole_fatal_error(E_WARNING, "socket error. Error: %s [%d]", strerror(error), error);
727-
}
728-
break;
729-
default:
730-
PQclear(pgsql_result);
731-
swoole_event_del(object->socket);
732-
ZVAL_FALSE(&return_value);
733-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), "Bad result returned to prepare");
734-
PHPCoroutine::resume_m(context, &return_value);
735-
if (error != 0) {
736-
php_swoole_fatal_error(E_WARNING, "socket error. Error: %s [%d]", strerror(error), error);
737-
}
738-
break;
700+
zend_update_property_long(
701+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultStatus"), status);
702+
703+
switch (status) {
704+
case PGRES_EMPTY_QUERY:
705+
case PGRES_BAD_RESPONSE:
706+
case PGRES_NONFATAL_ERROR:
707+
case PGRES_FATAL_ERROR:
708+
err_msg = PQerrorMessage(object->conn);
709+
set_error_diag(object, pgsql_result);
710+
PQclear(pgsql_result);
711+
ZVAL_FALSE(&return_value);
712+
swoole_event_del(object->socket);
713+
zend_update_property_string(
714+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"), err_msg);
715+
PHPCoroutine::resume_m(context, &return_value);
716+
if (error != 0) {
717+
php_swoole_fatal_error(E_WARNING, "socket error. Error: %s [%d]", strerror(error), error);
718+
}
719+
break;
720+
case PGRES_COMMAND_OK: /* successful command that did not return rows */
721+
/* Wait to finish sending buffer */
722+
// res = PQflush(object->conn);
723+
PQclear(pgsql_result);
724+
swoole_event_del(object->socket);
725+
ZVAL_TRUE(&return_value);
726+
zend_update_property_null(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("error"));
727+
zend_update_property_null(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(object->object), ZEND_STRL("resultDiag"));
728+
PHPCoroutine::resume_m(context, &return_value);
729+
if (error != 0) {
730+
php_swoole_fatal_error(E_WARNING, "socket error. Error: %s [%d]", strerror(error), error);
731+
}
732+
break;
733+
default:
734+
PQclear(pgsql_result);
735+
swoole_event_del(object->socket);
736+
ZVAL_FALSE(&return_value);
737+
zend_update_property_string(swoole_postgresql_coro_ce,
738+
SW_Z8_OBJ_P(object->object),
739+
ZEND_STRL("error"),
740+
"Bad result returned to prepare");
741+
PHPCoroutine::resume_m(context, &return_value);
742+
if (error != 0) {
743+
php_swoole_fatal_error(E_WARNING, "socket error. Error: %s [%d]", strerror(error), error);
744+
}
745+
break;
739746
}
740747
(void) res;
741748

@@ -744,7 +751,7 @@ static int prepare_result_parse(pg_object *object) {
744751

745752
static bool swoole_pgsql_wait_write_ready(PGconn *pgsql, pg_object *object, zval *zthis) {
746753
int retval = 0;
747-
object->co = Coroutine::get_current_safe();;
754+
object->co = Coroutine::get_current_safe();
748755

749756
ON_SCOPE_EXIT {
750757
if (object->socket->isset_writable_event()) {
@@ -759,12 +766,16 @@ static bool swoole_pgsql_wait_write_ready(PGconn *pgsql, pg_object *object, zval
759766
}
760767
object->co->yield_ex(object->timeout);
761768
if (object->co->is_canceled()) {
762-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(zthis), ZEND_STRL("error"),
769+
zend_update_property_string(swoole_postgresql_coro_ce,
770+
SW_Z8_OBJ_P(zthis),
771+
ZEND_STRL("error"),
763772
swoole_strerror(SW_ERROR_CO_CANCELED));
764773
return false;
765774
}
766775
if (object->co->is_timedout()) {
767-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(zthis), ZEND_STRL("error"),
776+
zend_update_property_string(swoole_postgresql_coro_ce,
777+
SW_Z8_OBJ_P(zthis),
778+
ZEND_STRL("error"),
768779
swoole_strerror(SW_ERROR_CO_TIMEDOUT));
769780
return false;
770781
}
@@ -1126,16 +1137,16 @@ static PHP_METHOD(swoole_postgresql_coro, numRows) {
11261137
RETVAL_LONG(PQntuples(pgsql_result));
11271138
}
11281139

1129-
//query's field count
1140+
// query's field count
11301141
static PHP_METHOD(swoole_postgresql_coro, fieldCount) {
11311142
zval *result;
11321143
PGresult *pgsql_result;
11331144

1134-
ZEND_PARSE_PARAMETERS_START(1,1)
1145+
ZEND_PARSE_PARAMETERS_START(1, 1)
11351146
Z_PARAM_RESOURCE(result)
11361147
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
11371148

1138-
if ((pgsql_result = (PGresult *)zend_fetch_resource(Z_RES_P(result), "PostgreSQL result", le_result)) == NULL) {
1149+
if ((pgsql_result = (PGresult *) zend_fetch_resource(Z_RES_P(result), "PostgreSQL result", le_result)) == NULL) {
11391150
RETURN_FALSE;
11401151
}
11411152

@@ -1513,7 +1524,8 @@ static PHP_METHOD(swoole_postgresql_coro, escape) {
15131524
size_t new_len = PQescapeStringConn(object->conn, result->val, str, l_str, &error);
15141525

15151526
if (new_len == 0 || error) {
1516-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), PQerrorMessage(pgsql));
1527+
zend_update_property_string(
1528+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), PQerrorMessage(pgsql));
15171529
zend_update_property_long(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), error);
15181530
zend_string_free(result);
15191531
RETURN_FALSE;
@@ -1538,7 +1550,8 @@ static PHP_METHOD(swoole_postgresql_coro, escapeLiteral) {
15381550

15391551
tmp = PQescapeLiteral(pgsql, str, l_str);
15401552
if (tmp == nullptr) {
1541-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), PQerrorMessage(pgsql));
1553+
zend_update_property_string(
1554+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), PQerrorMessage(pgsql));
15421555

15431556
RETURN_FALSE;
15441557
}
@@ -1561,7 +1574,8 @@ static PHP_METHOD(swoole_postgresql_coro, escapeIdentifier) {
15611574

15621575
tmp = PQescapeIdentifier(pgsql, str, l_str);
15631576
if (tmp == nullptr) {
1564-
zend_update_property_string(swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), PQerrorMessage(pgsql));
1577+
zend_update_property_string(
1578+
swoole_postgresql_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("error"), PQerrorMessage(pgsql));
15651579

15661580
RETURN_FALSE;
15671581
}

0 commit comments

Comments
 (0)