diff options
Diffstat (limited to 'plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTable.java')
-rw-r--r-- | plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTable.java | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTable.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTable.java new file mode 100644 index 0000000000..3c15211f7f --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTable.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.IDBField; +import org.eclipse.net4j.db.IDBIndex; +import org.eclipse.net4j.db.IDBTable; +import org.eclipse.net4j.db.IDBIndex.Type; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eike Stepper + */ +public class DBTable implements IDBTable +{ + private DBSchema schema; + + private String name; + + private List<DBField> fields = new ArrayList(); + + private List<DBIndex> 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, IDBField.Type type) + { + return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, false); + } + + public DBField addField(String name, IDBField.Type type, boolean notNull) + { + return addField(name, type, IDBField.DEFAULT, IDBField.DEFAULT, notNull); + } + + public DBField addField(String name, IDBField.Type type, int precision) + { + return addField(name, type, precision, IDBField.DEFAULT, false); + } + + public DBField addField(String name, IDBField.Type type, int precision, boolean notNull) + { + return addField(name, type, precision, IDBField.DEFAULT, notNull); + } + + public DBField addField(String name, IDBField.Type type, int precision, int scale) + { + return addField(name, type, precision, scale, false); + } + + public DBField addField(String name, IDBField.Type type, int precision, int scale, boolean notNull) + { + schema.assertUnlocked(); + if (getField(name) != null) + { + throw new IllegalStateException("DBField exists: " + name); + } + + 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 field) + { + IDBField[] fields = { field }; + return addIndex(type, fields); + } + + 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.geType() == IDBIndex.Type.PRIMARY_KEY) + { + return index; + } + } + + return null; + } + + public void appendFieldNames(Appendable appendable) + { + try + { + boolean first = true; + for (DBField 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++) + { + DBField 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; + } +} |