View Javadoc

1   package org.apache.torque.engine.platform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.Connection;
23  import java.sql.DatabaseMetaData;
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.util.List;
28  
29  import org.apache.torque.engine.database.model.Domain;
30  import org.apache.torque.engine.database.model.SchemaType;
31  import org.kuali.common.impex.service.OracleProducer;
32  import org.kuali.common.impex.service.SqlProducer;
33  
34  /**
35   * Oracle Platform implementation.
36   *
37   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
38   * @version $Id: PlatformOracleImpl.java,v 1.1.6.1 2008-04-18 17:04:37 jkeller Exp $
39   */
40  public class PlatformOracleImpl extends PlatformDefaultImpl {
41  	/**
42  	 * Default constructor.
43  	 */
44  	public PlatformOracleImpl() {
45  		super();
46  		initialize();
47  	}
48  
49  	/**
50  	 * Initializes db specific domain mapping.
51  	 */
52  	private void initialize() {
53  		setSchemaDomainMapping(new Domain(SchemaType.TINYINT, "NUMBER", "3", "0"));
54  		setSchemaDomainMapping(new Domain(SchemaType.SMALLINT, "NUMBER", "5", "0"));
55  		setSchemaDomainMapping(new Domain(SchemaType.INTEGER, "NUMBER", "10", "0"));
56  		setSchemaDomainMapping(new Domain(SchemaType.BOOLEANINT, "NUMBER", "1", "0"));
57  		setSchemaDomainMapping(new Domain(SchemaType.BIGINT, "NUMBER", "20", "0"));
58  		setSchemaDomainMapping(new Domain(SchemaType.REAL, "NUMBER"));
59  		setSchemaDomainMapping(new Domain(SchemaType.DOUBLE, "FLOAT"));
60  		setSchemaDomainMapping(new Domain(SchemaType.DECIMAL, "NUMBER"));
61  		setSchemaDomainMapping(new Domain(SchemaType.NUMERIC, "NUMBER"));
62  		setSchemaDomainMapping(new Domain(SchemaType.VARCHAR, "VARCHAR2"));
63  		setSchemaDomainMapping(new Domain(SchemaType.LONGVARCHAR, "VARCHAR2", "2000"));
64  		setSchemaDomainMapping(new Domain(SchemaType.TIME, "DATE"));
65  		setSchemaDomainMapping(new Domain(SchemaType.TIMESTAMP, "TIMESTAMP"));
66  		setSchemaDomainMapping(new Domain(SchemaType.BINARY, "LONG RAW"));
67  		setSchemaDomainMapping(new Domain(SchemaType.VARBINARY, "BLOB"));
68  		setSchemaDomainMapping(new Domain(SchemaType.LONGVARBINARY, "LONG RAW"));
69  	}
70  
71  	@Override
72  	public String getSchemaName(String artifactId) {
73  		String s = super.getSchemaName(artifactId);
74  		return s.toUpperCase();
75  	}
76  
77  	/**
78  	 * @see Platform#getMaxColumnNameLength()
79  	 */
80  	@Override
81  	public int getMaxColumnNameLength() {
82  		return 30;
83  	}
84  
85  	/**
86  	 * @see Platform#getNativeIdMethod()
87  	 */
88  	@Override
89  	public String getNativeIdMethod() {
90  		return Platform.SEQUENCE;
91  	}
92  
93  	/**
94  	 * @see Platform#getAutoIncrement()
95  	 */
96  	@Override
97  	public String getAutoIncrement() {
98  		return "";
99  	}
100 
101 	@Override
102 	public List<String> getPrimaryKeys(DatabaseMetaData dbMeta, String dbSchema, String tableName) throws SQLException {
103 		return super.getPrimaryKeys(dbMeta, dbSchema.toUpperCase(), tableName);
104 	}
105 
106 	@Override
107 	public List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
108 		return super.getTableNames(dbMeta, databaseSchema.toUpperCase());
109 	}
110 
111 	@Override
112 	public boolean isSpecialDefault(String defaultValue) {
113 		defaultValue = defaultValue.toUpperCase();
114 		if (defaultValue.contains("SYS_GUID()") || defaultValue.contains("SYSDATE") || defaultValue.contains("USERENV(\'SESSIONID\')")) {
115 			return true;
116 		}
117 		return false;
118 	}
119 
120 	@Override
121 	public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
122 		try {
123 			PreparedStatement ps = con.prepareStatement("SELECT last_number FROM all_sequences WHERE sequence_owner = ? AND sequence_name = ?");
124 			Long nextVal = 0L;
125 			ps.setString(1, schema.toUpperCase());
126 			ps.setString(2, sequenceName.toUpperCase());
127 			ResultSet rs = ps.executeQuery();
128 			if (rs.next()) {
129 				nextVal = rs.getLong(1);
130 			}
131 			rs.close();
132 			ps.close();
133 			return nextVal;
134 		} catch (SQLException ex) {
135 			System.err.println("Unable to extract sequence definition: " + schema + "." + sequenceName);
136 			ex.printStackTrace();
137 			return 0L;
138 		}
139 	}
140 
141 	@Override
142 	public String getViewDefinition(Connection con, String schema, String viewName) {
143 		try {
144 			PreparedStatement ps = con.prepareStatement("SELECT text FROM all_views WHERE owner = ? AND view_name = ?");
145 			String definition = "";
146 			ps.setString(1, schema.toUpperCase());
147 			ps.setString(2, viewName.toUpperCase());
148 			ResultSet rs = ps.executeQuery();
149 			if (rs.next()) {
150 				definition = rs.getString(1);
151 			}
152 			rs.close();
153 			ps.close();
154 			return definition;
155 		} catch (SQLException ex) {
156 			System.err.println("Unable to extract view definition: " + schema + "." + viewName);
157 			ex.printStackTrace();
158 			return "";
159 		}
160 	}
161 
162 	@Override
163 	public SqlProducer getSqlProducer() {
164 		return new OracleProducer();
165 	}
166 
167 }