| 1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemamanipulation; 2 3 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 4 import com.daffodilwoods.daffodildb.server.serversystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 7 import com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.token.*; 10 import com.daffodilwoods.daffodildb.utils.*; 11 import com.daffodilwoods.daffodildb.utils.field.*; 12 import com.daffodilwoods.daffodildb.utils.parser.*; 13 import com.daffodilwoods.database.general.*; 14 import com.daffodilwoods.database.resource.*; 15 import com.daffodilwoods.daffodildb.server.sql99.utils.VariableValues; 16 17 public class addcolumndefinition implements altertableaction { 18 19 public columndefinition _columndefinition0; 20 public SRESERVEDWORD1206543922 _OptSRESERVEDWORD12065439221; 21 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222; 22 23 private TableDescriptor tableDescriptor; 24 25 public void setTableDescriptor(_Descriptor tableDes) throws DException { 26 tableDescriptor = (TableDescriptor) tableDes; 27 } 28 29 public Object run(Object object) throws DException { 30 _ServerSession currentSession = (_ServerSession) object; 31 QualifiedIdentifier tableName = tableDescriptor.getQualifiedTableName(); 32 _DataDictionary dataDictionary = currentSession.getDataDictionary(); 33 dataDictionary.removeTable(tableName); 34 _ColumnCharacteristics oldCharacteristics = currentSession. 35 getColumnCharacteristics(tableName); 36 37 ColumnDescriptor columnDescriptor = setColumnName(object); 38 checkForPrimaryAndUnique(currentSession, columnDescriptor); 39 createPrivileges(currentSession, columnDescriptor.column_name); 40 _columndefinition0.setReferentialConstraints(currentSession); 41 42 checkForeignKeyConstraintValidity(currentSession, columnDescriptor); 43 44 _columndefinition0.validateCheckConstraintSemanticAndVerify(currentSession, 45 columnDescriptor); 46 47 50 refreshServerSystem(currentSession, tableName, columnDescriptor); 51 return null; 52 } 53 54 private void checkForeignKeyConstraintValidity(_ServerSession currentSession, ColumnDescriptor columnDescriptor) throws DException { 55 StringBuffer query = new StringBuffer (); 56 query.append("select * from ") 57 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_catalog)) 58 .append(".").append(QuotedUtility.getUserIdentifier(tableDescriptor. 59 table_schema)) 60 .append(".").append(QuotedUtility.getUserIdentifier(tableDescriptor. 61 table_name)); 62 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(currentSession, 63 query.toString(), null); 64 boolean dataExists = iter.first(); 65 66 columnconstraintdefinition[] ccd = _columndefinition0._OptRepcolumnconstraintdefinition1; 67 if (ccd != null && dataExists) { 68 for (int i = 0; i < ccd.length; i++) { 69 TableConstraintDescriptor tcd = ccd[i].getTableConstraintDescriptor(); 70 String constType = tcd.constraint_type; 71 if (constType.equalsIgnoreCase(SqlKeywords.FOREIGN + " " + 72 SqlKeywords.KEY)) { 73 if (columnDescriptor.column_default != null) { 74 ReferentialConstraintDescriptor rcd = (ReferentialConstraintDescriptor) tcd.constraintDescriptor; 75 String referencedColumn = rcd.getReferencedColumnNames()[0]; 76 QualifiedIdentifier referencedTable = rcd.getReferencedTable(); 77 String clause = "Select * From " + referencedTable + " where " + referencedColumn + " = " + columnDescriptor.column_default; 78 _SelectQueryIterator iterator = SqlSchemaConstants.getIteratorOnCurrent(currentSession, clause, null); 79 if (!iterator.first()) 80 throw new DException("DSE7075", new Object [] {rcd.getConstraintName(), referencedTable}); 81 } 82 } 83 } 84 } 85 } 86 87 private ColumnDescriptor setColumnName(Object object) throws DException { 88 ColumnDescriptor columnDescriptor; 89 _columndefinition0.setTableDescriptor(tableDescriptor); 90 columnDescriptor = (ColumnDescriptor) _columndefinition0.run(object); 91 return columnDescriptor; 92 } 93 94 private void refreshServerSystem(_ServerSession currentSession, 95 QualifiedIdentifier tableName, 96 ColumnDescriptor columnDescriptor) throws 97 DException { 98 _DataDictionary dataDictionary = currentSession.getDataDictionary(); 99 dataDictionary.removeTable(tableName); 100 _ColumnCharacteristics newCharacteristics = currentSession. 101 getColumnCharacteristics(tableName); 102 String columnName = columnDescriptor.column_name; 103 int columnIndex = newCharacteristics.getColumnIndex(columnName); 104 105 if (columnDescriptor.column_default == null) { 106 currentSession.alterTable(tableName, newCharacteristics, new FieldLiteral(FieldUtility.NULLBUFFERRANGE, newCharacteristics.getColumnType(columnIndex))); 107 } else { 108 defaultoption _defaultOption = _columndefinition0._Optdefaultclause3. 109 _defaultoption0; 110 112 VariableValues vv = new VariableValues(currentSession); 113 Object defaultValue = _defaultOption.run(vv); 114 115 116 currentSession.alterTable(tableName, newCharacteristics, 117 FieldUtility. 118 convertToAppropriateType( (FieldBase) 119 defaultValue, newCharacteristics.getColumnType(columnIndex), 120 newCharacteristics.getPrecision(columnIndex), newCharacteristics.getCollator())); 121 } 122 } 123 124 private void createPrivileges(_ServerSession currentSession, 125 String columnName) throws 126 DException { 127 String queryForTablePrivileges = 128 "select grantor,grantee,Privilege_Type,is_grantable from " + 129 SqlSchemaConstants.table_privileges_TableName 130 + " where table_catalog = ? and table_schema = ? and table_name = ? "; 131 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(currentSession, 132 queryForTablePrivileges, new Object [] {tableDescriptor.table_catalog, 133 tableDescriptor.table_schema, tableDescriptor.table_name}); 134 if (iter.first()) { 135 String tableName = tableDescriptor.getTableName(); 136 String table_name = tableDescriptor.table_name; 137 String table_catalog = tableDescriptor.table_catalog; 138 String table_schema = tableDescriptor.table_schema; 139 do { 140 Object [] values = (Object []) iter.getObject(); 141 String grantee = (String ) values[1]; 142 String isGrantable = (String ) values[3]; 143 String privilegeType = (String ) values[2]; 144 if (privilegeType.equalsIgnoreCase(SqlKeywords.DELETE) || 145 privilegeType.equalsIgnoreCase(SqlKeywords.TRIGGER)) { 146 continue; 147 } 148 StringBuffer grantQuery = new StringBuffer (); 149 grantQuery.append("grant ") 150 .append(privilegeType) 151 .append("( ").append(QuotedUtility.getUserIdentifier(columnName)) 152 .append(" ) on ").append(QuotedUtility.getUserIdentifier( 153 table_catalog)) 154 .append(".").append(QuotedUtility.getUserIdentifier(table_schema)) 155 .append(".").append(QuotedUtility.getUserIdentifier(table_name)) 156 .append(" to ").append(QuotedUtility.getUserIdentifier(grantee)) 157 .append(" granted by ") 158 .append(SqlKeywords.CURRENT_USER); 159 grantprivilegestatement _grantprivilegestatement = ( 160 grantprivilegestatement) Parser.parseQuery(grantQuery.toString()); 161 _grantprivilegestatement.setObjectDescriptor(tableDescriptor); 162 _grantprivilegestatement.run(currentSession.getSystemServerSession()); 163 } while (iter.next()); 164 } 165 } 166 167 private void checkForPrimaryAndUnique(_ServerSession serverSession, 168 ColumnDescriptor columnDescriptor) throws DException { 169 columnconstraintdefinition[] _RepcolumnConstraintDefintion = _columndefinition0._OptRepcolumnconstraintdefinition1; 170 if (_RepcolumnConstraintDefintion != null) { 171 StringBuffer query = new StringBuffer (); 172 query.append("select * from ") 173 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_catalog)) 174 .append(".").append(QuotedUtility.getUserIdentifier(tableDescriptor. 175 table_schema)) 176 .append(".").append(QuotedUtility.getUserIdentifier(tableDescriptor. 177 table_name)); 178 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 179 query.toString(), null); 180 boolean dataExists = iter.first(); 181 columnconstraintdefinition[] constraintDefinitions = _RepcolumnConstraintDefintion; 182 if (dataExists) { 183 boolean rowCount = iter.next(); 184 for (int i = 0; i < constraintDefinitions.length; i++) { 185 TableConstraintDescriptor tcd = constraintDefinitions[i].getTableConstraintDescriptor(); 186 if (tcd == null) { 187 continue; 188 } 189 String constType = tcd.constraint_type; 190 if (constType.equalsIgnoreCase(SqlKeywords.PRIMARY + " " + SqlKeywords.KEY) && rowCount) { 191 throw new DException("DSE7087", 192 new Object [] {tcd.getQualifiedConstraintName(). 193 getIdentifier(), columnDescriptor.column_name, 194 tableDescriptor.getQualifiedTableName(). 195 getIdentifier()}); } 197 if (columnDescriptor.isNullable.equalsIgnoreCase(SqlKeywords.NO) && (columnDescriptor.column_default == null || 198 columnDescriptor.column_default.equalsIgnoreCase("null"))) { 199 throw new DException("DSE7089", 200 new Object [] {tcd.getQualifiedConstraintName(). 201 getIdentifier(), columnDescriptor.column_name, 202 tableDescriptor.getQualifiedTableName(). 203 getIdentifier()}); } 205 206 if (constType.equalsIgnoreCase(SqlKeywords.UNIQUE) && rowCount && 207 (columnDescriptor.column_default != null && !columnDescriptor.column_default.equalsIgnoreCase("null"))) { 208 throw new DException("DSE7088", 209 new Object [] {tcd.getQualifiedConstraintName(). 210 getIdentifier(), 211 tableDescriptor.getQualifiedTableName(). 212 getIdentifier()}); } 214 } 215 } 216 } 217 } 218 219 public String toString() { 220 StringBuffer sb = new StringBuffer (); 221 sb.append(" "); 222 sb.append(_SRESERVEDWORD12065439222); 223 sb.append(" "); 224 if (_OptSRESERVEDWORD12065439221 != null) { 225 sb.append(_OptSRESERVEDWORD12065439221); 226 } 227 sb.append(" "); 228 sb.append(_columndefinition0); 229 return sb.toString(); 230 } 231 232 public Object clone() throws CloneNotSupportedException { 233 return this; 234 } 235 } 236 | Popular Tags |