1 10 11 package com.triactive.jdo.test; 12 13 import com.triactive.jdo.store.NoExtentException; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import javax.jdo.Extent; 17 import javax.jdo.PersistenceManager; 18 import javax.jdo.Query; 19 import javax.jdo.Transaction; 20 import org.apache.log4j.Category; 21 22 23 29 30 public class DirectSQLQueryTest extends StorageTestCase 31 { 32 private static final Category LOG = Category.getInstance(DirectSQLQueryTest.class); 33 34 private boolean schemaInitialized = false; 35 36 37 43 44 public DirectSQLQueryTest(String name) 45 { 46 super(name); 47 } 48 49 50 protected void setUp() throws Exception 51 { 52 super.setUp(); 53 54 if (!schemaInitialized) 55 { 56 addClassesToSchema(new Class [] 57 { 58 Widget.class, 59 } 60 ); 61 62 schemaInitialized = true; 63 } 64 } 65 66 67 public void testTJDOSQLQueryOfWidgets() throws Exception 68 { 69 73 if ("sqlserver".equals(vendorID)) 74 return; 75 76 LOG.info("Testing TJDOSQL query against " + TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects"); 77 insertObjects(Widget.class); 78 79 PersistenceManager pm = pmf.getPersistenceManager(); 80 Transaction tx = pm.currentTransaction(); 81 82 try 83 { 84 tx.begin(); 85 86 String sqlText; 87 88 if ("db2".equals(vendorID)) 89 { 90 94 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue}," 95 + " INTEGER(AVG(FLOAT(W.{Widget.intField}))) AS {this.avgIntValue}," 96 + " SMALLINT(AVG(FLOAT(W.{Widget.shortField}))) AS {this.avgShortValue}" 97 + " FROM {Widget} W" 98 + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?" 99 + " GROUP BY W.{Widget.booleanField}"; 100 } 101 else if ("mysql".equals(vendorID)) 102 { 103 107 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue}," 108 + " AVG(W.{Widget.intField}) AS {this.avgIntValue}," 109 + " AVG(W.{Widget.shortField}) AS {this.avgShortValue}" 110 + " FROM {Widget} W" 111 + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?" 112 + " GROUP BY {this.booleanValue}"; 113 } 114 else if ("postgresql".equals(vendorID)) 115 { 116 117 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue}," 118 + " int8(AVG(W.{Widget.intField})) AS {this.avgIntValue}," 119 + " int4(AVG(W.{Widget.shortField})) AS {this.avgShortValue}" 120 + " FROM {Widget} W" 121 + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?" 122 + " GROUP BY W.{Widget.booleanField}"; 123 } 124 else 125 { 126 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue}," 127 + " AVG(W.{Widget.intField}) AS {this.avgIntValue}," 128 + " AVG(W.{Widget.shortField}) AS {this.avgShortValue}" 129 + " FROM {Widget} W" 130 + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?" 131 + " GROUP BY W.{Widget.booleanField}"; 132 } 133 134 short loVal = 0; 135 short hiVal = Short.MAX_VALUE / 2; 136 137 Query query = pm.newQuery("javax.jdo.query.TJDOSQL", sqlText); 138 query.setClass(WidgetAverages.class); 139 query.declareImports("import com.triactive.jdo.test.Widget"); 140 query.declareParameters("short loVal, short hiVal"); 141 Collection results = (Collection )query.execute(new Short (loVal), new Short (hiVal)); 142 143 Iterator i = results.iterator(); 144 int count = 0; 145 146 while (i.hasNext()) 147 { 148 WidgetAverages wa = (WidgetAverages)i.next(); 149 150 assertTrue("", wa.getAvgShortValue() >= loVal && wa.getAvgShortValue() <= hiVal); 151 ++count; 152 } 153 154 assertEquals("TJDOSQL query returned wrong number of rows", 2, count); 155 156 tx.commit(); 157 158 162 163 try 164 { 165 tx.begin(); 166 167 WidgetAverages wa = (WidgetAverages)results.iterator().next(); 168 wa.fillRandom(); 169 170 tx.commit(); 171 172 fail("Writing to a TJDOSQL query result object succeeded"); 173 } 174 catch (NoExtentException e) 175 { 176 if (tx.isActive()) 177 tx.rollback(); 178 } 179 180 184 185 try 186 { 187 tx.begin(); 188 189 WidgetAverages wa = new WidgetAverages(); 190 pm.makePersistent(wa); 191 192 tx.commit(); 193 194 fail("Making a query result object persistent succeeded"); 195 } 196 catch (NoExtentException e) 197 { 198 if (tx.isActive()) 199 tx.rollback(); 200 } 201 202 206 207 try 208 { 209 tx.begin(); 210 211 WidgetAverages wa = (WidgetAverages)results.iterator().next(); 212 pm.deletePersistent(wa); 213 214 tx.commit(); 215 216 fail("Deleting a TJDOSQL query result object succeeded"); 217 } 218 catch (NoExtentException e) 219 { 220 if (tx.isActive()) 221 tx.rollback(); 222 } 223 } 224 finally 225 { 226 if (tx.isActive()) 227 tx.rollback(); 228 229 pm.close(); 230 } 231 232 removeObjects(); 233 } 234 } 235 | Popular Tags |