mysql2json 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mysql2json.rb +53 -0
  3. metadata +71 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b318dc41c79f612e994513125790f69bc6d87cf2
4
+ data.tar.gz: b40b7f19cfe38e30acf84e32bdfb9801c8fc83cd
5
+ SHA512:
6
+ metadata.gz: 8a0fc5b6c6214284b757c82eb66be099b7fae1d622720cdaa6257ec0555cc404f020015d2d69ce4b23f6bcf840ba7d7cc546b54105a1359d2af71a528caa6cd4
7
+ data.tar.gz: 42e9fe4c6269fe46bc3d5e59b073d5d7d45a966a2029843464bdb4caa412a964ecc54066b98e3d57e20537a86061956a9fe4e0e457f015d33e11aa95ec33f624
data/lib/mysql2json.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'mysql2'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ def is_json_valid(object)
6
+ JSON.parse(object)
7
+ return true
8
+ rescue JSON::ParserError
9
+ return false
10
+ end
11
+
12
+ def convert(results)
13
+ non_json_data = []
14
+
15
+ results = Array(results)
16
+
17
+ results.each_index do |index|
18
+ non_json_data.push(results[index]);
19
+ index += 1
20
+ puts 'row: '+index.to_s
21
+ break if index == 10
22
+ end
23
+
24
+ json_data = non_json_data.map { |o| Hash[o.each_pair.to_a] }.to_json
25
+
26
+ if is_json_valid(json_data)
27
+ json_data = json_data.to_s
28
+ else
29
+ json_data = 'ParserError: Could not parse to json, retry.'
30
+ end
31
+
32
+ return json_data
33
+ end
34
+
35
+ class Mysql2json
36
+ def self.query(q, client)
37
+ results = client.query(q)
38
+ return convert(results)
39
+ end
40
+
41
+ def self.query_export(q, client, filename)
42
+ results = client.query(q)
43
+ convert(results)
44
+ file = File.open(filename, 'w')
45
+ file.write(json_data)
46
+ file.close()
47
+ puts 'Data written to %s' % filename
48
+ end
49
+
50
+ end
51
+
52
+
53
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql2json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jared Wright
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.11
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.11
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.7
41
+ description: A simple script to convert mysql data into json
42
+ email: jawerty210@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/mysql2json.rb
48
+ homepage: http://rubygems.org/gems/mysql2json
49
+ licenses: []
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.0.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Convert mysql data into json
71
+ test_files: []