KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > descriptors > ViewDescriptor


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
11 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
12 import com.daffodilwoods.daffodildb.server.sql99.utils.parser.*;
13 import com.daffodilwoods.daffodildb.utils.*;
14 import com.daffodilwoods.database.general.*;
15 import com.daffodilwoods.database.resource.*;
16
17 public class ViewDescriptor extends Descriptor {
18    /**
19     * View catalog is stored in form of string in table_catalog variable. */

20    public String JavaDoc table_catalog;
21    /**
22     * View schema is stored in form of string in table_schema variable. */

23    public String JavaDoc table_schema;
24    /**
25     * View name is stored in form of string in table_name variable. */

26    public String JavaDoc table_name;
27    /**
28     * View definition(Such as select query) is stored in form of string in view_definition variable. */

29    public String JavaDoc view_definition;
30
31    public String JavaDoc check_option = SqlKeywords.NONE;
32    public String JavaDoc is_updatable = SqlSchemaConstants.NO;
33    public String JavaDoc is_insertable_into = SqlSchemaConstants.NO;
34    public String JavaDoc materialized_table_name;
35    /**
36     * Table names are stored in form of a String[] catalog,schema,name in tableUsed variable. */

37    public ArrayList tablesUsed;
38    /**
39     * Table names are stored in form of a String[] catalog,schema,Tablename,columnName in columnUsed variable. */

40    public ArrayList columnsUsed;
41
42    public TableDescriptor tableDescriptor;
43
44    /**
45     * Query to load all information (Such as table_catalog, table_schema, table_name, view_definition, check_option, is_updatable,
46     * is_insertable_into, materialized_table_name) from views system table according to schema qualified table name. */

47    private static String JavaDoc viewQuery = "select * from " +
48        SqlSchemaConstants.views_TableName +
49        " where table_catalog = ? and table_schema = ? and table_name = ? ";
50
51    /**
52     * Query to load all information (Such as table_catalog,table_schema,table_name) from view_table_usage system table according to schema qualified view name. */

53    private static String JavaDoc selectViewTableUsageQuery =
54        "Select table_catalog,table_schema,table_name from " +
55        SystemTables.view_table_usage_TableName +
56        " where view_catalog = ? and view_schema = ? and view_name = ? ";
57
58    /**
59     * Query to load all information (Such as table_catalog,table_schema,table_name,column_name) from view_colum_usage system table according to schema qualified view name. */

60    private static String JavaDoc selcetViewColumnUsageQuery =
61        "select table_catalog,table_schema,table_name ,column_name from " +
62        SqlSchemaConstants.view_colum_usage_TableName +
63        " where view_catalog = ? and view_schema = ? and view_name = ? ";
64    public ViewDescriptor() throws DException {
65    }
66
67    /**
68     * Load all information (Such as table_catalog, table_schema, table_name, view_definition, check_option, is_updatable,
69     * is_insertable_into, materialized_table_name) from views system table according to schema qualified table name. */

70    public void load(_ServerSession serverSession) throws DException {
71       initTableDescriptor();
72       tableDescriptor.load(serverSession); // rethought later;
73

74       _SelectQueryIterator viewIterator = SqlSchemaConstants.getIterator(serverSession,
75           viewQuery, new Object JavaDoc[] {table_catalog, table_schema, table_name});
76       if (!viewIterator.first()) {
77          throw new DException("DSE1067", null);
78       }
79       loadDataFromRecord(viewIterator);
80       loadTableUsage(serverSession);
81       loadColumnUsage(serverSession);
82    }
83
84    /**
85     * Load all information(Like table_catalog, table_schema, table_name, view_definition and check_option etc...) for a particular view. */

86    public void loadDataFromRecord(_SelectQueryIterator iter) throws DException {
87       Object JavaDoc[] values = (Object JavaDoc[]) iter.getObject();
88       loadDataFromRecord(values);
89    }
90
91    /**
92     * Load all information(Like table_catalog, table_schema, table_name, view_definition and check_option etc...) for a particular view. */

93    public void loadDataFromRecord(_SelectQueryIterator iter, _ServerSession serverSession) throws
94        DException {
95       Object JavaDoc[] values = (Object JavaDoc[]) iter.getObject();
96       loadDataFromRecord(values);
97       initTableDescriptor();
98       tableDescriptor.load(serverSession); // rethought later;
99
loadTableUsage(serverSession);
100       loadColumnUsage(serverSession);
101    }
102
103    /**
104     * Load all information(Such as table_catalog, table_schema, table_name, view_definition, is_updatable, is_insertable_into
105     * check_option, and materialized_table_name) for a particular view. */

106    private void loadDataFromRecord(Object JavaDoc[] values) throws DException {
107       table_catalog = (String JavaDoc) values[SystemTablesFields.view_table_catalog];
108       table_schema = (String JavaDoc) values[SystemTablesFields.view_table_schema];
109       table_name = (String JavaDoc) values[SystemTablesFields.view_table_name];
110       view_definition = (String JavaDoc) values[SystemTablesFields.view_view_definition];
111       check_option = (String JavaDoc) values[SystemTablesFields.view_check_option];
112       is_updatable = (String JavaDoc) values[SystemTablesFields.view_is_updatable];
113       is_insertable_into = (String JavaDoc) values[SystemTablesFields.
114           view_is_insertable_into];
115       materialized_table_name = (String JavaDoc) values[SystemTablesFields.
116           view_materialized_table_name];
117    }
118
119
120    /* save the view definition in the views table,
121       save the tables used in the queryExpression of the view in the view_table_usage table,
122         save the columns used in the seacrh condition of the queryExpression of the
123          view in the view_column_usage_table;
124     */

125
126    /**
127     * Save all the information(Such as table_catalog, table_schema, table_name, view_definition, check_option, is_updatable,
128     * is_insertable_into, materialized_table_name) of a particular view in views system table at the time of view creation.
129     * If such information is already exists in system table then it throws PrimaryConstraintException. */

130    public void save(_ServerSession serverSession) throws DException {
131       view_definition = removeSpaces(view_definition);
132       Object JavaDoc[] tableData = new Object JavaDoc[] {
133           table_catalog, table_schema, table_name,
134           view_definition, check_option, is_updatable, is_insertable_into,
135           materialized_table_name};
136
137       tableDescriptor.save(serverSession);
138       tableDescriptor.saveColumns(serverSession);
139       try {
140          SqlSchemaConstants.insert(serverSession,
141                                    SqlSchemaConstants.views_TableName, null,
142                                    tableData);
143       } catch (PrimaryConstraintException de) {
144          DException tde = new DException("DSE1137", new Object JavaDoc[] {getViewName()});
145          throw tde;
146       } catch (SizeMisMatchException de) {
147          if (de.getDseCode().equals("DSE773")) {
148             if (view_definition.length() > 4192)
149                throw new DException("DSE8162", new Object JavaDoc[] {new Integer JavaDoc(view_definition.length()), getViewName()});
150             DException tde = new DException("DSE8103", null);
151             throw tde;
152          }
153       } catch (DException de) {
154          if (de.getDseCode().equals("DSE1255")) {
155             DException tde = new DException("DSE1137", new Object JavaDoc[] {getViewName()});
156             throw tde;
157          }
158          if (de.getDseCode().equals("DSE773")) {
159             DException tde = new DException("DSE8103", null);
160             throw tde;
161          }
162          throw de;
163       }
164       saveTablesUsed(serverSession);
165       saveColumnsUsed(serverSession);
166    }
167
168    /**
169     * Save all the information(Such as view_catalog, view_schema, view_name, table_catalog, table_schema, table_name)
170     * of a particular view in view_table_usage system table at the time of view creation. */

171    private void saveTablesUsed(_ServerSession serverSession) throws DException {
172       Object JavaDoc[] columnValues = new Object JavaDoc[6];
173       columnValues[0] = table_catalog;
174       columnValues[1] = table_schema;
175       columnValues[2] = table_name;
176       for (int i = 0, tableCount = tablesUsed.size(); i < tableCount; i++) {
177          String JavaDoc[] tableName = (String JavaDoc[]) tablesUsed.get(i);
178          columnValues[3] = tableName[0]; // table catalog
179
columnValues[4] = tableName[1]; // table schema
180
columnValues[5] = tableName[2]; // table name
181
SqlSchemaConstants.insert(serverSession,
182                                    SqlSchemaConstants.view_table_usage_TableName, null, columnValues);
183       }
184    }
185
186    /**
187     * Save all the information(Such as view_catalog, view_schema, view_name, table_catalog, table_schema, table_name, column_name)
188     * of a particular view in view_colum_usage system table at the time of view creation. */

189    private void saveColumnsUsed(_ServerSession serverSession) throws DException {
190       Object JavaDoc[] columnValues = new Object JavaDoc[7];
191       columnValues[0] = table_catalog;
192       columnValues[1] = table_schema;
193       columnValues[2] = table_name;
194       for (int i = 0, columnCount = columnsUsed.size(); i < columnCount; i++) {
195          String JavaDoc[] columnName = (String JavaDoc[]) columnsUsed.get(i);
196          columnValues[3] = columnName[0]; // table catalog
197
columnValues[4] = columnName[1]; // table schema
198
columnValues[5] = columnName[2]; // table name
199
columnValues[6] = columnName[3]; // column name
200
SqlSchemaConstants.insert(serverSession,
201                                    SqlSchemaConstants.view_colum_usage_TableName, null, columnValues);
202       }
203    }
204
205    /*
206       public void delete(DConnection connection) throws DException {
207         delete((UserSession)connection.getUserSession());
208       }*/

209
210    /**
211     * Remove the all information of a particular view (Like columns(which are used for a particular view) and tables(which are used for a particular view) OR views(which are used for a particular view) etc...) from views_TableName system table */

212    public void delete(_ServerSession serverSession) throws DException {
213       deleteViewTableUsage(serverSession);
214       deleteViewColumnUsage(serverSession);
215       tableDescriptor.delete(serverSession);
216
217       String JavaDoc bve = "TABLE_CATALOG=? and TABLE_SCHEMA=? and TABLE_NAME=?";
218       _ServerSession globalSession = serverSession.getGlobalSession();
219       booleanvalueexpression condition = ConditionParser.parseCondition(bve,
220           globalSession, SqlSchemaConstants.views_TableName);
221       super.delete(serverSession, SqlSchemaConstants.views_TableName, condition,
222                    new Object JavaDoc[] {table_catalog, table_schema, table_name});
223    }
224
225    /**
226     * Remove all information of tables(which are used for a particular view) from view_table_usage system table */

227    private void deleteViewTableUsage(_ServerSession serverSession) throws DException {
228       TableDetails tableDetail = new TableDetails();
229       tableDetail.setTableName(SqlSchemaConstants.view_table_usage_TableName.
230                                getTableName());
231       String JavaDoc bve = "VIEW_CATALOG=? and VIEW_SCHEMA=? and VIEW_NAME=?";
232       _ServerSession globalSession = serverSession.getGlobalSession();
233       booleanvalueexpression condition = ConditionParser.parseCondition(bve,
234           globalSession, SqlSchemaConstants.view_table_usage_TableName);
235       ConditionSingleTableExecuter conSingTE = new ConditionSingleTableExecuter(null,
236           tableDetail, globalSession, condition, null);
237       _Iterator iter = globalSession.getInternalIterator(SqlSchemaConstants.
238           view_table_usage_TableName,
239           conSingTE);
240       iter.setConditionVariableValue(condition.getReferences(new TableDetails[] {
241           tableDetail}), FieldUtility.getFields(new Object JavaDoc[] {table_catalog, table_schema, table_name})
242                                      , 1);
243       SqlSchemaConstants.deleteAll(serverSession.getSystemServerSession(),
244                                    SqlSchemaConstants.view_table_usage_TableName,
245                                    iter);
246    }
247
248    /**
249     * Remove all information of columns(which are used for a particular view) from view_colum_usage system table */

250    private void deleteViewColumnUsage(_ServerSession serverSession) throws DException {
251       TableDetails tableDetail = new TableDetails();
252       tableDetail.setTableName(SqlSchemaConstants.view_colum_usage_TableName.
253                                getTableName());
254       String JavaDoc bve = "VIEW_CATALOG=? and VIEW_SCHEMA=? and VIEW_NAME=?";
255       _ServerSession globalSession = serverSession.getGlobalSession();
256       booleanvalueexpression condition = ConditionParser.parseCondition(bve,
257           globalSession, SqlSchemaConstants.view_colum_usage_TableName);
258       ConditionSingleTableExecuter conSingTE = new ConditionSingleTableExecuter(null,
259           tableDetail, globalSession, condition, null);
260       _Iterator iter = globalSession.getInternalIterator(SqlSchemaConstants.
261           view_colum_usage_TableName,
262           conSingTE);
263       iter.setConditionVariableValue(condition.getReferences(new TableDetails[] {
264           tableDetail}), FieldUtility.getFields(new Object JavaDoc[] {table_catalog, table_schema, table_name})
265                                      , 1);
266       SqlSchemaConstants.deleteAll(serverSession.getSystemServerSession(),
267                                    SqlSchemaConstants.view_colum_usage_TableName,
268                                    iter);
269    }
270
271    /**
272     * Set view name in view descriptor fields through table descriptor fields*/

273    public void setViewName() throws DException {
274       table_catalog = tableDescriptor.table_catalog;
275       table_schema = tableDescriptor.table_schema;
276       table_name = tableDescriptor.table_name;
277    }
278
279    /*private String getClauseForUsageTable() throws DException{
280      StringBuffer clause = new StringBuffer();
281      clause.append("view_catalog = '");clause.append(table_catalog);
282      clause.append("' and view_schema = '");clause.append(table_schema);
283      clause.append("' and view_name = '");clause.append(table_name);
284      clause.append('\'');
285      return clause.toString();
286        }*/

287
288    /**
289     * Intialize the table descriptor for a particular view. */

290    private void initTableDescriptor() throws DException {
291       if (tableDescriptor == null) {
292          tableDescriptor = new TableDescriptor();
293       }
294       tableDescriptor.table_catalog = table_catalog;
295       tableDescriptor.table_schema = table_schema;
296       tableDescriptor.table_name = table_name;
297    }
298
299 // expression of this view*/
300

301    /* Return whether the column passed is a column of this view
302       columnName is fully qualified name;
303     */

304
305    /**
306     * Returns schema qualified view name. */

307     public String JavaDoc getViewName() {
308       StringBuffer JavaDoc viewName = new StringBuffer JavaDoc();
309       viewName.append(table_catalog).append(".").append(table_schema).append(".").
310           append(table_name);
311       return viewName.toString();
312    }
313
314    /**
315     * Load all information(Such as table_catalog, table_schema, table_name) from view_table_usage
316     * system table according to schema qualified view name. */

317    private void loadTableUsage(_ServerSession serverSession) throws DException {
318       loadTableUsed(SqlSchemaConstants.getIterator(serverSession,
319           selectViewTableUsageQuery,
320           new Object JavaDoc[] {table_catalog,
321           table_schema, table_name}));
322    }
323
324    /**
325     * Load all information(Such as table_catalog, table_schema, table_name) from view_table_usage
326     * system table according to schema qualified view name. */

327    private void loadTableUsed(_SelectQueryIterator viewTableUsageIterator) throws
328        DException {
329       tablesUsed = new ArrayList();
330       if (viewTableUsageIterator.first()) {
331          do {
332             Object JavaDoc[] values = (Object JavaDoc[]) viewTableUsageIterator.getObject();
333             tablesUsed.add(new String JavaDoc[] { (String JavaDoc) values[0], (String JavaDoc) values[1],
334                            (String JavaDoc) values[2]});
335          } while (viewTableUsageIterator.next());
336       }
337    }
338
339    /**
340     * Load all information(Such as table_catalog, table_schema, table_name,column_name) from view_colum_usage
341     * system table according to schema qualified view name. */

342    private void loadColumnUsage(_ServerSession serverSession) throws DException {
343       loadColumnUsed(SqlSchemaConstants.getIterator(serverSession,
344           selcetViewColumnUsageQuery,
345           new Object JavaDoc[] {table_catalog,
346           table_schema, table_name}));
347    }
348
349    /**
350     * Load all information(Such as table_catalog, table_schema, table_name,column_name) from view_colum_usage
351     * system table according to schema qualified view name. */

352    private void loadColumnUsed(_SelectQueryIterator viewColumnUsageIterator) throws
353        DException {
354       columnsUsed = new ArrayList();
355       if (viewColumnUsageIterator.first()) {
356          do {
357             Object JavaDoc[] values = (Object JavaDoc[]) viewColumnUsageIterator.getObject();
358             columnsUsed.add(new String JavaDoc[] { (String JavaDoc) values[0], (String JavaDoc) values[1],
359                             (String JavaDoc) values[2], (String JavaDoc) values[3]});
360          } while (viewColumnUsageIterator.next());
361       }
362    }
363
364    /**
365     * If Object parameter instance of viewDescriptor then it returns schema qualified table name. otherwise it returns false. */

366    public boolean equals(Object JavaDoc parm1) {
367       if (! (parm1 instanceof ViewDescriptor)) {
368          return false;
369       }
370       ViewDescriptor des = (ViewDescriptor) parm1;
371       return table_catalog.equalsIgnoreCase(des.table_catalog)
372           && table_schema.equalsIgnoreCase(des.table_schema)
373           && table_name.equalsIgnoreCase(des.table_name);
374    }
375
376    /**
377     * Returns the int value 30 for view_descriptor. */

378    public int getDescriptorType() {
379       return VIEW_DESCRIPTOR;
380    }
381
382
383    private String JavaDoc removeSpaces(String JavaDoc query) {
384      String JavaDoc query1 = query;
385      while( query.length() > 4096 ){
386        String JavaDoc newQuery = query.replaceAll(" "," ");
387        if( newQuery.length() == query.length() )
388          break;
389        else
390          query = newQuery;
391      }
392
393      if( query1.length() > 4096 ){
394        query = query.replaceAll(" ,",",");
395        query = query.replaceAll(", ",",");
396        if(query.length() > 4096 )
397          ;//// Removed By Program ** System.out.println(" ################ "+query);
398
}
399      return query;
400    }
401 }
402
Popular Tags