Skip to content

Commit

Permalink
Use show create table.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Dec 2, 2011
1 parent 36cc1f7 commit f3470b0
Showing 1 changed file with 8 additions and 3 deletions.
Expand Up @@ -499,9 +499,14 @@ def show_variable(name)

# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table)
execute_and_free("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA') do |result|
keys = each_hash(result).map { |row| row[:Column_name] }
keys.length == 1 ? [keys.first, nil] : nil
execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result|
create_table = each_hash(result).first[:"Create Table"]
if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/
keys = $1.split(",").map { |key| key.gsub(/`/, "") }
keys.length == 1 ? [keys.first, nil] : nil
else
nil
end
end
end

Expand Down

0 comments on commit f3470b0

Please sign in to comment.