/** * Method declaration * * * @param bDrop * @param bInsert * @param bCached * @param channel * * @return * * @throws Exception */ public Result getScript(bool bDrop, bool bInsert, bool bCached, Channel channel) { channel.checkAdmin(); Result r = new Result(1); r.iType[0] = Column.VARCHAR; r.sTable[0] = "SYSTEM_SCRIPT"; r.sLabel[0] = "COMMAND"; r.sName[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.getName()); } a.Remove(0, a.Length); a.Append("CREATE "); if (t.isCached()) { a.Append("CACHED "); } a.Append("TABLE "); a.Append(t.getName()); a.Append("("); int columns = t.getColumnCount(); Index pki = t.getIndex("SYSTEM_PK"); int pk = (pki == null) ? -1 : pki.getColumns()[0]; for (int j = 0; j < columns; j++) { a.Append(t.getColumnName(j)); a.Append(" "); a.Append(Column.getType(t.getType(j))); if (!t.getColumnIsNullable(j)) { a.Append(" NOT NULL"); } if (j == t.getIdentityColumn()) { a.Append(" IDENTITY"); } if (j == pk) { a.Append(" PRIMARY KEY"); } if (j < columns - 1) { a.Append(","); } } ArrayList v = t.getConstraints(); for (int j = 0; j < v.Count; j++) { Constraint c = (Constraint)v[j]; if (c.getType() == Constraint.FOREIGN_KEY) { a.Append(",FOREIGN KEY"); int[] col = c.getRefColumns(); a.Append(getColumnList(c.getRef(), col, col.Length)); a.Append("REFERENCES "); a.Append(c.getMain().getName()); col = c.getMainColumns(); a.Append(getColumnList(c.getMain(), col, col.Length)); } else if (c.getType() == Constraint.UNIQUE) { a.Append(",UNIQUE"); int[] col = c.getMainColumns(); a.Append(getColumnList(c.getMain(), 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.getName(); 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.getName()); int[] col = index.getColumns(); int len = col.Length; if (!index.isUnique()) { len--; } a.Append(getColumnList(t, col, len)); addRow(r, a.ToString()); } if (bInsert) { Index primary = t.getPrimaryIndex(); 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(t.getName()); a.Append(" INDEX '"); a.Append(t.getIndexRoots()); 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.getName(); if (!name.Equals("PUBLIC")) { a.Remove(0, a.Length); a.Append("CREATE USER "); a.Append(name); a.Append(" PASSWORD "); a.Append("\"" + u.getPassword() + "\""); if (u.isAdmin()) { a.Append(" ADMIN"); } addRow(r, a.ToString()); } Hashtable rights = u.getRights(); if (rights == null) { continue; } foreach (string dbObject in rights.Keys) { int right = (int)rights[dbObject]; if (right == 0) { 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.getName()); addRow(r, a.ToString()); } } if (dDatabase.isIgnoreCase()) { addRow(r, "SET IGNORECASE TRUE"); } Hashtable h = dDatabase.getAlias(); foreach (string alias in h.Keys) { string java = (string)h[alias]; addRow(r, "CREATE ALIAS " + alias + " FOR \"" + java + "\""); } return(r); }
/** * Constructor declaration * * * @param db * @param tables * @param access */ public DatabaseInformation(Database db, ArrayList tables, Access access) { dDatabase = db; tTable = tables; aAccess = access; }