cvs: pecl /dbdo dbdo.c php_dbdo.h
| From: | Alan Knowles | Date: | Sat, 02 Apr 2005 05:32:05 +0000 |
| Subject: | cvs: pecl /dbdo dbdo.c php_dbdo.h | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-2918@lists.php.net to get a copy of this message | ||
alan_k Sat Apr 2 00:32:05 2005 EDT
Modified files:
/pecl/dbdo dbdo.c php_dbdo.h
Log:
replace klunky multiple view hashes with simple concept of object vars store active row, (no more
hidden fetching) - and we only compare to fetched data on update.
http://cvs.php.net/diff.php/pecl/dbdo/dbdo.c?r1=1.78&r2=1.79&ty=u Index: pecl/dbdo/dbdo.c diff -u pecl/dbdo/dbdo.c:1.78 pecl/dbdo/dbdo.c:1.79 --- pecl/dbdo/dbdo.c:1.78 Wed Mar 30 13:33:37 2005 +++ pecl/dbdo/dbdo.c Sat Apr 2 00:32:04 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: dbdo.c,v 1.78 2005/03/30 18:33:37 alan_k Exp $ */ +/* $Id: dbdo.c,v 1.79 2005/04/02 05:32:04 alan_k Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -67,23 +67,7 @@ zend_hash_destroy(intern->zo.properties); FREE_HASHTABLE(intern->zo.properties); - if (intern->row_cache) { - zend_hash_destroy(intern->row_cache); - FREE_HASHTABLE(intern->row_cache); - intern->row_cache = NULL; - } - if (intern->visable_hash) { - zend_hash_destroy(intern->visable_hash); - FREE_HASHTABLE(intern->visable_hash); - intern->visable_hash = NULL; - } - intern->row_cached = 0; - - if (intern->assigned_hash) { - zend_hash_destroy(intern->assigned_hash); - FREE_HASHTABLE(intern->assigned_hash); - intern->assigned_hash = NULL; - } + if (G_IS_OBJECT(intern->result)) { g_object_unref (intern->result); @@ -134,13 +118,6 @@ intern->zo.in_get = 0; intern->zo.in_set = 0; intern->zo.properties = NULL; - intern->row_cache = NULL; - intern->visable_hash = NULL; - intern->assigned_hash = NULL; - ALLOC_HASHTABLE(intern->assigned_hash); - zend_hash_init(intern->assigned_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - - intern->row_cached = 0; intern->con = NULL; intern->result = NULL; intern->row_id = -1; @@ -369,13 +346,7 @@ memcpy(&dbdo_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - dbdo_object_handlers.write_property = dbdo_property_write; - dbdo_object_handlers.read_property = dbdo_property_read; - dbdo_object_handlers.has_property = dbdo_property_has; - dbdo_object_handlers.get_properties = dbdo_properties_get; dbdo_object_handlers.clone_obj = dbdo_clone; - - INIT_CLASS_ENTRY(ce, "DBDO", dbdo_class_functions); ce.create_object = dbdo_objects_new; @@ -494,13 +465,13 @@ int i; /* free client */ - //if (DBDO_G(client)) { + /*if (DBDO_G(client)) { // gda_client_close_all_connections ( DBDO_G(client)); // g_object_unref(G_OBJECT( DBDO_G(client))); DBDO_G(client) = NULL; //} - + */ /* free config */ /* if (DBDO_G(config_len)) { @@ -601,7 +572,7 @@ GdaProviderInfo *info; php_info_print_table_start(); php_info_print_table_header(2, "DBDO support", "enabled"); - php_info_print_table_row(2, "CVS id", "$Id: dbdo.c,v 1.78 2005/03/30 18:33:37 alan_k Exp $"); + php_info_print_table_row(2, "CVS id", "$Id: dbdo.c,v 1.79 2005/04/02 05:32:04 alan_k Exp $"); prov_list = gda_config_get_provider_list (); @@ -633,20 +604,19 @@ /* 6 */ "password", "row", /* 8 */ "rows", - "assigned", - /* 10*/ "fetched", - "config", - /* 12*/ "sequence_column", - "sequence_is_native", - /* 14*/ "sequence_assigned", + "fetched", + /* 10*/ "config", + "sequence_column", + /* 12*/ "sequence_is_native", + "sequence_assigned", "sequence_name", - /* 16*/ "keys", - "where", - /* 18*/ "group_by", + "keys", + /* 16*/ "where", + "group_by", "order_by", - /* 20*/ "having", - "limit_from", - /* 22*/ "limit_qty", + "having", + /* 20*/ "limit_from", + "limit_qty", "select" }; @@ -716,69 +686,7 @@ case 8: /* rows in result. */ ZVAL_LONG(ret, obj->result ? gda_data_model_get_n_rows (obj->result) : 0 ); return ret; - case 9: /* 9 = assigned */ - array_init(ret); - if (obj->result) { - /* TODO: check if we have results.. */ - - int column_id; - GdaRow *row; - -/* TODO!!! - use schema here - if we are assigning on a new object! !!!! */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - - zval *value, **element; - - char *title; - ulong h; - - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - - if (zend_hash_find(obj->assigned_hash, title, strlen(title)+1, (void **) &element) != SUCCESS) { - continue; - } - value = *element; - /* g_free(title);is this needed?? */ - - - h = zend_hash_func(title, strlen(title)+1); - zend_hash_quick_update((HashTable *) Z_ARRVAL_P(ret), title, strlen(title)+1, h, &value, sizeof(zval *), NULL); - - } - return ret; - - } - /* else - no results = use schema */ - { - int row_id; - long h; - dbdo_schema_load(obj TSRMLS_CC); - - - - for (row_id = 0; row_id < (int) gda_data_model_get_n_rows (obj->table->schema); row_id++) { - char *title; - zval *value, **element; - title = (char *) gda_value_get_string ( - (GdaValue *) gda_data_model_get_value_at (obj->table->schema, 0, row_id)); - - - if (zend_hash_find(obj->assigned_hash, title, strlen(title)+1, (void **) &element) != SUCCESS) { - continue; - } - - value = *element; - /* g_free(title);is this needed?? */ - - h = zend_hash_func(title, strlen(title)+1); - zend_hash_quick_update((HashTable *) Z_ARRVAL_P(ret), title, strlen(title)+1, h, &value, sizeof(zval *), NULL); - - } - return ret; - } - - case 10: /* 10 = fetched */ + case 9: /* 9 = fetched */ { int column_id; @@ -808,7 +716,7 @@ } return ret; } - case 11: + case 10: /* config */ { dbdo_config *config; @@ -840,7 +748,7 @@ return ret; } - case 12: /* 12 = sequence Column */ + case 11: /* 11 = sequence Column */ if (!obj->table->sequenceColumn) { @@ -849,20 +757,20 @@ ZVAL_STRING(ret, obj->table->sequenceColumn, 1); } return ret; - case 13: /* 13 = sequence is native? */ + case 12: /* 12 = sequence is native? */ ZVAL_BOOL(ret, obj->table->sequenceIsNative); return ret; - case 14: /* 14 = assigned or guessed? */ + case 13: /* 13 = assigned or guessed? */ ZVAL_BOOL(ret, obj->table->sequenceAssigned); return ret; - case 15: /* 15 = sequenceName */ + case 14: /* 14 = sequenceName */ if (!obj->table->sequenceName) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->table->sequenceName, 1); } return ret; - case 16: /* keys */ + case 15: /* keys */ { zval **zconfig; int i; @@ -884,49 +792,49 @@ } - case 17: /*"where", */ + case 16: /*"where", */ if (!obj->condition->where) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->where, 1); } return ret; - case 18: /* "group_by", */ + case 17: /* "group_by", */ if (!obj->condition->groupBy) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->groupBy, 1); } return ret; - case 19: /* "order_by",*/ + case 18: /* "order_by",*/ if (!obj->condition->orderBy) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->orderBy, 1); } return ret; - case 20: /* "having",*/ + case 19: /* "having",*/ if (!obj->condition->having) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->having, 1); } return ret; - case 21: /* "limit_from",*/ + case 20: /* "limit_from",*/ if (!obj->condition->limit_use) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_LONG(ret, obj->condition->limit_from); } return ret; - case 22: /* "limit_qty",*/ + case 21: /* "limit_qty",*/ if (!obj->condition->limit_use) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_LONG(ret, obj->condition->limit_qty); } return ret; - case 23: /* "select" */ + case 22: /* "select" */ if (!obj->condition->data_select) { ZVAL_BOOL(ret, FALSE); } else { @@ -1351,6 +1259,9 @@ dbdo_class_entry_exception_ce , 0 TSRMLS_CC, "DBDO::%s returned %d rows, only one is expected", get_active_function_name(TSRMLS_C), rows); } + + dbdo_fetch_into_properties(object TSRMLS_CC); + /* g_print("returning %d",rows); */ RETURN_LONG(rows); @@ -1555,6 +1466,7 @@ zclone->value.obj = dbdo_clone(object TSRMLS_CC); dclone = zend_object_store_get_object(zclone TSRMLS_CC); dclone->row_id = r; + dbdo_fetch_into_properties(zclone TSRMLS_CC); zend_hash_next_index_insert(HASH_OF(return_value), &zclone, sizeof(zval *), NULL); } g_object_unref(obj->result); @@ -1690,6 +1602,41 @@ } } +void * +dbdo_fetch_into_properties(zval *object TSRMLS_DC) +{ + + GdaRow *row; + GdaValue *value; + + gchar *string, + *title; + gint column_id; + zval *znew_value, + *ztitle; + + dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); + /* TODO assert stuff */ + + row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); + for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { + + title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); + value = (GdaValue *) gda_row_get_value (row, column_id); + znew_value = dbdo_value_to_zval(value); + + zval_add_ref(&znew_value); + MAKE_STD_ZVAL(ztitle); + ZVAL_STRING(ztitle, title, 1); + std_object_handlers.write_property(object, ztitle, znew_value TSRMLS_CC); + + /*zval_dtor(ztitle); + FREE_ZVAL(ztitle); + FREE_ZVAL(znew_value); + */ + } + +} /* {{{ proto mixed $dbdo->fetch ( ) fetch the results into the object's properties @@ -1711,19 +1658,8 @@ RETURN_FALSE; } /* destroy the row data hash */ - if (obj->row_cache) { - zend_hash_destroy(obj->row_cache); - FREE_HASHTABLE(obj->row_cache); - obj->row_cache = NULL; - } - if (obj->visable_hash) { - zend_hash_destroy(obj->visable_hash); - FREE_HASHTABLE(obj->visable_hash); - obj->visable_hash = NULL; - } - obj->row_cached = 0; obj->row_id++; if (obj->row_id >= gda_data_model_get_n_rows (obj->result)) { /*g_print("fetched all? %d >= %d unreffing dm?\n",obj->row_id , gda_data_model_get_n_rows (obj->dm)); */ @@ -1737,31 +1673,8 @@ RETURN_FALSE; } - - - /* char *ntitle = NULL; */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - value = (GdaValue *) gda_row_get_value (row, column_id); - string = gda_value_stringify (value); - MAKE_STD_ZVAL(ztitle); - ZVAL_STRING(ztitle, title, 1); - if (obj->assigned_hash) { - DBDO_DEBUG(16,"Deleteing Hash Item: %s", title); - zend_hash_del(obj->assigned_hash, title, strlen(title)+1); - } - - /*if (std_object_handlers.has_property(object, ztitle, 0 TSRMLS_CC)) { - std_object_handlers.unset_property(object, ztitle TSRMLS_CC); - } - */ - zval_dtor(ztitle); - FREE_ZVAL(ztitle); - - g_free(string); - } + dbdo_fetch_into_properties(object TSRMLS_CC); + @@ -1879,230 +1792,7 @@ zval_dtor(ret); FREE_ZVAL(ret); } - -static -int -dbdo_property_has(zval *object, zval *member, int check_empty TSRMLS_DC) -{ - zval *return_value; - GdaRow *row; - gint column_id; - GdaValue *value; - char *string; - char *title; - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - - /* TODO: honour check_empty */ - /* if we have it stored return that! */ - if (std_object_handlers.has_property(object, member, 0 TSRMLS_CC)) { - return 1; - } - if ((obj->result == NULL) || !G_IS_OBJECT(obj->result)) { - return 0; - } - /* fetch past end ! - do we really want to do this!!! ??? */ - if (obj->row_id >= gda_data_model_get_n_rows (obj->result)) { - return 0; - } - /* - ?? should we be using the schema to determine this? = probably not! - */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - if (strcmp(Z_STRVAL_P(member), title) != 0) { - continue; - } - return 1; - } - return 0; -} - -/* write property - store twice - in properties and object. */ -static -void -dbdo_property_write(zval *object, zval *member, zval *value TSRMLS_DC) -{ - long h; - zval *test; - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - - h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member) +1); - /* do we need to copy or add ref stuff */ - - value->refcount++; - if (PZVAL_IS_REF(value)) { - SEPARATE_ZVAL(&value); - } - - - zend_hash_quick_update(obj->assigned_hash, Z_STRVAL_P(member), Z_STRLEN_P(member) +1, h, - &value, sizeof(zval *), NULL); - /* not sure if this is needed... */ - std_object_handlers.write_property(object, member, value TSRMLS_CC); - - -} -static -zval * -dbdo_property_read(zval *object, zval *member, int type TSRMLS_DC) -{ - zval *return_value; - char *name; - GdaRow *row; - gint column_id; - GdaValue *value; - char *string; - char *title; - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - zval **element, *use; - /* serialize values - always start with "." */ - DBDO_DEBUG2(16, "dbdo_property_read '%s' FROM %x",Z_STRVAL_P(member),obj); - - /* has it been assigned - return that.. */ - if (zend_hash_find(obj->assigned_hash, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &element) == SUCCESS) { - DBDO_DEBUG(16, "property_read(%s) from assigned data..",Z_STRVAL_P(member)); - use = *element; - return use; - } - - /* if we are asking for something not from the resultset. */ - if ((obj->result == NULL) || !G_IS_OBJECT(obj->result) || (gda_data_model_get_n_rows (obj->result) < 1)) { - DBDO_DEBUG(16,"property_read(%s) from properties.",Z_STRVAL_P(member)); - /* this may be a bit flaky - fetched past end of object.. any old crap left in - properties.. */ - return std_object_handlers.read_property(object, member, type TSRMLS_CC); - } - - - - /* find the matching column */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - zval *ret; - ulong h; - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - if (strcmp(Z_STRVAL_P(member), title) != 0) { - continue; - } - value = (GdaValue *) gda_row_get_value (row, column_id); - ret = dbdo_value_to_zval(value); - - - DBDO_DEBUG(16,"property_read(%s) Directly from result set.",Z_STRVAL_P(member)); - return ret; - } - DBDO_DEBUG(16 ,"property_read(%s) fell back to properties.",Z_STRVAL_P(member)); - return std_object_handlers.read_property(object, member, type TSRMLS_CC); - - -} - - -/* -get all the properties.. (eg. used by print_r etc..) -Note this is a little complex as we have a number of considerations -a) data fetched -b) data assigned (thats replaces stuff) -c) data assigned (thats extra / nothing to do with it.. ) -e) .info data used when __sleep etc. is used. -f) a visable view of this data (eg. intern->zo.properties) -g) the fact we have to free it all! - -The strategy we have is: -* maintain an assigned data array : - free/unref on clone / destruct / fetch. --- changed when we set properties -* maintain a fetched data array : - free/unref on clone / destruct / fetch. --- only built when we do a properties_get -* maintain a .info data array : - free/unref on clone / destruct / fetch. --- only added to by __sleep - - - -methods used: -dbdo_object_free_stores(zval *obj TSRMLS_DC) -* destroys all our hash tables -* loops through each of them, and removes any reference in the zo.properties.. - - -This does ask the question - how do/should we deal with arguments that are not related to -the table... - - - - - -*/ -static HashTable * -dbdo_properties_get(zval *object TSRMLS_DC) -{ - zval *tmp; - zval **data_ptr; - zval *value,*ztitle; - zval *newptr; - zval *rvalue; - GdaValue *gdavalue; - gint column_id; - char *title; - char *string; - GdaRow *row; - ulong h; - HashTable *row_hash; - - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - char *name; - /* BOTH THESE APPEAR TO LEAK */ - - if (obj->result == NULL) { - /* throw errror!! - what about user set values...???? */ - /* empty! */ - /* zend_hash_init(rv, 0, NULL, ZVAL_PTR_DTOR, 0); */ - return std_object_handlers.get_properties(object TSRMLS_CC); - } - - - if (obj->visable_hash) { - /* if we only have a partial view (eg. from a property_read */ - zend_hash_destroy(obj->visable_hash); - FREE_HASHTABLE(obj->visable_hash); - obj->visable_hash = NULL; - } - - ALLOC_HASHTABLE(row_hash); - zend_hash_init(row_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - - /* g_print("copying existing hash?\n"); */ - /* FIXME : visable hash should only be a represenation of the object now */ - - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - - if (!row) { - /* assigned data or empty array. */ - return obj->assigned_hash ? obj->assigned_hash : row_hash; - } - - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - gdavalue = (GdaValue *) gda_row_get_value (row, column_id); - rvalue = dbdo_value_to_zval(gdavalue); - DBDO_DEBUG2(16,"fetch row: %s = '%s'",title, gda_value_stringify(gdavalue)); - h = zend_hash_func(title, strlen(title)+1); - zend_hash_quick_update(row_hash, title, strlen(title)+1, h, &rvalue, sizeof(zval *), NULL); - } - ALLOC_HASHTABLE(obj->visable_hash); - zend_hash_init(obj->visable_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_copy(obj->visable_hash, row_hash, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); - zend_hash_destroy(row_hash); - FREE_HASHTABLE(row_hash); - /* overwrite with assigned values. */ - zend_hash_merge(obj->visable_hash, obj->assigned_hash, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *), 1); - return obj->visable_hash; - -} - - - + /* {{{ proto array $do->toArray ( [ string $format ] ) get the result as an array from the object, and optionally format it. @@ -2132,7 +1822,7 @@ MAKE_STD_ZVAL(ztitle); ZVAL_STRING(ztitle, title, 1); - value = dbdo_property_read(object, ztitle, 0 TSRMLS_CC); + value = std_object_handlers.read_property(object, ztitle, 0 TSRMLS_CC); zval_dtor(ztitle); FREE_ZVAL(ztitle); @@ -2338,10 +2028,6 @@ intern->zo.in_get = 0; intern->zo.in_set = 0; intern->zo.properties = NULL; - intern->row_cache = NULL; - intern->visable_hash = NULL; - intern->assigned_hash = NULL; - intern->row_cached = 0; intern->con = old_obj->con; @@ -2364,13 +2050,6 @@ zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); - - ALLOC_HASHTABLE(intern->assigned_hash); - zend_hash_init(intern->assigned_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_copy(intern->assigned_hash, old_obj->assigned_hash, (copy_ctor_func_t) zval_add_ref, - (void *) &tmp, sizeof(zval *)); - - new_obj_val.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) dbdo_object_free_storage, @@ -2525,8 +2204,7 @@ /* TODO: sequenceName usage */ if (obj->table->sequenceName) { - zend_throw_exception(dbdo_class_entry_exception_ce , - "DBDO::insert sequenceName not implemented yet.", 0 TSRMLS_CC); + php_error(E_ERROR, "DBDO::insert with sequenceName not implemented yet."); return; } @@ -2559,20 +2237,15 @@ /* g_print("get from schema row:%d, col:%d\n",row_id,0); */ colname = (gchar *) gda_value_get_string ( (GdaValue *) gda_data_model_get_value_at (obj->table->schema, 0, row_id)); - - /* dont assign the sequence column */ - if (!skippedseq && !strcmp(colname,obj->table->sequenceColumn)) { skippedseq = 1; /* g_print("Skipping matches column: %s\n",colname); */ - continue; } - - + MAKE_STD_ZVAL(ztitle); ZVAL_STRING(ztitle, colname, 1); @@ -2595,8 +2268,6 @@ } - - if (!started) { zend_throw_exception(dbdo_class_entry_exception_ce , "DBDO::insert No data found to assign to columns", 0 TSRMLS_CC); @@ -2606,8 +2277,6 @@ } - - DBDO_DEBUG0(16,"DONE LEFT:"); smart_str_appends(&cmd, " ) VALUES ( "); started = 0; @@ -2655,16 +2324,9 @@ } /* perhaps we should copy here.. */ - std_object_handlers.unset_property(object, ztitle TSRMLS_CC); - if (obj->assigned_hash) { - DBDO_DEBUG(16,"Deleteing Hash Item: %s", colname); - zend_hash_del(obj->assigned_hash, colname, strlen(colname)+1); - } zval_dtor(ztitle); FREE_ZVAL(ztitle); /* zval_dtor(zvalue); */ - - DBDO_DEBUG0(16,"Succeeded!"); if (started) { @@ -2689,7 +2351,6 @@ gda_cmd = gda_command_new ((gchar*)cmd.c, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); smart_str_free(&cmd); dm = gda_connection_execute_single_command (obj->con, gda_cmd, NULL); - gda_command_free (gda_cmd); @@ -2704,10 +2365,11 @@ ret_int = atoi(ret_str); /* g_free(ret_str); */ - - /* TODO:: set the value column to match.... */ + /* TODO: write_property the value to the object */ + + /* make the dataset for the object from the args.. */ - /* TODO: add the autoinc col. */ + { GdaDataModel *model; @@ -2786,8 +2448,10 @@ */ PHP_METHOD(dbdo,update) { - zval *object = getThis(); - zval *zvalue,*ztitle; + zval *object = getThis(), + *zvalue, + *ztitle, + compare_result; GdaCommand *gdacmd; GList *node; gboolean errors = FALSE; @@ -2811,10 +2475,12 @@ smart_str_appends(&cmd, "UPDATE "); smart_str_appends(&cmd, obj->table->name); smart_str_appends(&cmd, " SET "); + + if (!build) { /* - not built = + not build = use Sequence Key to update.. set : changes in everything except for sequenceKey (or keys if set) where = sequenceKeys or keys() */ @@ -2833,6 +2499,7 @@ started = 0; for (row_id = 0; row_id < rows;row_id++) { /* g_print ("rows: %d\n", title,string); */ + int skip = 0; row = (GdaRow *) gda_data_model_get_row (obj->table->schema, row_id); value = (GdaValue *) gda_row_get_value (row, 0); @@ -2843,12 +2510,18 @@ ZVAL_STRING(ztitle, colname, 1); /* skip empty values, sequence columns or keys.. */ - if ( - (obj->table->sequenceColumn && !strcmp(colname,obj->table->sequenceColumn)) || - dbdo_str_in_array(colname, obj->table->keys, obj->table->keys_len) || - !std_object_handlers.has_property(object, ztitle, 0 TSRMLS_CC) - - ) { + if (obj->table->sequenceColumn && !strcmp(colname,obj->table->sequenceColumn)) { + skip++; + } + if (dbdo_str_in_array(colname, obj->table->keys, obj->table->keys_len)) { + skip++; + } + if (!std_object_handlers.has_property(object, ztitle, 0 TSRMLS_CC)) { + skip++; + } + /* TODO: has the value changed?? */ + + if (skip > 0) { DBDO_DEBUG0(16,"Skipping is sequence, is key or has no property"); g_free(colname); zval_dtor(ztitle); @@ -2856,19 +2529,45 @@ continue; } + + zvalue = std_object_handlers.read_property(object, ztitle, 0 TSRMLS_CC); + + /* + see if the value is different to the one in our result set.. + + */ + if (obj->result && G_IS_OBJECT(obj->result) && + (gda_data_model_get_column_position(obj->result, colname) > -1) + ) { + GdaValue *r_gvalue; + zval *znew_value; + r_gvalue = (GdaValue *) gda_data_model_get_value_at ( + obj->result, + gda_data_model_get_column_position(obj->result, colname), + obj->row_id); + + znew_value = dbdo_value_to_zval(r_gvalue); + is_equal_function(&compare_result, zvalue, znew_value TSRMLS_CC); + zval_dtor(znew_value); + FREE_ZVAL(znew_value); + if (Z_LVAL(compare_result)) { + /* they are the same */ + DBDO_DEBUG0(16,"Skipping col, looks like it matches the row.*/"); + g_free(colname); + zval_dtor(ztitle); + FREE_ZVAL(ztitle); + continue; + } + + } if (started) { smart_str_appends(&cmd, " , "); } smart_str_appends(&cmd, colname); smart_str_appends(&cmd, "="); - zvalue = std_object_handlers.read_property(object, ztitle, 0 TSRMLS_CC); - /* - create a GdaValue with type matching the column - do a dbdo_gda_to_sql on it. - */ if (zvalue->type != IS_STRING) { convert_to_string(zvalue); } @@ -2972,7 +2671,7 @@ return; } - smart_str_appends(&cmd, ")"); + smart_str_appends(&cmd, ")"); } else { /* use all the object values to update set. */ @@ -3351,7 +3050,6 @@ - ???password??? - current row - numrows - - assigned data - retrieved data - config ? - sequenceColumn @@ -3696,7 +3394,7 @@ RETURN_FALSE; } /* we have no idea what type (3rd arg means but we are sticking 1 in there */ - ret = dbdo_property_read(object, member, 1 TSRMLS_CC); + ret = std_object_handlers.read_property(object, member, 1 TSRMLS_CC); RETURN_ZVAL(ret, 1, 0); } @@ -3777,7 +3475,7 @@ ZVAL_STRING(member, function+3, 0); /* we have no idea what type (3rd arg means but we are sticking 1 in there */ - ret = dbdo_property_read(object, member, 1 TSRMLS_CC); + ret = std_object_handlers.read_property(object, member, 1 TSRMLS_CC); FREE_ZVAL(member); RETURN_ZVAL(ret, 1, 0); return; http://cvs.php.net/diff.php/pecl/dbdo/php_dbdo.h?r1=1.32&r2=1.33&ty=u Index: pecl/dbdo/php_dbdo.h diff -u pecl/dbdo/php_dbdo.h:1.32 pecl/dbdo/php_dbdo.h:1.33 --- pecl/dbdo/php_dbdo.h:1.32 Thu Mar 17 03:12:46 2005 +++ pecl/dbdo/php_dbdo.h Sat Apr 2 00:32:04 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_dbdo.h,v 1.32 2005/03/17 08:12:46 alan_k Exp $ */ +/* $Id: php_dbdo.h,v 1.33 2005/04/02 05:32:04 alan_k Exp $ */ #ifndef PHP_DBDO_H #define PHP_DBDO_H @@ -42,10 +42,10 @@ -#define DBDO_DEBUG0(lvl,string) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string); } -#define DBDO_DEBUG(lvl,string, arg) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string, arg); } -#define DBDO_DEBUG2(lvl,string, arg1,arg2) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string, arg1,arg2); } -#define DBDO_DEBUG3(lvl,string, arg1,arg2,arg3) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string, arg1,arg2,arg3); } +#define DBDO_DEBUG0(lvl,string) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__); } +#define DBDO_DEBUG(lvl,string, arg) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__, arg); } +#define DBDO_DEBUG2(lvl,string, arg1,arg2) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__,arg1,arg2); } +#define DBDO_DEBUG3(lvl,string, arg1,arg2,arg3) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__,arg1,arg2,arg3); } #define DBDO_QUERY_BUILD_DELETE 0 #define DBDO_QUERY_BUILD_SELECT 1 #define DBDO_QUERY_BUILD_UPDATE 2 @@ -53,16 +53,12 @@ extern ZEND_RSRC_DTOR_FUNC(php_dbdo_client_dtor); static void dbdo_objects_dtor(void *object, zend_object_handle handle TSRMLS_DC); -static int dbdo_property_has(zval *object, zval *member, int check_empty TSRMLS_DC); -static zval * dbdo_property_read(zval *object, zval *member, int type TSRMLS_DC); -static HashTable * dbdo_properties_get(zval *object TSRMLS_DC); -static void dbdo_property_write(zval *object, zval *member, zval *value TSRMLS_DC); static zend_object_value dbdo_clone(zval *zobject TSRMLS_DC); PHP_DBDO_API zend_object_value dbdo_objects_exception_new(zend_class_entry *class_type TSRMLS_DC); PHP_DBDO_API zend_object_value dbdo_objects_new(zend_class_entry *class_type TSRMLS_DC); - +void * dbdo_fetch_into_properties(zval *object TSRMLS_DC); zend_class_entry *dbdo_class_entry_ce, *dbdo_class_entry_exception_ce; @@ -110,10 +106,6 @@ GdaDataModel *result; dbdo_table *table; dbdo_condition *condition; /* conditions builder-- still need free policy */ - HashTable *row_cache; - HashTable *visable_hash; - HashTable *assigned_hash; - int row_cached; } dbdo_object; /* extends zend_object */ /* in dbdo_object.c */
http://cvs.php.net/diff.php/pecl/dbdo/dbdo.c?r1=1.78&r2=1.79&ty=u Index: pecl/dbdo/dbdo.c diff -u pecl/dbdo/dbdo.c:1.78 pecl/dbdo/dbdo.c:1.79 --- pecl/dbdo/dbdo.c:1.78 Wed Mar 30 13:33:37 2005 +++ pecl/dbdo/dbdo.c Sat Apr 2 00:32:04 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: dbdo.c,v 1.78 2005/03/30 18:33:37 alan_k Exp $ */ +/* $Id: dbdo.c,v 1.79 2005/04/02 05:32:04 alan_k Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -67,23 +67,7 @@ zend_hash_destroy(intern->zo.properties); FREE_HASHTABLE(intern->zo.properties); - if (intern->row_cache) { - zend_hash_destroy(intern->row_cache); - FREE_HASHTABLE(intern->row_cache); - intern->row_cache = NULL; - } - if (intern->visable_hash) { - zend_hash_destroy(intern->visable_hash); - FREE_HASHTABLE(intern->visable_hash); - intern->visable_hash = NULL; - } - intern->row_cached = 0; - - if (intern->assigned_hash) { - zend_hash_destroy(intern->assigned_hash); - FREE_HASHTABLE(intern->assigned_hash); - intern->assigned_hash = NULL; - } + if (G_IS_OBJECT(intern->result)) { g_object_unref (intern->result); @@ -134,13 +118,6 @@ intern->zo.in_get = 0; intern->zo.in_set = 0; intern->zo.properties = NULL; - intern->row_cache = NULL; - intern->visable_hash = NULL; - intern->assigned_hash = NULL; - ALLOC_HASHTABLE(intern->assigned_hash); - zend_hash_init(intern->assigned_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - - intern->row_cached = 0; intern->con = NULL; intern->result = NULL; intern->row_id = -1; @@ -369,13 +346,7 @@ memcpy(&dbdo_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - dbdo_object_handlers.write_property = dbdo_property_write; - dbdo_object_handlers.read_property = dbdo_property_read; - dbdo_object_handlers.has_property = dbdo_property_has; - dbdo_object_handlers.get_properties = dbdo_properties_get; dbdo_object_handlers.clone_obj = dbdo_clone; - - INIT_CLASS_ENTRY(ce, "DBDO", dbdo_class_functions); ce.create_object = dbdo_objects_new; @@ -494,13 +465,13 @@ int i; /* free client */ - //if (DBDO_G(client)) { + /*if (DBDO_G(client)) { // gda_client_close_all_connections ( DBDO_G(client)); // g_object_unref(G_OBJECT( DBDO_G(client))); DBDO_G(client) = NULL; //} - + */ /* free config */ /* if (DBDO_G(config_len)) { @@ -601,7 +572,7 @@ GdaProviderInfo *info; php_info_print_table_start(); php_info_print_table_header(2, "DBDO support", "enabled"); - php_info_print_table_row(2, "CVS id", "$Id: dbdo.c,v 1.78 2005/03/30 18:33:37 alan_k Exp $"); + php_info_print_table_row(2, "CVS id", "$Id: dbdo.c,v 1.79 2005/04/02 05:32:04 alan_k Exp $"); prov_list = gda_config_get_provider_list (); @@ -633,20 +604,19 @@ /* 6 */ "password", "row", /* 8 */ "rows", - "assigned", - /* 10*/ "fetched", - "config", - /* 12*/ "sequence_column", - "sequence_is_native", - /* 14*/ "sequence_assigned", + "fetched", + /* 10*/ "config", + "sequence_column", + /* 12*/ "sequence_is_native", + "sequence_assigned", "sequence_name", - /* 16*/ "keys", - "where", - /* 18*/ "group_by", + "keys", + /* 16*/ "where", + "group_by", "order_by", - /* 20*/ "having", - "limit_from", - /* 22*/ "limit_qty", + "having", + /* 20*/ "limit_from", + "limit_qty", "select" }; @@ -716,69 +686,7 @@ case 8: /* rows in result. */ ZVAL_LONG(ret, obj->result ? gda_data_model_get_n_rows (obj->result) : 0 ); return ret; - case 9: /* 9 = assigned */ - array_init(ret); - if (obj->result) { - /* TODO: check if we have results.. */ - - int column_id; - GdaRow *row; - -/* TODO!!! - use schema here - if we are assigning on a new object! !!!! */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - - zval *value, **element; - - char *title; - ulong h; - - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - - if (zend_hash_find(obj->assigned_hash, title, strlen(title)+1, (void **) &element) != SUCCESS) { - continue; - } - value = *element; - /* g_free(title);is this needed?? */ - - - h = zend_hash_func(title, strlen(title)+1); - zend_hash_quick_update((HashTable *) Z_ARRVAL_P(ret), title, strlen(title)+1, h, &value, sizeof(zval *), NULL); - - } - return ret; - - } - /* else - no results = use schema */ - { - int row_id; - long h; - dbdo_schema_load(obj TSRMLS_CC); - - - - for (row_id = 0; row_id < (int) gda_data_model_get_n_rows (obj->table->schema); row_id++) { - char *title; - zval *value, **element; - title = (char *) gda_value_get_string ( - (GdaValue *) gda_data_model_get_value_at (obj->table->schema, 0, row_id)); - - - if (zend_hash_find(obj->assigned_hash, title, strlen(title)+1, (void **) &element) != SUCCESS) { - continue; - } - - value = *element; - /* g_free(title);is this needed?? */ - - h = zend_hash_func(title, strlen(title)+1); - zend_hash_quick_update((HashTable *) Z_ARRVAL_P(ret), title, strlen(title)+1, h, &value, sizeof(zval *), NULL); - - } - return ret; - } - - case 10: /* 10 = fetched */ + case 9: /* 9 = fetched */ { int column_id; @@ -808,7 +716,7 @@ } return ret; } - case 11: + case 10: /* config */ { dbdo_config *config; @@ -840,7 +748,7 @@ return ret; } - case 12: /* 12 = sequence Column */ + case 11: /* 11 = sequence Column */ if (!obj->table->sequenceColumn) { @@ -849,20 +757,20 @@ ZVAL_STRING(ret, obj->table->sequenceColumn, 1); } return ret; - case 13: /* 13 = sequence is native? */ + case 12: /* 12 = sequence is native? */ ZVAL_BOOL(ret, obj->table->sequenceIsNative); return ret; - case 14: /* 14 = assigned or guessed? */ + case 13: /* 13 = assigned or guessed? */ ZVAL_BOOL(ret, obj->table->sequenceAssigned); return ret; - case 15: /* 15 = sequenceName */ + case 14: /* 14 = sequenceName */ if (!obj->table->sequenceName) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->table->sequenceName, 1); } return ret; - case 16: /* keys */ + case 15: /* keys */ { zval **zconfig; int i; @@ -884,49 +792,49 @@ } - case 17: /*"where", */ + case 16: /*"where", */ if (!obj->condition->where) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->where, 1); } return ret; - case 18: /* "group_by", */ + case 17: /* "group_by", */ if (!obj->condition->groupBy) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->groupBy, 1); } return ret; - case 19: /* "order_by",*/ + case 18: /* "order_by",*/ if (!obj->condition->orderBy) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->orderBy, 1); } return ret; - case 20: /* "having",*/ + case 19: /* "having",*/ if (!obj->condition->having) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_STRING(ret, obj->condition->having, 1); } return ret; - case 21: /* "limit_from",*/ + case 20: /* "limit_from",*/ if (!obj->condition->limit_use) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_LONG(ret, obj->condition->limit_from); } return ret; - case 22: /* "limit_qty",*/ + case 21: /* "limit_qty",*/ if (!obj->condition->limit_use) { ZVAL_BOOL(ret, FALSE); } else { ZVAL_LONG(ret, obj->condition->limit_qty); } return ret; - case 23: /* "select" */ + case 22: /* "select" */ if (!obj->condition->data_select) { ZVAL_BOOL(ret, FALSE); } else { @@ -1351,6 +1259,9 @@ dbdo_class_entry_exception_ce , 0 TSRMLS_CC, "DBDO::%s returned %d rows, only one is expected", get_active_function_name(TSRMLS_C), rows); } + + dbdo_fetch_into_properties(object TSRMLS_CC); + /* g_print("returning %d",rows); */ RETURN_LONG(rows); @@ -1555,6 +1466,7 @@ zclone->value.obj = dbdo_clone(object TSRMLS_CC); dclone = zend_object_store_get_object(zclone TSRMLS_CC); dclone->row_id = r; + dbdo_fetch_into_properties(zclone TSRMLS_CC); zend_hash_next_index_insert(HASH_OF(return_value), &zclone, sizeof(zval *), NULL); } g_object_unref(obj->result); @@ -1690,6 +1602,41 @@ } } +void * +dbdo_fetch_into_properties(zval *object TSRMLS_DC) +{ + + GdaRow *row; + GdaValue *value; + + gchar *string, + *title; + gint column_id; + zval *znew_value, + *ztitle; + + dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); + /* TODO assert stuff */ + + row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); + for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { + + title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); + value = (GdaValue *) gda_row_get_value (row, column_id); + znew_value = dbdo_value_to_zval(value); + + zval_add_ref(&znew_value); + MAKE_STD_ZVAL(ztitle); + ZVAL_STRING(ztitle, title, 1); + std_object_handlers.write_property(object, ztitle, znew_value TSRMLS_CC); + + /*zval_dtor(ztitle); + FREE_ZVAL(ztitle); + FREE_ZVAL(znew_value); + */ + } + +} /* {{{ proto mixed $dbdo->fetch ( ) fetch the results into the object's properties @@ -1711,19 +1658,8 @@ RETURN_FALSE; } /* destroy the row data hash */ - if (obj->row_cache) { - zend_hash_destroy(obj->row_cache); - FREE_HASHTABLE(obj->row_cache); - obj->row_cache = NULL; - } - if (obj->visable_hash) { - zend_hash_destroy(obj->visable_hash); - FREE_HASHTABLE(obj->visable_hash); - obj->visable_hash = NULL; - } - obj->row_cached = 0; obj->row_id++; if (obj->row_id >= gda_data_model_get_n_rows (obj->result)) { /*g_print("fetched all? %d >= %d unreffing dm?\n",obj->row_id , gda_data_model_get_n_rows (obj->dm)); */ @@ -1737,31 +1673,8 @@ RETURN_FALSE; } - - - /* char *ntitle = NULL; */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - value = (GdaValue *) gda_row_get_value (row, column_id); - string = gda_value_stringify (value); - MAKE_STD_ZVAL(ztitle); - ZVAL_STRING(ztitle, title, 1); - if (obj->assigned_hash) { - DBDO_DEBUG(16,"Deleteing Hash Item: %s", title); - zend_hash_del(obj->assigned_hash, title, strlen(title)+1); - } - - /*if (std_object_handlers.has_property(object, ztitle, 0 TSRMLS_CC)) { - std_object_handlers.unset_property(object, ztitle TSRMLS_CC); - } - */ - zval_dtor(ztitle); - FREE_ZVAL(ztitle); - - g_free(string); - } + dbdo_fetch_into_properties(object TSRMLS_CC); + @@ -1879,230 +1792,7 @@ zval_dtor(ret); FREE_ZVAL(ret); } - -static -int -dbdo_property_has(zval *object, zval *member, int check_empty TSRMLS_DC) -{ - zval *return_value; - GdaRow *row; - gint column_id; - GdaValue *value; - char *string; - char *title; - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - - /* TODO: honour check_empty */ - /* if we have it stored return that! */ - if (std_object_handlers.has_property(object, member, 0 TSRMLS_CC)) { - return 1; - } - if ((obj->result == NULL) || !G_IS_OBJECT(obj->result)) { - return 0; - } - /* fetch past end ! - do we really want to do this!!! ??? */ - if (obj->row_id >= gda_data_model_get_n_rows (obj->result)) { - return 0; - } - /* - ?? should we be using the schema to determine this? = probably not! - */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - if (strcmp(Z_STRVAL_P(member), title) != 0) { - continue; - } - return 1; - } - return 0; -} - -/* write property - store twice - in properties and object. */ -static -void -dbdo_property_write(zval *object, zval *member, zval *value TSRMLS_DC) -{ - long h; - zval *test; - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - - h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member) +1); - /* do we need to copy or add ref stuff */ - - value->refcount++; - if (PZVAL_IS_REF(value)) { - SEPARATE_ZVAL(&value); - } - - - zend_hash_quick_update(obj->assigned_hash, Z_STRVAL_P(member), Z_STRLEN_P(member) +1, h, - &value, sizeof(zval *), NULL); - /* not sure if this is needed... */ - std_object_handlers.write_property(object, member, value TSRMLS_CC); - - -} -static -zval * -dbdo_property_read(zval *object, zval *member, int type TSRMLS_DC) -{ - zval *return_value; - char *name; - GdaRow *row; - gint column_id; - GdaValue *value; - char *string; - char *title; - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - zval **element, *use; - /* serialize values - always start with "." */ - DBDO_DEBUG2(16, "dbdo_property_read '%s' FROM %x",Z_STRVAL_P(member),obj); - - /* has it been assigned - return that.. */ - if (zend_hash_find(obj->assigned_hash, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &element) == SUCCESS) { - DBDO_DEBUG(16, "property_read(%s) from assigned data..",Z_STRVAL_P(member)); - use = *element; - return use; - } - - /* if we are asking for something not from the resultset. */ - if ((obj->result == NULL) || !G_IS_OBJECT(obj->result) || (gda_data_model_get_n_rows (obj->result) < 1)) { - DBDO_DEBUG(16,"property_read(%s) from properties.",Z_STRVAL_P(member)); - /* this may be a bit flaky - fetched past end of object.. any old crap left in - properties.. */ - return std_object_handlers.read_property(object, member, type TSRMLS_CC); - } - - - - /* find the matching column */ - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - zval *ret; - ulong h; - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - if (strcmp(Z_STRVAL_P(member), title) != 0) { - continue; - } - value = (GdaValue *) gda_row_get_value (row, column_id); - ret = dbdo_value_to_zval(value); - - - DBDO_DEBUG(16,"property_read(%s) Directly from result set.",Z_STRVAL_P(member)); - return ret; - } - DBDO_DEBUG(16 ,"property_read(%s) fell back to properties.",Z_STRVAL_P(member)); - return std_object_handlers.read_property(object, member, type TSRMLS_CC); - - -} - - -/* -get all the properties.. (eg. used by print_r etc..) -Note this is a little complex as we have a number of considerations -a) data fetched -b) data assigned (thats replaces stuff) -c) data assigned (thats extra / nothing to do with it.. ) -e) .info data used when __sleep etc. is used. -f) a visable view of this data (eg. intern->zo.properties) -g) the fact we have to free it all! - -The strategy we have is: -* maintain an assigned data array : - free/unref on clone / destruct / fetch. --- changed when we set properties -* maintain a fetched data array : - free/unref on clone / destruct / fetch. --- only built when we do a properties_get -* maintain a .info data array : - free/unref on clone / destruct / fetch. --- only added to by __sleep - - - -methods used: -dbdo_object_free_stores(zval *obj TSRMLS_DC) -* destroys all our hash tables -* loops through each of them, and removes any reference in the zo.properties.. - - -This does ask the question - how do/should we deal with arguments that are not related to -the table... - - - - - -*/ -static HashTable * -dbdo_properties_get(zval *object TSRMLS_DC) -{ - zval *tmp; - zval **data_ptr; - zval *value,*ztitle; - zval *newptr; - zval *rvalue; - GdaValue *gdavalue; - gint column_id; - char *title; - char *string; - GdaRow *row; - ulong h; - HashTable *row_hash; - - dbdo_object *obj = zend_object_store_get_object(object TSRMLS_CC); - char *name; - /* BOTH THESE APPEAR TO LEAK */ - - if (obj->result == NULL) { - /* throw errror!! - what about user set values...???? */ - /* empty! */ - /* zend_hash_init(rv, 0, NULL, ZVAL_PTR_DTOR, 0); */ - return std_object_handlers.get_properties(object TSRMLS_CC); - } - - - if (obj->visable_hash) { - /* if we only have a partial view (eg. from a property_read */ - zend_hash_destroy(obj->visable_hash); - FREE_HASHTABLE(obj->visable_hash); - obj->visable_hash = NULL; - } - - ALLOC_HASHTABLE(row_hash); - zend_hash_init(row_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - - /* g_print("copying existing hash?\n"); */ - /* FIXME : visable hash should only be a represenation of the object now */ - - row = (GdaRow *) gda_data_model_get_row (obj->result, obj->row_id); - - if (!row) { - /* assigned data or empty array. */ - return obj->assigned_hash ? obj->assigned_hash : row_hash; - } - - for (column_id = 0; column_id < gda_data_model_get_n_columns (obj->result); column_id++) { - - title = (gchar *) gda_data_model_get_column_title (obj->result, column_id); - gdavalue = (GdaValue *) gda_row_get_value (row, column_id); - rvalue = dbdo_value_to_zval(gdavalue); - DBDO_DEBUG2(16,"fetch row: %s = '%s'",title, gda_value_stringify(gdavalue)); - h = zend_hash_func(title, strlen(title)+1); - zend_hash_quick_update(row_hash, title, strlen(title)+1, h, &rvalue, sizeof(zval *), NULL); - } - ALLOC_HASHTABLE(obj->visable_hash); - zend_hash_init(obj->visable_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_copy(obj->visable_hash, row_hash, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); - zend_hash_destroy(row_hash); - FREE_HASHTABLE(row_hash); - /* overwrite with assigned values. */ - zend_hash_merge(obj->visable_hash, obj->assigned_hash, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *), 1); - return obj->visable_hash; - -} - - - + /* {{{ proto array $do->toArray ( [ string $format ] ) get the result as an array from the object, and optionally format it. @@ -2132,7 +1822,7 @@ MAKE_STD_ZVAL(ztitle); ZVAL_STRING(ztitle, title, 1); - value = dbdo_property_read(object, ztitle, 0 TSRMLS_CC); + value = std_object_handlers.read_property(object, ztitle, 0 TSRMLS_CC); zval_dtor(ztitle); FREE_ZVAL(ztitle); @@ -2338,10 +2028,6 @@ intern->zo.in_get = 0; intern->zo.in_set = 0; intern->zo.properties = NULL; - intern->row_cache = NULL; - intern->visable_hash = NULL; - intern->assigned_hash = NULL; - intern->row_cached = 0; intern->con = old_obj->con; @@ -2364,13 +2050,6 @@ zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); - - ALLOC_HASHTABLE(intern->assigned_hash); - zend_hash_init(intern->assigned_hash, 0, NULL, ZVAL_PTR_DTOR, 0); - zend_hash_copy(intern->assigned_hash, old_obj->assigned_hash, (copy_ctor_func_t) zval_add_ref, - (void *) &tmp, sizeof(zval *)); - - new_obj_val.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) dbdo_object_free_storage, @@ -2525,8 +2204,7 @@ /* TODO: sequenceName usage */ if (obj->table->sequenceName) { - zend_throw_exception(dbdo_class_entry_exception_ce , - "DBDO::insert sequenceName not implemented yet.", 0 TSRMLS_CC); + php_error(E_ERROR, "DBDO::insert with sequenceName not implemented yet."); return; } @@ -2559,20 +2237,15 @@ /* g_print("get from schema row:%d, col:%d\n",row_id,0); */ colname = (gchar *) gda_value_get_string ( (GdaValue *) gda_data_model_get_value_at (obj->table->schema, 0, row_id)); - - /* dont assign the sequence column */ - if (!skippedseq && !strcmp(colname,obj->table->sequenceColumn)) { skippedseq = 1; /* g_print("Skipping matches column: %s\n",colname); */ - continue; } - - + MAKE_STD_ZVAL(ztitle); ZVAL_STRING(ztitle, colname, 1); @@ -2595,8 +2268,6 @@ } - - if (!started) { zend_throw_exception(dbdo_class_entry_exception_ce , "DBDO::insert No data found to assign to columns", 0 TSRMLS_CC); @@ -2606,8 +2277,6 @@ } - - DBDO_DEBUG0(16,"DONE LEFT:"); smart_str_appends(&cmd, " ) VALUES ( "); started = 0; @@ -2655,16 +2324,9 @@ } /* perhaps we should copy here.. */ - std_object_handlers.unset_property(object, ztitle TSRMLS_CC); - if (obj->assigned_hash) { - DBDO_DEBUG(16,"Deleteing Hash Item: %s", colname); - zend_hash_del(obj->assigned_hash, colname, strlen(colname)+1); - } zval_dtor(ztitle); FREE_ZVAL(ztitle); /* zval_dtor(zvalue); */ - - DBDO_DEBUG0(16,"Succeeded!"); if (started) { @@ -2689,7 +2351,6 @@ gda_cmd = gda_command_new ((gchar*)cmd.c, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); smart_str_free(&cmd); dm = gda_connection_execute_single_command (obj->con, gda_cmd, NULL); - gda_command_free (gda_cmd); @@ -2704,10 +2365,11 @@ ret_int = atoi(ret_str); /* g_free(ret_str); */ - - /* TODO:: set the value column to match.... */ + /* TODO: write_property the value to the object */ + + /* make the dataset for the object from the args.. */ - /* TODO: add the autoinc col. */ + { GdaDataModel *model; @@ -2786,8 +2448,10 @@ */ PHP_METHOD(dbdo,update) { - zval *object = getThis(); - zval *zvalue,*ztitle; + zval *object = getThis(), + *zvalue, + *ztitle, + compare_result; GdaCommand *gdacmd; GList *node; gboolean errors = FALSE; @@ -2811,10 +2475,12 @@ smart_str_appends(&cmd, "UPDATE "); smart_str_appends(&cmd, obj->table->name); smart_str_appends(&cmd, " SET "); + + if (!build) { /* - not built = + not build = use Sequence Key to update.. set : changes in everything except for sequenceKey (or keys if set) where = sequenceKeys or keys() */ @@ -2833,6 +2499,7 @@ started = 0; for (row_id = 0; row_id < rows;row_id++) { /* g_print ("rows: %d\n", title,string); */ + int skip = 0; row = (GdaRow *) gda_data_model_get_row (obj->table->schema, row_id); value = (GdaValue *) gda_row_get_value (row, 0); @@ -2843,12 +2510,18 @@ ZVAL_STRING(ztitle, colname, 1); /* skip empty values, sequence columns or keys.. */ - if ( - (obj->table->sequenceColumn && !strcmp(colname,obj->table->sequenceColumn)) || - dbdo_str_in_array(colname, obj->table->keys, obj->table->keys_len) || - !std_object_handlers.has_property(object, ztitle, 0 TSRMLS_CC) - - ) { + if (obj->table->sequenceColumn && !strcmp(colname,obj->table->sequenceColumn)) { + skip++; + } + if (dbdo_str_in_array(colname, obj->table->keys, obj->table->keys_len)) { + skip++; + } + if (!std_object_handlers.has_property(object, ztitle, 0 TSRMLS_CC)) { + skip++; + } + /* TODO: has the value changed?? */ + + if (skip > 0) { DBDO_DEBUG0(16,"Skipping is sequence, is key or has no property"); g_free(colname); zval_dtor(ztitle); @@ -2856,19 +2529,45 @@ continue; } + + zvalue = std_object_handlers.read_property(object, ztitle, 0 TSRMLS_CC); + + /* + see if the value is different to the one in our result set.. + + */ + if (obj->result && G_IS_OBJECT(obj->result) && + (gda_data_model_get_column_position(obj->result, colname) > -1) + ) { + GdaValue *r_gvalue; + zval *znew_value; + r_gvalue = (GdaValue *) gda_data_model_get_value_at ( + obj->result, + gda_data_model_get_column_position(obj->result, colname), + obj->row_id); + + znew_value = dbdo_value_to_zval(r_gvalue); + is_equal_function(&compare_result, zvalue, znew_value TSRMLS_CC); + zval_dtor(znew_value); + FREE_ZVAL(znew_value); + if (Z_LVAL(compare_result)) { + /* they are the same */ + DBDO_DEBUG0(16,"Skipping col, looks like it matches the row.*/"); + g_free(colname); + zval_dtor(ztitle); + FREE_ZVAL(ztitle); + continue; + } + + } if (started) { smart_str_appends(&cmd, " , "); } smart_str_appends(&cmd, colname); smart_str_appends(&cmd, "="); - zvalue = std_object_handlers.read_property(object, ztitle, 0 TSRMLS_CC); - /* - create a GdaValue with type matching the column - do a dbdo_gda_to_sql on it. - */ if (zvalue->type != IS_STRING) { convert_to_string(zvalue); } @@ -2972,7 +2671,7 @@ return; } - smart_str_appends(&cmd, ")"); + smart_str_appends(&cmd, ")"); } else { /* use all the object values to update set. */ @@ -3351,7 +3050,6 @@ - ???password??? - current row - numrows - - assigned data - retrieved data - config ? - sequenceColumn @@ -3696,7 +3394,7 @@ RETURN_FALSE; } /* we have no idea what type (3rd arg means but we are sticking 1 in there */ - ret = dbdo_property_read(object, member, 1 TSRMLS_CC); + ret = std_object_handlers.read_property(object, member, 1 TSRMLS_CC); RETURN_ZVAL(ret, 1, 0); } @@ -3777,7 +3475,7 @@ ZVAL_STRING(member, function+3, 0); /* we have no idea what type (3rd arg means but we are sticking 1 in there */ - ret = dbdo_property_read(object, member, 1 TSRMLS_CC); + ret = std_object_handlers.read_property(object, member, 1 TSRMLS_CC); FREE_ZVAL(member); RETURN_ZVAL(ret, 1, 0); return; http://cvs.php.net/diff.php/pecl/dbdo/php_dbdo.h?r1=1.32&r2=1.33&ty=u Index: pecl/dbdo/php_dbdo.h diff -u pecl/dbdo/php_dbdo.h:1.32 pecl/dbdo/php_dbdo.h:1.33 --- pecl/dbdo/php_dbdo.h:1.32 Thu Mar 17 03:12:46 2005 +++ pecl/dbdo/php_dbdo.h Sat Apr 2 00:32:04 2005 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_dbdo.h,v 1.32 2005/03/17 08:12:46 alan_k Exp $ */ +/* $Id: php_dbdo.h,v 1.33 2005/04/02 05:32:04 alan_k Exp $ */ #ifndef PHP_DBDO_H #define PHP_DBDO_H @@ -42,10 +42,10 @@ -#define DBDO_DEBUG0(lvl,string) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string); } -#define DBDO_DEBUG(lvl,string, arg) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string, arg); } -#define DBDO_DEBUG2(lvl,string, arg1,arg2) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string, arg1,arg2); } -#define DBDO_DEBUG3(lvl,string, arg1,arg2,arg3) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) " string, arg1,arg2,arg3); } +#define DBDO_DEBUG0(lvl,string) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__); } +#define DBDO_DEBUG(lvl,string, arg) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__, arg); } +#define DBDO_DEBUG2(lvl,string, arg1,arg2) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__,arg1,arg2); } +#define DBDO_DEBUG3(lvl,string, arg1,arg2,arg3) if (DBDO_G(debugLevel) & lvl) { php_error(E_WARNING, "DBDO:(Debug) %s(%d) " string, __FUNCTION__, __LINE__,arg1,arg2,arg3); } #define DBDO_QUERY_BUILD_DELETE 0 #define DBDO_QUERY_BUILD_SELECT 1 #define DBDO_QUERY_BUILD_UPDATE 2 @@ -53,16 +53,12 @@ extern ZEND_RSRC_DTOR_FUNC(php_dbdo_client_dtor); static void dbdo_objects_dtor(void *object, zend_object_handle handle TSRMLS_DC); -static int dbdo_property_has(zval *object, zval *member, int check_empty TSRMLS_DC); -static zval * dbdo_property_read(zval *object, zval *member, int type TSRMLS_DC); -static HashTable * dbdo_properties_get(zval *object TSRMLS_DC); -static void dbdo_property_write(zval *object, zval *member, zval *value TSRMLS_DC); static zend_object_value dbdo_clone(zval *zobject TSRMLS_DC); PHP_DBDO_API zend_object_value dbdo_objects_exception_new(zend_class_entry *class_type TSRMLS_DC); PHP_DBDO_API zend_object_value dbdo_objects_new(zend_class_entry *class_type TSRMLS_DC); - +void * dbdo_fetch_into_properties(zval *object TSRMLS_DC); zend_class_entry *dbdo_class_entry_ce, *dbdo_class_entry_exception_ce; @@ -110,10 +106,6 @@ GdaDataModel *result; dbdo_table *table; dbdo_condition *condition; /* conditions builder-- still need free policy */ - HashTable *row_cache; - HashTable *visable_hash; - HashTable *assigned_hash; - int row_cached; } dbdo_object; /* extends zend_object */ /* in dbdo_object.c */