KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > engine > MetaRecord


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.engine;
6
7 import java.sql.SQLException JavaDoc;
8 import java.util.Comparator JavaDoc;
9
10 import org.h2.api.DatabaseEventListener;
11 import org.h2.command.Prepared;
12 import org.h2.message.Message;
13 import org.h2.message.Trace;
14 import org.h2.result.SearchRow;
15 import org.h2.util.ObjectArray;
16 import org.h2.value.ValueInt;
17 import org.h2.value.ValueString;
18
19 public class MetaRecord {
20     private int id;
21     private int objectType;
22     private int headPos;
23     private String JavaDoc sql;
24     
25     public MetaRecord(SearchRow r) throws SQLException JavaDoc {
26         id = r.getValue(0).getInt();
27         headPos = r.getValue(1).getInt();
28         objectType = r.getValue(2).getInt();
29         sql = r.getValue(3).getString();
30     }
31     
32     public static void sort(ObjectArray records) {
33         records.sort(new Comparator JavaDoc() {
34             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
35                 MetaRecord m1 = (MetaRecord)o1;
36                 MetaRecord m2 = (MetaRecord)o2;
37                 int c1 = DbObject.getCreateOrder(m1.getObjectType());
38                 int c2 = DbObject.getCreateOrder(m2.getObjectType());
39                 if(c1 != c2) {
40                     return c1 - c2;
41                 }
42                 return m1.getId() - m2.getId();
43             }
44         });
45     }
46     
47     public void setRecord(SearchRow r) {
48         r.setValue(0, ValueInt.get(id));
49         r.setValue(1, ValueInt.get(headPos));
50         r.setValue(2, ValueInt.get(objectType));
51         r.setValue(3, ValueString.get(sql));
52     }
53     
54     public MetaRecord(DbObject obj) {
55         id = obj.getId();
56         objectType = obj.getType();
57         headPos = obj.getHeadPos();
58         sql = obj.getCreateSQL();
59     }
60
61     void execute(Database db, Session systemSession, DatabaseEventListener listener) throws SQLException JavaDoc {
62         try {
63             Prepared command = systemSession.prepare(sql);
64             command.setObjectId(id);
65             command.setHeadPos(headPos);
66             command.update();
67         } catch(Throwable JavaDoc e) {
68             SQLException JavaDoc s = Message.convert(e);
69             db.getTrace(Trace.DATABASE).error(sql, s);
70             if(listener != null) {
71                 listener.exceptionThrown(s);
72                 // continue startup in this case
73
} else {
74                 throw s;
75             }
76         }
77     }
78
79     public int getHeadPos() {
80         return headPos;
81     }
82
83     public void setHeadPos(int headPos) {
84         this.headPos = headPos;
85     }
86
87     public int getId() {
88         return id;
89     }
90
91     public void setId(int id) {
92         this.id = id;
93     }
94
95     public int getObjectType() {
96         return objectType;
97     }
98
99     public void setObjectType(int objectType) {
100         this.objectType = objectType;
101     }
102
103     public String JavaDoc getSQL() {
104         return sql;
105     }
106
107     public void setSql(String JavaDoc sql) {
108         this.sql = sql;
109     }
110
111 }
112
Popular Tags