/** {{{ public ZeRecorder::fetch() */ PHP_METHOD(ze_recorder, fetch) { zval * self = NULL; zval * stmt = NULL; long style = 0; zval * style_z = NULL; zval * success = NULL; zval * row = NULL; /* PDO::FETCH_ASSOC */ style = 2; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l" , &style ) == FAILURE) { WRONG_PARAM_COUNT; } /* call execute */ /* call fetch */ self = getThis(); stmt = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_STMT), 0 TSRMLS_CC); do { if (!stmt || Z_TYPE_P(stmt) != IS_OBJECT) { ze_error(E_ERROR TSRMLS_CC, "recorder.fetch.stmt.not_object"); break; } zend_call_method(&stmt, Z_OBJCE_P(stmt), NULL, ZEND_STRL("execute"), &success, 0, NULL, NULL TSRMLS_CC); if (!success || EG(exception)) { zval_dtor(success); FREE_ZVAL(success); break; } convert_to_boolean(success); if (Z_BVAL_P(success) == 1) { ALLOC_INIT_ZVAL(style_z); ZVAL_LONG(style_z, style); zend_call_method(&stmt, NULL, NULL, ZEND_STRL("fetch"), &row, 1, style_z, NULL TSRMLS_CC); zval_dtor(style_z); FREE_ZVAL(style_z); } zval_dtor(success); FREE_ZVAL(success); } while (0); if (row) { RETURN_ZVAL(row, 1, 1); } else { RETURN_FALSE; } }
/** {{{ public ZeRecorder::exec() */ PHP_METHOD(ze_recorder, exec) { zval * self = NULL; zval * stmt = NULL; zval * success = NULL; zval * row_count = NULL; /* call execute */ /* call rowCount */ self = getThis(); stmt = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_STMT), 0 TSRMLS_CC); do { if (!stmt || Z_TYPE_P(stmt) != IS_OBJECT) { ze_error(E_ERROR TSRMLS_CC, "recorder.exec.stmt.not_object"); break; } zend_call_method(&stmt, Z_OBJCE_P(stmt), NULL, ZEND_STRL("execute"), &success, 0, NULL, NULL TSRMLS_CC); if (!success || EG(exception)) { break; } convert_to_boolean(success); if (Z_BVAL_P(success) == 1) { zend_call_method(&stmt, NULL, NULL, ZEND_STRL("rowCount"), &row_count, 0, NULL, NULL TSRMLS_CC); } zval_dtor(success); FREE_ZVAL(success); } while (0); if (row_count) { convert_to_long(row_count); RETURN_ZVAL(row_count, 1, 1); } else { RETURN_FALSE; } }
/** {{{ public ZeRecorder::lastId() */ PHP_METHOD(ze_recorder, lastId) { zval * self = NULL; zval * conn = NULL; char * name = NULL; int name_len = 0; zval * name_z = NULL; zval * lastid = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s" , &name, &name_len ) == FAILURE) { WRONG_PARAM_COUNT; } /* call lastInsertId */ self = getThis(); conn = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_PDO_DBH), 0 TSRMLS_CC); do { if (!conn || Z_TYPE_P(conn) != IS_OBJECT) { ze_error(E_ERROR TSRMLS_CC, "recorder.lastId.conn.not_object"); break; } ALLOC_INIT_ZVAL(name_z); ZVAL_STRINGL(name_z, name, name_len, 1); zend_call_method(&conn, NULL, NULL, ZEND_STRL("lastInsertId"), &lastid, 1, name_z, NULL TSRMLS_CC); zval_dtor(name_z); FREE_ZVAL(name_z); } while (0); if (lastid) { convert_to_long(lastid); RETURN_ZVAL(lastid, 1, 1); } else { RETURN_FALSE; } }
/** {{{ public ZeRecorder::__construct() */ PHP_METHOD(ze_recorder, __construct) { zval * self = NULL; zval * pdo_dbh = NULL; zval * keys = NULL; zval * stmt = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O" , &pdo_dbh , php_pdo_get_dbh_ce() ) == FAILURE) { WRONG_PARAM_COUNT; } self = getThis(); if (pdo_dbh && Z_TYPE_P(pdo_dbh) == IS_OBJECT) { zend_update_property(ze_recorder_ce, self, ZEND_STRL(ZE_PDO_DBH) , pdo_dbh TSRMLS_CC); } else { /* PDO is not connected */ ze_error(E_ERROR TSRMLS_CC, "recorder.construct.pdo_dbh.not_object"); } }
/** {{{ public ZeActiveString::toString($separator) */ PHP_METHOD(ze_activestring, toString) { zval * self = NULL; zval * items = NULL; zval * actives = NULL; char * sep = NULL; int sep_len = 0; smart_str sstr = {NULL, 0, 0}; HashTable * items_table = NULL; HashPosition items_pointer = NULL; HashTable * actives_table = NULL; char * key = NULL; uint key_len = 0; ulong idx = 0; zval ** val = NULL; char * str = NULL; int str_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s" , &sep, &sep_len ) == FAILURE) { WRONG_PARAM_COUNT; } self = getThis(); items = zend_read_property(ze_activestring_ce, self, ZEND_STRL(ZE_ITEMS) , 0 TSRMLS_CC); actives = zend_read_property(ze_activestring_ce, self, ZEND_STRL(ZE_ACTIVES), 0 TSRMLS_CC); do { if (!items || Z_TYPE_P(items) != IS_ARRAY) { ZE_PTR_DTOR(items); ze_error(E_ERROR TSRMLS_CC, "recorder.toString.items.not_array"); break; } if (!actives || Z_TYPE_P(actives) != IS_ARRAY) { ZE_PTR_DTOR(actives); ze_error(E_ERROR TSRMLS_CC, "recorder.toString.actives.not_array"); break; } items_table = Z_ARRVAL_P(items); actives_table = Z_ARRVAL_P(actives); for (zend_hash_internal_pointer_reset_ex(items_table, &items_pointer); zend_hash_get_current_key_ex(items_table, &key, &key_len, &idx, 0, &items_pointer) == HASH_KEY_IS_STRING; zend_hash_move_forward_ex(items_table, &items_pointer)) { if (!key || !key_len) { continue; } if (zend_hash_exists(actives_table, key, key_len )) { if (zend_hash_get_current_data_ex(items_table , (void**) &val, &items_pointer) == SUCCESS) { smart_str_appendl(&sstr, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); if (sep_len) { smart_str_appendl(&sstr, sep, sep_len); } } } } smart_str_0(&sstr); } while(0); if (sstr.len) { str_len = sstr.len - sep_len; spprintf(&str, str_len, "%s", sstr.c); smart_str_free(&sstr); RETURN_STRING(str , 0); }else{ smart_str_free(&sstr); RETURN_EMPTY_STRING(); } }
/** {{{ public ZeRecorder::getInt() */ PHP_METHOD(ze_recorder, getInt) { zval * self = NULL; zval * stmt = NULL; zval * success = NULL; zval * row = NULL; long style = 0; zval * style_z = NULL; long count = 0; zval ** count_z = NULL; /* PDO::FETCH_NUM */ style = 3; /* call execute */ /* call fetch */ self = getThis(); stmt = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_STMT), 0 TSRMLS_CC); do { if (!stmt) { ze_error(E_ERROR TSRMLS_CC, "recorder.fetch.stmt.null"); break; } if (Z_TYPE_P(stmt) != IS_OBJECT) { ze_error(E_ERROR TSRMLS_CC, "recorder.fetch.stmt.not_object"); break; } zend_call_method(&stmt, Z_OBJCE_P(stmt), NULL, ZEND_STRL("execute"), &success, 0, NULL, NULL TSRMLS_CC); if (!success || EG(exception)) { break; } convert_to_boolean(success); if (Z_BVAL_P(success) == 1) { ALLOC_INIT_ZVAL(style_z); ZVAL_LONG(style_z, style); zend_call_method(&stmt, NULL, NULL, ZEND_STRL("fetch"), &row, 1, style_z, NULL TSRMLS_CC); zval_dtor(style_z); FREE_ZVAL(style_z); } zval_dtor(success); FREE_ZVAL(success); if (!row) { break; } if (Z_TYPE_P(row) == IS_ARRAY && zend_hash_has_more_elements(Z_ARRVAL_P(row)) == SUCCESS && zend_hash_get_current_data(Z_ARRVAL_P(row), (void **) &count_z) == SUCCESS) { convert_to_long_ex(count_z); count = Z_LVAL_PP(count_z); zval_dtor(*count_z); FREE_ZVAL(*count_z); } zval_dtor(row); FREE_ZVAL(row); } while (0); RETURN_LONG(count); }
//@todo PHP_METHOD(ze_recorder, bind) { zval * self = NULL; zval * stmt = NULL; zval * fields = NULL; zval * types = NULL; HashTable * keys_table = NULL; HashPosition keys_pointer = NULL; HashTable * fields_table = NULL; HashTable * types_table = NULL; zval ** val = NULL; zval * val_copy = NULL; zval ** type = NULL; zval * type_copy = NULL; zval * keys = NULL; zval ** key = NULL; int pos = 0; zval * pos_z = NULL; zval ** params[3]; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z" , &fields , &types ) == FAILURE) { WRONG_PARAM_COUNT; } if (Z_TYPE_P(fields) == IS_OBJECT && Z_OBJ_HT_P(fields)->get_properties != NULL) { fields_table = Z_OBJ_HT_P(fields)->get_properties(fields TSRMLS_CC); } else if (Z_TYPE_P(fields) == IS_ARRAY) { fields_table = Z_ARRVAL_P(fields); } if (!fields_table) { /* need array or object for '$fields' . */ ze_error(E_ERROR TSRMLS_CC, "recorder.bind.fields.type"); return; } if (types) { if (Z_TYPE_P(fields) == IS_OBJECT && Z_OBJ_HT_P(types)->get_properties != NULL) { types_table = Z_OBJ_HT_P(types)->get_properties(types TSRMLS_CC); } else if (Z_TYPE_P(fields) == IS_ARRAY) { types_table = Z_ARRVAL_P(types); } if (!types_table){ /* need array or object for '$types' . */ ze_error(E_ERROR TSRMLS_CC, "recorder.bind.types.type"); return; } } self = getThis(); stmt = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_STMT), 0 TSRMLS_CC); keys = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_KEYS), 0 TSRMLS_CC); do { if (!stmt || Z_TYPE_P(stmt) != IS_OBJECT) { ze_error(E_ERROR TSRMLS_CC, "recorder.bind.stmt.null"); break; } if (!keys || Z_TYPE_P(keys) != IS_ARRAY) { ze_error(E_ERROR TSRMLS_CC, "recorder.bind.keys.is_array"); break; } keys_table = Z_ARRVAL_P(keys); pos = 0; for (zend_hash_internal_pointer_reset_ex(keys_table, &keys_pointer); zend_hash_get_current_data_ex(keys_table,(void**) &key, &keys_pointer) == SUCCESS; zend_hash_move_forward_ex(keys_table, &keys_pointer)) { if (zend_hash_find(fields_table, Z_STRVAL_PP(key), Z_STRLEN_PP(key) + 1, (void **) &val) == SUCCESS) { pos++; ALLOC_INIT_ZVAL(pos_z); ZVAL_LONG(pos_z, pos); val_copy = *val; zval_copy_ctor(val_copy); if (types_table && zend_hash_find(types_table, Z_STRVAL_PP(key), Z_STRLEN_PP(key) + 1, (void **) &type) == SUCCESS) { type_copy = *type; zval_copy_ctor(type_copy); params[0] = &pos_z; params[1] = &val_copy; params[2] = &type_copy; ze_call_method(&stmt, NULL, NULL, ZEND_STRL("bindValue"), NULL, 3, params TSRMLS_CC); }else{ zend_call_method(&stmt, NULL, NULL, ZEND_STRL("bindValue"), NULL, 2, pos_z, val_copy TSRMLS_CC); } zval_dtor(pos_z); FREE_ZVAL(pos_z); } } } while (0); RETURN_ZVAL(self, 1, 0); }
/** {{{ public ZeRecorder::query($sql = null, $args = null, $_ = null) */ PHP_METHOD(ze_recorder, query) { zval * self = NULL; zval * conn = NULL; zval * stmt = NULL; char * query = NULL; int query_len = 0; zval * sql = NULL; zval * keys = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s" , &query , &query_len ) == FAILURE) { WRONG_PARAM_COUNT; } /* call prepare */ self = getThis(); conn = zend_read_property(ze_recorder_ce, self, ZEND_STRL(ZE_PDO_DBH), 0 TSRMLS_CC); do { if (!conn || Z_TYPE_P(conn) != IS_OBJECT) { ze_error(E_ERROR TSRMLS_CC, "recorder.query.conn.not_object"); break; } MAKE_STD_ZVAL(keys); array_init(keys); sql = to_indexed(keys, query, query_len); if (!sql) { ze_error(E_ERROR TSRMLS_CC, "recorder.query.sql.is_empty"); break; } zend_call_method(&conn, Z_OBJCE_P(conn), NULL, ZEND_STRL("prepare"), &stmt, 1, sql, NULL TSRMLS_CC); if (!stmt || EG(exception)) { zval_dtor(keys); FREE_ZVAL(keys); ze_error(E_ERROR TSRMLS_CC, "recorder.query.stmt.not_object"); break; } zend_update_property(ze_recorder_ce, self, ZEND_STRL(ZE_STMT), stmt TSRMLS_CC); zend_update_property(ze_recorder_ce, self, ZEND_STRL(ZE_KEYS), keys TSRMLS_CC); if (stmt != NULL) { zval_ptr_dtor(&stmt); } if (keys != NULL) { zval_ptr_dtor(&keys); } } while (0); if (sql != NULL) { zval_dtor(sql); FREE_ZVAL(sql); } RETURN_ZVAL(self, 1, 0); }