@@ -1108,66 +1108,25 @@ PHP_MINFO_FUNCTION(pgsql)
1108
1108
/* {{{ php_pgsql_do_connect */
1109
1109
static void php_pgsql_do_connect (INTERNAL_FUNCTION_PARAMETERS , int persistent )
1110
1110
{
1111
- char * host = NULL ,* port = NULL ,* options = NULL ,* tty = NULL ,* dbname = NULL ,* connstring = NULL ;
1111
+ char * connstring ;
1112
+ size_t connstring_len ;
1112
1113
PGconn * pgsql ;
1113
1114
smart_str str = {0 };
1114
- zval * args ;
1115
- uint32_t i ;
1116
- int connect_type = 0 ;
1115
+ zend_long connect_type = 0 ;
1117
1116
PGresult * pg_result ;
1118
1117
1119
- args = (zval * )safe_emalloc (ZEND_NUM_ARGS (), sizeof (zval ), 0 );
1120
- if (ZEND_NUM_ARGS () < 1 || ZEND_NUM_ARGS () > 5
1121
- || zend_get_parameters_array_ex (ZEND_NUM_ARGS (), args ) == FAILURE ) {
1122
- efree (args );
1123
- WRONG_PARAM_COUNT ;
1118
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "s|l" , & connstring , & connstring_len , & connect_type ) == FAILURE ) {
1119
+ RETURN_THROWS ();
1124
1120
}
1125
1121
1126
1122
smart_str_appends (& str , "pgsql" );
1127
-
1128
- for (i = 0 ; i < ZEND_NUM_ARGS (); i ++ ) {
1129
- /* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent connections
1130
- * can re-use this connection. Bug #39979
1131
- */
1132
- if (i == 1 && ZEND_NUM_ARGS () == 2 && Z_TYPE (args [i ]) == IS_LONG ) {
1133
- if (Z_LVAL (args [1 ]) == PGSQL_CONNECT_FORCE_NEW ) {
1134
- continue ;
1135
- } else if (Z_LVAL (args [1 ]) & PGSQL_CONNECT_FORCE_NEW ) {
1136
- smart_str_append_long (& str , Z_LVAL (args [1 ]) ^ PGSQL_CONNECT_FORCE_NEW );
1137
- }
1138
- }
1139
- ZVAL_STR (& args [i ], zval_get_string (& args [i ]));
1140
- smart_str_appendc (& str , '_' );
1141
- smart_str_appendl (& str , Z_STRVAL (args [i ]), Z_STRLEN (args [i ]));
1142
- }
1143
-
1144
- /* Exception thrown during a string conversion. */
1145
- if (EG (exception )) {
1146
- goto cleanup ;
1147
- }
1148
-
1123
+ smart_str_appendl (& str , connstring , connstring_len );
1124
+ smart_str_appendc (& str , '_' );
1125
+ /* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent
1126
+ * connections can re-use this connection. See bug #39979. */
1127
+ smart_str_append_long (& str , connect_type & ~PGSQL_CONNECT_FORCE_NEW );
1149
1128
smart_str_0 (& str );
1150
1129
1151
- if (ZEND_NUM_ARGS () == 1 ) { /* new style, using connection string */
1152
- connstring = Z_STRVAL (args [0 ]);
1153
- } else if (ZEND_NUM_ARGS () == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */
1154
- connstring = Z_STRVAL (args [0 ]);
1155
- connect_type = (int )zval_get_long (& args [1 ]);
1156
- } else {
1157
- host = Z_STRVAL (args [0 ]);
1158
- port = Z_STRVAL (args [1 ]);
1159
- dbname = Z_STRVAL (args [ZEND_NUM_ARGS ()- 1 ]);
1160
-
1161
- switch (ZEND_NUM_ARGS ()) {
1162
- case 5 :
1163
- tty = Z_STRVAL (args [3 ]);
1164
- /* fall through */
1165
- case 4 :
1166
- options = Z_STRVAL (args [2 ]);
1167
- break ;
1168
- }
1169
- }
1170
-
1171
1130
if (persistent && PGG (allow_persistent )) {
1172
1131
zend_resource * le ;
1173
1132
@@ -1185,11 +1144,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1185
1144
}
1186
1145
1187
1146
/* create the link */
1188
- if (connstring ) {
1189
- pgsql = PQconnectdb (connstring );
1190
- } else {
1191
- pgsql = PQsetdb (host , port , options , tty , dbname );
1192
- }
1147
+ pgsql = PQconnectdb (connstring );
1193
1148
if (pgsql == NULL || PQstatus (pgsql ) == CONNECTION_BAD ) {
1194
1149
PHP_PQ_ERROR ("Unable to connect to PostgreSQL server: %s" , pgsql )
1195
1150
if (pgsql ) {
@@ -1218,11 +1173,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1218
1173
}
1219
1174
if (PQstatus (le -> ptr ) == CONNECTION_BAD ) { /* the link died */
1220
1175
if (le -> ptr == NULL ) {
1221
- if (connstring ) {
1222
- le -> ptr = PQconnectdb (connstring );
1223
- } else {
1224
- le -> ptr = PQsetdb (host ,port ,options ,tty ,dbname );
1225
- }
1176
+ le -> ptr = PQconnectdb (connstring );
1226
1177
}
1227
1178
else {
1228
1179
PQreset (le -> ptr );
@@ -1270,25 +1221,16 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1270
1221
1271
1222
/* Non-blocking connect */
1272
1223
if (connect_type & PGSQL_CONNECT_ASYNC ) {
1273
- if (connstring ) {
1274
- pgsql = PQconnectStart (connstring );
1275
- if (pgsql == NULL || PQstatus (pgsql )== CONNECTION_BAD ) {
1276
- PHP_PQ_ERROR ("Unable to connect to PostgreSQL server: %s" , pgsql );
1277
- if (pgsql ) {
1278
- PQfinish (pgsql );
1279
- }
1280
- goto err ;
1224
+ pgsql = PQconnectStart (connstring );
1225
+ if (pgsql == NULL || PQstatus (pgsql )== CONNECTION_BAD ) {
1226
+ PHP_PQ_ERROR ("Unable to connect to PostgreSQL server: %s" , pgsql );
1227
+ if (pgsql ) {
1228
+ PQfinish (pgsql );
1281
1229
}
1282
- } else {
1283
- php_error_docref (NULL , E_WARNING , "Connection string required for async connections" );
1284
1230
goto err ;
1285
1231
}
1286
1232
} else {
1287
- if (connstring ) {
1288
- pgsql = PQconnectdb (connstring );
1289
- } else {
1290
- pgsql = PQsetdb (host ,port ,options ,tty ,dbname );
1291
- }
1233
+ pgsql = PQconnectdb (connstring );
1292
1234
if (pgsql == NULL || PQstatus (pgsql )== CONNECTION_BAD ) {
1293
1235
PHP_PQ_ERROR ("Unable to connect to PostgreSQL server: %s" , pgsql );
1294
1236
if (pgsql ) {
@@ -1324,18 +1266,10 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1324
1266
php_pgsql_set_default_link (Z_RES_P (return_value ));
1325
1267
1326
1268
cleanup :
1327
- for (i = 0 ; i < ZEND_NUM_ARGS (); i ++ ) {
1328
- zval_ptr_dtor (& args [i ]);
1329
- }
1330
- efree (args );
1331
1269
smart_str_free (& str );
1332
1270
return ;
1333
1271
1334
1272
err :
1335
- for (i = 0 ; i < ZEND_NUM_ARGS (); i ++ ) {
1336
- zval_ptr_dtor (& args [i ]);
1337
- }
1338
- efree (args );
1339
1273
smart_str_free (& str );
1340
1274
RETURN_FALSE ;
1341
1275
}
0 commit comments