1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 import com.mckoi.debug.*; 29 30 35 36 public class Sequence extends Statement { 37 38 String type; 39 40 TableName seq_name; 41 42 Expression increment; 43 Expression min_value; 44 Expression max_value; 45 Expression start_value; 46 Expression cache_value; 47 boolean cycle; 48 49 51 public void prepare() throws DatabaseException { 52 type = (String ) cmd.getObject("type"); 53 String sname = (String ) cmd.getObject("seq_name"); 54 String schema_name = database.getCurrentSchema(); 55 seq_name = TableName.resolve(schema_name, sname); 56 seq_name = database.tryResolveCase(seq_name); 57 58 if (type.equals("create")) { 59 increment = (Expression) cmd.getObject("increment"); 61 min_value = (Expression) cmd.getObject("min_value"); 62 max_value = (Expression) cmd.getObject("max_value"); 63 start_value = (Expression) cmd.getObject("start"); 64 cache_value = (Expression) cmd.getObject("cache"); 65 cycle = cmd.getObject("cycle") != null; 66 } 67 68 } 69 70 public Table evaluate() throws DatabaseException { 71 72 DatabaseQueryContext context = new DatabaseQueryContext(database); 73 74 boolean ignore_case = database.isInCaseInsensitiveMode(); 76 SchemaDef schema = 77 database.resolveSchemaCase(seq_name.getSchema(), ignore_case); 78 if (schema == null) { 79 throw new DatabaseException("Schema '" + seq_name.getSchema() + 80 "' doesn't exist."); 81 } 82 else { 83 seq_name = new TableName(schema.getName(), seq_name.getName()); 84 } 85 86 if (type.equals("create")) { 87 88 if (!database.getDatabase().canUserCreateSequenceObject(context, 90 user, seq_name)) { 91 throw new UserAccessException( 92 "User not permitted to create sequence: " + seq_name); 93 } 94 95 if (database.tableExists(seq_name)) { 97 throw new DatabaseException("Database object with name '" + seq_name + 98 "' already exists."); 99 } 100 101 long v_start_value = 0; 103 if (start_value != null) { 104 v_start_value = 105 start_value.evaluate(null, null, context).toBigNumber().longValue(); 106 } 107 long v_increment_by = 1; 108 if (increment != null) { 109 v_increment_by = 110 increment.evaluate(null, null, context).toBigNumber().longValue(); 111 } 112 long v_min_value = 0; 113 if (min_value != null) { 114 v_min_value = 115 min_value.evaluate(null, null, context).toBigNumber().longValue(); 116 } 117 long v_max_value = Long.MAX_VALUE; 118 if (max_value != null) { 119 v_max_value = 120 max_value.evaluate(null, null, context).toBigNumber().longValue(); 121 } 122 long v_cache = 16; 123 if (cache_value != null) { 124 v_cache = 125 cache_value.evaluate(null, null, context).toBigNumber().longValue(); 126 if (v_cache <= 0) { 127 throw new DatabaseException("Cache size can not be <= 0"); 128 } 129 } 130 131 if (v_min_value >= v_max_value) { 132 throw new DatabaseException("Min value can not be >= the max value."); 133 } 134 if (v_start_value < v_min_value || 135 v_start_value >= v_max_value) { 136 throw new DatabaseException( 137 "Start value is outside the min/max sequence bounds."); 138 } 139 140 database.createSequenceGenerator(seq_name, 141 v_start_value, v_increment_by, v_min_value, v_max_value, 142 v_cache, cycle); 143 144 database.getGrantManager().addGrant( 147 Privileges.PROCEDURE_ALL_PRIVS, GrantManager.TABLE, 148 seq_name.toString(), user.getUserName(), true, 149 Database.INTERNAL_SECURE_USERNAME); 150 151 } 152 else if (type.equals("drop")) { 153 154 if (!database.getDatabase().canUserDropSequenceObject(context, 156 user, seq_name)) { 157 throw new UserAccessException( 158 "User not permitted to drop sequence: " + seq_name); 159 } 160 161 database.dropSequenceGenerator(seq_name); 162 163 database.getGrantManager().revokeAllGrantsOnObject( 165 GrantManager.TABLE, seq_name.toString()); 166 167 } 168 else { 169 throw new RuntimeException ("Unknown type: " + type); 170 } 171 172 return FunctionTable.resultTable(context, 0); 174 175 } 176 177 } 178 179 | Popular Tags |