Skip to content

Commit

Permalink
Fix when using reserved keywords for column names during insert/updat…
Browse files Browse the repository at this point in the history
…e operations
  • Loading branch information
gdarko committed Aug 19, 2023
1 parent d6aba75 commit 2655b52
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public function set( $args ) {
$statement = $key === 'raw'
? [ $arg_value ]
: [
sprintf( '%s', $key ),
sprintf( '`%s`', $key ),
'=',
is_array( $value ) && array_key_exists( 'raw', $value )
? $value['raw']
Expand Down Expand Up @@ -493,7 +493,7 @@ public function values( $args ) {
$arg_value = $this->sanitize_value( $sanitize_callback, $arg_value );
}

$preparedKey = sprintf( '%s', $key );
$preparedKey = sprintf( '`%s`', $key );

if ( is_array( $value ) && array_key_exists( 'raw', $value ) ) {
$this->builder['values'][ $preparedKey ] = $value['raw'];
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/QueryBuilderOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function testUpdate()
$this->assertIsBool( $var );
$this->assertTrue( $var );
$this->assertEquals(
'UPDATE prefix_up SET a = %s',
'UPDATE prefix_up SET `a` = %s',
$wpdb->get_query()
);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/QueryBuilderStatementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ public function testUpdateSetTypes()
->update();
// Assert
$this->assertEquals(
'UPDATE prefix_set1 SET a = %s,b = %d,c = null,d = \'7,8\'',
'UPDATE prefix_set1 SET `a` = %s,`b` = %d,`c` = null,`d` = \'7,8\'',
$wpdb->get_query()
);
}
Expand Down Expand Up @@ -1034,7 +1034,7 @@ public function testUpdateSetRaw()
->update();
// Assert
$this->assertEquals(
'UPDATE prefix_set2 SET c = 1,b = c+1',
'UPDATE prefix_set2 SET c = 1,`b` = c+1',
$wpdb->get_query()
);
}
Expand Down Expand Up @@ -1062,7 +1062,7 @@ public function testUpdateSetForceString()
->update();
// Assert
$this->assertEquals(
'UPDATE prefix_set2 SET a = %s',
'UPDATE prefix_set2 SET `a` = %s',
$wpdb->get_query()
);
}
Expand Down Expand Up @@ -1098,7 +1098,7 @@ public function testUpdateJoinWhere()
->update();
// Assert
$this->assertEquals(
'UPDATE prefix_u1,prefix_u2 JOIN prefix_u2 ON u1.id = u2.id SET u1.title = u2.title,u1.parent = %s WHERE u1.status = %s',
'UPDATE prefix_u1,prefix_u2 JOIN prefix_u2 ON u1.id = u2.id SET u1.title = u2.title,`u1.parent` = %s WHERE u1.status = %s',
$wpdb->get_query()
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/TraitModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function testUpdate()
$this->assertIsBool( $flag );
$this->assertTrue( $flag );
$this->assertEquals(
'UPDATE prefix_' . Model::TABLE . ' SET status = %s',
'UPDATE prefix_' . Model::TABLE . ' SET `status` = %s',
$wpdb->get_query()
);
}
Expand All @@ -149,7 +149,7 @@ public function testUpdateWhere()
$flag = Model::update_all( ['status' => 'active'], ['type' => 'yolo'] );
// Assert
$this->assertEquals(
'UPDATE prefix_' . Model::TABLE . ' SET status = %s WHERE type = %s',
'UPDATE prefix_' . Model::TABLE . ' SET `status` = %s WHERE type = %s',
$wpdb->get_query()
);
}
Expand Down

0 comments on commit 2655b52

Please sign in to comment.