Opened 17 years ago
Closed 16 years ago
#7171 closed task (blessed) (fixed)
wpdb::insert() & wpdb::update() need to allow typecasting of params.
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 2.8 | Priority: | normal |
Severity: | normal | Version: | 2.6 |
Component: | Administration | Keywords: | dev-feedback has-patch early |
Focuses: | Cc: |
Description
As a follow up to: http://trac.wordpress.org/ticket/6836#comment:8
insert() and update() should prepare the values before inserting/updating
I see 2 options;
- 3rd param to insert() & update() which specifies the field types; Eg: http://trac.wordpress.org/ticket/6836#comment:9
The downside to that is it'll require the type to be specified on each insert/update statement
- defining the field-types in an array to check against eg:
$wpdb->fields = array( $wpdb->posts => array('ID' => '%d', 'post_content' => '%s') ); .. in insert(): foreach( (array)$data as $key => $value ) if( isset($this->fields[ $table ][ $key ]) ) $data[$key] = sprintf($this->fields[ $table ][ $key ], $value);
The downside of this approach, Is that it'll require the list to be kept updated, However, it has the advantage of being kept updated in a single location.
If theres a preference for either method, I'm happy to roll up a patch for it.
Attachments (3)
Change History (17)
#4
@
16 years ago
- Keywords has-patch early added
attachment 7171.diff added.
- Auto prepares insert() and update() queries
- Caches table column types upon query, and uses them later on to determine the sprintf token to use
- Loads any columns from the DB needed if they're not allready loaded.
- No changes to calling of insert() / update() needed, Just ensure data is passed non-sql-escaped.
- Code modified from a version of another app i'm developing off BackPress, So i've tested it over the past few weeks with 0 problems.
#5
@
16 years ago
How about hard-coding an array of tables, columns, and types so we can avoid any extra queries?
#6
@
16 years ago
How about hard-coding an array of tables, columns, and types so we can avoid any extra queries?
That was an option, But in my testing, Its rare any extra queries are needed, at least on the most commonly accessed tables, My main reasoning for the approach i took was so that it would be usable by all applications/plugins(custom tables), and BackPress without having to define the list for every application.
Of course, Go ahead and hard code them if you want :)
#7
@
16 years ago
I added a patch so that you can pass an optional argument to update or insert that specifies the format. If a string, that format will be applied to all of the fields; if an array, then the array member format corresponds to the field value.
Any comments on the method of typecasting?
3 options:
the 3rd option half-takes place on all SELECTS allready: http://trac.wordpress.org/browser/trunk/wp-includes/wp-db.php#L631