1 package net.sourceforge.phpdt.sql.sql;
3 import java.util.Vector;
5 import net.sourceforge.phpdt.sql.IConstants;
6 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
7 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
8 import net.sourceforge.phpdt.sql.adapters.DatabaseInfo;
9 import net.sourceforge.phpdt.sql.adapters.FeatureNotSupported;
10 import net.sourceforge.phpdt.sql.adapters.GenericAdapter;
11 import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException;
12 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
13 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
14 import net.sourceforge.phpdt.sql.view.bookmark.MetaDataNode;
15 import net.sourceforge.phpdt.sql.view.bookmark.SequenceNode;
16 import net.sourceforge.phpdt.sql.view.bookmark.TableNode;
17 import net.sourceforge.phpdt.sql.view.bookmark.ViewNode;
19 public class SQLHelper implements IConstants {
20 MultiSQLServer server = MultiSQLServer.getInstance();
21 public Vector getTableList(BookmarkNode current)
22 throws NoSuchAdapterException, FeatureNotSupported {
23 DatabaseAdapter adapter =
24 AdapterFactory.getInstance().getAdapter(current.getType());
25 Vector children = new Vector();
26 DatabaseInfo info = DatabaseInfo.create(current);
27 if (adapter instanceof GenericAdapter) {
28 Vector tables = server.listTables(info.getSchema(), "TABLE");
29 for (int i = 0; i < tables.size(); i++) {
30 String tableName = (String) tables.elementAt(i);
31 TableNode node = new TableNode(current, tableName);
32 children.addElement(node);
35 SQLResults results = server.execute(adapter.getShowTableQuery(info));
36 String filter = adapter.getTableListFilter();
37 int size = results.getRowCount();
39 System.out.println("Found " + size + " tables");
41 for (int i = 1; i <= size; i++) {
42 String tableName = results.getElement(1, i).toString();
45 || tableName.indexOf(filter) < 0) {
46 TableNode node = new TableNode(current, tableName);
47 children.addElement(node);
53 public int getSize(String query) {
54 SQLResults results = server.execute("SELECT COUNT(*) FROM " + query);
55 return Integer.parseInt(results.getElement(1, 1).toString());
57 public Vector getViewList(BookmarkNode current)
58 throws NoSuchAdapterException, FeatureNotSupported {
59 DatabaseAdapter adapter =
60 AdapterFactory.getInstance().getAdapter(current.getType());
61 Vector children = new Vector();
62 DatabaseInfo info = DatabaseInfo.create(current);
63 if (adapter instanceof GenericAdapter) {
64 Vector views = server.listTables(info.getSchema(), "VIEW");
65 for (int i = 0; i < views.size(); i++) {
66 String viewName = (String) views.elementAt(i);
67 ViewNode node = new ViewNode(current, viewName);
68 children.addElement(node);
71 SQLResults results = server.execute(adapter.getShowViewQuery(info));
72 String filter = adapter.getTableListFilter();
73 int size = results.getRowCount();
74 for (int i = 1; i <= size; i++) {
75 String viewName = results.getElement(1, i).toString();
78 || viewName.indexOf(filter) < 0) {
79 ViewNode node = new ViewNode(current, viewName);
80 children.addElement(node);
86 public Vector getSequenceList(BookmarkNode current)
87 throws NoSuchAdapterException, FeatureNotSupported {
88 DatabaseAdapter adapter =
89 AdapterFactory.getInstance().getAdapter(current.getType());
90 MultiSQLServer server = MultiSQLServer.getInstance();
91 Vector children = new Vector();
92 if (!(adapter instanceof GenericAdapter)) {
95 adapter.getShowSequenceQuery(DatabaseInfo.create(current)));
96 String filter = adapter.getTableListFilter();
97 int size = results.getRowCount();
98 for (int i = 1; i <= size; i++) {
99 String seqName = results.getElement(1, i).toString();
102 || seqName.indexOf(filter) < 0) {
103 SequenceNode node = new SequenceNode(current, seqName);
104 children.addElement(node);
110 public Vector getMetaData(TableNode parent, String entity) {
111 Vector columns = new Vector();
112 String query = "SELECT * FROM " + entity;
113 SQLResults results = server.execute(query);
114 for (int i = 1; i <= results.getColumnCount(); i++) {
115 String name = results.getColumnName(i);
116 String type = results.getColumnType(i);
117 MetaDataNode node = new MetaDataNode(parent, name, type);
118 columns.addElement(node);
122 public Vector getMetaData(ViewNode parent, String entity) {
123 Vector columns = new Vector();
124 String query = "SELECT * FROM " + entity;
125 SQLResults results = server.execute(query);
126 for (int i = 1; i <= results.getColumnCount(); i++) {
127 String name = results.getColumnName(i);
128 String type = results.getColumnType(i);
129 MetaDataNode node = new MetaDataNode(parent, name, type);
130 columns.addElement(node);
134 public SQLResults getResults(String query, int start, int end) {
135 return server.execute(query, start, end);
137 public SQLResults getResults(
143 return server.execute(query, start, end, maxLength, encoding);
145 public static String getFullTableName(Bookmark current, String table) {
146 String schema = current.getSchema();
147 if (schema == null || schema.equals("")) {
150 return schema + "." + table;