Пример #1
0
  public void initDB(byte[] data) {
    MySQLMessage mm = new MySQLMessage(data);
    mm.position(5);
    String db = mm.readString();

    // 检查schema是否已经设置
    if (schema != null) {
      if (schema.equals(db)) {
        write(writeToBuffer(OkPacket.OK, allocate()));
      } else {
        writeErrMessage(ErrorCode.ER_DBACCESS_DENIED_ERROR, "Not allowed to change the database!");
      }
      return;
    }

    // 检查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);
    }
  }
Пример #2
0
  public void stmtPrepare(byte[] data) {
    if (prepareHandler != 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;
      }

      // 执行预处理
      prepareHandler.prepare(sql);
    } else {
      writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Prepare unsupported!");
    }
  }