001: /*
002: * AlterSessionCommand.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.commands;
013:
014: import java.lang.reflect.Method;
015: import java.sql.Connection;
016: import java.sql.SQLException;
017: import workbench.db.DbMetadata;
018: import workbench.db.WbConnection;
019: import workbench.log.LogMgr;
020: import workbench.resource.ResourceMgr;
021: import workbench.sql.SqlCommand;
022: import workbench.sql.StatementRunnerResult;
023: import workbench.sql.formatter.SQLLexer;
024: import workbench.sql.formatter.SQLToken;
025: import workbench.util.StringUtil;
026:
027: /**
028: * @author support@sql-workbench.net
029: */
030: public class AlterSessionCommand extends SqlCommand {
031: public static final String VERB = "ALTER SESSION";
032:
033: public AlterSessionCommand() {
034: super ();
035: this .isUpdatingCommand = true;
036: }
037:
038: public String getVerb() {
039: return VERB;
040: }
041:
042: public StatementRunnerResult execute(String sql)
043: throws SQLException {
044: StatementRunnerResult result = new StatementRunnerResult();
045: result.setSuccess();
046:
047: String oldSchema = null;
048: SQLLexer lexer = new SQLLexer(sql);
049:
050: DbMetadata meta = currentConnection.getMetadata();
051:
052: // Skip the ALTER SESSION verb
053: SQLToken token = lexer.getNextToken(false, false);
054:
055: token = lexer.getNextToken(false, false);
056: if (token.getContents().equals("SET")) {
057: // check for known statements
058: token = lexer.getNextToken(false, false);
059: String parm = (token != null ? token.getContents() : null);
060: if ("CURRENT_SCHEMA".equalsIgnoreCase(parm)) {
061: oldSchema = meta.getCurrentSchema();
062: } else if ("TIME_ZONE".equalsIgnoreCase(parm)
063: && meta.isOracle()) {
064: // this should be the = sign, skip it
065: token = lexer.getNextToken(false, false);
066:
067: // this is the parameter for the new timezone
068: token = lexer.getNextToken(false, false);
069: if (token != null) {
070: if (changeOracleTimeZone(currentConnection, result,
071: token.getContents())) {
072: return result;
073: }
074: }
075: }
076: }
077:
078: try {
079: this .currentStatement = currentConnection.createStatement();
080: this .currentStatement.executeUpdate(sql);
081: if (oldSchema == null) {
082: String msg = VERB + " "
083: + ResourceMgr.getString("MsgKnownStatementOK");
084: result.addMessage(msg);
085: } else {
086: String schema = meta.getCurrentSchema();
087: if (!oldSchema.equalsIgnoreCase(schema)) {
088: currentConnection.schemaChanged(oldSchema, schema);
089: result.addMessage(ResourceMgr.getFormattedString(
090: "MsgSchemaChanged", schema));
091: }
092: }
093:
094: result.setSuccess();
095: } catch (Exception e) {
096: addErrorInfo(result, sql, e);
097: LogMgr.logSqlError("AlterSessionCommand.execute()", sql, e);
098: }
099:
100: return result;
101: }
102:
103: private boolean changeOracleTimeZone(WbConnection con,
104: StatementRunnerResult result, String tz) {
105: Connection sqlCon = currentConnection.getSqlConnection();
106: Method setTimezone = null;
107:
108: try {
109: Class cls = currentConnection.getSqlConnection().getClass();
110: setTimezone = cls.getMethod("setSessionTimeZone",
111: new Class[] { String.class });
112: } catch (Exception e) {
113: // Ignore
114: return false;
115: }
116:
117: if (setTimezone != null) {
118: try {
119: String zone = StringUtil.trimQuotes(tz);
120: LogMgr.logDebug(
121: "AlterSessionCommand.changeOracleTimeZone()",
122: "Calling Oracle's setSessionTimeZone");
123: setTimezone.setAccessible(true);
124: setTimezone.invoke(sqlCon, new Object[] { zone });
125: result.addMessage(ResourceMgr
126: .getString("MsgTimezoneChanged")
127: + " " + zone);
128: result.setSuccess();
129: return true;
130: } catch (Exception e) {
131: e.printStackTrace();
132: }
133: }
134: return false;
135: }
136: }
|