Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b00a56aa3643b7d7e3668119eddbf98efe4249ad (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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * 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
 **************************************************************************/
package org.eclipse.net4j.internal.db;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBTable;
import org.eclipse.net4j.internal.db.bundle.OM;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Eike Stepper
 */
public abstract class DBAdapter implements IDBAdapter
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_SQL, DBAdapter.class);

  private String name;

  private String version;

  public DBAdapter(String name, String version)
  {
    this.name = name;
    this.version = version;
  }

  public String getName()
  {
    return name;
  }

  public String getVersion()
  {
    return version;
  }

  public void createTables(IDBTable[] tables, Connection connection) throws DBException
  {
    Statement statement = null;

    try
    {
      statement = connection.createStatement();
      for (IDBTable table : tables)
      {
        createTable(table, statement);
      }
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
    finally
    {
      DBUtil.close(statement);
    }
  }

  public void createTable(IDBTable table, Statement statement) throws DBException
  {
    try
    {
      doCreateTable((DBTable)table, statement);
    }
    catch (SQLException ex)
    {
      if (TRACER.isEnabled())
      {
        TRACER.trace("-- " + ex.getMessage());
      }
    }

    validateTable((DBTable)table, statement);
  }

  @Override
  public String toString()
  {
    return getName() + "-" + getVersion();
  }

  protected void doCreateTable(DBTable table, Statement statement) throws SQLException
  {
    StringBuilder builder = new StringBuilder();
    builder.append("CREATE TABLE ");
    builder.append(table.getName());
    builder.append(" (");
    table.appendFieldDefs(builder, createFieldDefinitions(table));
    String constraints = createConstraints(table);
    if (constraints != null)
    {
      builder.append(", ");
      builder.append(constraints);
    }

    builder.append(")");
    String sql = builder.toString();
    if (TRACER.isEnabled())
    {
      TRACER.trace(sql);
    }

    statement.execute(sql);
  }

  protected String createConstraints(DBTable table)
  {
    return null;
  }

  protected String createFieldDefinition(DBField field)
  {
    return getTypeName(field) + (field.isNotNull() ? " NOT NULL" : "");
  }

  protected String getTypeName(DBField field)
  {
    DBType type = field.getType();
    switch (type)
    {
    case BOOLEAN:
    case BIT:
    case TINYINT:
    case SMALLINT:
    case INTEGER:
    case BIGINT:
    case FLOAT:
    case REAL:
    case DOUBLE:
    case DATE:
    case TIME:
    case TIMESTAMP:
    case LONGVARCHAR:
    case LONGVARBINARY:
    case BLOB:
    case CLOB:
      return type.toString();

    case CHAR:
    case VARCHAR:
    case BINARY:
    case VARBINARY:
      return type.toString() + field.formatPrecision();

    case NUMERIC:
    case DECIMAL:
      return type.toString() + field.formatPrecisionAndScale();
    }

    throw new IllegalArgumentException("Unknown type: " + type);
  }

  protected void validateTable(DBTable table, Statement statement) throws DBException
  {
    try
    {
      StringBuilder builder = new StringBuilder();
      builder.append("SELECT ");
      table.appendFieldNames(builder);
      builder.append(" FROM ");
      builder.append(table.getName());
      String sql = builder.toString();

      ResultSet resultSet = statement.executeQuery(sql);
      ResultSetMetaData metaData = resultSet.getMetaData();
      int columnCount = metaData.getColumnCount();
      if (columnCount != table.getFieldCount())
      {
        throw new DBException("DBTable " + table.getName() + " has " + columnCount + " columns instead of "
            + table.getFieldCount());
      }

      // for (int i = 0; i < columnCount; i++)
      // {
      // int existingCode = metaData.getColumnType(i + 1);
      // DBField field = table.getField(i);
      // int code = field.getType().getCode();
      // if (code != existingCode)
      // {
      // throw new DBException("DBField " + field.getFullName() + " has type " +
      // existingCode + " instead of " + code
      // + " (" + field.getType() + ")");
      // }
      // }
    }
    catch (SQLException ex)
    {
      throw new DBException(ex.getMessage());
    }
  }

  private String[] createFieldDefinitions(DBTable table)
  {
    DBField[] fields = table.getFields();
    int fieldCount = fields.length;

    String[] result = new String[fieldCount];
    for (int i = 0; i < fieldCount; i++)
    {
      DBField field = fields[i];
      result[i] = createFieldDefinition(field);
    }

    return result;
  }
}

Back to the top