fluiddb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/FluidDb.rb +49 -45
  2. data/lib/FluidDb/Mysql.rb +84 -81
  3. metadata +1 -1
@@ -1,57 +1,61 @@
1
1
  require 'date'
2
2
 
3
- class FluidDb_ConnectionError<StandardError
4
- end
5
- class FluidDb_NoDataFoundError<StandardError
6
- end
7
- class FluidDb_TooManyRowsError<StandardError
8
- end
9
- class FluidDb_ParamTypeNotSupportedError<StandardError
10
- end
11
-
12
- class FluidDb
3
+ module FluidDb
13
4
 
14
- @connection;
5
+ class ConnectionError<StandardError
6
+ end
7
+ class NoDataFoundError<StandardError
8
+ end
9
+ class TooManyRowsError<StandardError
10
+ end
11
+ class ParamTypeNotSupportedError<StandardError
12
+ end
15
13
 
16
- def format_to_sql( sql, params=nil )
17
- if params.nil? then
14
+ class Base
15
+
16
+ @connection;
17
+
18
+ def format_to_sql( sql, params=nil )
19
+ if params.nil? then
20
+ return sql
21
+ end
22
+ #timestamp.strftime( "%Y-%m-%d %H:%M:%S" )
23
+ params.each do |v|
24
+ if v.kind_of? String then
25
+ v = "'" + v.sub( "'", "\'" ) + "'"
26
+ sql = sql.sub( "?", v )
27
+ elsif v.is_a? DateTime then
28
+ s = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
29
+ sql = sql.sub( "?", s )
30
+ elsif v.kind_of? Date then
31
+ v = "'" + v.to_s + "'"
32
+ sql = sql.sub( "?", v.to_s )
33
+ elsif v.is_a?(Numeric) then
34
+ sql = sql.sub( "?", v.to_s )
35
+ else
36
+ raise FluidDb_ParamTypeNotSupportedError.new
37
+ end
38
+ end
39
+
18
40
  return sql
19
41
  end
20
42
 
21
- params.each do |v|
22
- if v.kind_of? String then
23
- v = "'" + v.sub( "'", "\'" ) + "'"
24
- sql = sql.sub( "?", v )
25
- elsif v.kind_of? Date then
26
- v = "'" + v.to_s + "'"
27
- sql = sql.sub( "?", v.to_s )
28
- elsif v.kind_of? DateTime then
29
- v = "'" + v.to_s + "'"
30
- sql = sql.sub( "?", v.to_s )
31
- elsif v.is_a?(Numeric) then
32
- sql = sql.sub( "?", v.to_s )
33
- else
34
- raise FluidDb_ParamTypeNotSupportedError.new
35
- end
43
+ def queryForArray( sql, params )
44
+ raise NotImplementedError.new("You must implement 'queryForArray'.")
45
+ end
46
+
47
+ def queryForValue( sql, params )
48
+ raise NotImplementedError.new("You must implement 'queryForValue'.")
49
+ end
50
+
51
+ def queryForResultset( sql, params )
52
+ raise NotImplementedError.new("You must implement 'queryForResultset'.")
53
+ end
54
+
55
+ def execute( sql, params, expected_affected_rows )
56
+ raise NotImplementedError.new("You must implement 'execute'.")
36
57
  end
37
58
 
38
- return sql
39
- end
40
-
41
- def queryForArray( sql, params )
42
- raise NotImplementedError.new("You must implement 'queryForArray'.")
43
- end
44
-
45
- def queryForValue( sql, params )
46
- raise NotImplementedError.new("You must implement 'queryForValue'.")
47
- end
48
-
49
- def queryForResultset( sql, params )
50
- raise NotImplementedError.new("You must implement 'queryForResultset'.")
51
- end
52
-
53
- def execute( sql, params, expected_affected_rows )
54
- raise NotImplementedError.new("You must implement 'execute'.")
55
59
  end
56
60
 
57
61
  end
@@ -1,101 +1,104 @@
1
1
  require "FluidDb"
2
2
  require "mysql2"
3
3
 
4
- class FluidDb_Mysql<FluidDb
5
-
6
- def initialize(uri)
7
- host = uri.host
8
- database = uri.path.sub( "/", "" )
9
-
10
- @connection = Mysql2::Client.new(:host => uri.host,
11
- :database => uri.path.sub( "/", "" ),
12
- :username => uri.user )
13
- end
4
+ module FluidDb
14
5
 
15
- def queryForArray( sql, params )
16
- sql = self.format_to_sql( sql, params )
17
- results = @connection.query(sql)
6
+ class Mysql<Base
18
7
 
19
- # if ( $result === false ) then
20
- # $message = pg_last_error( $this->connection );
21
- # throw new Fluid_ConnectionException( $message );
22
- #end
8
+ def initialize(uri)
9
+ host = uri.host
10
+ database = uri.path.sub( "/", "" )
11
+
12
+ @connection = Mysql2::Client.new(:host => uri.host,
13
+ :database => uri.path.sub( "/", "" ),
14
+ :username => uri.user )
15
+ end
23
16
 
24
- results.count == 0
25
- case results.count
26
- when -1
27
- raise FluidDb_ConnectionError.new
28
- when 0
29
- raise FluidDb_NoDataFoundError.new
30
- when 1
31
- r=nil;
32
- results.each do |row|
33
- r=row
17
+ def queryForArray( sql, params )
18
+ sql = self.format_to_sql( sql, params )
19
+ results = @connection.query(sql)
20
+
21
+ # if ( $result === false ) then
22
+ # $message = pg_last_error( $this->connection );
23
+ # throw new Fluid_ConnectionException( $message );
24
+ #end
25
+
26
+ results.count == 0
27
+ case results.count
28
+ when -1
29
+ raise FluidDb::ConnectionError.new
30
+ when 0
31
+ raise FluidDb::NoDataFoundError.new
32
+ when 1
33
+ r=nil;
34
+ results.each do |row|
35
+ r=row
36
+ end
37
+ return r
38
+ else
39
+ raise FluidDb::TooManyRowsError.new
34
40
  end
35
- return r
36
- else
37
- raise FluidDb_TooManyRowsError.new
41
+
38
42
  end
39
43
 
40
- end
41
-
42
- def queryForValue( sql, params )
43
- sql = self.format_to_sql( sql, params )
44
- results = @connection.query(sql, :as => :array)
44
+ def queryForValue( sql, params )
45
+ sql = self.format_to_sql( sql, params )
46
+ results = @connection.query(sql, :as => :array)
47
+
48
+ # if ( $result === false ) then
49
+ # $message = pg_last_error( $this->connection );
50
+ # throw new Fluid_ConnectionException( $message );
51
+ #end
52
+
53
+ results.count == 0
54
+ case results.count
55
+ when -1
56
+ raise FluidDb::ConnectionError.new
57
+ when 0
58
+ raise FluidDb::NoDataFoundError.new
59
+ when 1
60
+ r=nil;
61
+ results.each do |row|
62
+ r=row
63
+ end
64
+ return r[0]
65
+ else
66
+ raise FluidDb::TooManyRowsError.new
67
+ end
68
+
69
+ end
45
70
 
46
- # if ( $result === false ) then
47
- # $message = pg_last_error( $this->connection );
48
- # throw new Fluid_ConnectionException( $message );
49
- #end
50
71
 
51
- results.count == 0
52
- case results.count
53
- when -1
54
- raise FluidDb_ConnectionError.new
55
- when 0
56
- raise FluidDb_NoDataFoundError.new
57
- when 1
58
- r=nil;
59
- results.each do |row|
60
- r=row
72
+ def queryForResultset( sql, params )
73
+ sql = self.format_to_sql( sql, params )
74
+ results = @connection.query(sql)
75
+
76
+ # if ( $result === false ) then
77
+ # $message = pg_last_error( $this->connection );
78
+ # throw new Fluid_ConnectionException( $message );
79
+ #end
80
+
81
+ results.count == 0
82
+ case results.count
83
+ when -1
84
+ raise FluidDb::ConnectionError.new
85
+ else
86
+ return results
61
87
  end
62
- return r[0]
63
- else
64
- raise FluidDb_TooManyRowsError.new
65
88
  end
66
89
 
67
- end
68
-
69
-
70
- def queryForResultset( sql, params )
71
- sql = self.format_to_sql( sql, params )
72
- results = @connection.query(sql)
73
90
 
74
- # if ( $result === false ) then
75
- # $message = pg_last_error( $this->connection );
76
- # throw new Fluid_ConnectionException( $message );
77
- #end
91
+ # def execute( sql, params, expected_affected_rows )
92
+ def execute( sql, params )
93
+ sql = self.format_to_sql( sql, params )
94
+ @connection.query( sql );
95
+ end
78
96
 
79
- results.count == 0
80
- case results.count
81
- when -1
82
- raise FluidDb_ConnectionError.new
83
- else
84
- return results
97
+ def insert( sql, params )
98
+ self.execute( sql, params )
99
+ return @connection.last_id
85
100
  end
101
+
86
102
  end
87
103
 
88
-
89
- # def execute( sql, params, expected_affected_rows )
90
- def execute( sql, params )
91
- sql = self.format_to_sql( sql, params )
92
- @connection.query( sql );
93
- end
94
-
95
- def insert( sql, params )
96
- self.execute( sql, params )
97
- return @connection.last_id
98
- end
99
-
100
-
101
104
  end
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: