~sgdg/stado/stado

« back to all changes in this revision

Viewing changes to src/org/postgresql/stado/parser/SqlAlterCluster.java

  • Committer: Andrei Martsinchyk
  • Date: 2013-04-22 15:04:09 UTC
  • mfrom: (9.1.2 stado30)
  • Revision ID: andrei.martsinchyk@gmail.com-20130422150409-cv4i89c21bbgvby1
Merge in 3.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * Copyright (C) 2008 EnterpriseDB Corporation.
 
3
 * Copyright (C) 2011 Stado Global Development Group.
 
4
 *
 
5
 * This file is part of Stado.
 
6
 *
 
7
 * Stado is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * Stado is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Stado.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * You can find Stado at http://www.stado.us
 
21
 *
 
22
 ****************************************************************************/
 
23
/*
 
24
 * SqlStopDatabase.java
 
25
 *
 
26
 */
 
27
 
 
28
package org.postgresql.stado.parser;
 
29
 
 
30
import java.util.Collection;
 
31
import java.util.Collections;
 
32
 
 
33
import org.postgresql.stado.common.util.XLogger;
 
34
import org.postgresql.stado.engine.Engine;
 
35
import org.postgresql.stado.engine.ExecutionResult;
 
36
import org.postgresql.stado.engine.IPreparable;
 
37
import org.postgresql.stado.engine.XDBSessionContext;
 
38
import org.postgresql.stado.exception.XDBSecurityException;
 
39
import org.postgresql.stado.metadata.DBNode;
 
40
import org.postgresql.stado.metadata.SysLogin;
 
41
import org.postgresql.stado.metadata.SysTable;
 
42
import org.postgresql.stado.metadata.scheduler.LockSpecification;
 
43
import org.postgresql.stado.parser.core.syntaxtree.AlterCluster;
 
44
import org.postgresql.stado.parser.core.visitor.DepthFirstVoidArguVisitor;
 
45
 
 
46
/**
 
47
 *
 
48
 *
 
49
 */
 
50
public class SqlAlterCluster extends DepthFirstVoidArguVisitor implements IXDBSql,
 
51
        IPreparable {
 
52
    private static final XLogger logger = XLogger
 
53
            .getLogger(SqlAlterCluster.class);
 
54
 
 
55
    private XDBSessionContext client;
 
56
 
 
57
    private boolean prepared = false;
 
58
    
 
59
    private boolean setReadOnly = false;
 
60
 
 
61
    /** Creates a new instance of SqlAlterCluster */
 
62
    public SqlAlterCluster(XDBSessionContext client) {
 
63
        this.client = client;
 
64
    }
 
65
 
 
66
    /*
 
67
     * (non-Javadoc)
 
68
     *
 
69
     * @see org.postgresql.stado.Engine.IPreparable#isPrepared()
 
70
     */
 
71
    public boolean isPrepared() {
 
72
        return prepared;
 
73
    }
 
74
 
 
75
    /*
 
76
     * (non-Javadoc)
 
77
     *
 
78
     * @see org.postgresql.stado.Parser.IXDBSql#getNodeList()
 
79
     */
 
80
    public Collection<DBNode> getNodeList() {
 
81
        return Collections.emptyList();
 
82
    }
 
83
 
 
84
    /*
 
85
     * (non-Javadoc)
 
86
     *
 
87
     * @see org.postgresql.stado.MetaData.Scheduler.ILockCost#getCost()
 
88
     */
 
89
    public long getCost() {
 
90
        return LOW_COST;
 
91
    }
 
92
 
 
93
    /*
 
94
     * (non-Javadoc)
 
95
     *
 
96
     * @see org.postgresql.stado.MetaData.Scheduler.ILockCost#getLockSpecs()
 
97
     */
 
98
    public LockSpecification<SysTable> getLockSpecs() {
 
99
        Collection<SysTable> empty = Collections.emptyList();
 
100
        return new LockSpecification<SysTable>(empty, empty);
 
101
    }
 
102
 
 
103
    /*
 
104
     * (non-Javadoc)
 
105
     *
 
106
     * @see org.postgresql.stado.MetaData.Scheduler.ILockCost#needCoordinatorConnection()
 
107
     */
 
108
    public boolean needCoordinatorConnection() {
 
109
        return false;
 
110
    }
 
111
 
 
112
    /*
 
113
     * (non-Javadoc)
 
114
     *
 
115
     * @see org.postgresql.stado.Engine.IPreparable#prepare()
 
116
     */
 
117
    public void prepare() throws Exception {
 
118
        final String method = "prepare";
 
119
        logger.entering(method);
 
120
        try {
 
121
 
 
122
            if (!prepared) {
 
123
                if (client.getCurrentUser().getLogin().getUserClass() != SysLogin.USER_CLASS_DBA) {
 
124
                    throw new XDBSecurityException(
 
125
                            "You are not allowed to alter the cluster");
 
126
                }
 
127
            }
 
128
            prepared = true;
 
129
 
 
130
        } finally {
 
131
            logger.exiting(method);
 
132
        }
 
133
    }
 
134
 
 
135
    /*
 
136
     * (non-Javadoc)
 
137
     *
 
138
     * @see org.postgresql.stado.Engine.IExecutable#execute(org.postgresql.stado.Engine.Engine)
 
139
     */
 
140
    public ExecutionResult execute(Engine engine) throws Exception {
 
141
        final String method = "execute";
 
142
        logger.entering(method, new Object[] { engine });
 
143
        try {
 
144
 
 
145
            if (!isPrepared()) {
 
146
                prepare();
 
147
            }
 
148
            
 
149
//            DatabaseState state;
 
150
//            if (setReadOnly) {
 
151
//              state = DatabaseState.USER_REQUESTED_READ_ONLY;
 
152
//            } else {
 
153
//              state = DatabaseState.READ_WRITE;               
 
154
//            }
 
155
//            SysDatabase.setState(state);
 
156
 
 
157
        } finally {
 
158
            logger.exiting(method);
 
159
        }
 
160
        return ExecutionResult
 
161
                .createSuccessResult(ExecutionResult.COMMAND_STOP_DATABASE);
 
162
    }
 
163
 
 
164
    @Override
 
165
    public void visit(AlterCluster n, Object argu) {
 
166
        if (n.f3.which == 0) {
 
167
                setReadOnly = true;
 
168
        } else {
 
169
                setReadOnly = false;
 
170
        }       
 
171
    }
 
172
 
 
173
    // This does not actually write out to the database so its read-only
 
174
        @Override
 
175
        public boolean isReadOnly() {
 
176
                return true;
 
177
        }
 
178
 
 
179
}