01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.constant.ErrorCode;
11: import org.h2.engine.Database;
12: import org.h2.engine.Session;
13: import org.h2.engine.UserDataType;
14: import org.h2.message.Message;
15: import org.h2.table.Column;
16:
17: /**
18: * This class represents the statement
19: * CREATE DOMAIN
20: */
21: public class CreateUserDataType extends DefineCommand {
22:
23: private String typeName;
24: private Column column;
25: private boolean ifNotExists;
26:
27: public CreateUserDataType(Session session) {
28: super (session);
29: }
30:
31: public void setTypeName(String name) {
32: this .typeName = name;
33: }
34:
35: public void setColumn(Column column) {
36: this .column = column;
37: }
38:
39: public void setIfNotExists(boolean ifNotExists) {
40: // TODO user data type: if exists - probably better use 'or replace'
41: this .ifNotExists = ifNotExists;
42: }
43:
44: public int update() throws SQLException {
45: session.getUser().checkAdmin();
46: session.commit(true);
47: Database db = session.getDatabase();
48: session.getUser().checkAdmin();
49: if (db.findUserDataType(typeName) != null) {
50: if (ifNotExists) {
51: return 0;
52: }
53: throw Message
54: .getSQLException(
55: ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1,
56: typeName);
57: }
58: int id = getObjectId(false, true);
59: UserDataType type = new UserDataType(db, id, typeName);
60: type.setColumn(column);
61: db.addDatabaseObject(session, type);
62: return 0;
63: }
64:
65: }
|