Skip to content

Commit

Permalink
Refactor copy_table_sql and copy_into_sql into postgres shared Databa…
Browse files Browse the repository at this point in the history
…seMethods.
  • Loading branch information
bdon committed Nov 2, 2012
1 parent d09bfc0 commit a4e3d91
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
36 changes: 4 additions & 32 deletions lib/sequel/adapters/postgres.rb
Expand Up @@ -268,24 +268,8 @@ def execute(sql, opts={}, &block)
# per row. If a block is not provided, a single string is returned with all
# of the data.
def copy_table(table, opts={})
sql = if table.is_a?(String)
sql = table
else
if opts[:options] || opts[:format]
options = " ("
options << "FORMAT #{opts[:format]}" if opts[:format]
options << "#{', ' if opts[:format]}#{opts[:options]}" if opts[:options]
options << ')'
end
table = if table.is_a?(::Sequel::Dataset)
"(#{table.sql})"
else
literal(table)
end
sql = "COPY #{table} TO STDOUT#{options}"
end
synchronize(opts[:server]) do |conn|
conn.execute(sql)
synchronize(opts[:server]) do |conn|
conn.execute(copy_table_sql(table, opts))
begin
if block_given?
while buf = conn.get_copy_data
Expand Down Expand Up @@ -323,18 +307,6 @@ def copy_table(table, opts={})
# If a block is provided and :data option is not, this will yield to the block repeatedly.
# The block should return a string, or nil to signal that it is finished.
def copy_into(table, opts={})
sql = "COPY #{literal(table)}"
if cols = opts[:columns]
sql << literal(Array(cols))
end
sql << " FROM STDIN"
if opts[:options] || opts[:format]
sql << " ("
sql << "FORMAT #{opts[:format]}" if opts[:format]
sql << "#{', ' if opts[:format]}#{opts[:options]}" if opts[:options]
sql << ')'
end

data = opts[:data]
data = Array(data) if data.is_a?(String)

Expand All @@ -344,8 +316,8 @@ def copy_into(table, opts={})
raise Error, "Must provide either a :data option or a block to copy_into"
end

synchronize(opts[:server]) do |conn|
conn.execute(sql)
synchronize(opts[:server]) do |conn|
conn.execute(copy_into_sql(table, opts))
begin
if block_given?
while buf = yield
Expand Down
36 changes: 36 additions & 0 deletions lib/sequel/adapters/shared/postgres.rb
Expand Up @@ -603,6 +603,42 @@ def constraint_definition_sql(constraint)
end
end

# SQL for doing fast table insert from stdin.
def copy_into_sql(table, opts)
sql = "COPY #{literal(table)}"
if cols = opts[:columns]
sql << literal(Array(cols))
end
sql << " FROM STDIN"
if opts[:options] || opts[:format]
sql << " ("
sql << "FORMAT #{opts[:format]}" if opts[:format]
sql << "#{', ' if opts[:format]}#{opts[:options]}" if opts[:options]
sql << ')'
end
sql
end

# SQL for doing fast table output to stdout.
def copy_table_sql(table, opts)
if table.is_a?(String)
return table
else
if opts[:options] || opts[:format]
options = " ("
options << "FORMAT #{opts[:format]}" if opts[:format]
options << "#{', ' if opts[:format]}#{opts[:options]}" if opts[:options]
options << ')'
end
table = if table.is_a?(::Sequel::Dataset)
"(#{table.sql})"
else
literal(table)
end
return "COPY #{table} TO STDOUT#{options}"
end
end

# SQL statement to create database function.
def create_function_sql(name, definition, opts={})
args = opts[:args]
Expand Down

0 comments on commit a4e3d91

Please sign in to comment.