Skip to content

Commit 5c4cabc

Browse files
committed
Address code review comments
1 parent 6a4704c commit 5c4cabc

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

ext/odbc/php_odbc.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,11 @@ static zend_function *odbc_result_get_constructor(zend_object *object) {
146146

147147
static void odbc_result_free(odbc_result *res)
148148
{
149-
int i;
150-
151149
if (res->values) {
152-
for(i = 0; i < res->numcols; i++) {
153-
if (res->values[i].value)
150+
for (int i = 0; i < res->numcols; i++) {
151+
if (res->values[i].value) {
154152
efree(res->values[i].value);
153+
}
155154
}
156155
efree(res->values);
157156
res->values = NULL;
@@ -165,7 +164,6 @@ static void odbc_result_free(odbc_result *res)
165164
SQLFreeStmt(res->stmt,SQL_DROP);
166165
/* We don't want the connection to be closed after the last statement has been closed
167166
* Connections will be closed on shutdown
168-
* zend_list_delete(res->conn_ptr->id);
169167
*/
170168
}
171169
if (res->param_info) {
@@ -190,6 +188,7 @@ static void odbc_result_free_obj(zend_object *obj) {
190188

191189
PHP_ODBC_API ZEND_DECLARE_MODULE_GLOBALS(odbc)
192190
static PHP_GINIT_FUNCTION(odbc);
191+
static PHP_GSHUTDOWN_FUNCTION(odbc);
193192

194193
/* {{{ odbc_module_entry */
195194
zend_module_entry odbc_module_entry = {
@@ -204,7 +203,7 @@ zend_module_entry odbc_module_entry = {
204203
PHP_ODBC_VERSION,
205204
PHP_MODULE_GLOBALS(odbc),
206205
PHP_GINIT(odbc),
207-
NULL,
206+
PHP_GSHUTDOWN(odbc),
208207
NULL,
209208
STANDARD_MODULE_PROPERTIES_EX
210209
};
@@ -218,12 +217,14 @@ ZEND_GET_MODULE(odbc)
218217
#endif
219218

220219
static void close_results_with_connection(const odbc_connection *conn) {
220+
zend_ulong num_index;
221221
zval *p;
222222

223-
ZEND_HASH_FOREACH_VAL(&ODBCG(results), p) {
223+
ZEND_HASH_FOREACH_NUM_KEY_VAL(&ODBCG(results), num_index, p) {
224224
odbc_result *result = Z_ODBC_RESULT_P(p);
225225
if (result->conn_ptr == conn) {
226226
odbc_result_free((odbc_result*) p);
227+
zend_hash_index_del(&ODBCG(results), num_index);
227228
}
228229
} ZEND_HASH_FOREACH_END();
229230
}
@@ -443,6 +444,12 @@ static PHP_GINIT_FUNCTION(odbc)
443444
zend_hash_init(&odbc_globals->connections, 0, NULL, NULL, 1);
444445
}
445446

447+
static PHP_GSHUTDOWN_FUNCTION(odbc)
448+
{
449+
zend_hash_destroy(&odbc_globals->results);
450+
zend_hash_destroy(&odbc_globals->connections);
451+
}
452+
446453
/* {{{ PHP_MINIT_FUNCTION */
447454
PHP_MINIT_FUNCTION(odbc)
448455
{
@@ -510,9 +517,6 @@ PHP_RSHUTDOWN_FUNCTION(odbc)
510517
/* {{{ PHP_MSHUTDOWN_FUNCTION */
511518
PHP_MSHUTDOWN_FUNCTION(odbc)
512519
{
513-
zend_hash_destroy(&ODBCG(results));
514-
zend_hash_destroy(&ODBCG(connections));
515-
516520
UNREGISTER_INI_ENTRIES();
517521
return SUCCESS;
518522
}
@@ -819,6 +823,8 @@ PHP_FUNCTION(odbc_close_all)
819823
odbc_result_free((odbc_result*) p);
820824
} ZEND_HASH_FOREACH_END();
821825

826+
zend_hash_clean(&ODBCG(results));
827+
822828
/* Second loop through list, now close all non-persistent connections */
823829
ZEND_HASH_FOREACH_VAL(&ODBCG(connections), p) {
824830
odbc_link_free((odbc_link*) p);
@@ -912,7 +918,7 @@ PHP_FUNCTION(odbc_prepare)
912918
break;
913919
default:
914920
odbc_sql_error(conn, result->stmt, "SQLPrepare");
915-
efree(result);
921+
zval_ptr_dtor(return_value);
916922
RETURN_FALSE;
917923
}
918924

@@ -940,6 +946,8 @@ PHP_FUNCTION(odbc_prepare)
940946
RETURN_FALSE;
941947
}
942948
}
949+
950+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
943951
}
944952
/* }}} */
945953

@@ -1314,6 +1322,8 @@ PHP_FUNCTION(odbc_exec)
13141322
Z_ADDREF_P(pv_conn);
13151323
result->conn_ptr = conn;
13161324
result->fetched = 0;
1325+
1326+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
13171327
}
13181328
/* }}} */
13191329

@@ -2072,7 +2082,7 @@ PHP_FUNCTION(odbc_pconnect)
20722082
/* }}} */
20732083

20742084
/* {{{ odbc_sqlconnect */
2075-
bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int persistent, zend_string *hash)
2085+
bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, bool persistent, zend_string *hash)
20762086
{
20772087
RETCODE rc;
20782088
odbc_link *link;
@@ -2103,7 +2113,6 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int
21032113
rc = SQLSetConnectOption(link->connection->hdbc, SQL_ODBC_CURSORS, cur_opt);
21042114
if (rc != SQL_SUCCESS) { /* && rc != SQL_SUCCESS_WITH_INFO ? */
21052115
odbc_sql_error(link->connection, SQL_NULL_HSTMT, "SQLSetConnectOption");
2106-
SQLFreeConnect(link->connection->hdbc);
21072116
return false;
21082117
}
21092118
}
@@ -2201,7 +2210,6 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int
22012210
#endif
22022211
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
22032212
odbc_sql_error(link->connection, SQL_NULL_HSTMT, "SQLConnect");
2204-
SQLFreeConnect(link->connection->hdbc);
22052213
return false;
22062214
}
22072215
return true;
@@ -2269,7 +2277,7 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
22692277
RETURN_FALSE;
22702278
}
22712279

2272-
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, 1, hashed_details.s)) {
2280+
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, true, hashed_details.s)) {
22732281
smart_str_free(&hashed_details);
22742282
zval_ptr_dtor(return_value);
22752283
RETURN_FALSE;
@@ -2344,8 +2352,9 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
23442352
RETURN_FALSE;
23452353
}
23462354

2347-
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, 0, hashed_details.s)) {
2355+
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, false, hashed_details.s)) {
23482356
smart_str_free(&hashed_details);
2357+
zval_ptr_dtor(return_value);
23492358
RETURN_FALSE;
23502359
}
23512360
ODBCG(num_links)++;
@@ -2805,6 +2814,8 @@ PHP_FUNCTION(odbc_tables)
28052814
}
28062815
result->conn_ptr = conn;
28072816
result->fetched = 0;
2817+
2818+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
28082819
}
28092820
/* }}} */
28102821

@@ -2871,6 +2882,8 @@ PHP_FUNCTION(odbc_columns)
28712882
}
28722883
result->conn_ptr = conn;
28732884
result->fetched = 0;
2885+
2886+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
28742887
}
28752888
/* }}} */
28762889

@@ -2931,6 +2944,8 @@ PHP_FUNCTION(odbc_columnprivileges)
29312944
}
29322945
result->conn_ptr = conn;
29332946
result->fetched = 0;
2947+
2948+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
29342949
}
29352950
/* }}} */
29362951
#endif /* HAVE_DBMAKER || HAVE_SOLID*/
@@ -2992,7 +3007,7 @@ PHP_FUNCTION(odbc_foreignkeys)
29923007

29933008
if (rc == SQL_ERROR) {
29943009
odbc_sql_error(conn, result->stmt, "SQLForeignKeys");
2995-
efree(result);
3010+
zval_ptr_dtor(return_value);
29963011
RETURN_FALSE;
29973012
}
29983013

@@ -3006,16 +3021,15 @@ PHP_FUNCTION(odbc_foreignkeys)
30063021
}
30073022
result->conn_ptr = conn;
30083023
result->fetched = 0;
3024+
3025+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
30093026
}
30103027
/* }}} */
30113028
#endif /* HAVE_SOLID */
30123029

30133030
/* {{{ Returns a result identifier containing information about data types supported by the data source */
30143031
PHP_FUNCTION(odbc_gettypeinfo)
30153032
{
3016-
zend_value_error("Nope");
3017-
RETURN_THROWS();
3018-
30193033
zval *pv_conn;
30203034
zend_long pv_data_type = SQL_ALL_TYPES;
30213035
odbc_result *result = NULL;
@@ -3027,8 +3041,6 @@ PHP_FUNCTION(odbc_gettypeinfo)
30273041
RETURN_THROWS();
30283042
}
30293043

3030-
printf("Hell noo");
3031-
30323044
data_type = (SQLSMALLINT) pv_data_type;
30333045

30343046
conn = Z_ODBC_CONNECTION_P(pv_conn);
@@ -3068,6 +3080,8 @@ PHP_FUNCTION(odbc_gettypeinfo)
30683080
}
30693081
result->conn_ptr = conn;
30703082
result->fetched = 0;
3083+
3084+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
30713085
}
30723086
/* }}} */
30733087

0 commit comments

Comments
 (0)