EMMA Coverage Report (generated Sun Mar 01 22:06:14 CET 2015)
[all classes][org.h2.engine]

COVERAGE SUMMARY FOR SOURCE FILE [UserAggregate.java]

nameclass, %method, %block, %line, %
UserAggregate.java100% (2/2)79%  (11/14)87%  (125/144)87%  (33/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class UserAggregate100% (1/1)67%  (6/9)81%  (80/99)81%  (21/26)
checkRename (): void 0%   (0/1)0%   (0/3)0%   (0/1)
getCreateSQLForCopy (Table, String): String 0%   (0/1)0%   (0/2)0%   (0/1)
getDropSQL (): String 0%   (0/1)0%   (0/10)0%   (0/1)
getInstance (): Aggregate 100% (1/1)87%  (27/31)78%  (7/9)
UserAggregate (Database, int, String, String, boolean): void 100% (1/1)100% (17/17)100% (6/6)
getCreateSQL (): String 100% (1/1)100% (16/16)100% (1/1)
getJavaClassName (): String 100% (1/1)100% (3/3)100% (1/1)
getType (): int 100% (1/1)100% (2/2)100% (1/1)
removeChildrenAndResources (Session): void 100% (1/1)100% (15/15)100% (5/5)
     
class UserAggregate$AggregateWrapper100% (1/1)100% (5/5)100% (45/45)100% (12/12)
UserAggregate$AggregateWrapper (AggregateFunction): void 100% (1/1)100% (6/6)100% (3/3)
add (Object): void 100% (1/1)100% (5/5)100% (2/2)
getInternalType (int []): int 100% (1/1)100% (25/25)100% (4/4)
getResult (): Object 100% (1/1)100% (4/4)100% (1/1)
init (Connection): void 100% (1/1)100% (5/5)100% (2/2)

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 */
6package org.h2.engine;
7 
8import java.sql.Connection;
9import java.sql.SQLException;
10 
11import org.h2.api.Aggregate;
12import org.h2.api.AggregateFunction;
13import org.h2.command.Parser;
14import org.h2.message.DbException;
15import org.h2.message.Trace;
16import org.h2.table.Table;
17import org.h2.util.JdbcUtils;
18import org.h2.value.DataType;
19 
20/**
21 * Represents a user-defined aggregate function.
22 */
23public class UserAggregate extends DbObjectBase {
24 
25    private String className;
26    private Class<?> javaClass;
27 
28    public UserAggregate(Database db, int id, String name, String className,
29            boolean force) {
30        initDbObjectBase(db, id, name, Trace.FUNCTION);
31        this.className = className;
32        if (!force) {
33            getInstance();
34        }
35    }
36 
37    public Aggregate getInstance() {
38        if (javaClass == null) {
39            javaClass = JdbcUtils.loadUserClass(className);
40        }
41        Object obj;
42        try {
43            obj = javaClass.newInstance();
44            Aggregate agg;
45            if (obj instanceof Aggregate) {
46                agg = (Aggregate) obj;
47            } else {
48                agg = new AggregateWrapper((AggregateFunction) obj);
49            }
50            return agg;
51        } catch (Exception e) {
52            throw DbException.convert(e);
53        }
54    }
55 
56    @Override
57    public String getCreateSQLForCopy(Table table, String quotedName) {
58        throw DbException.throwInternalError();
59    }
60 
61    @Override
62    public String getDropSQL() {
63        return "DROP AGGREGATE IF EXISTS " + getSQL();
64    }
65 
66    @Override
67    public String getCreateSQL() {
68        return "CREATE FORCE AGGREGATE " + getSQL() +
69                " FOR " + Parser.quoteIdentifier(className);
70    }
71 
72    @Override
73    public int getType() {
74        return DbObject.AGGREGATE;
75    }
76 
77    @Override
78    public synchronized void removeChildrenAndResources(Session session) {
79        database.removeMeta(session, getId());
80        className = null;
81        javaClass = null;
82        invalidate();
83    }
84 
85    @Override
86    public void checkRename() {
87        throw DbException.getUnsupportedException("AGGREGATE");
88    }
89 
90    public String getJavaClassName() {
91        return this.className;
92    }
93 
94    /**
95     * Wrap {@link AggregateFunction} in order to behave as
96     * {@link org.h2.api.Aggregate}
97     **/
98    private static class AggregateWrapper implements Aggregate {
99        private final AggregateFunction aggregateFunction;
100 
101        AggregateWrapper(AggregateFunction aggregateFunction) {
102            this.aggregateFunction = aggregateFunction;
103        }
104 
105        @Override
106        public void init(Connection conn) throws SQLException {
107            aggregateFunction.init(conn);
108        }
109 
110        @Override
111        public int getInternalType(int[] inputTypes) throws SQLException {
112            int[] sqlTypes = new int[inputTypes.length];
113            for (int i = 0; i < inputTypes.length; i++) {
114                sqlTypes[i] = DataType.convertTypeToSQLType(inputTypes[i]);
115            }
116            return  DataType.convertSQLTypeToValueType(aggregateFunction.getType(sqlTypes));
117        }
118 
119        @Override
120        public void add(Object value) throws SQLException {
121            aggregateFunction.add(value);
122        }
123 
124        @Override
125        public Object getResult() throws SQLException {
126            return aggregateFunction.getResult();
127        }
128    }
129 
130}

[all classes][org.h2.engine]
EMMA 2.0.5312 (C) Vladimir Roubtsov