File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\engine\MetaRecord.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.engine;
8
9 import java.sql.SQLException;
10 import org.h2.api.DatabaseEventListener;
11 import org.h2.command.Prepared;
12 import org.h2.message.DbException;
13 import org.h2.message.Trace;
14 import org.h2.result.SearchRow;
15 import org.h2.value.ValueInt;
16 import org.h2.value.ValueString;
17
18 /**
19 * A record in the system table of the database.
20 * It contains the SQL statement to create the database object.
21 */
22 public class MetaRecord implements Comparable<MetaRecord> {
23
24 private final int id;
25 private final int objectType;
26 private final String sql;
27
28 public MetaRecord(SearchRow r) {
29 id = r.getValue(0).getInt();
30 objectType = r.getValue(2).getInt();
31 sql = r.getValue(3).getString();
32 }
33
34 MetaRecord(DbObject obj) {
35 id = obj.getId();
36 objectType = obj.getType();
37 sql = obj.getCreateSQL();
38 }
39
40 void setRecord(SearchRow r) {
41 r.setValue(0, ValueInt.get(id));
42 r.setValue(1, ValueInt.get(0));
43 r.setValue(2, ValueInt.get(objectType));
44 r.setValue(3, ValueString.get(sql));
45 }
46
47 /**
48 * Execute the meta data statement.
49 *
50 * @param db the database
51 * @param systemSession the system session
52 * @param listener the database event listener
53 */
54 void execute(Database db, Session systemSession, 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 public int compareTo(MetaRecord other) {
91 int c1 = getCreateOrder();
92 int c2 = other.getCreateOrder();
93 if (c1 != c2) {
94 return c1 - c2;
95 }
96 return getId() - other.getId();
97 }
98
99 /**
100 * Get the sort order id for this object type. Objects are created in this
101 * order when opening a database.
102 *
103 * @return the sort index
104 */
105 private int getCreateOrder() {
106 switch(objectType) {
107 case DbObject.SETTING:
108 return 0;
109 case DbObject.USER:
110 return 1;
111 case DbObject.SCHEMA:
112 return 2;
113 case DbObject.FUNCTION_ALIAS:
114 return 3;
115 case DbObject.USER_DATATYPE:
116 return 4;
117 case DbObject.SEQUENCE:
118 return 5;
119 case DbObject.CONSTANT:
120 return 6;
121 case DbObject.TABLE_OR_VIEW:
122 return 7;
123 case DbObject.INDEX:
124 return 8;
125 case DbObject.CONSTRAINT:
126 return 9;
127 case DbObject.TRIGGER:
128 return 10;
129 case DbObject.ROLE:
130 return 11;
131 case DbObject.RIGHT:
132 return 12;
133 case DbObject.AGGREGATE:
134 return 13;
135 case DbObject.COMMENT:
136 return 14;
137 default:
138 throw DbException.throwInternalError("type="+objectType);
139 }
140 }
141
142 @Override
143 public String toString() {
144 return "MetaRecord [id=" + id + ", objectType=" + objectType + ", sql=" + sql + "]";
145 }
146
147 }
148