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  
32  /**
33   * Oracle Platform implementation.
34   * 
35   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
36   * @version $Id: PlatformOracleImpl.java,v 1.1.6.1 2008-04-18 17:04:37 jkeller Exp $
37   */
38  public class PlatformOracleImpl extends PlatformDefaultImpl {
39  	/**
40  	 * Default constructor.
41  	 */
42  	public PlatformOracleImpl() {
43  		super();
44  		initialize();
45  	}
46  
47  	/**
48  	 * Initializes db specific domain mapping.
49  	 */
50  	private void initialize() {
51  		setSchemaDomainMapping(new Domain(SchemaType.TINYINT, "NUMBER", "3", "0"));
52  		setSchemaDomainMapping(new Domain(SchemaType.SMALLINT, "NUMBER", "5", "0"));
53  		setSchemaDomainMapping(new Domain(SchemaType.INTEGER, "NUMBER", "10", "0"));
54  		setSchemaDomainMapping(new Domain(SchemaType.BOOLEANINT, "NUMBER", "1", "0"));
55  		setSchemaDomainMapping(new Domain(SchemaType.BIGINT, "NUMBER", "20", "0"));
56  		setSchemaDomainMapping(new Domain(SchemaType.REAL, "NUMBER"));
57  		setSchemaDomainMapping(new Domain(SchemaType.DOUBLE, "FLOAT"));
58  		setSchemaDomainMapping(new Domain(SchemaType.DECIMAL, "NUMBER"));
59  		setSchemaDomainMapping(new Domain(SchemaType.NUMERIC, "NUMBER"));
60  		setSchemaDomainMapping(new Domain(SchemaType.VARCHAR, "VARCHAR2"));
61  		setSchemaDomainMapping(new Domain(SchemaType.LONGVARCHAR, "VARCHAR2", "2000"));
62  		setSchemaDomainMapping(new Domain(SchemaType.TIME, "DATE"));
63  		setSchemaDomainMapping(new Domain(SchemaType.TIMESTAMP, "TIMESTAMP"));
64  		setSchemaDomainMapping(new Domain(SchemaType.BINARY, "LONG RAW"));
65  		setSchemaDomainMapping(new Domain(SchemaType.VARBINARY, "BLOB"));
66  		setSchemaDomainMapping(new Domain(SchemaType.LONGVARBINARY, "LONG RAW"));
67  	}
68  
69  	@Override
70  	public String getSchemaName(String artifactId) {
71  		String s = super.getSchemaName(artifactId);
72  		return s.toUpperCase();
73  	}
74  
75  	/**
76  	 * @see Platform#getMaxColumnNameLength()
77  	 */
78  	public int getMaxColumnNameLength() {
79  		return 30;
80  	}
81  
82  	/**
83  	 * @see Platform#getNativeIdMethod()
84  	 */
85  	public String getNativeIdMethod() {
86  		return Platform.SEQUENCE;
87  	}
88  
89  	/**
90  	 * @see Platform#getAutoIncrement()
91  	 */
92  	public String getAutoIncrement() {
93  		return "";
94  	}
95  
96  	@Override
97  	public List<String> getPrimaryKeys(DatabaseMetaData dbMeta, String dbSchema, String tableName) throws SQLException {
98  		return super.getPrimaryKeys(dbMeta, dbSchema.toUpperCase(), tableName);
99  	}
100 
101 	public List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
102 		return super.getTableNames(dbMeta, databaseSchema.toUpperCase());
103 	}
104 
105 	@Override
106 	public boolean isSpecialDefault(String defaultValue) {
107 		defaultValue = defaultValue.toUpperCase();
108 		if (defaultValue.contains("SYS_GUID()") || defaultValue.contains("SYSDATE") || defaultValue.contains("USERENV(\'SESSIONID\')")) {
109 			return true;
110 		}
111 		return false;
112 	}
113 
114 	@Override
115 	public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
116 		try {
117 			PreparedStatement ps = con.prepareStatement("SELECT last_number FROM all_sequences WHERE sequence_owner = ? AND sequence_name = ?");
118 			Long nextVal = 0L;
119 			ps.setString(1, schema.toUpperCase());
120 			ps.setString(2, sequenceName.toUpperCase());
121 			ResultSet rs = ps.executeQuery();
122 			if (rs.next()) {
123 				nextVal = rs.getLong(1);
124 			}
125 			rs.close();
126 			ps.close();
127 			return nextVal;
128 		} catch (SQLException ex) {
129 			System.err.println("Unable to extract sequence definition: " + schema + "." + sequenceName);
130 			ex.printStackTrace();
131 			return 0L;
132 		}
133 	}
134 
135 	@Override
136 	public String getViewDefinition(Connection con, String schema, String viewName) {
137 		try {
138 			PreparedStatement ps = con.prepareStatement("SELECT text FROM all_views WHERE owner = ? AND view_name = ?");
139 			String definition = "";
140 			ps.setString(1, schema.toUpperCase());
141 			ps.setString(2, viewName.toUpperCase());
142 			ResultSet rs = ps.executeQuery();
143 			if (rs.next()) {
144 				definition = rs.getString(1);
145 			}
146 			rs.close();
147 			ps.close();
148 			return definition;
149 		} catch (SQLException ex) {
150 			System.err.println("Unable to extract view definition: " + schema + "." + viewName);
151 			ex.printStackTrace();
152 			return "";
153 		}
154 	}
155 
156 }