~sgdg/stado/stado30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*****************************************************************************
 * Copyright (C) 2008 EnterpriseDB Corporation.
 * Copyright (C) 2011 Stado Global Development Group.
 *
 * This file is part of Stado.
 *
 * Stado is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Stado is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stado.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can find Stado at http://www.stado.us
 *
 ****************************************************************************/
/**
 *
 */
package org.postgresql.stado.parser;

import java.util.HashMap;
import java.util.Map;

import org.postgresql.stado.common.util.XLogger;
import org.postgresql.stado.engine.Engine;
import org.postgresql.stado.engine.ExecutionResult;
import org.postgresql.stado.engine.IPreparable;
import org.postgresql.stado.engine.XDBSessionContext;
import org.postgresql.stado.exception.XDBSecurityException;
import org.postgresql.stado.exception.XDBServerException;
import org.postgresql.stado.metadata.DBNode;
import org.postgresql.stado.metadata.MetaData;
import org.postgresql.stado.metadata.SyncAlterTableSetTablespace;
import org.postgresql.stado.metadata.SysLogin;
import org.postgresql.stado.metadata.SysTablespace;
import org.postgresql.stado.parser.core.syntaxtree.SetTablespace;
import org.postgresql.stado.parser.core.visitor.DepthFirstVoidArguVisitor;
import org.postgresql.stado.parser.handler.IdentifierHandler;


/**
 *
 *
 */
public class SqlAlterSetTablespace extends DepthFirstVoidArguVisitor implements
        IPreparable {
    private static final XLogger logger = XLogger
            .getLogger(SqlAlterSetTablespace.class);

    private XDBSessionContext client;

    private SqlAlterTable parent;

    private String tablespaceName = null;

    private SysTablespace tablespace = null;

    private Map<DBNode,String> commands = null;

    /**
     *
     */
    public SqlAlterSetTablespace(SqlAlterTable parent, XDBSessionContext client) {
        this.client = client;
        this.parent = parent;
    }

    /**
     * Grammar production:
     * f0 -> <SET_>
     * f1 -> <TABLESPACE_>
     * f2 -> Identifier(prn)
     */
    @Override
    public void visit(SetTablespace n, Object argu) {
        tablespaceName = (String) n.f2.accept(new IdentifierHandler(), argu);
    }

    public SqlAlterTable getParent() {
        return parent;
    }

    public SysTablespace getTablespace() {
        return tablespace;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.postgresql.stado.Engine.IPreparable#isPrepared()
     */
    public boolean isPrepared() {
        return tablespace != null;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.postgresql.stado.Engine.IPreparable#prepare()
     */
    public void prepare() throws Exception {
        final String method = "prepare";
        logger.entering(method, new Object[] {});
        try {

            if (!isPrepared()) {
                if (client.getCurrentUser().getUserClass() != SysLogin.USER_CLASS_DBA) {
                    XDBSecurityException ex = new XDBSecurityException("Only "
                            + SysLogin.USER_CLASS_DBA_STR
                            + " can change table workspace");
                    logger.throwing(ex);
                    throw ex;
                }
                SysTablespace newTablespace = MetaData.getMetaData()
                        .getTablespace(tablespaceName);
                commands = new HashMap<DBNode,String>();
                for (DBNode dbNode : parent.getTable().getNodeList()) {
                    if (!newTablespace.getLocations().containsKey(dbNode.getNodeId())) {
                        throw new XDBServerException("Tablespace "
                                + IdentifierHandler.quote(tablespaceName)
                                + " does not exist on Node "
                                + dbNode.getNodeId());
                    }
                    String command = "ALTER TABLE "
                        + IdentifierHandler.quote(parent.getTableName())
                        + " SET TABLESPACE "
                        + IdentifierHandler.quote(tablespaceName + "_" + dbNode.getNodeId());
                    commands.put(dbNode, command);
                }
                tablespace = newTablespace;
            }

        } finally {
            logger.exiting(method);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.postgresql.stado.Engine.IExecutable#execute(org.postgresql.stado.Engine.Engine)
     */
    public ExecutionResult execute(Engine engine) throws Exception {
        final String method = "execute";
        logger.entering(method, new Object[] {});
        try {

            engine.executeDDLOnMultipleNodes(commands,
                    new SyncAlterTableSetTablespace(this), client);
            return null;

        } finally {
            logger.exiting(method);
        }
    }
}