sqldump 0.0.2 → 0.0.3

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.
data/CHANGELOG CHANGED
@@ -1,4 +1,10 @@
1
- Version 0.0.1
1
+ Version 0.0.3
2
+ -------------
3
+
4
+ - Added pretty print option for SQL output.
5
+
6
+
7
+ Version 0.0.2
2
8
  -------------
3
9
 
4
10
  New
data/README.md CHANGED
@@ -55,7 +55,6 @@ This gem is created by Mats Sigge and is under the MIT License.
55
55
  ###Immediate goals
56
56
  * Selecting only some columns.
57
57
  * Support for UPDATEs.
58
- * Pretty-printing SQL.
59
58
  * Suppressing NULL columns in output.
60
59
  * Handling auto-incrementing columns, e.g. nextval('sequence_name') in PostgreSQL.
61
60
  * ODBC driver to enable SQL Server.
@@ -73,6 +72,7 @@ This gem is created by Mats Sigge and is under the MIT License.
73
72
  * Support for SQLite3
74
73
  * Support for PostgreSQL
75
74
  * Support for MySQL
75
+ * Pretty-printing SQL.
76
76
 
77
77
 
78
78
  ## History
@@ -1,7 +1,7 @@
1
1
  Feature: dump as csv
2
2
 
3
3
  The default mode of operation of sqldump is to output data in CSV
4
-
4
+
5
5
  Scenario: one-column, one-row table
6
6
  Given a database "foo.sqlite" with a table "number" with the following data
7
7
  | number[int] |
@@ -21,3 +21,20 @@ Feature: dump data as INSERT statements
21
21
  """
22
22
  INSERT INTO number (number) VALUES (NULL);
23
23
  """
24
+
25
+ Scenario: pretty print
26
+ Given a database "foo.sqlite" with a table "numbers_and_strings" with the following data
27
+ | number[int] | string |
28
+ | 42 | thingy |
29
+ When I run `sqldump -d foo.sqlite -it numbers_and_strings`
30
+ Then it should pass with:
31
+ """
32
+ INSERT INTO numbers_and_strings (
33
+ number,
34
+ string
35
+ )
36
+ VALUES (
37
+ 42,
38
+ 'thingy'
39
+ );
40
+ """
@@ -18,8 +18,15 @@ module Sqldump
18
18
  @column_type_by_name[column_name]
19
19
  end
20
20
 
21
+ def output_list(list)
22
+ @io.print "\n#{indent}" if pretty
23
+ join_string = pretty ? ",\n#{indent}" : ", "
24
+ @io.print list.join(join_string)
25
+ @io.print "\n" if pretty
26
+ end
27
+
21
28
  def output_column_names
22
- @io.print @sth.column_names.join(", ")
29
+ output_list(@sth.column_names)
23
30
  end
24
31
 
25
32
  def output_values(row)
@@ -27,14 +34,17 @@ module Sqldump
27
34
  row.each_with_name do |value, column_name|
28
35
  quoted_list.push quote(value, column_name)
29
36
  end
30
- @io.print quoted_list.join(", ")
37
+ output_list(quoted_list)
38
+ #@io.print quoted_list.join(", ")
31
39
  end
32
40
 
33
41
  def output
34
42
  @sth.fetch do |row|
35
43
  @io.print("INSERT INTO #{@options.table} (")
36
44
  output_column_names()
37
- @io.print ") VALUES ("
45
+ @io.print ")"
46
+ @io.print pretty ? "\n" : " "
47
+ @io.print "VALUES ("
38
48
  output_values(row)
39
49
  @io.print ");\n"
40
50
  end
@@ -54,6 +64,14 @@ module Sqldump
54
64
  end
55
65
  end
56
66
 
67
+ def indent
68
+ " " * 4
69
+ end
70
+
71
+ def pretty
72
+ @options.pretty
73
+ end
74
+
57
75
  end
58
76
 
59
77
  end
@@ -15,6 +15,7 @@ module Sqldump
15
15
  attr_accessor :sql
16
16
  attr_accessor :csv_header
17
17
  attr_accessor :dump_mode
18
+ attr_accessor :pretty
18
19
 
19
20
  def initialize(argv)
20
21
  parse_options(argv)
@@ -85,6 +86,10 @@ module Sqldump
85
86
  self.dump_mode = :insert
86
87
  end
87
88
 
89
+ opts.on('-t', '--pretty', 'Pretty-print SQL output (i.e. columns on separate lines, indentation).') do
90
+ self.pretty = true
91
+ end
92
+
88
93
  opts.on('-H', '--header', 'Include column names in csv mode.') do
89
94
  self.csv_header = true
90
95
  end
@@ -1,3 +1,3 @@
1
1
  module Sqldump
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -8,11 +8,14 @@ module Sqldump
8
8
 
9
9
  describe "#output" do
10
10
 
11
- def formatter_example(expected_result)
11
+ def formatter_example(expected_result, options = nil)
12
12
  strio = StringIO.new
13
13
 
14
- options = double("Options")
15
- options.stub(:table).and_return('numbers_and_strings')
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
16
19
 
17
20
  formatter = InsertFormatter.new(@sth, strio, options)
18
21
  formatter.output
@@ -37,6 +40,22 @@ module Sqldump
37
40
  formatter_example("INSERT INTO numbers_and_strings (number, string) VALUES (42, 'thingy');\n")
38
41
  end
39
42
 
43
+ it "pretty-prints output with the pretty option" do
44
+ options = double("options")
45
+ options.stub(:table).and_return('numbers_and_strings')
46
+ options.stub(:pretty).and_return(true)
47
+ formatter_example(<<"EOT", options)
48
+ INSERT INTO numbers_and_strings (
49
+ number,
50
+ string
51
+ )
52
+ VALUES (
53
+ 42,
54
+ 'thingy'
55
+ );
56
+ EOT
57
+ end
58
+
40
59
  end
41
60
 
42
61
  describe "#quote" do
@@ -62,7 +62,7 @@ module Sqldump
62
62
 
63
63
  end
64
64
 
65
- describe 'username' do
65
+ describe '-U --username' do
66
66
  it 'sets the username to nil if not specified' do
67
67
  options = make_options([])
68
68
  options.username.should be_nil
@@ -74,7 +74,7 @@ module Sqldump
74
74
  end
75
75
  end
76
76
 
77
- describe 'password' do
77
+ describe '-P --password' do
78
78
  it 'sets the password to nil if not specified' do
79
79
  options = make_options([])
80
80
  options.password.should be_nil
@@ -93,7 +93,7 @@ module Sqldump
93
93
  end
94
94
  end
95
95
 
96
- describe 'csv header option' do
96
+ describe '-H --header : csv header option' do
97
97
  it "the csv_header flag is false by default" do
98
98
  options = make_options([])
99
99
  options.csv_header.should be_false
@@ -105,13 +105,19 @@ module Sqldump
105
105
  end
106
106
  end
107
107
 
108
- describe 'insert mode option' do
108
+ describe '-i --insert : insert mode option' do
109
109
  it "sets the mode to :insert" do
110
110
  options = make_options(%w(-i table))
111
111
  options.dump_mode.should == :insert
112
112
  end
113
113
  end
114
114
 
115
+ describe '-t --pretty : pretty print option' do
116
+ it "sets the pretty flag" do
117
+ options = make_options(%w(-t))
118
+ options.pretty.should be_true
119
+ end
120
+ end
115
121
  end
116
122
 
117
123
  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.2
4
+ version: 0.0.3
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-06 00:00:00.000000000Z
12
+ date: 2012-02-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dbi
16
- requirement: &2152906260 !ruby/object:Gem::Requirement
16
+ requirement: &2160964240 !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: *2152906260
24
+ version_requirements: *2160964240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: dbd-sqlite3
27
- requirement: &2152905460 !ruby/object:Gem::Requirement
27
+ requirement: &2160963440 !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: *2152905460
35
+ version_requirements: *2160963440
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2152897840 !ruby/object:Gem::Requirement
38
+ requirement: &2160962740 !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: *2152897840
46
+ version_requirements: *2160962740
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: cucumber
49
- requirement: &2152896120 !ruby/object:Gem::Requirement
49
+ requirement: &2160961720 !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: *2152896120
57
+ version_requirements: *2160961720
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: aruba
60
- requirement: &2152895320 !ruby/object:Gem::Requirement
60
+ requirement: &2160960920 !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: *2152895320
68
+ version_requirements: *2160960920
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &2152894720 !ruby/object:Gem::Requirement
71
+ requirement: &2160959940 !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: *2152894720
79
+ version_requirements: *2160959940
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard-cucumber
82
- requirement: &2152893780 !ruby/object:Gem::Requirement
82
+ requirement: &2160958740 !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: *2152893780
90
+ version_requirements: *2160958740
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rb-fsevent
93
- requirement: &2152892500 !ruby/object:Gem::Requirement
93
+ requirement: &2160956780 !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: *2152892500
101
+ version_requirements: *2160956780
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: dbd-pg
104
- requirement: &2152890760 !ruby/object:Gem::Requirement
104
+ requirement: &2160955100 !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: *2152890760
112
+ version_requirements: *2160955100
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: