Skip to main content
summaryrefslogtreecommitdiffstats
blob: ec2280ae217ba0597ed7a50db9cb58da8100f595 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2007, 2011 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.gen.internal.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jpt.jpa.db.Column;
import org.eclipse.jpt.jpa.db.ForeignKey;
import org.eclipse.jpt.jpa.db.Schema;
import org.eclipse.jpt.jpa.db.Table;
import org.eclipse.jpt.jpa.db.ForeignKey.ColumnPair;

/**
 * Collection of utility methods to access DTP and other jpt.db APIs 
 *
 */
public class DTPUtil {

	public static boolean isAutoIncrement(Column c){
		//@ TODO
		//Blocked by DTP bug
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=250023
		//The Dali bug is
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=249658
		//
		return false;
	}

	/**
	 * Return list of fk
	 * @param dbTable
	 * @return
	 */
	public static List<ForeignKeyInfo> getForeignKeys(Table dbTable) {
		List<ForeignKeyInfo> ret = new ArrayList<ForeignKeyInfo>();
		if(dbTable!=null){
			for (ForeignKey fk : dbTable.getForeignKeys()) {
				Iterator<ColumnPair> columnPaires = fk.getColumnPairs().iterator();
				ForeignKeyInfo fkInfo = null;
				while( columnPaires.hasNext() ){
					ColumnPair columnPair = columnPaires.next();
					if( fkInfo == null){
						String tableName = dbTable.getName();
						String referencedTableName = "";
						Table referencedTable = fk.getReferencedTable();
						referencedTableName = referencedTable.getName();
						fkInfo = new ForeignKeyInfo(fk, tableName, referencedTableName );
					}
					String baseColName = columnPair.getBaseColumn().getName();
					String referencedColName = columnPair.getReferencedColumn().getName();
					fkInfo.addColumnMapping( baseColName,  referencedColName );
				} 
				if( fkInfo !=null )
					ret.add( fkInfo );
			}
		}
		return ret;
	}

	public static String getJavaType(Column dbColumn) {
		return dbColumn.isPartOfPrimaryKey() ?
				dbColumn.getPrimaryKeyJavaTypeDeclaration() :
				dbColumn.getJavaTypeDeclaration();
	}

	public static boolean isDefaultSchema(Table dbTable){
		String schemaName = dbTable.getSchema().getName();
		Schema defaultSchema = dbTable.getSchema().getDatabase().getDefaultSchema();
		return defaultSchema.getName() == schemaName;
	}	
}

Back to the top