From 1460904fd3c193ffaf809913b2983a400fce9d94 Mon Sep 17 00:00:00 2001 From: Eike Stepper Date: Thu, 19 Jul 2012 10:22:04 +0200 Subject: Fix line endings in master (dos2unix) --- .../src/org/eclipse/net4j/db/DBException.java | 162 +- .../src/org/eclipse/net4j/db/DBType.java | 2138 ++++++++++---------- .../src/org/eclipse/net4j/db/IDBAdapter.java | 180 +- .../eclipse/net4j/db/IDBConnectionProvider.java | 56 +- .../src/org/eclipse/net4j/db/IDBRowHandler.java | 44 +- .../src/org/eclipse/net4j/db/ddl/IDBField.java | 106 +- .../src/org/eclipse/net4j/db/ddl/IDBIndex.java | 88 +- .../src/org/eclipse/net4j/db/ddl/IDBSchema.java | 118 +- .../org/eclipse/net4j/db/ddl/IDBSchemaElement.java | 62 +- .../src/org/eclipse/net4j/db/ddl/IDBTable.java | 106 +- .../src/org/eclipse/net4j/db/ddl/package-info.java | 30 +- .../src/org/eclipse/net4j/db/dml/IDBParameter.java | 66 +- .../src/org/eclipse/net4j/db/dml/IDBStatement.java | 78 +- .../src/org/eclipse/net4j/db/dml/package-info.java | 30 +- .../src/org/eclipse/net4j/db/package-info.java | 30 +- .../net4j/internal/db/DBAdapterDescriptor.java | 66 +- .../net4j/internal/db/DBAdapterRegistry.java | 158 +- .../internal/db/DataSourceConnectionProvider.java | 110 +- .../org/eclipse/net4j/internal/db/bundle/OM.java | 184 +- .../org/eclipse/net4j/internal/db/ddl/DBField.java | 334 +-- .../org/eclipse/net4j/internal/db/ddl/DBIndex.java | 164 +- .../net4j/internal/db/ddl/DBSchemaElement.java | 88 +- .../org/eclipse/net4j/internal/db/ddl/DBTable.java | 350 ++-- .../eclipse/net4j/internal/db/dml/DBParameter.java | 98 +- .../eclipse/net4j/internal/db/dml/DBStatement.java | 212 +- .../src/org/eclipse/net4j/spi/db/DBAdapter.java | 1098 +++++----- .../src/org/eclipse/net4j/spi/db/DBSchema.java | 468 ++--- .../src/org/eclipse/net4j/spi/db/package-info.java | 30 +- 28 files changed, 3327 insertions(+), 3327 deletions(-) (limited to 'plugins/org.eclipse.net4j.db/src') diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBException.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBException.java index 940b1859bb..08d22adaa6 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBException.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBException.java @@ -1,81 +1,81 @@ -/* - * 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; - -import org.eclipse.net4j.util.StringUtil; - -import java.sql.SQLException; - -/** - * A {@link RuntimeException runtime exception} thrown to indicate problems with a database, frequently used to wrap - * checked {@link SQLException SQL exceptions}. - * - * @author Eike Stepper - */ -public class DBException extends RuntimeException -{ - private static final long serialVersionUID = 1L; - - public DBException() - { - } - - public DBException(String message) - { - super(message); - } - - public DBException(Throwable cause) - { - super(cause); - } - - public DBException(String message, Throwable cause) - { - super(message, cause); - } - - /** - * @since 4.0 - */ - public DBException(Throwable cause, String sql) - { - super(format(null, sql), cause); - } - - /** - * @since 4.0 - */ - public DBException(String message, Throwable cause, String sql) - { - super(format(message, sql), cause); - } - - private static String format(String message, String sql) - { - if (StringUtil.isEmpty(message)) - { - if (StringUtil.isEmpty(sql)) - { - return ""; - } - - return sql; - } - - if (StringUtil.isEmpty(sql)) - { - return message; - } - - return message + ": " + sql; - } -} +/* + * 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; + +import org.eclipse.net4j.util.StringUtil; + +import java.sql.SQLException; + +/** + * A {@link RuntimeException runtime exception} thrown to indicate problems with a database, frequently used to wrap + * checked {@link SQLException SQL exceptions}. + * + * @author Eike Stepper + */ +public class DBException extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public DBException() + { + } + + public DBException(String message) + { + super(message); + } + + public DBException(Throwable cause) + { + super(cause); + } + + public DBException(String message, Throwable cause) + { + super(message, cause); + } + + /** + * @since 4.0 + */ + public DBException(Throwable cause, String sql) + { + super(format(null, sql), cause); + } + + /** + * @since 4.0 + */ + public DBException(String message, Throwable cause, String sql) + { + super(format(message, sql), cause); + } + + private static String format(String message, String sql) + { + if (StringUtil.isEmpty(message)) + { + if (StringUtil.isEmpty(sql)) + { + return ""; + } + + return sql; + } + + if (StringUtil.isEmpty(sql)) + { + return message; + } + + return message + ": " + sql; + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java index 2e4ef12037..878a94190f 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBType.java @@ -1,1069 +1,1069 @@ -/* - * 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 - * Kai Schlamp - bug 282976: [DB] Influence Mappings through EAnnotations - */ -package org.eclipse.net4j.db; - -import org.eclipse.net4j.util.io.ExtendedDataInput; -import org.eclipse.net4j.util.io.ExtendedDataOutput; -import org.eclipse.net4j.util.io.IOUtil; -import org.eclipse.net4j.util.io.TMPUtil; - -import java.io.ByteArrayInputStream; -import java.io.EOFException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * Enumerates the SQL data types that are compatible with the DB framework. - * - * @author Eike Stepper - * @noextend This interface is not intended to be extended by clients. - */ -public enum DBType -{ - BOOLEAN(16) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - return writeValueBoolean(out, resultSet, column, canBeNull); - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - return readValueBoolean(in, statement, column, canBeNull, getCode()); - } - }, - - BIT(-7) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - return writeValueBoolean(out, resultSet, column, canBeNull); - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - return readValueBoolean(in, statement, column, canBeNull, getCode()); - } - }, - - TINYINT(-6) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - byte value = resultSet.getByte(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeByte(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - byte value = in.readByte(); - statement.setByte(column, value); - return value; - } - }, - - SMALLINT(5) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - short value = resultSet.getShort(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeShort(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - short value = in.readShort(); - statement.setShort(column, value); - return value; - } - }, - - INTEGER(4) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - int value = resultSet.getInt(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeInt(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - int value = in.readInt(); - statement.setInt(column, value); - return value; - } - }, - - BIGINT(-5) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - long value = resultSet.getLong(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeLong(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - long value = in.readLong(); - statement.setLong(column, value); - return value; - } - }, - - FLOAT(6) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - float value = resultSet.getFloat(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeFloat(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - float value = in.readFloat(); - statement.setFloat(column, value); - return value; - } - }, - - REAL(7) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - float value = resultSet.getFloat(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeFloat(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - float value = in.readFloat(); - statement.setFloat(column, value); - return value; - } - }, - - DOUBLE(8) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - double value = resultSet.getDouble(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeDouble(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - double value = in.readDouble(); - statement.setDouble(column, value); - return value; - } - }, - - NUMERIC(2) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - throw new UnsupportedOperationException("SQL NULL has to be considered"); - // BigDecimal value = resultSet.getBigDecimal(column); - // BigInteger valueUnscaled = value.unscaledValue(); - // - // byte[] byteArray = valueUnscaled.toByteArray(); - // out.writeInt(byteArray.length); - // out.write(byteArray); - // out.writeInt(value.scale()); - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - throw new UnsupportedOperationException("SQL NULL has to be considered"); - // byte[] bytes = in.readByteArray(); - // int scale = in.readInt(); - // BigInteger valueUnscaled = new BigInteger(bytes); - // BigDecimal value = new BigDecimal(valueUnscaled, scale); - // - // // TODO: Read out the precision, scale information and bring the big decimal to the correct form. - // statement.setBigDecimal(column, value); - } - }, - - DECIMAL(3) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - throw new UnsupportedOperationException("SQL NULL has to be considered"); - // BigDecimal value = resultSet.getBigDecimal(column); - // BigInteger valueUnscaled = value.unscaledValue(); - // - // byte[] byteArray = valueUnscaled.toByteArray(); - // out.writeInt(byteArray.length); - // out.write(byteArray); - // out.writeInt(value.scale()); - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - throw new UnsupportedOperationException("SQL NULL has to be considered"); - // byte[] bytes = in.readByteArray(); - // int scale = in.readInt(); - // - // BigInteger valueUnscaled = new BigInteger(bytes); - // BigDecimal value = new BigDecimal(valueUnscaled, scale); - // statement.setBigDecimal(column, value); - } - }, - - CHAR(1) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - String value = resultSet.getString(column); - out.writeString(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - String value = in.readString(); - statement.setString(column, value); - return value; - } - }, - - VARCHAR(12) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - String value = resultSet.getString(column); - out.writeString(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - String value = in.readString(); - statement.setString(column, value); - return value; - } - }, - - LONGVARCHAR(-1, "LONG VARCHAR") //$NON-NLS-1$ - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - String value = resultSet.getString(column); - out.writeString(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - String value = in.readString(); - statement.setString(column, value); - return value; - } - }, - - CLOB(2005) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - Clob value = resultSet.getClob(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - long length = value.length(); - Reader reader = value.getCharacterStream(); - - try - { - out.writeLong(length); - while (length-- > 0) - { - int c = reader.read(); - out.writeChar(c); - } - } - finally - { - IOUtil.close(reader); - } - - return null; - } - - @Override - public Object readValueWithResult(final ExtendedDataInput in, PreparedStatement statement, int column, - boolean canBeNull) throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - Reader reader; - - long length = in.readLong(); - if (length > 0) - { - reader = createFileReader(in, length); - } - else - { - reader = new Reader() - { - @Override - public int read(char[] cbuf, int off, int len) throws IOException - { - return -1; - } - - @Override - public void close() throws IOException - { - // Do nothing - } - }; - } - - statement.setCharacterStream(column, reader, (int)length); - // reader.close(); - return null; - } - - private FileReader createFileReader(final ExtendedDataInput in, long length) throws IOException - { - FileWriter fw = null; - - try - { - final File tempFile = TMPUtil.createTempFile("lob-", ".tmp"); - tempFile.deleteOnExit(); - - fw = new FileWriter(tempFile); - - Reader reader = new Reader() - { - @Override - public int read(char[] cbuf, int off, int len) throws IOException - { - int read = 0; - - try - { - while (read < len) - { - cbuf[off++] = in.readChar(); - read++; - } - } - catch (EOFException ex) - { - read = -1; - } - - return read; - } - - @Override - public void close() throws IOException - { - } - }; - - IOUtil.copyCharacter(reader, fw, length); - - return new FileReader(tempFile) - { - @Override - public void close() throws IOException - { - super.close(); - tempFile.delete(); - } - }; - } - finally - { - IOUtil.close(fw); - } - } - }, - - DATE(91) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - java.sql.Date value = resultSet.getDate(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - long longValue = value.getTime(); - out.writeLong(longValue); - return longValue; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - long value = in.readLong(); - statement.setDate(column, new java.sql.Date(value)); - return value; - } - }, - - TIME(92) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - java.sql.Time value = resultSet.getTime(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - long longValue = value.getTime(); - out.writeLong(longValue); - return longValue; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - long value = in.readLong(); - statement.setTime(column, new java.sql.Time(value)); - return value; - } - }, - - TIMESTAMP(93) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - java.sql.Timestamp value = resultSet.getTimestamp(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeLong(value.getTime()); - out.writeInt(value.getNanos()); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - long value = in.readLong(); - int nanos = in.readInt(); - java.sql.Timestamp timeStamp = new java.sql.Timestamp(value); - timeStamp.setNanos(nanos); - statement.setTimestamp(column, timeStamp); - return timeStamp; - } - }, - - BINARY(-2) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - byte[] value = resultSet.getBytes(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeInt(value.length); - out.write(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - byte[] value = in.readByteArray(); - statement.setBytes(column, value); - return value; - } - }, - - VARBINARY(-3) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - byte[] value = resultSet.getBytes(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeInt(value.length); - out.write(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - byte[] value = in.readByteArray(); - statement.setBytes(column, value); - return value; - } - }, - - LONGVARBINARY(-4, "LONG VARBINARY") //$NON-NLS-1$ - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - byte[] value = resultSet.getBytes(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - out.writeInt(value.length); - out.write(value); - return value; - } - - @Override - public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - byte[] value = in.readByteArray(); - statement.setBytes(column, value); - return value; - } - }, - - BLOB(2004) - { - @Override - public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - Blob value = resultSet.getBlob(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeBoolean(false); - return null; - } - - out.writeBoolean(true); - } - - long length = value.length(); - InputStream stream = value.getBinaryStream(); - - try - { - out.writeLong(length); - IOUtil.copyBinary(stream, new ExtendedDataOutput.Stream(out), length); - } - finally - { - IOUtil.close(stream); - } - - return null; - } - - @Override - public Object readValueWithResult(final ExtendedDataInput in, PreparedStatement statement, int column, - boolean canBeNull) throws SQLException, IOException - { - if (canBeNull && !in.readBoolean()) - { - statement.setNull(column, getCode()); - return null; - } - - long length = in.readLong(); - InputStream value = null; - - if (length > 0) - { - value = createFileInputStream(in, length); - } - else - { - value = new ByteArrayInputStream(new byte[0]); - } - - statement.setBinaryStream(column, value, (int)length); - - // XXX cannot close the input stream here, because - // it is still used in executeBatch() later. - // so maybe we could return it here and let the caller - // collect and close the streams. - return null; - } - - private FileInputStream createFileInputStream(final ExtendedDataInput in, long length) throws IOException - { - FileOutputStream fos = null; - - try - { - final File tempFile = TMPUtil.createTempFile("lob-", ".tmp"); - tempFile.deleteOnExit(); - - fos = new FileOutputStream(tempFile); - IOUtil.copyBinary(new ExtendedDataInput.Stream(in), fos, length); - - return new FileInputStream(tempFile) - { - @Override - public void close() throws IOException - { - super.close(); - tempFile.delete(); - } - }; - } - finally - { - IOUtil.close(fos); - } - } - }; - - private static final int BOOLEAN_NULL = -1; - - private static final int BOOLEAN_FALSE = 0; - - private static final int BOOLEAN_TRUE = 1; - - private int code; - - private String keyword; - - private DBType(int code, String keyword) - { - this.code = code; - this.keyword = keyword; - } - - private DBType(int code) - { - this(code, null); - } - - public int getCode() - { - return code; - } - - public String getKeyword() - { - return keyword == null ? super.toString() : keyword; - } - - @Override - public String toString() - { - return getKeyword(); - } - - /** - * @since 3.0 - */ - public void writeValue(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - writeValueWithResult(out, resultSet, column, canBeNull); - } - - /** - * @since 4.1 - */ - public abstract Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException; - - /** - * @since 3.0 - */ - public void readValue(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) - throws SQLException, IOException - { - readValueWithResult(in, statement, column, canBeNull); - } - - /** - * @since 4.1 - */ - public abstract Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, - boolean canBeNull) throws SQLException, IOException; - - private static Boolean writeValueBoolean(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) - throws SQLException, IOException - { - boolean value = resultSet.getBoolean(column); - if (canBeNull) - { - if (resultSet.wasNull()) - { - out.writeByte(BOOLEAN_NULL); - return null; - } - - out.writeByte(value ? BOOLEAN_TRUE : BOOLEAN_FALSE); - return value; - } - - out.writeBoolean(value); - return value; - } - - private static Boolean readValueBoolean(ExtendedDataInput in, PreparedStatement statement, int column, - boolean canBeNull, int sqlType) throws IOException, SQLException - { - if (canBeNull) - { - byte opcode = in.readByte(); - switch (opcode) - { - case BOOLEAN_NULL: - statement.setNull(column, sqlType); - return null; - - case BOOLEAN_FALSE: - statement.setBoolean(column, false); - return false; - - case BOOLEAN_TRUE: - statement.setBoolean(column, true); - return true; - - default: - throw new IOException("Invalid boolean opcode: " + opcode); - } - } - - boolean value = in.readBoolean(); - statement.setBoolean(column, value); - return value; - } - - /** - * @since 3.0 - */ - public static DBType getTypeByKeyword(String keyword) - { - DBType[] values = DBType.values(); - for (int i = 0; i < values.length; i++) - { - DBType dbType = values[i]; - if (dbType.getKeyword().equalsIgnoreCase(keyword)) - { - return dbType; - } - } - - return null; - } -} +/* + * 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 + * Kai Schlamp - bug 282976: [DB] Influence Mappings through EAnnotations + */ +package org.eclipse.net4j.db; + +import org.eclipse.net4j.util.io.ExtendedDataInput; +import org.eclipse.net4j.util.io.ExtendedDataOutput; +import org.eclipse.net4j.util.io.IOUtil; +import org.eclipse.net4j.util.io.TMPUtil; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Enumerates the SQL data types that are compatible with the DB framework. + * + * @author Eike Stepper + * @noextend This interface is not intended to be extended by clients. + */ +public enum DBType +{ + BOOLEAN(16) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + return writeValueBoolean(out, resultSet, column, canBeNull); + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + return readValueBoolean(in, statement, column, canBeNull, getCode()); + } + }, + + BIT(-7) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + return writeValueBoolean(out, resultSet, column, canBeNull); + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + return readValueBoolean(in, statement, column, canBeNull, getCode()); + } + }, + + TINYINT(-6) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + byte value = resultSet.getByte(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeByte(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + byte value = in.readByte(); + statement.setByte(column, value); + return value; + } + }, + + SMALLINT(5) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + short value = resultSet.getShort(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeShort(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + short value = in.readShort(); + statement.setShort(column, value); + return value; + } + }, + + INTEGER(4) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + int value = resultSet.getInt(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeInt(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + int value = in.readInt(); + statement.setInt(column, value); + return value; + } + }, + + BIGINT(-5) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + long value = resultSet.getLong(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeLong(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + long value = in.readLong(); + statement.setLong(column, value); + return value; + } + }, + + FLOAT(6) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + float value = resultSet.getFloat(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeFloat(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + float value = in.readFloat(); + statement.setFloat(column, value); + return value; + } + }, + + REAL(7) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + float value = resultSet.getFloat(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeFloat(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + float value = in.readFloat(); + statement.setFloat(column, value); + return value; + } + }, + + DOUBLE(8) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + double value = resultSet.getDouble(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeDouble(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + double value = in.readDouble(); + statement.setDouble(column, value); + return value; + } + }, + + NUMERIC(2) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + throw new UnsupportedOperationException("SQL NULL has to be considered"); + // BigDecimal value = resultSet.getBigDecimal(column); + // BigInteger valueUnscaled = value.unscaledValue(); + // + // byte[] byteArray = valueUnscaled.toByteArray(); + // out.writeInt(byteArray.length); + // out.write(byteArray); + // out.writeInt(value.scale()); + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + throw new UnsupportedOperationException("SQL NULL has to be considered"); + // byte[] bytes = in.readByteArray(); + // int scale = in.readInt(); + // BigInteger valueUnscaled = new BigInteger(bytes); + // BigDecimal value = new BigDecimal(valueUnscaled, scale); + // + // // TODO: Read out the precision, scale information and bring the big decimal to the correct form. + // statement.setBigDecimal(column, value); + } + }, + + DECIMAL(3) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + throw new UnsupportedOperationException("SQL NULL has to be considered"); + // BigDecimal value = resultSet.getBigDecimal(column); + // BigInteger valueUnscaled = value.unscaledValue(); + // + // byte[] byteArray = valueUnscaled.toByteArray(); + // out.writeInt(byteArray.length); + // out.write(byteArray); + // out.writeInt(value.scale()); + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + throw new UnsupportedOperationException("SQL NULL has to be considered"); + // byte[] bytes = in.readByteArray(); + // int scale = in.readInt(); + // + // BigInteger valueUnscaled = new BigInteger(bytes); + // BigDecimal value = new BigDecimal(valueUnscaled, scale); + // statement.setBigDecimal(column, value); + } + }, + + CHAR(1) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + String value = resultSet.getString(column); + out.writeString(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + String value = in.readString(); + statement.setString(column, value); + return value; + } + }, + + VARCHAR(12) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + String value = resultSet.getString(column); + out.writeString(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + String value = in.readString(); + statement.setString(column, value); + return value; + } + }, + + LONGVARCHAR(-1, "LONG VARCHAR") //$NON-NLS-1$ + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + String value = resultSet.getString(column); + out.writeString(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + String value = in.readString(); + statement.setString(column, value); + return value; + } + }, + + CLOB(2005) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + Clob value = resultSet.getClob(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + long length = value.length(); + Reader reader = value.getCharacterStream(); + + try + { + out.writeLong(length); + while (length-- > 0) + { + int c = reader.read(); + out.writeChar(c); + } + } + finally + { + IOUtil.close(reader); + } + + return null; + } + + @Override + public Object readValueWithResult(final ExtendedDataInput in, PreparedStatement statement, int column, + boolean canBeNull) throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + Reader reader; + + long length = in.readLong(); + if (length > 0) + { + reader = createFileReader(in, length); + } + else + { + reader = new Reader() + { + @Override + public int read(char[] cbuf, int off, int len) throws IOException + { + return -1; + } + + @Override + public void close() throws IOException + { + // Do nothing + } + }; + } + + statement.setCharacterStream(column, reader, (int)length); + // reader.close(); + return null; + } + + private FileReader createFileReader(final ExtendedDataInput in, long length) throws IOException + { + FileWriter fw = null; + + try + { + final File tempFile = TMPUtil.createTempFile("lob-", ".tmp"); + tempFile.deleteOnExit(); + + fw = new FileWriter(tempFile); + + Reader reader = new Reader() + { + @Override + public int read(char[] cbuf, int off, int len) throws IOException + { + int read = 0; + + try + { + while (read < len) + { + cbuf[off++] = in.readChar(); + read++; + } + } + catch (EOFException ex) + { + read = -1; + } + + return read; + } + + @Override + public void close() throws IOException + { + } + }; + + IOUtil.copyCharacter(reader, fw, length); + + return new FileReader(tempFile) + { + @Override + public void close() throws IOException + { + super.close(); + tempFile.delete(); + } + }; + } + finally + { + IOUtil.close(fw); + } + } + }, + + DATE(91) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + java.sql.Date value = resultSet.getDate(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + long longValue = value.getTime(); + out.writeLong(longValue); + return longValue; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + long value = in.readLong(); + statement.setDate(column, new java.sql.Date(value)); + return value; + } + }, + + TIME(92) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + java.sql.Time value = resultSet.getTime(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + long longValue = value.getTime(); + out.writeLong(longValue); + return longValue; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + long value = in.readLong(); + statement.setTime(column, new java.sql.Time(value)); + return value; + } + }, + + TIMESTAMP(93) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + java.sql.Timestamp value = resultSet.getTimestamp(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeLong(value.getTime()); + out.writeInt(value.getNanos()); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + long value = in.readLong(); + int nanos = in.readInt(); + java.sql.Timestamp timeStamp = new java.sql.Timestamp(value); + timeStamp.setNanos(nanos); + statement.setTimestamp(column, timeStamp); + return timeStamp; + } + }, + + BINARY(-2) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + byte[] value = resultSet.getBytes(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeInt(value.length); + out.write(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + byte[] value = in.readByteArray(); + statement.setBytes(column, value); + return value; + } + }, + + VARBINARY(-3) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + byte[] value = resultSet.getBytes(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeInt(value.length); + out.write(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + byte[] value = in.readByteArray(); + statement.setBytes(column, value); + return value; + } + }, + + LONGVARBINARY(-4, "LONG VARBINARY") //$NON-NLS-1$ + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + byte[] value = resultSet.getBytes(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + out.writeInt(value.length); + out.write(value); + return value; + } + + @Override + public Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + byte[] value = in.readByteArray(); + statement.setBytes(column, value); + return value; + } + }, + + BLOB(2004) + { + @Override + public Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + Blob value = resultSet.getBlob(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeBoolean(false); + return null; + } + + out.writeBoolean(true); + } + + long length = value.length(); + InputStream stream = value.getBinaryStream(); + + try + { + out.writeLong(length); + IOUtil.copyBinary(stream, new ExtendedDataOutput.Stream(out), length); + } + finally + { + IOUtil.close(stream); + } + + return null; + } + + @Override + public Object readValueWithResult(final ExtendedDataInput in, PreparedStatement statement, int column, + boolean canBeNull) throws SQLException, IOException + { + if (canBeNull && !in.readBoolean()) + { + statement.setNull(column, getCode()); + return null; + } + + long length = in.readLong(); + InputStream value = null; + + if (length > 0) + { + value = createFileInputStream(in, length); + } + else + { + value = new ByteArrayInputStream(new byte[0]); + } + + statement.setBinaryStream(column, value, (int)length); + + // XXX cannot close the input stream here, because + // it is still used in executeBatch() later. + // so maybe we could return it here and let the caller + // collect and close the streams. + return null; + } + + private FileInputStream createFileInputStream(final ExtendedDataInput in, long length) throws IOException + { + FileOutputStream fos = null; + + try + { + final File tempFile = TMPUtil.createTempFile("lob-", ".tmp"); + tempFile.deleteOnExit(); + + fos = new FileOutputStream(tempFile); + IOUtil.copyBinary(new ExtendedDataInput.Stream(in), fos, length); + + return new FileInputStream(tempFile) + { + @Override + public void close() throws IOException + { + super.close(); + tempFile.delete(); + } + }; + } + finally + { + IOUtil.close(fos); + } + } + }; + + private static final int BOOLEAN_NULL = -1; + + private static final int BOOLEAN_FALSE = 0; + + private static final int BOOLEAN_TRUE = 1; + + private int code; + + private String keyword; + + private DBType(int code, String keyword) + { + this.code = code; + this.keyword = keyword; + } + + private DBType(int code) + { + this(code, null); + } + + public int getCode() + { + return code; + } + + public String getKeyword() + { + return keyword == null ? super.toString() : keyword; + } + + @Override + public String toString() + { + return getKeyword(); + } + + /** + * @since 3.0 + */ + public void writeValue(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + writeValueWithResult(out, resultSet, column, canBeNull); + } + + /** + * @since 4.1 + */ + public abstract Object writeValueWithResult(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException; + + /** + * @since 3.0 + */ + public void readValue(ExtendedDataInput in, PreparedStatement statement, int column, boolean canBeNull) + throws SQLException, IOException + { + readValueWithResult(in, statement, column, canBeNull); + } + + /** + * @since 4.1 + */ + public abstract Object readValueWithResult(ExtendedDataInput in, PreparedStatement statement, int column, + boolean canBeNull) throws SQLException, IOException; + + private static Boolean writeValueBoolean(ExtendedDataOutput out, ResultSet resultSet, int column, boolean canBeNull) + throws SQLException, IOException + { + boolean value = resultSet.getBoolean(column); + if (canBeNull) + { + if (resultSet.wasNull()) + { + out.writeByte(BOOLEAN_NULL); + return null; + } + + out.writeByte(value ? BOOLEAN_TRUE : BOOLEAN_FALSE); + return value; + } + + out.writeBoolean(value); + return value; + } + + private static Boolean readValueBoolean(ExtendedDataInput in, PreparedStatement statement, int column, + boolean canBeNull, int sqlType) throws IOException, SQLException + { + if (canBeNull) + { + byte opcode = in.readByte(); + switch (opcode) + { + case BOOLEAN_NULL: + statement.setNull(column, sqlType); + return null; + + case BOOLEAN_FALSE: + statement.setBoolean(column, false); + return false; + + case BOOLEAN_TRUE: + statement.setBoolean(column, true); + return true; + + default: + throw new IOException("Invalid boolean opcode: " + opcode); + } + } + + boolean value = in.readBoolean(); + statement.setBoolean(column, value); + return value; + } + + /** + * @since 3.0 + */ + public static DBType getTypeByKeyword(String keyword) + { + DBType[] values = DBType.values(); + for (int i = 0; i < values.length; i++) + { + DBType dbType = values[i]; + if (dbType.getKeyword().equalsIgnoreCase(keyword)) + { + return dbType; + } + } + + return null; + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBAdapter.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBAdapter.java index 58f1c6b0b8..fe97aaacf0 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBAdapter.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBAdapter.java @@ -1,90 +1,90 @@ -/* - * 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 - * Stefan Winkler - bug 289445 - */ -package org.eclipse.net4j.db; - -import org.eclipse.net4j.db.ddl.IDBTable; -import org.eclipse.net4j.internal.db.DBAdapterRegistry; -import org.eclipse.net4j.util.registry.IRegistry; - -import javax.sql.DataSource; - -import java.sql.Connection; -import java.sql.Driver; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Collection; -import java.util.Set; - -/** - * Abstracts all aspects of a database that are vendor-specific. - * - * @author Eike Stepper - */ -public interface IDBAdapter -{ - public static final IRegistry REGISTRY = DBAdapterRegistry.INSTANCE; - - public String getName(); - - public String getVersion(); - - public Driver getJDBCDriver(); - - public DataSource createJDBCDataSource(); - - public Set createTables(Iterable tables, Connection connection) throws DBException; - - public boolean createTable(IDBTable table, Statement statement) throws DBException; - - public Collection dropTables(Iterable tables, Connection connection) throws DBException; - - public boolean dropTable(IDBTable table, Statement statement); - - public String[] getReservedWords(); - - public boolean isReservedWord(String word); - - /** - * @since 2.0 - */ - public int getMaxTableNameLength(); - - /** - * @since 2.0 - */ - public int getMaxFieldNameLength(); - - public boolean isTypeIndexable(DBType type); - - /** - * Provide a way for the DBAdapter to override unsupported DB types with replacements. The default implementation just - * returns the given type. Subclasses may override single types with replacements. - * - * @since 3.0 - */ - public DBType adaptType(DBType type); - - /** - * Check if a character is valid as first character. (e.g., underscores are forbidden as first character in Derby - * elements. - * - * @since 4.0 - */ - public boolean isValidFirstChar(char ch); - - /** - * Check if an exception indicates a constraint violation (duplicate key) - * - * @since 4.0 - */ - public boolean isDuplicateKeyException(SQLException ex); -} +/* + * 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 + * Stefan Winkler - bug 289445 + */ +package org.eclipse.net4j.db; + +import org.eclipse.net4j.db.ddl.IDBTable; +import org.eclipse.net4j.internal.db.DBAdapterRegistry; +import org.eclipse.net4j.util.registry.IRegistry; + +import javax.sql.DataSource; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Collection; +import java.util.Set; + +/** + * Abstracts all aspects of a database that are vendor-specific. + * + * @author Eike Stepper + */ +public interface IDBAdapter +{ + public static final IRegistry REGISTRY = DBAdapterRegistry.INSTANCE; + + public String getName(); + + public String getVersion(); + + public Driver getJDBCDriver(); + + public DataSource createJDBCDataSource(); + + public Set createTables(Iterable tables, Connection connection) throws DBException; + + public boolean createTable(IDBTable table, Statement statement) throws DBException; + + public Collection dropTables(Iterable tables, Connection connection) throws DBException; + + public boolean dropTable(IDBTable table, Statement statement); + + public String[] getReservedWords(); + + public boolean isReservedWord(String word); + + /** + * @since 2.0 + */ + public int getMaxTableNameLength(); + + /** + * @since 2.0 + */ + public int getMaxFieldNameLength(); + + public boolean isTypeIndexable(DBType type); + + /** + * Provide a way for the DBAdapter to override unsupported DB types with replacements. The default implementation just + * returns the given type. Subclasses may override single types with replacements. + * + * @since 3.0 + */ + public DBType adaptType(DBType type); + + /** + * Check if a character is valid as first character. (e.g., underscores are forbidden as first character in Derby + * elements. + * + * @since 4.0 + */ + public boolean isValidFirstChar(char ch); + + /** + * Check if an exception indicates a constraint violation (duplicate key) + * + * @since 4.0 + */ + public boolean isDuplicateKeyException(SQLException ex); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnectionProvider.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnectionProvider.java index 66f662074c..606db237f4 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnectionProvider.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnectionProvider.java @@ -1,28 +1,28 @@ -/* - * 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; - -import javax.sql.DataSource; - -import java.sql.Connection; - -/** - * Provides a database {@link Connection connection}, roughly comparable with a {@link DataSource data source}. - * - * @author Eike Stepper - */ -public interface IDBConnectionProvider -{ - /** - * Returns a connection. - */ - public Connection getConnection() throws DBException; -} +/* + * 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; + +import javax.sql.DataSource; + +import java.sql.Connection; + +/** + * Provides a database {@link Connection connection}, roughly comparable with a {@link DataSource data source}. + * + * @author Eike Stepper + */ +public interface IDBConnectionProvider +{ + /** + * Returns a connection. + */ + public Connection getConnection() throws DBException; +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBRowHandler.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBRowHandler.java index dbf59ccd77..b887c182ae 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBRowHandler.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBRowHandler.java @@ -1,22 +1,22 @@ -/* - * 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; - -/** - * Call-back that handles the values of, for example, a row in a database table. - * - * @see DBUtil#select(java.sql.Connection, IDBRowHandler, String, org.eclipse.net4j.db.ddl.IDBField...) - * @author Eike Stepper - */ -public interface IDBRowHandler -{ - public boolean handle(int row, Object... values); -} +/* + * 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; + +/** + * Call-back that handles the values of, for example, a row in a database table. + * + * @see DBUtil#select(java.sql.Connection, IDBRowHandler, String, org.eclipse.net4j.db.ddl.IDBField...) + * @author Eike Stepper + */ +public interface IDBRowHandler +{ + public boolean handle(int row, Object... values); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBField.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBField.java index 798d25bb0d..39a278a012 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBField.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBField.java @@ -1,53 +1,53 @@ -/* - * 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.ddl; - -import org.eclipse.net4j.db.DBType; - -/** - * A field (column) specification in a {@link IDBTable DB table}. - * - * @author Eike Stepper - * @noimplement This interface is not intended to be implemented by clients. - * @noextend This interface is not intended to be extended by clients. - */ -public interface IDBField extends IDBSchemaElement -{ - public static final int DEFAULT = -1; - - public IDBTable getTable(); - - public void setName(String name); - - public DBType getType(); - - public void setType(DBType type); - - public int getPrecision(); - - public void setPrecision(int precision); - - public int getScale(); - - public void setScale(int scale); - - public boolean isNotNull(); - - public void setNotNull(boolean on); - - public int getPosition(); - - public String getFullName(); - - public String formatPrecision(); - - public String formatPrecisionAndScale(); -} +/* + * 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.ddl; + +import org.eclipse.net4j.db.DBType; + +/** + * A field (column) specification in a {@link IDBTable DB table}. + * + * @author Eike Stepper + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IDBField extends IDBSchemaElement +{ + public static final int DEFAULT = -1; + + public IDBTable getTable(); + + public void setName(String name); + + public DBType getType(); + + public void setType(DBType type); + + public int getPrecision(); + + public void setPrecision(int precision); + + public int getScale(); + + public void setScale(int scale); + + public boolean isNotNull(); + + public void setNotNull(boolean on); + + public int getPosition(); + + public String getFullName(); + + public String formatPrecision(); + + public String formatPrecisionAndScale(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBIndex.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBIndex.java index ede2e86c71..caa4959422 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBIndex.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBIndex.java @@ -1,44 +1,44 @@ -/* - * 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.ddl; - -/** - * An index specification in a {@link IDBTable DB table}. - * - * @author Eike Stepper - * @noimplement This interface is not intended to be implemented by clients. - * @noextend This interface is not intended to be extended by clients. - */ -public interface IDBIndex extends IDBSchemaElement -{ - public IDBTable getTable(); - - public Type getType(); - - public IDBField getField(int index); - - public int getFieldCount(); - - public IDBField[] getFields(); - - public int getPosition(); - - /** - * The type of an {@link IDBIndex index} specification in a {@link IDBTable DB table}. - * - * @author Eike Stepper - * @noextend This interface is not intended to be extended by clients. - */ - public enum Type - { - PRIMARY_KEY, UNIQUE, NON_UNIQUE - } -} +/* + * 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.ddl; + +/** + * An index specification in a {@link IDBTable DB table}. + * + * @author Eike Stepper + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IDBIndex extends IDBSchemaElement +{ + public IDBTable getTable(); + + public Type getType(); + + public IDBField getField(int index); + + public int getFieldCount(); + + public IDBField[] getFields(); + + public int getPosition(); + + /** + * The type of an {@link IDBIndex index} specification in a {@link IDBTable DB table}. + * + * @author Eike Stepper + * @noextend This interface is not intended to be extended by clients. + */ + public enum Type + { + PRIMARY_KEY, UNIQUE, NON_UNIQUE + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchema.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchema.java index f5ea36340a..0d4dbb416e 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchema.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchema.java @@ -1,59 +1,59 @@ -/* - * 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.ddl; - -import org.eclipse.net4j.db.DBException; -import org.eclipse.net4j.db.IDBAdapter; -import org.eclipse.net4j.db.IDBConnectionProvider; - -import javax.sql.DataSource; - -import java.io.PrintStream; -import java.sql.Connection; -import java.util.Set; - -/** - * Specifies a number of {@link IDBTable DB tables} that can be created in or dropped from a database through a - * {@link IDBAdapter DB adapter}. - * - * @author Eike Stepper - */ -public interface IDBSchema extends IDBSchemaElement -{ - public IDBTable addTable(String name) throws DBException; - - /** - * @since 4.0 - */ - public IDBTable removeTable(String name) throws DBException; - - public IDBTable getTable(String name); - - public IDBTable[] getTables(); - - public Set create(IDBAdapter dbAdapter, Connection connection) throws DBException; - - public Set create(IDBAdapter dbAdapter, DataSource dataSource) throws DBException; - - public Set create(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException; - - public void drop(IDBAdapter dbAdapter, Connection connection) throws DBException; - - public void drop(IDBAdapter dbAdapter, DataSource dataSource) throws DBException; - - public void drop(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException; - - public void export(Connection connection, PrintStream out) throws DBException; - - public void export(DataSource dataSource, PrintStream out) throws DBException; - - public void export(IDBConnectionProvider connectionProvider, PrintStream out) throws DBException; -} +/* + * 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.ddl; + +import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.db.IDBConnectionProvider; + +import javax.sql.DataSource; + +import java.io.PrintStream; +import java.sql.Connection; +import java.util.Set; + +/** + * Specifies a number of {@link IDBTable DB tables} that can be created in or dropped from a database through a + * {@link IDBAdapter DB adapter}. + * + * @author Eike Stepper + */ +public interface IDBSchema extends IDBSchemaElement +{ + public IDBTable addTable(String name) throws DBException; + + /** + * @since 4.0 + */ + public IDBTable removeTable(String name) throws DBException; + + public IDBTable getTable(String name); + + public IDBTable[] getTables(); + + public Set create(IDBAdapter dbAdapter, Connection connection) throws DBException; + + public Set create(IDBAdapter dbAdapter, DataSource dataSource) throws DBException; + + public Set create(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException; + + public void drop(IDBAdapter dbAdapter, Connection connection) throws DBException; + + public void drop(IDBAdapter dbAdapter, DataSource dataSource) throws DBException; + + public void drop(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException; + + public void export(Connection connection, PrintStream out) throws DBException; + + public void export(DataSource dataSource, PrintStream out) throws DBException; + + public void export(IDBConnectionProvider connectionProvider, PrintStream out) throws DBException; +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaElement.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaElement.java index bf50a17088..94df2e8564 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaElement.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaElement.java @@ -1,31 +1,31 @@ -/* - * 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.ddl; - -import java.util.Properties; - -/** - * Specifies a hierachical namespace for elements in a {@link IDBSchema DB schema}. - * - * @author Eike Stepper - * @noimplement This interface is not intended to be implemented by clients. - * @noextend This interface is not intended to be extended by clients. - */ -public interface IDBSchemaElement -{ - public IDBSchema getSchema(); - - public String getName(); - - public String getFullName(); - - public Properties getProperties(); -} +/* + * 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.ddl; + +import java.util.Properties; + +/** + * Specifies a hierachical namespace for elements in a {@link IDBSchema DB schema}. + * + * @author Eike Stepper + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IDBSchemaElement +{ + public IDBSchema getSchema(); + + public String getName(); + + public String getFullName(); + + public Properties getProperties(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBTable.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBTable.java index d06c8e2eb3..2e55a5c260 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBTable.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBTable.java @@ -1,53 +1,53 @@ -/* - * 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.ddl; - -import org.eclipse.net4j.db.DBType; - -/** - * A table specification in a {@link IDBSchema DB schema}. - * - * @author Eike Stepper - * @noimplement This interface is not intended to be implemented by clients. - * @noextend This interface is not intended to be extended by clients. - */ -public interface IDBTable extends IDBSchemaElement -{ - public IDBField addField(String name, DBType type); - - public IDBField addField(String name, DBType type, boolean notNull); - - public IDBField addField(String name, DBType type, int precision); - - public IDBField addField(String name, DBType type, int precision, boolean notNull); - - public IDBField addField(String name, DBType type, int precision, int scale); - - public IDBField addField(String name, DBType type, int precision, int scale, boolean notNull); - - public IDBField getField(String name); - - public IDBField getField(int index); - - public int getFieldCount(); - - public IDBField[] getFields(); - - public IDBIndex addIndex(IDBIndex.Type type, IDBField... fields); - - public int getIndexCount(); - - public IDBIndex[] getIndices(); - - public IDBIndex getPrimaryKeyIndex(); - - public String sqlInsert(); -} +/* + * 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.ddl; + +import org.eclipse.net4j.db.DBType; + +/** + * A table specification in a {@link IDBSchema DB schema}. + * + * @author Eike Stepper + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IDBTable extends IDBSchemaElement +{ + public IDBField addField(String name, DBType type); + + public IDBField addField(String name, DBType type, boolean notNull); + + public IDBField addField(String name, DBType type, int precision); + + public IDBField addField(String name, DBType type, int precision, boolean notNull); + + public IDBField addField(String name, DBType type, int precision, int scale); + + public IDBField addField(String name, DBType type, int precision, int scale, boolean notNull); + + public IDBField getField(String name); + + public IDBField getField(int index); + + public int getFieldCount(); + + public IDBField[] getFields(); + + public IDBIndex addIndex(IDBIndex.Type type, IDBField... fields); + + public int getIndexCount(); + + public IDBIndex[] getIndices(); + + public IDBIndex getPrimaryKeyIndex(); + + public String sqlInsert(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/package-info.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/package-info.java index 9632316778..8ccfdc8fb5 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/package-info.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/package-info.java @@ -1,15 +1,15 @@ -/* - * 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 - */ - -/** - * The Net4j DB framework concepts for the abstraction of the SQL data definition language. - */ -package org.eclipse.net4j.db.ddl; +/* + * 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 + */ + +/** + * The Net4j DB framework concepts for the abstraction of the SQL data definition language. + */ +package org.eclipse.net4j.db.ddl; diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBParameter.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBParameter.java index 130b4d3682..64b0da5bec 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBParameter.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBParameter.java @@ -1,33 +1,33 @@ -/* - * 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.dml; - -import org.eclipse.net4j.db.DBType; - -/** - * A parameter specification in a {@link IDBStatement DB statement}. - * - * @author Eike Stepper - * @noimplement This interface is not intended to be implemented by clients. - * @noextend This interface is not intended to be extended by clients. - */ -public interface IDBParameter -{ - public IDBStatement getStatement(); - - /** - * Returns the zero based position of this parameter within the {@link IDBStatement#getParameters() parameters} list - * of the containing {@link #getStatement() statement}. - */ - public int getPosition(); - - public DBType getType(); -} +/* + * 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.dml; + +import org.eclipse.net4j.db.DBType; + +/** + * A parameter specification in a {@link IDBStatement DB statement}. + * + * @author Eike Stepper + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IDBParameter +{ + public IDBStatement getStatement(); + + /** + * Returns the zero based position of this parameter within the {@link IDBStatement#getParameters() parameters} list + * of the containing {@link #getStatement() statement}. + */ + public int getPosition(); + + public DBType getType(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBStatement.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBStatement.java index 99644a82f2..ecad18571e 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBStatement.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/IDBStatement.java @@ -1,39 +1,39 @@ -/* - * 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.dml; - -import org.eclipse.net4j.db.DBType; -import org.eclipse.net4j.db.ddl.IDBField; -import org.eclipse.net4j.db.ddl.IDBSchemaElement; - -/** - * Specifies an SQL statement with zero or more {@link IDBParameter parameters}. - * - * @author Eike Stepper - * @noimplement This interface is not intended to be implemented by clients. - * @noextend This interface is not intended to be extended by clients. - */ -public interface IDBStatement -{ - public IDBParameter addParameter(DBType type); - - public IDBParameter addParameter(IDBField field); - - public IDBParameter[] getParameters(); - - public void addSQL(String literal); - - public void addSQL(IDBParameter parameter); - - public void addSQL(IDBSchemaElement schemaElement); - - public String getSQL(); -} +/* + * 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.dml; + +import org.eclipse.net4j.db.DBType; +import org.eclipse.net4j.db.ddl.IDBField; +import org.eclipse.net4j.db.ddl.IDBSchemaElement; + +/** + * Specifies an SQL statement with zero or more {@link IDBParameter parameters}. + * + * @author Eike Stepper + * @noimplement This interface is not intended to be implemented by clients. + * @noextend This interface is not intended to be extended by clients. + */ +public interface IDBStatement +{ + public IDBParameter addParameter(DBType type); + + public IDBParameter addParameter(IDBField field); + + public IDBParameter[] getParameters(); + + public void addSQL(String literal); + + public void addSQL(IDBParameter parameter); + + public void addSQL(IDBSchemaElement schemaElement); + + public String getSQL(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/package-info.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/package-info.java index a09ce747ed..e283cd4a9f 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/package-info.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/dml/package-info.java @@ -1,15 +1,15 @@ -/* - * 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 - */ - -/** - * The Net4j DB framework concepts for the abstraction of the SQL data manipulation language. - */ -package org.eclipse.net4j.db.dml; +/* + * 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 + */ + +/** + * The Net4j DB framework concepts for the abstraction of the SQL data manipulation language. + */ +package org.eclipse.net4j.db.dml; diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/package-info.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/package-info.java index 3b7935a3f9..6b214a00be 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/package-info.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/package-info.java @@ -1,15 +1,15 @@ -/* - * 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 - */ - -/** - * The Net4j DB framework. - */ -package org.eclipse.net4j.db; +/* + * 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 + */ + +/** + * The Net4j DB framework. + */ +package org.eclipse.net4j.db; diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterDescriptor.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterDescriptor.java index 788caec773..c078ecaae6 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterDescriptor.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterDescriptor.java @@ -1,33 +1,33 @@ -/* - * 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.internal.db; - -import org.eclipse.net4j.db.IDBAdapter; - -/** - * @author Eike Stepper - */ -public abstract class DBAdapterDescriptor -{ - private String name; - - public DBAdapterDescriptor(String name) - { - this.name = name; - } - - public String getName() - { - return name; - } - - public abstract IDBAdapter createDBAdapter(); -} +/* + * 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.internal.db; + +import org.eclipse.net4j.db.IDBAdapter; + +/** + * @author Eike Stepper + */ +public abstract class DBAdapterDescriptor +{ + private String name; + + public DBAdapterDescriptor(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + + public abstract IDBAdapter createDBAdapter(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterRegistry.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterRegistry.java index 92f2ddbe6b..2b953b2d65 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterRegistry.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterRegistry.java @@ -1,79 +1,79 @@ -/* - * 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.internal.db; - -import org.eclipse.net4j.db.IDBAdapter; -import org.eclipse.net4j.util.registry.HashMapRegistry; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author Eike Stepper - */ -public class DBAdapterRegistry extends HashMapRegistry -{ - public static final DBAdapterRegistry INSTANCE = new DBAdapterRegistry(); - - private Map descriptors = new HashMap(); - - public DBAdapterRegistry() - { - } - - public DBAdapterRegistry(int initialCapacity) - { - super(initialCapacity); - } - - public DBAdapterRegistry(int initialCapacity, float loadFactor) - { - super(initialCapacity, loadFactor); - } - - public DBAdapterRegistry(Map m) - { - super(m); - } - - @Override - public IDBAdapter get(Object key) - { - IDBAdapter adapter = super.get(key); - if (adapter == null) - { - if (key instanceof String) - { - DBAdapterDescriptor descriptor = descriptors.get(key); - if (descriptor != null) - { - adapter = descriptor.createDBAdapter(); - if (adapter != null) - { - put((String)key, adapter); - } - } - } - } - - return adapter; - } - - public DBAdapterDescriptor addDescriptor(DBAdapterDescriptor descriptor) - { - return descriptors.put(descriptor.getName(), descriptor); - } - - public DBAdapterDescriptor removeDescriptor(String name) - { - return descriptors.remove(name); - } -} +/* + * 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.internal.db; + +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.util.registry.HashMapRegistry; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Eike Stepper + */ +public class DBAdapterRegistry extends HashMapRegistry +{ + public static final DBAdapterRegistry INSTANCE = new DBAdapterRegistry(); + + private Map descriptors = new HashMap(); + + public DBAdapterRegistry() + { + } + + public DBAdapterRegistry(int initialCapacity) + { + super(initialCapacity); + } + + public DBAdapterRegistry(int initialCapacity, float loadFactor) + { + super(initialCapacity, loadFactor); + } + + public DBAdapterRegistry(Map m) + { + super(m); + } + + @Override + public IDBAdapter get(Object key) + { + IDBAdapter adapter = super.get(key); + if (adapter == null) + { + if (key instanceof String) + { + DBAdapterDescriptor descriptor = descriptors.get(key); + if (descriptor != null) + { + adapter = descriptor.createDBAdapter(); + if (adapter != null) + { + put((String)key, adapter); + } + } + } + } + + return adapter; + } + + public DBAdapterDescriptor addDescriptor(DBAdapterDescriptor descriptor) + { + return descriptors.put(descriptor.getName(), descriptor); + } + + public DBAdapterDescriptor removeDescriptor(String name) + { + return descriptors.remove(name); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DataSourceConnectionProvider.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DataSourceConnectionProvider.java index 722404cb17..bceab25706 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DataSourceConnectionProvider.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DataSourceConnectionProvider.java @@ -1,55 +1,55 @@ -/* - * 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.internal.db; - -import org.eclipse.net4j.db.DBException; -import org.eclipse.net4j.db.IDBConnectionProvider; - -import javax.sql.DataSource; - -import java.sql.Connection; -import java.sql.SQLException; - -/** - * @author Eike Stepper - */ -public class DataSourceConnectionProvider implements IDBConnectionProvider -{ - private DataSource dataSource; - - public DataSourceConnectionProvider(DataSource dataSource) - { - this.dataSource = dataSource; - } - - public DataSource getDataSource() - { - return dataSource; - } - - public Connection getConnection() - { - try - { - return dataSource.getConnection(); - } - catch (SQLException ex) - { - throw new DBException(ex); - } - } - - @Override - public String toString() - { - return dataSource.toString(); - } -} +/* + * 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.internal.db; + +import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.IDBConnectionProvider; + +import javax.sql.DataSource; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * @author Eike Stepper + */ +public class DataSourceConnectionProvider implements IDBConnectionProvider +{ + private DataSource dataSource; + + public DataSourceConnectionProvider(DataSource dataSource) + { + this.dataSource = dataSource; + } + + public DataSource getDataSource() + { + return dataSource; + } + + public Connection getConnection() + { + try + { + return dataSource.getConnection(); + } + catch (SQLException ex) + { + throw new DBException(ex); + } + } + + @Override + public String toString() + { + return dataSource.toString(); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/bundle/OM.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/bundle/OM.java index 923ba0a3f9..8d1f75f086 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/bundle/OM.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/bundle/OM.java @@ -1,92 +1,92 @@ -/* - * 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.internal.db.bundle; - -import org.eclipse.net4j.db.IDBAdapter; -import org.eclipse.net4j.internal.db.DBAdapterDescriptor; -import org.eclipse.net4j.internal.db.DBAdapterRegistry; -import org.eclipse.net4j.util.om.OMBundle; -import org.eclipse.net4j.util.om.OMPlatform; -import org.eclipse.net4j.util.om.OSGiActivator; -import org.eclipse.net4j.util.om.log.OMLogger; -import org.eclipse.net4j.util.om.trace.OMTracer; - -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IConfigurationElement; -import org.eclipse.core.runtime.IExtensionRegistry; -import org.eclipse.core.runtime.Platform; - -/** - * The Operations & Maintenance class of this bundle. - * - * @author Eike Stepper - */ -public abstract class OM -{ - public static final String BUNDLE_ID = "org.eclipse.net4j.db"; //$NON-NLS-1$ - - public static final OMBundle BUNDLE = OMPlatform.INSTANCE.bundle(BUNDLE_ID, OM.class); - - public static final OMTracer DEBUG = BUNDLE.tracer("debug"); //$NON-NLS-1$ - - public static final OMTracer DEBUG_SQL = DEBUG.tracer("sql"); //$NON-NLS-1$ - - public static final OMLogger LOG = BUNDLE.logger(); - - public static final String EXT_POINT = "dbAdapters"; //$NON-NLS-1$ - - /** - * @author Eike Stepper - */ - public static final class Activator extends OSGiActivator - { - public Activator() - { - super(BUNDLE); - } - - @Override - protected void doStart() throws Exception - { - initDBAdapterRegistry(); - } - - private void initDBAdapterRegistry() - { - IExtensionRegistry registry = Platform.getExtensionRegistry(); - IConfigurationElement[] elements = registry.getConfigurationElementsFor(BUNDLE_ID, EXT_POINT); - for (final IConfigurationElement element : elements) - { - if ("dbAdapter".equals(element.getName())) //$NON-NLS-1$ - { - DBAdapterDescriptor descriptor = new DBAdapterDescriptor(element.getAttribute("name")) //$NON-NLS-1$ - { - @Override - public IDBAdapter createDBAdapter() - { - try - { - return (IDBAdapter)element.createExecutableExtension("class"); //$NON-NLS-1$ - } - catch (CoreException ex) - { - OM.LOG.error(ex); - return null; - } - } - }; - - DBAdapterRegistry.INSTANCE.addDescriptor(descriptor); - } - } - } - } -} +/* + * 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.internal.db.bundle; + +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.internal.db.DBAdapterDescriptor; +import org.eclipse.net4j.internal.db.DBAdapterRegistry; +import org.eclipse.net4j.util.om.OMBundle; +import org.eclipse.net4j.util.om.OMPlatform; +import org.eclipse.net4j.util.om.OSGiActivator; +import org.eclipse.net4j.util.om.log.OMLogger; +import org.eclipse.net4j.util.om.trace.OMTracer; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; + +/** + * The Operations & Maintenance class of this bundle. + * + * @author Eike Stepper + */ +public abstract class OM +{ + public static final String BUNDLE_ID = "org.eclipse.net4j.db"; //$NON-NLS-1$ + + public static final OMBundle BUNDLE = OMPlatform.INSTANCE.bundle(BUNDLE_ID, OM.class); + + public static final OMTracer DEBUG = BUNDLE.tracer("debug"); //$NON-NLS-1$ + + public static final OMTracer DEBUG_SQL = DEBUG.tracer("sql"); //$NON-NLS-1$ + + public static final OMLogger LOG = BUNDLE.logger(); + + public static final String EXT_POINT = "dbAdapters"; //$NON-NLS-1$ + + /** + * @author Eike Stepper + */ + public static final class Activator extends OSGiActivator + { + public Activator() + { + super(BUNDLE); + } + + @Override + protected void doStart() throws Exception + { + initDBAdapterRegistry(); + } + + private void initDBAdapterRegistry() + { + IExtensionRegistry registry = Platform.getExtensionRegistry(); + IConfigurationElement[] elements = registry.getConfigurationElementsFor(BUNDLE_ID, EXT_POINT); + for (final IConfigurationElement element : elements) + { + if ("dbAdapter".equals(element.getName())) //$NON-NLS-1$ + { + DBAdapterDescriptor descriptor = new DBAdapterDescriptor(element.getAttribute("name")) //$NON-NLS-1$ + { + @Override + public IDBAdapter createDBAdapter() + { + try + { + return (IDBAdapter)element.createExecutableExtension("class"); //$NON-NLS-1$ + } + catch (CoreException ex) + { + OM.LOG.error(ex); + return null; + } + } + }; + + DBAdapterRegistry.INSTANCE.addDescriptor(descriptor); + } + } + } + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBField.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBField.java index 7dc06d9226..47966d6f3b 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBField.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBField.java @@ -1,167 +1,167 @@ -/* - * 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.internal.db.ddl; - -import org.eclipse.net4j.db.DBType; -import org.eclipse.net4j.db.ddl.IDBField; -import org.eclipse.net4j.db.ddl.IDBSchema; - -/** - * @author Eike Stepper - */ -public class DBField extends DBSchemaElement implements IDBField -{ - public static final int DEFAULT_DECIMAL_PRECISION = 5; - - public static final int DEFAULT_SCALE = 0; - - public static final int DEFAULT_CHAR_LENGTH = 1; - - public static final int DEFAULT_VARCHAR_LENGTH = 255; - - private DBTable table; - - private String name; - - private DBType type; - - private int precision; - - private int scale; - - private boolean notNull; - - private int position; - - public DBField(DBTable table, String name, DBType type, int precision, int scale, boolean notNull, int position) - { - this.table = table; - this.name = name; - this.type = type; - this.precision = precision; - this.scale = scale; - this.notNull = notNull; - this.position = position; - } - - public IDBSchema getSchema() - { - return table.getSchema(); - } - - public DBTable getTable() - { - return table; - } - - public String getName() - { - return name; - } - - public void setName(String name) - { - this.name = name; - } - - public DBType getType() - { - return type; - } - - public void setType(DBType type) - { - this.type = type; - } - - public int getPrecision() - { - if (precision == DEFAULT) - { - switch (type) - { - case CHAR: - return DEFAULT_CHAR_LENGTH; - - case VARCHAR: - case VARBINARY: - return DEFAULT_VARCHAR_LENGTH; - - case DECIMAL: - case NUMERIC: - return DEFAULT_DECIMAL_PRECISION; - } - } - - return precision; - } - - public void setPrecision(int precision) - { - this.precision = precision; - } - - public int getScale() - { - if (scale == DEFAULT) - { - return DEFAULT_SCALE; - } - - return scale; - } - - public void setScale(int scale) - { - this.scale = scale; - } - - public boolean isNotNull() - { - return notNull; - } - - public void setNotNull(boolean on) - { - notNull = on; - } - - public int getPosition() - { - return position; - } - - public String getFullName() - { - return table.getName() + "." + name; //$NON-NLS-1$ - } - - public String formatPrecision() - { - int precision = getPrecision(); - if (precision > 0) - { - return "(" + precision + ")"; //$NON-NLS-1$ //$NON-NLS-2$ - } - - return ""; //$NON-NLS-1$ - } - - public String formatPrecisionAndScale() - { - if (scale == DEFAULT) - { - return "(" + getPrecision() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ - } - - return "(" + getPrecision() + ", " + scale + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - } -} +/* + * 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.internal.db.ddl; + +import org.eclipse.net4j.db.DBType; +import org.eclipse.net4j.db.ddl.IDBField; +import org.eclipse.net4j.db.ddl.IDBSchema; + +/** + * @author Eike Stepper + */ +public class DBField extends DBSchemaElement implements IDBField +{ + public static final int DEFAULT_DECIMAL_PRECISION = 5; + + public static final int DEFAULT_SCALE = 0; + + public static final int DEFAULT_CHAR_LENGTH = 1; + + public static final int DEFAULT_VARCHAR_LENGTH = 255; + + private DBTable table; + + private String name; + + private DBType type; + + private int precision; + + private int scale; + + private boolean notNull; + + private int position; + + public DBField(DBTable table, String name, DBType type, int precision, int scale, boolean notNull, int position) + { + this.table = table; + this.name = name; + this.type = type; + this.precision = precision; + this.scale = scale; + this.notNull = notNull; + this.position = position; + } + + public IDBSchema getSchema() + { + return table.getSchema(); + } + + public DBTable getTable() + { + return table; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public DBType getType() + { + return type; + } + + public void setType(DBType type) + { + this.type = type; + } + + public int getPrecision() + { + if (precision == DEFAULT) + { + switch (type) + { + case CHAR: + return DEFAULT_CHAR_LENGTH; + + case VARCHAR: + case VARBINARY: + return DEFAULT_VARCHAR_LENGTH; + + case DECIMAL: + case NUMERIC: + return DEFAULT_DECIMAL_PRECISION; + } + } + + return precision; + } + + public void setPrecision(int precision) + { + this.precision = precision; + } + + public int getScale() + { + if (scale == DEFAULT) + { + return DEFAULT_SCALE; + } + + return scale; + } + + public void setScale(int scale) + { + this.scale = scale; + } + + public boolean isNotNull() + { + return notNull; + } + + public void setNotNull(boolean on) + { + notNull = on; + } + + public int getPosition() + { + return position; + } + + public String getFullName() + { + return table.getName() + "." + name; //$NON-NLS-1$ + } + + public String formatPrecision() + { + int precision = getPrecision(); + if (precision > 0) + { + return "(" + precision + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + return ""; //$NON-NLS-1$ + } + + public String formatPrecisionAndScale() + { + if (scale == DEFAULT) + { + return "(" + getPrecision() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + return "(" + getPrecision() + ", " + scale + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java index 160791c432..6c00ca4c0a 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBIndex.java @@ -1,82 +1,82 @@ -/* - * 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.internal.db.ddl; - -import org.eclipse.net4j.db.ddl.IDBField; -import org.eclipse.net4j.db.ddl.IDBIndex; -import org.eclipse.net4j.db.ddl.IDBSchema; - -/** - * @author Eike Stepper - */ -public class DBIndex extends DBSchemaElement implements IDBIndex -{ - private DBTable table; - - private Type type; - - private IDBField[] fields; - - public int position; - - public DBIndex(DBTable table, Type type, IDBField[] fields, int position) - { - this.table = table; - this.type = type; - this.fields = fields; - this.position = position; - } - - public IDBSchema getSchema() - { - return table.getSchema(); - } - - public DBTable getTable() - { - return table; - } - - public Type getType() - { - return type; - } - - public IDBField getField(int index) - { - return fields[index]; - } - - public int getFieldCount() - { - return fields.length; - } - - public IDBField[] getFields() - { - return fields; - } - - public int getPosition() - { - return position; - } - - public String getName() - { - return "idx_" + table.getName() + "_" + position; //$NON-NLS-1$ //$NON-NLS-2$ - } - - public String getFullName() - { - return getName(); - } -} +/* + * 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.internal.db.ddl; + +import org.eclipse.net4j.db.ddl.IDBField; +import org.eclipse.net4j.db.ddl.IDBIndex; +import org.eclipse.net4j.db.ddl.IDBSchema; + +/** + * @author Eike Stepper + */ +public class DBIndex extends DBSchemaElement implements IDBIndex +{ + private DBTable table; + + private Type type; + + private IDBField[] fields; + + public int position; + + public DBIndex(DBTable table, Type type, IDBField[] fields, int position) + { + this.table = table; + this.type = type; + this.fields = fields; + this.position = position; + } + + public IDBSchema getSchema() + { + return table.getSchema(); + } + + public DBTable getTable() + { + return table; + } + + public Type getType() + { + return type; + } + + public IDBField getField(int index) + { + return fields[index]; + } + + public int getFieldCount() + { + return fields.length; + } + + public IDBField[] getFields() + { + return fields; + } + + public int getPosition() + { + return position; + } + + public String getName() + { + return "idx_" + table.getName() + "_" + position; //$NON-NLS-1$ //$NON-NLS-2$ + } + + public String getFullName() + { + return getName(); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBSchemaElement.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBSchemaElement.java index e856129cee..8deaec86f0 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBSchemaElement.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBSchemaElement.java @@ -1,44 +1,44 @@ -/* - * 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.internal.db.ddl; - -import org.eclipse.net4j.db.ddl.IDBSchemaElement; - -import java.util.Properties; - -/** - * @author Eike Stepper - * @since 2.0 - */ -public abstract class DBSchemaElement implements IDBSchemaElement -{ - private Properties properties; - - public DBSchemaElement() - { - } - - public synchronized final Properties getProperties() - { - if (properties == null) - { - properties = new Properties(); - } - - return properties; - } - - @Override - public String toString() - { - return getName(); - } -} +/* + * 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.internal.db.ddl; + +import org.eclipse.net4j.db.ddl.IDBSchemaElement; + +import java.util.Properties; + +/** + * @author Eike Stepper + * @since 2.0 + */ +public abstract class DBSchemaElement implements IDBSchemaElement +{ + private Properties properties; + + public DBSchemaElement() + { + } + + public synchronized final Properties getProperties() + { + if (properties == null) + { + properties = new Properties(); + } + + return properties; + } + + @Override + public String toString() + { + return getName(); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBTable.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBTable.java index 467815aeda..b98a5bb609 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBTable.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/ddl/DBTable.java @@ -1,175 +1,175 @@ -/* - * 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.internal.db.ddl; - -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.IDBIndex.Type; -import org.eclipse.net4j.db.ddl.IDBTable; -import org.eclipse.net4j.spi.db.DBSchema; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Eike Stepper - */ -public class DBTable extends DBSchemaElement implements IDBTable -{ - private DBSchema schema; - - private String name; - - private List fields = new ArrayList(); - - private List indices = new ArrayList(); - - public DBTable(DBSchema schema, String name) - { - this.schema = schema; - this.name = name; - } - - public DBSchema getSchema() - { - return schema; - } - - public String getName() - { - return name; - } - - public DBField addField(String name, DBType type) - { - return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, false); - } - - public DBField addField(String name, DBType type, boolean notNull) - { - return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, notNull); - } - - public DBField addField(String name, DBType type, int precision) - { - return addField(name, type, precision, IDBField.DEFAULT, false); - } - - public DBField addField(String name, DBType type, int precision, boolean notNull) - { - return addField(name, type, precision, IDBField.DEFAULT, notNull); - } - - public DBField addField(String name, DBType type, int precision, int scale) - { - return addField(name, type, precision, scale, false); - } - - public DBField addField(String name, DBType type, int precision, int scale, boolean notNull) - { - schema.assertUnlocked(); - if (getField(name) != null) - { - throw new DBException("DBField exists: " + name); //$NON-NLS-1$ - } - - DBField field = new DBField(this, name, type, precision, scale, notNull, fields.size()); - fields.add(field); - return field; - } - - public DBField getField(String name) - { - for (DBField field : fields) - { - if (name.equals(field.getName())) - { - return field; - } - } - - return null; - } - - public DBField getField(int index) - { - return fields.get(index); - } - - public int getFieldCount() - { - return fields.size(); - } - - public DBField[] getFields() - { - return fields.toArray(new DBField[fields.size()]); - } - - public DBIndex addIndex(Type type, IDBField... fields) - { - schema.assertUnlocked(); - DBIndex index = new DBIndex(this, type, fields, indices.size()); - indices.add(index); - return index; - } - - public int getIndexCount() - { - return indices.size(); - } - - public DBIndex[] getIndices() - { - return indices.toArray(new DBIndex[indices.size()]); - } - - public IDBIndex getPrimaryKeyIndex() - { - for (IDBIndex index : indices) - { - if (index.getType() == IDBIndex.Type.PRIMARY_KEY) - { - return index; - } - } - - return null; - } - - public String getFullName() - { - return name; - } - - public String sqlInsert() - { - StringBuilder builder = new StringBuilder(); - builder.append("INSERT INTO "); //$NON-NLS-1$ - builder.append(getName()); - builder.append(" VALUES ("); //$NON-NLS-1$ - - for (int i = 0; i < fields.size(); i++) - { - if (i > 0) - { - builder.append(", "); //$NON-NLS-1$ - } - - builder.append("?"); //$NON-NLS-1$ - } - - builder.append(")"); //$NON-NLS-1$ - return builder.toString(); - } -} +/* + * 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.internal.db.ddl; + +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.IDBIndex.Type; +import org.eclipse.net4j.db.ddl.IDBTable; +import org.eclipse.net4j.spi.db.DBSchema; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eike Stepper + */ +public class DBTable extends DBSchemaElement implements IDBTable +{ + private DBSchema schema; + + private String name; + + private List fields = new ArrayList(); + + private List indices = new ArrayList(); + + public DBTable(DBSchema schema, String name) + { + this.schema = schema; + this.name = name; + } + + public DBSchema getSchema() + { + return schema; + } + + public String getName() + { + return name; + } + + public DBField addField(String name, DBType type) + { + return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, false); + } + + public DBField addField(String name, DBType type, boolean notNull) + { + return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, notNull); + } + + public DBField addField(String name, DBType type, int precision) + { + return addField(name, type, precision, IDBField.DEFAULT, false); + } + + public DBField addField(String name, DBType type, int precision, boolean notNull) + { + return addField(name, type, precision, IDBField.DEFAULT, notNull); + } + + public DBField addField(String name, DBType type, int precision, int scale) + { + return addField(name, type, precision, scale, false); + } + + public DBField addField(String name, DBType type, int precision, int scale, boolean notNull) + { + schema.assertUnlocked(); + if (getField(name) != null) + { + throw new DBException("DBField exists: " + name); //$NON-NLS-1$ + } + + DBField field = new DBField(this, name, type, precision, scale, notNull, fields.size()); + fields.add(field); + return field; + } + + public DBField getField(String name) + { + for (DBField field : fields) + { + if (name.equals(field.getName())) + { + return field; + } + } + + return null; + } + + public DBField getField(int index) + { + return fields.get(index); + } + + public int getFieldCount() + { + return fields.size(); + } + + public DBField[] getFields() + { + return fields.toArray(new DBField[fields.size()]); + } + + public DBIndex addIndex(Type type, IDBField... fields) + { + schema.assertUnlocked(); + DBIndex index = new DBIndex(this, type, fields, indices.size()); + indices.add(index); + return index; + } + + public int getIndexCount() + { + return indices.size(); + } + + public DBIndex[] getIndices() + { + return indices.toArray(new DBIndex[indices.size()]); + } + + public IDBIndex getPrimaryKeyIndex() + { + for (IDBIndex index : indices) + { + if (index.getType() == IDBIndex.Type.PRIMARY_KEY) + { + return index; + } + } + + return null; + } + + public String getFullName() + { + return name; + } + + public String sqlInsert() + { + StringBuilder builder = new StringBuilder(); + builder.append("INSERT INTO "); //$NON-NLS-1$ + builder.append(getName()); + builder.append(" VALUES ("); //$NON-NLS-1$ + + for (int i = 0; i < fields.size(); i++) + { + if (i > 0) + { + builder.append(", "); //$NON-NLS-1$ + } + + builder.append("?"); //$NON-NLS-1$ + } + + builder.append(")"); //$NON-NLS-1$ + return builder.toString(); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBParameter.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBParameter.java index 4b8afbb50a..276129cb07 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBParameter.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBParameter.java @@ -1,49 +1,49 @@ -/* - * 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.internal.db.dml; - -import org.eclipse.net4j.db.DBType; -import org.eclipse.net4j.db.dml.IDBParameter; -import org.eclipse.net4j.db.dml.IDBStatement; - -/** - * @author Eike Stepper - */ -public class DBParameter implements IDBParameter -{ - private IDBStatement statement; - - private int position; - - private DBType type; - - public DBParameter(IDBStatement statement, int position, DBType type) - { - this.statement = statement; - this.position = position; - this.type = type; - } - - public IDBStatement getStatement() - { - return statement; - } - - public int getPosition() - { - return position; - } - - public DBType getType() - { - return type; - } -} +/* + * 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.internal.db.dml; + +import org.eclipse.net4j.db.DBType; +import org.eclipse.net4j.db.dml.IDBParameter; +import org.eclipse.net4j.db.dml.IDBStatement; + +/** + * @author Eike Stepper + */ +public class DBParameter implements IDBParameter +{ + private IDBStatement statement; + + private int position; + + private DBType type; + + public DBParameter(IDBStatement statement, int position, DBType type) + { + this.statement = statement; + this.position = position; + this.type = type; + } + + public IDBStatement getStatement() + { + return statement; + } + + public int getPosition() + { + return position; + } + + public DBType getType() + { + return type; + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBStatement.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBStatement.java index 797c58bbda..1afa04de1e 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBStatement.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/dml/DBStatement.java @@ -1,106 +1,106 @@ -/* - * 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.internal.db.dml; - -import org.eclipse.net4j.db.DBType; -import org.eclipse.net4j.db.ddl.IDBField; -import org.eclipse.net4j.db.ddl.IDBSchemaElement; -import org.eclipse.net4j.db.dml.IDBParameter; -import org.eclipse.net4j.db.dml.IDBStatement; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Eike Stepper - */ -public class DBStatement implements IDBStatement -{ - private static final DBParameter[] NO_PARAMETERS = {}; - - private List parameters; - - private List sequence = new ArrayList(); - - public IDBParameter addParameter(DBType type) - { - int position = 0; - if (parameters == null) - { - parameters = new ArrayList(); - } - else - { - position = parameters.size(); - } - - DBParameter parameter = new DBParameter(this, position, type); - parameters.add(parameter); - return parameter; - } - - public IDBParameter addParameter(IDBField field) - { - return addParameter(field.getType()); - } - - public DBParameter[] getParameters() - { - if (parameters == null) - { - return NO_PARAMETERS; - } - - return parameters.toArray(new DBParameter[parameters.size()]); - } - - public void addSQL(String literal) - { - int tailPos = sequence.size() - 1; - Object tail = sequence.get(tailPos); - if (tail instanceof String) - { - sequence.set(tailPos, (String)tail + literal); - } - else - { - sequence.add(literal); - } - } - - public void addSQL(IDBParameter parameter) - { - sequence.add(parameter); - } - - public void addSQL(IDBSchemaElement schemaElement) - { - addSQL(schemaElement.getName()); - } - - public String getSQL() - { - StringBuilder builder = new StringBuilder(); - for (Object element : sequence) - { - if (element instanceof IDBParameter) - { - builder.append("?"); //$NON-NLS-1$ - } - else - { - builder.append(element); - } - } - - return builder.toString(); - } -} +/* + * 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.internal.db.dml; + +import org.eclipse.net4j.db.DBType; +import org.eclipse.net4j.db.ddl.IDBField; +import org.eclipse.net4j.db.ddl.IDBSchemaElement; +import org.eclipse.net4j.db.dml.IDBParameter; +import org.eclipse.net4j.db.dml.IDBStatement; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eike Stepper + */ +public class DBStatement implements IDBStatement +{ + private static final DBParameter[] NO_PARAMETERS = {}; + + private List parameters; + + private List sequence = new ArrayList(); + + public IDBParameter addParameter(DBType type) + { + int position = 0; + if (parameters == null) + { + parameters = new ArrayList(); + } + else + { + position = parameters.size(); + } + + DBParameter parameter = new DBParameter(this, position, type); + parameters.add(parameter); + return parameter; + } + + public IDBParameter addParameter(IDBField field) + { + return addParameter(field.getType()); + } + + public DBParameter[] getParameters() + { + if (parameters == null) + { + return NO_PARAMETERS; + } + + return parameters.toArray(new DBParameter[parameters.size()]); + } + + public void addSQL(String literal) + { + int tailPos = sequence.size() - 1; + Object tail = sequence.get(tailPos); + if (tail instanceof String) + { + sequence.set(tailPos, (String)tail + literal); + } + else + { + sequence.add(literal); + } + } + + public void addSQL(IDBParameter parameter) + { + sequence.add(parameter); + } + + public void addSQL(IDBSchemaElement schemaElement) + { + addSQL(schemaElement.getName()); + } + + public String getSQL() + { + StringBuilder builder = new StringBuilder(); + for (Object element : sequence) + { + if (element instanceof IDBParameter) + { + builder.append("?"); //$NON-NLS-1$ + } + else + { + builder.append(element); + } + } + + return builder.toString(); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBAdapter.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBAdapter.java index b6ee28153a..feee7fc5a5 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBAdapter.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBAdapter.java @@ -1,549 +1,549 @@ -/* - * 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 - * Stefan Winkler - Bug 289445 - */ -package org.eclipse.net4j.spi.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.ddl.IDBField; -import org.eclipse.net4j.db.ddl.IDBIndex; -import org.eclipse.net4j.db.ddl.IDBTable; -import org.eclipse.net4j.internal.db.bundle.OM; -import org.eclipse.net4j.util.om.trace.ContextTracer; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * A useful base class for implementing custom {@link IDBAdapter DB adapters}. - * - * @author Eike Stepper - */ -public abstract class DBAdapter implements IDBAdapter -{ - private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_SQL, DBAdapter.class); - - private static final String[] SQL92_RESERVED_WORDS = { "ABSOLUTE", "ACTION", "ADD", "AFTER", "ALL", "ALLOCATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ - "ALTER", "AND", "ANY", "ARE", "ARRAY", "AS", "ASC", "ASENSITIVE", "ASSERTION", "ASYMMETRIC", "AT", "ATOMIC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "AUTHORIZATION", "AVG", "BEFORE", "BEGIN", "BETWEEN", "BIGINT", "BINARY", "BIT", "BIT_LENGTH", "BLOB", "BOOLEAN", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "BOTH", "BREADTH", "BY", "CALL", "CALLED", "CASCADE", "CASCADED", "CASE", "CAST", "CATALOG", "CHAR", "CHARACTER", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "CHARACTER_LENGTH", "CHAR_LENGTH", "CHECK", "CLOB", "CLOSE", "COALESCE", "COLLATE", "COLLATION", "COLUMN", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ - "COMMIT", "CONDITION", "CONNECT", "CONNECTION", "CONSTRAINT", "CONSTRAINTS", "CONSTRUCTOR", "CONTAINS", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ - "CONTINUE", "CONVERT", "CORRESPONDING", "COUNT", "CREATE", "CROSS", "CUBE", "CURRENT", "CURRENT_DATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ - "CURRENT_DEFAULT_TRANSFORM_GROUP", "CURRENT_PATH", "CURRENT_ROLE", "CURRENT_TIME", "CURRENT_TIMESTAMP", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ - "CURRENT_TRANSFORM_GROUP_FOR_TYPE", "CURRENT_USER", "CURSOR", "CYCLE", "DATA", "DATE", "DAY", "DEALLOCATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ - "DEC", "DECIMAL", "DECLARE", "DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", "DEPTH", "DEREF", "DESC", "DESCRIBE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "DESCRIPTOR", "DETERMINISTIC", "DIAGNOSTICS", "DISCONNECT", "DISTINCT", "DO", "DOMAIN", "DOUBLE", "DROP", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ - "DYNAMIC", "EACH", "ELEMENT", "ELSE", "ELSEIF", "END", "EQUALS", "ESCAPE", "EXCEPT", "EXCEPTION", "EXEC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "EXECUTE", "EXISTS", "EXIT", "EXTERNAL", "EXTRACT", "FALSE", "FETCH", "FILTER", "FIRST", "FLOAT", "FOR", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "FOREIGN", "FOUND", "FREE", "FROM", "FULL", "FUNCTION", "GENERAL", "GET", "GLOBAL", "GO", "GOTO", "GRANT", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "GROUP", "GROUPING", "HANDLER", "HAVING", "HOLD", "HOUR", "IDENTITY", "IF", "IMMEDIATE", "IN", "INDICATOR", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "INITIALLY", "INNER", "INOUT", "INPUT", "INSENSITIVE", "INSERT", "INT", "INTEGER", "INTERSECT", "INTERVAL", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ - "INTO", "IS", "ISOLATION", "ITERATE", "JOIN", "KEY", "LANGUAGE", "LARGE", "LAST", "LATERAL", "LEADING", "LEAVE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "LEFT", "LEVEL", "LIKE", "LOCAL", "LOCALTIME", "LOCALTIMESTAMP", "LOCATOR", "LOOP", "LOWER", "MAP", "MATCH", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "MAX", "MEMBER", "MERGE", "METHOD", "MIN", "MINUTE", "MODIFIES", "MODULE", "MONTH", "MULTISET", "NAMES", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "NATIONAL", "NATURAL", "NCHAR", "NCLOB", "NEW", "NEXT", "NO", "NONE", "NOT", "NULL", "NULLIF", "NUMERIC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "OBJECT", "OCTET_LENGTH", "OF", "OLD", "ON", "ONLY", "OPEN", "OPTION", "OR", "ORDER", "ORDINALITY", "OUT", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "OUTER", "OUTPUT", "OVER", "OVERLAPS", "PAD", "PARAMETER", "PARTIAL", "PARTITION", "PATH", "POSITION", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ - "PRECISION", "PREPARE", "PRESERVE", "PRIMARY", "PRIOR", "PRIVILEGES", "PROCEDURE", "PUBLIC", "RANGE", "READ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ - "READS", "REAL", "RECURSIVE", "REF", "REFERENCES", "REFERENCING", "RELATIVE", "RELEASE", "REPEAT", "RESIGNAL", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ - "RESTRICT", "RESULT", "RETURN", "RETURNS", "REVOKE", "RIGHT", "ROLE", "ROLLBACK", "ROLLUP", "ROUTINE", "ROW", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "ROWS", "SAVEPOINT", "SCHEMA", "SCOPE", "SCROLL", "SEARCH", "SECOND", "SECTION", "SELECT", "SENSITIVE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ - "SESSION", "SESSION_USER", "SET", "SETS", "SIGNAL", "SIMILAR", "SIZE", "SMALLINT", "SOME", "SPACE", "SPECIFIC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "SPECIFICTYPE", "SQL", "SQLCODE", "SQLERROR", "SQLEXCEPTION", "SQLSTATE", "SQLWARNING", "START", "STATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ - "STATIC", "SUBMULTISET", "SUBSTRING", "SUM", "SYMMETRIC", "SYSTEM", "SYSTEM_USER", "TABLE", "TABLESAMPLE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ - "TEMPORARY", "THEN", "TIME", "TIMESTAMP", "TIMEZONE_HOUR", "TIMEZONE_MINUTE", "TO", "TRAILING", "TRANSACTION", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ - "TRANSLATE", "TRANSLATION", "TREAT", "TRIGGER", "TRIM", "TRUE", "UNDER", "UNDO", "UNION", "UNIQUE", "UNKNOWN", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ - "UNNEST", "UNTIL", "UPDATE", "UPPER", "USAGE", "USER", "USING", "VALUE", "VALUES", "VARCHAR", "VARYING", "VIEW", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - "WHEN", "WHENEVER", "WHERE", "WHILE", "WINDOW", "WITH", "WITHIN", "WITHOUT", "WORK", "WRITE", "YEAR", "ZONE" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ - - private String name; - - private String version; - - private Set reservedWords; - - public DBAdapter(String name, String version) - { - this.name = name; - this.version = version; - } - - public String getName() - { - return name; - } - - public String getVersion() - { - return version; - } - - public Set createTables(Iterable tables, Connection connection) throws DBException - { - Set createdTables = new HashSet(); - Statement statement = null; - - try - { - statement = connection.createStatement(); - for (IDBTable table : tables) - { - if (createTable(table, statement)) - { - createdTables.add(table); - } - } - } - catch (SQLException ex) - { - throw new DBException(ex); - } - finally - { - DBUtil.close(statement); - } - - return createdTables; - } - - public boolean createTable(IDBTable table, Statement statement) throws DBException - { - boolean created = true; - - try - { - doCreateTable(table, statement); - } - catch (SQLException ex) - { - created = false; - if (TRACER.isEnabled()) - { - TRACER.trace("-- " + ex.getMessage()); //$NON-NLS-1$ - } - } - - validateTable(table, statement); - return created; - } - - public Collection dropTables(Iterable tables, Connection connection) throws DBException - { - List droppedTables = new ArrayList(); - Statement statement = null; - - try - { - statement = connection.createStatement(); - for (IDBTable table : tables) - { - if (dropTable(table, statement)) - { - droppedTables.add(table); - } - } - } - catch (SQLException ex) - { - OM.LOG.error(ex); - } - finally - { - DBUtil.close(statement); - } - - return droppedTables; - } - - public boolean dropTable(IDBTable table, Statement statement) - { - try - { - String sql = getDropTableSQL(table); - if (TRACER.isEnabled()) - { - TRACER.trace(sql); - } - - statement.execute(sql); - return true; - } - catch (SQLException ex) - { - if (TRACER.isEnabled()) - { - TRACER.trace(ex.getMessage()); - } - - return false; - } - } - - protected String getDropTableSQL(IDBTable table) - { - return "DROP TABLE " + table; //$NON-NLS-1$ - } - - /** - * @since 2.0 - */ - public int getMaxTableNameLength() - { - // Ansi SQL 92 default value - return 128; - } - - /** - * @since 2.0 - */ - public int getMaxFieldNameLength() - { - // Ansi SQL 92 default value - return 128; - } - - public boolean isTypeIndexable(DBType type) - { - switch (type) - { - case CLOB: - case BLOB: - case LONGVARCHAR: - case LONGVARBINARY: - case VARBINARY: - case BINARY: - return false; - - default: - return true; - } - } - - @Override - public String toString() - { - return getName() + "-" + getVersion(); //$NON-NLS-1$ - } - - /** - * @since 2.0 - */ - protected void doCreateTable(IDBTable table, Statement statement) throws SQLException - { - StringBuilder builder = new StringBuilder(); - builder.append("CREATE TABLE "); //$NON-NLS-1$ - builder.append(table); - builder.append(" ("); //$NON-NLS-1$ - appendFieldDefs(builder, table, createFieldDefinitions(table)); - String constraints = createConstraints(table); - if (constraints != null) - { - builder.append(", "); //$NON-NLS-1$ - builder.append(constraints); - } - - builder.append(")"); //$NON-NLS-1$ - String sql = builder.toString(); - if (TRACER.isEnabled()) - { - TRACER.trace(sql); - } - - statement.execute(sql); - - IDBIndex[] indices = table.getIndices(); - for (int i = 0; i < indices.length; i++) - { - createIndex(indices[i], statement, i); - } - } - - /** - * @since 2.0 - */ - 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 "); //$NON-NLS-1$ - builder.append(table); - builder.append("_idx"); //$NON-NLS-1$ - 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); - } - - protected void addIndexField(StringBuilder builder, IDBField field) - { - builder.append(field); - } - - /** - * @since 2.0 - */ - protected String createConstraints(IDBTable table) - { - return null; - } - - /** - * @since 2.0 - */ - protected String createFieldDefinition(IDBField field) - { - return getTypeName(field) + (field.isNotNull() ? " NOT NULL" : ""); //$NON-NLS-1$ //$NON-NLS-2$ - } - - protected String getTypeName(IDBField 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); //$NON-NLS-1$ - } - - public String[] getSQL92ReservedWords() - { - return SQL92_RESERVED_WORDS; - } - - public boolean isReservedWord(String word) - { - if (reservedWords == null) - { - reservedWords = new HashSet(); - for (String reservedWord : getReservedWords()) - { - reservedWords.add(reservedWord.toUpperCase()); - } - } - - word = word.toUpperCase(); - return reservedWords.contains(word); - } - - /** - * @since 2.0 - */ - protected void validateTable(IDBTable table, Statement statement) throws DBException - { - int maxRows = 1; - - try - { - maxRows = statement.getMaxRows(); - statement.setMaxRows(1); - - String sql = null; - - try - { - StringBuilder builder = new StringBuilder(); - builder.append("SELECT "); //$NON-NLS-1$ - appendFieldNames(builder, table); - builder.append(" FROM "); //$NON-NLS-1$ - builder.append(table); - sql = builder.toString(); - - if (TRACER.isEnabled()) - { - TRACER.format("{0}", sql); //$NON-NLS-1$ - } - - ResultSet resultSet = statement.executeQuery(sql); - - try - { - ResultSetMetaData metaData = resultSet.getMetaData(); - int columnCount = metaData.getColumnCount(); - if (columnCount != table.getFieldCount()) - { - throw new DBException("DBTable " + table + " has " + columnCount + " columns instead of " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + table.getFieldCount()); - } - } - finally - { - DBUtil.close(resultSet); - } - } - catch (SQLException ex) - { - throw new DBException("Problem with table " + table, ex, sql); - } - finally - { - if (maxRows != 1) - { - statement.setMaxRows(1); - } - } - } - catch (SQLException ex) - { - throw new DBException(ex); - } - } - - private String[] createFieldDefinitions(IDBTable table) - { - IDBField[] fields = table.getFields(); - int fieldCount = fields.length; - - String[] result = new String[fieldCount]; - for (int i = 0; i < fieldCount; i++) - { - IDBField field = fields[i]; - result[i] = createFieldDefinition(field); - } - - return result; - } - - public void appendFieldNames(Appendable appendable, IDBTable table) - { - try - { - IDBField[] fields = table.getFields(); - for (int i = 0; i < fields.length; i++) - { - IDBField field = fields[i]; - if (i != 0) - { - appendable.append(", "); //$NON-NLS-1$ - } - - String fieldName = field.getName(); - appendable.append(fieldName); - } - } - catch (IOException canNotHappen) - { - } - } - - private void appendFieldDefs(Appendable appendable, IDBTable table, String[] defs) - { - try - { - IDBField[] fields = table.getFields(); - for (int i = 0; i < fields.length; i++) - { - IDBField field = fields[i]; - if (i != 0) - { - appendable.append(", "); //$NON-NLS-1$ - } - - // String fieldName = mangleFieldName(field.getName(), 0); - String fieldName = field.getName(); - appendable.append(fieldName); - appendable.append(" "); //$NON-NLS-1$ - appendable.append(defs[i]); - } - } - catch (IOException canNotHappen) - { - } - } - - /** - * @since 3.0 - */ - public DBType adaptType(DBType type) - { - return type; - } - - /** - * @since 4.0 - */ - public boolean isValidFirstChar(char ch) - { - return true; - } - - /** - * @since 4.0 - */ - public boolean isDuplicateKeyException(SQLException ex) - { - /* SQL code for duplicate keys is 23001 */ - return "23001".equals(ex.getSQLState()); - } -} +/* + * 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 + * Stefan Winkler - Bug 289445 + */ +package org.eclipse.net4j.spi.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.ddl.IDBField; +import org.eclipse.net4j.db.ddl.IDBIndex; +import org.eclipse.net4j.db.ddl.IDBTable; +import org.eclipse.net4j.internal.db.bundle.OM; +import org.eclipse.net4j.util.om.trace.ContextTracer; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * A useful base class for implementing custom {@link IDBAdapter DB adapters}. + * + * @author Eike Stepper + */ +public abstract class DBAdapter implements IDBAdapter +{ + private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_SQL, DBAdapter.class); + + private static final String[] SQL92_RESERVED_WORDS = { "ABSOLUTE", "ACTION", "ADD", "AFTER", "ALL", "ALLOCATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ + "ALTER", "AND", "ANY", "ARE", "ARRAY", "AS", "ASC", "ASENSITIVE", "ASSERTION", "ASYMMETRIC", "AT", "ATOMIC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "AUTHORIZATION", "AVG", "BEFORE", "BEGIN", "BETWEEN", "BIGINT", "BINARY", "BIT", "BIT_LENGTH", "BLOB", "BOOLEAN", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "BOTH", "BREADTH", "BY", "CALL", "CALLED", "CASCADE", "CASCADED", "CASE", "CAST", "CATALOG", "CHAR", "CHARACTER", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "CHARACTER_LENGTH", "CHAR_LENGTH", "CHECK", "CLOB", "CLOSE", "COALESCE", "COLLATE", "COLLATION", "COLUMN", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ + "COMMIT", "CONDITION", "CONNECT", "CONNECTION", "CONSTRAINT", "CONSTRAINTS", "CONSTRUCTOR", "CONTAINS", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ + "CONTINUE", "CONVERT", "CORRESPONDING", "COUNT", "CREATE", "CROSS", "CUBE", "CURRENT", "CURRENT_DATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ + "CURRENT_DEFAULT_TRANSFORM_GROUP", "CURRENT_PATH", "CURRENT_ROLE", "CURRENT_TIME", "CURRENT_TIMESTAMP", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ + "CURRENT_TRANSFORM_GROUP_FOR_TYPE", "CURRENT_USER", "CURSOR", "CYCLE", "DATA", "DATE", "DAY", "DEALLOCATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ + "DEC", "DECIMAL", "DECLARE", "DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", "DEPTH", "DEREF", "DESC", "DESCRIBE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "DESCRIPTOR", "DETERMINISTIC", "DIAGNOSTICS", "DISCONNECT", "DISTINCT", "DO", "DOMAIN", "DOUBLE", "DROP", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ + "DYNAMIC", "EACH", "ELEMENT", "ELSE", "ELSEIF", "END", "EQUALS", "ESCAPE", "EXCEPT", "EXCEPTION", "EXEC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "EXECUTE", "EXISTS", "EXIT", "EXTERNAL", "EXTRACT", "FALSE", "FETCH", "FILTER", "FIRST", "FLOAT", "FOR", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "FOREIGN", "FOUND", "FREE", "FROM", "FULL", "FUNCTION", "GENERAL", "GET", "GLOBAL", "GO", "GOTO", "GRANT", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "GROUP", "GROUPING", "HANDLER", "HAVING", "HOLD", "HOUR", "IDENTITY", "IF", "IMMEDIATE", "IN", "INDICATOR", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "INITIALLY", "INNER", "INOUT", "INPUT", "INSENSITIVE", "INSERT", "INT", "INTEGER", "INTERSECT", "INTERVAL", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ + "INTO", "IS", "ISOLATION", "ITERATE", "JOIN", "KEY", "LANGUAGE", "LARGE", "LAST", "LATERAL", "LEADING", "LEAVE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "LEFT", "LEVEL", "LIKE", "LOCAL", "LOCALTIME", "LOCALTIMESTAMP", "LOCATOR", "LOOP", "LOWER", "MAP", "MATCH", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "MAX", "MEMBER", "MERGE", "METHOD", "MIN", "MINUTE", "MODIFIES", "MODULE", "MONTH", "MULTISET", "NAMES", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "NATIONAL", "NATURAL", "NCHAR", "NCLOB", "NEW", "NEXT", "NO", "NONE", "NOT", "NULL", "NULLIF", "NUMERIC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "OBJECT", "OCTET_LENGTH", "OF", "OLD", "ON", "ONLY", "OPEN", "OPTION", "OR", "ORDER", "ORDINALITY", "OUT", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "OUTER", "OUTPUT", "OVER", "OVERLAPS", "PAD", "PARAMETER", "PARTIAL", "PARTITION", "PATH", "POSITION", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ + "PRECISION", "PREPARE", "PRESERVE", "PRIMARY", "PRIOR", "PRIVILEGES", "PROCEDURE", "PUBLIC", "RANGE", "READ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ + "READS", "REAL", "RECURSIVE", "REF", "REFERENCES", "REFERENCING", "RELATIVE", "RELEASE", "REPEAT", "RESIGNAL", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ + "RESTRICT", "RESULT", "RETURN", "RETURNS", "REVOKE", "RIGHT", "ROLE", "ROLLBACK", "ROLLUP", "ROUTINE", "ROW", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "ROWS", "SAVEPOINT", "SCHEMA", "SCOPE", "SCROLL", "SEARCH", "SECOND", "SECTION", "SELECT", "SENSITIVE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ + "SESSION", "SESSION_USER", "SET", "SETS", "SIGNAL", "SIMILAR", "SIZE", "SMALLINT", "SOME", "SPACE", "SPECIFIC", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "SPECIFICTYPE", "SQL", "SQLCODE", "SQLERROR", "SQLEXCEPTION", "SQLSTATE", "SQLWARNING", "START", "STATE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ + "STATIC", "SUBMULTISET", "SUBSTRING", "SUM", "SYMMETRIC", "SYSTEM", "SYSTEM_USER", "TABLE", "TABLESAMPLE", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ + "TEMPORARY", "THEN", "TIME", "TIMESTAMP", "TIMEZONE_HOUR", "TIMEZONE_MINUTE", "TO", "TRAILING", "TRANSACTION", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ + "TRANSLATE", "TRANSLATION", "TREAT", "TRIGGER", "TRIM", "TRUE", "UNDER", "UNDO", "UNION", "UNIQUE", "UNKNOWN", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + "UNNEST", "UNTIL", "UPDATE", "UPPER", "USAGE", "USER", "USING", "VALUE", "VALUES", "VARCHAR", "VARYING", "VIEW", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + "WHEN", "WHENEVER", "WHERE", "WHILE", "WINDOW", "WITH", "WITHIN", "WITHOUT", "WORK", "WRITE", "YEAR", "ZONE" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ + + private String name; + + private String version; + + private Set reservedWords; + + public DBAdapter(String name, String version) + { + this.name = name; + this.version = version; + } + + public String getName() + { + return name; + } + + public String getVersion() + { + return version; + } + + public Set createTables(Iterable tables, Connection connection) throws DBException + { + Set createdTables = new HashSet(); + Statement statement = null; + + try + { + statement = connection.createStatement(); + for (IDBTable table : tables) + { + if (createTable(table, statement)) + { + createdTables.add(table); + } + } + } + catch (SQLException ex) + { + throw new DBException(ex); + } + finally + { + DBUtil.close(statement); + } + + return createdTables; + } + + public boolean createTable(IDBTable table, Statement statement) throws DBException + { + boolean created = true; + + try + { + doCreateTable(table, statement); + } + catch (SQLException ex) + { + created = false; + if (TRACER.isEnabled()) + { + TRACER.trace("-- " + ex.getMessage()); //$NON-NLS-1$ + } + } + + validateTable(table, statement); + return created; + } + + public Collection dropTables(Iterable tables, Connection connection) throws DBException + { + List droppedTables = new ArrayList(); + Statement statement = null; + + try + { + statement = connection.createStatement(); + for (IDBTable table : tables) + { + if (dropTable(table, statement)) + { + droppedTables.add(table); + } + } + } + catch (SQLException ex) + { + OM.LOG.error(ex); + } + finally + { + DBUtil.close(statement); + } + + return droppedTables; + } + + public boolean dropTable(IDBTable table, Statement statement) + { + try + { + String sql = getDropTableSQL(table); + if (TRACER.isEnabled()) + { + TRACER.trace(sql); + } + + statement.execute(sql); + return true; + } + catch (SQLException ex) + { + if (TRACER.isEnabled()) + { + TRACER.trace(ex.getMessage()); + } + + return false; + } + } + + protected String getDropTableSQL(IDBTable table) + { + return "DROP TABLE " + table; //$NON-NLS-1$ + } + + /** + * @since 2.0 + */ + public int getMaxTableNameLength() + { + // Ansi SQL 92 default value + return 128; + } + + /** + * @since 2.0 + */ + public int getMaxFieldNameLength() + { + // Ansi SQL 92 default value + return 128; + } + + public boolean isTypeIndexable(DBType type) + { + switch (type) + { + case CLOB: + case BLOB: + case LONGVARCHAR: + case LONGVARBINARY: + case VARBINARY: + case BINARY: + return false; + + default: + return true; + } + } + + @Override + public String toString() + { + return getName() + "-" + getVersion(); //$NON-NLS-1$ + } + + /** + * @since 2.0 + */ + protected void doCreateTable(IDBTable table, Statement statement) throws SQLException + { + StringBuilder builder = new StringBuilder(); + builder.append("CREATE TABLE "); //$NON-NLS-1$ + builder.append(table); + builder.append(" ("); //$NON-NLS-1$ + appendFieldDefs(builder, table, createFieldDefinitions(table)); + String constraints = createConstraints(table); + if (constraints != null) + { + builder.append(", "); //$NON-NLS-1$ + builder.append(constraints); + } + + builder.append(")"); //$NON-NLS-1$ + String sql = builder.toString(); + if (TRACER.isEnabled()) + { + TRACER.trace(sql); + } + + statement.execute(sql); + + IDBIndex[] indices = table.getIndices(); + for (int i = 0; i < indices.length; i++) + { + createIndex(indices[i], statement, i); + } + } + + /** + * @since 2.0 + */ + 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 "); //$NON-NLS-1$ + builder.append(table); + builder.append("_idx"); //$NON-NLS-1$ + 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); + } + + protected void addIndexField(StringBuilder builder, IDBField field) + { + builder.append(field); + } + + /** + * @since 2.0 + */ + protected String createConstraints(IDBTable table) + { + return null; + } + + /** + * @since 2.0 + */ + protected String createFieldDefinition(IDBField field) + { + return getTypeName(field) + (field.isNotNull() ? " NOT NULL" : ""); //$NON-NLS-1$ //$NON-NLS-2$ + } + + protected String getTypeName(IDBField 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); //$NON-NLS-1$ + } + + public String[] getSQL92ReservedWords() + { + return SQL92_RESERVED_WORDS; + } + + public boolean isReservedWord(String word) + { + if (reservedWords == null) + { + reservedWords = new HashSet(); + for (String reservedWord : getReservedWords()) + { + reservedWords.add(reservedWord.toUpperCase()); + } + } + + word = word.toUpperCase(); + return reservedWords.contains(word); + } + + /** + * @since 2.0 + */ + protected void validateTable(IDBTable table, Statement statement) throws DBException + { + int maxRows = 1; + + try + { + maxRows = statement.getMaxRows(); + statement.setMaxRows(1); + + String sql = null; + + try + { + StringBuilder builder = new StringBuilder(); + builder.append("SELECT "); //$NON-NLS-1$ + appendFieldNames(builder, table); + builder.append(" FROM "); //$NON-NLS-1$ + builder.append(table); + sql = builder.toString(); + + if (TRACER.isEnabled()) + { + TRACER.format("{0}", sql); //$NON-NLS-1$ + } + + ResultSet resultSet = statement.executeQuery(sql); + + try + { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + if (columnCount != table.getFieldCount()) + { + throw new DBException("DBTable " + table + " has " + columnCount + " columns instead of " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + + table.getFieldCount()); + } + } + finally + { + DBUtil.close(resultSet); + } + } + catch (SQLException ex) + { + throw new DBException("Problem with table " + table, ex, sql); + } + finally + { + if (maxRows != 1) + { + statement.setMaxRows(1); + } + } + } + catch (SQLException ex) + { + throw new DBException(ex); + } + } + + private String[] createFieldDefinitions(IDBTable table) + { + IDBField[] fields = table.getFields(); + int fieldCount = fields.length; + + String[] result = new String[fieldCount]; + for (int i = 0; i < fieldCount; i++) + { + IDBField field = fields[i]; + result[i] = createFieldDefinition(field); + } + + return result; + } + + public void appendFieldNames(Appendable appendable, IDBTable table) + { + try + { + IDBField[] fields = table.getFields(); + for (int i = 0; i < fields.length; i++) + { + IDBField field = fields[i]; + if (i != 0) + { + appendable.append(", "); //$NON-NLS-1$ + } + + String fieldName = field.getName(); + appendable.append(fieldName); + } + } + catch (IOException canNotHappen) + { + } + } + + private void appendFieldDefs(Appendable appendable, IDBTable table, String[] defs) + { + try + { + IDBField[] fields = table.getFields(); + for (int i = 0; i < fields.length; i++) + { + IDBField field = fields[i]; + if (i != 0) + { + appendable.append(", "); //$NON-NLS-1$ + } + + // String fieldName = mangleFieldName(field.getName(), 0); + String fieldName = field.getName(); + appendable.append(fieldName); + appendable.append(" "); //$NON-NLS-1$ + appendable.append(defs[i]); + } + } + catch (IOException canNotHappen) + { + } + } + + /** + * @since 3.0 + */ + public DBType adaptType(DBType type) + { + return type; + } + + /** + * @since 4.0 + */ + public boolean isValidFirstChar(char ch) + { + return true; + } + + /** + * @since 4.0 + */ + public boolean isDuplicateKeyException(SQLException ex) + { + /* SQL code for duplicate keys is 23001 */ + return "23001".equals(ex.getSQLState()); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java index ed106df666..dcec747c64 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java @@ -1,234 +1,234 @@ -/* - * 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.spi.db; - -import org.eclipse.net4j.db.DBException; -import org.eclipse.net4j.db.DBUtil; -import org.eclipse.net4j.db.IDBAdapter; -import org.eclipse.net4j.db.IDBConnectionProvider; -import org.eclipse.net4j.db.IDBRowHandler; -import org.eclipse.net4j.db.ddl.IDBSchema; -import org.eclipse.net4j.db.ddl.IDBTable; -import org.eclipse.net4j.internal.db.ddl.DBSchemaElement; -import org.eclipse.net4j.internal.db.ddl.DBTable; - -import javax.sql.DataSource; - -import java.io.PrintStream; -import java.sql.Connection; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -/** - * A useful base class for implementing custom {@link IDBSchema DB schemas}. - * - * @author Eike Stepper - */ -public class DBSchema extends DBSchemaElement implements IDBSchema -{ - private String name; - - private Map tables = new HashMap(); - - private boolean locked; - - public DBSchema(String name) - { - this.name = name; - } - - public String getFullName() - { - return name; - } - - public IDBSchema getSchema() - { - return this; - } - - public String getName() - { - return name; - } - - /** - * @since 2.0 - */ - public IDBTable addTable(String name) throws DBException - { - assertUnlocked(); - if (tables.containsKey(name)) - { - throw new DBException("DBTable exists: " + name); //$NON-NLS-1$ - } - - DBTable table = new DBTable(this, name); - tables.put(name, table); - return table; - } - - /** - * @since 4.0 - */ - public IDBTable removeTable(String name) - { - assertUnlocked(); - return tables.remove(name); - } - - /** - * @since 2.0 - */ - public IDBTable getTable(String name) - { - return tables.get(name); - } - - /** - * @since 2.0 - */ - public IDBTable[] getTables() - { - return tables.values().toArray(new DBTable[tables.size()]); - } - - public boolean isLocked() - { - return locked; - } - - public boolean lock() - { - return locked = true; - } - - public Set create(IDBAdapter dbAdapter, Connection connection) throws DBException - { - return dbAdapter.createTables(tables.values(), connection); - } - - public Set create(IDBAdapter dbAdapter, DataSource dataSource) throws DBException - { - return create(dbAdapter, DBUtil.createConnectionProvider(dataSource)); - } - - public Set create(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException - { - Connection connection = null; - - try - { - connection = connectionProvider.getConnection(); - if (connection == null) - { - throw new DBException("No connection available from " + connectionProvider); //$NON-NLS-1$ - } - - return create(dbAdapter, connection); - } - finally - { - DBUtil.close(connection); - } - } - - public void drop(IDBAdapter dbAdapter, Connection connection) throws DBException - { - dbAdapter.dropTables(tables.values(), connection); - } - - public void drop(IDBAdapter dbAdapter, DataSource dataSource) throws DBException - { - drop(dbAdapter, DBUtil.createConnectionProvider(dataSource)); - } - - public void drop(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException - { - Connection connection = null; - - try - { - connection = connectionProvider.getConnection(); - drop(dbAdapter, connection); - } - finally - { - DBUtil.close(connection); - } - } - - public void export(Connection connection, PrintStream out) throws DBException - { - for (IDBTable table : getTables()) - { - export(table, connection, out); - } - } - - private void export(final IDBTable table, Connection connection, final PrintStream out) - { - if (DBUtil.select(connection, new IDBRowHandler() - { - public boolean handle(int row, Object... values) - { - if (row == 0) - { - String tableName = table.getName(); - out.println(tableName); - for (int i = 0; i < tableName.length(); i++) - { - out.print("="); //$NON-NLS-1$ - } - - out.println(); - } - - out.println(Arrays.asList(values)); - return true; - } - }, table.getFields()) > 0) - - { - out.println(); - } - } - - public void export(DataSource dataSource, PrintStream out) throws DBException - { - export(DBUtil.createConnectionProvider(dataSource), out); - } - - public void export(IDBConnectionProvider connectionProvider, PrintStream out) throws DBException - { - Connection connection = null; - - try - { - connection = connectionProvider.getConnection(); - export(connection, out); - } - finally - { - DBUtil.close(connection); - } - } - - public void assertUnlocked() throws DBException - { - if (locked) - { - throw new DBException("DBSchema locked: " + name); //$NON-NLS-1$ - } - } -} +/* + * 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.spi.db; + +import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.DBUtil; +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.db.IDBConnectionProvider; +import org.eclipse.net4j.db.IDBRowHandler; +import org.eclipse.net4j.db.ddl.IDBSchema; +import org.eclipse.net4j.db.ddl.IDBTable; +import org.eclipse.net4j.internal.db.ddl.DBSchemaElement; +import org.eclipse.net4j.internal.db.ddl.DBTable; + +import javax.sql.DataSource; + +import java.io.PrintStream; +import java.sql.Connection; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * A useful base class for implementing custom {@link IDBSchema DB schemas}. + * + * @author Eike Stepper + */ +public class DBSchema extends DBSchemaElement implements IDBSchema +{ + private String name; + + private Map tables = new HashMap(); + + private boolean locked; + + public DBSchema(String name) + { + this.name = name; + } + + public String getFullName() + { + return name; + } + + public IDBSchema getSchema() + { + return this; + } + + public String getName() + { + return name; + } + + /** + * @since 2.0 + */ + public IDBTable addTable(String name) throws DBException + { + assertUnlocked(); + if (tables.containsKey(name)) + { + throw new DBException("DBTable exists: " + name); //$NON-NLS-1$ + } + + DBTable table = new DBTable(this, name); + tables.put(name, table); + return table; + } + + /** + * @since 4.0 + */ + public IDBTable removeTable(String name) + { + assertUnlocked(); + return tables.remove(name); + } + + /** + * @since 2.0 + */ + public IDBTable getTable(String name) + { + return tables.get(name); + } + + /** + * @since 2.0 + */ + public IDBTable[] getTables() + { + return tables.values().toArray(new DBTable[tables.size()]); + } + + public boolean isLocked() + { + return locked; + } + + public boolean lock() + { + return locked = true; + } + + public Set create(IDBAdapter dbAdapter, Connection connection) throws DBException + { + return dbAdapter.createTables(tables.values(), connection); + } + + public Set create(IDBAdapter dbAdapter, DataSource dataSource) throws DBException + { + return create(dbAdapter, DBUtil.createConnectionProvider(dataSource)); + } + + public Set create(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException + { + Connection connection = null; + + try + { + connection = connectionProvider.getConnection(); + if (connection == null) + { + throw new DBException("No connection available from " + connectionProvider); //$NON-NLS-1$ + } + + return create(dbAdapter, connection); + } + finally + { + DBUtil.close(connection); + } + } + + public void drop(IDBAdapter dbAdapter, Connection connection) throws DBException + { + dbAdapter.dropTables(tables.values(), connection); + } + + public void drop(IDBAdapter dbAdapter, DataSource dataSource) throws DBException + { + drop(dbAdapter, DBUtil.createConnectionProvider(dataSource)); + } + + public void drop(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException + { + Connection connection = null; + + try + { + connection = connectionProvider.getConnection(); + drop(dbAdapter, connection); + } + finally + { + DBUtil.close(connection); + } + } + + public void export(Connection connection, PrintStream out) throws DBException + { + for (IDBTable table : getTables()) + { + export(table, connection, out); + } + } + + private void export(final IDBTable table, Connection connection, final PrintStream out) + { + if (DBUtil.select(connection, new IDBRowHandler() + { + public boolean handle(int row, Object... values) + { + if (row == 0) + { + String tableName = table.getName(); + out.println(tableName); + for (int i = 0; i < tableName.length(); i++) + { + out.print("="); //$NON-NLS-1$ + } + + out.println(); + } + + out.println(Arrays.asList(values)); + return true; + } + }, table.getFields()) > 0) + + { + out.println(); + } + } + + public void export(DataSource dataSource, PrintStream out) throws DBException + { + export(DBUtil.createConnectionProvider(dataSource), out); + } + + public void export(IDBConnectionProvider connectionProvider, PrintStream out) throws DBException + { + Connection connection = null; + + try + { + connection = connectionProvider.getConnection(); + export(connection, out); + } + finally + { + DBUtil.close(connection); + } + } + + public void assertUnlocked() throws DBException + { + if (locked) + { + throw new DBException("DBSchema locked: " + name); //$NON-NLS-1$ + } + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/package-info.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/package-info.java index 4ee766ed38..c17213eb58 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/package-info.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/package-info.java @@ -1,15 +1,15 @@ -/* - * 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 - */ - -/** - * Server service provider interfaces and useful base implementations for the Net4j DB framework. - */ -package org.eclipse.net4j.spi.db; +/* + * 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 + */ + +/** + * Server service provider interfaces and useful base implementations for the Net4j DB framework. + */ +package org.eclipse.net4j.spi.db; -- cgit v1.2.3