fluiddb 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/FluidDb.rb +49 -45
- data/lib/FluidDb/Mysql.rb +84 -81
- metadata +1 -1
data/lib/FluidDb.rb
CHANGED
@@ -1,57 +1,61 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
-
|
17
|
-
|
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
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
data/lib/FluidDb/Mysql.rb
CHANGED
@@ -1,101 +1,104 @@
|
|
1
1
|
require "FluidDb"
|
2
2
|
require "mysql2"
|
3
3
|
|
4
|
-
|
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
|
-
|
16
|
-
sql = self.format_to_sql( sql, params )
|
17
|
-
results = @connection.query(sql)
|
6
|
+
class Mysql<Base
|
18
7
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
36
|
-
else
|
37
|
-
raise FluidDb_TooManyRowsError.new
|
41
|
+
|
38
42
|
end
|
39
43
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
#
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
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
|