001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.engine;
007:
008: import java.sql.SQLException;
009: import java.util.Comparator;
010:
011: import org.h2.api.DatabaseEventListener;
012: import org.h2.command.Prepared;
013: import org.h2.message.Message;
014: import org.h2.message.Trace;
015: import org.h2.result.SearchRow;
016: import org.h2.util.ObjectArray;
017: import org.h2.value.ValueInt;
018: import org.h2.value.ValueString;
019:
020: /**
021: * A record in the system table of the database.
022: * It contains the SQL statement to create the database object.
023: */
024: public class MetaRecord {
025:
026: private int id;
027: private int objectType;
028: private int headPos;
029: private String sql;
030:
031: public MetaRecord(SearchRow r) throws SQLException {
032: id = r.getValue(0).getInt();
033: headPos = r.getValue(1).getInt();
034: objectType = r.getValue(2).getInt();
035: sql = r.getValue(3).getString();
036: }
037:
038: public static void sort(ObjectArray records) {
039: records.sort(new Comparator() {
040: public int compare(Object o1, Object o2) {
041: MetaRecord m1 = (MetaRecord) o1;
042: MetaRecord m2 = (MetaRecord) o2;
043: int c1 = DbObjectBase
044: .getCreateOrder(m1.getObjectType());
045: int c2 = DbObjectBase
046: .getCreateOrder(m2.getObjectType());
047: if (c1 != c2) {
048: return c1 - c2;
049: }
050: return m1.getId() - m2.getId();
051: }
052: });
053: }
054:
055: public void setRecord(SearchRow r) {
056: r.setValue(0, ValueInt.get(id));
057: r.setValue(1, ValueInt.get(headPos));
058: r.setValue(2, ValueInt.get(objectType));
059: r.setValue(3, ValueString.get(sql));
060: }
061:
062: public MetaRecord(DbObject obj) {
063: id = obj.getId();
064: objectType = obj.getType();
065: headPos = obj.getHeadPos();
066: sql = obj.getCreateSQL();
067: }
068:
069: void execute(Database db, Session systemSession,
070: DatabaseEventListener listener) throws SQLException {
071: try {
072: Prepared command = systemSession.prepare(sql);
073: command.setObjectId(id);
074: command.setHeadPos(headPos);
075: command.update();
076: } catch (Throwable e) {
077: SQLException s = Message.addSQL(Message.convert(e), sql);
078: db.getTrace(Trace.DATABASE).error(sql, s);
079: if (listener != null) {
080: listener.exceptionThrown(s, sql);
081: // continue startup in this case
082: } else {
083: throw s;
084: }
085: }
086: }
087:
088: public int getHeadPos() {
089: return headPos;
090: }
091:
092: public void setHeadPos(int headPos) {
093: this .headPos = headPos;
094: }
095:
096: public int getId() {
097: return id;
098: }
099:
100: public void setId(int id) {
101: this .id = id;
102: }
103:
104: public int getObjectType() {
105: return objectType;
106: }
107:
108: public void setObjectType(int objectType) {
109: this .objectType = objectType;
110: }
111:
112: public String getSQL() {
113: return sql;
114: }
115:
116: public void setSql(String sql) {
117: this.sql = sql;
118: }
119:
120: }
|