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));
   }
 }