Added the PHP wizards again
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / model / EntityFactory.java
1 package net.sourceforge.phpdt.sql.model;
2
3 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
4 import net.sourceforge.phpdt.sql.sql.DbElementsConstants;
5 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
6 import net.sourceforge.phpdt.sql.sql.SQLHelper;
7
8 /**
9  * 
10  * 
11  * @author BC
12  */
13 public class EntityFactory {
14     
15     public abstract class EntityImpl implements Entity { 
16         private String schema;
17         private String name;
18         private String type;
19         private Bookmark bookmark;
20         
21         public EntityImpl(Bookmark bookmark, String schema, String name, String type) {
22             this.schema = schema;
23             this.name = name;
24             this.type = type;
25             this.bookmark = bookmark;
26         }
27         protected Bookmark getBookmark() {
28             return this.bookmark;
29         }
30         public String getName() {
31             return this.name;
32         }
33         public String getSchema() {
34             return this.schema;
35         }
36         public String getType() {
37             return this.type;
38         }
39         public String getQualifiedName() {
40             return (this.schema == null || this.schema.length() == 0) ?
41                 this.name : this.schema + "." + this.name;
42         }
43     }
44     
45     public class TableImpl extends EntityImpl implements Table {
46         public TableImpl(Bookmark bookmark, String schema, String name) {
47             super(bookmark, schema, name, DbElementsConstants.Table);
48         }
49         
50         public int getSize() {
51             return SQLHelper.getSize(getBookmark(), getQualifiedName());
52         }
53         
54         public void deleteAllRows() {
55             String sql = "DELETE FROM " + getQualifiedName();
56             MultiSQLServer.getInstance().execute(getBookmark().getConnection(), sql);
57         }
58     }
59     
60     public class ViewImpl extends EntityImpl implements View {
61         public ViewImpl(Bookmark bookmark, String schema, String name) {
62             super(bookmark, schema, name, DbElementsConstants.View);
63         }
64
65         public int getSize() {
66             return SQLHelper.getSize(getBookmark(), getQualifiedName());
67         }
68     }
69     
70     public class SequenceImpl extends EntityImpl implements Sequence {
71         public SequenceImpl(Bookmark bookmark, String schema, String name) {
72             super(bookmark, schema, name, DbElementsConstants.Sequence);
73         }
74     }
75     
76     private static EntityFactory instance = new EntityFactory();
77     
78     private EntityFactory() {
79     }
80     
81     public static EntityFactory getInstance() {
82         return EntityFactory.instance;
83     }
84     
85     public Entity create(Bookmark bookmark, String schema, String name, String type) {
86         if (type != null) {
87             type = type.trim();
88         }
89         
90         if (DbElementsConstants.Table.equals(type)) {
91             return new TableImpl(bookmark, schema, name);
92         } else if (DbElementsConstants.View.equals(type)) {
93             return new ViewImpl(bookmark, schema, name);
94         } else if (DbElementsConstants.Sequence.equals(type)) {
95             return new SequenceImpl(bookmark, schema, name);
96         } else {
97             return null;
98 //            throw new IllegalArgumentException("Unknown type: " + type);
99         }
100     }
101 }