fluiddb 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/FluidDb.rb +43 -23
  2. metadata +2 -2
@@ -16,58 +16,78 @@ module FluidDb
16
16
  end
17
17
  class ExpectedAffectedRowsError<StandardError
18
18
  end
19
+ class IncorrectNumberOfParametersError<StandardError
20
+ end
19
21
 
20
22
  class Base
21
-
23
+
22
24
  attr_writer :verbose
23
25
 
24
26
  @connection;
25
27
  @uri
26
28
  @verbose
27
-
29
+
28
30
  # Constructor.
29
31
  #
30
32
  # @param [String] uri a location for the resource to which we will attach, eg mysql://user:pass@127.0.0.1/foo
31
33
  def initialize(uri)
32
34
  if uri.kind_of? String then
33
35
  @uri = Uri.parse( uri )
34
- else
36
+ else
35
37
  @uri = uri
36
38
  end
37
-
39
+
38
40
  self.connect
39
41
  end
40
-
42
+
43
+ def splice_sql( sql, params )
44
+
45
+ if params.length != sql.count( "?" ) then
46
+ raise IncorrectNumberOfParametersError.new
47
+ end
48
+
49
+ sql_out = ""
50
+ sql.split( "?" ).each_with_index do |s,idx|
51
+ sql_out = sql_out + s
52
+ sql_out = sql_out + params[idx] unless params[idx].nil?
53
+ end
54
+
55
+ return sql_out
56
+ end
57
+
41
58
  def format_to_sql( sql, params=nil )
42
- if params.nil? then
59
+ if params.nil? || params.count == 0 then
43
60
  return sql
44
61
  end
45
- #timestamp.strftime( "%Y-%m-%d %H:%M:%S" )
46
- params.each do |v|
62
+
63
+ params.each_with_index do |v, idx|
47
64
  if v.kind_of? String then
48
- v = "'" + v.sub( "'", "\'" ) + "'"
49
- sql = sql.sub( "?", v )
65
+ v = "'" + v.split( "'" ).join( "''" ) + "'"
66
+ # v = "'" + v.sub( "'", "\'" ) + "'"
50
67
  elsif v.is_a? DateTime then
51
- s = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
52
- sql = sql.sub( "?", s )
68
+ v = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
53
69
  elsif v.is_a? Time then
54
- s = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
55
- sql = sql.sub( "?", s )
70
+ v = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
56
71
  elsif v.kind_of? Date then
57
72
  v = "'" + v.to_s + "'"
58
- sql = sql.sub( "?", v.to_s )
59
73
  elsif v.is_a?(Numeric) then
60
- sql = sql.sub( "?", v.to_s )
74
+ v = v.to_s
61
75
  elsif v.nil? then
62
- sql = sql.sub( "?", 'NULL' )
76
+ v = 'NULL'
63
77
  else
64
78
  raise ParamTypeNotSupportedError.new( "Name of unknown param type, #{v.class.name}, for sql, #{sql}" )
65
79
  end
80
+ params[idx] = v
66
81
  end
67
-
68
- puts sql if @verbose == true
69
-
70
- return sql
82
+
83
+ sql_out = self.splice_sql( sql, params )
84
+ if @verbose == true then
85
+ puts sql
86
+ puts params
87
+ puts sql_out
88
+ end
89
+
90
+ return sql_out
71
91
  end
72
92
 
73
93
  def convertTupleToHash( fields, tuple, j )
@@ -86,12 +106,12 @@ module FluidDb
86
106
  def close
87
107
  raise NotImplementedError.new("You must implement 'close'.")
88
108
  end
89
-
109
+
90
110
  def reconnect
91
111
  self.close
92
112
  self.connect
93
113
  end
94
-
114
+
95
115
  # Return a single row from the database, given the sql parameter.
96
116
  # Throwa an error for no data.
97
117
  # Throws an error for more than 1 row
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluiddb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
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: 2012-11-19 00:00:00.000000000 Z
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A semantic layer for db interaction
15
15
  email: guy@guyirvine.com