File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\DropUser.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.User;
14 import org.h2.message.DbException;
15
16 /**
17 * This class represents the statement
18 * DROP USER
19 */
20 public class DropUser extends DefineCommand {
21
22 private boolean ifExists;
23 private String userName;
24
25 public DropUser(Session session) {
26 super(session);
27 }
28
29 public void setIfExists(boolean b) {
30 ifExists = b;
31 }
32
33 public void setUserName(String userName) {
34 this.userName = userName;
35 }
36
37 public int update() {
38 session.getUser().checkAdmin();
39 session.commit(true);
40 Database db = session.getDatabase();
41 User user = db.findUser(userName);
42 if (user == null) {
43 if (!ifExists) {
44 throw DbException.get(ErrorCode.USER_NOT_FOUND_1, userName);
45 }
46 } else {
47 if (user == session.getUser()) {
48 int adminUserCount = 0;
49 for (User u : db.getAllUsers()) {
50 if (u.isAdmin()) {
51 adminUserCount++;
52 }
53 }
54 if (adminUserCount == 1) {
55 throw DbException.get(ErrorCode.CANNOT_DROP_CURRENT_USER);
56 }
57 }
58 user.checkOwnsNoSchemas();
59 db.removeDatabaseObject(session, user);
60 }
61 return 0;
62 }
63
64 public boolean isTransactional() {
65 return false;
66 }
67
68 public int getType() {
69 return CommandInterface.DROP_USER;
70 }
71
72 }
73