Skip to content

Commit 756b7e1

Browse files
committed
Address code review comments
1 parent 9b4b534 commit 756b7e1

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
}
@@ -820,6 +824,8 @@ PHP_FUNCTION(odbc_close_all)
820824
odbc_result_free((odbc_result*) p);
821825
} ZEND_HASH_FOREACH_END();
822826

827+
zend_hash_clean(&ODBCG(results));
828+
823829
/* Second loop through list, now close all non-persistent connections */
824830
ZEND_HASH_FOREACH_VAL(&ODBCG(connections), p) {
825831
odbc_link_free((odbc_link*) p);
@@ -913,7 +919,7 @@ PHP_FUNCTION(odbc_prepare)
913919
break;
914920
default:
915921
odbc_sql_error(conn, result->stmt, "SQLPrepare");
916-
efree(result);
922+
zval_ptr_dtor(return_value);
917923
RETURN_FALSE;
918924
}
919925

@@ -944,6 +950,8 @@ PHP_FUNCTION(odbc_prepare)
944950
RETURN_FALSE;
945951
}
946952
}
953+
954+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
947955
}
948956
/* }}} */
949957

@@ -1324,6 +1332,8 @@ PHP_FUNCTION(odbc_exec)
13241332
Z_ADDREF_P(pv_conn);
13251333
result->conn_ptr = conn;
13261334
result->fetched = 0;
1335+
1336+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
13271337
}
13281338
/* }}} */
13291339

@@ -2085,7 +2095,7 @@ PHP_FUNCTION(odbc_pconnect)
20852095
/* }}} */
20862096

20872097
/* {{{ odbc_sqlconnect */
2088-
bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int persistent, zend_string *hash)
2098+
bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, bool persistent, zend_string *hash)
20892099
{
20902100
RETCODE rc;
20912101
odbc_link *link;
@@ -2116,7 +2126,6 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int
21162126
rc = SQLSetConnectOption(link->connection->hdbc, SQL_ODBC_CURSORS, cur_opt);
21172127
if (rc != SQL_SUCCESS) { /* && rc != SQL_SUCCESS_WITH_INFO ? */
21182128
odbc_sql_error(link->connection, SQL_NULL_HSTMT, "SQLSetConnectOption");
2119-
SQLFreeConnect(link->connection->hdbc);
21202129
return false;
21212130
}
21222131
}
@@ -2190,7 +2199,6 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int
21902199
#endif
21912200
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
21922201
odbc_sql_error(link->connection, SQL_NULL_HSTMT, "SQLConnect");
2193-
SQLFreeConnect(link->connection->hdbc);
21942202
return false;
21952203
}
21962204
return true;
@@ -2257,7 +2265,7 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
22572265
RETURN_FALSE;
22582266
}
22592267

2260-
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, 1, hashed_details.s)) {
2268+
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, true, hashed_details.s)) {
22612269
smart_str_free(&hashed_details);
22622270
zval_ptr_dtor(return_value);
22632271
RETURN_FALSE;
@@ -2332,8 +2340,9 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
23322340
RETURN_FALSE;
23332341
}
23342342

2335-
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, 0, hashed_details.s)) {
2343+
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, false, hashed_details.s)) {
23362344
smart_str_free(&hashed_details);
2345+
zval_ptr_dtor(return_value);
23372346
RETURN_FALSE;
23382347
}
23392348
ODBCG(num_links)++;
@@ -2799,6 +2808,8 @@ PHP_FUNCTION(odbc_tables)
27992808
}
28002809
result->conn_ptr = conn;
28012810
result->fetched = 0;
2811+
2812+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
28022813
}
28032814
/* }}} */
28042815

@@ -2868,6 +2879,8 @@ PHP_FUNCTION(odbc_columns)
28682879
}
28692880
result->conn_ptr = conn;
28702881
result->fetched = 0;
2882+
2883+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
28712884
}
28722885
/* }}} */
28732886

@@ -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

@@ -3009,16 +3024,15 @@ PHP_FUNCTION(odbc_foreignkeys)
30093024
}
30103025
result->conn_ptr = conn;
30113026
result->fetched = 0;
3027+
3028+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
30123029
}
30133030
/* }}} */
30143031
#endif /* HAVE_SOLID */
30153032

30163033
/* {{{ Returns a result identifier containing information about data types supported by the data source */
30173034
PHP_FUNCTION(odbc_gettypeinfo)
30183035
{
3019-
zend_value_error("Nope");
3020-
RETURN_THROWS();
3021-
30223036
zval *pv_conn;
30233037
zend_long pv_data_type = SQL_ALL_TYPES;
30243038
odbc_result *result = NULL;
@@ -3030,8 +3044,6 @@ PHP_FUNCTION(odbc_gettypeinfo)
30303044
RETURN_THROWS();
30313045
}
30323046

3033-
printf("Hell noo");
3034-
30353047
data_type = (SQLSMALLINT) pv_data_type;
30363048

30373049
conn = Z_ODBC_CONNECTION_P(pv_conn);
@@ -3074,6 +3086,8 @@ PHP_FUNCTION(odbc_gettypeinfo)
30743086
}
30753087
result->conn_ptr = conn;
30763088
result->fetched = 0;
3089+
3090+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
30773091
}
30783092
/* }}} */
30793093

0 commit comments

Comments
 (0)