| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://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.SessionLocal; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.schema.FunctionAlias; |
| 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 |
|
| 30 |
public CreateFunctionAlias(SessionLocal session, Schema schema) { |
| 31 |
super(session, schema); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public long update() { |
| 36 |
session.getUser().checkAdmin(); |
| 37 |
Database db = getDatabase(); |
| 38 |
Schema schema = getSchema(); |
| 39 |
if (schema.findFunctionOrAggregate(aliasName) != null) { |
| 40 |
if (!ifNotExists) { |
| 41 |
throw DbException.get(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, aliasName); |
| 42 |
} |
| 43 |
} else { |
| 44 |
int id = getObjectId(); |
| 45 |
FunctionAlias functionAlias; |
| 46 |
if (javaClassMethod != null) { |
| 47 |
functionAlias = FunctionAlias.newInstance(schema, id, aliasName, javaClassMethod, force); |
| 48 |
} else { |
| 49 |
functionAlias = FunctionAlias.newInstanceFromSource(schema, id, aliasName, source, force); |
| 50 |
} |
| 51 |
functionAlias.setDeterministic(deterministic); |
| 52 |
db.addSchemaObject(session, functionAlias); |
| 53 |
} |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
public void setAliasName(String name) { |
| 58 |
this.aliasName = name; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Set the qualified method name after removing whitespace. |
| 63 |
* |
| 64 |
* @param method the qualified method name |
| 65 |
*/ |
| 66 |
public void setJavaClassMethod(String method) { |
| 67 |
this.javaClassMethod = StringUtils.replaceAll(method, " ", ""); |
| 68 |
} |
| 69 |
|
| 70 |
public void setIfNotExists(boolean ifNotExists) { |
| 71 |
this.ifNotExists = ifNotExists; |
| 72 |
} |
| 73 |
|
| 74 |
public void setForce(boolean force) { |
| 75 |
this.force = force; |
| 76 |
} |
| 77 |
|
| 78 |
public void setDeterministic(boolean deterministic) { |
| 79 |
this.deterministic = deterministic; |
| 80 |
} |
| 81 |
|
| 82 |
public void setSource(String source) { |
| 83 |
this.source = source; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public int getType() { |
| 88 |
return CommandInterface.CREATE_ALIAS; |
| 89 |
} |
| 90 |
|
| 91 |
} |