Debian Codegraph @

src/main/org/h2/command/ddl/AlterDomain.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command.ddl;
7
8 import java.util.function.BiPredicate;
9
10 import org.h2.api.ErrorCode;
11 import org.h2.engine.Database;
12 import org.h2.engine.SessionLocal;
13 import org.h2.message.DbException;
14 import org.h2.schema.Domain;
15 import org.h2.schema.Schema;
16 import org.h2.table.Column;
17 import org.h2.table.Table;
18
19 /**
20 * The base class for ALTER DOMAIN commands.
21 */
22 public abstract class AlterDomain extends SchemaOwnerCommand {
23
24 /**
25 * Processes all columns and domains that use the specified domain.
26 *
27 * @param session
28 * the session
29 * @param domain
30 * the domain to process
31 * @param columnProcessor
32 * column handler
33 * @param domainProcessor
34 * domain handler
35 * @param recompileExpressions
36 * whether processed expressions need to be recompiled
37 */
38 public static void forAllDependencies(SessionLocal session, Domain domain,
39 BiPredicate<Domain, Column> columnProcessor, BiPredicate<Domain, Domain> domainProcessor,
40 boolean recompileExpressions) {
41 Database db = session.getDatabase();
42 for (Schema schema : db.getAllSchemasNoMeta()) {
43 for (Domain targetDomain : schema.getAllDomains()) {
44 if (targetDomain.getDomain() == domain) {
45 if (domainProcessor == null || domainProcessor.test(domain, targetDomain)) {
46 if (recompileExpressions) {
47 domain.prepareExpressions(session);
48 }
49 db.updateMeta(session, targetDomain);
50 }
51 }
52 }
53 for (Table t : schema.getAllTablesAndViews(null)) {
54 if (forTable(session, domain, columnProcessor, recompileExpressions, t)) {
55 db.updateMeta(session, t);
56 }
57 }
58 }
59 for (Table t : session.getLocalTempTables()) {
60 forTable(session, domain, columnProcessor, recompileExpressions, t);
61 }
62 }
63
64 private static boolean forTable(SessionLocal session, Domain domain, BiPredicate<Domain, Column> columnProcessor,
65 boolean recompileExpressions, Table t) {
66 boolean modified = false;
67 for (Column targetColumn : t.getColumns()) {
68 if (targetColumn.getDomain() == domain) {
69 boolean m = columnProcessor == null || columnProcessor.test(domain, targetColumn);
70 if (m) {
71 if (recompileExpressions) {
72 targetColumn.prepareExpressions(session);
73 }
74 modified = true;
75 }
76 }
77 }
78 return modified;
79 }
80
81 String domainName;
82
83 boolean ifDomainExists;
84
85 AlterDomain(SessionLocal session, Schema schema) {
86 super(session, schema);
87 }
88
89 public final void setDomainName(String domainName) {
90 this.domainName = domainName;
91 }
92
93 public final void setIfDomainExists(boolean b) {
94 ifDomainExists = b;
95 }
96
97 @Override
98 final long update(Schema schema) {
99 Domain domain = getSchema().findDomain(domainName);
100 if (domain == null) {
101 if (ifDomainExists) {
102 return 0;
103 }
104 throw DbException.get(ErrorCode.DOMAIN_NOT_FOUND_1, domainName);
105 }
106 return update(schema, domain);
107 }
108
109 abstract long update(Schema schema, Domain domain);
110
111 }