コード例 #1
0
  public void query(byte[] data) {
    if (queryHandler != null) {
      // 取得语句
      MySQLMessage mm = new MySQLMessage(data);
      mm.position(5);
      String sql = null;
      try {
        sql = mm.readString(charset);
      } catch (UnsupportedEncodingException e) {
        writeErrMessage(ErrorCode.ER_UNKNOWN_CHARACTER_SET, "Unknown charset '" + charset + "'");
        return;
      }
      if (sql == null || sql.length() == 0) {
        writeErrMessage(ErrorCode.ER_NOT_ALLOWED_COMMAND, "Empty SQL");
        return;
      }

      // sql = StringUtil.replace(sql, "`", "");

      // remove last ';'
      if (sql.endsWith(";")) {
        sql = sql.substring(0, sql.length() - 1);
      }

      // 执行查询
      queryHandler.setReadOnly(privileges.isReadOnly(user));
      queryHandler.query(sql);
    } else {
      writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Query unsupported!");
    }
  }
コード例 #2
0
  public void initDB(byte[] data) {
    MySQLMessage mm = new MySQLMessage(data);
    mm.position(5);
    String db = mm.readString();

    // 检查schema的有效性
    if (db == null || !privileges.schemaExists(db)) {
      writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
      return;
    }
    if (!privileges.userExists(user, host)) {
      writeErrMessage(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + user + "'");
      return;
    }
    Set<String> schemas = privileges.getUserSchemas(user);
    if (schemas == null || schemas.size() == 0 || schemas.contains(db)) {
      this.schema = db;
      write(writeToBuffer(OkPacket.OK, allocate()));
    } else {
      String s = "Access denied for user '" + user + "' to database '" + db + "'";
      writeErrMessage(ErrorCode.ER_DBACCESS_DENIED_ERROR, s);
    }
  }