镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ext/sqlite3/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,45 @@ step(VALUE self)
return list;
}

/* call-seq: stmt.to_a
*
* Step through all remaining rows, returning them as an Array of frozen
* Arrays.
*/
static VALUE
to_a(VALUE self)
{
VALUE rows = rb_ary_new();
VALUE row;

while (!NIL_P(row = step(self))) {
rb_ary_push(rows, row);
}

return rows;
}

/* Like to_a, but returns each row as a Hash keyed by +columns+. */
static VALUE
to_a_hash(VALUE self, VALUE columns)
{
VALUE rows = rb_ary_new();
VALUE row;

Check_Type(columns, T_ARRAY);

while (!NIL_P(row = step(self))) {
VALUE hash = rb_hash_new();
long i;
for (i = 0; i < RARRAY_LEN(columns); i++) {
rb_hash_aset(hash, RARRAY_AREF(columns, i), rb_ary_entry(row, i));
}
rb_ary_push(rows, hash);
}

return rows;
}

/* call-seq: stmt.bind_param(key, value)
*
* Binds value to the named (or positional) placeholder. If +param+ is a
Expand Down Expand Up @@ -794,6 +833,7 @@ init_sqlite3_statement(void)
rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
rb_define_method(cSqlite3Statement, "clear_bindings!", clear_bindings_bang, 0);
rb_define_method(cSqlite3Statement, "step", step, 0);
rb_define_method(cSqlite3Statement, "to_a", to_a, 0);
rb_define_method(cSqlite3Statement, "done?", done_p, 0);
rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
Expand All @@ -811,6 +851,7 @@ init_sqlite3_statement(void)

rb_define_private_method(cSqlite3Statement, "column_decltype_downcased", column_decltype_downcased, 1);
rb_define_private_method(cSqlite3Statement, "prepare", prepare, 2);
rb_define_private_method(cSqlite3Statement, "to_a_hash", to_a_hash, 1);
rb_define_private_method(cSqlite3Statement, "stats_as_hash", stats_as_hash, 0);
rb_define_private_method(cSqlite3Statement, "stat_for", stat_for, 1);
}
9 changes: 9 additions & 0 deletions lib/sqlite3/resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,18 @@ def next_hash

@stmt.columns.zip(row).to_h
end

# Return all remaining rows.
def to_a
@stmt.to_a
end
end

class HashResultSet < ResultSet # :nodoc:
alias_method :next, :next_hash

def to_a
@stmt.__send__(:to_a_hash, @stmt.columns)
end
end
end
29 changes: 29 additions & 0 deletions test/test_result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,34 @@ def test_next_hash
end
rs.close
end

def test_to_a
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo (b) values ('x'), ('y'), ('z')"

rs = @db.prepare("select * from foo order by a").execute
assert_equal [1, "x"], rs.next
assert_equal [[2, "y"], [3, "z"]], rs.to_a
assert_equal [], rs.to_a
rs.close
end

def test_to_a_as_hash
@db.results_as_hash = true
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo (b) values ('x'), ('y'), ('z')"

rs = @db.prepare("select * from foo order by a").execute
assert_kind_of HashResultSet, rs
assert_equal({"a" => 1, "b" => "x"}, rs.next)
assert_equal [{"a" => 2, "b" => "y"}, {"a" => 3, "b" => "z"}], rs.to_a
assert_equal [], rs.to_a
rs.close
end

def test_to_a_as_hash_duplicate_column_names
@db.results_as_hash = true
assert_equal [{"a" => 2}], @db.execute("select 1 as a, 2 as a")
end
end
end
24 changes: 24 additions & 0 deletions test/test_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,30 @@ def test_step_does_not_allocate_when_done
assert_equal 0, allocations.last
end

def test_to_a
stmt = @db.prepare("select 1 union all select 2 union all select 3")
assert_equal [[1], [2], [3]], stmt.to_a
assert_predicate stmt, :done?
assert_equal [], stmt.to_a
ensure
stmt&.close
end

def test_to_a_returns_remaining_rows
stmt = @db.prepare("select 1 union all select 2 union all select 3")
assert_equal [1], stmt.step
rows = stmt.to_a
assert_equal [[2], [3]], rows
assert rows.all?(&:frozen?)
ensure
stmt&.close
end

def test_to_a_on_closed_statement
@stmt.close
assert_raises(SQLite3::Exception) { @stmt.to_a }
end

def test_column_count
assert_equal 1, @stmt.column_count
end
Expand Down
Loading