MYSQLSafe 0.0.5 → 0.0.6
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/base.rb +25 -25
- data/lib/MYSQLSafe/version.rb +1 -1
- data/test/lib/MYSQLSafe/base_test.rb +31 -18
- metadata +1 -1
data/lib/MYSQLSafe/base.rb
CHANGED
@@ -2,27 +2,23 @@ require 'mysql'
|
|
2
2
|
|
3
3
|
module MYSQLSafe
|
4
4
|
class Base
|
5
|
-
attr_accessor :encoding
|
6
|
-
attr_reader :host, :database, :user
|
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
|
5
|
+
attr_accessor :host, :database, :user, :encoding, :password
|
20
6
|
|
21
7
|
def connect_safe(raw_sql)
|
8
|
+
@mysql_array = []
|
9
|
+
@encoding ||= 'utf-8'
|
10
|
+
options = {}
|
11
|
+
self.instance_variables.map{|name| options = options.merge({ name.to_s.delete("@") => self.instance_variable_get(name) }) }
|
12
|
+
options.each do |k,v|
|
13
|
+
options[k] = esc_enc_string(v)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
22
17
|
sql = esc_enc_string(raw_sql)
|
23
|
-
if
|
18
|
+
if options["host"] && options["database"] && options["user"] && options["password"]
|
24
19
|
begin
|
25
|
-
|
20
|
+
options.each {|k,v| puts "#{k} => #{v}"}
|
21
|
+
@cxtn = Mysql.new(options[host], options[database], options[user], options[password])
|
26
22
|
table_names = get_table_names
|
27
23
|
table_match = match_name(table_names, sql)
|
28
24
|
|
@@ -40,16 +36,19 @@ module MYSQLSafe
|
|
40
36
|
end
|
41
37
|
|
42
38
|
mysql_object = cxtn.query(ticked_sql)
|
43
|
-
mysql_array
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
mysql_object.each { |row| @mysql_array.push(row) }
|
40
|
+
puts "After push: #{@mysql_array}"
|
41
|
+
rescue Mysql::Error => msqle
|
42
|
+
puts "Error! #{msqle}, #{@mysql_array}"
|
43
|
+
@mysql_array.push(["MYSQL Error: #{msqle}"])
|
47
44
|
ensure
|
48
|
-
@cxtn.close
|
45
|
+
@cxtn.close if @cxtn
|
49
46
|
end
|
50
47
|
else
|
51
|
-
raise
|
48
|
+
raise "MYSQLSafe error: Host, Database, User and Password must be set to run a query. You included #{options}"
|
52
49
|
end
|
50
|
+
puts "@mysql_array is #{@mysql_array} a #{@mysql_array.class}"
|
51
|
+
return @mysql_array
|
53
52
|
end
|
54
53
|
|
55
54
|
private
|
@@ -108,15 +107,16 @@ module MYSQLSafe
|
|
108
107
|
end
|
109
108
|
|
110
109
|
def esc_enc_string(string)
|
111
|
-
return esc_string(enc_string(string))
|
110
|
+
return esc_string(enc_string(string.to_s))
|
112
111
|
end
|
113
112
|
|
114
113
|
def enc_string(string)
|
115
|
-
return string.encode
|
114
|
+
return string.encode("#{@encoding}", "#{@encoding}", :invalid => :replace)
|
116
115
|
end
|
117
116
|
|
118
117
|
def esc_string(string)
|
119
118
|
return Mysql.escape_string(string)
|
120
119
|
end
|
120
|
+
|
121
121
|
end
|
122
122
|
end
|
data/lib/MYSQLSafe/version.rb
CHANGED
@@ -2,37 +2,50 @@ require_relative '../../test_helper.rb'
|
|
2
2
|
|
3
3
|
describe MYSQLSafe::Base do
|
4
4
|
|
5
|
-
|
5
|
+
before do
|
6
|
+
@obj = MYSQLSafe::Base.new
|
7
|
+
end
|
6
8
|
|
7
9
|
it "should allow enconding to be set and read" do
|
8
|
-
|
9
|
-
obj.
|
10
|
-
obj.
|
10
|
+
encoding_name = 'utf-8'
|
11
|
+
@obj.encoding = encoding_name
|
12
|
+
@obj.encoding.must_equal encoding_name
|
11
13
|
end
|
12
14
|
it "should allow username to be set and read" do
|
13
15
|
username = 'sam'
|
14
|
-
obj.user = username
|
15
|
-
obj.user.
|
16
|
+
@obj.user = username
|
17
|
+
@obj.user.must_equal username
|
16
18
|
end
|
17
19
|
it "should allow host to be set and read" do
|
18
20
|
hostname = 'localhost'
|
19
|
-
obj.host = hostname
|
20
|
-
obj.host.
|
21
|
+
@obj.host = hostname
|
22
|
+
@obj.host.must_equal hostname
|
21
23
|
end
|
22
24
|
it "should allow database to be set and read" do
|
23
25
|
database_name = 'test'
|
24
|
-
obj.database = database_name
|
25
|
-
obj.database.
|
26
|
+
@obj.database = database_name
|
27
|
+
@obj.database.must_equal database_name
|
26
28
|
end
|
27
29
|
|
28
|
-
it "must allow password to be set
|
29
|
-
password_key =
|
30
|
-
obj.password = password_key
|
31
|
-
obj.password.
|
30
|
+
it "must allow password to be set and read" do
|
31
|
+
password_key = ENV['MYSQLPASSWORD'] || "password"
|
32
|
+
@obj.password = password_key
|
33
|
+
@obj.password.must_equal password_key
|
32
34
|
end
|
33
35
|
|
34
|
-
it "should allow connections to be made" do
|
35
|
-
|
36
|
-
obj.
|
36
|
+
it "should allow connections to be made, and return an array" do
|
37
|
+
encoding_name = 'utf-8'
|
38
|
+
@obj.encoding = encoding_name
|
39
|
+
username = 'sam'
|
40
|
+
@obj.user = username
|
41
|
+
hostname = 'localhost'
|
42
|
+
@obj.host = hostname
|
43
|
+
database_name = 'test'
|
44
|
+
@obj.database = database_name
|
45
|
+
password_key = ENV['MYSQLPASSWORD'] || "password"
|
46
|
+
@obj.password = password_key
|
47
|
+
|
48
|
+
success = @obj.connect_safe("SELECT * FROM test LIMIT 1")
|
49
|
+
success.must_be_instance_of Array
|
37
50
|
end
|
38
|
-
end
|
51
|
+
end
|