From 463b6934877e925bcf32ba7885dded61267302c3 Mon Sep 17 00:00:00 2001 From: Eike Stepper Date: Sat, 30 Jun 2007 08:03:46 +0000 Subject: task 1: Develop 0.8.0 --- .../src/org/eclipse/net4j/db/DBException.java | 38 ++++ .../src/org/eclipse/net4j/db/DBUtil.java | 61 ++++++ .../src/org/eclipse/net4j/db/IDBAdapter.java | 34 ++++ .../src/org/eclipse/net4j/db/IField.java | 76 ++++++++ .../src/org/eclipse/net4j/db/IIndex.java | 39 ++++ .../src/org/eclipse/net4j/db/ISchema.java | 29 +++ .../src/org/eclipse/net4j/db/ITable.java | 53 ++++++ .../net4j/internal/db/AbstractDBAdapter.java | 186 +++++++++++++++++++ .../net4j/internal/db/DBAdapterDescriptor.java | 23 +++ .../net4j/internal/db/DBAdapterRegistry.java | 80 ++++++++ .../src/org/eclipse/net4j/internal/db/Field.java | 107 +++++++++++ .../src/org/eclipse/net4j/internal/db/Index.java | 77 ++++++++ .../src/org/eclipse/net4j/internal/db/Schema.java | 109 +++++++++++ .../src/org/eclipse/net4j/internal/db/Table.java | 205 +++++++++++++++++++++ .../org/eclipse/net4j/internal/db/bundle/OM.java | 87 +++++++++ 15 files changed, 1204 insertions(+) create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBException.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBAdapter.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IField.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IIndex.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ISchema.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ITable.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/AbstractDBAdapter.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterDescriptor.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterRegistry.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Field.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Index.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Schema.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Table.java create mode 100644 plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/bundle/OM.java (limited to 'plugins/org.eclipse.net4j.db/src/org/eclipse') 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 new file mode 100644 index 0000000000..2758cb17f1 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBException.java @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +/** + * @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); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java new file mode 100644 index 0000000000..ce4a704040 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +import org.eclipse.net4j.internal.db.Schema; +import org.eclipse.net4j.internal.db.bundle.OM; +import org.eclipse.net4j.util.ReflectUtil; + +import javax.sql.DataSource; + +import java.util.Map; + +/** + * @author Eike Stepper + */ +public final class DBUtil +{ + private DBUtil() + { + } + + public static ISchema createSchema(String name) + { + return new Schema(name); + } + + public static DataSource createDataSource(Map properties) + { + return createDataSource(properties, null); + } + + public static DataSource createDataSource(Map properties, String namespace) + { + return createDataSource(properties, namespace, "driverClass"); + } + + public static DataSource createDataSource(Map properties, String namespace, String driverClassKey) + { + try + { + return (DataSource)ReflectUtil.instantiate(properties, namespace, driverClassKey, OM.class.getClassLoader()); + } + catch (Exception ex) + { + throw new DBException(ex); + } + } + + public static IDBAdapter getDBAdapter(String adapterName) + { + return IDBAdapter.REGISTRY.get(adapterName); + } +} 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 new file mode 100644 index 0000000000..b3ffba055c --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBAdapter.java @@ -0,0 +1,34 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +import org.eclipse.net4j.internal.db.DBAdapterRegistry; +import org.eclipse.net4j.internal.db.Table; +import org.eclipse.net4j.util.registry.IRegistry; + +import java.sql.Driver; +import java.sql.Statement; + +/** + * @author Eike Stepper + */ +public interface IDBAdapter +{ + public static final IRegistry REGISTRY = DBAdapterRegistry.INSTANCE; + + public String getName(); + + public String getVersion(); + + public Driver getJDBCDriver(); + + public void createTable(Table table, Statement statement); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IField.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IField.java new file mode 100644 index 0000000000..d644e6c99d --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IField.java @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +/** + * @author Eike Stepper + */ +public interface IField +{ + public static final int DEFAULT = -1; + + public ITable geTable(); + + public String getName(); + + public Type getType(); + + public int getPrecision(); + + public int getScale(); + + public boolean isNotNull(); + + public int getPosition(); + + public String getFullName(); + + /** + * @author Eike Stepper + */ + public enum Type + { + BOOLEAN(16), // + BIT(-7), // + TINYINT(-6), // + SMALLINT(5), // + INTEGER(4), // + BIGINT(-5), // + FLOAT(6), // + REAL(7), // + DOUBLE(8), // + NUMERIC(2), // + DECIMAL(3), // + CHAR(1), // + VARCHAR(12), // + LONGVARCHAR(-1), // + DATE(91), // + TIME(92), // + TIMESTAMP(93), // + BINARY(-2), // + VARBINARY(-3), // + LONGVARBINARY(-4), // + BLOB(2004), // + CLOB(2005); // + + private int code; + + private Type(int code) + { + this.code = code; + } + + public int getCode() + { + return code; + } + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IIndex.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IIndex.java new file mode 100644 index 0000000000..6347ff7345 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IIndex.java @@ -0,0 +1,39 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +/** + * @author Eike Stepper + */ +public interface IIndex +{ + public ITable geTable(); + + public Type geType(); + + public IField getField(int index); + + public int getFieldCount(); + + public IField[] getFields(); + + public int getPosition(); + + public String getName(); + + /** + * @author Eike Stepper + */ + public enum Type + { + PRIMARY_KEY, UNIQUE, NON_UNIQUE + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ISchema.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ISchema.java new file mode 100644 index 0000000000..36515c8132 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ISchema.java @@ -0,0 +1,29 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +import javax.sql.DataSource; + +/** + * @author Eike Stepper + */ +public interface ISchema +{ + public String getName(); + + public ITable addTable(String name); + + public ITable getTable(String name); + + public ITable[] getTables(); + + public void create(IDBAdapter dbAdapter, DataSource dataSource); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ITable.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ITable.java new file mode 100644 index 0000000000..221b192133 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ITable.java @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.db; + +import org.eclipse.net4j.internal.db.Field; + +/** + * @author Eike Stepper + */ +public interface ITable +{ + public ISchema getSchema(); + + public String getName(); + + public IField addField(String name, IField.Type type); + + public Field addField(String name, IField.Type type, boolean notNull); + + public Field addField(String name, IField.Type type, int precision); + + public Field addField(String name, IField.Type type, int precision, boolean notNull); + + public Field addField(String name, IField.Type type, int precision, int scale); + + public Field addField(String name, IField.Type type, int precision, int scale, boolean notNull); + + public IField getField(String name); + + public IField getField(int index); + + public int getFieldCount(); + + public IField[] getFields(); + + public IIndex addIndex(IIndex.Type type, IField field); + + public IIndex addIndex(IIndex.Type type, IField[] fields); + + public int getIndexCount(); + + public IIndex[] getIndices(); + + public IIndex getPrimaryKeyIndex(); +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/AbstractDBAdapter.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/AbstractDBAdapter.java new file mode 100644 index 0000000000..552e77f15d --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/AbstractDBAdapter.java @@ -0,0 +1,186 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db; + +import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.db.IField.Type; + +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * @author Eike Stepper + */ +public abstract class AbstractDBAdapter implements IDBAdapter +{ + private String name; + + private String version; + + public AbstractDBAdapter(String name, String version) + { + this.name = name; + this.version = version; + } + + public String getName() + { + return name; + } + + public String getVersion() + { + return version; + } + + public void createTable(Table table, Statement statement) + { + try + { + doCreateTable(table, statement); + } + catch (SQLException ex) + { + System.out.println(ex.getMessage()); + } + + validateTable(table, statement); + } + + @Override + public String toString() + { + return getName() + "-" + getVersion(); + } + + protected void doCreateTable(Table table, Statement statement) throws SQLException + { + StringBuilder builder = new StringBuilder(); + builder.append("CREATE TABLE "); + builder.append(table.getName()); + builder.append(" ("); + table.appendFieldDefs(builder, createFieldDefinitions(table)); + String constraints = createConstraints(table); + if (constraints != null) + { + builder.append(", "); + builder.append(constraints); + } + + builder.append(")"); + String sql = builder.toString(); + System.out.println(sql); + statement.execute(sql); + } + + protected String createConstraints(Table table) + { + return null; + } + + protected String createFieldDefinition(Field field) + { + return getTypeName(field) + (field.isNotNull() ? " NOT NULL" : ""); + } + + protected String getTypeName(Field field) + { + Type 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 BLOB: + case CLOB: + return type.toString(); + + case CHAR: + case VARCHAR: + case LONGVARCHAR: + case BINARY: + case VARBINARY: + case LONGVARBINARY: + return type.toString() + field.formatPrecision(); + + case NUMERIC: + case DECIMAL: + return type.toString() + field.formatPrecisionAndScale(); + } + + throw new IllegalArgumentException("Unknown type: " + type); + } + + protected void validateTable(Table table, Statement statement) + { + try + { + StringBuilder builder = new StringBuilder(); + builder.append("SELECT "); + table.appendFieldNames(builder); + builder.append(" FROM "); + builder.append(table.getName()); + String sql = builder.toString(); + + ResultSet resultSet = statement.executeQuery(sql); + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + if (columnCount != table.getFieldCount()) + { + throw new DBException("Table " + table.getName() + " has " + columnCount + " columns instead of " + + table.getFieldCount()); + } + + for (int i = 0; i < columnCount; i++) + { + int existingCode = metaData.getColumnType(i + 1); + Field field = table.getField(i); + int code = field.getType().getCode(); + if (code != existingCode) + { + throw new DBException("Field " + field.getFullName() + " has type " + existingCode + " instead of " + code + + " (" + field.getType() + ")"); + } + } + } + catch (SQLException ex) + { + throw new DBException(ex.getMessage()); + } + } + + private String[] createFieldDefinitions(Table table) + { + Field[] fields = table.getFields(); + int fieldCount = fields.length; + + String[] result = new String[fieldCount]; + for (int i = 0; i < fieldCount; i++) + { + Field field = fields[i]; + result[i] = createFieldDefinition(field); + } + + return result; + } +} 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 new file mode 100644 index 0000000000..c6e0ace47b --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterDescriptor.java @@ -0,0 +1,23 @@ +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(); +} \ No newline at end of file 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 new file mode 100644 index 0000000000..5bb1b26b20 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBAdapterRegistry.java @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db; + +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.internal.util.registry.HashMapRegistry; +import org.eclipse.net4j.util.registry.IRegistry; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Eike Stepper + */ +public class DBAdapterRegistry extends HashMapRegistry implements IRegistry +{ + 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/Field.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Field.java new file mode 100644 index 0000000000..461d41c8ea --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Field.java @@ -0,0 +1,107 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db; + +import org.eclipse.net4j.db.IField; + +/** + * @author Eike Stepper + */ +public class Field implements IField +{ + private static final int DEFAULT_PRECISION = 255; + + private Table table; + + private String name; + + private Type type; + + private int precision; + + private int scale; + + private boolean notNull; + + private int position; + + public Field(Table table, String name, Type 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 Table geTable() + { + return table; + } + + public String getName() + { + return name; + } + + public Type getType() + { + return type; + } + + public int getPrecision() + { + return precision == DEFAULT ? DEFAULT_PRECISION : precision; + } + + public int getScale() + { + return scale; + } + + public boolean isNotNull() + { + return notNull; + } + + public int getPosition() + { + return position; + } + + public String getFullName() + { + return table.getName() + "." + name; + } + + @Override + public String toString() + { + return getFullName(); + } + + public String formatPrecision() + { + return "(" + getPrecision() + ")"; + } + + public String formatPrecisionAndScale() + { + if (scale == DEFAULT) + { + return "(" + getPrecision() + ")"; + } + + return "(" + getPrecision() + ", " + scale + ")"; + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Index.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Index.java new file mode 100644 index 0000000000..a950e600df --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Index.java @@ -0,0 +1,77 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db; + +import org.eclipse.net4j.db.IField; +import org.eclipse.net4j.db.IIndex; + +/** + * @author Eike Stepper + */ +public class Index implements IIndex +{ + private Table table; + + private Type type; + + private IField[] fields; + + public int position; + + public Index(Table table, Type type, IField[] fields, int position) + { + this.table = table; + this.type = type; + this.fields = fields; + this.position = position; + } + + public Table geTable() + { + return table; + } + + public Type geType() + { + return type; + } + + public IField getField(int index) + { + return fields[index]; + } + + public int getFieldCount() + { + return fields.length; + } + + public IField[] getFields() + { + return fields; + } + + public int getPosition() + { + return position; + } + + public String getName() + { + return "idx_" + table.getName() + "_" + position; + } + + @Override + public String toString() + { + return getName(); + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Schema.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Schema.java new file mode 100644 index 0000000000..04bd7dfbac --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Schema.java @@ -0,0 +1,109 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db; + +import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.db.ISchema; + +import javax.sql.DataSource; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Eike Stepper + */ +public class Schema implements ISchema +{ + private String name; + + private Map tables = new HashMap(); + + private boolean locked; + + public Schema(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + + public Table addTable(String name) + { + assertUnlocked(); + if (tables.containsKey(name)) + { + throw new IllegalStateException("Table exists: " + name); + } + + Table table = new Table(this, name); + tables.put(name, table); + return table; + } + + public Table getTable(String name) + { + return tables.get(name); + } + + public Table[] getTables() + { + return tables.values().toArray(new Table[tables.size()]); + } + + public boolean isLocked() + { + return locked; + } + + public boolean lock() + { + return locked = true; + } + + public void create(IDBAdapter dbAdapter, DataSource dataSource) + { + try + { + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement(); + for (Table table : tables.values()) + { + dbAdapter.createTable(table, statement); + } + } + catch (SQLException ex) + { + throw new DBException(ex); + } + } + + @Override + public String toString() + { + return name; + } + + void assertUnlocked() + { + if (locked) + { + throw new IllegalStateException("Schema locked: " + name); + } + } +} diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Table.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Table.java new file mode 100644 index 0000000000..f119586bd8 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/Table.java @@ -0,0 +1,205 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db; + +import org.eclipse.net4j.db.IField; +import org.eclipse.net4j.db.IIndex; +import org.eclipse.net4j.db.ITable; +import org.eclipse.net4j.db.IIndex.Type; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eike Stepper + */ +public class Table implements ITable +{ + private Schema schema; + + private String name; + + private List fields = new ArrayList(); + + private List indices = new ArrayList(); + + public Table(Schema schema, String name) + { + this.schema = schema; + this.name = name; + } + + public Schema getSchema() + { + return schema; + } + + public String getName() + { + return name; + } + + public Field addField(String name, IField.Type type) + { + return addField(name, type, IField.DEFAULT, IField.DEFAULT, false); + } + + public Field addField(String name, IField.Type type, boolean notNull) + { + return addField(name, type, IField.DEFAULT, IField.DEFAULT, notNull); + } + + public Field addField(String name, IField.Type type, int precision) + { + return addField(name, type, precision, IField.DEFAULT, false); + } + + public Field addField(String name, IField.Type type, int precision, boolean notNull) + { + return addField(name, type, precision, IField.DEFAULT, notNull); + } + + public Field addField(String name, IField.Type type, int precision, int scale) + { + return addField(name, type, precision, scale, false); + } + + public Field addField(String name, IField.Type type, int precision, int scale, boolean notNull) + { + schema.assertUnlocked(); + if (getField(name) != null) + { + throw new IllegalStateException("Field exists: " + name); + } + + Field field = new Field(this, name, type, precision, scale, notNull, fields.size()); + fields.add(field); + return field; + } + + public Field getField(String name) + { + for (Field field : fields) + { + if (name.equals(field.getName())) + { + return field; + } + } + + return null; + } + + public Field getField(int index) + { + return fields.get(index); + } + + public int getFieldCount() + { + return fields.size(); + } + + public Field[] getFields() + { + return fields.toArray(new Field[fields.size()]); + } + + public Index addIndex(Type type, IField field) + { + IField[] fields = { field }; + return addIndex(type, fields); + } + + public Index addIndex(Type type, IField[] fields) + { + schema.assertUnlocked(); + Index index = new Index(this, type, fields, indices.size()); + indices.add(index); + return index; + } + + public int getIndexCount() + { + return indices.size(); + } + + public Index[] getIndices() + { + return indices.toArray(new Index[indices.size()]); + } + + public IIndex getPrimaryKeyIndex() + { + for (IIndex index : indices) + { + if (index.geType() == IIndex.Type.PRIMARY_KEY) + { + return index; + } + } + + return null; + } + + public void appendFieldNames(Appendable appendable) + { + try + { + boolean first = true; + for (Field field : fields) + { + if (first) + { + first = false; + } + else + { + appendable.append(", "); + } + + appendable.append(field.getName()); + } + } + catch (IOException canNotHappen) + { + } + } + + public void appendFieldDefs(Appendable appendable, String[] defs) + { + try + { + for (int i = 0; i < fields.size(); i++) + { + Field field = fields.get(i); + if (i != 0) + { + appendable.append(", "); + } + + appendable.append(field.getName()); + appendable.append(" "); + appendable.append(defs[i]); + } + } + catch (IOException canNotHappen) + { + } + } + + @Override + public String toString() + { + return name; + } +} 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 new file mode 100644 index 0000000000..97ce7f3099 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/bundle/OM.java @@ -0,0 +1,87 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2007 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.internal.db.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.internal.util.om.OSGiActivator; +import org.eclipse.net4j.util.om.OMBundle; +import org.eclipse.net4j.util.om.OMLogger; +import org.eclipse.net4j.util.om.OMPlatform; +import org.eclipse.net4j.util.om.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; + +/** + * @author Eike Stepper + */ +public final 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"; + + private OM() + { + } + + public static final class Activator extends OSGiActivator + { + @Override + protected OMBundle getOMBundle() + { + return BUNDLE; + } + + @Override + protected void start() throws Exception + { + IExtensionRegistry registry = Platform.getExtensionRegistry(); + IConfigurationElement[] elements = registry.getConfigurationElementsFor(BUNDLE_ID, EXT_POINT); + for (final IConfigurationElement element : elements) + { + if ("dbAdapter".equals(element.getName())) + { + DBAdapterDescriptor descriptor = new DBAdapterDescriptor(element.getAttribute("name")) + { + @Override + public IDBAdapter createDBAdapter() + { + try + { + return (IDBAdapter)element.createExecutableExtension("class"); + } + catch (CoreException ex) + { + OM.LOG.error(ex); + return null; + } + } + }; + + DBAdapterRegistry.INSTANCE.addDescriptor(descriptor); + } + } + } + } +} -- cgit v1.2.3