package net.sourceforge.phpdt.sql.model; import net.sourceforge.phpdt.sql.bookmarks.Bookmark; import net.sourceforge.phpdt.sql.sql.DbElementsConstants; import net.sourceforge.phpdt.sql.sql.MultiSQLServer; import net.sourceforge.phpdt.sql.sql.SQLHelper; /** * * * @author BC */ public class EntityFactory { public abstract class EntityImpl implements Entity { private String schema; private String name; private String type; private Bookmark bookmark; public EntityImpl(Bookmark bookmark, String schema, String name, String type) { this.schema = schema; this.name = name; this.type = type; this.bookmark = bookmark; } protected Bookmark getBookmark() { return this.bookmark; } public String getName() { return this.name; } public String getSchema() { return this.schema; } public String getType() { return this.type; } public String getQualifiedName() { return (this.schema == null || this.schema.length() == 0) ? this.name : this.schema + "." + this.name; } } public class TableImpl extends EntityImpl implements Table { public TableImpl(Bookmark bookmark, String schema, String name) { super(bookmark, schema, name, DbElementsConstants.Table); } public int getSize() { return SQLHelper.getSize(getBookmark(), getQualifiedName()); } public void deleteAllRows() { String sql = "DELETE FROM " + getQualifiedName(); MultiSQLServer.getInstance().execute(getBookmark().getConnection(), sql); } } public class ViewImpl extends EntityImpl implements View { public ViewImpl(Bookmark bookmark, String schema, String name) { super(bookmark, schema, name, DbElementsConstants.View); } public int getSize() { return SQLHelper.getSize(getBookmark(), getQualifiedName()); } } public class SequenceImpl extends EntityImpl implements Sequence { public SequenceImpl(Bookmark bookmark, String schema, String name) { super(bookmark, schema, name, DbElementsConstants.Sequence); } } private static EntityFactory instance = new EntityFactory(); private EntityFactory() { } public static EntityFactory getInstance() { return EntityFactory.instance; } public Entity create(Bookmark bookmark, String schema, String name, String type) { if (type != null) { type = type.trim(); } if (DbElementsConstants.Table.equals(type)) { return new TableImpl(bookmark, schema, name); } else if (DbElementsConstants.View.equals(type)) { return new ViewImpl(bookmark, schema, name); } else if (DbElementsConstants.Sequence.equals(type)) { return new SequenceImpl(bookmark, schema, name); } else { return null; // throw new IllegalArgumentException("Unknown type: " + type); } } }