File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateUserDataType.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
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.DbException;
15 import org.h2.table.Column;
16 import org.h2.table.Table;
17 import org.h2.value.DataType;
18
19 /**
20 * This class represents the statement
21 * CREATE DOMAIN
22 */
23 public class CreateUserDataType extends DefineCommand {
24
25 private String typeName;
26 private Column column;
27 private boolean ifNotExists;
28
29 public CreateUserDataType(Session session) {
30 super(session);
31 }
32
33 public void setTypeName(String name) {
34 this.typeName = name;
35 }
36
37 public void setColumn(Column column) {
38 this.column = column;
39 }
40
41 public void setIfNotExists(boolean ifNotExists) {
42 this.ifNotExists = ifNotExists;
43 }
44
45 public int update() {
46 session.getUser().checkAdmin();
47 session.commit(true);
48 Database db = session.getDatabase();
49 session.getUser().checkAdmin();
50 if (db.findUserDataType(typeName) != null) {
51 if (ifNotExists) {
52 return 0;
53 }
54 throw DbException.get(ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1, typeName);
55 }
56 DataType builtIn = DataType.getTypeByName(typeName);
57 if (builtIn != null) {
58 if (!builtIn.hidden) {
59 throw DbException.get(ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1, typeName);
60 }
61 Table table = session.getDatabase().getFirstUserTable();
62 if (table != null) {
63 throw DbException.get(ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1, typeName + " (" + table.getSQL() + ")");
64 }
65 }
66 int id = getObjectId();
67 UserDataType type = new UserDataType(db, id, typeName);
68 type.setColumn(column);
69 db.addDatabaseObject(session, type);
70 return 0;
71 }
72
73 public int getType() {
74 return CommandInterface.CREATE_DOMAIN;
75 }
76
77 }
78