Debian Codegraph @

src/main/org/h2/engine/MetaRecord.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.engine;
7
8 import java.sql.SQLException;
9 import java.util.Comparator;
10 import org.h2.api.DatabaseEventListener;
11 import org.h2.command.CommandInterface;
12 import org.h2.command.Prepared;
13 import org.h2.message.DbException;
14 import org.h2.message.Trace;
15 import org.h2.result.SearchRow;
16 import org.h2.value.ValueInteger;
17 import org.h2.value.ValueVarchar;
18
19 /**
20 * A record in the system table of the database.
21 * It contains the SQL statement to create the database object.
22 */
23 public class MetaRecord implements Comparable<MetaRecord> {
24
25 /**
26 * Comparator for prepared constraints, sorts unique and primary key
27 * constraints first.
28 */
29 static final Comparator<Prepared> CONSTRAINTS_COMPARATOR = (o1, o2) -> {
30 int t1 = o1.getType(), t2 = o2.getType();
31 boolean u1 = t1 == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY
32 || t1 == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE;
33 boolean u2 = t2 == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY
34 || t2 == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE;
35 if (u1 == u2) {
36 return o1.getPersistedObjectId() - o2.getPersistedObjectId();
37 }
38 return u1 ? -1 : 1;
39 };
40
41 private final int id;
42 private final int objectType;
43 private final String sql;
44
45 /**
46 * Copy metadata from the specified object into specified search row.
47 *
48 * @param obj
49 * database object
50 * @param r
51 * search row
52 */
53 public static void populateRowFromDBObject(DbObject obj, SearchRow r) {
54 r.setValue(0, ValueInteger.get(obj.getId()));
55 r.setValue(1, ValueInteger.get(0));
56 r.setValue(2, ValueInteger.get(obj.getType()));
57 r.setValue(3, ValueVarchar.get(obj.getCreateSQLForMeta()));
58 }
59
60 public MetaRecord(SearchRow r) {
61 id = r.getValue(0).getInt();
62 objectType = r.getValue(2).getInt();
63 sql = r.getValue(3).getString();
64 }
65
66 /**
67 * Execute the meta data statement.
68 *
69 * @param db the database
70 * @param systemSession the system session
71 * @param listener the database event listener
72 */
73 void prepareAndExecute(Database db, SessionLocal systemSession, DatabaseEventListener listener) {
74 try {
75 Prepared command = systemSession.prepare(sql);
76 command.setPersistedObjectId(id);
77 command.update();
78 } catch (DbException e) {
79 throwException(db, listener, e, sql);
80 }
81 }
82
83 /**
84 * Prepares the meta data statement.
85 *
86 * @param db the database
87 * @param systemSession the system session
88 * @param listener the database event listener
89 * @return the prepared command
90 */
91 Prepared prepare(Database db, SessionLocal systemSession, DatabaseEventListener listener) {
92 try {
93 Prepared command = systemSession.prepare(sql);
94 command.setPersistedObjectId(id);
95 return command;
96 } catch (DbException e) {
97 throwException(db, listener, e, sql);
98 return null;
99 }
100 }
101
102 /**
103 * Execute the meta data statement.
104 *
105 * @param db the database
106 * @param command the prepared command
107 * @param listener the database event listener
108 * @param sql SQL
109 */
110 static void execute(Database db, Prepared command, DatabaseEventListener listener, String sql) {
111 try {
112 command.update();
113 } catch (DbException e) {
114 throwException(db, listener, e, sql);
115 }
116 }
117
118 private static void throwException(Database db, DatabaseEventListener listener, DbException e, String sql) {
119 e = e.addSQL(sql);
120 SQLException s = e.getSQLException();
121 db.getTrace(Trace.DATABASE).error(s, sql);
122 if (listener != null) {
123 listener.exceptionThrown(s, sql);
124 // continue startup in this case
125 } else {
126 throw e;
127 }
128 }
129
130 public int getId() {
131 return id;
132 }
133
134 public int getObjectType() {
135 return objectType;
136 }
137
138 public String getSQL() {
139 return sql;
140 }
141
142 /**
143 * Sort the list of meta records by 'create order'.
144 *
145 * @param other the other record
146 * @return -1, 0, or 1
147 */
148 @Override
149 public int compareTo(MetaRecord other) {
150 int c1 = getCreateOrder();
151 int c2 = other.getCreateOrder();
152 if (c1 != c2) {
153 return c1 - c2;
154 }
155 return getId() - other.getId();
156 }
157
158 /**
159 * Get the sort order id for this object type. Objects are created in this
160 * order when opening a database.
161 *
162 * @return the sort index
163 */
164 private int getCreateOrder() {
165 switch (objectType) {
166 case DbObject.SETTING:
167 return 0;
168 case DbObject.USER:
169 return 1;
170 case DbObject.SCHEMA:
171 return 2;
172 case DbObject.FUNCTION_ALIAS:
173 return 3;
174 case DbObject.DOMAIN:
175 return 4;
176 case DbObject.SEQUENCE:
177 return 5;
178 case DbObject.CONSTANT:
179 return 6;
180 case DbObject.TABLE_OR_VIEW:
181 return 7;
182 case DbObject.INDEX:
183 return 8;
184 case DbObject.CONSTRAINT:
185 return 9;
186 case DbObject.TRIGGER:
187 return 10;
188 case DbObject.SYNONYM:
189 return 11;
190 case DbObject.ROLE:
191 return 12;
192 case DbObject.RIGHT:
193 return 13;
194 case DbObject.AGGREGATE:
195 return 14;
196 case DbObject.COMMENT:
197 return 15;
198 default:
199 throw DbException.getInternalError("type=" + objectType);
200 }
201 }
202
203 @Override
204 public String toString() {
205 return "MetaRecord [id=" + id + ", objectType=" + objectType + ", sql=" + sql + ']';
206 }
207
208 }