Permalink
Browse files

Refactored rhom objects to use hash for vars instead of instance vari…

…ables. Converted find method to generate rhom objects based on horizontal sql resultset [#702]
  • Loading branch information...
1 parent 4ba3168 commit 12e85386ebe4ff6da9386b58c4067d9bb375b21a @larsburgess larsburgess committed Jun 3, 2009
View
@@ -1,6 +1,8 @@
*~
*.iseq
.DS_Store
+rhodes/*/pkg/
+rhodes/rhodes-framework/spec/bin/
platform/iphone/build/*
platform/iphone/rhorubylib/build/*
platform/iphone/rhosynclib/build/*
Binary file not shown.
@@ -21,17 +21,10 @@
require 'rhom/rhom_object_factory'
require 'rhom/rhom_object'
-if defined? RHO_DBME
- require 'rhom/rhom_db_adapterME'
-else
- require 'rhom/rhom_db_adapter'
-end
+require 'rhom/rhom_db_adapter'
module Rhom
- TABLE_NAME = 'object_values'
-
class Rhom
- include RhomObject
attr_accessor :factory
def initialize
@@ -40,18 +33,18 @@ def initialize
class << Rhom
def client_id
- c_id = ::Rhom::RhomDbAdapter::select_from_table('client_info','client_id')[0]
+ c_id = RhomDbAdapter.select_from_table('client_info','client_id')[0]
c_id.nil? ? nil : c_id['client_id']
end
def database_full_reset
- ::Rhom::RhomDbAdapter::delete_all_from_table(TABLE_NAME)
- ::Rhom::RhomDbAdapter::delete_all_from_table('client_info')
+ RhomDbAdapter.delete_all_from_table('object_values')
+ RhomDbAdapter.delete_all_from_table('client_info')
end
def database_full_reset_and_logout
database_full_reset
- SyncEngine::logout
+ SyncEngine.logout
end
end #class methods
end # Rhom
@@ -17,8 +17,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
-#$:.unshift(File.join(File.dirname(__FILE__), '../../'))
-# require 'sqlite3/database'
require 'rhodes'
module Rhom
@@ -30,15 +28,14 @@ class << self
# maintains a single database connection
def open(dbfile=nil)
- #puts "DB name = " + dbfile.inspect
unless @@database or dbfile.nil?
@@database = SQLite3::Database.new(dbfile)
end
end
# closes the database if and only if it is open
def close
- if @@database #and not @@database.closed?
+ if @@database
@@database.close
@@database = nil
else
@@ -53,26 +50,21 @@ def close
def execute_sql(sql=nil)
result = []
if sql
- #puts 'query is ' + sql
+ puts "RhomDbAdapter: Executing query - #{sql}"
# Make sure we lock the sync engine's mutex
# before we perform a database transaction.
# This prevents concurrency issues.
begin
- SyncEngine::lock_sync_mutex
- # execute sql statement inside of transaction
- # result is returned as an array of hashes
- # @@database.transaction unless @@database.transaction_active?
- # @@database.results_as_hash = true
+ SyncEngine.lock_sync_mutex
result = @@database.execute sql
- # @@database.commit
- SyncEngine::unlock_sync_mutex
+ SyncEngine.unlock_sync_mutex
rescue Exception => e
puts "exception when running query: #{e}"
# make sure we unlock even if there's an error!
- SyncEngine::unlock_sync_mutex
+ SyncEngine.unlock_sync_mutex
end
end
- #puts "returned #{result.length.to_s} records..."
+ puts "result is: #{result.inspect}"
result
end
@@ -87,7 +79,6 @@ def where_str(condition,select_arr=nil)
if select_arr and select_arr.length > 0
where_str += " and attrib in (#{select_str(select_arr)})"
end
- #puts "where_str: #{where_str}" if where_str
where_str
end
@@ -212,5 +203,5 @@ def update_into_table(table=nil,values=nil,condition=nil)
end # Rhom
at_exit do
- Rhom::RhomDbAdapter::close
+ RhomDbAdapter.close
end
@@ -1,92 +0,0 @@
-#
-# rhom_db_adapter.rb
-# rhodes
-#
-# Copyright (C) 2008 Rhomobile, Inc. All rights reserved.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-#
-require 'rhodes'
-
-module Rhom
- class RhomDbAdapter
-
- @@database = nil
-
- class << self
- def open(dbfile=nil)
- puts "DB name = " + dbfile.inspect
- unless @@database or dbfile.nil?
- db = DbAdapter.new(Rhodes::DBVERSION)
- @@database = db
- end
- end
-
- def close
- if @@database and not @@database.closed?
- @@database.close
- @@database = nil
- else
- return false
- end
- return true
- end
-
- # execute a sql statement
- # optionally, disable the factory processing
- # which returns the result array directly
- def execute_sql
- result = []
- # Make sure we lock the sync engine's mutex
- # before we perform a database transaction.
- # This prevents concurrency issues.
- begin
- SyncEngine::lock_sync_mutex
- result = yield
- SyncEngine::unlock_sync_mutex
- rescue Exception => e
- puts "exception when running query: #{e}"
- # make sure we unlock even if there's an error!
- SyncEngine::unlock_sync_mutex
- end
- puts "returned #{result.length.to_s} records..."
- result
- end
-
- def select_from_table(table=nil,columns=nil,condition=nil,params=nil)
- execute_sql { @@database.selectFromTable(table, columns, condition, params) }
- end
-
- def insert_into_table(table=nil,values=nil)
- execute_sql { @@database.insertIntoTable(table, values) }
- end
-
- def delete_from_table(table=nil,condition=nil)
- execute_sql { @@database.deleteFromTable(table, condition) }
- end
-
- def delete_all_from_table(table=nil)
- execute_sql { @@database.deleteAllFromTable(table) }
- end
-
- def update_into_table(table=nil,values=nil,condition=nil)
- execute_sql { @@database.updateIntoTable(table, values, condition) }
- end
- end # class methods
- end # RhomDbAdapter
-end # Rhom
-
-at_exit do
- Rhom::RhomDbAdapter::close
-end
@@ -18,33 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
module Rhom
- module RhomObject
- # defines a method at runtime for the
- # dynamically created class
-
-
- # we override method_missing here so that instance variables,
- # when retrieved or set, are added to the object
- def method_missing(name, *args)
- unless name == Fixnum
- varname = name.to_s.gsub(/=/,"")
- setting = (name.to_s =~ /=/)
- inst_var = nil
-
- if setting
- inst_var = instance_variable_set( "@#{varname}", args[0] )
- else
- inst_var = instance_variable_get( "@#{varname}" )
- end
-
- inst_var
- end
- end
-
- def remove_var(name)
- remove_instance_variable("@#{name}")
- end
-
+ module RhomObject
def strip_braces(str=nil)
str ? str.gsub(/\{/,"").gsub(/\}/,"") : nil
end
@@ -59,7 +33,7 @@ def djb_hash(str, len)
end
def extract_options(arr=[])
- arr.last.is_a?(::Hash) ? arr.pop : {}
+ arr.last.is_a?(Hash) ? arr.pop : {}
end
@@reserved_names = {"object" => "1",
@@ -73,9 +47,6 @@ def extract_options(arr=[])
def method_name_reserved?(method)
@@reserved_names.has_key?(method)
- end
- #def method_name_reserved?(method)
- # method =~ /\bobject\b|\bsource_id\b|\bupdate_type\b|\battrib_type\b|\btype\b|\bset_notification\b|\bclear_notification\b/
- #end
+ end
end # RhomObject
end # Rhom
Oops, something went wrong.

0 comments on commit 12e8538

Please sign in to comment.