private Result ProcessGrantOrRevoke(Tokenizer c, Channel channel, bool grant) { channel.CheckReadWrite(); channel.CheckAdmin(); AccessType right = AccessType.None; string sToken; do { string sRight = c.GetString(); right |= Access.GetRight(sRight); sToken = c.GetString(); } while (sToken.Equals(",")); if (!sToken.Equals("ON")) { throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken); } string table = c.GetString(); if (table.Equals("CLASS")) { // object is saved as 'CLASS "java.lang.Math"' // tables like 'CLASS "xy"' should not be created table += " \"" + c.GetString() + "\""; } else { GetTable(table, channel); // to make sure the table exists } c.GetThis("TO"); string user = c.GetStringToken(); // string command; if (grant) { _access.Grant(user, table, right); // command = "GRANT"; } else { _access.Revoke(user, table, right); // command = "REVOKE"; } return(new Result()); }
public Result GetScript(bool bDrop, bool bInsert, bool bCached, Channel channel) { channel.CheckAdmin(); Result r = new Result(1); r.Type[0] = ColumnType.VarChar; r.Table[0] = "SYSTEM_SCRIPT"; r.Label[0] = "COMMAND"; r.Name[0] = "COMMAND"; StringBuilder a = new StringBuilder(); for (int i = 0; i < tTable.Count; i++) { Table t = (Table)tTable[i]; if (bDrop) { AddRow(r, "DROP TABLE \"" + t.Name + "\""); } a.Remove(0, a.Length); a.Append("CREATE "); if (t.IsCached) { a.Append("CACHED "); } a.Append("TABLE "); a.Append('"'); a.Append(t.Name); a.Append('"'); a.Append("("); int columns = t.ColumnCount; Index pki = t.GetIndex("SYSTEM_PK"); int pk = (pki == null) ? -1 : pki.Columns[0]; for (int j = 0; j < columns; j++) { a.Append('"'); a.Append(t.GetColumnName(j)); a.Append('"'); a.Append(" "); a.Append(Column.GetColumnTypeString(t.GetType(j))); if (!t.GetColumnIsNullable(j)) { a.Append(" NOT NULL"); } if (j == t.IdentityColumn) { a.Append(" IDENTITY"); } if (j == pk) { a.Append(" PRIMARY KEY"); } if (j < columns - 1) { a.Append(","); } } ArrayList v = t.Constraints; for (int j = 0; j < v.Count; j++) { Constraint c = (Constraint)v[j]; if (c.ConstraintType == ConstraintType.ForeignKey) { a.Append(",FOREIGN KEY"); int[] col = c.RefTableColumns; a.Append(GetColumnList(c.RefTable, col, col.Length)); a.Append("REFERENCES "); a.Append(c.MainTable.Name); col = c.MainTableColumns; a.Append(GetColumnList(c.MainTable, col, col.Length)); } else if (c.ConstraintType == ConstraintType.Unique) { a.Append(",UNIQUE"); int[] col = c.MainTableColumns; a.Append(GetColumnList(c.MainTable, col, col.Length)); } } a.Append(")"); AddRow(r, a.ToString()); Index index = null; while (true) { index = t.GetNextIndex(index); if (index == null) { break; } string indexname = index.Name; if (indexname.Equals("SYSTEM_PK")) { continue; } else if (indexname.StartsWith("SYSTEM_FOREIGN_KEY")) { // foreign keys where created in the 'create table' continue; } else if (indexname.StartsWith("SYSTEM_CONSTRAINT")) { // constraints where created in the 'create table' continue; } a.Remove(0, a.Length); a.Append("CREATE "); if (index.IsUnique) { a.Append("UNIQUE "); } a.Append("INDEX "); a.Append(indexname); a.Append(" ON "); a.Append(t.Name); int[] col = index.Columns; int len = col.Length; if (!index.IsUnique) { len--; } a.Append(GetColumnList(t, col, len)); AddRow(r, a.ToString()); } if (bInsert) { Index primary = t.PrimaryIndex; Node x = primary.First(); bool integrity = true; if (x != null) { integrity = false; AddRow(r, "SET REFERENTIAL_INTEGRITY FALSE"); } while (x != null) { AddRow(r, t.GetInsertStatement(x.GetData())); x = primary.Next(x); } if (!integrity) { AddRow(r, "SET REFERENTIAL_INTEGRITY TRUE"); } } if (bCached && t.IsCached) { a.Remove(0, a.Length); a.Append("SET TABLE "); a.Append('"'); a.Append(t.Name); a.Append('"'); a.Append(" INDEX '"); a.Append(t.IndexRoots); a.Append("'"); AddRow(r, a.ToString()); } } ArrayList uList = aAccess.GetUsers(); for (int i = 0; i < uList.Count; i++) { User u = (User)uList[i]; // todo: this is not a nice implementation if (u == null) { continue; } string name = u.Name; if (!name.Equals("PUBLIC")) { a.Remove(0, a.Length); a.Append("CREATE USER "); a.Append(name); a.Append(" PASSWORD "); a.Append("\"" + u.Password + "\""); if (u.IsAdmin) { a.Append(" ADMIN"); } AddRow(r, a.ToString()); } Hashtable rights = u.Rights; if (rights == null) { continue; } foreach (string dbObject in rights.Keys) { AccessType right = (AccessType)rights[dbObject]; if (right == AccessType.None) { continue; } a.Remove(0, a.Length); a.Append("GRANT "); a.Append(Access.GetRight(right)); a.Append(" ON "); a.Append(dbObject); a.Append(" TO "); a.Append(u.Name); AddRow(r, a.ToString()); } } if (dDatabase.IsIgnoreCase) { AddRow(r, "SET IGNORECASE TRUE"); } Hashtable h = dDatabase.Alias; foreach (string alias in h.Keys) { string className = (string)h[alias]; AddRow(r, "CREATE ALIAS " + alias + " FOR \"" + className + "\""); } return(r); }