fluiddb 0.1.15 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,36 @@
1
1
  require "FluidDb"
2
2
 
3
3
  module FluidDb
4
-
5
- def FluidDb.Db( uri )
6
- uri = URI.parse( uri ) if uri.is_a? String
7
-
8
- case uri.scheme
9
- when "mysql"
10
- require "FluidDb/Mysql"
11
- return FluidDb::Mysql.new( uri )
12
- when "mysql2"
13
- require "FluidDb/Mysql2"
14
- return FluidDb::Mysql2.new( uri )
15
- when "pgsql"
16
- require "FluidDb/Pgsql"
17
- return FluidDb::Pgsql.new( uri )
18
- when "fb"
19
- require "FluidDb/Firebird"
20
- return FluidDb::Firebird.new( uri )
21
- when "mock"
22
- require "FluidDb/Mock"
23
- return FluidDb::Mock.new( uri )
24
- when "tinytds"
25
- require "FluidDb/TinyTds"
26
- return FluidDb::TinyTds.new( uri )
27
4
 
28
- else
29
- abort("Scheme, #{uri.scheme}, not recognised when configuring creating db connection");
30
- end
31
-
5
+ def FluidDb.Db( uri )
6
+ uri = URI.parse( uri ) if uri.is_a? String
7
+
8
+ case uri.scheme
9
+ when "mysql"
10
+ require "FluidDb/Mysql"
11
+ return FluidDb::Mysql.new( uri )
12
+ when "mysql2"
13
+ require "FluidDb/Mysql2"
14
+ return FluidDb::Mysql2.new( uri )
15
+ when "pgsql"
16
+ require "FluidDb/Pgsql"
17
+ return FluidDb::Pgsql.new( uri )
18
+ when "fb"
19
+ require "FluidDb/Firebird"
20
+ return FluidDb::Firebird.new( uri )
21
+ when "mock"
22
+ require "FluidDb/Mock"
23
+ return FluidDb::Mock.new( uri )
24
+ when "tinytds"
25
+ require "FluidDb/TinyTds"
26
+ return FluidDb::TinyTds.new( uri )
27
+ when "sqlite"
28
+ require "FluidDb/SQLite"
29
+ return FluidDb::SQLite.new( uri )
30
+
31
+ else
32
+ abort("Scheme, #{uri.scheme}, not recognised when configuring creating db connection");
32
33
  end
34
+
35
+ end
33
36
  end
@@ -30,6 +30,8 @@ end
30
30
 
31
31
  def getSqlFromHash( sql )
32
32
  raise SqlNotMatchedError.new( sql ) unless @hash.has_key?( sql )
33
+
34
+ return @hash[sql]
33
35
  end
34
36
 
35
37
  def queryForArray( sql, params=[] )
@@ -0,0 +1,135 @@
1
+ require "FluidDb"
2
+ require "sqlite3"
3
+
4
+ module FluidDb
5
+
6
+ class SQLite<Base
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
13
+ @connection = SQLite3::Database.new uri.path
14
+ end
15
+
16
+ def close
17
+ @connection.close
18
+ end
19
+
20
+ def queryForArray( sql, params=[] )
21
+ sql = self.format_to_sql( sql, params )
22
+ @connection.results_as_hash = true
23
+ results = @connection.execute(sql)
24
+
25
+ # if ( $result === false ) then
26
+ # $message = pg_last_error( $this->connection );
27
+ # throw new Fluid_ConnectionException( $message );
28
+ #end
29
+
30
+ case results.length
31
+ when -1
32
+ raise FluidDb::ConnectionError.new
33
+ when 0
34
+ raise FluidDb::NoDataFoundError.new
35
+ when 1
36
+ return results[0]
37
+ else
38
+ raise FluidDb::TooManyRowsError.new
39
+ end
40
+ end
41
+
42
+ def queryForValue( sql, params=[] )
43
+ sql = self.format_to_sql( sql, params )
44
+ @connection.results_as_hash = false
45
+ results = @connection.execute(sql)
46
+
47
+ # if ( $result === false ) then
48
+ # $message = pg_last_error( $this->connection );
49
+ # throw new Fluid_ConnectionException( $message );
50
+ #end
51
+
52
+ case results.length
53
+ when -1
54
+ raise FluidDb::ConnectionError.new
55
+ when 0
56
+ raise FluidDb::NoDataFoundError.new
57
+ when 1
58
+ return results[0][0]
59
+ else
60
+ raise FluidDb::TooManyRowsError.new
61
+ end
62
+
63
+ end
64
+
65
+
66
+ def queryForResultset( sql, params=[] )
67
+ sql = self.format_to_sql( sql, params )
68
+ @connection.results_as_hash = true
69
+ results = @connection.execute(sql)
70
+
71
+ # if ( $result === false ) then
72
+ # $message = pg_last_error( $this->connection );
73
+ # throw new Fluid_ConnectionException( $message );
74
+ #end
75
+
76
+ case results.length
77
+ when -1
78
+ raise FluidDb::ConnectionError.new
79
+ else
80
+ return results
81
+ end
82
+ end
83
+
84
+
85
+ def execute( sql, params=[], expected_affected_rows=nil )
86
+ sql = self.format_to_sql( sql, params )
87
+
88
+ self.verboseLog( "#{self.class.name}.execute. #{sql}" )
89
+ r = @connection.execute(sql)
90
+
91
+ if !expected_affected_rows.nil? and
92
+ r.changes != expected_affected_rows then
93
+ raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}")
94
+ end
95
+ end
96
+
97
+ def exec_params( sql, params=[], expected_affected_rows=nil )
98
+ parts = sql.split( "?" )
99
+ sql = ""
100
+ parts.each_with_index do |p,idx|
101
+ sql = sql + p;
102
+ sql = sql + "$#{idx+1}" if idx < parts.length - 1
103
+ end
104
+ r = @connection.exec_params( sql, params );
105
+
106
+ if !expected_affected_rows.nil? and
107
+ r.changes != expected_affected_rows then
108
+ raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}")
109
+ end
110
+ end
111
+
112
+ # Transaction Semantics
113
+ def Begin
114
+ @connection.transaction
115
+ end
116
+
117
+ # Transaction Semantics
118
+ def Commit
119
+ @connection.commit
120
+ end
121
+
122
+ # Transaction Semantics
123
+ def Rollback
124
+ @connection.rollback
125
+ end
126
+
127
+ def insert( sql, params )
128
+ raise "SQLite uses SEQUENCES, so possibly easier to use 2 executes"
129
+ # self.execute( sql, params )
130
+ #return @connection.last_id
131
+ end
132
+
133
+ end
134
+
135
+ 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.1.15
4
+ version: 0.1.17
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: 2014-07-04 00:00:00.000000000 Z
12
+ date: 2014-08-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A semantic layer for db interaction
15
15
  email: guy@guyirvine.com
@@ -23,6 +23,7 @@ files:
23
23
  - lib/FluidDb/Mysql.rb
24
24
  - lib/FluidDb/Mysql2.rb
25
25
  - lib/FluidDb/Pgsql.rb
26
+ - lib/FluidDb/SQLite.rb
26
27
  - lib/FluidDb/TinyTds.rb
27
28
  - lib/FluidDb.rb
28
29
  - README.md