KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > CreateUserDataType


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Database;
10 import org.h2.engine.Session;
11 import org.h2.engine.UserDataType;
12 import org.h2.message.Message;
13 import org.h2.table.Column;
14
15 public class CreateUserDataType extends DefineCommand {
16
17     private String JavaDoc typeName;
18     private Column column;
19     private boolean ifNotExists;
20
21     public CreateUserDataType(Session session) {
22         super(session);
23     }
24
25     public void setTypeName(String JavaDoc name) {
26         this.typeName = name;
27     }
28
29     public void setColumn(Column column) {
30         this.column = column;
31     }
32
33     public void setIfNotExists(boolean ifNotExists) {
34         // TODO user data type: if exists - probably better use 'or replace'
35
this.ifNotExists = ifNotExists;
36     }
37
38     public int update() throws SQLException JavaDoc {
39         session.getUser().checkAdmin();
40         session.commit();
41         Database db = session.getDatabase();
42         session.getUser().checkAdmin();
43         if(db.findUserDataType(typeName)!=null) {
44             if (ifNotExists) {
45                 return 0;
46             }
47             throw Message.getSQLException(Message.USER_DATA_TYPE_ALREADY_EXISTS_1,
48                     typeName);
49         }
50         int id = getObjectId(false, true);
51         UserDataType type = new UserDataType(db, id, typeName);
52         type.setColumn(column);
53         db.addDatabaseObject(session, type);
54         return 0;
55     }
56
57 }
58
Popular Tags