10
10
use Yiisoft \Db \Expression \Expression ;
11
11
use Yiisoft \Db \Oracle \Column \BinaryColumn ;
12
12
use Yiisoft \Db \Oracle \Column \JsonColumn ;
13
+ use Yiisoft \Db \Oracle \Connection ;
13
14
use Yiisoft \Db \Oracle \Tests \Provider \ColumnProvider ;
14
15
use Yiisoft \Db \Oracle \Tests \Support \TestTrait ;
15
16
use Yiisoft \Db \Query \Query ;
@@ -29,6 +30,135 @@ final class ColumnTest extends AbstractColumnTest
29
30
{
30
31
use TestTrait;
31
32
33
+ private function insertTypeValues (Connection $ db ): void
34
+ {
35
+ $ db ->createCommand ()->insert (
36
+ 'type ' ,
37
+ [
38
+ 'int_col ' => 1 ,
39
+ 'char_col ' => str_repeat ('x ' , 100 ),
40
+ 'char_col3 ' => null ,
41
+ 'float_col ' => 1.234 ,
42
+ 'blob_col ' => "\x10\x11\x12" ,
43
+ 'timestamp_col ' => new Expression ("TIMESTAMP '2023-07-11 14:50:23' " ),
44
+ 'bool_col ' => false ,
45
+ 'bit_col ' => 0b0110_0110 , // 102
46
+ 'json_col ' => [['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]],
47
+ ]
48
+ )->execute ();
49
+ }
50
+
51
+ private function assertResultValues (array $ result , string $ varsion ): void
52
+ {
53
+ $ this ->assertSame (1 , $ result ['int_col ' ]);
54
+ $ this ->assertSame (str_repeat ('x ' , 100 ), $ result ['char_col ' ]);
55
+ $ this ->assertNull ($ result ['char_col3 ' ]);
56
+ $ this ->assertSame (1.234 , $ result ['float_col ' ]);
57
+ $ this ->assertSame ("\x10\x11\x12" , stream_get_contents ($ result ['blob_col ' ]));
58
+ $ this ->assertEquals (false , $ result ['bool_col ' ]);
59
+ $ this ->assertSame (0b0110_0110 , $ result ['bit_col ' ]);
60
+
61
+ if (version_compare ($ varsion , '21 ' , '>= ' )) {
62
+ $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ result ['json_col ' ]);
63
+ } else {
64
+ $ this ->assertSame ('[{"a":1,"b":null,"c":[1,3,5]}] ' , stream_get_contents ($ result ['json_col ' ]));
65
+ }
66
+ }
67
+
68
+ public function testQueryWithTypecasting (): void
69
+ {
70
+ $ db = $ this ->getConnection ();
71
+ $ varsion = $ db ->getServerInfo ()->getVersion ();
72
+ $ db ->close ();
73
+
74
+ if (version_compare ($ varsion , '21 ' , '>= ' )) {
75
+ $ this ->fixture = 'oci21.sql ' ;
76
+ }
77
+
78
+ $ db = $ this ->getConnection (true );
79
+
80
+ $ this ->insertTypeValues ($ db );
81
+
82
+ $ query = (new Query ($ db ))->from ('type ' )->withTypecasting ();
83
+
84
+ $ result = $ query ->one ();
85
+
86
+ $ this ->assertResultValues ($ result , $ varsion );
87
+
88
+ $ result = $ query ->all ();
89
+
90
+ $ this ->assertResultValues ($ result [0 ], $ varsion );
91
+
92
+ $ db ->close ();
93
+ }
94
+
95
+ public function testCommandWithPhpTypecasting (): void
96
+ {
97
+ $ db = $ this ->getConnection ();
98
+ $ varsion = $ db ->getServerInfo ()->getVersion ();
99
+ $ db ->close ();
100
+
101
+ if (version_compare ($ varsion , '21 ' , '>= ' )) {
102
+ $ this ->fixture = 'oci21.sql ' ;
103
+ }
104
+
105
+ $ db = $ this ->getConnection (true );
106
+
107
+ $ this ->insertTypeValues ($ db );
108
+
109
+ $ command = $ db ->createCommand ('SELECT * FROM "type" ' );
110
+
111
+ $ result = $ command ->withPhpTypecasting ()->queryOne ();
112
+
113
+ $ this ->assertResultValues ($ result , $ varsion );
114
+
115
+ $ result = $ command ->withPhpTypecasting ()->queryAll ();
116
+
117
+ $ this ->assertResultValues ($ result [0 ], $ varsion );
118
+
119
+ $ db ->close ();
120
+ }
121
+
122
+ public function testSelectWithPhpTypecasting (): void
123
+ {
124
+ $ db = $ this ->getConnection ();
125
+
126
+ $ sql = "SELECT null, 1, 2.5, 'string' FROM DUAL " ;
127
+
128
+ $ expected = [
129
+ 'NULL ' => null ,
130
+ 1 => 1.0 ,
131
+ '2.5 ' => 2.5 ,
132
+ "'STRING' " => 'string ' ,
133
+ ];
134
+
135
+ $ result = $ db ->createCommand ($ sql )
136
+ ->withPhpTypecasting ()
137
+ ->queryOne ();
138
+
139
+ $ this ->assertSame ($ expected , $ result );
140
+
141
+ $ result = $ db ->createCommand ($ sql )
142
+ ->withPhpTypecasting ()
143
+ ->queryAll ();
144
+
145
+ $ this ->assertSame ([$ expected ], $ result );
146
+
147
+ $ result = $ db ->createCommand ('SELECT 2.5 FROM DUAL ' )
148
+ ->withPhpTypecasting ()
149
+ ->queryScalar ();
150
+
151
+ $ this ->assertSame (2.5 , $ result );
152
+
153
+ $ result = $ db ->createCommand ('SELECT 2.5 FROM DUAL UNION SELECT 3.3 FROM DUAL ' )
154
+ ->withPhpTypecasting ()
155
+ ->queryColumn ();
156
+
157
+ $ this ->assertSame ([2.5 , 3.3 ], $ result );
158
+
159
+ $ db ->close ();
160
+ }
161
+
32
162
public function testPhpTypeCast (): void
33
163
{
34
164
$ db = $ this ->getConnection ();
@@ -39,26 +169,12 @@ public function testPhpTypeCast(): void
39
169
40
170
$ db ->close ();
41
171
$ db = $ this ->getConnection (true );
42
-
43
- $ command = $ db ->createCommand ();
44
172
$ schema = $ db ->getSchema ();
45
173
$ tableSchema = $ schema ->getTableSchema ('type ' );
46
174
47
- $ command ->insert ('type ' , [
48
- 'int_col ' => 1 ,
49
- 'char_col ' => str_repeat ('x ' , 100 ),
50
- 'char_col3 ' => null ,
51
- 'float_col ' => 1.234 ,
52
- 'blob_col ' => "\x10\x11\x12" ,
53
- 'timestamp_col ' => new Expression ("TIMESTAMP '2023-07-11 14:50:23' " ),
54
- 'bool_col ' => false ,
55
- 'bit_col ' => 0b0110_0110 , // 102
56
- 'json_col ' => [['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]],
57
- ]);
58
- $ command ->execute ();
59
- $ query = (new Query ($ db ))->from ('type ' )->one ();
175
+ $ this ->insertTypeValues ($ db );
60
176
61
- $ this -> assertNotNull ( $ tableSchema );
177
+ $ query = ( new Query ( $ db ))-> from ( ' type ' )-> one ( );
62
178
63
179
$ intColPhpType = $ tableSchema ->getColumn ('int_col ' )?->phpTypecast($ query ['int_col ' ]);
64
180
$ charColPhpType = $ tableSchema ->getColumn ('char_col ' )?->phpTypecast($ query ['char_col ' ]);
0 commit comments