1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.engine; |
7 | |
8 | import java.sql.SQLException; |
9 | import org.h2.api.DatabaseEventListener; |
10 | import org.h2.command.Prepared; |
11 | import org.h2.message.DbException; |
12 | import org.h2.message.Trace; |
13 | import org.h2.result.SearchRow; |
14 | import org.h2.value.ValueInt; |
15 | import org.h2.value.ValueString; |
16 | |
17 | /** |
18 | * A record in the system table of the database. |
19 | * It contains the SQL statement to create the database object. |
20 | */ |
21 | public class MetaRecord implements Comparable<MetaRecord> { |
22 | |
23 | private final int id; |
24 | private final int objectType; |
25 | private final String sql; |
26 | |
27 | public MetaRecord(SearchRow r) { |
28 | id = r.getValue(0).getInt(); |
29 | objectType = r.getValue(2).getInt(); |
30 | sql = r.getValue(3).getString(); |
31 | } |
32 | |
33 | MetaRecord(DbObject obj) { |
34 | id = obj.getId(); |
35 | objectType = obj.getType(); |
36 | sql = obj.getCreateSQL(); |
37 | } |
38 | |
39 | void setRecord(SearchRow r) { |
40 | r.setValue(0, ValueInt.get(id)); |
41 | r.setValue(1, ValueInt.get(0)); |
42 | r.setValue(2, ValueInt.get(objectType)); |
43 | r.setValue(3, ValueString.get(sql)); |
44 | } |
45 | |
46 | /** |
47 | * Execute the meta data statement. |
48 | * |
49 | * @param db the database |
50 | * @param systemSession the system session |
51 | * @param listener the database event listener |
52 | */ |
53 | void execute(Database db, Session systemSession, |
54 | DatabaseEventListener listener) { |
55 | try { |
56 | Prepared command = systemSession.prepare(sql); |
57 | command.setObjectId(id); |
58 | command.update(); |
59 | } catch (DbException e) { |
60 | e = e.addSQL(sql); |
61 | SQLException s = e.getSQLException(); |
62 | db.getTrace(Trace.DATABASE).error(s, sql); |
63 | if (listener != null) { |
64 | listener.exceptionThrown(s, sql); |
65 | // continue startup in this case |
66 | } else { |
67 | throw e; |
68 | } |
69 | } |
70 | } |
71 | |
72 | public int getId() { |
73 | return id; |
74 | } |
75 | |
76 | public int getObjectType() { |
77 | return objectType; |
78 | } |
79 | |
80 | public String getSQL() { |
81 | return sql; |
82 | } |
83 | |
84 | /** |
85 | * Sort the list of meta records by 'create order'. |
86 | * |
87 | * @param other the other record |
88 | * @return -1, 0, or 1 |
89 | */ |
90 | @Override |
91 | public int compareTo(MetaRecord other) { |
92 | int c1 = getCreateOrder(); |
93 | int c2 = other.getCreateOrder(); |
94 | if (c1 != c2) { |
95 | return c1 - c2; |
96 | } |
97 | return getId() - other.getId(); |
98 | } |
99 | |
100 | /** |
101 | * Get the sort order id for this object type. Objects are created in this |
102 | * order when opening a database. |
103 | * |
104 | * @return the sort index |
105 | */ |
106 | private int getCreateOrder() { |
107 | switch(objectType) { |
108 | case DbObject.SETTING: |
109 | return 0; |
110 | case DbObject.USER: |
111 | return 1; |
112 | case DbObject.SCHEMA: |
113 | return 2; |
114 | case DbObject.FUNCTION_ALIAS: |
115 | return 3; |
116 | case DbObject.USER_DATATYPE: |
117 | return 4; |
118 | case DbObject.SEQUENCE: |
119 | return 5; |
120 | case DbObject.CONSTANT: |
121 | return 6; |
122 | case DbObject.TABLE_OR_VIEW: |
123 | return 7; |
124 | case DbObject.INDEX: |
125 | return 8; |
126 | case DbObject.CONSTRAINT: |
127 | return 9; |
128 | case DbObject.TRIGGER: |
129 | return 10; |
130 | case DbObject.ROLE: |
131 | return 11; |
132 | case DbObject.RIGHT: |
133 | return 12; |
134 | case DbObject.AGGREGATE: |
135 | return 13; |
136 | case DbObject.COMMENT: |
137 | return 14; |
138 | default: |
139 | throw DbException.throwInternalError("type="+objectType); |
140 | } |
141 | } |
142 | |
143 | @Override |
144 | public String toString() { |
145 | return "MetaRecord [id=" + id + ", objectType=" + objectType + |
146 | ", sql=" + sql + "]"; |
147 | } |
148 | |
149 | } |