class Mysql2::Result - mysql2-0.4.9 Documentation

# class Mysql2::Result

### Public Instance Methods

count() click to toggle source

    static VALUE rb_mysql_result_count(VALUE self) {
      GET_RESULT(self);

      if (wrapper->is_streaming) {
        /* This is an unsigned long per result.h */
        return ULONG2NUM(wrapper->numberOfRows);
      }

      if (wrapper->resultFreed) {
        /* Ruby arrays have platform signed long length */
        return LONG2NUM(RARRAY_LEN(wrapper->rows));
      } else {
        /* MySQL returns an unsigned 64-bit long here */
        if (wrapper->stmt_wrapper) {
          return ULL2NUM(mysql_stmt_num_rows(wrapper->stmt_wrapper->stmt));
        } else {
          return ULL2NUM(mysql_num_rows(wrapper->result));
        }
      }
    }

Also aliased as: size
each(p1 = v1, &block) click to toggle source

    static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
      result_each_args args;
      VALUE defaults, opts, block, (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
      ID db_timezone, app_timezone, dbTz, appTz;
      int symbolizeKeys, asArray, castBool, cacheRows, cast;

      GET_RESULT(self);

      if (wrapper->stmt_wrapper && wrapper->stmt_wrapper->closed) {
        rb_raise(cMysql2Error, "Statement handle already closed");
      }

      defaults = rb_iv_get(self, "@query_options");
      Check_Type(defaults, T_HASH);
      if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
        opts = rb_funcall(defaults, intern_merge, 1, opts);
      } else {
        opts = defaults;
      }

      symbolizeKeys = RTEST(rb_hash_aref(opts, sym_symbolize_keys));
      asArray       = rb_hash_aref(opts, sym_as) == sym_array;
      castBool      = RTEST(rb_hash_aref(opts, sym_cast_booleans));
      cacheRows     = RTEST(rb_hash_aref(opts, sym_cache_rows));
      cast          = RTEST(rb_hash_aref(opts, sym_cast));

      if (wrapper->is_streaming && cacheRows) {
        rb_warn(":cache_rows is ignored if :stream is true");
      }

      if (wrapper->stmt_wrapper && !cacheRows && !wrapper->is_streaming) {
        rb_warn(":cache_rows is forced for prepared statements (if not streaming)");
        cacheRows = 1;
      }

      if (wrapper->stmt_wrapper && !cast) {
        rb_warn(":cast is forced for prepared statements");
      }

      dbTz = rb_hash_aref(opts, sym_database_timezone);
      if (dbTz == sym_local) {
        db_timezone = intern_local;
      } else if (dbTz == sym_utc) {
        db_timezone = intern_utc;
      } else {
        if (!NIL_P(dbTz)) {
          rb_warn(":database_timezone option must be :utc or :local - defaulting to :local");
        }
        db_timezone = intern_local;
      }

      appTz = rb_hash_aref(opts, sym_application_timezone);
      if (appTz == sym_local) {
        app_timezone = intern_local;
      } else if (appTz == sym_utc) {
        app_timezone = intern_utc;
      } else {
        app_timezone = Qnil;
      }

      if (wrapper->rows == Qnil && !wrapper->is_streaming) {
        wrapper->numberOfRows = wrapper->stmt_wrapper ? mysql_stmt_num_rows(wrapper->stmt_wrapper->stmt) : mysql_num_rows(wrapper->result);
        wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
      } else if (wrapper->rows && !cacheRows) {
        if (wrapper->resultFreed) {
          rb_raise(cMysql2Error, "Result set has already been freed");
        }
        mysql_data_seek(wrapper->result, 0);
        wrapper->lastRowProcessed = 0;
        wrapper->rows = rb_ary_new2(wrapper->numberOfRows);
      }

      // Backward compat
      args.symbolizeKeys = symbolizeKeys;
      args.asArray = asArray;
      args.castBool = castBool;
      args.cacheRows = cacheRows;
      args.cast = cast;
      args.db_timezone = db_timezone;
      args.app_timezone = app_timezone;
      args.block_given = block;

      if (wrapper->stmt_wrapper) {
        fetch_row_func = rb_mysql_result_fetch_row_stmt;
      } else {
        fetch_row_func = rb_mysql_result_fetch_row;
      }

      return rb_mysql_result_each_(self, fetch_row_func, &args);
    }

fields() click to toggle source

    static VALUE rb_mysql_result_fetch_fields(VALUE self) {
      unsigned int i = 0;
      short int symbolizeKeys = 0;
      VALUE defaults;

      GET_RESULT(self);

      defaults = rb_iv_get(self, "@query_options");
      Check_Type(defaults, T_HASH);
      if (rb_hash_aref(defaults, sym_symbolize_keys) == Qtrue) {
        symbolizeKeys = 1;
      }

      if (wrapper->fields == Qnil) {
        wrapper->numberOfFields = mysql_num_fields(wrapper->result);
        wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
      }

      if ((my_ulonglong)RARRAY_LEN(wrapper->fields) != wrapper->numberOfFields) {
        for (i=0; i<wrapper->numberOfFields; i++) {
          rb_mysql_result_fetch_field(self, i, symbolizeKeys);
        }
      }

      return wrapper->fields;
    }

free() click to toggle source

    static VALUE rb_mysql_result_free_(VALUE self) {
      GET_RESULT(self);
      rb_mysql_result_free_result(wrapper);
      return Qnil;
    }

size()
Alias for: count