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.FunctionAlias; |
12 | import org.h2.engine.Session; |
13 | import org.h2.message.DbException; |
14 | import org.h2.schema.Schema; |
15 | import org.h2.util.StringUtils; |
16 | |
17 | /** |
18 | * This class represents the statement |
19 | * CREATE ALIAS |
20 | */ |
21 | public class CreateFunctionAlias extends SchemaCommand { |
22 | |
23 | private String aliasName; |
24 | private String javaClassMethod; |
25 | private boolean deterministic; |
26 | private boolean ifNotExists; |
27 | private boolean force; |
28 | private String source; |
29 | private boolean bufferResultSetToLocalTemp = true; |
30 | |
31 | public CreateFunctionAlias(Session session, Schema schema) { |
32 | super(session, schema); |
33 | } |
34 | |
35 | @Override |
36 | public int update() { |
37 | session.commit(true); |
38 | session.getUser().checkAdmin(); |
39 | Database db = session.getDatabase(); |
40 | if (getSchema().findFunction(aliasName) != null) { |
41 | if (!ifNotExists) { |
42 | throw DbException.get( |
43 | ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, aliasName); |
44 | } |
45 | } else { |
46 | int id = getObjectId(); |
47 | FunctionAlias functionAlias; |
48 | if (javaClassMethod != null) { |
49 | functionAlias = FunctionAlias.newInstance(getSchema(), id, |
50 | aliasName, javaClassMethod, force, |
51 | bufferResultSetToLocalTemp); |
52 | } else { |
53 | functionAlias = FunctionAlias.newInstanceFromSource( |
54 | getSchema(), id, aliasName, source, force, |
55 | bufferResultSetToLocalTemp); |
56 | } |
57 | functionAlias.setDeterministic(deterministic); |
58 | db.addSchemaObject(session, functionAlias); |
59 | } |
60 | return 0; |
61 | } |
62 | |
63 | public void setAliasName(String name) { |
64 | this.aliasName = name; |
65 | } |
66 | |
67 | /** |
68 | * Set the qualified method name after removing whitespace. |
69 | * |
70 | * @param method the qualified method name |
71 | */ |
72 | public void setJavaClassMethod(String method) { |
73 | this.javaClassMethod = StringUtils.replaceAll(method, " ", ""); |
74 | } |
75 | |
76 | public void setIfNotExists(boolean ifNotExists) { |
77 | this.ifNotExists = ifNotExists; |
78 | } |
79 | |
80 | public void setForce(boolean force) { |
81 | this.force = force; |
82 | } |
83 | |
84 | public void setDeterministic(boolean deterministic) { |
85 | this.deterministic = deterministic; |
86 | } |
87 | |
88 | /** |
89 | * Should the return value ResultSet be buffered in a local temporary file? |
90 | * |
91 | * @param b the new value |
92 | */ |
93 | public void setBufferResultSetToLocalTemp(boolean b) { |
94 | this.bufferResultSetToLocalTemp = b; |
95 | } |
96 | |
97 | public void setSource(String source) { |
98 | this.source = source; |
99 | } |
100 | |
101 | @Override |
102 | public int getType() { |
103 | return CommandInterface.CREATE_ALIAS; |
104 | } |
105 | |
106 | } |