2bf17ccc45be022b33ec51833acd17aea9d2dc27
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / tableview / TableAdapter.java
1 package net.sourceforge.phpdt.sql.view.tableview;
2
3 import java.util.Vector;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.widgets.Table;
7 import org.eclipse.swt.widgets.TableColumn;
8 import org.eclipse.swt.widgets.TableItem;
9
10 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
11 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
12 import net.sourceforge.phpdt.sql.adapters.DatabaseInfo;
13 import net.sourceforge.phpdt.sql.adapters.NoSuchAdapterException;
14 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
15 import net.sourceforge.phpdt.sql.sql.FilterSort;
16 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
17 import net.sourceforge.phpdt.sql.sql.SQLHelper;
18 import net.sourceforge.phpdt.sql.sql.SQLResults;
19
20 public class TableAdapter {
21         public static final String DEFAULT = "";
22         public static final String UTF_8 = "UTF-8";
23         public static final String UTF_16 = "UTF-16";
24         
25         private int pageSize = DefaultSizes.PAGE_SIZE;
26         private int maxColumnSize = DefaultSizes.MAX_COLUMN_SIZE;
27         private SQLHelper helper = new SQLHelper();
28         private FilterSort extra = new FilterSort();
29         private int offset = 1;
30         private int totalSize = -1;
31         private Vector rows = new Vector();
32         private Vector columnNames = new Vector();
33         private boolean hasMore = false;
34                 
35         private String table;
36         private String query;
37         
38         private String encoding = "";
39         
40         private TableAdapter() {
41         }
42         public void fullMode() {
43                 offset = 1;
44                 pageSize = Integer.MAX_VALUE;
45         }
46         public void resetMode() {
47                 offset = 1;
48                 pageSize = DefaultSizes.PAGE_SIZE;
49         }
50         public static TableAdapter createFromQuery(SQLResults results) {
51                 TableAdapter retVal = new TableAdapter();
52                 retVal.setQuery(results.getQuery());
53                 retVal.setData(results);
54                 return retVal;
55         }
56         public static TableAdapter createFromTable(String table) {
57                 TableAdapter retVal = new TableAdapter();
58                 retVal.setTable(table);
59                 return retVal;
60         }
61         private void loadSize() {
62                 if (table != null) {
63                         totalSize = helper.getSize(getTableCountQuery());
64                 }
65         }
66         public int getStartIndex() {
67                 if (totalSize == 0) {
68                         return 0;
69                 }
70                 return offset;
71         }
72         public int getEndIndex() {
73                 return offset + rows.size() - 1;
74         }
75         public int getTotalSize() {
76                 return totalSize;
77         }
78         public void nextPage() {
79                 loadSize();
80                 offset = offset + pageSize;
81                 if (totalSize >= 0 && offset > totalSize) {
82                         offset = offset - pageSize;
83                 }
84         }
85         public void previousPage() {
86                 offset = offset - pageSize;
87                 if (offset < 1) {
88                         offset = 1;
89                 }
90         }
91         public boolean hasNextPage() {
92                 if (table != null) {
93                         if (offset + pageSize <= totalSize) {
94                                 return true;
95                         }
96                         return false;
97                 }
98                 return hasMore;
99         }
100         public boolean hasPreviousPage() {
101                 if (offset > 1) {
102                         return true;
103                 }
104                 return false;
105         }
106         public String getTableCountQuery() {
107                 if (table != null) {
108                         Bookmark current = MultiSQLServer.getInstance().getConnected();
109                         return SQLHelper.getFullTableName(current, table);
110                 }
111                 return query;
112         }
113         public String getQuery() {
114                 if (table != null) {
115                         MultiSQLServer server = MultiSQLServer.getInstance();
116                         Bookmark current = server.getConnected();
117                         try {
118                                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(current.getType());
119                                 return adapter.getShowTableQuery(DatabaseInfo.create(current), table) + extra.toString();
120                         } catch (NoSuchAdapterException e) {
121                                 throw new RuntimeException(e.toString());
122                         }
123                 }
124                 return query;
125         }
126         public void loadData() {
127                 loadSize();
128                 if (table != null) {
129                         if (offset > totalSize) {
130                                 offset = 1;
131                         }
132                 }
133                 String query = getQuery();
134                 System.out.println(offset + " to " + (offset + pageSize - 1));
135                 SQLResults results = helper.getResults(query, offset, offset + pageSize - 1, maxColumnSize, encoding);
136                 setData(results);
137         }
138         public void resetOffset() {
139                 offset = 1;
140         }
141         public void setData(SQLResults results) {
142                 int rowCount = results.getRowCount();
143                 int columnCount = results.getColumnCount();
144                 rows = new Vector();
145                 columnNames = new Vector();
146                 for (int col = 1; col <= columnCount; col++) {
147                         columnNames.addElement(results.getColumnName(col));
148                 }
149                 for (int row = 1; row <= rowCount; row++) {
150                         String rowData[] = new String[columnCount];
151                         for (int col = 1; col <= columnCount; col++) {
152                                 rowData[col - 1] = results.getElement(col, row).toString();
153                         }
154                         rows.addElement(rowData);
155                 }
156                 hasMore = results.hasMore();
157                 if (table == null && results.getMaxSize() >= 0) {
158                         if (offset > results.getMaxSize()) {
159                                 offset = 1;
160                                 loadData();
161                         }
162                 }
163         }
164         public void loadTable(Table table) {
165                 table.setHeaderVisible(true);
166                 for (int i = 0; i < columnNames.size(); i++) {
167                         TableColumn column = new TableColumn(table, SWT.NONE);
168                         column.setText(columnNames.elementAt(i).toString());
169                 }
170                 for (int i = 0; i < columnNames.size(); i++) {
171                         table.getColumn(i).pack();
172                 }
173                 for (int row = 0; row < rows.size(); row++) {
174                         TableItem item = new TableItem(table, SWT.NONE);
175                         String itemData[] = (String[]) rows.elementAt(row);
176                         item.setText(itemData);
177                 }
178                 for (int i = 0; i < columnNames.size(); i++) {
179                         table.getColumn(i).pack();
180                 }
181         }
182         
183         public int getPageSize() {
184                 return pageSize;
185         }
186         
187         public void setFilterSort(FilterSort extra) {
188                 this.extra = extra;
189         }
190         public String getTable() {
191                 return table;
192         }
193
194         public void setTable(String table) {
195                 this.table = table;
196         }
197         public void setQuery(String query) {
198                 this.query = query;
199         }
200         public String getEncoding() {
201                 return encoding;
202         }
203
204         public void setEncoding(String encoding) {
205                 this.encoding = encoding;
206         }
207         public String getStatusString() {
208                 String status = getStartIndex() + " to " + getEndIndex() + " of " +
209                                 getTotalSize();
210                 if (!encoding.equals(DEFAULT)) {
211                         status += " (" + encoding + ")";
212                 }
213                 String filterText = extra.toString();
214                 if (!filterText.equals("")) {
215                         status += " (" + filterText + ")";
216                 }
217                 if (pageSize == Integer.MAX_VALUE) {
218                         status += " (FULL)";
219                 }
220                 return status;
221         }
222 }