|
| 1 | +<?php namespace CodeIgniter\Database\Live; |
| 2 | + |
| 3 | +use CodeIgniter\Test\CIDatabaseTestCase; |
| 4 | +use CodeIgniter\Database\BaseBuilder; |
| 5 | + |
| 6 | +/** |
| 7 | + * @group DatabaseLive |
| 8 | + */ |
| 9 | +class WriteTypeQueryTest extends CIDatabaseTestCase |
| 10 | +{ |
| 11 | + protected $refresh = true; |
| 12 | + |
| 13 | + protected $seed = 'Tests\Support\Database\Seeds\CITestSeeder'; |
| 14 | + |
| 15 | + public function testSet() |
| 16 | + { |
| 17 | + $sql = 'SET FOREIGN_KEY_CHECKS=0'; |
| 18 | + |
| 19 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 20 | + } |
| 21 | + |
| 22 | + //-------------------------------------------------------------------- |
| 23 | + |
| 24 | + public function testInsert() |
| 25 | + { |
| 26 | + $builder = $this->db->table('jobs'); |
| 27 | + |
| 28 | + $insertData = [ |
| 29 | + 'id' => 1, |
| 30 | + 'name' => 'Grocery Sales', |
| 31 | + ]; |
| 32 | + $builder->testMode()->insert($insertData, true); |
| 33 | + $sql = $builder->getCompiledInsert(); |
| 34 | + |
| 35 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 36 | + |
| 37 | + if ($this->db->DBDriver === 'Postgre') |
| 38 | + { |
| 39 | + $sql = "INSERT INTO my_table (col1, col2) VALUES ('Joe', 'Cool') RETURNING id;"; |
| 40 | + |
| 41 | + $this->assertFalse($this->db->isWriteType($sql)); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + //-------------------------------------------------------------------- |
| 46 | + |
| 47 | + public function testUpdate() |
| 48 | + { |
| 49 | + $builder = new BaseBuilder('jobs', $this->db); |
| 50 | + $builder->testMode()->where('id', 1)->update(['name' => 'Programmer'], null, null); |
| 51 | + $sql = $builder->getCompiledInsert(); |
| 52 | + |
| 53 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 54 | + |
| 55 | + if ($this->db->DBDriver === 'Postgre') |
| 56 | + { |
| 57 | + $sql = "UPDATE my_table SET col1 = 'foo' WHERE id = 2 RETURNING *;"; |
| 58 | + |
| 59 | + $this->assertFalse($this->db->isWriteType($sql)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + //-------------------------------------------------------------------- |
| 64 | + |
| 65 | + public function testDelete() |
| 66 | + { |
| 67 | + $builder = $this->db->table('jobs'); |
| 68 | + $sql = $builder->testMode()->delete(['id' => 1], null, true); |
| 69 | + |
| 70 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 71 | + } |
| 72 | + |
| 73 | + //-------------------------------------------------------------------- |
| 74 | + |
| 75 | + public function testReplace() |
| 76 | + { |
| 77 | + if (in_array($this->db->DBDriver, ['Postgre', 'SQLSRV'])) |
| 78 | + { |
| 79 | + // these two were complaining about the builder stuff so i just cooked up this |
| 80 | + $sql = 'REPLACE INTO `db_jobs` (`title`, `name`, `date`) VALUES (:title:, :name:, :date:)'; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + $builder = $this->db->table('jobs'); |
| 85 | + $data = [ |
| 86 | + 'title' => 'My title', |
| 87 | + 'name' => 'My Name', |
| 88 | + 'date' => 'My date', |
| 89 | + ]; |
| 90 | + $sql = $builder->testMode()->replace($data); |
| 91 | + } |
| 92 | + |
| 93 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 94 | + } |
| 95 | + |
| 96 | + //-------------------------------------------------------------------- |
| 97 | + |
| 98 | + public function testCreate() |
| 99 | + { |
| 100 | + $sql = 'CREATE DATABASE foo'; |
| 101 | + |
| 102 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 103 | + } |
| 104 | + |
| 105 | + //-------------------------------------------------------------------- |
| 106 | + |
| 107 | + public function testDrop() |
| 108 | + { |
| 109 | + $sql = 'DROP DATABASE foo'; |
| 110 | + |
| 111 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 112 | + |
| 113 | + $sql = 'DROP TABLE foo'; |
| 114 | + |
| 115 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 116 | + } |
| 117 | + |
| 118 | + //-------------------------------------------------------------------- |
| 119 | + |
| 120 | + public function testTruncate() |
| 121 | + { |
| 122 | + $builder = new BaseBuilder('user', $this->db); |
| 123 | + $sql = $builder->testMode()->truncate(); |
| 124 | + |
| 125 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 126 | + } |
| 127 | + |
| 128 | + //-------------------------------------------------------------------- |
| 129 | + |
| 130 | + public function testLoad() |
| 131 | + { |
| 132 | + $sql = "LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test FIELDS TERMINATED BY ',' LINES STARTING BY 'xxx';"; |
| 133 | + |
| 134 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 135 | + } |
| 136 | + |
| 137 | + //-------------------------------------------------------------------- |
| 138 | + |
| 139 | + public function testCopy() |
| 140 | + { |
| 141 | + $sql = "COPY demo(firstname, lastname) TO 'demo.txt' DELIMITER ' ';"; |
| 142 | + |
| 143 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 144 | + } |
| 145 | + |
| 146 | + public function testAlter() |
| 147 | + { |
| 148 | + $sql = 'ALTER TABLE supplier ADD supplier_name char(50);'; |
| 149 | + |
| 150 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 151 | + } |
| 152 | + |
| 153 | + public function testRename() |
| 154 | + { |
| 155 | + $sql = 'EXEC sp_rename table1 , table2 ;'; |
| 156 | + |
| 157 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 158 | + } |
| 159 | + |
| 160 | + public function testGrant() |
| 161 | + { |
| 162 | + $sql = 'GRANT SELECT ON TABLE my_table TO user1,user2'; |
| 163 | + |
| 164 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 165 | + } |
| 166 | + |
| 167 | + public function testRevoke() |
| 168 | + { |
| 169 | + $sql = 'REVOKE SELECT ON TABLE my_table FROM user1,user2'; |
| 170 | + |
| 171 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 172 | + } |
| 173 | + |
| 174 | + public function testLock() |
| 175 | + { |
| 176 | + $sql = 'LOCK TABLE my_table IN SHARE MODE;'; |
| 177 | + |
| 178 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 179 | + } |
| 180 | + |
| 181 | + public function testUnlock() |
| 182 | + { |
| 183 | + // i think this is only a valid command for MySQL? |
| 184 | + $sql = 'UNLOCK TABLES;'; |
| 185 | + |
| 186 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 187 | + } |
| 188 | + |
| 189 | + public function testReindex() |
| 190 | + { |
| 191 | + // i think this is only a valid command for Postgre? |
| 192 | + $sql = 'REINDEX TABLE foo'; |
| 193 | + |
| 194 | + $this->assertTrue($this->db->isWriteType($sql)); |
| 195 | + } |
| 196 | + |
| 197 | + public function testSelect() |
| 198 | + { |
| 199 | + $builder = new BaseBuilder('users', $this->db); |
| 200 | + $builder->select('*'); |
| 201 | + $sql = $builder->getCompiledSelect(); |
| 202 | + |
| 203 | + $this->assertFalse($this->db->isWriteType($sql)); |
| 204 | + } |
| 205 | + |
| 206 | + public function testTrick() |
| 207 | + { |
| 208 | + $builder = new BaseBuilder('users', $this->db); |
| 209 | + $builder->select('UPDATE'); |
| 210 | + $sql = $builder->getCompiledSelect(); |
| 211 | + |
| 212 | + $this->assertFalse($this->db->isWriteType($sql)); |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments