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 abstract 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  	@Override
68      public String getServerUrl(String url) {
69  		
70  		return url;
71  	}
72  
73  	@Override
74      public String getSchemaName(String artifactId) {
75  		String suffix = "-db";
76  		if (artifactId.endsWith(suffix)) {
77  			int length = artifactId.length();
78  			artifactId = artifactId.substring(0, length - suffix.length());
79  		}
80  		return StringUtils.remove(artifactId, "-");
81  	}
82  
83  	
84  
85  
86  	@Override
87      public int getMaxColumnNameLength() {
88  		return 64;
89  	}
90  
91  	
92  
93  
94  	@Override
95      public String getNativeIdMethod() {
96  		return Platform.IDENTITY;
97  	}
98  
99  	
100 
101 
102 	@Override
103     public Domain getDomainForSchemaType(SchemaType jdbcType) {
104 		return schemaDomainMap.get(jdbcType);
105 	}
106 
107 	
108 
109 
110 
111 	@Override
112     public String getNullString(boolean notNull) {
113 		
114 		
115 		return (notNull ? "NOT NULL" : "");
116 	}
117 
118 	
119 
120 
121 	@Override
122     public String getAutoIncrement() {
123 		return "IDENTITY";
124 	}
125 
126 	
127 
128 
129 	@Override
130     public boolean hasScale(String sqlType) {
131 		return true;
132 	}
133 
134 	
135 
136 
137 	@Override
138     public boolean hasSize(String sqlType) {
139 		return true;
140 	}
141 
142 	
143 
144 
145 	@Override
146     public boolean createNotNullBeforeAutoincrement() {
147 		return true;
148 	}
149 
150 	@Override
151     public String filterInvalidDefaultValues(String defaultValue) {
152 		return defaultValue;
153 	}
154 
155 	@Override
156     public boolean isSpecialDefault(String defaultValue) {
157 		return false;
158 	}
159 
160 	@Override
161     public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
162 		throw new UnsupportedOperationException("getSequenceDefinition");
163 	}
164 
165 	@Override
166     public String getViewDefinition(Connection con, String schema, String viewName) {
167 		throw new UnsupportedOperationException("getViewDefinition");
168 	}
169 
170 	
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 	@Override
181     public List<String> getPrimaryKeys(DatabaseMetaData dbMeta, String dbSchema, String tableName) throws SQLException {
182 		List<String> pk = new ArrayList<String>();
183 		ResultSet parts = null;
184 		try {
185 			parts = dbMeta.getPrimaryKeys(null, dbSchema, tableName);
186 			while (parts.next()) {
187 				pk.add(parts.getString(4));
188 			}
189 		} finally {
190 			if (parts != null) {
191 				parts.close();
192 			}
193 		}
194 		return pk;
195 	}
196 
197 	protected List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema, String tableType) throws SQLException {
198 		
199 		List<String> tables = new ArrayList<String>();
200 		ResultSet tableNames = null;
201 		
202 		String[] types = { tableType }; 
203 		try {
204 			tableNames = dbMeta.getTables(null, databaseSchema, null, types);
205 			while (tableNames.next()) {
206 				String name = tableNames.getString(3);
207 				tables.add(name);
208 			}
209 		} finally {
210 			if (tableNames != null) {
211 				tableNames.close();
212 			}
213 		}
214 		
215 		return tables;
216 	}
217 
218 	
219 
220 
221 
222 
223 
224 
225 
226 	@Override
227     public List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
228 		return this.getTableNames(dbMeta, databaseSchema, "TABLE");
229 	}
230 
231 	@Override
232     public List<String> getSequenceNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
233 		return this.getTableNames(dbMeta, databaseSchema, "SEQUENCE");
234 	}
235 }