rhodes-framework 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,7 +37,6 @@ lib/rhom/rhom_source.rb
37
37
  lib/singleton.rb
38
38
  lib/time.rb
39
39
  lib/version.rb
40
- rhodes-framework.gemspec
41
40
  spec/README
42
41
  spec/Rakefile
43
42
  spec/app/Account/config.rb
@@ -1,9 +1,9 @@
1
1
  module Rhodes
2
2
  unless defined? Rhodes::VERSION
3
- VERSION = '1.2.0'
3
+ VERSION = '1.2.1'
4
4
  end
5
5
  unless defined? Rhodes::DBVERSION
6
- DBVERSION = '1.2.0'
6
+ DBVERSION = '1.2.1'
7
7
  end
8
8
 
9
9
  end
@@ -1,71 +1,71 @@
1
- #
2
- # rhom.rb
3
- # rhodes
4
- # This module represents the rhodes mini OM
5
- #
6
- # Copyright (C) 2008 Rhomobile, Inc. All rights reserved.
7
- #
8
- # This program is free software: you can redistribute it and/or modify
9
- # it under the terms of the GNU General Public License as published by
10
- # the Free Software Foundation, either version 3 of the License, or
11
- # (at your option) any later version.
12
- #
13
- # This program is distributed in the hope that it will be useful,
14
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- # GNU General Public License for more details.
17
- #
18
- # You should have received a copy of the GNU General Public License
19
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
- #
21
-
22
- require 'rhom/rhom_object_factory'
23
- require 'rhom/rhom_object'
24
- require 'rhom/rhom_db_adapter'
25
-
26
- module Rhom
27
- UPDATE_TYPES = ["'create'","'query'","'ask'"]
28
-
29
- class RecordNotFound < StandardError
30
- end
31
-
32
- class Rhom
33
- attr_accessor :factory
34
-
35
- def initialize
36
- @factory = RhomObjectFactory.new
37
- end
38
-
39
- class << Rhom
40
- def client_id
41
- c_id = ::Rhom::RhomDbAdapter.select_from_table('client_info','client_id')[0]
42
- c_id.nil? ? nil : c_id['client_id']
43
- end
44
-
45
- def database_full_reset
46
- SyncEngine.stop_sync
47
-
48
- ::Rhom::RhomDbAdapter.execute_sql("UPDATE client_info SET reset=1")
49
-
50
- if defined? RHO_ME
51
- ::Rhom::RhomDbAdapter.execute_sql("UPDATE sources SET token=NULL")
52
- else
53
- ::Rhom::RhomDbAdapter.execute_sql("UPDATE sources SET token=0")
54
- end
55
-
56
- if defined? RHO_DBME
57
- ::Rhom::RhomDbAdapter.destroy_table('object_values')
58
- #::Rhom::RhomDbAdapter.delete_all_from_table('object_values')
59
- else
60
- ::Rhom::RhomDbAdapter.delete_all_from_table('object_values')
61
- ::Rhom::RhomDbAdapter.execute_sql("VACUUM")
62
- end
63
- end
64
-
65
- def database_full_reset_and_logout
66
- database_full_reset
67
- SyncEngine.logout
68
- end
69
- end #class methods
70
- end # Rhom
71
- end # Rhom
1
+ #
2
+ # rhom.rb
3
+ # rhodes
4
+ # This module represents the rhodes mini OM
5
+ #
6
+ # Copyright (C) 2008 Rhomobile, Inc. All rights reserved.
7
+ #
8
+ # This program is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+
22
+ require 'rhom/rhom_object_factory'
23
+ require 'rhom/rhom_object'
24
+ require 'rhom/rhom_db_adapter'
25
+
26
+ module Rhom
27
+ UPDATE_TYPES = ["'create'","'query'","'ask'"]
28
+
29
+ class RecordNotFound < StandardError
30
+ end
31
+
32
+ class Rhom
33
+ attr_accessor :factory
34
+
35
+ def initialize
36
+ @factory = RhomObjectFactory.new
37
+ end
38
+
39
+ class << Rhom
40
+ def client_id
41
+ c_id = ::Rhom::RhomDbAdapter.select_from_table('client_info','client_id')[0]
42
+ c_id.nil? ? nil : c_id['client_id']
43
+ end
44
+
45
+ def database_full_reset
46
+ SyncEngine.stop_sync
47
+
48
+ ::Rhom::RhomDbAdapter.execute_sql("UPDATE client_info SET reset=1")
49
+
50
+ if defined? RHO_ME
51
+ ::Rhom::RhomDbAdapter.execute_sql("UPDATE sources SET token=NULL")
52
+ else
53
+ ::Rhom::RhomDbAdapter.execute_sql("UPDATE sources SET token=0")
54
+ end
55
+
56
+ # if defined? RHO_DBME
57
+ ::Rhom::RhomDbAdapter.destroy_table('object_values')
58
+ #::Rhom::RhomDbAdapter.delete_all_from_table('object_values')
59
+ # else
60
+ # ::Rhom::RhomDbAdapter.delete_all_from_table('object_values')
61
+ # ::Rhom::RhomDbAdapter.execute_sql("VACUUM")
62
+ # end
63
+ end
64
+
65
+ def database_full_reset_and_logout
66
+ database_full_reset
67
+ SyncEngine.logout
68
+ end
69
+ end #class methods
70
+ end # Rhom
71
+ end # Rhom
@@ -1,249 +1,252 @@
1
- #
2
- # rhom_db_adapter.rb
3
- # rhodes
4
- #
5
- # Copyright (C) 2008 Rhomobile, Inc. All rights reserved.
6
- #
7
- # This program is free software: you can redistribute it and/or modify
8
- # it under the terms of the GNU General Public License as published by
9
- # the Free Software Foundation, either version 3 of the License, or
10
- # (at your option) any later version.
11
- #
12
- # This program is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- # GNU General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public License
18
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
- #
20
- require 'rhodes'
21
-
22
- module Rhom
23
- class RhomDbAdapter
24
-
25
- @@database = nil
26
- @@inside_transaction = false
27
-
28
- class << self
29
-
30
- # maintains a single database connection
31
- def open(dbfile=nil)
32
- unless @@database or dbfile.nil?
33
- @@database = SQLite3::Database.new(dbfile)
34
- end
35
- end
36
-
37
- # closes the database if and only if it is open
38
- def close
39
- if @@database
40
- @@database.close
41
- @@database = nil
42
- else
43
- return false
44
- end
45
- return true
46
- end
47
-
48
- def start_transaction
49
- begin
50
- @@inside_transaction = true
51
- @@database.start_transaction
52
- rescue Exception => e
53
- puts "exception when start_transaction"
54
- end
55
- end
56
-
57
- def commit
58
- begin
59
- @@inside_transaction = false
60
- @@database.commit
61
- rescue Exception => e
62
- puts "exception when commit transaction"
63
- end
64
- end
65
-
66
- def rollback
67
- begin
68
- @@inside_transaction = false
69
- @@database.rollback
70
- rescue Exception => e
71
- puts "exception when rollback transaction"
72
- end
73
- end
74
-
75
- # execute a sql statement
76
- # optionally, disable the factory processing
77
- # which returns the result array directly
78
- def execute_sql(sql=nil)
79
- result = []
80
- if sql
81
- #puts "RhomDbAdapter: Executing query - #{sql}"
82
- # Make sure we lock the sync engine's mutex
83
- # before we perform a database transaction.
84
- # This prevents concurrency issues.
85
- begin
86
- SyncEngine.lock_sync_mutex unless @@inside_transaction
87
- result = @@database.execute sql
88
- SyncEngine.unlock_sync_mutex unless @@inside_transaction
89
- rescue Exception => e
90
- #puts "exception when running query: #{e}"
91
- # make sure we unlock even if there's an error!
92
- if @@inside_transaction
93
- raise
94
- else
95
- SyncEngine.unlock_sync_mutex
96
- end
97
- end
98
- end
99
- #puts "result is: #{result.inspect}"
100
- result
101
- end
102
-
103
- # generates where clause based on hash
104
- def where_str(condition,select_arr=nil)
105
- where_str = ""
106
- if condition
107
- where_str += string_from_key_vals(condition,"and")
108
- where_str = where_str[0..where_str.length - 5]
109
- end
110
-
111
- if select_arr and select_arr.length > 0
112
- where_str += " and attrib in (#{select_str(select_arr)})"
113
- end
114
- where_str
115
- end
116
-
117
- def select_str(select_arr)
118
- select_str = ""
119
- select_arr.each do |attrib|
120
- select_str << "'#{attrib}'" + ","
121
- end
122
- select_str.length > 2 ? select_str[0..select_str.length-2] : select_str
123
- end
124
-
125
- # generates value clause based on hash
126
- def vals_str(values)
127
- vals = string_from_key_vals(values, ",")
128
- vals[0..vals.length - 2]
129
- end
130
-
131
- # generates key/value list
132
- def string_from_key_vals(values, delim)
133
- vals = ""
134
- values.each do |key,value|
135
- op = value.nil? ? 'is ' : '= '
136
- vals << " \"#{key}\" #{op} #{get_value_for_sql_stmt(value)} #{delim}"
137
- end
138
- vals
139
- end
140
-
141
- # generates a value for sql statement
142
- def get_value_for_sql_stmt(value)
143
- if value.nil? or value == 'NULL'
144
- "NULL"
145
- elsif value.is_a?(String)
146
- "'#{value}'"
147
- else
148
- "'#{value.to_s}'"
149
- end
150
- end
151
-
152
- # support for select statements
153
- # this function takes table name, columns (as a comma-separated list),
154
- # condition (as a hash), and params (as a hash)
155
- # example usage is the following:
156
- # select_from_table('object_values', '*', {"source_id"=>2,"update_type"=>'query'},
157
- # {"order by"=>'object'})
158
- # this would return all columns where source_id = 2 and update_type = 'query' ordered
159
- # by the "object" column
160
- def select_from_table(table=nil,columns=nil,condition=nil,params=nil,select_arr=nil)
161
- query = nil
162
- if table and columns and condition
163
- if params and params['distinct']
164
- query = "select distinct #{columns} from #{table} where #{where_str(condition,select_arr)}"
165
- elsif params and params['order by']
166
- query = "select #{columns} from #{table} where #{where_str(condition,select_arr)} order by #{params['order by']}"
167
- else
168
- query = "select #{columns} from #{table} where #{where_str(condition,select_arr)}"
169
- end
170
- elsif table and columns
171
- query = "select #{columns} from #{table}"
172
- end
173
- execute_sql query
174
- end
175
-
176
- # inserts a single row into the database
177
- # takes the table name and values (hash) as arguments
178
- # exmaple usage is the following:
179
- # insert_into_table('object_values, {"source_id"=>1,"object"=>"some-object","update_type"=>'delete'})
180
- # this would execute the following sql:
181
- # insert into object_values (source_id,object,update_type) values (1,'some-object','delete');
182
- def insert_into_table(table=nil,values=nil)
183
- query = nil
184
- cols = ""
185
- vals = ""
186
- if table and values
187
- values.each do |key,val|
188
- value = get_value_for_sql_stmt(val)+","
189
- cols << "#{key},"
190
- vals << value
191
- end
192
- cols = cols[0..cols.length - 2]
193
- vals = vals[0..vals.length - 2]
194
- query = "insert into #{table} (#{cols}) values (#{vals})"
195
- end
196
- execute_sql query
197
- end
198
-
199
- # deletes rows from a table which satisfy condition (hash)
200
- # example usage is the following:
201
- # delete_from_table('object_values',{"object"=>"some-object"})
202
- # this would execute the following sql:
203
- # delete from object_values where object="some-object"
204
- def delete_from_table(table=nil,condition=nil)
205
- query = nil
206
- if table and condition
207
- query = "delete from #{table} where #{where_str(condition)}"
208
- end
209
- execute_sql query
210
- end
211
-
212
- # deletes all rows from a given table
213
- def delete_all_from_table(table=nil)
214
- query = nil
215
- if table
216
- query = "delete from #{table}"
217
- end
218
- execute_sql query
219
- end
220
-
221
- # deletes all rows from a given table by recreating db-file and save all other tables
222
- def destroy_table(table)
223
- query = nil
224
- if table
225
- query = "destroy #{table}"
226
- end
227
- execute_sql query
228
- end
229
-
230
- # updates values (hash) in a given table which satisfy condition (hash)
231
- # example usage is the following:
232
- # update_into_table('object_values',{"value"=>"Electronics"},{"object"=>"some-object", "attrib"=>"industry"})
233
- # this executes the following sql:
234
- # update table object_values set value='Electronics' where object='some-object' and attrib='industry';
235
- def update_into_table(table=nil,values=nil,condition=nil)
236
- query = nil
237
- vals = values.nil? ? nil : vals_str(values)
238
- if table and condition and vals
239
- query = "update #{table} set #{vals} where #{where_str(condition)}"
240
- end
241
- execute_sql query
242
- end
243
- end # class methods
244
- end # RhomDbAdapter
245
- end # Rhom
246
-
247
- at_exit do
248
- ::Rhom::RhomDbAdapter.close
249
- end
1
+ #
2
+ # rhom_db_adapter.rb
3
+ # rhodes
4
+ #
5
+ # Copyright (C) 2008 Rhomobile, Inc. All rights reserved.
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+ #
20
+ require 'rhodes'
21
+
22
+ module Rhom
23
+ class RhomDbAdapter
24
+
25
+ @@database = nil
26
+ @@inside_transaction = false
27
+
28
+ class << self
29
+
30
+ # maintains a single database connection
31
+ def open(dbfile=nil)
32
+ unless @@database or dbfile.nil?
33
+ @@database = SQLite3::Database.new(dbfile)
34
+ end
35
+ end
36
+
37
+ # closes the database if and only if it is open
38
+ def close
39
+ if @@database
40
+ @@database.close
41
+ @@database = nil
42
+ else
43
+ return false
44
+ end
45
+ return true
46
+ end
47
+
48
+ def start_transaction
49
+ begin
50
+ @@inside_transaction = true
51
+ @@database.start_transaction
52
+ rescue Exception => e
53
+ puts "exception when start_transaction"
54
+ end
55
+ end
56
+
57
+ def commit
58
+ begin
59
+ @@inside_transaction = false
60
+ @@database.commit
61
+ rescue Exception => e
62
+ puts "exception when commit transaction"
63
+ end
64
+ end
65
+
66
+ def rollback
67
+ begin
68
+ @@inside_transaction = false
69
+ @@database.rollback
70
+ rescue Exception => e
71
+ puts "exception when rollback transaction"
72
+ end
73
+ end
74
+
75
+ # execute a sql statement
76
+ # optionally, disable the factory processing
77
+ # which returns the result array directly
78
+ def execute_sql(sql=nil)
79
+ result = []
80
+ if sql
81
+ #puts "RhomDbAdapter: Executing query - #{sql}"
82
+ # Make sure we lock the sync engine's mutex
83
+ # before we perform a database transaction.
84
+ # This prevents concurrency issues.
85
+ begin
86
+ SyncEngine.lock_sync_mutex unless @@inside_transaction
87
+ result = @@database.execute sql
88
+ SyncEngine.unlock_sync_mutex unless @@inside_transaction
89
+ rescue Exception => e
90
+ #puts "exception when running query: #{e}"
91
+ # make sure we unlock even if there's an error!
92
+ if @@inside_transaction
93
+ raise
94
+ else
95
+ SyncEngine.unlock_sync_mutex
96
+ end
97
+ end
98
+ end
99
+ #puts "result is: #{result.inspect}"
100
+ result
101
+ end
102
+
103
+ # generates where clause based on hash
104
+ def where_str(condition,select_arr=nil)
105
+ where_str = ""
106
+ if condition
107
+ where_str += string_from_key_vals(condition,"and")
108
+ where_str = where_str[0..where_str.length - 5]
109
+ end
110
+
111
+ if select_arr and select_arr.length > 0
112
+ where_str += " and attrib in (#{select_str(select_arr)})"
113
+ end
114
+ where_str
115
+ end
116
+
117
+ def select_str(select_arr)
118
+ select_str = ""
119
+ select_arr.each do |attrib|
120
+ select_str << "'#{attrib}'" + ","
121
+ end
122
+ select_str.length > 2 ? select_str[0..select_str.length-2] : select_str
123
+ end
124
+
125
+ # generates value clause based on hash
126
+ def vals_str(values)
127
+ vals = string_from_key_vals(values, ",")
128
+ vals[0..vals.length - 2]
129
+ end
130
+
131
+ # generates key/value list
132
+ def string_from_key_vals(values, delim)
133
+ vals = ""
134
+ values.each do |key,value|
135
+ op = value.nil? ? 'is ' : '= '
136
+ vals << " \"#{key}\" #{op} #{get_value_for_sql_stmt(value)} #{delim}"
137
+ end
138
+ vals
139
+ end
140
+
141
+ # generates a value for sql statement
142
+ def get_value_for_sql_stmt(value)
143
+ if value.nil? or value == 'NULL'
144
+ "NULL"
145
+ elsif value.is_a?(String)
146
+ "'#{value}'"
147
+ else
148
+ "'#{value.to_s}'"
149
+ end
150
+ end
151
+
152
+ # support for select statements
153
+ # this function takes table name, columns (as a comma-separated list),
154
+ # condition (as a hash), and params (as a hash)
155
+ # example usage is the following:
156
+ # select_from_table('object_values', '*', {"source_id"=>2,"update_type"=>'query'},
157
+ # {"order by"=>'object'})
158
+ # this would return all columns where source_id = 2 and update_type = 'query' ordered
159
+ # by the "object" column
160
+ def select_from_table(table=nil,columns=nil,condition=nil,params=nil,select_arr=nil)
161
+ query = nil
162
+ if table and columns and condition
163
+ if params and params['distinct']
164
+ query = "select distinct #{columns} from #{table} where #{where_str(condition,select_arr)}"
165
+ elsif params and params['order by']
166
+ query = "select #{columns} from #{table} where #{where_str(condition,select_arr)} order by #{params['order by']}"
167
+ else
168
+ query = "select #{columns} from #{table} where #{where_str(condition,select_arr)}"
169
+ end
170
+ elsif table and columns
171
+ query = "select #{columns} from #{table}"
172
+ end
173
+ execute_sql query
174
+ end
175
+
176
+ # inserts a single row into the database
177
+ # takes the table name and values (hash) as arguments
178
+ # exmaple usage is the following:
179
+ # insert_into_table('object_values, {"source_id"=>1,"object"=>"some-object","update_type"=>'delete'})
180
+ # this would execute the following sql:
181
+ # insert into object_values (source_id,object,update_type) values (1,'some-object','delete');
182
+ def insert_into_table(table=nil,values=nil)
183
+ query = nil
184
+ cols = ""
185
+ vals = ""
186
+ if table and values
187
+ values.each do |key,val|
188
+ value = get_value_for_sql_stmt(val)+","
189
+ cols << "#{key},"
190
+ vals << value
191
+ end
192
+ cols = cols[0..cols.length - 2]
193
+ vals = vals[0..vals.length - 2]
194
+ query = "insert into #{table} (#{cols}) values (#{vals})"
195
+ end
196
+ execute_sql query
197
+ end
198
+
199
+ # deletes rows from a table which satisfy condition (hash)
200
+ # example usage is the following:
201
+ # delete_from_table('object_values',{"object"=>"some-object"})
202
+ # this would execute the following sql:
203
+ # delete from object_values where object="some-object"
204
+ def delete_from_table(table=nil,condition=nil)
205
+ query = nil
206
+ if table and condition
207
+ query = "delete from #{table} where #{where_str(condition)}"
208
+ end
209
+ execute_sql query
210
+ end
211
+
212
+ # deletes all rows from a given table
213
+ def delete_all_from_table(table=nil)
214
+ query = nil
215
+ if table
216
+ query = "delete from #{table}"
217
+ end
218
+ execute_sql query
219
+ end
220
+
221
+ # deletes all rows from a given table by recreating db-file and save all other tables
222
+ def destroy_table(table)
223
+ begin
224
+ SyncEngine.lock_sync_mutex unless @@inside_transaction
225
+ @@database.destroy_table table
226
+ SyncEngine.unlock_sync_mutex unless @@inside_transaction
227
+ rescue Exception => e
228
+ SyncEngine.unlock_sync_mutex
229
+ raise
230
+ end
231
+ end
232
+
233
+ # updates values (hash) in a given table which satisfy condition (hash)
234
+ # example usage is the following:
235
+ # update_into_table('object_values',{"value"=>"Electronics"},{"object"=>"some-object", "attrib"=>"industry"})
236
+ # this executes the following sql:
237
+ # update table object_values set value='Electronics' where object='some-object' and attrib='industry';
238
+ def update_into_table(table=nil,values=nil,condition=nil)
239
+ query = nil
240
+ vals = values.nil? ? nil : vals_str(values)
241
+ if table and condition and vals
242
+ query = "update #{table} set #{vals} where #{where_str(condition)}"
243
+ end
244
+ execute_sql query
245
+ end
246
+ end # class methods
247
+ end # RhomDbAdapter
248
+ end # Rhom
249
+
250
+ at_exit do
251
+ ::Rhom::RhomDbAdapter.close
252
+ end