зеркало из https://github.com/github/github-ds.git
Merge pull request #38 from github/fix-results-when-duplicate-column-name-is-passed
Don't de-dup values in results array
This commit is contained in:
Коммит
81d4ef3cc4
7
Gemfile
7
Gemfile
|
@ -2,6 +2,11 @@ source "https://rubygems.org"
|
|||
gemspec
|
||||
|
||||
DEFAULT_RAILS_VERSION = '5.0.2'
|
||||
gem "mysql2"
|
||||
|
||||
if ENV['RAILS_VERSION'] = '4.2.10'
|
||||
gem 'mysql2', '~> 0.3.18'
|
||||
else
|
||||
gem "mysql2"
|
||||
end
|
||||
gem "rails", "~> #{ENV['RAILS_VERSION'] || DEFAULT_RAILS_VERSION}"
|
||||
gem "activerecord", "~> #{ENV['RAILS_VERSION'] || DEFAULT_RAILS_VERSION}"
|
||||
|
|
|
@ -21,11 +21,18 @@ module GitHub
|
|||
# * `nil` is always considered an error and not a usable value. If you need a
|
||||
# SQL NULL, use the NULL constant instead.
|
||||
#
|
||||
# * Identical column names in SELECTs will be overridden:
|
||||
# `SELECT t1.id, t2.id FROM...` will only return one value for `id`. To get
|
||||
# more than one column of the same name, use aliases:
|
||||
# * Identical column names in SELECTs will be overridden for hash_results:
|
||||
# `SELECT t1.id, t2.id FROM...` will only return one value for `id`. The
|
||||
# second ID colum won't be included in the hash:
|
||||
#
|
||||
# [{ "id" => "1" }]
|
||||
#
|
||||
# To get more than one column of the same name, use aliases:
|
||||
# `SELECT t1.id t1_id, t2.id t2_id FROM ...`
|
||||
#
|
||||
# Calling `results` however will return an array with all the values:
|
||||
# [[1, 1]]
|
||||
#
|
||||
# * Arrays are escaped as `(item, item, item)`. If you need to insert multiple
|
||||
# rows (Arrays of Arrays), you must specify the bind value using
|
||||
# GitHub::SQL::ROWS(array_of_arrays).
|
||||
|
@ -232,9 +239,9 @@ module GitHub
|
|||
|
||||
when /\ASELECT/i
|
||||
# Why not execute or select_rows? Because select_all hits the query cache.
|
||||
@hash_results = connection.select_all(query, "#{self.class.name} Select").to_ary
|
||||
@results = @hash_results.map(&:values)
|
||||
|
||||
ar_results = connection.select_all(query, "#{self.class.name} Select")
|
||||
@hash_results = ar_results.to_ary
|
||||
@results = ar_results.rows
|
||||
else
|
||||
@results = connection.execute(query, "#{self.class.name} Execute").to_a
|
||||
end
|
||||
|
|
|
@ -359,4 +359,31 @@ class GitHub::SQLModelTest < Minitest::Test
|
|||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `repositories`")
|
||||
end
|
||||
end
|
||||
|
||||
def test_results_return_all_values_and_hash_returns_deduplicated_values
|
||||
begin
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `repositories`")
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
CREATE TABLE `repositories` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) DEFAULT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
`created_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
SQL
|
||||
Repository.create(name: "I am a repo")
|
||||
|
||||
sql = GitHub::SQL.new(repo_id: 1)
|
||||
|
||||
sql.add("SELECT id, NULL, NULL, name from repositories")
|
||||
|
||||
assert_equal [1, nil, nil, "I am a repo"], sql.results.flatten
|
||||
|
||||
# Hashes can't have more than one key with the same name, so `to_ary` will de-dup the NULL column values
|
||||
assert_equal [{ "id" => 1, "NULL" => nil, "name" => "I am a repo" }], sql.hash_results
|
||||
ensure
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `repositories`")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче