1 package net.sourceforge.phpdt.sql.sql;
3 import java.util.Vector;
5 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
6 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
7 import net.sourceforge.phpdt.sql.adapters.DatabaseInfo;
8 import net.sourceforge.phpdt.sql.adapters.FeatureNotSupported;
9 import net.sourceforge.phpdt.sql.adapters.GenericAdapter;
10 import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException;
11 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
12 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
13 import net.sourceforge.phpdt.sql.view.bookmark.MetaDataNode;
14 import net.sourceforge.phpdt.sql.view.bookmark.SequenceNode;
15 import net.sourceforge.phpdt.sql.view.bookmark.TableNode;
16 import net.sourceforge.phpdt.sql.view.bookmark.ViewNode;
18 public class SQLHelper {
19 MultiSQLServer server = MultiSQLServer.getInstance();
20 public Vector getTableList(BookmarkNode current) throws NoSuchAdapterException, FeatureNotSupported {
21 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(current.getType());
22 Vector children = new Vector();
23 DatabaseInfo info = DatabaseInfo.create(current);
24 if (adapter instanceof GenericAdapter) {
25 Vector tables = server.listTables(info.getSchema(), "TABLE");
26 for (int i = 0; i < tables.size(); i++) {
27 String tableName = (String) tables.elementAt(i);
28 TableNode node = new TableNode(current, tableName);
29 children.addElement(node);
32 SQLResults results = server.execute(adapter.getShowTableQuery(info));
33 String filter = adapter.getTableListFilter();
34 int size = results.getRowCount();
35 System.out.println("Found " + size + " tables");
36 for (int i = 1; i <= size; i++) {
37 String tableName = results.getElement(1, i).toString();
38 if (filter == null || filter.equals("") || tableName.indexOf(filter) < 0) {
39 TableNode node = new TableNode(current, tableName);
40 children.addElement(node);
46 public int getSize(String query) {
47 SQLResults results = server.execute("SELECT COUNT(*) FROM " + query);
48 return Integer.parseInt(results.getElement(1, 1).toString());
50 public Vector getViewList(BookmarkNode current)
51 throws NoSuchAdapterException, FeatureNotSupported {
52 DatabaseAdapter adapter =
53 AdapterFactory.getInstance().getAdapter(current.getType());
54 Vector children = new Vector();
55 DatabaseInfo info = DatabaseInfo.create(current);
56 if (adapter instanceof GenericAdapter) {
57 Vector views = server.listTables(info.getSchema(), "VIEW");
58 for (int i = 0; i < views.size(); i++) {
59 String viewName = (String) views.elementAt(i);
60 ViewNode node = new ViewNode(current, viewName);
61 children.addElement(node);
66 adapter.getShowViewQuery(info));
67 String filter = adapter.getTableListFilter();
68 int size = results.getRowCount();
69 for (int i = 1; i <= size; i++) {
70 String viewName = results.getElement(1, i).toString();
73 || viewName.indexOf(filter) < 0) {
74 ViewNode node = new ViewNode(current, viewName);
75 children.addElement(node);
81 public Vector getSequenceList(BookmarkNode current) throws NoSuchAdapterException, FeatureNotSupported {
82 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(current.getType());
83 MultiSQLServer server = MultiSQLServer.getInstance();
84 Vector children = new Vector();
85 if (!(adapter instanceof GenericAdapter)) {
86 SQLResults results = server.execute(adapter.getShowSequenceQuery(DatabaseInfo.create(current)));
87 String filter = adapter.getTableListFilter();
88 int size = results.getRowCount();
89 for (int i = 1; i <= size; i++) {
90 String seqName = results.getElement(1, i).toString();
91 if (filter == null || filter.equals("") || seqName.indexOf(filter) < 0) {
92 SequenceNode node = new SequenceNode(current, seqName);
93 children.addElement(node);
99 public Vector getMetaData(TableNode parent, String entity) {
100 Vector columns = new Vector();
101 String query = "SELECT * FROM " + entity;
102 SQLResults results = server.execute(query);
103 for (int i = 1; i <= results.getColumnCount(); i++) {
104 String name = results.getColumnName(i);
105 String type = results.getColumnType(i);
106 MetaDataNode node = new MetaDataNode(parent, name, type);
107 columns.addElement(node);
111 public Vector getMetaData(ViewNode parent, String entity) {
112 Vector columns = new Vector();
113 String query = "SELECT * FROM " + entity;
114 SQLResults results = server.execute(query);
115 for (int i = 1; i <= results.getColumnCount(); i++) {
116 String name = results.getColumnName(i);
117 String type = results.getColumnType(i);
118 MetaDataNode node = new MetaDataNode(parent, name, type);
119 columns.addElement(node);
123 public SQLResults getResults(String query, int start, int end) {
124 return server.execute(query, start, end);
126 public SQLResults getResults(String query, int start, int end, int maxLength, String encoding) {
127 return server.execute(query, start, end, maxLength, encoding);
129 public static String getFullTableName(Bookmark current, String table) {
130 String schema = current.getSchema();
131 if (schema == null || schema.equals("")) {
134 return schema + "." + table;