Skip to content

Commit 73132a6

Browse files
committed
Implement setting session SQL modes
1 parent 257e916 commit 73132a6

2 files changed

Lines changed: 329 additions & 11 deletions

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,7 +4041,148 @@ public function testTemporaryTableHasPriorityOverStandardTable(): void {
40414041
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
40424042
}
40434043

4044+
public function testStrictSqlModeNullWithoutDefault(): void {
4045+
$this->assertQuery( "SET SESSION sql_mode = 'STRICT_ALL_TABLES'" );
4046+
4047+
// No value saves NULL:
4048+
$this->assertQuery( 'CREATE TABLE t1 (id INT, value TEXT NULL)' );
4049+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
4050+
$result = $this->assertQuery( 'SELECT * FROM t1' );
4051+
$this->assertCount( 1, $result );
4052+
$this->assertNull( $result[0]->value );
4053+
4054+
// NULL value saves NULL on INSERT:
4055+
$this->assertQuery( 'CREATE TABLE t2 (id INT, value TEXT NULL)' );
4056+
$this->assertQuery( 'INSERT INTO t2 (id, value) VALUES (1, NULL)' );
4057+
$result = $this->assertQuery( 'SELECT * FROM t2' );
4058+
$this->assertCount( 1, $result );
4059+
$this->assertNull( $result[0]->value );
4060+
4061+
// NULL value saves NULL on UPDATE:
4062+
$this->assertQuery( 'CREATE TABLE t3 (id INT, value TEXT NULL)' );
4063+
$this->assertQuery( "INSERT INTO t3 (id, value) VALUES (1, 'initial-value')" );
4064+
$this->assertQuery( 'UPDATE t3 SET value = NULL WHERE id = 1' );
4065+
$result = $this->assertQuery( 'SELECT * FROM t3' );
4066+
$this->assertCount( 1, $result );
4067+
$this->assertNull( $result[0]->value );
4068+
}
4069+
4070+
public function testStrictSqlModeNullWithDefault(): void {
4071+
$this->assertQuery( "SET SESSION sql_mode = 'STRICT_ALL_TABLES'" );
4072+
4073+
// No value saves DEFAULT:
4074+
$this->assertQuery( "CREATE TABLE t1 (id INT, value TEXT NULL DEFAULT 'd')" );
4075+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
4076+
$result = $this->assertQuery( 'SELECT * FROM t1' );
4077+
$this->assertCount( 1, $result );
4078+
$this->assertSame( 'd', $result[0]->value );
4079+
4080+
// NULL value saves NULL on INSERT:
4081+
$this->assertQuery( "CREATE TABLE t2 (id INT, value TEXT NULL DEFAULT 'd')" );
4082+
$this->assertQuery( 'INSERT INTO t2 (id, value) VALUES (1, NULL)' );
4083+
$result = $this->assertQuery( 'SELECT * FROM t2' );
4084+
$this->assertCount( 1, $result );
4085+
$this->assertNull( $result[0]->value );
4086+
4087+
// NULL value saves NULL on UPDATE:
4088+
$this->assertQuery( "CREATE TABLE t3 (id INT, value TEXT NULL DEFAULT 'd')" );
4089+
$this->assertQuery( "INSERT INTO t3 (id, value) VALUES (1, 'initial-value')" );
4090+
$this->assertQuery( 'UPDATE t3 SET value = NULL WHERE id = 1' );
4091+
$result = $this->assertQuery( 'SELECT * FROM t3' );
4092+
$this->assertCount( 1, $result );
4093+
$this->assertNull( $result[0]->value );
4094+
}
4095+
4096+
public function testStrictSqlModeNotNullWithoutDefault(): void {
4097+
$this->assertQuery( "SET SESSION sql_mode = 'STRICT_ALL_TABLES'" );
4098+
4099+
// No value is rejected:
4100+
$this->assertQuery( 'CREATE TABLE t1 (id INT, value TEXT NOT NULL)' );
4101+
$exception = null;
4102+
try {
4103+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
4104+
} catch ( WP_SQLite_Driver_Exception $e ) {
4105+
$exception = $e;
4106+
}
4107+
$this->assertNotNull( $exception );
4108+
$this->assertSame(
4109+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t1.value',
4110+
$exception->getMessage()
4111+
);
4112+
4113+
// NULL value is rejected on INSERT.
4114+
$this->assertQuery( 'CREATE TABLE t2 (id INT, value TEXT NOT NULL)' );
4115+
$exception = null;
4116+
try {
4117+
$this->assertQuery( 'INSERT INTO t2 (id, value) VALUES (1, NULL)' );
4118+
} catch ( WP_SQLite_Driver_Exception $e ) {
4119+
$exception = $e;
4120+
}
4121+
$this->assertNotNull( $exception );
4122+
$this->assertSame(
4123+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t2.value',
4124+
$exception->getMessage()
4125+
);
4126+
4127+
// NULL value is rejected on UPDATE:
4128+
$this->assertQuery( 'CREATE TABLE t3 (id INT, value TEXT NOT NULL)' );
4129+
$exception = null;
4130+
try {
4131+
$this->assertQuery( "INSERT INTO t3 (id, value) VALUES (1, 'initial-value')" );
4132+
$this->assertQuery( 'UPDATE t3 SET value = NULL WHERE id = 1' );
4133+
} catch ( WP_SQLite_Driver_Exception $e ) {
4134+
$exception = $e;
4135+
}
4136+
$this->assertNotNull( $exception );
4137+
$this->assertSame(
4138+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t3.value',
4139+
$exception->getMessage()
4140+
);
4141+
}
4142+
4143+
public function testStrictSqlModeNotNullWithDefault(): void {
4144+
$this->assertQuery( "SET SESSION sql_mode = 'STRICT_ALL_TABLES'" );
4145+
4146+
// No value saves DEFAULT:
4147+
$this->assertQuery( "CREATE TABLE t1 (id INT, value TEXT NOT NULL DEFAULT 'd')" );
4148+
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
4149+
$result = $this->assertQuery( 'SELECT * FROM t1' );
4150+
$this->assertCount( 1, $result );
4151+
$this->assertSame( 'd', $result[0]->value );
4152+
4153+
// NULL value is rejected on INSERT.
4154+
$this->assertQuery( "CREATE TABLE t2 (id INT, value TEXT NOT NULL DEFAULT 'd')" );
4155+
$exception = null;
4156+
try {
4157+
$this->assertQuery( 'INSERT INTO t2 (id, value) VALUES (1, NULL)' );
4158+
} catch ( WP_SQLite_Driver_Exception $e ) {
4159+
$exception = $e;
4160+
}
4161+
$this->assertNotNull( $exception );
4162+
$this->assertSame(
4163+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t2.value',
4164+
$exception->getMessage()
4165+
);
4166+
4167+
// NULL value is rejected on UPDATE:
4168+
$this->assertQuery( 'CREATE TABLE t3 (id INT, value TEXT NOT NULL DEFAULT "d")' );
4169+
$exception = null;
4170+
try {
4171+
$this->assertQuery( "INSERT INTO t3 (id, value) VALUES (1, 'initial-value')" );
4172+
$this->assertQuery( 'UPDATE t3 SET value = NULL WHERE id = 1' );
4173+
} catch ( WP_SQLite_Driver_Exception $e ) {
4174+
$exception = $e;
4175+
}
4176+
$this->assertNotNull( $exception );
4177+
$this->assertSame(
4178+
'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t3.value',
4179+
$exception->getMessage()
4180+
);
4181+
}
4182+
40444183
public function testNonStrictSqlModeNullWithoutDefault(): void {
4184+
$this->assertQuery( "SET SESSION sql_mode = ''" );
4185+
40454186
// No value saves NULL:
40464187
$this->assertQuery( 'CREATE TABLE t1 (id INT, value TEXT NULL)' );
40474188
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
@@ -4066,6 +4207,8 @@ public function testNonStrictSqlModeNullWithoutDefault(): void {
40664207
}
40674208

40684209
public function testNonStrictSqlModeNullWithDefault(): void {
4210+
$this->assertQuery( "SET SESSION sql_mode = ''" );
4211+
40694212
// No value saves DEFAULT:
40704213
$this->assertQuery( "CREATE TABLE t1 (id INT, value TEXT NULL DEFAULT 'd')" );
40714214
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
@@ -4090,6 +4233,8 @@ public function testNonStrictSqlModeNullWithDefault(): void {
40904233
}
40914234

40924235
public function testNonStrictSqlModeNotNullWithoutDefault(): void {
4236+
$this->assertQuery( "SET SESSION sql_mode = ''" );
4237+
40934238
// No value saves IMPLICIT DEFAULT:
40944239
$this->assertQuery( 'CREATE TABLE t1 (id INT, value TEXT NOT NULL)' );
40954240
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );
@@ -4121,6 +4266,8 @@ public function testNonStrictSqlModeNotNullWithoutDefault(): void {
41214266
}
41224267

41234268
public function testNonStrictSqlModeNotNullWithDefault(): void {
4269+
$this->assertQuery( "SET SESSION sql_mode = ''" );
4270+
41244271
// No value saves DEFAULT:
41254272
$this->assertQuery( "CREATE TABLE t1 (id INT, value TEXT NOT NULL DEFAULT 'd')" );
41264273
$this->assertQuery( 'INSERT INTO t1 (id) VALUES (1)' );

0 commit comments

Comments
 (0)