KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > schemadefinition > oncolumns


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition;
2
3 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
4 import com.daffodilwoods.daffodildb.server.serversystem.*;
5 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
7 import com.daffodilwoods.database.resource.*;
8
9 public class oncolumns implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter {
10
11    public String JavaDoc toString() {
12       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
13       sb.append(" ");
14       sb.append("(");
15       sb.append(" ");
16       sb.append(_indextypelist0);
17       sb.append(" ");
18       sb.append(")");
19       return sb.toString();
20    }
21
22    public indextypelist _indextypelist0;
23
24    private IndexDescriptor indexDes;
25    private TableDescriptor tableDes;
26
27    /**
28     * Algo :-
29     * get the columns and their respective order
30     * check existance of columns
31     * set the index type fixed or variable based on dataTypes
32     * check if such column list already indexed
33     * set the IndexColumnDescriptors in IndexDescriptor
34     */

35
36    public Object JavaDoc run(Object JavaDoc object) throws DException {
37       _ServerSession currentSession = (_ServerSession) object;
38       String JavaDoc[] columns = _indextypelist0.getColumnNames();
39       Boolean JavaDoc[] orderTypes = _indextypelist0.getOrderTypes();
40       ensureColumns(columns);
41       setIsVariable(columns);
42       checkIsColumnListAlreadyIndexed(columns, orderTypes, currentSession);
43       setIndexColumnDescriptor(columns, orderTypes);
44       return null;
45    }
46
47    private void ensureColumns(String JavaDoc[] columns) throws DException {
48       int index = tableDes.isTableColumns(columns);
49       if (index != -1) {
50          throw new DException("DSE255", new Object JavaDoc[] {columns[index],
51                               tableDes.getQualifiedTableName()});
52       }
53    }
54
55    /**
56     * Check for whether the columns are of fixed type or variable type
57     * set value of fixedVariable column true if any of the column
58     * is of variable type other wise false.
59     * Throw Exception if any of the column is of Blob,Clob type
60     * as Indexes can't be maintained on these type of columns
61     */

62
63    private void setIsVariable(String JavaDoc[] columns) throws DException {
64       int len = columns.length;
65       int size = 0;
66       Boolean JavaDoc fixedVariable = Boolean.FALSE;
67       String JavaDoc countryCode = tableDes.country_code;
68       for (int i = 0; i < len; i++) {
69          ColumnDescriptor columnDescriptor = tableDes.getColumnDescriptor(
70              columns[i]);
71          DataTypeDescriptor dtd = columnDescriptor.dataTypeDescriptor;
72          if (columnDescriptor.isBlobClob()) {
73             throw new DException("DSE478",
74                                  new Object JavaDoc[] {columnDescriptor.column_name,
75                                  dtd.data_Type});
76          }
77          if (!columnDescriptor.isFixedColumn()) {
78             fixedVariable = Boolean.TRUE;
79          }
80          int columnSize = dtd.getBytesCount();
81          size += countryCode != null && dtd.isCharacterType() ? 2 * (columnSize + 1) : columnSize;
82       }
83       if (size > IndexDescriptor.maxIndexColumnSize) {
84          throw new DException("DSE8049",
85                               new Object JavaDoc[] {new Integer JavaDoc(IndexDescriptor.maxIndexColumnSize),
86                               indexDes.indexname, new Integer JavaDoc(size)});
87       }
88       indexDes.fixedVariable = fixedVariable;
89    }
90
91    private void setIndexColumnDescriptor(String JavaDoc[] columnNames,
92                                          Boolean JavaDoc[] orderTypes) throws
93        DException {
94       for (int i = 0; i < columnNames.length; i++) {
95          IndexColumnDescriptor indexColDes = new IndexColumnDescriptor(SystemTables.INDEXCOLUMNS);
96          indexColDes.table_catalog = indexDes.table_catalog;
97          indexColDes.table_schema = indexDes.table_schema;
98          indexColDes.table_name = indexDes.table_name;
99          indexColDes.indexname = indexDes.indexname;
100          indexColDes.columnName = columnNames[i];
101          indexColDes.orderType = orderTypes[i];
102          indexDes.addIndexColumnDescriptor(indexColDes);
103       }
104    }
105
106    public void setIndexDescriptor(_Descriptor indexDes0) {
107       indexDes = (IndexDescriptor) indexDes0;
108    }
109
110    public void setTableDescriptor(_Descriptor tableDes0) {
111       tableDes = (TableDescriptor) tableDes0;
112    }
113
114    private void checkIsColumnListAlreadyIndexed(String JavaDoc[] columnNames,
115                                                 Boolean JavaDoc[] orderTypes, _ServerSession currentSession) throws DException {
116       PreparedStatementGetter psg = ( (DataDictionary) currentSession.getDataDictionary()).getPreparedStatementGetter();
117       _SelectQueryIterator indexIterator = (_SelectQueryIterator) psg.getIndexInfoExecuter().executeForFresh(new Object JavaDoc[] {tableDes.table_catalog, tableDes.table_schema, tableDes.table_name});
118       int numberOfIndexColumns = columnNames.length;
119       if (indexIterator.first()) {
120          do {
121             Object JavaDoc[] record = (Object JavaDoc[]) indexIterator.getObject();
122             String JavaDoc indexName = null;
123             indexName = (String JavaDoc) record[SystemTablesFields.indexInfo_indexname];
124             _SelectQueryIterator indexColumnIterator = (_SelectQueryIterator) psg.getIndexColumnsExecuter().executeForFresh(new Object JavaDoc[] {tableDes.table_catalog, tableDes.table_schema, tableDes.table_name, indexName});
125             int rowCount = indexColumnIterator.getRowCount();
126             boolean matched = true;
127             if (rowCount == numberOfIndexColumns) {
128                int i = 0;
129                indexColumnIterator.first();
130                do {
131                   Object JavaDoc[] columnsRecord = (Object JavaDoc[]) indexColumnIterator.getObject();
132                   String JavaDoc columnName = (String JavaDoc) columnsRecord[SystemTablesFields.indexColumns_column_name];
133                   Boolean JavaDoc orderType = (Boolean JavaDoc) columnsRecord[SystemTablesFields.indexColumns_orderType];
134                   if (!columnName.equalsIgnoreCase(columnNames[i])) {
135                      matched = false;
136                      break;
137                   }
138                   if (!orderType.equals(orderTypes[i])) {
139                      matched = false;
140                      break;
141                   }
142                   i++;
143                } while (indexColumnIterator.next());
144                if (matched) {
145                   throw new DException("DSE8050", null);
146                }
147             }
148          } while (indexIterator.next());
149       }
150
151    }
152
153    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
154       return this;
155    }
156
157 }
158
Popular Tags