示例#1
0
  /** @see org.castor.xml.InternalContext#getRegExpEvaluator() */
  public RegExpEvaluator getRegExpEvaluator() {
    if (_regExpEvaluator != null) {
      return _regExpEvaluator;
    }

    String regExpEvalClassName = _configuration.getString(XMLConfiguration.REG_EXP_CLASS_NAME);
    if ((regExpEvalClassName == null) || (regExpEvalClassName.length() == 0)) {
      _regExpEvaluator = null;
    } else {
      try {
        Class regExpEvalClass = Class.forName(regExpEvalClassName);
        _regExpEvaluator = (RegExpEvaluator) regExpEvalClass.newInstance();
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(
            Messages.format("conf.failedInstantiateRegExp", regExpEvalClassName, e));
      } catch (InstantiationException e) {
        throw new RuntimeException(
            Messages.format("conf.failedInstantiateRegExp", regExpEvalClassName, e));
      } catch (IllegalAccessException e) {
        throw new RuntimeException(
            Messages.format("conf.failedInstantiateRegExp", regExpEvalClassName, e));
      }
    }
    return _regExpEvaluator;
  } // -- getRegExpEvaluator
示例#2
0
  /** Initialize the SEQUENCE key generator. */
  public SequenceKeyGenerator(PersistenceFactory factory, Properties params, int sqlType)
      throws MappingException {
    boolean returning;

    _factoryName = factory.getFactoryName();
    returning = "true".equals(params.getProperty("returning"));
    _triggerPresent = "true".equals(params.getProperty("trigger", "false"));

    if (!_factoryName.equals(OracleFactory.FACTORY_NAME)
        && !_factoryName.equals(PostgreSQLFactory.FACTORY_NAME)
        && !_factoryName.equals(InterbaseFactory.FACTORY_NAME)
        && !_factoryName.equals("sapdb")
        && !_factoryName.equals(DB2Factory.FACTORY_NAME)) {
      throw new MappingException(
          Messages.format("mapping.keyGenNotCompatible", getClass().getName(), _factoryName));
    }
    if (!_factoryName.equals(OracleFactory.FACTORY_NAME) && returning) {
      throw new MappingException(
          Messages.format(
              "mapping.keyGenParamNotCompat",
              "returning=\"true\"",
              getClass().getName(),
              _factoryName));
    }
    _factory = factory;
    _seqName = params.getProperty("sequence", "{0}_seq");

    _style =
        (_factoryName.equals(PostgreSQLFactory.FACTORY_NAME)
                || _factoryName.equals(InterbaseFactory.FACTORY_NAME)
                || _factoryName.equals(DB2Factory.FACTORY_NAME)
            ? BEFORE_INSERT
            : (returning ? DURING_INSERT : AFTER_INSERT));
    if (_triggerPresent && !returning) {
      _style = AFTER_INSERT;
    }
    if (_triggerPresent && _style == BEFORE_INSERT)
      throw new MappingException(
          Messages.format(
              "mapping.keyGenParamNotCompat",
              "trigger=\"true\"",
              getClass().getName(),
              _factoryName));

    _sqlType = sqlType;
    supportsSqlType(sqlType);

    try {
      _increment = Integer.parseInt(params.getProperty("increment", "1"));
    } catch (NumberFormatException nfe) {
      _increment = 1;
    }
  }
示例#3
0
  public PersistenceQuery createCall(final String spCall, final Class[] types) {
    FieldDescriptor[] fields;
    String[] jdoFields0;
    String[] jdoFields;
    String sql;
    int[] sqlTypes0;
    int[] sqlTypes;
    int count;

    // changes for the SQL Direct interface begins here
    if (spCall.startsWith("SQL")) {
      sql = spCall.substring(4);

      if (LOG.isDebugEnabled()) {
        LOG.debug(Messages.format("jdo.directSQL", sql));
      }

      return new SQLQuery(this, _factory, sql, types, true);
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug(Messages.format("jdo.spCall", spCall));
    }

    fields = _clsDesc.getFields();
    jdoFields0 = new String[fields.length + 1];
    sqlTypes0 = new int[fields.length + 1];
    // the first field is the identity

    count = 1;
    jdoFields0[0] = _clsDesc.getIdentity().getFieldName();
    sqlTypes0[0] = ((JDOFieldDescriptor) _clsDesc.getIdentity()).getSQLType()[0];
    for (int i = 0; i < fields.length; ++i) {
      if (fields[i] instanceof JDOFieldDescriptor) {
        jdoFields0[count] = ((JDOFieldDescriptor) fields[i]).getSQLName()[0];
        sqlTypes0[count] = ((JDOFieldDescriptor) fields[i]).getSQLType()[0];
        ++count;
      }
    }
    jdoFields = new String[count];
    sqlTypes = new int[count];
    System.arraycopy(jdoFields0, 0, jdoFields, 0, count);
    System.arraycopy(sqlTypes0, 0, sqlTypes, 0, count);

    return ((BaseFactory) _factory)
        .getCallQuery(spCall, types, _clsDesc.getJavaClass(), jdoFields, sqlTypes);
  }
示例#4
0
 /**
  * Determine if the key generator supports a given sql type.
  *
  * @param sqlType
  * @throws MappingException
  */
 public void supportsSqlType(int sqlType) throws MappingException {
   if (sqlType != Types.INTEGER
       && sqlType != Types.NUMERIC
       && sqlType != Types.DECIMAL
       && sqlType != Types.BIGINT
       && sqlType != Types.CHAR
       && sqlType != Types.VARCHAR) {
     throw new MappingException(
         Messages.format("mapping.keyGenSQLType", getClass().getName(), new Integer(sqlType)));
   }
 }
示例#5
0
  public PersistenceQuery createQuery(
      final QueryExpression query, final Class[] types, final AccessMode accessMode)
      throws QueryException {
    AccessMode mode = (accessMode != null) ? accessMode : _clsDesc.getAccessMode();
    String sql = query.getStatement(mode == AccessMode.DbLocked);

    if (LOG.isDebugEnabled()) {
      LOG.debug(Messages.format("jdo.createSql", sql));
    }

    return new SQLQuery(this, _factory, sql, types, false);
  }
示例#6
0
  /** @see org.castor.xml.InternalContext#getSerializer(java.io.Writer) */
  public DocumentHandler getSerializer(final Writer output) throws IOException {
    Serializer serializer;
    DocumentHandler docHandler;

    serializer = getSerializer();
    serializer.setOutputCharStream(output);
    docHandler = serializer.asDocumentHandler();
    if (docHandler == null) {
      throw new RuntimeException(
          Messages.format("conf.serializerNotSaxCapable", serializer.getClass().getName()));
    }
    return docHandler;
  }
示例#7
0
  /** @see org.castor.xml.InternalContext#getXMLReader(java.lang.String) */
  public XMLReader getXMLReader(final String features) {
    XMLReader reader = null;
    Boolean validation = _configuration.getBoolean(XMLConfiguration.PARSER_VALIDATION);
    Boolean namespaces = _configuration.getBoolean(XMLConfiguration.NAMESPACES);

    String readerClassName = _configuration.getString(XMLConfiguration.PARSER);

    if (readerClassName == null || readerClassName.length() == 0) {
      SAXParser saxParser =
          XMLParserUtils.getSAXParser(validation.booleanValue(), namespaces.booleanValue());
      if (saxParser != null) {
        try {
          reader = saxParser.getXMLReader();
        } catch (SAXException e) {
          LOG.error(Messages.format("conf.configurationError", e));
        }
      }
    }

    if (reader == null) {
      if ((readerClassName == null)
          || (readerClassName.length() == 0)
          || (readerClassName.equalsIgnoreCase("xerces"))) {
        readerClassName = "org.apache.xerces.parsers.SAXParser";
      }

      reader = XMLParserUtils.instantiateXMLReader(readerClassName);
    }

    XMLParserUtils.setFeaturesOnXmlReader(
        _configuration.getString(XMLConfiguration.PARSER_FEATURES, features),
        _configuration.getString(XMLConfiguration.PARSER_FEATURES_DISABLED, ""),
        validation.booleanValue(),
        namespaces.booleanValue(),
        reader);

    return reader;
  } // -- getXMLReader
示例#8
0
  /**
   * Gives a possibility to patch the Castor-generated SQL statement for INSERT (makes sense for
   * DURING_INSERT key generators)
   */
  public String patchSQL(String insert, String primKeyName) throws MappingException {
    StringTokenizer st;
    String tableName;
    String seqName;
    String nextval;
    StringBuffer sb;
    int lp1; // the first left parenthesis, which starts fields list
    int lp2; // the second left parenthesis, which starts values list

    if (_style == BEFORE_INSERT) {
      return insert;
    }

    // First find the table name
    st = new StringTokenizer(insert);
    if (!st.hasMoreTokens() || !st.nextToken().equalsIgnoreCase("INSERT")) {
      throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
    }
    if (!st.hasMoreTokens() || !st.nextToken().equalsIgnoreCase("INTO")) {
      throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
    }
    if (!st.hasMoreTokens()) {
      throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
    }
    tableName = st.nextToken();

    // remove every double quote in the tablename
    int idxQuote = tableName.indexOf('"');
    if (idxQuote >= 0) {
      StringBuffer buffer2 = new StringBuffer();
      int pos = 0;

      do {
        buffer2.append(tableName.substring(pos, idxQuote));
        pos = idxQuote + 1;
      } while ((idxQuote = tableName.indexOf('"', pos)) != -1);

      buffer2.append(tableName.substring(pos));

      tableName = buffer2.toString();
    }

    seqName =
        MessageFormat.format(
            _seqName,
            new String[] {tableName, primKeyName}); // due to varargs in 1.5, see CASTOR-1097
    nextval = _factory.quoteName(seqName + ".nextval");
    lp1 = insert.indexOf('(');
    lp2 = insert.indexOf('(', lp1 + 1);
    if (lp1 < 0) {
      throw new MappingException(Messages.format("mapping.keyGenCannotParse", insert));
    }
    sb = new StringBuffer(insert);
    if (!_triggerPresent) { // if no onInsert triggers in the DB, we have to supply the Key values
                            // manually
      if (lp2 < 0) {
        // Only one pk field in the table, the INSERT statement would be
        // INSERT INTO table VALUES ()
        lp2 = lp1;
        lp1 = insert.indexOf(" VALUES ");
        // don't change the order of lines below,
        // otherwise index becomes invalid
        sb.insert(lp2 + 1, nextval);
        sb.insert(lp1 + 1, "(" + _factory.quoteName(primKeyName) + ") ");
      } else {
        // don't change the order of lines below,
        // otherwise index becomes invalid
        sb.insert(lp2 + 1, nextval + ",");
        sb.insert(lp1 + 1, _factory.quoteName(primKeyName) + ",");
      }
    }
    if (_style == DURING_INSERT) {
      // append 'RETURNING primKeyName INTO ?'
      sb.append(" RETURNING ");
      sb.append(_factory.quoteName(primKeyName));
      sb.append(" INTO ?");
    }
    return sb.toString();
  }
示例#9
0
  /**
   * @param conn An open connection within the given transaction
   * @param tableName The table name
   * @param primKeyName The primary key name
   * @param props A temporary replacement for Principal object
   * @return A new key
   * @throws PersistenceException An error occured talking to persistent storage
   */
  public Object generateKey(Connection conn, String tableName, String primKeyName, Properties props)
      throws PersistenceException {
    PreparedStatement stmt = null;
    ResultSet rs;
    String seqName;
    String table;

    seqName =
        MessageFormat.format(
            _seqName,
            new String[] {tableName, primKeyName}); // due to varargs in 1.5, see CASTOR-1097
    table = _factory.quoteName(tableName);
    try {
      if (_factory.getFactoryName().equals(InterbaseFactory.FACTORY_NAME)) {
        // interbase only does before_insert, and does it its own way
        stmt =
            conn.prepareStatement(
                "SELECT gen_id(" + seqName + "," + _increment + ") FROM rdb$database");
        rs = stmt.executeQuery();
      } else if (_factory.getFactoryName().equals(DB2Factory.FACTORY_NAME)) {
        stmt = conn.prepareStatement("SELECT nextval FOR " + seqName + " FROM SYSIBM.SYSDUMMY1");
        rs = stmt.executeQuery();
      } else {
        if (_style == BEFORE_INSERT) {
          stmt = conn.prepareStatement("SELECT nextval('" + seqName + "')");
          rs = stmt.executeQuery();
        } else if (_triggerPresent && _factoryName.equals(PostgreSQLFactory.FACTORY_NAME)) {
          Object insStmt = props.get("insertStatement");
          Class psqlStmtClass = Class.forName("org.postgresql.Statement");
          Method getInsertedOID = psqlStmtClass.getMethod("getInsertedOID", (Class[]) null);
          int insertedOID = ((Integer) getInsertedOID.invoke(insStmt, (Object[]) null)).intValue();
          stmt =
              conn.prepareStatement(
                  "SELECT " + _factory.quoteName(primKeyName) + " FROM " + table + " WHERE OID=?");
          stmt.setInt(1, insertedOID);
          rs = stmt.executeQuery();

        } else {
          stmt =
              conn.prepareStatement(
                  "SELECT " + _factory.quoteName(seqName + ".currval") + " FROM " + table);
          rs = stmt.executeQuery();
        }
      }

      if (!rs.next()) {
        throw new PersistenceException(
            Messages.format("persist.keyGenFailed", getClass().getName()));
      }

      Object resultKey = null;
      int resultValue = rs.getInt(1);
      String resultColName = rs.getMetaData().getColumnName(1);
      int resultColType = rs.getMetaData().getColumnType(1);
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "JDBC query returned value "
                + resultValue
                + " from column "
                + resultColName
                + "/"
                + resultColType);
      }
      if (_sqlType == Types.INTEGER) {
        resultKey = new Integer(resultValue);
      } else if (_sqlType == Types.BIGINT) {
        resultKey = new Long(resultValue);
      } else if (_sqlType == Types.CHAR || _sqlType == Types.VARCHAR) {
        resultKey = String.valueOf(resultValue);
      } else {
        resultKey = new BigDecimal(resultValue);
      }

      if (LOG.isDebugEnabled()) {
        if (resultKey != null) {
          LOG.debug(
              "Returning value "
                  + resultKey
                  + " of type "
                  + resultKey.getClass().getName()
                  + " as key.");
        }
      }
      return resultKey;
    } catch (Exception ex) {
      throw new PersistenceException(
          Messages.format("persist.keyGenSQL", getClass().getName(), ex.toString()));
    } finally {
      JDOUtils.closeStatement(stmt);
    }
  }
示例#10
0
 /**
  * Throws a PersistenceException if the database is closed, otherwise it does nothing.
  *
  * @throws PersistenceException
  */
 private void testForOpenDatabase() throws PersistenceException {
   if (_db.isClosed()) {
     throw new PersistenceException(Messages.message("jdo.dbClosed"));
   }
 }