mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
WPDB: Allow null values in the CRUD functions.
Specifically, `::insert()`, `::replace()`, `::update()`, and `::delete()` can now set a column to `NULL`, or add the `IS NULL` condition to the `WHERE` clause. This is based on [backpress 279]. Props pento, nbachiyski, sorich87. Fixes #15158. git-svn-id: https://develop.svn.wordpress.org/trunk@34737 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -807,5 +807,138 @@ class Tests_DB extends WP_UnitTestCase {
|
||||
function filter_pre_get_col_charset( $charset, $table, $column ) {
|
||||
return 'fake_col_charset';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 15158
|
||||
*/
|
||||
function test_null_insert() {
|
||||
global $wpdb;
|
||||
|
||||
$key = 'null_insert_key';
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->postmeta,
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => NULL
|
||||
),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertNull( $row->meta_value );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 15158
|
||||
*/
|
||||
function test_null_update_value() {
|
||||
global $wpdb;
|
||||
|
||||
$key = 'null_update_value_key';
|
||||
$value = 'null_update_value_key';
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->postmeta,
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => $value
|
||||
),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertSame( $value, $row->meta_value );
|
||||
|
||||
$wpdb->update(
|
||||
$wpdb->postmeta,
|
||||
array( 'meta_value' => NULL ),
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => $value
|
||||
),
|
||||
array( '%s' ),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertNull( $row->meta_value );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 15158
|
||||
*/
|
||||
function test_null_update_where() {
|
||||
global $wpdb;
|
||||
|
||||
$key = 'null_update_where_key';
|
||||
$value = 'null_update_where_key';
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->postmeta,
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => NULL
|
||||
),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertNull( $row->meta_value );
|
||||
|
||||
$wpdb->update(
|
||||
$wpdb->postmeta,
|
||||
array( 'meta_value' => $value ),
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => NULL
|
||||
),
|
||||
array( '%s' ),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertSame( $value, $row->meta_value );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 15158
|
||||
*/
|
||||
function test_null_delete() {
|
||||
global $wpdb;
|
||||
|
||||
$key = 'null_update_where_key';
|
||||
$value = 'null_update_where_key';
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->postmeta,
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => NULL
|
||||
),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertNull( $row->meta_value );
|
||||
|
||||
$wpdb->delete(
|
||||
$wpdb->postmeta,
|
||||
array(
|
||||
'meta_key' => $key,
|
||||
'meta_value' => NULL
|
||||
),
|
||||
array( '%s', '%s' )
|
||||
);
|
||||
|
||||
$row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
|
||||
|
||||
$this->assertNull( $row );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user