1   package org.apache.torque.engine.platform;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.sql.Connection;
23  import java.sql.DatabaseMetaData;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.apache.commons.lang.StringUtils;
33  import org.apache.torque.engine.database.model.Domain;
34  import org.apache.torque.engine.database.model.SchemaType;
35  
36  
37  
38  
39  
40  
41  
42  public class PlatformDefaultImpl implements Platform {
43  	private Map<SchemaType, Domain> schemaDomainMap;
44  
45  	
46  
47  
48  	public PlatformDefaultImpl() {
49  		initialize();
50  	}
51  
52  	private void initialize() {
53  		schemaDomainMap = new HashMap<SchemaType, Domain>(30);
54  		Iterator<SchemaType> iter = SchemaType.iterator();
55  		while (iter.hasNext()) {
56  			SchemaType type = iter.next();
57  			schemaDomainMap.put(type, new Domain(type));
58  		}
59  		schemaDomainMap.put(SchemaType.BOOLEANCHAR, new Domain(SchemaType.BOOLEANCHAR, "CHAR"));
60  		schemaDomainMap.put(SchemaType.BOOLEANINT, new Domain(SchemaType.BOOLEANINT, "INTEGER"));
61  	}
62  
63  	protected void setSchemaDomainMapping(Domain domain) {
64  		schemaDomainMap.put(domain.getType(), domain);
65  	}
66  
67  	public String getServerUrl(String url) {
68  		
69  		return url;
70  	}
71  
72  	public String getSchemaName(String artifactId) {
73  		String suffix = "-db";
74  		if (artifactId.endsWith(suffix)) {
75  			int length = artifactId.length();
76  			artifactId = artifactId.substring(0, length - suffix.length());
77  		}
78  		return StringUtils.remove(artifactId, "-");
79  	}
80  
81  	
82  
83  
84  	public int getMaxColumnNameLength() {
85  		return 64;
86  	}
87  
88  	
89  
90  
91  	public String getNativeIdMethod() {
92  		return Platform.IDENTITY;
93  	}
94  
95  	
96  
97  
98  	public Domain getDomainForSchemaType(SchemaType jdbcType) {
99  		return schemaDomainMap.get(jdbcType);
100 	}
101 
102 	
103 
104 
105 
106 	public String getNullString(boolean notNull) {
107 		
108 		
109 		return (notNull ? "NOT NULL" : "");
110 	}
111 
112 	
113 
114 
115 	public String getAutoIncrement() {
116 		return "IDENTITY";
117 	}
118 
119 	
120 
121 
122 	public boolean hasScale(String sqlType) {
123 		return true;
124 	}
125 
126 	
127 
128 
129 	public boolean hasSize(String sqlType) {
130 		return true;
131 	}
132 
133 	
134 
135 
136 	public boolean createNotNullBeforeAutoincrement() {
137 		return true;
138 	}
139 
140 	public String filterInvalidDefaultValues(String defaultValue) {
141 		return defaultValue;
142 	}
143 
144 	public boolean isSpecialDefault(String defaultValue) {
145 		return false;
146 	}
147 
148 	public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
149 		throw new UnsupportedOperationException("getSequenceDefinition");
150 	}
151 
152 	public String getViewDefinition(Connection con, String schema, String viewName) {
153 		throw new UnsupportedOperationException("getViewDefinition");
154 	}
155 
156 	
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 	public List<String> getPrimaryKeys(DatabaseMetaData dbMeta, String dbSchema, String tableName) throws SQLException {
167 		List<String> pk = new ArrayList<String>();
168 		ResultSet parts = null;
169 		try {
170 			parts = dbMeta.getPrimaryKeys(null, dbSchema, tableName);
171 			while (parts.next()) {
172 				pk.add(parts.getString(4));
173 			}
174 		} finally {
175 			if (parts != null) {
176 				parts.close();
177 			}
178 		}
179 		return pk;
180 	}
181 
182 	protected List<String>getTableNames (DatabaseMetaData dbMeta, String databaseSchema, String tableType) throws SQLException {
183 		
184 				List<String> tables = new ArrayList<String>();
185 				ResultSet tableNames = null;
186 				
187 				String[] types = { tableType }; 
188 				try {
189 					tableNames = dbMeta.getTables(null, databaseSchema, null, types);
190 					while (tableNames.next()) {
191 						String name = tableNames.getString(3);
192 						tables.add(name);
193 					}
194 				} finally {
195 					if (tableNames != null) {
196 						tableNames.close();
197 					}
198 				}
199 				
200 				return tables;
201 	}
202 	
203 
204 
205 
206 
207 
208 
209 
210 	public List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
211 		return this.getTableNames(dbMeta, databaseSchema, "TABLE");
212 	}
213 	
214 	public List<String>getSequenceNames (DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
215 		return this.getTableNames(dbMeta, databaseSchema, "SEQUENCE");
216 	}
217 }