001/*
002 * Copyright (c) 2008-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-21 22:15:53 +0000 (Fri, 21 Aug 2015) $' 
007 * '$Revision: 33602 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.util.sql;
031
032import java.sql.SQLException;
033
034/**
035 *
036 * An implementation of DatabaseType for Oracle.
037 *
038 * @author Daniel Crawl
039 * @version $Id: Oracle.java 33602 2015-08-21 22:15:53Z crawl $
040 *
041 */
042    
043public class Oracle extends DatabaseType
044{
045    /** Only this package (DatabaseFactory) can instantiate. */
046    protected Oracle()
047    {
048        super();
049    }
050    
051    /** Adjust the name of a column. */
052    @Override
053    public String getColumnName(String columnName)
054    {
055        // user is reserved
056        if(columnName.equals("user"))
057        {
058            return "\"user\"";
059        }
060        return super.getColumnName(columnName);
061    }
062    
063    /** Get a string representing the default time. */
064    @Override
065    public String getDefaultTimeStr()
066    {
067        return "'01-Jan-00 1:0:0 am'";
068    }
069
070    /** Get the name of the type. */
071    @Override
072    public String getName()
073    {
074        return "Oracle";
075    }
076
077    /** Rename a column. */
078    @Override
079    public void renameColumn(String oldName, Column newColumn, String tableName) throws SQLException
080    {
081        String newName = getColumnName(newColumn.getName());
082        String sqlStr = "ALTER TABLE " + getTableName(tableName) +
083        " RENAME COLUMN " + getColumnName(oldName) + " TO " + newName;
084        _executeSQL(sqlStr);
085    }
086
087    /** Change a column to allow null values. */
088    @Override
089    public void setColumnNull(Column column, String tableName) throws SQLException
090    {
091        String sqlStr = "ALTER TABLE " + getTableName(tableName) +
092        " MODIFY (" + getColumnName(column.getName()) + " NULL)";
093        _executeSQL(sqlStr);
094    }
095    
096    /** Set not null constraint to a column. */
097    @Override
098    public void setColumnNotNull(Column column, String tableName) throws SQLException
099    {
100        String sqlStr = "ALTER TABLE " + getTableName(tableName) +
101        " MODIFY (" + getColumnName(column.getName()) + " NOT NULL)";
102        _executeSQL(sqlStr);
103    }
104
105    ///////////////////////////////////////////////////////////////////
106    // protected methods
107    
108    /** Returns true if foreign keys are automatically indexed. */
109    @Override
110    protected boolean _areForeignKeysIndexed()
111    {
112        return false;
113    }
114
115    /** Returns true if primary keys are automatically indexed. */
116    @Override
117    protected boolean _arePrimaryKeysIndexed()
118    {
119        return false;
120    }
121
122    /** Get the driver class name. */
123    @Override
124    protected String _getDriverName()
125    {
126        return "oracle.jdbc.driver.OracleDriver";
127    }
128    
129    /** Get a JDBC URL. */
130    @Override
131    protected String _getJDBCUrl(String hostName, String port,
132            String databaseName) throws SQLException
133    {
134        String hostAndPort = _combineHostAndPort(hostName, port);
135        return "jdbc:oracle:thin:@" + hostAndPort + ":" + databaseName;
136    }
137
138    /** Get the SQL string of a column type. */
139    @Override
140    protected String _getTypeString(Column column)
141    {
142        String retval = null;
143
144        switch(column.getType())
145        {
146            case Boolean:
147                retval = "number(1)";
148                break;
149            case Blob:
150                retval = "blob";
151                break;
152            case TextBlob:
153                retval = "clob";
154                break;
155            case Integer:
156                retval = "int";
157                break;
158            case Timestamp:
159                retval = "timestamp";
160                break;
161            case Varchar:
162                retval = "varchar";
163                break;
164        }
165
166        return retval;
167    }
168
169    /** Returns true if database supports auto-generated keys in its prepared
170     *  statements.
171     */
172    @Override
173    protected boolean _hasGeneratedKeys()
174    {
175        return false;
176    }
177    
178    /** Returns true if identifier is too long. */
179    @Override
180    protected boolean _isIdentifierTooLong(String identifier)
181    {
182        return identifier.length() > 30;
183    }
184
185    /** Returns true if column names should be capitalized. */
186    @Override
187    protected boolean _needCapitalColumnNames()
188    {
189        return true;
190    }
191
192    /** Returns true if table names should be capitalized. */
193    @Override
194    protected boolean _needCapitalTableNames()
195    {
196        return true;
197    }
198
199    /** Returns true if need to use sequences for autoincrement columns. */
200    @Override
201    protected boolean _needSequencesForAutoInc()
202    {
203        return true;
204    }
205}