MYSQLSafe 0.1.3 → 0.1.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/base.rb +40 -29
- data/lib/MYSQLSafe/version.rb +1 -1
- data/test/lib/MYSQLSafe/base_test.rb +9 -2
- data/test/test_helper.rb +2 -1
- metadata +2 -2
data/lib/MYSQLSafe/base.rb
CHANGED
@@ -15,39 +15,50 @@ module MYSQLSafe
|
|
15
15
|
|
16
16
|
|
17
17
|
sql = esc_enc_string(raw_sql)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
column_match = match_name(column_names, sql)
|
27
|
-
column_match = [] if !(sql.to_s.downcase.include?('where'))
|
18
|
+
begin
|
19
|
+
case
|
20
|
+
when options["host"], options["user"], options["password"], options["database"]
|
21
|
+
@cxtn = Mysql.new(options["host"], options["user"], options["password"], options["database"])
|
22
|
+
when options["host"], options["user"], options["password"]
|
23
|
+
@cxtn = Mysql.new(options["host"], options["user"], options["password"])
|
24
|
+
when options["host"], options["user"]
|
25
|
+
@cxtn = Mysql.new(options["host"], options["user"])
|
28
26
|
else
|
29
|
-
raise
|
30
|
-
end
|
31
|
-
|
32
|
-
if column_match
|
33
|
-
ticked_sql = tick_sql(sql, table_match, column_match)
|
34
|
-
else
|
35
|
-
raise 'MYSQLSafe error: no valid column name(s) could be found in your SQL statement'
|
36
|
-
end
|
37
|
-
|
38
|
-
mysql_object = @cxtn.query(ticked_sql)
|
39
|
-
mysql_object.each { |row| @mysql_array.push(row) }
|
40
|
-
rescue Mysql::Error => msqle
|
41
|
-
puts "Error! #{msqle}, #{@mysql_array}"
|
42
|
-
@mysql_array.push(["MYSQL Error: #{msqle}"])
|
43
|
-
ensure
|
44
|
-
@cxtn.close if @cxtn
|
27
|
+
raise "MYSQLSafe error: In order to connect to MYSQL you must at least set the host and username. So far you have included #{options}."
|
45
28
|
end
|
46
|
-
|
47
|
-
|
29
|
+
|
30
|
+
table_names = get_table_names
|
31
|
+
table_match = match_name(table_names, sql)
|
32
|
+
|
33
|
+
if table_match
|
34
|
+
column_names = get_column_names(table_match)
|
35
|
+
column_match = match_name(column_names, sql)
|
36
|
+
column_match = [] if !(sql.to_s.downcase.include?('where'))
|
37
|
+
else
|
38
|
+
raise 'MYSQLSafe error: no valid table name could be found in your SQL statement'
|
39
|
+
end
|
40
|
+
|
41
|
+
if column_match
|
42
|
+
ticked_sql = tick_sql(sql, table_match, column_match)
|
43
|
+
else
|
44
|
+
raise 'MYSQLSafe error: no valid column name(s) could be found in your SQL statement'
|
45
|
+
end
|
46
|
+
|
47
|
+
mysql_object = @cxtn.query(ticked_sql)
|
48
|
+
mysql_object.each { |row| @mysql_array.push(row) } if mysql_object
|
49
|
+
unless @mysql_array.size > 0
|
50
|
+
@mysql_array = ["Success, with 'nil' result"]
|
51
|
+
end
|
52
|
+
rescue Mysql::Error => msqle
|
53
|
+
puts "Error! #{msqle}, #{@mysql_array}"
|
54
|
+
@mysql_array.push(["MYSQL Error: #{msqle}"])
|
55
|
+
ensure
|
56
|
+
@cxtn.close if @cxtn
|
48
57
|
end
|
58
|
+
|
49
59
|
return @mysql_array
|
50
|
-
|
60
|
+
|
61
|
+
end
|
51
62
|
|
52
63
|
private
|
53
64
|
def tick_sql(sql, table_array, column_array)
|
data/lib/MYSQLSafe/version.rb
CHANGED
@@ -8,7 +8,7 @@ describe MYSQLSafe::Base do
|
|
8
8
|
@obj = MYSQLSafe::Base.new
|
9
9
|
end
|
10
10
|
|
11
|
-
it "should allow
|
11
|
+
it "should allow encoding to be set and read" do
|
12
12
|
encoding_name = 'utf-8'
|
13
13
|
@obj.encoding = encoding_name
|
14
14
|
@obj.encoding.must_equal encoding_name
|
@@ -24,7 +24,7 @@ describe MYSQLSafe::Base do
|
|
24
24
|
@obj.host.must_equal hostname
|
25
25
|
end
|
26
26
|
it "should allow database to be set and read" do
|
27
|
-
database_name = '
|
27
|
+
database_name = 'test_mysql'
|
28
28
|
@obj.database = database_name
|
29
29
|
@obj.database.must_equal database_name
|
30
30
|
end
|
@@ -62,4 +62,11 @@ describe MYSQLSafe::Base do
|
|
62
62
|
success.to_s.wont_include "MYSQL Error"
|
63
63
|
success.must_be_instance_of Array
|
64
64
|
end
|
65
|
+
|
66
|
+
it "should allow insert into syntax" do
|
67
|
+
@obj = help.set_variables(@obj)
|
68
|
+
success = @obj.connect_safe("INSERT INTO `performance_test` SELECT * FROM `performance_test` WHERE `test_int` = 1 LIMIT 1")
|
69
|
+
success.to_s.wont_include "MYSQL Error"
|
70
|
+
success.must_be_instance_of Array
|
71
|
+
end
|
65
72
|
end
|
data/test/test_helper.rb
CHANGED
@@ -4,6 +4,7 @@ require File.expand_path('../../lib/MYSQLSafe.rb', __FILE__)
|
|
4
4
|
|
5
5
|
class TestHelpers
|
6
6
|
attr_accessor :encoding_name, :hostname_name, :user_name, :database_name, :password_name
|
7
|
+
|
7
8
|
def set_encoding(obj)
|
8
9
|
@encoding_name = 'utf-8'
|
9
10
|
obj.encoding = @encoding_name
|
@@ -20,7 +21,7 @@ class TestHelpers
|
|
20
21
|
return obj
|
21
22
|
end
|
22
23
|
def set_database(obj)
|
23
|
-
@database_name = '
|
24
|
+
@database_name = 'test_mysql'
|
24
25
|
obj.database = @database_name
|
25
26
|
return obj
|
26
27
|
end
|
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.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|