pg_copy 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82d865ae378b95ab956b69ac6231829b6c0c0240
4
- data.tar.gz: ef1c2b9b2d0742eb5ef227e36061d8880533d1cd
3
+ metadata.gz: 231532457b1b447cf36b4a3011c85fb583d544c7
4
+ data.tar.gz: ef3badfdaafcf3aff0d7ad845193a7d31b7a0f99
5
5
  SHA512:
6
- metadata.gz: bc015125da0d789f149ed15daf1b24f2ad4f04a21f0dc657b9d5af7ee28c6773ab323acbdb10641ac3705978b987d14f6c1dd9424a4299ac08b02a4b13b21b5d
7
- data.tar.gz: 385234498337426f1da91ff29da79e4c0cce219c2320b5e9c92353f97bbf5a32e2f87bd39614fb68f76f795cf88829546e7f1b13b8cce2ef044a28a353923259
6
+ metadata.gz: 2d822841d50c580d7c83688445c95a6d3149653d350f1062c17aea0b6f8d114d1dcd6f1a60969fcdf403a6c39021de4ccb3062273aa10cc94972365f88fea304
7
+ data.tar.gz: 4767d61720cc1a29a29460535a1860c036b61ceeae3d7adc175bda509aaeeefa0131a815be08e5698454581e98a913723ed68ca2501495bb42c5189f2fda7451
@@ -1,7 +1,18 @@
1
- require "pg"
2
- require "pg_copy/version"
3
- require "pg_copy/copy_to_csv"
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
- # Your code goes here...
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
@@ -1,15 +1,29 @@
1
1
  module PgCopy
2
2
  class CopyToCSV
3
- SQL = 'COPY (%<sql>s) TO STDOUT WITH CSV'.freeze
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
- def initialize(conn, sql, dest)
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
@@ -1,3 +1,3 @@
1
1
  module PgCopy
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'.freeze
3
3
  end
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.1
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: exe
14
+ bindir: bin
15
15
  cert_chain: []
16
- date: 2016-02-16 00:00:00.000000000 Z
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
@@ -1,10 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- /spec/database.yml
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
@@ -1,4 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.3.0
4
- before_install: gem install bundler -v 1.11.2
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in pg_copy.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -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
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -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