@@ -3787,6 +3787,53 @@ public function testColumnDefaults(): void {
3787
3787
"
3788
3788
);
3789
3789
3790
+ $ result = $ this ->assertQuery ( 'DESCRIBE t ' );
3791
+ $ this ->assertEquals (
3792
+ array (
3793
+ (object ) array (
3794
+ 'Field ' => 'name ' ,
3795
+ 'Type ' => 'varchar(255) ' ,
3796
+ 'Null ' => 'YES ' ,
3797
+ 'Key ' => '' ,
3798
+ 'Default ' => 'CURRENT_TIMESTAMP ' ,
3799
+ 'Extra ' => '' ,
3800
+ ),
3801
+ (object ) array (
3802
+ 'Field ' => 'type ' ,
3803
+ 'Type ' => 'varchar(255) ' ,
3804
+ 'Null ' => 'NO ' ,
3805
+ 'Key ' => '' ,
3806
+ 'Default ' => 'DEFAULT ' ,
3807
+ 'Extra ' => '' ,
3808
+ ),
3809
+ (object ) array (
3810
+ 'Field ' => 'description ' ,
3811
+ 'Type ' => 'varchar(250) ' ,
3812
+ 'Null ' => 'NO ' ,
3813
+ 'Key ' => '' ,
3814
+ 'Default ' => '' ,
3815
+ 'Extra ' => '' ,
3816
+ ),
3817
+ (object ) array (
3818
+ 'Field ' => 'created_at ' ,
3819
+ 'Type ' => 'timestamp ' ,
3820
+ 'Null ' => 'YES ' ,
3821
+ 'Key ' => '' ,
3822
+ 'Default ' => 'CURRENT_TIMESTAMP ' ,
3823
+ 'Extra ' => 'DEFAULT_GENERATED ' ,
3824
+ ),
3825
+ (object ) array (
3826
+ 'Field ' => 'updated_at ' ,
3827
+ 'Type ' => 'timestamp ' ,
3828
+ 'Null ' => 'NO ' ,
3829
+ 'Key ' => '' ,
3830
+ 'Default ' => 'CURRENT_TIMESTAMP ' ,
3831
+ 'Extra ' => 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP ' ,
3832
+ ),
3833
+ ),
3834
+ $ result
3835
+ );
3836
+
3790
3837
$ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
3791
3838
$ this ->assertEquals (
3792
3839
"CREATE TABLE `t` ( \n"
@@ -4379,6 +4426,40 @@ public function testNonStrictSqlModeNotNullWithDefault(): void {
4379
4426
$ this ->assertSame ( '' , $ result [0 ]->value );
4380
4427
}
4381
4428
4429
+ public function testNonStrictModeWithDefaultCurrentTimestamp (): void {
4430
+ $ this ->assertQuery ( "SET SESSION sql_mode = '' " );
4431
+ $ this ->assertQuery ( 'CREATE TABLE t (id INT, value TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ' );
4432
+
4433
+ // INSERT without a value saves CURRENT_TIMESTAMP:
4434
+ $ this ->assertQuery ( 'INSERT INTO t (id) VALUES (1) ' );
4435
+ $ result = $ this ->assertQuery ( 'SELECT * FROM t ' );
4436
+ $ this ->assertCount ( 1 , $ result );
4437
+ $ this ->assertRegExp ( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/ ' , $ result [0 ]->value );
4438
+
4439
+ // UPDATE with NULL saves NULL:
4440
+ $ this ->assertQuery ( 'UPDATE t SET value = NULL ' );
4441
+ $ result = $ this ->assertQuery ( 'SELECT * FROM t ' );
4442
+ $ this ->assertCount ( 1 , $ result );
4443
+ $ this ->assertNull ( $ result [0 ]->value );
4444
+ }
4445
+
4446
+ public function testNonStrictModeWithDefaultCurrentTimestampNotNull (): void {
4447
+ $ this ->assertQuery ( "SET SESSION sql_mode = '' " );
4448
+ $ this ->assertQuery ( 'CREATE TABLE t (id INT, value TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP) ' );
4449
+
4450
+ // INSERT without a value saves CURRENT_TIMESTAMP:
4451
+ $ this ->assertQuery ( 'INSERT INTO t (id) VALUES (1) ' );
4452
+ $ result = $ this ->assertQuery ( 'SELECT * FROM t ' );
4453
+ $ this ->assertCount ( 1 , $ result );
4454
+ $ this ->assertRegExp ( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/ ' , $ result [0 ]->value );
4455
+
4456
+ // UPDATE with NULL saves IMPLICIT DEFAULT:
4457
+ $ this ->assertQuery ( 'UPDATE t SET value = NULL ' );
4458
+ $ result = $ this ->assertQuery ( 'SELECT * FROM t ' );
4459
+ $ this ->assertCount ( 1 , $ result );
4460
+ $ this ->assertSame ( '0000-00-00 00:00:00 ' , $ result [0 ]->value );
4461
+ }
4462
+
4382
4463
public function testNonStrictSqlModeWithNoListedColumns (): void {
4383
4464
$ this ->assertQuery ( "SET SESSION sql_mode = '' " );
4384
4465
@@ -4543,6 +4624,113 @@ public function testNonStrictModeWithReplaceStatement(): void {
4543
4624
$ this ->assertSame ( 'blue ' , $ result [0 ]->color );
4544
4625
}
4545
4626
4627
+ public function testNonStrictModeTypeCasting (): void {
4628
+ $ this ->assertQuery (
4629
+ "CREATE TABLE t (
4630
+ col_int INT,
4631
+ col_float FLOAT,
4632
+ col_double DOUBLE,
4633
+ col_decimal DECIMAL,
4634
+ col_char CHAR(255),
4635
+ col_varchar VARCHAR(255),
4636
+ col_text TEXT,
4637
+ col_bool BOOL,
4638
+ col_bit BIT,
4639
+ col_binary BINARY(255),
4640
+ col_varbinary VARBINARY(255),
4641
+ col_blob BLOB,
4642
+ col_date DATE,
4643
+ col_time TIME,
4644
+ col_datetime DATETIME,
4645
+ col_timestamp TIMESTAMP,
4646
+ col_year YEAR,
4647
+ col_enum ENUM('a', 'b', 'c'),
4648
+ col_set SET('a', 'b', 'c'),
4649
+ col_json JSON
4650
+ ) "
4651
+ );
4652
+
4653
+ // Set non-strict mode.
4654
+ $ this ->assertQuery ( "SET SESSION sql_mode = '' " );
4655
+
4656
+ // INSERT.
4657
+ $ this ->assertQuery (
4658
+ "INSERT INTO t VALUES ('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '') "
4659
+ );
4660
+
4661
+ $ result = $ this ->assertQuery ( 'SELECT * FROM t ' );
4662
+ $ this ->assertCount ( 1 , $ result );
4663
+ $ this ->assertSame ( '0 ' , $ result [0 ]->col_int );
4664
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? '0.0 ' : '0 ' , $ result [0 ]->col_float );
4665
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? '0.0 ' : '0 ' , $ result [0 ]->col_double );
4666
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? '0.0 ' : '0 ' , $ result [0 ]->col_decimal );
4667
+ $ this ->assertSame ( '' , $ result [0 ]->col_char );
4668
+ $ this ->assertSame ( '' , $ result [0 ]->col_varchar );
4669
+ $ this ->assertSame ( '' , $ result [0 ]->col_text );
4670
+ $ this ->assertSame ( '0 ' , $ result [0 ]->col_bool );
4671
+ $ this ->assertSame ( '0 ' , $ result [0 ]->col_bit );
4672
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? null : '' , $ result [0 ]->col_binary );
4673
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? null : '' , $ result [0 ]->col_varbinary );
4674
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? null : '' , $ result [0 ]->col_blob );
4675
+ $ this ->assertSame ( '0000-00-00 ' , $ result [0 ]->col_date );
4676
+ $ this ->assertSame ( '00:00:00 ' , $ result [0 ]->col_time );
4677
+ $ this ->assertSame ( '0000-00-00 00:00:00 ' , $ result [0 ]->col_datetime );
4678
+ $ this ->assertSame ( '0000-00-00 00:00:00 ' , $ result [0 ]->col_timestamp );
4679
+ $ this ->assertSame ( '0000 ' , $ result [0 ]->col_year );
4680
+ $ this ->assertSame ( '' , $ result [0 ]->col_enum );
4681
+ $ this ->assertSame ( '' , $ result [0 ]->col_set );
4682
+ $ this ->assertSame ( '' , $ result [0 ]->col_json ); // TODO: This should not be allowed.
4683
+
4684
+ // UPDATE.
4685
+ $ this ->assertQuery (
4686
+ "UPDATE t SET
4687
+ col_int = '',
4688
+ col_float = '',
4689
+ col_double = '',
4690
+ col_decimal = '',
4691
+ col_char = '',
4692
+ col_varchar = '',
4693
+ col_text = '',
4694
+ col_bool = '',
4695
+ col_bit = '',
4696
+ col_binary = '',
4697
+ col_varbinary = '',
4698
+ col_blob = '',
4699
+ col_date = '',
4700
+ col_time = '',
4701
+ col_datetime = '',
4702
+ col_timestamp = '',
4703
+ col_year = '',
4704
+ col_enum = '',
4705
+ col_set = '',
4706
+ col_json = ''
4707
+ "
4708
+ );
4709
+
4710
+ $ result = $ this ->assertQuery ( 'SELECT * FROM t ' );
4711
+ $ this ->assertCount ( 1 , $ result );
4712
+ $ this ->assertSame ( '0 ' , $ result [0 ]->col_int );
4713
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? '0.0 ' : '0 ' , $ result [0 ]->col_float );
4714
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? '0.0 ' : '0 ' , $ result [0 ]->col_double );
4715
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? '0.0 ' : '0 ' , $ result [0 ]->col_decimal );
4716
+ $ this ->assertSame ( '' , $ result [0 ]->col_char );
4717
+ $ this ->assertSame ( '' , $ result [0 ]->col_varchar );
4718
+ $ this ->assertSame ( '' , $ result [0 ]->col_text );
4719
+ $ this ->assertSame ( '0 ' , $ result [0 ]->col_bool );
4720
+ $ this ->assertSame ( '0 ' , $ result [0 ]->col_bit );
4721
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? null : '' , $ result [0 ]->col_binary );
4722
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? null : '' , $ result [0 ]->col_varbinary );
4723
+ $ this ->assertSame ( PHP_VERSION_ID < 80100 ? null : '' , $ result [0 ]->col_blob );
4724
+ $ this ->assertSame ( '0000-00-00 ' , $ result [0 ]->col_date );
4725
+ $ this ->assertSame ( '00:00:00 ' , $ result [0 ]->col_time );
4726
+ $ this ->assertSame ( '0000-00-00 00:00:00 ' , $ result [0 ]->col_datetime );
4727
+ $ this ->assertSame ( '0000-00-00 00:00:00 ' , $ result [0 ]->col_timestamp );
4728
+ $ this ->assertSame ( '0000 ' , $ result [0 ]->col_year );
4729
+ $ this ->assertSame ( '' , $ result [0 ]->col_enum );
4730
+ $ this ->assertSame ( '' , $ result [0 ]->col_set );
4731
+ $ this ->assertSame ( '' , $ result [0 ]->col_json ); // TODO: This should not be allowed.
4732
+ }
4733
+
4546
4734
public function testSessionSqlModes (): void {
4547
4735
// Syntax: "sql_mode" ("@@sql_mode" for SELECT)
4548
4736
$ this ->assertQuery ( 'SET sql_mode = "ERROR_FOR_DIVISION_BY_ZERO" ' );
0 commit comments