fluiddb 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,3 +5,6 @@ A semantic layer for db interaction
5
5
  ##install
6
6
  gem install fluiddb
7
7
 
8
+ ##Examples
9
+
10
+ See the tests for a reasonable run down on usage.
@@ -1,4 +1,5 @@
1
1
  require 'date'
2
+ require 'time'
2
3
  require 'uri'
3
4
 
4
5
  module FluidDb
@@ -17,6 +18,15 @@ module FluidDb
17
18
  class Base
18
19
 
19
20
  @connection;
21
+ @uri
22
+
23
+ # Constructor.
24
+ #
25
+ # @param [String] uri a location for the resource to which we will attach, eg mysql://user:pass@127.0.0.1/foo
26
+ def initialize(uri)
27
+ @uri = uri
28
+ self.connect
29
+ end
20
30
 
21
31
  def format_to_sql( sql, params=nil )
22
32
  if params.nil? then
@@ -30,6 +40,9 @@ module FluidDb
30
40
  elsif v.is_a? DateTime then
31
41
  s = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
32
42
  sql = sql.sub( "?", s )
43
+ elsif v.is_a? Time then
44
+ s = "'" + v.strftime( "%Y-%m-%d %H:%M:%S" ) + "'"
45
+ sql = sql.sub( "?", s )
33
46
  elsif v.kind_of? Date then
34
47
  v = "'" + v.to_s + "'"
35
48
  sql = sql.sub( "?", v.to_s )
@@ -42,7 +55,7 @@ module FluidDb
42
55
 
43
56
  return sql
44
57
  end
45
-
58
+
46
59
  def convertTupleToHash( fields, tuple, j )
47
60
  hash = Hash.new
48
61
  0.upto( fields.length-1 ).each do |i|
@@ -50,12 +63,37 @@ module FluidDb
50
63
  end
51
64
 
52
65
  return hash
53
- end
66
+ end
54
67
 
68
+ def connect
69
+ raise NotImplementedError.new("You must implement 'connect'.")
70
+ end
71
+
72
+ def close
73
+ raise NotImplementedError.new("You must implement 'close'.")
74
+ end
75
+
76
+ def reconnect
77
+ self.close
78
+ self.connect
79
+ end
80
+
81
+ # Return a single row from the database, given the sql parameter.
82
+ # Throwa an error for no data.
83
+ # Throws an error for more than 1 row
84
+ #
85
+ # @param [String] sql The SELECT statement to run
86
+ # @param [Array] parama The parameters to be added to the sql query. Ruby types are used to determine formatting and escaping.
55
87
  def queryForArray( sql, params )
56
88
  raise NotImplementedError.new("You must implement 'queryForArray'.")
57
89
  end
58
90
 
91
+ # Return a single value is returned from a single row from the database, given the sql parameter.
92
+ # Throwa an error for no data.
93
+ # Throws an error for more than 1 row
94
+ #
95
+ # @param [String] sql The SELECT statement to run
96
+ # @param [Array] parama The parameters to be added to the sql query. Ruby types are used to determine formatting and escaping.
59
97
  def queryForValue( sql, params )
60
98
  raise NotImplementedError.new("You must implement 'queryForValue'.")
61
99
  end
@@ -64,6 +102,11 @@ module FluidDb
64
102
  raise NotImplementedError.new("You must implement 'queryForResultset'.")
65
103
  end
66
104
 
105
+ # Execute an insert, update or delete, then check the impact that statement has on the data.
106
+ #
107
+ # @param [String] sql The SELECT statement to run
108
+ # @param [Array] parama The parameters to be added to the sql query. Ruby types are used to determine formatting and escaping.
109
+ # @param [String] expected_affected_rows The number of rows that should have been updated.
67
110
  def execute( sql, params, expected_affected_rows )
68
111
  raise NotImplementedError.new("You must implement 'execute'.")
69
112
  end
@@ -5,12 +5,20 @@ module FluidDb
5
5
 
6
6
  class Mysql<Base
7
7
 
8
- def initialize(uri)
8
+ # Connect to Db.
9
+ #
10
+ # @param [String] uri a location for the resource to which we will attach, eg mysql://user:pass@127.0.0.1/foo
11
+ def connect()
12
+ uri = @uri
9
13
  database = uri.path.sub( "/", "" )
10
14
 
11
15
  @connection = ::Mysql.new uri.host, uri.user, uri.password, database, nil, nil, ::Mysql::CLIENT_FOUND_ROWS
12
16
  end
13
17
 
18
+ def close
19
+ @connection.close
20
+ end
21
+
14
22
  def queryForArray( sql, params )
15
23
  sql = self.format_to_sql( sql, params )
16
24
  results = @connection.query(sql)
@@ -60,7 +68,6 @@ module FluidDb
60
68
 
61
69
  end
62
70
 
63
-
64
71
  def queryForResultset( sql, params )
65
72
  sql = self.format_to_sql( sql, params )
66
73
  results = @connection.query(sql)
@@ -82,7 +89,7 @@ module FluidDb
82
89
  return list
83
90
  end
84
91
  end
85
-
92
+
86
93
  def execute( sql, params, expected_affected_rows=nil )
87
94
  sql = self.format_to_sql( sql, params )
88
95
  # puts "sql: #{sql}"
@@ -93,7 +100,7 @@ module FluidDb
93
100
  raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.affected_rows}")
94
101
  end
95
102
  end
96
-
103
+
97
104
  def insert( sql, params )
98
105
  self.execute( sql, params )
99
106
  return @connection.insert_id
@@ -4,8 +4,12 @@ require "mysql2"
4
4
  module FluidDb
5
5
 
6
6
  class Mysql2<Base
7
-
8
- def initialize(uri)
7
+
8
+ # Connect to Db.
9
+ #
10
+ # @param [String] uri a location for the resource to which we will attach, eg mysql://user:pass@127.0.0.1/foo
11
+ def connect()
12
+ uri = @uri
9
13
  host = uri.host
10
14
  database = uri.path.sub( "/", "" )
11
15
 
@@ -16,6 +20,10 @@ module FluidDb
16
20
  :flags => ::Mysql2::Client::FOUND_ROWS )
17
21
  end
18
22
 
23
+ def close
24
+ @connection.close
25
+ end
26
+
19
27
  def queryForArray( sql, params )
20
28
  sql = self.format_to_sql( sql, params )
21
29
  results = @connection.query(sql)
@@ -5,13 +5,21 @@ module FluidDb
5
5
 
6
6
  class Pgsql<Base
7
7
 
8
- def initialize(uri)
8
+ # Connect to Db.
9
+ #
10
+ # @param [String] uri a location for the resource to which we will attach, eg mysql://user:pass@127.0.0.1/foo
11
+ def connect()
12
+ uri = @uri
9
13
  host = uri.host
10
14
  database = uri.path.sub( "/", "" )
11
15
 
12
16
  @connection = PG.connect( dbname:uri.path.sub( "/", "" ) )
13
17
  end
14
-
18
+
19
+ def close
20
+ @connection.close
21
+ end
22
+
15
23
  def queryForArray( sql, params )
16
24
  sql = self.format_to_sql( sql, params )
17
25
  results = @connection.exec(sql)
@@ -36,7 +44,7 @@ module FluidDb
36
44
  def queryForValue( sql, params )
37
45
  sql = self.format_to_sql( sql, params )
38
46
  results = @connection.exec(sql)
39
-
47
+
40
48
  # if ( $result === false ) then
41
49
  # $message = pg_last_error( $this->connection );
42
50
  # throw new Fluid_ConnectionException( $message );
@@ -89,13 +97,13 @@ module FluidDb
89
97
  raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}")
90
98
  end
91
99
  end
92
-
100
+
93
101
  def insert( sql, params )
94
102
  raise "Pgsql uses SEQUENCES, so possibly easier to use 2 executes"
95
103
  # self.execute( sql, params )
96
104
  #return @connection.last_id
97
105
  end
98
-
106
+
99
107
  end
100
108
 
101
109
  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.6
4
+ version: 0.0.8
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-06 00:00:00.000000000 Z
12
+ date: 2012-09-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A semantic layer for db interaction
15
15
  email: guy@guyirvine.com