fluiddb 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +7 -0
  2. data/lib/FluidDb.rb +57 -0
  3. data/lib/FluidDb/Mysql.rb +101 -0
  4. metadata +48 -0
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ #Fluid DB
2
+
3
+ A semantic layer for db interaction
4
+
5
+ ##install
6
+ gem install fluiddb
7
+
data/lib/FluidDb.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'date'
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
13
+
14
+ @connection;
15
+
16
+ def format_to_sql( sql, params=nil )
17
+ if params.nil? then
18
+ return sql
19
+ end
20
+
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
36
+ end
37
+
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
+ end
56
+
57
+ end
@@ -0,0 +1,101 @@
1
+ require "FluidDb"
2
+ require "mysql2"
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
14
+
15
+ def queryForArray( sql, params )
16
+ sql = self.format_to_sql( sql, params )
17
+ results = @connection.query(sql)
18
+
19
+ # if ( $result === false ) then
20
+ # $message = pg_last_error( $this->connection );
21
+ # throw new Fluid_ConnectionException( $message );
22
+ #end
23
+
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
34
+ end
35
+ return r
36
+ else
37
+ raise FluidDb_TooManyRowsError.new
38
+ end
39
+
40
+ end
41
+
42
+ def queryForValue( sql, params )
43
+ sql = self.format_to_sql( sql, params )
44
+ results = @connection.query(sql, :as => :array)
45
+
46
+ # if ( $result === false ) then
47
+ # $message = pg_last_error( $this->connection );
48
+ # throw new Fluid_ConnectionException( $message );
49
+ #end
50
+
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
61
+ end
62
+ return r[0]
63
+ else
64
+ raise FluidDb_TooManyRowsError.new
65
+ end
66
+
67
+ end
68
+
69
+
70
+ def queryForResultset( sql, params )
71
+ sql = self.format_to_sql( sql, params )
72
+ results = @connection.query(sql)
73
+
74
+ # if ( $result === false ) then
75
+ # $message = pg_last_error( $this->connection );
76
+ # throw new Fluid_ConnectionException( $message );
77
+ #end
78
+
79
+ results.count == 0
80
+ case results.count
81
+ when -1
82
+ raise FluidDb_ConnectionError.new
83
+ else
84
+ return results
85
+ end
86
+ end
87
+
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
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluiddb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guy Irvine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A semantic layer for db interaction
15
+ email: guy@guyirvine.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/FluidDb/Mysql.rb
21
+ - lib/FluidDb.rb
22
+ - README.md
23
+ homepage: http://rubygems.org/gems/fluiddb
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.11
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: FluidDB
47
+ test_files: []
48
+ has_rdoc: