Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 032b0b84666f6d373dca660e6155e01143b84bbc (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
/*
 * 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
 */
package org.eclipse.net4j.db.h2;

import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.spi.db.DBAdapter;
import org.eclipse.net4j.util.ReflectUtil;

import org.h2.command.CommandInterface;
import org.h2.expression.ParameterInterface;
import org.h2.jdbc.JdbcPreparedStatement;
import org.h2.jdbcx.JdbcDataSource;
import org.h2.util.ObjectArray;
import org.h2.value.Value;

import javax.sql.DataSource;

import java.lang.reflect.Field;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * A {@link IDBAdapter DB adapter} for <a href="http://www.h2database.com/html/main.html">H2</a> databases.
 *
 * @author Eike Stepper
 * @since 2.0
 */
public class H2Adapter extends DBAdapter
{
  private static final String NAME = "h2"; //$NON-NLS-1$

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

  private static final Field SQL_FIELD = ReflectUtil.getField(JdbcPreparedStatement.class, "sql");

  private static final Field COMMAND_FIELD = ReflectUtil.getField(JdbcPreparedStatement.class, "command");

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

  public Driver getJDBCDriver()
  {
    return new org.h2.Driver();
  }

  public DataSource createJDBCDataSource()
  {
    return new JdbcDataSource();
  }

  @Override
  protected String getTypeName(IDBField field)
  {
    DBType type = field.getType();
    switch (type)
    {
    case BIT:
      return "SMALLINT"; //$NON-NLS-1$

    case FLOAT:
      return "REAL"; //$NON-NLS-1$

    case LONGVARCHAR:
      return "VARCHAR"; //$NON-NLS-1$

    case NUMERIC:
      return "DECIMAL"; //$NON-NLS-1$

    case LONGVARBINARY:
    case VARBINARY:
      return "BLOB"; //$NON-NLS-1$
    }

    return super.getTypeName(field);
  }

  public String[] getReservedWords()
  {
    return getSQL92ReservedWords();
  }

  @Override
  public boolean isDuplicateKeyException(SQLException ex)
  {
    String sqlState = ex.getSQLState();
    return "23001".equals(sqlState) || "23505".equals(sqlState);
  }

  @Override
  public String sqlRenameField(IDBField field, String oldName)
  {
    return "ALTER TABLE " + field.getTable() + " ALTER COLUMN " + oldName + " RENAME TO " + field;
  }

  /**
   * @since 4.1
   */
  @Override
  public String format(PreparedStatement stmt)
  {
    try
    {
      if (stmt instanceof JdbcPreparedStatement)
      {
        String sql = (String)ReflectUtil.getValue(SQL_FIELD, stmt);

        boolean insert = false;
        if (sql.toUpperCase().startsWith("INSERT INTO"))
        {
          int pos = sql.indexOf("(");
          sql = sql.substring(0, pos) + "SET " + sql.substring(pos + 1);
          sql = sql.substring(0, sql.indexOf(" VALUES"));
          insert = true;
        }

        CommandInterface command = (CommandInterface)ReflectUtil.getValue(COMMAND_FIELD, stmt);
        ObjectArray<? extends ParameterInterface> parameters = command.getParameters();

        int pos = 0;
        for (int i = 0; i < parameters.size(); i++)
        {
          ParameterInterface parameter = parameters.get(i);
          Value value = parameter.getParamValue();

          if (insert)
          {
            String string = "=" + value;

            pos = sql.indexOf(',', pos);
            if (pos == -1)
            {
              pos = sql.indexOf(')');
              sql = sql.substring(0, pos) + string;
              break;
            }

            sql = sql.substring(0, pos) + string + sql.substring(pos);
            pos += string.length() + 1;
          }
          else
          {
            pos = sql.indexOf('?');
            sql = sql.substring(0, pos) + value + sql.substring(pos + 1);
          }
        }

        return sql;
      }
    }
    catch (Throwable t)
    {
      //$FALL-THROUGH$
    }

    return super.format(stmt);
  }
}

Back to the top