Exemplo n.º 1
0
 /**
  * Establishes a connection to a relational database.
  *
  * @param ctx query context
  * @return connection id
  * @throws QueryException query exception
  */
 private Itr connect(final QueryContext ctx) throws QueryException {
   // URL to relational database
   final String url = string(checkStr(expr[0], ctx));
   try {
     if (expr.length > 2) {
       // Credentials
       final String user = string(checkStr(expr[1], ctx));
       final String pass = string(checkStr(expr[2], ctx));
       if (expr.length == 4) {
         // Connection options
         final TokenObjMap<Object> options = options(3, E_OPS, ctx);
         boolean autoCommit = true;
         final Object commit = options.get(AUTO_COMM);
         if (commit != null) {
           // Extract auto-commit mode from options
           autoCommit = Boolean.parseBoolean(commit.toString());
           options.delete(AUTO_COMM);
         }
         // Connection properties
         final Properties props = connProps(options(3, E_OPS, ctx));
         props.setProperty(USER, user);
         props.setProperty(PASS, pass);
         // Open connection
         final Connection conn = getConnection(url, props);
         // Set auto/commit mode
         conn.setAutoCommit(autoCommit);
         return Itr.get(ctx.jdbc.add(conn));
       }
       return Itr.get(ctx.jdbc.add(getConnection(url, user, pass)));
     }
     return Itr.get(ctx.jdbc.add(getConnection(url)));
   } catch (final SQLException ex) {
     throw SQLEXC.thrw(input, ex.getMessage());
   }
 }
Exemplo n.º 2
0
 /**
  * Sets the parameter with the given index in a prepared statement.
  *
  * @param index parameter index
  * @param stmt prepared statement
  * @param paramType parameter type
  * @param value parameter value
  * @param isNull indicator if the parameter is null or not
  * @throws QueryException query exception
  */
 private void setParam(
     final int index,
     final PreparedStatement stmt,
     final byte[] paramType,
     final String value,
     final boolean isNull)
     throws QueryException {
   try {
     if (eq(BOOL, paramType)) {
       if (isNull) stmt.setNull(index, Types.BOOLEAN);
       else stmt.setBoolean(index, Boolean.parseBoolean(value));
     } else if (eq(DATE, paramType)) {
       if (isNull) stmt.setNull(index, Types.DATE);
       else stmt.setDate(index, Date.valueOf(value));
     } else if (eq(DOUBLE, paramType)) {
       if (isNull) stmt.setNull(index, Types.DOUBLE);
       else stmt.setDouble(index, Double.parseDouble(value));
     } else if (eq(FLOAT, paramType)) {
       if (isNull) stmt.setNull(index, Types.FLOAT);
       else stmt.setFloat(index, Float.parseFloat(value));
     } else if (eq(INT, paramType)) {
       if (isNull) stmt.setNull(index, Types.INTEGER);
       else stmt.setInt(index, Integer.parseInt(value));
     } else if (eq(SHORT, paramType)) {
       if (isNull) stmt.setNull(index, Types.SMALLINT);
       else stmt.setShort(index, Short.parseShort(value));
     } else if (eq(STRING, paramType)) {
       if (isNull) stmt.setNull(index, Types.VARCHAR);
       else stmt.setString(index, value);
     } else if (eq(TIME, paramType)) {
       if (isNull) stmt.setNull(index, Types.TIME);
       else stmt.setTime(index, Time.valueOf(value));
     } else if (eq(TIMESTAMP, paramType)) {
       if (isNull) stmt.setNull(index, Types.TIMESTAMP);
       else stmt.setTimestamp(index, Timestamp.valueOf(value));
     } else {
       throw SQLEXC.thrw(input, "unsupported type: " + string(paramType));
     }
   } catch (final SQLException ex) {
     throw SQLEXC.thrw(input, ex.getMessage());
   } catch (final IllegalArgumentException ex) {
     throw ILLFORMAT.thrw(input, string(paramType));
   }
 }
Exemplo n.º 3
0
  /**
   * Reads the configuration file and initializes the project properties. The file is located in the
   * project home directory.
   *
   * @param prop property file extension
   */
  protected synchronized void read(final String prop) {
    file = new IOFile(HOME + IO.BASEXSUFFIX + prop);

    final StringList read = new StringList();
    final TokenBuilder err = new TokenBuilder();
    if (!file.exists()) {
      err.addExt("Saving properties in \"%\"..." + NL, file);
    } else {
      BufferedReader br = null;
      try {
        br = new BufferedReader(new FileReader(file.file()));
        for (String line; (line = br.readLine()) != null; ) {
          line = line.trim();
          if (line.isEmpty() || line.charAt(0) == '#') continue;
          final int d = line.indexOf('=');
          if (d < 0) {
            err.addExt("%: \"%\" ignored. " + NL, file, line);
            continue;
          }

          final String val = line.substring(d + 1).trim();
          String key = line.substring(0, d).trim();

          // extract numeric value in key
          int num = 0;
          final int ss = key.length();
          for (int s = 0; s < ss; ++s) {
            if (Character.isDigit(key.charAt(s))) {
              num = Integer.parseInt(key.substring(s));
              key = key.substring(0, s);
              break;
            }
          }
          read.add(key);

          final Object entry = props.get(key);
          if (entry == null) {
            err.addExt("%: \"%\" not found. " + NL, file, key);
          } else if (entry instanceof String) {
            props.put(key, val);
          } else if (entry instanceof Integer) {
            props.put(key, Integer.parseInt(val));
          } else if (entry instanceof Boolean) {
            props.put(key, Boolean.parseBoolean(val));
          } else if (entry instanceof String[]) {
            if (num == 0) {
              props.put(key, new String[Integer.parseInt(val)]);
            } else {
              ((String[]) entry)[num - 1] = val;
            }
          } else if (entry instanceof int[]) {
            ((int[]) entry)[num] = Integer.parseInt(val);
          }
        }
      } catch (final Exception ex) {
        err.addExt("% could not be parsed." + NL, file);
        Util.debug(ex);
      } finally {
        if (br != null)
          try {
            br.close();
          } catch (final IOException ex) {
          }
      }
    }

    // check if all mandatory files have been read
    try {
      if (err.isEmpty()) {
        boolean ok = true;
        for (final Field f : getClass().getFields()) {
          final Object obj = f.get(null);
          if (!(obj instanceof Object[])) continue;
          final String key = ((Object[]) obj)[0].toString();
          ok &= read.contains(key);
        }
        if (!ok) err.addExt("Saving properties in \"%\"..." + NL, file);
      }
    } catch (final IllegalAccessException ex) {
      Util.notexpected(ex);
    }

    if (!err.isEmpty()) {
      Util.err(err.toString());
      write();
    }
  }