From 6b51b7c94e2859e7bca4440d29f918d5cd4e9f87 Mon Sep 17 00:00:00 2001 From: lresende Date: Mon, 15 Mar 2010 16:06:52 +0000 Subject: TUSCANY-3494 - Applying patch from Florian Pinel to create a new command method that takes in consideration the type of command being created git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@923318 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/tuscany/das/rdb/DAS.java | 11 ++++ .../org/apache/tuscany/das/rdb/impl/DASImpl.java | 73 +++++++++++++++------- 2 files changed, 61 insertions(+), 23 deletions(-) (limited to 'das-java') diff --git a/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java b/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java index 43f1e30d1a..f1444b52a4 100644 --- a/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java +++ b/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/DAS.java @@ -57,6 +57,7 @@ public interface DAS { /** * Creates a Command based on the provided SQL statement + * The Command kind is determined automatically for most common cases * * @param sql * The SQL statement @@ -64,4 +65,14 @@ public interface DAS { */ Command createCommand(String sql); + /** + * Creates a Command of the specified kind based on the provided SQL statement + * + * @param sql + * The SQL statement + * @param sql + * The Command kind (select, insert, update, delete or procedure), case-insensitive + * @return returns a Command instance + */ + Command createCommand(String sql, String kind); } diff --git a/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java b/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java index e456d65ef1..2c8497a0f1 100644 --- a/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java +++ b/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DASImpl.java @@ -50,7 +50,13 @@ import commonj.sdo.DataObject; */ public class DASImpl implements DAS { - private MappingWrapper configWrapper; + private static final String KIND_SELECT = "select"; + private static final String KIND_UPDATE = "update"; + private static final String KIND_INSERT = "insert"; + private static final String KIND_DELETE = "delete"; + private static final String KIND_PROCEDURE = "procedure"; + + private MappingWrapper configWrapper; private Connection connection; @@ -73,15 +79,15 @@ public class DASImpl implements DAS { org.apache.tuscany.das.rdb.config.Command commandConfig = (org.apache.tuscany.das.rdb.config.Command) i.next(); String kind = commandConfig.getKind(); - if (kind.equalsIgnoreCase("select")) { + if (kind.equalsIgnoreCase(KIND_SELECT)) { commands.put(commandConfig.getName(), new ReadCommandImpl(commandConfig, configWrapper, commandConfig.getResultDescriptor())); - } else if (kind.equalsIgnoreCase("update")) { + } else if (kind.equalsIgnoreCase(KIND_UPDATE)) { commands.put(commandConfig.getName(), new UpdateCommandImpl(commandConfig)); - } else if (kind.equalsIgnoreCase("insert")) { + } else if (kind.equalsIgnoreCase(KIND_INSERT)) { commands.put(commandConfig.getName(), new InsertCommandImpl(commandConfig, new String[0])); - } else if (kind.equalsIgnoreCase("delete")) { + } else if (kind.equalsIgnoreCase(KIND_DELETE)) { commands.put(commandConfig.getName(), new DeleteCommandImpl(commandConfig)); - } else if (kind.equalsIgnoreCase("procedure")) { + } else if (kind.equalsIgnoreCase(KIND_PROCEDURE)) { commands.put(commandConfig.getName(), new SPCommandImpl(commandConfig.getSQL(), configWrapper, commandConfig.getParameter())); } else { throw new RuntimeException("Invalid kind of command: " + kind); @@ -285,41 +291,62 @@ public class DASImpl implements DAS { } public Command createCommand(String sql) { - return baseCreateCommand(sql, this.configWrapper); + return baseCreateCommand(sql, null, this.configWrapper); + } + + public Command createCommand(String sql, String kind) { + return baseCreateCommand(sql, kind, this.configWrapper); } public Command createCommand(String sql, Config config) { - return baseCreateCommand(sql, new MappingWrapper(config)); + return baseCreateCommand(sql, null, new MappingWrapper(config)); } - private Command baseCreateCommand(String inSql, MappingWrapper config) { + public Command createCommand(String sql, String kind, Config config) { + return baseCreateCommand(sql, kind, new MappingWrapper(config)); + } + + private Command baseCreateCommand(String inSql, String kind, MappingWrapper config) { CommandImpl returnCmd = null; String sql = inSql.trim(); // Remove leading white space + if (kind == null || kind.trim().length() == 0) kind = getKind(sql); + if (kind.equalsIgnoreCase(KIND_SELECT)) { + returnCmd = new ReadCommandImpl(sql, config, null); + } else if (kind.equalsIgnoreCase(KIND_INSERT)) { + returnCmd = new InsertCommandImpl(sql, new String[0]); + } else if (kind.equalsIgnoreCase(KIND_UPDATE)) { + returnCmd = new UpdateCommandImpl(sql); + } else if (kind.equalsIgnoreCase(KIND_DELETE)) { + returnCmd = new DeleteCommandImpl(sql); + } else if (kind.equalsIgnoreCase(KIND_PROCEDURE)) { + returnCmd = new SPCommandImpl(sql, config, Collections.EMPTY_LIST); + } else { + throw new RuntimeException("Invalid kind of command: " + kind); + } + + returnCmd.setConnection(getConnection(), config.getConfig()); + return returnCmd; + } + + private String getKind(String sql) { char firstChar = Character.toUpperCase(sql.charAt(0)); switch (firstChar) { case 'S': - returnCmd = new ReadCommandImpl(sql, config, null); - break; + return KIND_SELECT; case 'I': - returnCmd = new InsertCommandImpl(sql, new String[0]); - break; + return KIND_INSERT; case 'U': - returnCmd = new UpdateCommandImpl(sql); - break; + return KIND_UPDATE; case 'D': - returnCmd = new DeleteCommandImpl(sql); - break; + return KIND_DELETE; case '{': - returnCmd = new SPCommandImpl(sql, config, Collections.EMPTY_LIST); - break; + return KIND_PROCEDURE; default: - throw new RuntimeException("SQL => " + sql + " is not valid"); + throw new RuntimeException("SQL => " + sql + " does not match any command kind, pass command kind (select, insert, update, delete, procedure) explicitly"); } - - returnCmd.setConnection(getConnection(), config.getConfig()); - return returnCmd; } + public void applyChanges(DataObject root) { getApplyChangesCommand().execute(root); } -- cgit v1.2.3