01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.engine;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.api.AggregateFunction;
11: import org.h2.command.Parser;
12: import org.h2.message.Message;
13: import org.h2.message.Trace;
14: import org.h2.table.Table;
15:
16: /**
17: * Represents a user defined aggregate function.
18: */
19: public class UserAggregate extends DbObjectBase {
20:
21: private String className;
22: private Class javaClass;
23:
24: public UserAggregate(Database db, int id, String name,
25: String className, boolean force) throws SQLException {
26: super (db, id, name, Trace.FUNCTION);
27: this .className = className;
28: if (!force) {
29: getInstance();
30: }
31: }
32:
33: public AggregateFunction getInstance() throws SQLException {
34: if (javaClass == null) {
35: javaClass = database.loadUserClass(className);
36: }
37: Object obj;
38: try {
39: obj = javaClass.newInstance();
40: AggregateFunction agg = (AggregateFunction) obj;
41: return agg;
42: } catch (Exception e) {
43: throw Message.convert(e);
44: }
45: }
46:
47: public String getCreateSQLForCopy(Table table, String quotedName) {
48: throw Message.getInternalError();
49: }
50:
51: public String getDropSQL() {
52: return "DROP AGGREGATE IF EXISTS " + getSQL();
53: }
54:
55: public String getCreateSQL() {
56: StringBuffer buff = new StringBuffer();
57: buff.append("CREATE FORCE AGGREGATE ");
58: buff.append(getSQL());
59: buff.append(" FOR ");
60: buff.append(Parser.quoteIdentifier(className));
61: return buff.toString();
62: }
63:
64: public int getType() {
65: return DbObject.AGGREGATE;
66: }
67:
68: public synchronized void removeChildrenAndResources(Session session)
69: throws SQLException {
70: database.removeMeta(session, getId());
71: className = null;
72: javaClass = null;
73: invalidate();
74: }
75:
76: public void checkRename() throws SQLException {
77: throw Message.getUnsupportedException();
78: }
79:
80: public String getJavaClassName() {
81: return this.className;
82: }
83:
84: }
|