sqldump 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ Version 0.0.4
2
+ -------------
3
+
4
+ - Added suppress nulls option for SQL output.
5
+
6
+
1
7
  Version 0.0.3
2
8
  -------------
3
9
 
data/NOTES ADDED
@@ -0,0 +1,3 @@
1
+ To run the Cucumber features, you will need a local PostgreSQL installation
2
+ and a user "sqldump" with password "sqldump". The sqldump user must have
3
+ the CREATE_DB attribute.
data/README.md CHANGED
@@ -10,10 +10,12 @@ Just install the gem with
10
10
  $ gem install sqldump
11
11
  ```
12
12
 
13
- This will make the executable `sqldump` available from your command line. SQLite3 is a direct dependency and is installed if you don't already have it. Other drivers must be installed manually. E.g.
13
+ This will make the executable `sqldump` available from your command line.
14
+
15
+ Database access is through Ruby/DBI. DBD::SQLite3 is a direct dependency and is installed if you don't already have it. Other drivers must be installed manually. E.g.
14
16
 
15
17
  ```bash
16
- $ gem install pg
18
+ $ gem install dbd-pg
17
19
  ```
18
20
 
19
21
  **Requires Ruby 1.9.2 or later.**
@@ -53,9 +55,7 @@ This gem is created by Mats Sigge and is under the MIT License.
53
55
  ## Roadmap
54
56
 
55
57
  ###Immediate goals
56
- * Selecting only some columns.
57
58
  * Support for UPDATEs.
58
- * Suppressing NULL columns in output.
59
59
  * Handling auto-incrementing columns, e.g. nextval('sequence_name') in PostgreSQL.
60
60
  * ODBC driver to enable SQL Server.
61
61
  * Ability to specify custom connection string parameters (or entire connection string).
@@ -73,6 +73,8 @@ This gem is created by Mats Sigge and is under the MIT License.
73
73
  * Support for PostgreSQL
74
74
  * Support for MySQL
75
75
  * Pretty-printing SQL.
76
+ * Suppressing NULL columns in output.
77
+ * Selecting only some columns.
76
78
 
77
79
 
78
80
  ## History
@@ -38,3 +38,23 @@ Feature: dump data as INSERT statements
38
38
  'thingy'
39
39
  );
40
40
  """
41
+
42
+ Scenario: suppress nulls
43
+ Given a database "foo.sqlite" with a table "numbers_and_strings" with the following data
44
+ | number[int] | string |
45
+ | 42 | <null> |
46
+ When I run `sqldump -d foo.sqlite -il numbers_and_strings`
47
+ Then it should pass with:
48
+ """
49
+ INSERT INTO numbers_and_strings (number) VALUES (42);
50
+ """
51
+
52
+ Scenario: select specific columns
53
+ Given a database "foo.sqlite" with a table "numbers_strings_and_things" with the following data
54
+ | number[int] | string | thing |
55
+ | 42 | foo | bar |
56
+ When I run `sqldump -d foo.sqlite -is "number,thing" numbers_strings_and_things`
57
+ Then it should pass with:
58
+ """
59
+ INSERT INTO numbers_strings_and_things (number, thing) VALUES (42, 'bar');
60
+ """
@@ -25,10 +25,6 @@ module Sqldump
25
25
  @io.print "\n" if pretty
26
26
  end
27
27
 
28
- def output_column_names
29
- output_list(@sth.column_names)
30
- end
31
-
32
28
  def output_values(row)
33
29
  quoted_list = []
34
30
  row.each_with_name do |value, column_name|
@@ -38,14 +34,27 @@ module Sqldump
38
34
  #@io.print quoted_list.join(", ")
39
35
  end
40
36
 
37
+ def cols_and_values(row)
38
+ cols = []
39
+ values = []
40
+ row.each_with_name do |value, column_name|
41
+ unless @options.suppress_nulls && value.nil?
42
+ cols.push column_name
43
+ values.push quote(value, column_name)
44
+ end
45
+ end
46
+ [cols, values]
47
+ end
48
+
41
49
  def output
42
50
  @sth.fetch do |row|
51
+ (cols, values) = cols_and_values(row)
43
52
  @io.print("INSERT INTO #{@options.table} (")
44
- output_column_names()
53
+ output_list(cols)
45
54
  @io.print ")"
46
55
  @io.print pretty ? "\n" : " "
47
56
  @io.print "VALUES ("
48
- output_values(row)
57
+ output_list(values)
49
58
  @io.print ");\n"
50
59
  end
51
60
  end
@@ -16,6 +16,8 @@ module Sqldump
16
16
  attr_accessor :csv_header
17
17
  attr_accessor :dump_mode
18
18
  attr_accessor :pretty
19
+ attr_accessor :suppress_nulls
20
+ attr_accessor :columns_to_select
19
21
 
20
22
  def initialize(argv)
21
23
  parse_options(argv)
@@ -26,7 +28,7 @@ module Sqldump
26
28
  def set_derived_options(argv)
27
29
  self.table = argv[0]
28
30
 
29
- self.sql = "select * from " + argv.join(" ")
31
+ self.sql = "select #{columns_to_select} from " + argv.join(" ")
30
32
  end
31
33
 
32
34
  def parse_options(argv)
@@ -42,6 +44,7 @@ module Sqldump
42
44
  self.dump_mode = :csv
43
45
  self.database_type = :sqlite3
44
46
  self.host = 'localhost'
47
+ self.columns_to_select = '*'
45
48
  end
46
49
 
47
50
  def define_options
@@ -90,6 +93,14 @@ module Sqldump
90
93
  self.pretty = true
91
94
  end
92
95
 
96
+ opts.on('-l', '--suppress-nulls', 'Suppresses null columns in insert mode.') do
97
+ self.suppress_nulls = true
98
+ end
99
+
100
+ opts.on('-s', '--select-columns COLUMN[,COLUMN...]', 'Specifies which columns to select.') do |columns|
101
+ self.columns_to_select = columns
102
+ end
103
+
93
104
  opts.on('-H', '--header', 'Include column names in csv mode.') do
94
105
  self.csv_header = true
95
106
  end
@@ -1,3 +1,3 @@
1
1
  module Sqldump
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -8,14 +8,19 @@ module Sqldump
8
8
 
9
9
  describe "#output" do
10
10
 
11
- def formatter_example(expected_result, options = nil)
11
+ let :options do
12
+ options = double("Options")
13
+ options.stub(:table).and_return('numbers_and_strings')
14
+ options.stub(:pretty).and_return(false)
15
+ options.stub(:suppress_nulls).and_return(false)
16
+ options.stub(:columns_to_select).and_return("*")
17
+ options
18
+ end
19
+
20
+ def formatter_example(expected_result, opts = nil)
12
21
  strio = StringIO.new
13
22
 
14
- unless options
15
- options = double("Options")
16
- options.stub(:table).and_return('numbers_and_strings')
17
- options.stub(:pretty).and_return(false)
18
- end
23
+ opts ||= options
19
24
 
20
25
  formatter = InsertFormatter.new(@sth, strio, options)
21
26
  formatter.output
@@ -41,8 +46,6 @@ module Sqldump
41
46
  end
42
47
 
43
48
  it "pretty-prints output with the pretty option" do
44
- options = double("options")
45
- options.stub(:table).and_return('numbers_and_strings')
46
49
  options.stub(:pretty).and_return(true)
47
50
  formatter_example(<<"EOT", options)
48
51
  INSERT INTO numbers_and_strings (
@@ -56,6 +59,13 @@ VALUES (
56
59
  EOT
57
60
  end
58
61
 
62
+ it "removes null columns and values with the suppress nulls option" do
63
+ options.stub(:suppress_nulls).and_return(true)
64
+ @dbh.do("update numbers_and_strings set string = null")
65
+ @sth.finish
66
+ @sth = @dbh.execute "select * from numbers_and_strings"
67
+ formatter_example("INSERT INTO numbers_and_strings (number) VALUES (42);\n", options)
68
+ end
59
69
  end
60
70
 
61
71
  describe "#quote" do
@@ -118,6 +118,21 @@ module Sqldump
118
118
  options.pretty.should be_true
119
119
  end
120
120
  end
121
+
122
+ describe '-l --suppress-nulls option' do
123
+ it "sets the suppress_nulls flag" do
124
+ options = make_options(%w(-l))
125
+ options.suppress_nulls.should be_true
126
+ end
127
+ end
128
+
129
+ describe '-s --select-columns option' do
130
+ it "sets the selected columns" do
131
+ options = make_options(%w(-s foo,bar))
132
+ options.columns_to_select.should == "foo,bar"
133
+ options.sql.should == "select foo,bar from table"
134
+ end
135
+ end
121
136
  end
122
137
 
123
138
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqldump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000Z
12
+ date: 2012-02-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dbi
16
- requirement: &2160964240 !ruby/object:Gem::Requirement
16
+ requirement: &2156508360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2160964240
24
+ version_requirements: *2156508360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: dbd-sqlite3
27
- requirement: &2160963440 !ruby/object:Gem::Requirement
27
+ requirement: &2156507400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2160963440
35
+ version_requirements: *2156507400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2160962740 !ruby/object:Gem::Requirement
38
+ requirement: &2156506720 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2160962740
46
+ version_requirements: *2156506720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: cucumber
49
- requirement: &2160961720 !ruby/object:Gem::Requirement
49
+ requirement: &2156505760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2160961720
57
+ version_requirements: *2156505760
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: aruba
60
- requirement: &2160960920 !ruby/object:Gem::Requirement
60
+ requirement: &2156504980 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2160960920
68
+ version_requirements: *2156504980
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &2160959940 !ruby/object:Gem::Requirement
71
+ requirement: &2156504160 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2160959940
79
+ version_requirements: *2156504160
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard-cucumber
82
- requirement: &2160958740 !ruby/object:Gem::Requirement
82
+ requirement: &2156503140 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2160958740
90
+ version_requirements: *2156503140
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rb-fsevent
93
- requirement: &2160956780 !ruby/object:Gem::Requirement
93
+ requirement: &2156501720 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2160956780
101
+ version_requirements: *2156501720
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: dbd-pg
104
- requirement: &2160955100 !ruby/object:Gem::Requirement
104
+ requirement: &2156499340 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *2160955100
112
+ version_requirements: *2156499340
113
113
  description: A command line tool to generate SQL insert or update statements from
114
114
  the data in a database.
115
115
  email:
@@ -127,6 +127,7 @@ files:
127
127
  - Gemfile
128
128
  - Guardfile
129
129
  - LICENSE
130
+ - NOTES
130
131
  - README.md
131
132
  - Rakefile
132
133
  - bin/sqldump