示例#1
0
 private static StringValue format(Env env,
                             StringValue msg,
                             Value []args)
 {
   if (args.length == 0)
     return msg;
   
   StringValue sb;
   
   if (msg.isUnicode())
     sb = env.createUnicodeBuilder();
   else
     sb = env.createBinaryBuilder();
   
   return formatImpl(env, msg, args, sb);
 }
示例#2
0
  @Override
  protected Value getColumnString(Env env, ResultSet rs, ResultSetMetaData md, int column)
      throws SQLException {
    // php/1464, php/144f, php/144g, php/144b

    // The "SET NAMES 'latin1'" in Mysqli is important to make the default
    // encoding sane

    Mysqli mysqli = getMysqli();

    if (rs instanceof QuercusResultSet) {
      QuercusResultSet qRs = (QuercusResultSet) rs;

      int length = qRs.getStringLength(column);

      if (length < 0) return NullValue.NULL;

      // XXX: i18n
      StringBuilderValue sb = new StringBuilderValue();
      sb.ensureAppendCapacity(length);

      qRs.getString(column, sb.getBuffer(), sb.getOffset());
      sb.setOffset(sb.getOffset() + length);

      return sb;
    }

    Method getColumnCharacterSetMethod = mysqli.getColumnCharacterSetMethod(md.getClass());

    String encoding = null;

    try {
      if (getColumnCharacterSetMethod != null)
        encoding = (String) getColumnCharacterSetMethod.invoke(md, column);
    } catch (Exception e) {
      log.log(Level.FINE, e.toString(), e);
    }

    if (encoding == null) {
      String value = rs.getString(column);

      if (value != null) return env.createString(value);
      else return NullValue.NULL;
    }

    // calling getString() will decode using the database encoding, so
    // get bytes directly.  Also, getBytes is faster for MySQL since
    // getString converts from bytes to string.

    if ("UTF-8".equals(encoding)) {
      byte[] bytes = rs.getBytes(column);

      if (bytes == null) return NullValue.NULL;

      StringValue bb = env.createUnicodeBuilder();

      int length = bytes.length;
      int offset = 0;

      bb.appendUtf8(bytes);

      /*
      while (offset < length) {
        int ch = bytes[offset++] & 0xff;

        if (ch < 0x80) {
          bb.append((char) ch);
        }
        else if (ch < 0xe0) {
          int ch2 = bytes[offset++] & 0xff;
          int v = ((ch & 0x1f) << 6) + ((ch2 & 0x3f));

          bb.append((char) MysqlLatin1Utility.decode(v));
        }
        else {
          int ch2 = bytes[offset++] & 0xff;
          int ch3 = bytes[offset++] & 0xff;
          int v = ((ch & 0xf) << 12) + ((ch2 & 0x3f) << 6) + ((ch3 & 0x3f));

          bb.append((char) MysqlLatin1Utility.decode(v));
        }
      }
      */

      return bb;
    } else if ("Cp1252".equals(encoding) || "LATIN1".equals(encoding)) {
      byte[] bytes = rs.getBytes(column);

      if (bytes == null) return NullValue.NULL;

      StringValue bb = env.createUnicodeBuilder();

      bb.append(bytes);

      return bb;
    } else {
      String value = rs.getString(column);

      if (value != null) return env.createString(value);
      else return NullValue.NULL;
    }
  }