1 23 24 29 30 31 package com.sun.jdo.spi.persistence.generator.database; 32 33 import org.netbeans.modules.dbschema.*; 34 import org.netbeans.modules.dbschema.jdbcimpl.*; 35 import org.netbeans.modules.dbschema.util.NameUtil; 36 37 41 class DBElementFactory { 42 45 private final static String TAGLINE = 46 "generated schema version "; 48 52 private static final String SIGNATURE = 53 "$RCSfile: DBElementFactory.java,v $ $Revision: 1.3 $"; 55 56 private static final String UNKNOWN_FIELD_TYPE = "java.lang.Long"; 58 59 private static final String DEFAULT_FIELD_TYPE = "java.lang.Object"; 61 64 private DBElementFactory() { 65 } 66 67 73 static SchemaElement createSchema(String schemaName) throws DBException { 74 SchemaElementImpl schemaImpl = new SchemaElementImpl(); 75 SchemaElement schema = new SchemaElement(schemaImpl); 76 schema.setName(DBIdentifier.create(schemaName)); 77 schema.setDatabaseProductVersion(TAGLINE + SIGNATURE); 78 return schema; 79 } 80 81 88 static TableElement createAndAttachTable(SchemaElement schema, 89 String tableName) throws DBException { 90 91 String fullName = NameUtil.getAbsoluteTableName( 92 schema.getName().getName(), tableName); 93 94 TableElementImpl tableImpl = new TableElementImpl(tableName); 95 TableElement table = new TableElement(tableImpl, schema); 96 table.setName(DBIdentifier.create(fullName)); 97 table.setTableOrView(true); 98 schema.addTable(table); 99 return table; 100 } 101 102 109 static ColumnElement createAndAttachColumn(String columnName, 110 TableElement table, JDBCInfo ji) throws DBException { 111 112 String fullName = NameUtil.getAbsoluteMemberName( 114 table.getName().getName(), columnName); 115 DBIdentifier columnId = DBIdentifier.create(columnName); 116 117 ColumnElementImpl columnImpl = new ColumnElementImpl(); 118 ColumnElement column = new ColumnElement(columnImpl, table); 119 column.setName(columnId); 120 column.setType(ji.getJdbcType()); 121 column.setNullable(ji.getNullable()); 122 column.setPrecision(ji.getPrecision()); 123 column.setScale(ji.getScale()); 124 column.setLength(ji.getLength()); 125 126 table.addColumn(column); 127 128 return column; 129 } 130 131 139 static ColumnPairElement createColumnPair(ColumnElement column, 140 ColumnElement refColumn, TableElement declaringTbl) 141 throws DBException { 142 143 ColumnPairElementImpl pairImpl = new ColumnPairElementImpl(); 144 ColumnPairElement pair = new ColumnPairElement( 145 pairImpl, column, refColumn, declaringTbl); 146 return pair; 147 } 148 149 155 static UniqueKeyElement createAndAttachPrimaryKey(TableElement table, 156 String pKeyName) throws DBException { 157 158 String tableName = table.getName().getName(); 159 String fullName = NameUtil.getAbsoluteMemberName(tableName, pKeyName); 160 161 TableElementImpl tableImpl = (TableElementImpl)table.getElementImpl(); 163 IndexElementImpl indexImpl = 164 new IndexElementImpl(tableImpl, fullName, true); 165 IndexElement index = new IndexElement(indexImpl, table); 166 index.setUnique(true); 167 168 UniqueKeyElementImpl pKeyImpl = new UniqueKeyElementImpl(); 169 UniqueKeyElement pKey = new UniqueKeyElement(pKeyImpl, table, index); 170 pKey.setName(DBIdentifier.create(fullName)); 171 pKey.setPrimaryKey(true); 172 table.addKey(pKey); 173 table.addIndex(pKey.getAssociatedIndex()); 174 return pKey; 175 } 176 177 188 static ForeignKeyElement createAndAttachForeignKey( 189 TableElement declaringTbl, TableElement refTbl, String keyName, 190 MappingPolicy mappingPolicy, String uniqueId) throws DBException { 191 192 String fkeyName = mappingPolicy.getConstraintName(keyName, uniqueId); 193 194 TableElementImpl tableImpl = 195 (TableElementImpl) declaringTbl. getElementImpl(); 196 ForeignKeyElementImpl fkeyImpl = 197 new ForeignKeyElementImpl(tableImpl, fkeyName); 198 ForeignKeyElement fkey = new ForeignKeyElement(fkeyImpl, declaringTbl); 199 200 UniqueKeyElement pk = refTbl.getPrimaryKey(); 201 ColumnElement [] pkColumns = pk.getColumns(); 202 String refTblName = refTbl.getName().getName(); 203 204 if (pkColumns != null) { 206 for (int i = 0; i < pkColumns.length; i++) { 207 ColumnElement refColumn = pkColumns[i]; 208 209 String columnName = 211 mappingPolicy.getConstraintColumnName( 212 refTblName, refColumn.getName().getName()); 213 214 JDBCInfo ji = new JDBCInfo( 216 refColumn.getType(), 217 refColumn.getPrecision(), 218 refColumn.getScale(), 219 refColumn.getLength(), 220 true); 221 222 ColumnElement column = createAndAttachColumn( 224 columnName, declaringTbl, ji); 225 226 ColumnPairElement pair = createColumnPair( 228 column, refColumn, declaringTbl); 229 fkey.addColumnPair(pair); 230 } 231 } 232 declaringTbl.addKey(fkey); 233 return fkey; 234 } 235 236 247 static JDBCInfo getColumnType(String fieldName, String fieldType, 248 MappingPolicy mappingPolicy) { 249 250 if (fieldType == null) { 253 fieldType = UNKNOWN_FIELD_TYPE; 254 } 255 256 JDBCInfo rc = mappingPolicy.getJDBCInfo(fieldName, fieldType); 257 258 if (null == rc) { 261 rc = mappingPolicy.getJDBCInfo(null, DEFAULT_FIELD_TYPE); } 264 return rc; 265 } 266 } 267 | Popular Tags |