1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.model.FieldMetaData; 14 import java.math.BigInteger ; 15 import java.sql.Connection ; 16 import java.sql.DatabaseMetaData ; 17 import java.sql.ResultSet ; 18 import java.sql.SQLException ; 19 import java.sql.Statement ; 20 import java.sql.Types ; 21 import java.util.ArrayList ; 22 import javax.jdo.JDOFatalDataStoreException; 23 24 25 33 34 class OracleAdapter extends DatabaseAdapter 35 { 36 41 42 public OracleAdapter(DatabaseMetaData metadata) 43 { 44 super(metadata); 45 46 47 typeMappings.put(boolean.class, BooleanCharMapping.class); 48 typeMappings.put(Boolean .class, BooleanCharMapping.class); 49 typeMappings.put(byte[].class, OracleBlobMapping.class); 50 } 51 52 public String getVendorID() 53 { 54 return "oracle"; 55 } 56 57 protected void createTypeInfo(DatabaseMetaData metadata) throws SQLException 58 { 59 super.createTypeInfo(metadata); 60 61 65 Integer clobKey = new Integer (Types.CLOB); 66 if (typesByTypeNumber.get(clobKey) == null) 67 { 68 TypeInfo ti = new TypeInfo("CLOB", 69 (short)Types.CLOB, 70 1073741823, 71 "'", 72 "'", 73 null, 74 1, 75 true, 76 (short)0, 77 false, 78 false, 79 false, 80 "CLOB", 81 (short)0, 82 (short)0, 83 10); 84 85 typesByTypeNumber.put(clobKey, ti); 86 } 87 } 88 89 90 public TypeInfo newTypeInfo(ResultSet rs) 91 { 92 TypeInfo ti = new OracleTypeInfo(rs); 93 94 102 if (ti.dataType == Types.TIMESTAMP) 103 { 104 if (!ti.typeName.equalsIgnoreCase("date")) 105 { 106 ti.typeName = "DATE"; 107 ti.localTypeName = "DATE"; 108 } 109 } 110 else if (ti.typeName.toLowerCase().startsWith("timestamp")) 111 return null; 112 113 return ti; 114 } 115 116 117 public Mapping getMapping(ClassBaseTable table, int relativeFieldNumber) 118 { 119 FieldMetaData fmd = table.getClassMetaData().getFieldRelative(relativeFieldNumber); 120 121 if (fmd.getType().equals(String .class)) 122 { 123 String option = fmd.getLength(); 124 125 if (option != null && option.toLowerCase().equals("unlimited")) 126 return new OracleClobMapping(table, relativeFieldNumber); 127 else 128 return new OracleStringMapping(table, relativeFieldNumber); 129 } 130 else 131 return super.getMapping(table, relativeFieldNumber); 132 } 133 134 public ColumnMapping getMapping(Column col) 135 { 136 if (col.getType().equals(String .class)) 137 return new OracleStringMapping(col); 138 else 139 return super.getMapping(col); 140 } 141 142 143 public boolean isEmbeddedType(Class c) 144 { 145 if (c.equals(String .class)) 146 return !OIDMapping.class.isAssignableFrom(OracleStringMapping.class); 147 else 148 return super.isEmbeddedType(c); 149 } 150 151 152 public String getSchemaName(Connection conn) throws SQLException 153 { 154 Statement stmt = conn.createStatement(); 155 156 try 157 { 158 String stmtText = "SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') FROM DUAL"; 159 ResultSet rs = stmt.executeQuery(stmtText); 160 161 try 162 { 163 if (!rs.next()) 164 throw new JDOFatalDataStoreException("No result returned from " + stmtText); 165 166 return rs.getString(1); 167 } 168 finally 169 { 170 rs.close(); 171 } 172 } 173 finally 174 { 175 stmt.close(); 176 } 177 } 178 179 public boolean supportsBooleanComparison() 180 { 181 return false; 182 } 183 184 public QueryStatement newQueryStatement(Table table) 185 { 186 return new OracleQueryStatement(table); 187 } 188 189 public QueryStatement newQueryStatement(Table table, SQLIdentifier rangeVar) 190 { 191 return new OracleQueryStatement(table, rangeVar); 192 } 193 194 public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar) 195 { 196 return new TableExprAsSubquery(qs, table, rangeVar); 197 } 198 199 public String getDropTableStatement(BaseTable table) 200 { 201 return "DROP TABLE " + table.getName() + " CASCADE CONSTRAINTS"; 202 } 203 204 public NumericExpression lengthMethod(CharacterExpression str) 205 { 206 ArrayList args = new ArrayList (); 207 args.add(str); 208 209 return new NumericExpression("LENGTH", args); 210 } 211 212 public CharacterExpression substringMethod(CharacterExpression str, 213 NumericExpression begin) 214 { 215 ArrayList args = new ArrayList (); 216 args.add(str); 217 args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE))); 218 219 return new CharacterExpression("SUBSTR", args); 220 } 221 222 public CharacterExpression substringMethod(CharacterExpression str, 223 NumericExpression begin, 224 NumericExpression end) 225 { 226 ArrayList args = new ArrayList (); 227 args.add(str); 228 args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE))); 229 args.add(end.sub(begin)); 230 231 return new CharacterExpression("SUBSTR", args); 232 } 233 } 234 | Popular Tags |