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 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import org.h2.api.ErrorCode; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.engine.Database; |
11 | import org.h2.engine.Session; |
12 | import org.h2.engine.UserAggregate; |
13 | import org.h2.message.DbException; |
14 | import org.h2.schema.Schema; |
15 | |
16 | /** |
17 | * This class represents the statement |
18 | * CREATE AGGREGATE |
19 | */ |
20 | public class CreateAggregate extends DefineCommand { |
21 | |
22 | private Schema schema; |
23 | private String name; |
24 | private String javaClassMethod; |
25 | private boolean ifNotExists; |
26 | private boolean force; |
27 | |
28 | public CreateAggregate(Session session) { |
29 | super(session); |
30 | } |
31 | |
32 | @Override |
33 | public int update() { |
34 | session.commit(true); |
35 | session.getUser().checkAdmin(); |
36 | Database db = session.getDatabase(); |
37 | if (db.findAggregate(name) != null || schema.findFunction(name) != null) { |
38 | if (!ifNotExists) { |
39 | throw DbException.get( |
40 | ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, name); |
41 | } |
42 | } else { |
43 | int id = getObjectId(); |
44 | UserAggregate aggregate = new UserAggregate( |
45 | db, id, name, javaClassMethod, force); |
46 | db.addDatabaseObject(session, aggregate); |
47 | } |
48 | return 0; |
49 | } |
50 | |
51 | public void setSchema(Schema schema) { |
52 | this.schema = schema; |
53 | } |
54 | |
55 | public void setName(String name) { |
56 | this.name = name; |
57 | } |
58 | |
59 | public void setJavaClassMethod(String string) { |
60 | this.javaClassMethod = string; |
61 | } |
62 | |
63 | public void setIfNotExists(boolean ifNotExists) { |
64 | this.ifNotExists = ifNotExists; |
65 | } |
66 | |
67 | public void setForce(boolean force) { |
68 | this.force = force; |
69 | } |
70 | |
71 | @Override |
72 | public int getType() { |
73 | return CommandInterface.CREATE_AGGREGATE; |
74 | } |
75 | |
76 | } |