| 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.Schema; |
| 14 |
import org.h2.schema.UserAggregate; |
| 15 |
|
| 16 |
/** |
| 17 |
* This class represents the statement |
| 18 |
* CREATE AGGREGATE |
| 19 |
*/ |
| 20 |
public class CreateAggregate extends SchemaCommand { |
| 21 |
|
| 22 |
private String name; |
| 23 |
private String javaClassMethod; |
| 24 |
private boolean ifNotExists; |
| 25 |
private boolean force; |
| 26 |
|
| 27 |
public CreateAggregate(SessionLocal session, Schema schema) { |
| 28 |
super(session, schema); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public long update() { |
| 33 |
session.getUser().checkAdmin(); |
| 34 |
Database db = getDatabase(); |
| 35 |
Schema schema = getSchema(); |
| 36 |
if (schema.findFunctionOrAggregate(name) != null) { |
| 37 |
if (!ifNotExists) { |
| 38 |
throw DbException.get(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, name); |
| 39 |
} |
| 40 |
} else { |
| 41 |
int id = getObjectId(); |
| 42 |
UserAggregate aggregate = new UserAggregate(schema, id, name, javaClassMethod, force); |
| 43 |
db.addSchemaObject(session, aggregate); |
| 44 |
} |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
public void setName(String name) { |
| 49 |
this.name = name; |
| 50 |
} |
| 51 |
|
| 52 |
public void setJavaClassMethod(String string) { |
| 53 |
this.javaClassMethod = string; |
| 54 |
} |
| 55 |
|
| 56 |
public void setIfNotExists(boolean ifNotExists) { |
| 57 |
this.ifNotExists = ifNotExists; |
| 58 |
} |
| 59 |
|
| 60 |
public void setForce(boolean force) { |
| 61 |
this.force = force; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public int getType() { |
| 66 |
return CommandInterface.CREATE_AGGREGATE; |
| 67 |
} |
| 68 |
|
| 69 |
} |