MYSQLSafe 0.0.0 → 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/MYSQLSafe.gemspec +23 -0
- data/README.md +29 -0
- data/Rakefile +11 -0
- data/lib/MYSQLSafe/version.rb +3 -0
- data/lib/MYSQLSafe.rb +5 -0
- metadata +55 -12
- checksums.yaml +0 -15
- data/lib/mysqlsafe.rb +0 -120
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Sam NIssen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/MYSQLSafe.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'MYSQLSafe/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "MYSQLSafe"
|
8
|
+
spec.version = MYSQLSafe::VERSION
|
9
|
+
spec.authors = ["Sam NIssen"]
|
10
|
+
spec.email = ["Samuel.Nissen@LinkShare.com"]
|
11
|
+
spec.description = %q{Connect to MYSQL more easily}
|
12
|
+
spec.summary = %q{An abstraction of the MYSQL gem to automatically close connections, return arrays and sanatize some of the inputs}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# MYSQLSafe
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'MYSQLSafe'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install MYSQLSafe
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/MYSQLSafe.rb
ADDED
metadata
CHANGED
@@ -1,44 +1,87 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: MYSQLSafe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
|
-
- Sam
|
8
|
+
- Sam NIssen
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
12
|
+
date: 2013-09-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Connect to MYSQL more easily
|
47
|
+
email:
|
48
|
+
- Samuel.Nissen@LinkShare.com
|
15
49
|
executables: []
|
16
50
|
extensions: []
|
17
51
|
extra_rdoc_files: []
|
18
52
|
files:
|
19
|
-
-
|
20
|
-
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- MYSQLSafe.gemspec
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/MYSQLSafe.rb
|
60
|
+
- lib/MYSQLSafe/version.rb
|
61
|
+
homepage: ''
|
21
62
|
licenses:
|
22
63
|
- MIT
|
23
|
-
metadata: {}
|
24
64
|
post_install_message:
|
25
65
|
rdoc_options: []
|
26
66
|
require_paths:
|
27
67
|
- lib
|
28
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
29
70
|
requirements:
|
30
71
|
- - ! '>='
|
31
72
|
- !ruby/object:Gem::Version
|
32
73
|
version: '0'
|
33
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
34
76
|
requirements:
|
35
77
|
- - ! '>='
|
36
78
|
- !ruby/object:Gem::Version
|
37
79
|
version: '0'
|
38
80
|
requirements: []
|
39
81
|
rubyforge_project:
|
40
|
-
rubygems_version:
|
82
|
+
rubygems_version: 1.8.25
|
41
83
|
signing_key:
|
42
|
-
specification_version:
|
43
|
-
summary:
|
84
|
+
specification_version: 3
|
85
|
+
summary: An abstraction of the MYSQL gem to automatically close connections, return
|
86
|
+
arrays and sanatize some of the inputs
|
44
87
|
test_files: []
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
YTZiNmU3NjU1MTM2ODAxMThjYWM1NjljNTcxZjQzZTUyNzgxMmMyNA==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YTJhYmNjZTE5NTQ2OGMxZWUxMjZmNzFmYzExYzg5OTAyYzNmNDg0YQ==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NDQ0Y2IyYmJjODdmYzRkM2FhMDNkODhiMDFkNzE2NWQxMmM2MjJhMDJhNThi
|
10
|
-
MDVmMTcyZDBmOGEwZmQ5N2U1M2UzOGI0NTVhNDNhM2Q0ZTBhYTNiMmJlNjRh
|
11
|
-
NWRlMzY2MjE3YzExYTg4ZmVmZWY0NDBjODk1MDAwMjMyYWJlOWM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
Mjk2NzA1ZmIzMzdhMGQ3MGRmMTk1MWYyZDc0ZmQwZGI4OGJiMjVlMjczOTE2
|
14
|
-
OTdjNWU3NjljOWM3NTk4Yzg1YzM4YzM0YmZjZGVkNTJlYjcwMDU2MGNlMzYz
|
15
|
-
ODljNmVkMDRhMDRhNTAzMWI0Y2Y5NjRhZDA1YmI2NmUwN2NjN2Q=
|
data/lib/mysqlsafe.rb
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
require 'mysql'
|
2
|
-
|
3
|
-
class MYSQLSafe
|
4
|
-
attr_accessor :encoding
|
5
|
-
attr_reader :host, :database, :user, :password
|
6
|
-
|
7
|
-
def host=(host_string)
|
8
|
-
@host = esc_enc_string(host_string)
|
9
|
-
end
|
10
|
-
def database=(database_string)
|
11
|
-
@database = esc_enc_string(database_string)
|
12
|
-
end
|
13
|
-
def user=(user_string)
|
14
|
-
@user = esc_enc_string(user_string)
|
15
|
-
end
|
16
|
-
def password=(password_string)
|
17
|
-
@password = esc_enc_string(password_string)
|
18
|
-
end
|
19
|
-
|
20
|
-
def connect_safe(raw_sql)
|
21
|
-
sql = esc_enc_string(raw_sql)
|
22
|
-
if @host && @database && @user && password
|
23
|
-
begin
|
24
|
-
@cxtn = Mysql.new(@host, @db, @user, @password)
|
25
|
-
table_names = get_table_names
|
26
|
-
table_match = match_name(table_names, sql)
|
27
|
-
|
28
|
-
if table_match
|
29
|
-
column_names = get_column_names(match)
|
30
|
-
column_match = match_name(column_names, sql)
|
31
|
-
else
|
32
|
-
raise 'MYSQLSafe error: no valid table name could be found in your SQL statement'
|
33
|
-
end
|
34
|
-
|
35
|
-
if column_match
|
36
|
-
ticked_sql = tick_sql(sql, table_match, column_match)
|
37
|
-
else
|
38
|
-
raise 'MYSQLSafe error: no valid column name(s) could be found in your SQL statement'
|
39
|
-
end
|
40
|
-
|
41
|
-
mysql_object = cxtn.query(ticked_sql)
|
42
|
-
mysql_array = []
|
43
|
-
mysql_object.each { |row| mysql_array.push(row) }
|
44
|
-
|
45
|
-
return mysql_array
|
46
|
-
ensure
|
47
|
-
@cxtn.close
|
48
|
-
end
|
49
|
-
else
|
50
|
-
raise 'MYSQLSafe error: Host, Database, User and Password must be set to run a query'
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
def tick_sql(sql, table_array, column_array)
|
56
|
-
ticked_sql = sql.delete("`")
|
57
|
-
table_array.each do |name|
|
58
|
-
ticked_sql = ticked_sql.gsub(name, "`#{name}`")
|
59
|
-
end
|
60
|
-
column_array.each do |col|
|
61
|
-
ticked_sql = ticked_sql.gsub(col, "`#{col}`")
|
62
|
-
end
|
63
|
-
|
64
|
-
return ticked_sql
|
65
|
-
end
|
66
|
-
|
67
|
-
def get_column_names(table_name)
|
68
|
-
column_names_sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='#{@database}' AND `TABLE_NAME`='#{table_name}';"
|
69
|
-
column_names_results_sql = query_safe(column_names_sql)
|
70
|
-
|
71
|
-
column_names = []
|
72
|
-
column_names_results_sql.each do |name|
|
73
|
-
column_names.push(name)
|
74
|
-
end
|
75
|
-
|
76
|
-
return column_names
|
77
|
-
end
|
78
|
-
|
79
|
-
def match_name(name_array, sql)
|
80
|
-
match = []
|
81
|
-
|
82
|
-
name_array.each do |name|
|
83
|
-
match.push(name) if sql.to_s.include?("#{name}=") || sql.to_s.match?(/#{name}\s+=/) || sql.to_s.match?(/#{name}`\s+=/)
|
84
|
-
end
|
85
|
-
|
86
|
-
if match.size > 0
|
87
|
-
return match
|
88
|
-
else
|
89
|
-
return false
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def query_safe(dangerous_sql)
|
94
|
-
@cxtn.query(Mysql.escape_string(dangerous_sql))
|
95
|
-
end
|
96
|
-
|
97
|
-
def get_table_names
|
98
|
-
table_names_sql = "SHOW TABLES FROM `#{@database}`;"
|
99
|
-
table_names_results_sql = query_safe(table_names_sql)
|
100
|
-
|
101
|
-
table_names = []
|
102
|
-
table_names_results_sql.each do |name|
|
103
|
-
table_names.push(name)
|
104
|
-
end
|
105
|
-
|
106
|
-
return table_names
|
107
|
-
end
|
108
|
-
|
109
|
-
def esc_enc_string(string)
|
110
|
-
return esc_string(enc_string(string))
|
111
|
-
end
|
112
|
-
|
113
|
-
def enc_string(string)
|
114
|
-
return string.encode!("#{@encoding}", "#{@encoding}", :invalid => :replace)
|
115
|
-
end
|
116
|
-
|
117
|
-
def esc_string(string)
|
118
|
-
return Mysql.escape_string(string)
|
119
|
-
end
|
120
|
-
end
|