MYSQLSafe 0.0.2 → 0.0.4

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/lib/MYSQLSafe.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "MYSQLSafe/version.rb"
2
+ require "MYSQLSafe/base.rb"
2
3
 
3
4
  module MYSQLSafe
4
- # Your code goes here...
5
+
5
6
  end
@@ -0,0 +1,122 @@
1
+ require 'mysql'
2
+
3
+ module MYSQLSafe
4
+ class Base
5
+ attr_accessor :encoding
6
+ attr_reader :host, :database, :user, :password
7
+
8
+ def host=(host_string)
9
+ @host = esc_enc_string(host_string)
10
+ end
11
+ def database=(database_string)
12
+ @database = esc_enc_string(database_string)
13
+ end
14
+ def user=(user_string)
15
+ @user = esc_enc_string(user_string)
16
+ end
17
+ def password=(password_string)
18
+ @password = esc_enc_string(password_string)
19
+ end
20
+
21
+ def connect_safe(raw_sql)
22
+ sql = esc_enc_string(raw_sql)
23
+ if @host && @database && @user && password
24
+ begin
25
+ @cxtn = Mysql.new(@host, @db, @user, @password)
26
+ table_names = get_table_names
27
+ table_match = match_name(table_names, sql)
28
+
29
+ if table_match
30
+ column_names = get_column_names(match)
31
+ column_match = match_name(column_names, sql)
32
+ else
33
+ raise 'MYSQLSafe error: no valid table name could be found in your SQL statement'
34
+ end
35
+
36
+ if column_match
37
+ ticked_sql = tick_sql(sql, table_match, column_match)
38
+ else
39
+ raise 'MYSQLSafe error: no valid column name(s) could be found in your SQL statement'
40
+ end
41
+
42
+ mysql_object = cxtn.query(ticked_sql)
43
+ mysql_array = []
44
+ mysql_object.each { |row| mysql_array.push(row) }
45
+
46
+ return mysql_array
47
+ ensure
48
+ @cxtn.close
49
+ end
50
+ else
51
+ raise 'MYSQLSafe error: Host, Database, User and Password must be set to run a query'
52
+ end
53
+ end
54
+
55
+ private
56
+ def tick_sql(sql, table_array, column_array)
57
+ ticked_sql = sql.delete("`")
58
+ table_array.each do |name|
59
+ ticked_sql = ticked_sql.gsub(name, "`#{name}`")
60
+ end
61
+ column_array.each do |col|
62
+ ticked_sql = ticked_sql.gsub(col, "`#{col}`")
63
+ end
64
+
65
+ return ticked_sql
66
+ end
67
+
68
+ def get_column_names(table_name)
69
+ column_names_sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='#{@database}' AND `TABLE_NAME`='#{table_name}';"
70
+ column_names_results_sql = query_safe(column_names_sql)
71
+
72
+ column_names = []
73
+ column_names_results_sql.each do |name|
74
+ column_names.push(name)
75
+ end
76
+
77
+ return column_names
78
+ end
79
+
80
+ def match_name(name_array, sql)
81
+ match = []
82
+
83
+ name_array.each do |name|
84
+ match.push(name) if sql.to_s.include?("#{name}=") || sql.to_s.match?(/#{name}\s+=/) || sql.to_s.match?(/#{name}`\s+=/)
85
+ end
86
+
87
+ if match.size > 0
88
+ return match
89
+ else
90
+ return false
91
+ end
92
+ end
93
+
94
+ def query_safe(dangerous_sql)
95
+ @cxtn.query(Mysql.escape_string(dangerous_sql))
96
+ end
97
+
98
+ def get_table_names
99
+ table_names_sql = "SHOW TABLES FROM `#{@database}`;"
100
+ table_names_results_sql = query_safe(table_names_sql)
101
+
102
+ table_names = []
103
+ table_names_results_sql.each do |name|
104
+ table_names.push(name)
105
+ end
106
+
107
+ return table_names
108
+ end
109
+
110
+ def esc_enc_string(string)
111
+ return esc_string(enc_string(string))
112
+ end
113
+
114
+ def enc_string(string)
115
+ return string.encode!("#{@encoding}", "#{@encoding}", :invalid => :replace)
116
+ end
117
+
118
+ def esc_string(string)
119
+ return Mysql.escape_string(string)
120
+ end
121
+ end
122
+ end
@@ -1,3 +1,3 @@
1
1
  module MYSQLSafe
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
File without changes
@@ -0,0 +1,9 @@
1
+ require_relative '../../test_helper.rb'
2
+
3
+ describe MYSQLSafe do
4
+
5
+ it "must be defined" do
6
+ MYSQLSafe::VERSION.wont_be_nil
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/MYSQLSafe.rb', __FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: MYSQLSafe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -73,7 +73,11 @@ files:
73
73
  - README.md
74
74
  - Rakefile
75
75
  - lib/MYSQLSafe.rb
76
+ - lib/MYSQLSafe/base.rb
76
77
  - lib/MYSQLSafe/version.rb
78
+ - test/lib/MYSQLSafe/base_test.rb
79
+ - test/lib/MYSQLSafe/version_test.rb
80
+ - test/test_helper.rb
77
81
  homepage: ''
78
82
  licenses:
79
83
  - MIT
@@ -100,4 +104,7 @@ signing_key:
100
104
  specification_version: 3
101
105
  summary: An abstraction of the MYSQL gem to automatically close connections, return
102
106
  arrays and sanatize some of the inputs
103
- test_files: []
107
+ test_files:
108
+ - test/lib/MYSQLSafe/base_test.rb
109
+ - test/lib/MYSQLSafe/version_test.rb
110
+ - test/test_helper.rb