1 package net.sourceforge.phpdt.sql.view.tableview;
3 import java.util.Vector;
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;
10 import net.sourceforge.phpdt.sql.Messages;
11 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
12 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
13 import net.sourceforge.phpdt.sql.adapters.DatabaseInfo;
14 import net.sourceforge.phpdt.sql.sql.FilterSort;
15 import net.sourceforge.phpdt.sql.sql.SQLHelper;
16 import net.sourceforge.phpdt.sql.sql.SQLResults;
17 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
19 public class TableAdapter {
20 public static final String DEFAULT = ""; //$NON-NLS-1$
21 public static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
22 public static final String UTF_16 = "UTF-16"; //$NON-NLS-1$
24 private int pageSize = DefaultSizes.PAGE_SIZE;
25 private int maxColumnSize = DefaultSizes.MAX_COLUMN_SIZE;
26 private FilterSort extra = new FilterSort();
27 private int offset = 1;
28 private int totalSize = -1;
29 private Vector rows = new Vector();
30 private Vector columnNames = new Vector();
31 private boolean hasMore = false;
33 private BookmarkNode bookmark = null;
37 private String encoding = ""; //$NON-NLS-1$
39 private TableAdapter(BookmarkNode bookmark) {
40 this.bookmark = bookmark;
42 public void fullMode() {
44 pageSize = Integer.MAX_VALUE;
46 public void resetMode() {
48 pageSize = DefaultSizes.PAGE_SIZE;
50 public static TableAdapter createFromQuery(BookmarkNode bookmark, SQLResults results) {
51 TableAdapter retVal = new TableAdapter(bookmark);
52 retVal.setQuery(results.getQuery());
53 retVal.setData(results);
56 public static TableAdapter createFromTable(BookmarkNode bookmark, String table) {
57 TableAdapter retVal = new TableAdapter(bookmark);
58 retVal.setTable(table);
61 private void loadSize() {
63 totalSize = SQLHelper.getSize(bookmark, getTableCountQuery());
66 public int getStartIndex() {
72 public int getEndIndex() {
73 return offset + rows.size() - 1;
75 public int getTotalSize() {
78 public void nextPage() {
80 offset = offset + pageSize;
81 if (totalSize >= 0 && offset > totalSize) {
82 offset = offset - pageSize;
85 public void previousPage() {
86 offset = offset - pageSize;
91 public boolean hasNextPage() {
93 if (offset + pageSize <= totalSize) {
100 public boolean hasPreviousPage() {
106 public String getTableCountQuery() {
107 if (table != null && bookmark != null) {
108 return SQLHelper.getFullTableName(bookmark, table);
112 public String getQuery() {
114 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
115 if (adapter == null) throw new RuntimeException();
116 else return adapter.getShowTableQuery(DatabaseInfo.create(bookmark), table) + extra.toString();
120 public void loadData() {
123 if (offset > totalSize) {
127 String query = getQuery();
128 System.out.println(offset + Messages.getString("TableAdapter.to") + (offset + pageSize - 1)); //$NON-NLS-1$
129 SQLResults results = SQLHelper.getResults(bookmark, query, offset, offset + pageSize - 1, maxColumnSize, encoding);
132 public void resetOffset() {
135 public void setData(SQLResults results) {
136 int rowCount = results.getRowCount();
137 int columnCount = results.getColumnCount();
139 columnNames = new Vector();
140 for (int col = 1; col <= columnCount; col++) {
141 columnNames.addElement(results.getColumnName(col));
143 for (int row = 1; row <= rowCount; row++) {
144 String rowData[] = new String[columnCount];
145 for (int col = 1; col <= columnCount; col++) {
146 rowData[col - 1] = results.getElement(col, row).toString();
148 rows.addElement(rowData);
150 hasMore = results.hasMore();
151 if (table == null && results.getMaxSize() >= 0) {
152 if (offset > results.getMaxSize()) {
158 public void loadTable(Table table) {
159 table.setHeaderVisible(true);
160 for (int i = 0; i < columnNames.size(); i++) {
161 TableColumn column = new TableColumn(table, SWT.NONE);
162 column.setText(columnNames.elementAt(i).toString());
164 for (int i = 0; i < columnNames.size(); i++) {
165 table.getColumn(i).pack();
167 for (int row = 0; row < rows.size(); row++) {
168 TableItem item = new TableItem(table, SWT.NONE);
169 String itemData[] = (String[]) rows.elementAt(row);
170 item.setText(itemData);
172 for (int i = 0; i < columnNames.size(); i++) {
173 table.getColumn(i).pack();
177 public int getPageSize() {
181 public void setFilterSort(FilterSort extra) {
184 public String getTable() {
188 public void setTable(String table) {
191 public void setQuery(String query) {
194 public String getEncoding() {
198 public void setEncoding(String encoding) {
199 this.encoding = encoding;
201 public String getStatusString() {
202 String status = getStartIndex() + Messages.getString("TableAdapter.to") + getEndIndex() + Messages.getString("TableAdapter.of") + //$NON-NLS-1$ //$NON-NLS-2$
204 if (!encoding.equals(DEFAULT)) {
205 status += " (" + encoding + ")"; //$NON-NLS-1$ //$NON-NLS-2$
207 String filterText = extra.toString();
208 if (!filterText.equals("")) { //$NON-NLS-1$
209 status += " (" + filterText + ")"; //$NON-NLS-1$ //$NON-NLS-2$
211 if (pageSize == Integer.MAX_VALUE) {
212 status += Messages.getString("TableAdapter.full"); //$NON-NLS-1$
219 public BookmarkNode getBookmark() {