public MetaRecord(DbObject obj) { id = obj.getId(); objectType = obj.getType(); sql = obj.getCreateSQL(); }
private String getUniqueName(DbObject obj, HashMap<String, ? extends SchemaObject> map, String prefix) { String hash = Integer.toHexString(obj.getName().hashCode()).toUpperCase(); String name = null; synchronized (temporaryUniqueNames) { for (int i = 1, len = hash.length(); i < len; i++) { name = prefix + hash.substring(0, i); if (!map.containsKey(name) && !temporaryUniqueNames.contains(name)) { break; } name = null; } if (name == null) { prefix = prefix + hash + "_"; for (int i = 0;; i++) { name = prefix + i; if (!map.containsKey(name) && !temporaryUniqueNames.contains(name)) { break; } } } temporaryUniqueNames.add(name); } return name; }
public Right(Database db, int id, RightOwner grantee, int grantedRight, DbObject grantedObject) { super(db, id, "" + id, Trace.USER); this.grantee = grantee; this.grantedRight = grantedRight; this.grantedObject = grantedObject; // TODO 如何更优雅的处理临时对象的授权(或者内存数据库中的所有对象的授权) // grantedObject有可能为null,如: GRANT ALTER ANY SCHEMA if (grantedObject != null && (grantedObject.isTemporary() || !grantedObject.getDatabase().isPersistent())) setTemporary(true); }
DbObjectType type = obj.getType(); synchronized (getLock(type)) { Map<String, DbObject> map = getMap(type); String oldName = obj.getName(); if (SysProperties.CHECK) { if (!map.containsKey(oldName)) { obj.checkRename(); int id = obj.getId(); obj.rename(newName); map.put(newName, obj);
/** * Remove the object from the database. * * @param session the session * @param obj the object to remove */ public void removeDatabaseObject(ServerSession session, DbObject obj) { checkWritingAllowed(); String objName = obj.getName(); DbObjectType type = obj.getType(); synchronized (getLock(type)) { Map<String, DbObject> map = getMap(type); if (SysProperties.CHECK && !map.containsKey(objName)) { DbException.throwInternalError("not found: " + objName); } Comment comment = findComment(obj); // lockMeta(session); if (comment != null) { removeDatabaseObject(session, comment); } int id = obj.getId(); obj.removeChildrenAndResources(session); map.remove(objName); removeMeta(session, id); } }
/** * Add an object to the database. * * @param session the session * @param obj the object to add */ public void addDatabaseObject(ServerSession session, DbObject obj) { int id = obj.getId(); if (id > 0 && !starting) { checkWritingAllowed(); } DbObjectType type = obj.getType(); synchronized (getLock(type)) { Map<String, DbObject> map = getMap(type); String name = obj.getName(); if (SysProperties.CHECK && map.get(name) != null) { DbException.throwInternalError("object already exists"); } // lockMeta(session); addMeta(session, obj); map.put(name, obj); } }
private void updateMetaAndFirstLevelChildren(ServerSession session, DbObject obj) { List<DbObject> list = obj.getChildren(); Comment comment = findComment(obj); if (comment != null) { DbException.throwInternalError(); } updateMeta(session, obj); // remember that this scans only one level deep! if (list != null) { for (DbObject o : list) { if (o.getCreateSQL() != null) { updateMeta(session, o); } } } }
private void addPrivilege(ArrayList<Row> rows, DbObject grantee, String catalog, Table table, String column, String right) { String isGrantable = "NO"; if (grantee.getType() == DbObjectType.USER) { User user = (User) grantee; if (user.isAdmin()) { identifier(grantee.getName()), identifier(grantee.getName()),
private void addMeta(ServerSession session, DbObject obj) { int id = obj.getId(); if (id > 0 && !starting && !obj.isTemporary()) { Row r = meta.getTemplateRow(); MetaRecord rec = new MetaRecord(obj); rec.setRecord(r); synchronized (objectIds) { objectIds.set(id); } // if (SysProperties.CHECK) { // verifyMetaLocked(session); // } meta.addRow(session, r); } }
/** * Check that a table or view is still valid. * * @param tableOrView the table or view to check */ private void checkViewsAreValid(DbObject tableOrView) { for (DbObject view : tableOrView.getChildren()) { if (view instanceof TableView) { String sql = ((TableView) view).getQuery(); // check if the query is still valid // do not execute, not even with limit 1, because that could // have side effects or take a very long time session.prepareStatement(sql); checkViewsAreValid(view); } } }
@Override public int update() { synchronized (getSchema().getLock(DbObjectType.TABLE_OR_VIEW)) { Database db = session.getDatabase(); session.getUser().checkRight(table, Right.ALL); table.checkSupportAlter(); // we need to update CHECK constraint // since it might reference the name of the column Expression newCheckExpr = (Expression) column.getCheckConstraint(session, newName); table.renameColumn(column, newName); column.removeCheckConstraint(); SingleColumnResolver resolver = new SingleColumnResolver(column); column.addCheckConstraint(session, newCheckExpr, resolver); table.setModified(); db.updateMeta(session, table); for (DbObject child : table.getChildren()) { if (child.getCreateSQL() != null) { db.updateMeta(session, child); } } } return 0; }
/** * Get the comment for the given database object if one exists, or null if * not. * * @param object the database object * @return the comment or null */ public Comment findComment(DbObject object) { if (object.getType() == DbObjectType.COMMENT) { return null; } String key = Comment.getKey(object); return comments.get(key); }
@Override public String getCreateSQL() { StringBuilder buff = new StringBuilder(); buff.append("GRANT "); if (grantedRole != null) { buff.append(grantedRole.getSQL()); } else { buff.append(getRights()); if (grantedObject != null) { if (grantedObject instanceof Schema) { buff.append(" ON SCHEMA ").append(grantedObject.getSQL()); } else if (grantedObject instanceof Table) { buff.append(" ON ").append(grantedObject.getSQL()); } } } buff.append(" TO ").append(grantee.getSQL()); return buff.toString(); }
/** * Update an object in the system table. * * @param session the session * @param obj the database object */ public void updateMeta(ServerSession session, DbObject obj) { // lockMeta(session); int id = obj.getId(); removeMeta(session, id); addMeta(session, obj); }
@Override public int update() { synchronized (getSchema().getLock(DbObjectType.TABLE_OR_VIEW)) { Table view = getSchema().findTableOrView(session, viewName); if (view == null) { if (!ifExists) { throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); } } else { if (view.getTableType() != TableType.VIEW) { throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); } session.getUser().checkRight(view, Right.ALL); if (dropAction == ConstraintReferential.RESTRICT) { for (DbObject child : view.getChildren()) { if (child instanceof TableView) { throw DbException.get(ErrorCode.CANNOT_DROP_2, viewName, child.getName()); } } } view.lock(session, true, true); session.getDatabase().removeSchemaObject(session, view); } } return 0; }