pg_copy 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_copy.rb +15 -4
- data/lib/pg_copy/copy_from_csv.rb +39 -0
- data/lib/pg_copy/copy_to_csv.rb +17 -3
- data/lib/pg_copy/version.rb +1 -1
- metadata +18 -11
- data/.gitignore +0 -10
- data/.rspec +0 -2
- data/.travis.yml +0 -4
- data/Gemfile +0 -4
- data/Rakefile +0 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/pg_copy.gemspec +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 231532457b1b447cf36b4a3011c85fb583d544c7
|
4
|
+
data.tar.gz: ef3badfdaafcf3aff0d7ad845193a7d31b7a0f99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d822841d50c580d7c83688445c95a6d3149653d350f1062c17aea0b6f8d114d1dcd6f1a60969fcdf403a6c39021de4ccb3062273aa10cc94972365f88fea304
|
7
|
+
data.tar.gz: 4767d61720cc1a29a29460535a1860c036b61ceeae3d7adc175bda509aaeeefa0131a815be08e5698454581e98a913723ed68ca2501495bb42c5189f2fda7451
|
data/lib/pg_copy.rb
CHANGED
@@ -1,7 +1,18 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'pg'
|
2
|
+
require 'pg_copy/version'
|
3
|
+
require 'pg_copy/copy_to_csv'
|
4
|
+
require 'pg_copy/copy_from_csv'
|
4
5
|
|
5
6
|
module PgCopy
|
6
|
-
|
7
|
+
class << self
|
8
|
+
attr_accessor :default_connection
|
9
|
+
|
10
|
+
def copy_to_csv(sql, dest, options = {})
|
11
|
+
PgCopy::CopyToCSV.new(default_connection, sql, dest, options).copy
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_from_csv(source, table, columns, options = {})
|
15
|
+
PgCopy::CopyFromCSV.new(default_connection, source, table, columns, options).copy
|
16
|
+
end
|
17
|
+
end
|
7
18
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module PgCopy
|
4
|
+
class CopyFromCSV
|
5
|
+
SQL = 'COPY %<table>s (%<columns>s) FROM STDIN WITH CSV'.freeze
|
6
|
+
|
7
|
+
DEFAULTS = {
|
8
|
+
header: false
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
def initialize(conn, file, table, columns, options = {})
|
12
|
+
@conn = conn
|
13
|
+
@file = file
|
14
|
+
@table = table
|
15
|
+
@columns = columns
|
16
|
+
@options = DEFAULTS.merge(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def copy
|
20
|
+
sql = format(SQL, table: @conn.escape_identifier(@table),
|
21
|
+
columns: @columns.join(','))
|
22
|
+
|
23
|
+
csv = CSV.foreach(@file).lazy
|
24
|
+
csv = csv.drop(1) if skip_header?
|
25
|
+
|
26
|
+
@conn.copy_data(sql) do
|
27
|
+
csv.each do |row|
|
28
|
+
@conn.put_copy_data(row.to_csv)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def skip_header?
|
36
|
+
@options[:header]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/pg_copy/copy_to_csv.rb
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
module PgCopy
|
2
2
|
class CopyToCSV
|
3
|
-
SQL =
|
3
|
+
SQL = <<-SQL.freeze
|
4
|
+
COPY (%<sql>s) TO STDOUT (
|
5
|
+
FORMAT CSV,
|
6
|
+
DELIMITER '%<delimiter>c',
|
7
|
+
HEADER %<header>s,
|
8
|
+
QUOTE '%<quote>c'
|
9
|
+
)
|
10
|
+
SQL
|
4
11
|
|
5
|
-
|
12
|
+
DEFAULTS = {
|
13
|
+
delimiter: ',',
|
14
|
+
header: false,
|
15
|
+
quote: '"'
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
def initialize(conn, sql, dest, options = {})
|
6
19
|
@conn = conn
|
7
20
|
@sql = sql
|
8
21
|
@dest = dest
|
22
|
+
@options = DEFAULTS.merge(options)
|
9
23
|
end
|
10
24
|
|
11
25
|
def copy
|
12
|
-
sql = format(SQL, sql: @sql)
|
26
|
+
sql = format(SQL, sql: @sql, **@options)
|
13
27
|
|
14
28
|
File.open(@dest, 'wb') do |f|
|
15
29
|
@conn.copy_data(sql) do
|
data/lib/pg_copy/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_copy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lenon Marcel
|
@@ -11,9 +11,9 @@ authors:
|
|
11
11
|
- Diogo Lisboa
|
12
12
|
- Pablo Silva
|
13
13
|
autorequire:
|
14
|
-
bindir:
|
14
|
+
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2016-02-
|
16
|
+
date: 2016-02-19 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: bundler
|
@@ -71,6 +71,20 @@ dependencies:
|
|
71
71
|
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: activerecord
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
type: :runtime
|
82
|
+
prerelease: false
|
83
|
+
version_requirements: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
74
88
|
description: A wrapper for PostgreSQL's COPY command
|
75
89
|
email:
|
76
90
|
- lenon.marcel@gmail.com
|
@@ -83,18 +97,11 @@ executables: []
|
|
83
97
|
extensions: []
|
84
98
|
extra_rdoc_files: []
|
85
99
|
files:
|
86
|
-
- ".gitignore"
|
87
|
-
- ".rspec"
|
88
|
-
- ".travis.yml"
|
89
|
-
- Gemfile
|
90
100
|
- README.md
|
91
|
-
- Rakefile
|
92
|
-
- bin/console
|
93
|
-
- bin/setup
|
94
101
|
- lib/pg_copy.rb
|
102
|
+
- lib/pg_copy/copy_from_csv.rb
|
95
103
|
- lib/pg_copy/copy_to_csv.rb
|
96
104
|
- lib/pg_copy/version.rb
|
97
|
-
- pg_copy.gemspec
|
98
105
|
homepage: https://github.com/locaweb/pg_copy
|
99
106
|
licenses: []
|
100
107
|
metadata: {}
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "pg_copy"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
data/bin/setup
DELETED
data/pg_copy.gemspec
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'pg_copy/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "pg_copy"
|
8
|
-
spec.version = PgCopy::VERSION
|
9
|
-
|
10
|
-
spec.authors = ['Lenon Marcel',
|
11
|
-
'Rafael Timbó',
|
12
|
-
'Rodolfo Liviero',
|
13
|
-
'Italo Moraes',
|
14
|
-
'Diogo Lisboa',
|
15
|
-
'Pablo Silva']
|
16
|
-
spec.email = ['lenon.marcel@gmail.com',
|
17
|
-
'rafaeltimbosoares@gmail.com',
|
18
|
-
'rodolfoliviero@gmail.com',
|
19
|
-
'itu.moraes@gmail.com',
|
20
|
-
'diogoslisboa@gmail.com',
|
21
|
-
'phsilbr@gmail.com']
|
22
|
-
|
23
|
-
spec.summary = "A wrapper for PostgreSQL's COPY command"
|
24
|
-
spec.description = "A wrapper for PostgreSQL's COPY command"
|
25
|
-
spec.homepage = 'https://github.com/locaweb/pg_copy'
|
26
|
-
|
27
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
-
spec.bindir = "exe"
|
29
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = ["lib"]
|
31
|
-
|
32
|
-
spec.add_development_dependency "bundler", "~> 1.11"
|
33
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
-
|
36
|
-
spec.add_dependency "pg"
|
37
|
-
end
|