Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c83ace9eb3d3fc935af0ae28e3942f7bc6e23918 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (c) 2012, 2013, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *    Erdal Karaca - copied from mysql impl to adapt to oracle db
 */
package org.eclipse.net4j.db.oracle;

import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.spi.db.DBAdapter;
import org.eclipse.net4j.util.security.IUserAware;

import javax.sql.DataSource;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import oracle.jdbc.pool.OracleDataSource;

public class OracleAdapter extends DBAdapter
{
  public static final String NAME = "oracle"; //$NON-NLS-1$

  public static final String VERSION = "11.1.0.7"; //$NON-NLS-1$

  public OracleAdapter()
  {
    super(NAME, VERSION);
  }

  @Override
  public IDBConnectionProvider createConnectionProvider(DataSource dataSource)
  {
    if (dataSource instanceof OracleDataSource)
    {
      return DBUtil.createConnectionProvider(dataSource, ((OracleDataSource)dataSource).getUser());
    }

    return super.createConnectionProvider(dataSource);
  }

  public String[] getReservedWords()
  {
    List<String> list = new ArrayList<String>(Arrays.asList(getSQL92ReservedWords()));
    list.add("INDEX");
    list.add("COMMENT");
    list.add("ACCESS");
    return list.toArray(new String[list.size()]);
  }

  @Override
  public boolean isTypeIndexable(DBType type)
  {
    switch (type)
    {
    case VARCHAR:
      return false;

    default:
      return super.isTypeIndexable(type);
    }
  }

  @Override
  protected String getTypeName(IDBField field)
  {
    DBType type = field.getType();
    switch (type)
    {
    case NUMERIC:
    case DECIMAL:
    case FLOAT:
    case REAL:
    case DOUBLE:
    case BIGINT:
      return "NUMBER";
    case TINYINT:
      return "NUMBER(5)";
    case SMALLINT:
    case BOOLEAN:
    case BIT:
      return "NUMBER(7)";
    case INTEGER:
      return "NUMBER(12)";
    case DATE:
    case TIME:
      return "DATE";
    case CHAR:
    case VARCHAR:
      return "VARCHAR2(" + field.getPrecision() + " CHAR)";
    case LONGVARCHAR:
      return "LONG";
    case BINARY:
    case VARBINARY:
    case LONGVARBINARY:
      return "LONG RAW";
    default:
      return super.getTypeName(field);
    }
  }

  @Override
  public int getFieldLength(DBType type)
  {
    if (type == DBType.VARCHAR)
    {
      return 4000; // Oracle only supports 4000 for VARCHAR
    }

    return super.getFieldLength(type);
  }

  @Override
  public DBType adaptType(DBType type)
  {
    if (type == DBType.BOOLEAN)
    {
      return DBType.SMALLINT;
    }

    return super.adaptType(type);
  }

  @Override
  public int getMaxTableNameLength()
  {
    return 30;
  }

  @Override
  public int getMaxFieldNameLength()
  {
    return 30;
  }

  @Override
  public boolean isTableNotFoundException(SQLException ex)
  {
    String message = ex.getMessage();
    return message != null && message.toLowerCase().contains("ora-00942") && "42000".equals(ex.getSQLState());
  }

  @Override
  public boolean isColumnNotFoundException(SQLException ex)
  {
    String message = ex.getMessage();
    return message != null && message.toLowerCase().contains("ora-00904") && "42000".equals(ex.getSQLState());
  }

  @Override
  public boolean isDuplicateKeyException(SQLException ex)
  {
    String message = ex.getMessage();
    return message != null && message.toLowerCase().contains("ora-00001") && "23000".equals(ex.getSQLState());
  }

  @Override
  protected String sqlModifyField(String tableName, String fieldName, String definition)
  {
    return "ALTER TABLE " + tableName + " MODIFY " + fieldName + " " + definition;
  }

  @Override
  protected ResultSet readTables(Connection connection, DatabaseMetaData metaData, String schemaName)
      throws SQLException
  {
    if (schemaName == null && connection instanceof IUserAware)
    {
      schemaName = ((IUserAware)connection).getUserID();
    }

    return metaData.getTables(null, schemaName, null, new String[] { "TABLE" });
  }

  @Override
  public String convertString(PreparedStatement preparedStatement, int parameterIndex, String value)
  {
    if (value != null && value.length() == 0)
    {
      String replacement = getEmptyStringReplacement();
      if (replacement != null)
      {
        value = replacement;
      }
    }

    return super.convertString(preparedStatement, parameterIndex, value);
  }

  @Override
  public String convertString(ResultSet resultSet, int columnIndex, String value)
  {
    value = super.convertString(resultSet, columnIndex, value);

    if (value != null)
    {
      String replacement = getEmptyStringReplacement();
      if (replacement != null && replacement.equals(value))
      {
        value = "";
      }
    }

    return value;
  }

  @Override
  public String convertString(ResultSet resultSet, String columnLabel, String value)
  {
    value = super.convertString(resultSet, columnLabel, value);

    if (value != null)
    {
      String replacement = getEmptyStringReplacement();
      if (replacement != null && replacement.equals(value))
      {
        value = null;
      }
    }

    return value;
  }

  /**
   * @since 1.1
   */
  protected String getEmptyStringReplacement()
  {
    return "<empty>";
  }
}

Back to the top