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.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;
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";
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;
38 private String encoding = "";
40 private TableAdapter() {
42 public void fullMode() {
44 pageSize = Integer.MAX_VALUE;
46 public void resetMode() {
48 pageSize = DefaultSizes.PAGE_SIZE;
50 public static TableAdapter createFromQuery(SQLResults results) {
51 TableAdapter retVal = new TableAdapter();
52 retVal.setQuery(results.getQuery());
53 retVal.setData(results);
56 public static TableAdapter createFromTable(String table) {
57 TableAdapter retVal = new TableAdapter();
58 retVal.setTable(table);
61 private void loadSize() {
63 totalSize = helper.getSize(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() {
108 Bookmark current = MultiSQLServer.getInstance().getConnected();
109 return SQLHelper.getFullTableName(current, table);
113 public String getQuery() {
115 MultiSQLServer server = MultiSQLServer.getInstance();
116 Bookmark current = server.getConnected();
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());
126 public void loadData() {
129 if (offset > totalSize) {
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);
138 public void resetOffset() {
141 public void setData(SQLResults results) {
142 int rowCount = results.getRowCount();
143 int columnCount = results.getColumnCount();
145 columnNames = new Vector();
146 for (int col = 1; col <= columnCount; col++) {
147 columnNames.addElement(results.getColumnName(col));
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();
154 rows.addElement(rowData);
156 hasMore = results.hasMore();
157 if (table == null && results.getMaxSize() >= 0) {
158 if (offset > results.getMaxSize()) {
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());
170 for (int i = 0; i < columnNames.size(); i++) {
171 table.getColumn(i).pack();
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);
178 for (int i = 0; i < columnNames.size(); i++) {
179 table.getColumn(i).pack();
183 public int getPageSize() {
187 public void setFilterSort(FilterSort extra) {
190 public String getTable() {
194 public void setTable(String table) {
197 public void setQuery(String query) {
200 public String getEncoding() {
204 public void setEncoding(String encoding) {
205 this.encoding = encoding;
207 public String getStatusString() {
208 String status = getStartIndex() + " to " + getEndIndex() + " of " +
210 if (!encoding.equals(DEFAULT)) {
211 status += " (" + encoding + ")";
213 String filterText = extra.toString();
214 if (!filterText.equals("")) {
215 status += " (" + filterText + ")";
217 if (pageSize == Integer.MAX_VALUE) {