01: /*
02: * WbDefinePk.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.sql.wbcommands;
13:
14: import java.sql.SQLException;
15: import workbench.resource.ResourceMgr;
16: import workbench.sql.SqlCommand;
17: import workbench.sql.StatementRunnerResult;
18: import workbench.storage.PkMapping;
19: import workbench.util.SqlUtil;
20: import workbench.util.SqlUtil;
21: import workbench.util.StringUtil;
22: import workbench.util.WbStringTokenizer;
23:
24: /**
25: *
26: * @author support@sql-workbench.net
27: */
28: public class WbDefinePk extends SqlCommand {
29: public static final String VERB = "WBDEFINEPK";
30:
31: public WbDefinePk() {
32: }
33:
34: public String getVerb() {
35: return VERB;
36: }
37:
38: protected boolean isConnectionRequired() {
39: return false;
40: }
41:
42: public StatementRunnerResult execute(String aSql)
43: throws SQLException {
44: StatementRunnerResult result = new StatementRunnerResult();
45:
46: String sql = SqlUtil.stripVerb(aSql);
47:
48: WbStringTokenizer tok = new WbStringTokenizer("=", true, "\"'",
49: false);
50: tok.setSourceString(sql);
51: String columns = null;
52: String table = null;
53:
54: if (tok.hasMoreTokens())
55: table = tok.nextToken();
56:
57: if (table == null) {
58: result.addMessage(ResourceMgr
59: .getString("ErrPkDefWrongParameter"));
60: result.setFailure();
61: return result;
62: }
63:
64: if (tok.hasMoreTokens())
65: columns = tok.nextToken();
66: String msg = null;
67: if (columns == null) {
68: PkMapping.getInstance().removeMapping(currentConnection,
69: table);
70: msg = ResourceMgr.getString("MsgPkDefinitionRemoved");
71: } else {
72: PkMapping.getInstance().addMapping(table, columns);
73: msg = ResourceMgr.getString("MsgPkDefinitionAdded");
74: }
75: msg = StringUtil.replace(msg, "%table%", table);
76: result.setSuccess();
77: result.addMessage(msg);
78: return result;
79: }
80: }
|