|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Db\Pgsql\Tests\Provider; |
| 6 | + |
| 7 | +use Yiisoft\Db\Expression\Expression; |
| 8 | +use Yiisoft\Db\Tests\Provider\AbstractSchemaProvider; |
| 9 | + |
| 10 | +final class SchemaProvider extends AbstractSchemaProvider |
| 11 | +{ |
| 12 | + public function columns(): array |
| 13 | + { |
| 14 | + return [ |
| 15 | + [ |
| 16 | + [ |
| 17 | + 'int_col' => [ |
| 18 | + 'type' => 'integer', |
| 19 | + 'dbType' => 'int4', |
| 20 | + 'phpType' => 'integer', |
| 21 | + 'allowNull' => false, |
| 22 | + 'autoIncrement' => false, |
| 23 | + 'enumValues' => null, |
| 24 | + 'size' => null, |
| 25 | + 'precision' => 32, |
| 26 | + 'scale' => 0, |
| 27 | + 'defaultValue' => null, |
| 28 | + ], |
| 29 | + 'int_col2' => [ |
| 30 | + 'type' => 'integer', |
| 31 | + 'dbType' => 'int4', |
| 32 | + 'phpType' => 'integer', |
| 33 | + 'allowNull' => true, |
| 34 | + 'autoIncrement' => false, |
| 35 | + 'enumValues' => null, |
| 36 | + 'size' => null, |
| 37 | + 'precision' => 32, |
| 38 | + 'scale' => 0, |
| 39 | + 'defaultValue' => 1, |
| 40 | + ], |
| 41 | + 'tinyint_col' => [ |
| 42 | + 'type' => 'smallint', |
| 43 | + 'dbType' => 'int2', |
| 44 | + 'phpType' => 'integer', |
| 45 | + 'allowNull' => true, |
| 46 | + 'autoIncrement' => false, |
| 47 | + 'enumValues' => null, |
| 48 | + 'size' => null, |
| 49 | + 'precision' => 16, |
| 50 | + 'scale' => 0, |
| 51 | + 'defaultValue' => 1, |
| 52 | + ], |
| 53 | + 'smallint_col' => [ |
| 54 | + 'type' => 'smallint', |
| 55 | + 'dbType' => 'int2', |
| 56 | + 'phpType' => 'integer', |
| 57 | + 'allowNull' => true, |
| 58 | + 'autoIncrement' => false, |
| 59 | + 'enumValues' => null, |
| 60 | + 'size' => null, |
| 61 | + 'precision' => 16, |
| 62 | + 'scale' => 0, |
| 63 | + 'defaultValue' => 1, |
| 64 | + ], |
| 65 | + 'char_col' => [ |
| 66 | + 'type' => 'char', |
| 67 | + 'dbType' => 'bpchar', |
| 68 | + 'phpType' => 'string', |
| 69 | + 'allowNull' => false, |
| 70 | + 'autoIncrement' => false, |
| 71 | + 'enumValues' => null, |
| 72 | + 'size' => 100, |
| 73 | + 'precision' => null, |
| 74 | + 'scale' => null, |
| 75 | + 'defaultValue' => null, |
| 76 | + ], |
| 77 | + 'char_col2' => [ |
| 78 | + 'type' => 'string', |
| 79 | + 'dbType' => 'varchar', |
| 80 | + 'phpType' => 'string', |
| 81 | + 'allowNull' => true, |
| 82 | + 'autoIncrement' => false, |
| 83 | + 'enumValues' => null, |
| 84 | + 'size' => 100, |
| 85 | + 'precision' => null, |
| 86 | + 'scale' => null, |
| 87 | + 'defaultValue' => 'something', |
| 88 | + ], |
| 89 | + 'char_col3' => [ |
| 90 | + 'type' => 'text', |
| 91 | + 'dbType' => 'text', |
| 92 | + 'phpType' => 'string', |
| 93 | + 'allowNull' => true, |
| 94 | + 'autoIncrement' => false, |
| 95 | + 'enumValues' => null, |
| 96 | + 'size' => null, |
| 97 | + 'precision' => null, |
| 98 | + 'scale' => null, |
| 99 | + 'defaultValue' => null, |
| 100 | + ], |
| 101 | + 'float_col' => [ |
| 102 | + 'type' => 'double', |
| 103 | + 'dbType' => 'float8', |
| 104 | + 'phpType' => 'double', |
| 105 | + 'allowNull' => false, |
| 106 | + 'autoIncrement' => false, |
| 107 | + 'enumValues' => null, |
| 108 | + 'size' => null, |
| 109 | + 'precision' => 53, |
| 110 | + 'scale' => null, |
| 111 | + 'defaultValue' => null, |
| 112 | + ], |
| 113 | + 'float_col2' => [ |
| 114 | + 'type' => 'double', |
| 115 | + 'dbType' => 'float8', |
| 116 | + 'phpType' => 'double', |
| 117 | + 'allowNull' => true, |
| 118 | + 'autoIncrement' => false, |
| 119 | + 'enumValues' => null, |
| 120 | + 'size' => null, |
| 121 | + 'precision' => 53, |
| 122 | + 'scale' => null, |
| 123 | + 'defaultValue' => 1.23, |
| 124 | + ], |
| 125 | + 'blob_col' => [ |
| 126 | + 'type' => 'binary', |
| 127 | + 'dbType' => 'bytea', |
| 128 | + 'phpType' => 'resource', |
| 129 | + 'allowNull' => true, |
| 130 | + 'autoIncrement' => false, |
| 131 | + 'enumValues' => null, |
| 132 | + 'size' => null, |
| 133 | + 'precision' => null, |
| 134 | + 'scale' => null, |
| 135 | + 'defaultValue' => null, |
| 136 | + ], |
| 137 | + 'numeric_col' => [ |
| 138 | + 'type' => 'decimal', |
| 139 | + 'dbType' => 'numeric', |
| 140 | + 'phpType' => 'string', |
| 141 | + 'allowNull' => true, |
| 142 | + 'autoIncrement' => false, |
| 143 | + 'enumValues' => null, |
| 144 | + 'size' => null, |
| 145 | + 'precision' => 5, |
| 146 | + 'scale' => 2, |
| 147 | + 'defaultValue' => '33.22', |
| 148 | + ], |
| 149 | + 'time' => [ |
| 150 | + 'type' => 'timestamp', |
| 151 | + 'dbType' => 'timestamp', |
| 152 | + 'phpType' => 'string', |
| 153 | + 'allowNull' => false, |
| 154 | + 'autoIncrement' => false, |
| 155 | + 'enumValues' => null, |
| 156 | + 'size' => null, |
| 157 | + 'precision' => null, |
| 158 | + 'scale' => null, |
| 159 | + 'defaultValue' => '2002-01-01 00:00:00', |
| 160 | + ], |
| 161 | + 'bool_col' => [ |
| 162 | + 'type' => 'boolean', |
| 163 | + 'dbType' => 'bool', |
| 164 | + 'phpType' => 'boolean', |
| 165 | + 'allowNull' => false, |
| 166 | + 'autoIncrement' => false, |
| 167 | + 'enumValues' => null, |
| 168 | + 'size' => null, |
| 169 | + 'precision' => null, |
| 170 | + 'scale' => null, |
| 171 | + 'defaultValue' => null, |
| 172 | + ], |
| 173 | + 'bool_col2' => [ |
| 174 | + 'type' => 'boolean', |
| 175 | + 'dbType' => 'bool', |
| 176 | + 'phpType' => 'boolean', |
| 177 | + 'allowNull' => true, |
| 178 | + 'autoIncrement' => false, |
| 179 | + 'enumValues' => null, |
| 180 | + 'size' => null, |
| 181 | + 'precision' => null, |
| 182 | + 'scale' => null, |
| 183 | + 'defaultValue' => true, |
| 184 | + ], |
| 185 | + 'ts_default' => [ |
| 186 | + 'type' => 'timestamp', |
| 187 | + 'dbType' => 'timestamp', |
| 188 | + 'phpType' => 'string', |
| 189 | + 'allowNull' => false, |
| 190 | + 'autoIncrement' => false, |
| 191 | + 'enumValues' => null, |
| 192 | + 'size' => null, |
| 193 | + 'precision' => null, |
| 194 | + 'scale' => null, |
| 195 | + 'defaultValue' => new Expression('now()'), |
| 196 | + ], |
| 197 | + 'bit_col' => [ |
| 198 | + 'type' => 'integer', |
| 199 | + 'dbType' => 'bit', |
| 200 | + 'phpType' => 'integer', |
| 201 | + 'allowNull' => false, |
| 202 | + 'autoIncrement' => false, |
| 203 | + 'enumValues' => null, |
| 204 | + 'size' => 8, |
| 205 | + 'precision' => null, |
| 206 | + 'scale' => null, |
| 207 | + 'defaultValue' => 130, //b '10000010' |
| 208 | + ], |
| 209 | + 'bigint_col' => [ |
| 210 | + 'type' => 'bigint', |
| 211 | + 'dbType' => 'int8', |
| 212 | + 'phpType' => 'integer', |
| 213 | + 'allowNull' => true, |
| 214 | + 'autoIncrement' => false, |
| 215 | + 'enumValues' => null, |
| 216 | + 'size' => null, |
| 217 | + 'precision' => 64, |
| 218 | + 'scale' => 0, |
| 219 | + 'defaultValue' => null, |
| 220 | + ], |
| 221 | + 'intarray_col' => [ |
| 222 | + 'type' => 'integer', |
| 223 | + 'dbType' => 'int4', |
| 224 | + 'phpType' => 'integer', |
| 225 | + 'allowNull' => true, |
| 226 | + 'autoIncrement' => false, |
| 227 | + 'enumValues' => null, |
| 228 | + 'size' => null, |
| 229 | + 'precision' => null, |
| 230 | + 'scale' => null, |
| 231 | + 'defaultValue' => null, |
| 232 | + 'dimension' => 1, |
| 233 | + ], |
| 234 | + 'textarray2_col' => [ |
| 235 | + 'type' => 'text', |
| 236 | + 'dbType' => 'text', |
| 237 | + 'phpType' => 'string', |
| 238 | + 'allowNull' => true, |
| 239 | + 'autoIncrement' => false, |
| 240 | + 'enumValues' => null, |
| 241 | + 'size' => null, |
| 242 | + 'precision' => null, |
| 243 | + 'scale' => null, |
| 244 | + 'defaultValue' => null, |
| 245 | + 'dimension' => 2, |
| 246 | + ], |
| 247 | + 'json_col' => [ |
| 248 | + 'type' => 'json', |
| 249 | + 'dbType' => 'json', |
| 250 | + 'phpType' => 'array', |
| 251 | + 'allowNull' => true, |
| 252 | + 'autoIncrement' => false, |
| 253 | + 'enumValues' => null, |
| 254 | + 'size' => null, |
| 255 | + 'precision' => null, |
| 256 | + 'scale' => null, |
| 257 | + 'defaultValue' => ['a' => 1], |
| 258 | + 'dimension' => 0, |
| 259 | + ], |
| 260 | + 'jsonb_col' => [ |
| 261 | + 'type' => 'json', |
| 262 | + 'dbType' => 'jsonb', |
| 263 | + 'phpType' => 'array', |
| 264 | + 'allowNull' => true, |
| 265 | + 'autoIncrement' => false, |
| 266 | + 'enumValues' => null, |
| 267 | + 'size' => null, |
| 268 | + 'precision' => null, |
| 269 | + 'scale' => null, |
| 270 | + 'defaultValue' => null, |
| 271 | + 'dimension' => 0, |
| 272 | + ], |
| 273 | + 'jsonarray_col' => [ |
| 274 | + 'type' => 'json', |
| 275 | + 'dbType' => 'json', |
| 276 | + 'phpType' => 'array', |
| 277 | + 'allowNull' => true, |
| 278 | + 'autoIncrement' => false, |
| 279 | + 'enumValues' => null, |
| 280 | + 'size' => null, |
| 281 | + 'precision' => null, |
| 282 | + 'scale' => null, |
| 283 | + 'defaultValue' => null, |
| 284 | + 'dimension' => 1, |
| 285 | + ], |
| 286 | + ], |
| 287 | + ], |
| 288 | + ]; |
| 289 | + } |
| 290 | + |
| 291 | + public function columnsTypeChar(): array |
| 292 | + { |
| 293 | + $columnsTypeChar = parent::columnsTypeChar(); |
| 294 | + |
| 295 | + $columnsTypeChar[0][3] = 'bpchar'; |
| 296 | + $columnsTypeChar[1][3] = 'varchar'; |
| 297 | + |
| 298 | + return $columnsTypeChar; |
| 299 | + } |
| 300 | + |
| 301 | + public function constraints(): array |
| 302 | + { |
| 303 | + $constraints = parent::constraints(); |
| 304 | + |
| 305 | + $constraints['1: check'][2][0]->expression('CHECK ((("C_check")::text <> \'\'::text))'); |
| 306 | + $constraints['3: foreign key'][2][0]->foreignSchemaName('public'); |
| 307 | + $constraints['3: index'][2] = []; |
| 308 | + |
| 309 | + return $constraints; |
| 310 | + } |
| 311 | + |
| 312 | + public function tableSchemaCacheWithTablePrefixes(): array |
| 313 | + { |
| 314 | + $configs = [ |
| 315 | + ['prefix' => '', 'name' => 'type'], |
| 316 | + ['prefix' => '', 'name' => '{{%type}}'], |
| 317 | + ['prefix' => 'ty', 'name' => '{{%pe}}'], |
| 318 | + ]; |
| 319 | + |
| 320 | + $data = []; |
| 321 | + |
| 322 | + foreach ($configs as $config) { |
| 323 | + foreach ($configs as $testConfig) { |
| 324 | + if ($config === $testConfig) { |
| 325 | + continue; |
| 326 | + } |
| 327 | + |
| 328 | + $description = sprintf( |
| 329 | + "%s (with '%s' prefix) against %s (with '%s' prefix)", |
| 330 | + $config['name'], |
| 331 | + $config['prefix'], |
| 332 | + $testConfig['name'], |
| 333 | + $testConfig['prefix'] |
| 334 | + ); |
| 335 | + $data[$description] = [ |
| 336 | + $config['prefix'], |
| 337 | + $config['name'], |
| 338 | + $testConfig['prefix'], |
| 339 | + $testConfig['name'], |
| 340 | + ]; |
| 341 | + } |
| 342 | + } |
| 343 | + |
| 344 | + return $data; |
| 345 | + } |
| 346 | + |
| 347 | + public function tableSchemaWithDbSchemes(): array |
| 348 | + { |
| 349 | + return [ |
| 350 | + ['animal', 'animal', 'public'], |
| 351 | + ['public.animal', 'animal', 'public'], |
| 352 | + ['"public"."animal"', 'animal', 'public'], |
| 353 | + ['"other"."animal2"', 'animal2', 'other',], |
| 354 | + ['other."animal2"', 'animal2', 'other',], |
| 355 | + ['other.animal2', 'animal2', 'other',], |
| 356 | + ['catalog.other.animal2', 'animal2', 'other'], |
| 357 | + ]; |
| 358 | + } |
| 359 | +} |
0 commit comments