MYSQLSafe 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,39 +15,50 @@ module MYSQLSafe
15
15
 
16
16
 
17
17
  sql = esc_enc_string(raw_sql)
18
- if options["host"] && options["database"] && options["user"] && options["password"]
19
- begin
20
- @cxtn = Mysql.new(options["host"], options["user"], options["password"], options["database"])
21
- table_names = get_table_names
22
- table_match = match_name(table_names, sql)
23
-
24
- if table_match
25
- column_names = get_column_names(table_match)
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 'MYSQLSafe error: no valid table name could be found in your SQL statement'
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
- else
47
- raise "MYSQLSafe error: Host, Database, User and Password must be set to run a query. You included #{options}"
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
- end
60
+
61
+ end
51
62
 
52
63
  private
53
64
  def tick_sql(sql, table_array, column_array)
@@ -1,3 +1,3 @@
1
1
  module MYSQLSafe
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -8,7 +8,7 @@ describe MYSQLSafe::Base do
8
8
  @obj = MYSQLSafe::Base.new
9
9
  end
10
10
 
11
- it "should allow enconding to be set and read" do
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 = 'test'
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 = 'test'
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.3
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-13 00:00:00.000000000 Z
12
+ date: 2013-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler