Beispiel #1
0
  @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);
  }
  /** @author Eike Stepper */
  private static final class LegacyProxyInvocationHandler
      implements InvocationHandler, LegacyProxy {
    private static final Method getIDMethod =
        ReflectUtil.getMethod(LegacyProxy.class, "getID"); // $NON-NLS-1$

    private static final Method eIsProxyMethod =
        ReflectUtil.getMethod(EObject.class, "eIsProxy"); // $NON-NLS-1$

    private static final Method eProxyURIMethod =
        ReflectUtil.getMethod(InternalEObject.class, "eProxyURI"); // $NON-NLS-1$

    private CDOLegacyWrapper wrapper;

    private CDOID id;

    public LegacyProxyInvocationHandler(CDOLegacyWrapper wrapper, CDOID id) {
      this.wrapper = wrapper;
      this.id = id;
    }

    public CDOID getID() {
      return id;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      if (method.equals(getIDMethod)) {
        return id;
      }

      if (method.equals(eIsProxyMethod)) {
        return true;
      }

      if (method.equals(eProxyURIMethod)) {
        // Use container's resource because it's guaranteed to be in the same CDOView as the
        // resource of the target!
        Resource resource = wrapper.eResource();

        // TODO Consider using a "fake" Resource implementation. See Resource.getEObject(...)
        return resource.getURI().appendFragment(id.toURIFragment());
      }

      // A client must have invoked the proxy while being told not to do so!
      throw new UnsupportedOperationException(method.getName());
    }
  }
  protected static int getEFlagMask(Class<?> instanceClass, String flagName) {
    Field field = ReflectUtil.getField(instanceClass, flagName);
    if (!field.isAccessible()) {
      field.setAccessible(true);
    }

    try {
      return (Integer) field.get(null);
    } catch (IllegalAccessException ex) {
      throw WrappedException.wrap(ex);
    }
  }
Beispiel #4
0
/**
 * 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 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);
  }
}