Package org.g4studio.core.orm.xibatis.sqlmap.engine.mapping.statement

Examples of org.g4studio.core.orm.xibatis.sqlmap.engine.mapping.statement.SelectKeyStatement


    boolean autoStart = trans == null;

    try {
      trans = autoStartTransaction(sessionScope, autoStart, trans);

      SelectKeyStatement selectKeyStatement = null;
      if (ms instanceof InsertStatement) {
        selectKeyStatement = ((InsertStatement) ms).getSelectKeyStatement();
      }

      // Here we get the old value for the key property. We'll want it
      // later if for some reason the
      // insert fails.
      Object oldKeyValue = null;
      String keyProperty = null;
      boolean resetKeyValueOnFailure = false;
      if (selectKeyStatement != null && !selectKeyStatement.isRunAfterSQL()) {
        keyProperty = selectKeyStatement.getKeyProperty();
        oldKeyValue = PROBE.getObject(param, keyProperty);
        generatedKey = executeSelectKey(sessionScope, trans, ms, param);
        resetKeyValueOnFailure = true;
      }

      StatementScope statementScope = beginStatementScope(sessionScope, ms);
      try {
        ms.executeUpdate(statementScope, trans, param);
      } catch (SQLException e) {
        // uh-oh, the insert failed, so if we set the reset flag
        // earlier, we'll put the old value
        // back...
        if (resetKeyValueOnFailure)
          PROBE.setObject(param, keyProperty, oldKeyValue);
        // ...and still throw the exception.
        throw e;
      } finally {
        endStatementScope(statementScope);
      }

      if (selectKeyStatement != null && selectKeyStatement.isRunAfterSQL()) {
        generatedKey = executeSelectKey(sessionScope, trans, ms, param);
      }

      autoCommitTransaction(sessionScope, autoStart);
    } finally {
View Full Code Here


  private Object executeSelectKey(SessionScope sessionScope, Transaction trans, MappedStatement ms, Object param)
      throws SQLException {
    Object generatedKey = null;
    StatementScope statementScope;
    InsertStatement insert = (InsertStatement) ms;
    SelectKeyStatement selectKeyStatement = insert.getSelectKeyStatement();
    if (selectKeyStatement != null) {
      statementScope = beginStatementScope(sessionScope, selectKeyStatement);
      try {
        generatedKey = selectKeyStatement.executeQueryForObject(statementScope, trans, param, null);
        String keyProp = selectKeyStatement.getKeyProperty();
        if (keyProp != null) {
          PROBE.setObject(param, keyProp, generatedKey);
        }
      } finally {
        endStatementScope(statementScope);
View Full Code Here

      boolean runAfterSQL, String type) {
    if (rootStatement instanceof InsertStatement) {
      InsertStatement insertStatement = ((InsertStatement) rootStatement);
      Class parameterClass = insertStatement.getParameterClass();
      errorContext.setActivity("parsing a select key");
      SelectKeyStatement selectKeyStatement = new SelectKeyStatement();
      resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
      Class resultClass = null;

      // get parameter and result maps
      selectKeyStatement.setSqlMapClient(client);
      selectKeyStatement.setId(insertStatement.getId() + "-SelectKey");
      selectKeyStatement.setResource(errorContext.getResource());
      selectKeyStatement.setKeyProperty(keyPropName);
      selectKeyStatement.setRunAfterSQL(runAfterSQL);
      // process the type (pre or post) attribute
      if (type != null) {
        selectKeyStatement.setRunAfterSQL("post".equals(type));
      }
      try {
        if (resultClassName != null) {
          errorContext.setMoreInfo("Check the select key result class.");
          resultClass = Resources.classForName(resultClassName);
        } else {
          if (keyPropName != null && parameterClass != null) {
            resultClass = PROBE.getPropertyTypeForSetter(parameterClass,
                selectKeyStatement.getKeyProperty());
          }
        }
      } catch (ClassNotFoundException e) {
        throw new SqlMapException("Error.  Could not set result class.  Cause: " + e, e);
      }
      if (resultClass == null) {
        resultClass = Object.class;
      }

      // process SQL statement, including inline parameter maps
      errorContext.setMoreInfo("Check the select key SQL statement.");
      Sql sql = processor.getSql();
      setSqlForStatement(selectKeyStatement, sql);
      ResultMap resultMap;
      resultMap = new AutoResultMap(client.getDelegate(), false);
      resultMap.setId(selectKeyStatement.getId() + "-AutoResultMap");
      resultMap.setResultClass(resultClass);
      resultMap.setResource(selectKeyStatement.getResource());
      selectKeyStatement.setResultMap(resultMap);
      errorContext.setMoreInfo(null);
      insertStatement.setSelectKeyStatement(selectKeyStatement);
    } else {
      throw new SqlMapException("You cant set a select key statement on statement named " + rootStatement.getId()
          + " because it is not an InsertStatement.");
View Full Code Here

TOP

Related Classes of org.g4studio.core.orm.xibatis.sqlmap.engine.mapping.statement.SelectKeyStatement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.