1 package net.sourceforge.phpdt.sql.view.tableview;
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.NoSuchAdapterException;
10 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
11 import net.sourceforge.phpdt.sql.sql.FilterSort;
12 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
13 import net.sourceforge.phpdt.sql.sql.SQLHelper;
14 import net.sourceforge.phpdt.sql.sql.SQLResults;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Table;
18 import org.eclipse.swt.widgets.TableColumn;
19 import org.eclipse.swt.widgets.TableItem;
21 public class TableAdapter implements IConstants {
22 public static final String DEFAULT = "";
23 public static final String UTF_8 = "UTF-8";
24 public static final String UTF_16 = "UTF-16";
26 private int pageSize = DefaultSizes.PAGE_SIZE;
27 private int maxColumnSize = DefaultSizes.MAX_COLUMN_SIZE;
28 private SQLHelper helper = new SQLHelper();
29 private FilterSort extra = new FilterSort();
30 private int offset = 1;
31 private int totalSize = -1;
32 private Vector rows = new Vector();
33 private Vector columnNames = new Vector();
34 private boolean hasMore = false;
39 private String encoding = "";
41 private TableAdapter() {
43 public void fullMode() {
45 pageSize = Integer.MAX_VALUE;
47 public void resetMode() {
49 pageSize = DefaultSizes.PAGE_SIZE;
51 public static TableAdapter createFromQuery(SQLResults results) {
52 TableAdapter retVal = new TableAdapter();
53 retVal.setQuery(results.getQuery());
54 retVal.setData(results);
57 public static TableAdapter createFromTable(String table) {
58 TableAdapter retVal = new TableAdapter();
59 retVal.setTable(table);
62 private void loadSize() {
64 totalSize = helper.getSize(getTableCountQuery());
67 public int getStartIndex() {
73 public int getEndIndex() {
74 return offset + rows.size() - 1;
76 public int getTotalSize() {
79 public void nextPage() {
81 offset = offset + pageSize;
82 if (totalSize >= 0 && offset > totalSize) {
83 offset = offset - pageSize;
86 public void previousPage() {
87 offset = offset - pageSize;
92 public boolean hasNextPage() {
94 if (offset + pageSize <= totalSize) {
101 public boolean hasPreviousPage() {
107 public String getTableCountQuery() {
109 Bookmark current = MultiSQLServer.getInstance().getConnected();
110 return SQLHelper.getFullTableName(current, table);
114 public String getQuery() {
116 MultiSQLServer server = MultiSQLServer.getInstance();
117 Bookmark current = server.getConnected();
119 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(current.getType());
120 return adapter.getShowTableQuery(DatabaseInfo.create(current), table) + extra.toString();
121 } catch (NoSuchAdapterException e) {
122 throw new RuntimeException(e.toString());
127 public void loadData() {
130 if (offset > totalSize) {
134 String query = getQuery();
136 System.out.println(offset + " to " + (offset + pageSize - 1));
138 SQLResults results = helper.getResults(query, offset, offset + pageSize - 1, maxColumnSize, encoding);
141 public void resetOffset() {
144 public void setData(SQLResults results) {
145 int rowCount = results.getRowCount();
146 int columnCount = results.getColumnCount();
148 columnNames = new Vector();
149 for (int col = 1; col <= columnCount; col++) {
150 columnNames.addElement(results.getColumnName(col));
152 for (int row = 1; row <= rowCount; row++) {
153 String rowData[] = new String[columnCount];
154 for (int col = 1; col <= columnCount; col++) {
155 rowData[col - 1] = results.getElement(col, row).toString();
157 rows.addElement(rowData);
159 hasMore = results.hasMore();
160 if (table == null && results.getMaxSize() >= 0) {
161 if (offset > results.getMaxSize()) {
167 public void loadTable(Table table) {
168 table.setHeaderVisible(true);
169 for (int i = 0; i < columnNames.size(); i++) {
170 TableColumn column = new TableColumn(table, SWT.NONE);
171 column.setText(columnNames.elementAt(i).toString());
173 for (int i = 0; i < columnNames.size(); i++) {
174 table.getColumn(i).pack();
176 for (int row = 0; row < rows.size(); row++) {
177 TableItem item = new TableItem(table, SWT.NONE);
178 String itemData[] = (String[]) rows.elementAt(row);
179 item.setText(itemData);
181 for (int i = 0; i < columnNames.size(); i++) {
182 table.getColumn(i).pack();
186 public int getPageSize() {
190 public void setFilterSort(FilterSort extra) {
193 public String getTable() {
197 public void setTable(String table) {
200 public void setQuery(String query) {
203 public String getEncoding() {
207 public void setEncoding(String encoding) {
208 this.encoding = encoding;
210 public String getStatusString() {
211 String status = getStartIndex() + " to " + getEndIndex() + " of " +
213 if (!encoding.equals(DEFAULT)) {
214 status += " (" + encoding + ")";
216 String filterText = extra.toString();
217 if (!filterText.equals("")) {
218 status += " (" + filterText + ")";
220 if (pageSize == Integer.MAX_VALUE) {