rhodes-framework 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/History.txt +37 -0
- data/Manifest.txt +66 -0
- data/README.rdoc +2 -0
- data/Rakefile +50 -0
- data/lib/ServeME.rb +7 -0
- data/lib/TestServe.rb +9 -0
- data/lib/bsearch.rb +120 -0
- data/lib/builtinME.rb +626 -0
- data/lib/date/format.rb +1339 -0
- data/lib/date.rb +1792 -0
- data/lib/dateME.rb +24 -0
- data/lib/erb.rb +896 -0
- data/lib/find.rb +81 -0
- data/lib/rational.rb +19 -0
- data/lib/rationalME.rb +530 -0
- data/lib/rho/render.rb +51 -0
- data/lib/rho/rho.rb +255 -0
- data/lib/rho/rhoapplication.rb +36 -0
- data/lib/rho/rhocontact.rb +110 -0
- data/lib/rho/rhocontroller.rb +35 -0
- data/lib/rho/rhofsconnector.rb +32 -0
- data/lib/rho/rhosupport.rb +146 -0
- data/lib/rho/rhoviewhelpers.rb +130 -0
- data/lib/rho.rb +1 -0
- data/lib/rhodes-framework.rb +2 -0
- data/lib/rhodes.rb +9 -0
- data/lib/rhoframework.rb +38 -0
- data/lib/rhofsconnector.rb +1 -0
- data/lib/rhom/rhom.rb +58 -0
- data/lib/rhom/rhom_db_adapter.rb +185 -0
- data/lib/rhom/rhom_db_adapterME.rb +93 -0
- data/lib/rhom/rhom_object.rb +69 -0
- data/lib/rhom/rhom_object_factory.rb +309 -0
- data/lib/rhom/rhom_source.rb +60 -0
- data/lib/rhom.rb +1 -0
- data/lib/singleton.rb +137 -0
- data/lib/time.rb +489 -0
- data/lib/version.rb +8 -0
- data/res/sqlite3/constants.rb +49 -0
- data/res/sqlite3/database.rb +715 -0
- data/res/sqlite3/driver/dl/api.rb +154 -0
- data/res/sqlite3/driver/dl/driver.rb +307 -0
- data/res/sqlite3/driver/native/driver.rb +257 -0
- data/res/sqlite3/errors.rb +68 -0
- data/res/sqlite3/pragmas.rb +271 -0
- data/res/sqlite3/resultset.rb +176 -0
- data/res/sqlite3/sqlite3_api.rb +0 -0
- data/res/sqlite3/statement.rb +230 -0
- data/res/sqlite3/translator.rb +109 -0
- data/res/sqlite3/value.rb +57 -0
- data/res/sqlite3/version.rb +14 -0
- data/rhodes-framework.gemspec +18 -0
- data/rhodes.gemspec +18 -0
- data/spec/app_manifest.txt +4 -0
- data/spec/configs/account.rb +3 -0
- data/spec/configs/case.rb +3 -0
- data/spec/configs/employee.rb +3 -0
- data/spec/rho_controller_spec.rb +144 -0
- data/spec/rho_spec.rb +75 -0
- data/spec/rhom_object_factory_spec.rb +372 -0
- data/spec/rhom_spec.rb +45 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +49 -0
- data/spec/stubs.rb +39 -0
- data/spec/syncdbtest.sqlite +0 -0
- metadata +202 -0
@@ -0,0 +1,130 @@
|
|
1
|
+
module Rho
|
2
|
+
class RhoController
|
3
|
+
|
4
|
+
def truncate(text,length)
|
5
|
+
if text
|
6
|
+
omission = '...'
|
7
|
+
l = length - omission.length
|
8
|
+
(text.length > length ? text[0...l] + omission : text).to_s
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def click_to_call(phone,description=nil)
|
13
|
+
description = phone if description.nil?
|
14
|
+
return "" if phone.nil? or phone.length == 0
|
15
|
+
"<a href=\"tel:#{phone}\" target=\"_self\">#{description}</a>"
|
16
|
+
end
|
17
|
+
|
18
|
+
def mailto(address,description=nil)
|
19
|
+
description = address if description.nil?
|
20
|
+
return "" if address.nil? or address.length == 0
|
21
|
+
"<a href=\"mailto:#{address}\" target=\"_self\">#{description}</a>"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Examples of how to use link_to method:
|
25
|
+
#
|
26
|
+
# link_to "Visit Other Site", "http://www.rhomobile.com/"
|
27
|
+
# ==> <a href=\"http://www.rhomobile.com/\" >Visit Other Site</a>
|
28
|
+
#
|
29
|
+
# link_to "Help", { :action => "help" }
|
30
|
+
# ==> <a href=\"/application/model/help\" >Help</a>
|
31
|
+
#
|
32
|
+
# link_to "Delete", { :action => "delete", :id => '{12}' }
|
33
|
+
# ==> <a href="/application/model/{12}/delete" onclick="var f = document.createElement('form');
|
34
|
+
# f.style.display = 'none';this.parentNode.appendChild(f); f.method = 'POST';
|
35
|
+
# f.action = this.href;f.submit();return false;">Delete</a>
|
36
|
+
#
|
37
|
+
# link_to "Show", { :action => "show", :id => '{12}'},"style=\"height:4px;width:7px;border-width:0px;\""
|
38
|
+
# ==> <a href="/application/model/{12}/show" style="height:4px;width:7px;border-width:0px;">Show</a>
|
39
|
+
#
|
40
|
+
# link_to "Delete", { :action => "delete", :id => '{12}' }, "class=\"delete_link\""
|
41
|
+
# ==> <a href="/application/model/{12}/delete" class="delete_link" onclick="var f = document.createElement('form');
|
42
|
+
# f.style.display = 'none';this.parentNode.appendChild(f); f.method = 'POST';
|
43
|
+
# f.action = this.href;f.submit();return false;\">Delete</a>"
|
44
|
+
#
|
45
|
+
# link_to "Invate",:action => :invite, :query => {:name => 'John Smith', 'address' => "http://john.smith.com"}
|
46
|
+
# ==> <a href="/application/model/invite?name=John%20Smith&address=http%3A%2F%2Fjohn.smith.com" >Invate</a>
|
47
|
+
#
|
48
|
+
def link_to(name,url_params = {},html_options = "",confirm = nil)
|
49
|
+
url = url_for(url_params)
|
50
|
+
if (url_params[:action].to_s != 'delete')
|
51
|
+
"<a href=\"#{url}\" #{html_options}>#{name || url}</a>"
|
52
|
+
else
|
53
|
+
"<a href=\"#{url}\" #{html_options} onclick=\""+ #if (confirm('#{confirm}')) {
|
54
|
+
"var f = document.createElement('form'); f.style.display = 'none';" +
|
55
|
+
"this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();"+
|
56
|
+
"return false;\">#{name || url}</a>"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Examples of how to use url_for method:
|
61
|
+
#
|
62
|
+
# url_for '/some_url'
|
63
|
+
# ==> /some_url
|
64
|
+
#
|
65
|
+
# When generating a new URL, missing values may be filled in from the current request's parameters.
|
66
|
+
# For example, if application name or model are not specifyed in the call parameters, they would be filled from the request.
|
67
|
+
#
|
68
|
+
# url_for :action => :index
|
69
|
+
# ==> /application/model
|
70
|
+
#
|
71
|
+
# url_for :action => :create
|
72
|
+
# ==> /application/model
|
73
|
+
#
|
74
|
+
# url_for :action => :new
|
75
|
+
# ==> /application/model/new
|
76
|
+
#
|
77
|
+
# url_for :action => :show, :id => '{12}'
|
78
|
+
# ==> /application/model/{12}/show
|
79
|
+
#
|
80
|
+
# url_for :model => :another_model, :action => :show, :id => '{12}'
|
81
|
+
# ==> /application/another_model/{12}/show
|
82
|
+
#
|
83
|
+
# url_for :controller => :another_controller, :action => :show, :id => '{12}'
|
84
|
+
# ==> /application/another_controller/{12}/show
|
85
|
+
#
|
86
|
+
# url_for :application => :another_app, :model => :another_model, :action => :show, :id => '{12}'
|
87
|
+
# ==> /another_app/another_model/{12}/show
|
88
|
+
#
|
89
|
+
# url_for :action => :create, :query => {:name => 'John Smith', 'address' => "http://john.smith.com"}
|
90
|
+
# ==> /application/model?name=John%20Smith&address=http%3A%2F%2Fjohn.smith.com
|
91
|
+
#
|
92
|
+
# url_for :action => :show, :id => '{12}', :fragment => "an-anchor"
|
93
|
+
# ==> /application/model/{12}/show#an-anchor
|
94
|
+
#
|
95
|
+
def url_for(params = {})
|
96
|
+
return params.to_s if params.is_a? String or params.is_a? Symbol
|
97
|
+
return '/' if not params.is_a? Hash or params.nil?
|
98
|
+
|
99
|
+
application = params[:application] || @request['application']
|
100
|
+
model = params[:controller] || params[:model] || @request['model']
|
101
|
+
action = params[:action].nil? ? nil : params[:action].to_s
|
102
|
+
id = params[:id].nil? ? nil : params[:id].to_s
|
103
|
+
query = query_to_s(params[:query])
|
104
|
+
fragment = params[:fragment].nil? ? '' : '#' + params[:fragment]
|
105
|
+
|
106
|
+
amurl = '/' + application.to_s + '/' + model.to_s
|
107
|
+
|
108
|
+
return amurl + query + fragment if action.nil? or action == 'create' or action == 'index'
|
109
|
+
return amurl +'/'+ (id.nil? ? action : id + '/' + action) + query + fragment
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
def query_to_s(query)
|
114
|
+
return '' if query.nil?
|
115
|
+
qstring = '?'
|
116
|
+
query.each do |key,value|
|
117
|
+
qstring += '&' if qstring.length > 1
|
118
|
+
qstring += Rho::RhoSupport.url_encode(key.to_s) + '=' + Rho::RhoSupport.url_encode(value.to_s)
|
119
|
+
end
|
120
|
+
qstring
|
121
|
+
end
|
122
|
+
|
123
|
+
# def url_encode(s)
|
124
|
+
# s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
|
125
|
+
# sprintf("%%%02X", $&.unpack("C")[0])
|
126
|
+
# }
|
127
|
+
# end
|
128
|
+
|
129
|
+
end # RhoController
|
130
|
+
end # Rho
|
data/lib/rho.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rho/rho'
|
data/lib/rhodes.rb
ADDED
data/lib/rhoframework.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
begin
|
2
|
+
require 'rational'
|
3
|
+
require 'date/format'
|
4
|
+
require 'time'
|
5
|
+
|
6
|
+
require 'sqlite3/constants'
|
7
|
+
require 'sqlite3/errors'
|
8
|
+
require 'sqlite3/pragmas'
|
9
|
+
|
10
|
+
require 'sqlite3/resultset'
|
11
|
+
require 'sqlite3/statement'
|
12
|
+
|
13
|
+
require 'date'
|
14
|
+
require 'sqlite3/translator'
|
15
|
+
|
16
|
+
require 'sqlite3/value'
|
17
|
+
|
18
|
+
require 'sqlite3/database'
|
19
|
+
require 'rhom/rhom_db_adapter'
|
20
|
+
|
21
|
+
require 'rhom/rhom_object'
|
22
|
+
require 'rhofsconnector'
|
23
|
+
|
24
|
+
require 'rhom/rhom_object_factory'
|
25
|
+
|
26
|
+
require 'rhom/rhom'
|
27
|
+
require 'rhom'
|
28
|
+
|
29
|
+
require 'rho/rhoapplication'
|
30
|
+
|
31
|
+
require 'rho/rho'
|
32
|
+
require 'rho'
|
33
|
+
|
34
|
+
puts 'RHO loaded'
|
35
|
+
Rho::RHO.new
|
36
|
+
rescue Exception => e
|
37
|
+
puts e.message
|
38
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rho/rhofsconnector.rb'
|
data/lib/rhom/rhom.rb
ADDED
@@ -0,0 +1,58 @@
|
|
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
|
+
if defined? RHO_ME
|
25
|
+
require 'rhom/rhom_db_adapterME'
|
26
|
+
else
|
27
|
+
require 'rhom/rhom_db_adapter'
|
28
|
+
end
|
29
|
+
|
30
|
+
module Rhom
|
31
|
+
TABLE_NAME = 'object_values'
|
32
|
+
|
33
|
+
class Rhom
|
34
|
+
include RhomObject
|
35
|
+
attr_accessor :factory
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@factory = RhomObjectFactory.new
|
39
|
+
end
|
40
|
+
|
41
|
+
class << Rhom
|
42
|
+
def client_id
|
43
|
+
c_id = ::Rhom::RhomDbAdapter::select_from_table('client_info','client_id')[0]
|
44
|
+
c_id.nil? ? nil : c_id['client_id']
|
45
|
+
end
|
46
|
+
|
47
|
+
def database_full_reset
|
48
|
+
::Rhom::RhomDbAdapter::delete_all_from_table(TABLE_NAME)
|
49
|
+
::Rhom::RhomDbAdapter::delete_all_from_table('client_info')
|
50
|
+
end
|
51
|
+
|
52
|
+
def database_full_reset_and_logout
|
53
|
+
database_full_reset
|
54
|
+
SyncEngine::logout
|
55
|
+
end
|
56
|
+
end #class methods
|
57
|
+
end # Rhom
|
58
|
+
end # Rhom
|
@@ -0,0 +1,185 @@
|
|
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
|
+
#$:.unshift(File.join(File.dirname(__FILE__), '../../'))
|
21
|
+
require 'sqlite3/database'
|
22
|
+
|
23
|
+
module Rhom
|
24
|
+
class RhomDbAdapter
|
25
|
+
|
26
|
+
@@database = nil
|
27
|
+
|
28
|
+
class << self
|
29
|
+
|
30
|
+
# maintains a single database connection
|
31
|
+
def open(dbfile=nil)
|
32
|
+
#puts "DB name = " + dbfile.inspect
|
33
|
+
unless @@database or dbfile.nil?
|
34
|
+
@@database = SQLite3::Database.new(dbfile)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# closes the database if and only if it is open
|
39
|
+
def close
|
40
|
+
if @@database and not @@database.closed?
|
41
|
+
@@database.close
|
42
|
+
@@database = nil
|
43
|
+
else
|
44
|
+
return false
|
45
|
+
end
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
|
49
|
+
# execute a sql statement
|
50
|
+
# optionally, disable the factory processing
|
51
|
+
# which returns the result array directly
|
52
|
+
def execute_sql(sql=nil)
|
53
|
+
result = []
|
54
|
+
if sql
|
55
|
+
#puts 'query is ' + sql
|
56
|
+
# Make sure we lock the sync engine's mutex
|
57
|
+
# before we perform a database transaction.
|
58
|
+
# This prevents concurrency issues.
|
59
|
+
begin
|
60
|
+
SyncEngine::lock_sync_mutex
|
61
|
+
# execute sql statement inside of transaction
|
62
|
+
# result is returned as an array of hashes
|
63
|
+
@@database.transaction unless @@database.transaction_active?
|
64
|
+
@@database.results_as_hash = true
|
65
|
+
result = @@database.execute sql
|
66
|
+
@@database.commit
|
67
|
+
SyncEngine::unlock_sync_mutex
|
68
|
+
rescue Exception => e
|
69
|
+
puts "exception when running query: #{e}"
|
70
|
+
# make sure we unlock even if there's an error!
|
71
|
+
SyncEngine::unlock_sync_mutex
|
72
|
+
end
|
73
|
+
end
|
74
|
+
#puts "returned #{result.length.to_s} records..."
|
75
|
+
result
|
76
|
+
end
|
77
|
+
|
78
|
+
# generates where clause based on hash
|
79
|
+
def where_str(condition)
|
80
|
+
cond = string_from_key_vals(condition," and")
|
81
|
+
cond[0..cond.length - 5]
|
82
|
+
end
|
83
|
+
|
84
|
+
# generates value clause based on hash
|
85
|
+
def vals_str(values)
|
86
|
+
vals = string_from_key_vals(values, ",")
|
87
|
+
vals[0..vals.length - 2]
|
88
|
+
end
|
89
|
+
|
90
|
+
# generates key/value list
|
91
|
+
def string_from_key_vals(values, delim)
|
92
|
+
vals = ""
|
93
|
+
values.each do |key,value|
|
94
|
+
val = value.is_a?(String) ? "'#{value}'" : "#{value}"
|
95
|
+
vals << " #{key} = #{val}#{delim}"
|
96
|
+
end
|
97
|
+
vals
|
98
|
+
end
|
99
|
+
|
100
|
+
# support for select statements
|
101
|
+
# this function takes table name, columns (as a comma-separated list),
|
102
|
+
# condition (as a hash), and params (as a hash)
|
103
|
+
# example usage is the following:
|
104
|
+
# select_from_table('object_values', '*', {"source_id"=>2,"update_type"=>'query'},
|
105
|
+
# {"order by"=>'object'})
|
106
|
+
# this would return all columns where source_id = 2 and update_type = 'query' ordered
|
107
|
+
# by the "object" column
|
108
|
+
def select_from_table(table=nil,columns=nil,condition=nil,params=nil)
|
109
|
+
query = nil
|
110
|
+
if table and columns and condition
|
111
|
+
if params and params['distinct']
|
112
|
+
query = "select distinct #{columns} from #{table} where #{where_str(condition)}"
|
113
|
+
elsif params and params['order by']
|
114
|
+
query = "select #{columns} from #{table} where #{where_str(condition)} \
|
115
|
+
order by #{params['order by']}"
|
116
|
+
else
|
117
|
+
query = "select #{columns} from #{table} where #{where_str(condition)}"
|
118
|
+
end
|
119
|
+
elsif table and columns
|
120
|
+
query = "select #{columns} from #{table}"
|
121
|
+
end
|
122
|
+
execute_sql query
|
123
|
+
end
|
124
|
+
|
125
|
+
# inserts a single row into the database
|
126
|
+
# takes the table name and values (hash) as arguments
|
127
|
+
# exmaple usage is the following:
|
128
|
+
# insert_into_table('object_values, {"source_id"=>1,"object"=>"some-object","update_type"=>'delete'})
|
129
|
+
# this would execute the following sql:
|
130
|
+
# insert into object_values (source_id,object,update_type) values (1,'some-object','delete');
|
131
|
+
def insert_into_table(table=nil,values=nil)
|
132
|
+
query = nil
|
133
|
+
cols = ""
|
134
|
+
vals = ""
|
135
|
+
if table and values
|
136
|
+
values.each do |key,val|
|
137
|
+
value = val.is_a?(Fixnum) ? "#{val}," : "'#{val}',"
|
138
|
+
cols << "#{key},"
|
139
|
+
vals << value
|
140
|
+
end
|
141
|
+
cols = cols[0..cols.length - 2]
|
142
|
+
vals = vals[0..vals.length - 2]
|
143
|
+
query = "insert into #{table} (#{cols}) values (#{vals})"
|
144
|
+
end
|
145
|
+
execute_sql query
|
146
|
+
end
|
147
|
+
|
148
|
+
# deletes rows from a table which satisfy condition (hash)
|
149
|
+
# example usage is the following:
|
150
|
+
# delete_from_table('object_values',{"object"=>"some-object"})
|
151
|
+
# this would execute the following sql:
|
152
|
+
# delete from object_values where object="some-object"
|
153
|
+
def delete_from_table(table=nil,condition=nil)
|
154
|
+
query = nil
|
155
|
+
if table and condition
|
156
|
+
query = "delete from #{table} where #{where_str(condition)}"
|
157
|
+
end
|
158
|
+
execute_sql query
|
159
|
+
end
|
160
|
+
|
161
|
+
# deletes all rows from a given table
|
162
|
+
def delete_all_from_table(table=nil)
|
163
|
+
query = nil
|
164
|
+
if table
|
165
|
+
query = "delete from #{table}"
|
166
|
+
end
|
167
|
+
execute_sql query
|
168
|
+
end
|
169
|
+
|
170
|
+
# updates values (hash) in a given table which satisfy condition (hash)
|
171
|
+
# example usage is the following:
|
172
|
+
# update_into_table('object_values',{"value"=>"Electronics"},{"object"=>"some-object", "attrib"=>"industry"})
|
173
|
+
# this executes the following sql:
|
174
|
+
# update table object_values set value='Electronics' where object='some-object' and attrib='industry';
|
175
|
+
def update_into_table(table=nil,values=nil,condition=nil)
|
176
|
+
query = nil
|
177
|
+
vals = values.nil? ? nil : vals_str(values)
|
178
|
+
if table and condition and vals
|
179
|
+
query = "update #{table} set #{vals} where #{where_str(condition)}"
|
180
|
+
end
|
181
|
+
execute_sql query
|
182
|
+
end
|
183
|
+
end # class methods
|
184
|
+
end # RhomDbAdapter
|
185
|
+
end # Rhom
|
@@ -0,0 +1,93 @@
|
|
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
|
+
|
27
|
+
class << self
|
28
|
+
def open(dbfile=nil)
|
29
|
+
puts "DB name = " + dbfile.inspect
|
30
|
+
unless @@database or dbfile.nil?
|
31
|
+
db = DbAdapter.new(Rhodes::DBVERSION)
|
32
|
+
@@database = db
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def close
|
38
|
+
if @@database and not @@database.closed?
|
39
|
+
@@database.close
|
40
|
+
@@database = nil
|
41
|
+
else
|
42
|
+
return false
|
43
|
+
end
|
44
|
+
return true
|
45
|
+
end
|
46
|
+
|
47
|
+
# execute a sql statement
|
48
|
+
# optionally, disable the factory processing
|
49
|
+
# which returns the result array directly
|
50
|
+
def execute_sql
|
51
|
+
result = []
|
52
|
+
# Make sure we lock the sync engine's mutex
|
53
|
+
# before we perform a database transaction.
|
54
|
+
# This prevents concurrency issues.
|
55
|
+
begin
|
56
|
+
SyncEngine::lock_sync_mutex
|
57
|
+
result = yield
|
58
|
+
SyncEngine::unlock_sync_mutex
|
59
|
+
rescue Exception => e
|
60
|
+
puts "exception when running query: #{e}"
|
61
|
+
# make sure we unlock even if there's an error!
|
62
|
+
SyncEngine::unlock_sync_mutex
|
63
|
+
end
|
64
|
+
puts "returned #{result.length.to_s} records..."
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
def select_from_table(table=nil,columns=nil,condition=nil,params=nil)
|
69
|
+
execute_sql { @@database.selectFromTable(table, columns, condition, params) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def insert_into_table(table=nil,values=nil)
|
73
|
+
execute_sql { @@database.insertIntoTable(table, values) }
|
74
|
+
end
|
75
|
+
|
76
|
+
def delete_from_table(table=nil,condition=nil)
|
77
|
+
execute_sql { @@database.deleteFromTable(table, condition) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def delete_all_from_table(table=nil)
|
81
|
+
execute_sql { @@database.deleteAllFromTable(table) }
|
82
|
+
end
|
83
|
+
|
84
|
+
def update_into_table(table=nil,values=nil,condition=nil)
|
85
|
+
execute_sql { @@database.updateIntoTable(table, values, condition) }
|
86
|
+
end
|
87
|
+
end # class methods
|
88
|
+
end # RhomDbAdapter
|
89
|
+
end # Rhom
|
90
|
+
|
91
|
+
at_exit do
|
92
|
+
Rhom::RhomDbAdapter::close
|
93
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#
|
2
|
+
# rhom_object.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
|
+
module Rhom
|
21
|
+
module RhomObject
|
22
|
+
# defines a method at runtime for the
|
23
|
+
# dynamically created class
|
24
|
+
|
25
|
+
|
26
|
+
# we override method_missing here so that instance variables,
|
27
|
+
# when retrieved or set, are added to the object
|
28
|
+
def method_missing(name, *args)
|
29
|
+
unless name == Fixnum
|
30
|
+
varname = name.to_s.gsub(/=/,"")
|
31
|
+
setting = (name.to_s =~ /=/)
|
32
|
+
inst_var = nil
|
33
|
+
|
34
|
+
if setting
|
35
|
+
inst_var = instance_variable_set( "@#{varname}", args[0] )
|
36
|
+
else
|
37
|
+
inst_var = instance_variable_get( "@#{varname}" )
|
38
|
+
end
|
39
|
+
|
40
|
+
inst_var
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def remove_var(name)
|
45
|
+
remove_instance_variable("@#{name}")
|
46
|
+
end
|
47
|
+
|
48
|
+
def strip_braces(str=nil)
|
49
|
+
str ? str.gsub(/\{/,"").gsub(/\}/,"") : nil
|
50
|
+
end
|
51
|
+
|
52
|
+
# use djb hash function to generate temp object id
|
53
|
+
def djb_hash(str, len)
|
54
|
+
hash = 5381
|
55
|
+
for i in (0..len)
|
56
|
+
hash = ((hash << 5) + hash) + str[i].to_i
|
57
|
+
end
|
58
|
+
return hash
|
59
|
+
end
|
60
|
+
|
61
|
+
def extract_options(arr=[])
|
62
|
+
arr.last.is_a?(::Hash) ? arr.pop : {}
|
63
|
+
end
|
64
|
+
|
65
|
+
def method_name_reserved?(method)
|
66
|
+
method =~ /\bobject\b|\bsource_id\b|\bupdate_type\b|\battrib_type\b|\btype\b|\bset_notification\b|\bclear_notification\b/
|
67
|
+
end
|
68
|
+
end # RhomObject
|
69
|
+
end # Rhom
|