1 30 31 32 package org.hsqldb.test; 33 34 import java.sql.Connection ; 35 import java.sql.PreparedStatement ; 36 import java.sql.ResultSet ; 37 import java.sql.Statement ; 38 39 import junit.framework.TestCase; 40 import junit.framework.TestResult; 41 42 49 public class TestLikePredicateOptimizations extends TestBase { 50 51 public TestLikePredicateOptimizations(String name) { 52 super(name); 53 } 54 55 56 public void test() throws Exception { 57 58 Connection conn = newConnection(); 59 Statement stmt = conn.createStatement(); 60 PreparedStatement pstmt; 61 ResultSet rs; 62 String sql; 63 int expectedCount; 64 int actualCount; 65 66 stmt.execute("drop table test if exists"); 67 68 sql = "create table test(name varchar(255))"; 69 70 stmt.execute(sql); 71 72 sql = "insert into test values(?)"; 73 pstmt = conn.prepareStatement(sql); 74 75 for (int i = 0; i < 10000; i++) { 76 pstmt.setString(1, "name" + i); 77 pstmt.addBatch(); 78 } 79 80 pstmt.executeBatch(); 81 82 sql = "select count(*) from test where name = null"; 83 rs = stmt.executeQuery(sql); 84 85 rs.next(); 86 87 expectedCount = rs.getInt(1); 88 sql = "select count(*) from test where name like null"; 89 pstmt = conn.prepareStatement(sql); 90 rs = pstmt.executeQuery(); 91 92 rs.next(); 93 94 actualCount = rs.getInt(1); 95 96 assertEquals("\"" + sql + "\"", expectedCount, actualCount); 97 98 sql = "select count(*) from test where name = ''"; 100 rs = stmt.executeQuery(sql); 101 102 rs.next(); 103 104 expectedCount = rs.getInt(1); 105 sql = "select count(*) from test where name like ''"; 106 pstmt = conn.prepareStatement(sql); 107 rs = pstmt.executeQuery(); 108 109 rs.next(); 110 111 actualCount = rs.getInt(1); 112 113 assertEquals("\"" + sql + "\"", expectedCount, actualCount); 114 115 sql = "select count(*) from test where name is not null"; 117 rs = stmt.executeQuery(sql); 118 119 rs.next(); 120 121 expectedCount = rs.getInt(1); 122 sql = "select count(*) from test where name like '%'"; 123 pstmt = conn.prepareStatement(sql); 124 rs = pstmt.executeQuery(); 125 126 rs.next(); 127 128 actualCount = rs.getInt(1); 129 130 assertEquals("\"" + sql + "\"", expectedCount, actualCount); 131 132 sql = "select count(*) from test where left(name, 6) = 'name44'"; 134 rs = stmt.executeQuery(sql); 135 136 rs.next(); 137 138 expectedCount = rs.getInt(1); 139 sql = "select count(*) from test where name like 'name44%'"; 140 pstmt = conn.prepareStatement(sql); 141 rs = pstmt.executeQuery(); 142 143 rs.next(); 144 145 actualCount = rs.getInt(1); 146 147 assertEquals("\"" + sql + "\"", expectedCount, actualCount); 148 149 sql = "select count(*) from test where left(name,5) = 'name4' and right(name,1) = 5"; 151 rs = stmt.executeQuery(sql); 152 153 rs.next(); 154 155 expectedCount = rs.getInt(1); 156 sql = "select count(*) from test where name like 'name4%5'"; 157 pstmt = conn.prepareStatement(sql); 158 rs = pstmt.executeQuery(); 159 160 rs.next(); 161 162 actualCount = rs.getInt(1); 163 164 assertEquals("\"" + sql + "\"", expectedCount, actualCount); 165 166 stmt.execute("drop table test1 if exists"); 168 169 sql = "CREATE TABLE test1 (col VARCHAR(30))"; 170 pstmt = conn.prepareStatement(sql); 171 172 pstmt.execute(); 173 174 sql = "INSERT INTO test1 (col) VALUES ('one')"; 175 pstmt = conn.prepareStatement(sql); 176 177 pstmt.execute(); 178 179 sql = "SELECT * FROM test1 WHERE ( col LIKE ? )"; 180 pstmt = conn.prepareStatement(sql); 181 182 pstmt.setString(1, "one"); 183 184 rs = pstmt.executeQuery(); 185 186 rs.next(); 187 188 String presult = rs.getString("COL"); 189 190 sql = "SELECT * FROM test1 WHERE ( col LIKE 'one' )"; 191 pstmt = conn.prepareStatement(sql); 192 rs = pstmt.executeQuery(); 193 194 rs.next(); 195 196 String result = rs.getString("COL"); 197 198 assertEquals("\"" + sql + "\"", result, presult); 199 } 200 201 202 public static void main(String [] args) throws Exception { 203 204 TestResult result; 205 TestCase test; 206 java.util.Enumeration failures; 207 int count; 208 209 result = new TestResult(); 210 test = new TestLikePredicateOptimizations("test"); 211 212 test.run(result); 213 214 count = result.failureCount(); 215 216 System.out.println("TestLikePredicateOptimizations failure count: " 217 + count); 218 219 failures = result.failures(); 220 221 while (failures.hasMoreElements()) { 222 System.out.println(failures.nextElement()); 223 } 224 } 225 } 226 | Popular Tags |