fluiddb 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/FluidDb.rb CHANGED
@@ -11,6 +11,8 @@ module FluidDb
11
11
  end
12
12
  class ParamTypeNotSupportedError<StandardError
13
13
  end
14
+ class ExpectedAffectedRowsError<StandardError
15
+ end
14
16
 
15
17
  class Base
16
18
 
@@ -40,6 +42,15 @@ module FluidDb
40
42
 
41
43
  return sql
42
44
  end
45
+
46
+ def convertTupleToHash( fields, tuple, j )
47
+ hash = Hash.new
48
+ 0.upto( fields.length-1 ).each do |i|
49
+ hash[fields[i].to_s] = tuple.getvalue(j, i)
50
+ end
51
+
52
+ return hash
53
+ end
43
54
 
44
55
  def queryForArray( sql, params )
45
56
  raise NotImplementedError.new("You must implement 'queryForArray'.")
data/lib/FluidDb/Mysql.rb CHANGED
@@ -1,17 +1,14 @@
1
1
  require "FluidDb"
2
- require "mysql2"
2
+ require "mysql"
3
3
 
4
4
  module FluidDb
5
-
5
+
6
6
  class Mysql<Base
7
7
 
8
8
  def initialize(uri)
9
- host = uri.host
10
9
  database = uri.path.sub( "/", "" )
11
10
 
12
- @connection = Mysql2::Client.new(:host => uri.host,
13
- :database => uri.path.sub( "/", "" ),
14
- :username => uri.user )
11
+ @connection = ::Mysql.new uri.host, uri.user, uri.password, database, nil, nil, ::Mysql::CLIENT_FOUND_ROWS
15
12
  end
16
13
 
17
14
  def queryForArray( sql, params )
@@ -23,33 +20,30 @@ module FluidDb
23
20
  # throw new Fluid_ConnectionException( $message );
24
21
  #end
25
22
 
26
- case results.count
23
+ case results.num_rows
27
24
  when -1
28
25
  raise FluidDb::ConnectionError.new
29
26
  when 0
30
27
  raise FluidDb::NoDataFoundError.new
31
28
  when 1
32
- r=nil;
33
- results.each do |row|
34
- r=row
35
- end
29
+ r = results.fetch_hash
36
30
  return r
37
31
  else
38
32
  raise FluidDb::TooManyRowsError.new
39
33
  end
40
34
 
41
35
  end
42
-
36
+
43
37
  def queryForValue( sql, params )
44
38
  sql = self.format_to_sql( sql, params )
45
- results = @connection.query(sql, :as => :array)
46
-
39
+ results = @connection.query(sql)
40
+
47
41
  # if ( $result === false ) then
48
42
  # $message = pg_last_error( $this->connection );
49
43
  # throw new Fluid_ConnectionException( $message );
50
44
  #end
51
45
 
52
- case results.count
46
+ case results.num_rows
53
47
  when -1
54
48
  raise FluidDb::ConnectionError.new
55
49
  when 0
@@ -76,24 +70,33 @@ module FluidDb
76
70
  # throw new Fluid_ConnectionException( $message );
77
71
  #end
78
72
 
79
- case results.count
73
+ case results.num_rows
80
74
  when -1
81
75
  raise FluidDb::ConnectionError.new
82
76
  else
83
- return results
77
+ list = Array.new
78
+ results.each_hash do |row|
79
+ list.push row
80
+ end
81
+
82
+ return list
84
83
  end
85
84
  end
86
-
87
-
88
- # def execute( sql, params, expected_affected_rows )
89
- def execute( sql, params )
85
+
86
+ def execute( sql, params, expected_affected_rows=nil )
90
87
  sql = self.format_to_sql( sql, params )
88
+ # puts "sql: #{sql}"
91
89
  @connection.query( sql );
90
+
91
+ if !expected_affected_rows.nil? and
92
+ @connection.affected_rows != expected_affected_rows then
93
+ raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.affected_rows}")
94
+ end
92
95
  end
93
-
96
+
94
97
  def insert( sql, params )
95
98
  self.execute( sql, params )
96
- return @connection.last_id
99
+ return @connection.insert_id
97
100
  end
98
101
 
99
102
  end
@@ -0,0 +1,112 @@
1
+ require "FluidDb"
2
+ require "mysql2"
3
+
4
+ module FluidDb
5
+
6
+ class Mysql2<Base
7
+
8
+ def initialize(uri)
9
+ host = uri.host
10
+ database = uri.path.sub( "/", "" )
11
+
12
+
13
+ @connection = ::Mysql2::Client.new(:host => uri.host,
14
+ :database => uri.path.sub( "/", "" ),
15
+ :username => uri.user,
16
+ :flags => ::Mysql2::Client::FOUND_ROWS )
17
+ end
18
+
19
+ def queryForArray( sql, params )
20
+ sql = self.format_to_sql( sql, params )
21
+ results = @connection.query(sql)
22
+
23
+ # if ( $result === false ) then
24
+ # $message = pg_last_error( $this->connection );
25
+ # throw new Fluid_ConnectionException( $message );
26
+ #end
27
+
28
+ case results.count
29
+ when -1
30
+ raise FluidDb::ConnectionError.new
31
+ when 0
32
+ raise FluidDb::NoDataFoundError.new
33
+ when 1
34
+ r=nil;
35
+ results.each do |row|
36
+ r=row
37
+ end
38
+ return r
39
+ else
40
+ raise FluidDb::TooManyRowsError.new
41
+ end
42
+
43
+ end
44
+
45
+ def queryForValue( sql, params )
46
+ sql = self.format_to_sql( sql, params )
47
+ results = @connection.query(sql, :as => :array)
48
+
49
+ # if ( $result === false ) then
50
+ # $message = pg_last_error( $this->connection );
51
+ # throw new Fluid_ConnectionException( $message );
52
+ #end
53
+
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
70
+
71
+
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
+ case results.count
82
+ when -1
83
+ raise FluidDb::ConnectionError.new
84
+ else
85
+ list = Array.new
86
+ results.each do |row|
87
+ list.push row
88
+ end
89
+
90
+ return list
91
+ end
92
+ end
93
+
94
+ def execute( sql, params, expected_affected_rows=nil )
95
+ sql = self.format_to_sql( sql, params )
96
+ @connection.query( sql );
97
+
98
+ if !expected_affected_rows.nil? and
99
+ @connection.affected_rows != expected_affected_rows then
100
+ raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.affected_rows}")
101
+ end
102
+ end
103
+
104
+
105
+ def insert( sql, params )
106
+ self.execute( sql, params )
107
+ return @connection.last_id
108
+ end
109
+
110
+ end
111
+
112
+ end
data/lib/FluidDb/Pgsql.rb CHANGED
@@ -11,16 +11,7 @@ module FluidDb
11
11
 
12
12
  @connection = PG.connect( dbname:uri.path.sub( "/", "" ) )
13
13
  end
14
-
15
- def convertTupleToHash( fields, tuple, j )
16
- hash = Hash.new
17
- 0.upto( fields.length-1 ).each do |i|
18
- hash[fields[i].to_s] = tuple.getvalue(j, i)
19
- end
20
-
21
- return hash
22
- end
23
-
14
+
24
15
  def queryForArray( sql, params )
25
16
  sql = self.format_to_sql( sql, params )
26
17
  results = @connection.exec(sql)
@@ -89,17 +80,22 @@ module FluidDb
89
80
  end
90
81
 
91
82
 
92
- # def execute( sql, params, expected_affected_rows )
93
- def execute( sql, params )
83
+ def execute( sql, params, expected_affected_rows=nil )
94
84
  sql = self.format_to_sql( sql, params )
95
- @connection.query( sql );
85
+ r = @connection.query( sql );
86
+
87
+ if !expected_affected_rows.nil? and
88
+ r.cmd_tuples != expected_affected_rows then
89
+ raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}")
90
+ end
96
91
  end
97
-
92
+
98
93
  def insert( sql, params )
99
- self.execute( sql, params )
100
- return @connection.last_id
94
+ raise "Pgsql uses SEQUENCES, so possibly easier to use 2 executes"
95
+ # self.execute( sql, params )
96
+ #return @connection.last_id
101
97
  end
102
-
98
+
103
99
  end
104
100
 
105
101
  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.5
4
+ version: 0.0.6
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-08-05 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A semantic layer for db interaction
15
15
  email: guy@guyirvine.com
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/FluidDb/Mysql.rb
21
+ - lib/FluidDb/Mysql2.rb
21
22
  - lib/FluidDb/Pgsql.rb
22
23
  - lib/FluidDb.rb
23
24
  - README.md