Skip to content

Commit 96b62de

Browse files
committed
Address code review comments
1 parent 7946145 commit 96b62de

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
}
@@ -2214,7 +2223,6 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, int
22142223
#endif
22152224
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
22162225
odbc_sql_error(link->connection, SQL_NULL_HSTMT, "SQLConnect");
2217-
SQLFreeConnect(link->connection->hdbc);
22182226
return false;
22192227
}
22202228
return true;
@@ -2282,7 +2290,7 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
22822290
RETURN_FALSE;
22832291
}
22842292

2285-
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, 1, hashed_details.s)) {
2293+
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, true, hashed_details.s)) {
22862294
smart_str_free(&hashed_details);
22872295
zval_ptr_dtor(return_value);
22882296
RETURN_FALSE;
@@ -2357,8 +2365,9 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
23572365
RETURN_FALSE;
23582366
}
23592367

2360-
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, 0, hashed_details.s)) {
2368+
if (!odbc_sqlconnect(return_value, db, uid, pwd, cur_opt, false, hashed_details.s)) {
23612369
smart_str_free(&hashed_details);
2370+
zval_ptr_dtor(return_value);
23622371
RETURN_FALSE;
23632372
}
23642373
ODBCG(num_links)++;
@@ -2824,6 +2833,8 @@ PHP_FUNCTION(odbc_tables)
28242833
}
28252834
result->conn_ptr = conn;
28262835
result->fetched = 0;
2836+
2837+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
28272838
}
28282839
/* }}} */
28292840

@@ -2893,6 +2904,8 @@ PHP_FUNCTION(odbc_columns)
28932904
}
28942905
result->conn_ptr = conn;
28952906
result->fetched = 0;
2907+
2908+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
28962909
}
28972910
/* }}} */
28982911

@@ -2956,6 +2969,8 @@ PHP_FUNCTION(odbc_columnprivileges)
29562969
}
29572970
result->conn_ptr = conn;
29582971
result->fetched = 0;
2972+
2973+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
29592974
}
29602975
/* }}} */
29612976
#endif /* HAVE_DBMAKER || HAVE_SOLID*/
@@ -3017,7 +3032,7 @@ PHP_FUNCTION(odbc_foreignkeys)
30173032

30183033
if (rc == SQL_ERROR) {
30193034
odbc_sql_error(conn, result->stmt, "SQLForeignKeys");
3020-
efree(result);
3035+
zval_ptr_dtor(return_value);
30213036
RETURN_FALSE;
30223037
}
30233038

@@ -3034,16 +3049,15 @@ PHP_FUNCTION(odbc_foreignkeys)
30343049
}
30353050
result->conn_ptr = conn;
30363051
result->fetched = 0;
3052+
3053+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
30373054
}
30383055
/* }}} */
30393056
#endif /* HAVE_SOLID */
30403057

30413058
/* {{{ Returns a result identifier containing information about data types supported by the data source */
30423059
PHP_FUNCTION(odbc_gettypeinfo)
30433060
{
3044-
zend_value_error("Nope");
3045-
RETURN_THROWS();
3046-
30473061
zval *pv_conn;
30483062
zend_long pv_data_type = SQL_ALL_TYPES;
30493063
odbc_result *result = NULL;
@@ -3055,8 +3069,6 @@ PHP_FUNCTION(odbc_gettypeinfo)
30553069
RETURN_THROWS();
30563070
}
30573071

3058-
printf("Hell noo");
3059-
30603072
data_type = (SQLSMALLINT) pv_data_type;
30613073

30623074
conn = Z_ODBC_CONNECTION_P(pv_conn);
@@ -3099,6 +3111,8 @@ PHP_FUNCTION(odbc_gettypeinfo)
30993111
}
31003112
result->conn_ptr = conn;
31013113
result->fetched = 0;
3114+
3115+
zend_hash_next_index_insert_new(&ODBCG(results), return_value);
31023116
}
31033117
/* }}} */
31043118

0 commit comments

Comments
 (0)