Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b05c4ba3aa0822079f97187d55dc84a3041c33a (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
/*
 * Copyright (c) 2004 - 2012 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.DBException;
import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBIndex;
import org.eclipse.net4j.db.ddl.IDBTable;
import org.eclipse.net4j.db.oracle.internal.bundle.OM;
import org.eclipse.net4j.spi.db.DBAdapter;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import javax.sql.DataSource;

import java.sql.Driver;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OracleAdapter extends DBAdapter
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_SQL, DBAdapter.class);

  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);
  }

  public Driver getJDBCDriver()
  {
    return new oracle.jdbc.driver.OracleDriver();
  }

  public DataSource createJDBCDataSource()
  {
    try
    {
      return new oracle.jdbc.pool.OracleDataSource();
    }
    catch (SQLException e)
    {
      throw new DBException(e);
    }
  }

  @Override
  protected void createIndex(IDBIndex index, Statement statement, int num) throws SQLException
  {
    IDBTable table = index.getTable();
    StringBuilder builder = new StringBuilder();
    builder.append("CREATE "); //$NON-NLS-1$
    if (index.getType() == IDBIndex.Type.UNIQUE || index.getType() == IDBIndex.Type.PRIMARY_KEY)
    {
      builder.append("UNIQUE "); //$NON-NLS-1$
    }

    builder.append("INDEX I"); //$NON-NLS-1$
    builder.append(System.currentTimeMillis());

    try
    {
      Thread.sleep(1);
    }
    catch (InterruptedException ex)
    {
      OM.LOG.error(ex);
      return;
    }

    builder.append("_");
    builder.append(num);
    builder.append(" ON "); //$NON-NLS-1$
    builder.append(table);
    builder.append(" ("); //$NON-NLS-1$
    IDBField[] fields = index.getFields();
    for (int i = 0; i < fields.length; i++)
    {
      if (i != 0)
      {
        builder.append(", "); //$NON-NLS-1$
      }

      addIndexField(builder, fields[i]);
    }

    builder.append(")"); //$NON-NLS-1$
    String sql = builder.toString();
    if (TRACER.isEnabled())
    {
      TRACER.trace(sql);
    }

    statement.execute(sql);
  }

  public String[] getReservedWords()
  {
    List<String> list = new ArrayList<String>(Arrays.asList(getSQL92ReservedWords()));
    list.add("INDEX");
    list.add("COMMENT");
    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:
      return "NUMBER(7)";
    case INTEGER:
      return "NUMBER(12)";
    case DATE:
    case TIME:
      return "DATE";
    case CHAR:
    case VARCHAR:
      return "VARCHAR2(" + field.getPrecision() + ")";
    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());
  }
}

Back to the top