lib/framework/rhom/rhom_object_factory.rb in rhodes-1.5.5 vs lib/framework/rhom/rhom_object_factory.rb in rhodes-2.0.0.beta1
- old
+ new
@@ -61,11 +61,11 @@
end
end
end
def to_s
- @vars.to_s if @vars
+ @vars.to_s if @vars
end
def method_missing(name, *args)
unless name == Fixnum
varname = name.to_s.gsub(/\=/,"")
@@ -77,32 +77,124 @@
end
end
end
class << self
-
+
+ def metadata
+ db = ::Rho::RHO.get_src_db(get_source_name)
+ result = db.select_from_table('sources', 'metadata', {"name"=>get_source_name} )
+ if result && result.length > 0 && result[0]['metadata']
+ return Rho::JSON.parse(result[0]['metadata'])
+ else
+ return nil
+ end
+
+ end
+
def count
- #SyncEngine.lock_sync_mutex
- res = ::Rhom::RhomDbAdapter.select_from_table('object_values','object', {"source_id"=>get_source_id}, {"distinct"=>true}).length
- #SyncEngine.unlock_sync_mutex
+ db = ::Rho::RHO.get_src_db(get_source_name)
+
+ if is_schema_source()
+ res = db.select_from_table(get_schema_table_name(),'object').length
+ else
+ res = db.select_from_table('object_values','object', {"source_id"=>get_source_id}, {"distinct"=>true}).length
+ end
+
res
end
def backend_refresh_time
- result = ::Rhom::RhomDbAdapter::select_from_table('sources', 'backend_refresh_time', {"source_id"=>get_source_id} )
+ db = ::Rho::RHO.get_src_db(get_source_name)
+ result = db.select_from_table('sources', 'backend_refresh_time', {"source_id"=>get_source_id} )
nTime = 0
if result && result.length > 0 && result[0]['backend_refresh_time']
nTime = result[0]['backend_refresh_time'].to_i
end
Time.at(nTime)
end
+
+ def get_source_name
+ self.name.to_s
+ end
+ def is_schema_source
+ Rho::RhoConfig.sources[get_source_name]['schema'] != nil
+ end
+ def get_schema_table_name
+ get_source_name()
+ end
+ def get_values_table_name
+ is_schema_source() ? get_schema_table_name() : 'object_values'
+ end
+
def get_source_id
- Rho::RhoConfig.sources[self.name.to_s]['source_id'].to_s
+ Rho::RhoConfig.sources[get_source_name]['source_id'].to_s
end
+ def convertConditionToStr(cond, op, condition_hash)
+ if cond.is_a?(String)
+ return cond
+ end
+
+ if cond.is_a?(Array)
+
+ if ( op )
+ condition_str = ""
+
+ cond.each do |item|
+ condition_str += ' ' + op + ' ' if condition_str.length() > 0
+ condition_str += '(' + convertConditionToStr(item[:conditions], item[:op], condition_hash) + ')'
+ end
+ else
+ condition_str = cond[0].split(/\?/).each_with_index { |param,i|
+ param << cond[i+1].to_s
+ }.join(' ').to_s
+ end
+
+ puts "condition_str : #{condition_str}"
+ return condition_str
+ end
+
+ bSimpleHash = true
+ condition_str = ""
+ cond.each do |key, value|
+ if key.is_a?(Hash)
+ bSimpleHash = false
+
+ val_op = key[:op] if key[:op]
+ val_func = key[:func] if key[:func]
+ attrib_name = key[:name] if key[:name]
+
+ condition_str += ' ' + op + ' ' if condition_str.length() > 0
+ if ( val_func && val_func.length() > 0 )
+ condition_str += val_func + "(\"" + attrib_name + "\")"
+ else
+ condition_str += "\"" + attrib_name + "\""
+ end
+ condition_str += ' '
+
+ if val_op == 'IN' or val_op == 'in'
+ svalue = ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(value)
+ condition_str += val_op + ' ( ' + svalue[1,svalue.length()-2] + ' )'
+ else
+ condition_str += val_op + ' ' + ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(value)
+ end
+
+ end
+
+ end
+
+ if bSimpleHash
+ condition_hash = cond
+ condition_str = ::Rhom::RhomDbAdapter.where_str(condition_hash) if condition_hash
+ end
+
+ condition_str
+ end
+
def makeCondWhere(key,value,srcid_value)
sql = ""
val_op = '='
val_func = ''
attrib_name = ''
@@ -115,11 +207,11 @@
end
sql << "attrib=" + ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(attrib_name)
sql << " AND source_id=" + srcid_value
sql << " AND " + (val_func.length > 0 ? val_func + "(value)" : "value") + ' '
-
+
if val_op == 'IN' or val_op == 'in'
svalue = ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(value)
sql << val_op + ' ( ' + svalue[1,svalue.length()-2] + ' )'
else
sql << val_op + ' ' + ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(value)
@@ -144,20 +236,21 @@
if !order_attr
strLimit = " LIMIT " + limit.to_s + " OFFSET " + offset.to_s if limit && offset && condition_hash.length <= 1 && nulls_cond.length == 0
end
puts "non-null select start"
+ db = ::Rho::RHO.get_src_db(get_source_name)
listObjs = []
if op == 'OR' && condition_hash.length > 1
mapObjs = {}
bStop = false
condition_hash.each do |key,value|
sql = ""
sql << "SELECT object,attrib,value FROM object_values WHERE \n"
sql << makeCondWhere(key,value,srcid_value)
- resObjs = ::Rhom::RhomDbAdapter.execute_sql(sql)
+ resObjs = db.execute_sql(sql)
resObjs.each do |rec|
next if mapObjs[ rec['object'] ]
mapObjs[ rec['object'] ] = 1
listObjs << rec
@@ -182,11 +275,11 @@
sql << "SELECT distinct object FROM object_values WHERE \n"
sql << "source_id=" + srcid_value
sql << strLimit if strLimit
end
- listObjs = ::Rhom::RhomDbAdapter.execute_sql(sql)
+ listObjs = db.execute_sql(sql)
end
puts "non-null select end : #{listObjs.length}"
nIndex = -1
res = []
@@ -201,11 +294,11 @@
sql << "SELECT value FROM object_values WHERE \n"
sql << "object=?" # + obj_value
sql << " AND attrib=?" # + ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(key)
sql << " AND source_id=?" # + srcid_value
- attrVal = ::Rhom::RhomDbAdapter.execute_sql(sql,
+ attrVal = db.execute_sql(sql,
obj['object'], key, get_source_id)
#puts 'attrVal: ' + attrVal.inspect if attrVal
if attrVal && attrVal.length>0 && attrVal[0]['value']
bSkip = true
break
@@ -301,11 +394,12 @@
#attribs = SyncEngine.get_src_attrs(nSrcID)
end
srcid_value = ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(get_source_id)
- ::Rhom::RhomDbAdapter.start_transaction
+ db = ::Rho::RHO.get_src_db(get_source_name)
+ db.start_transaction
begin
listObjs = []
if condition_hash.is_a?(Hash)
listObjs = find_objects(condition_hash, op, limit, offset, order_attr)
else
@@ -342,23 +436,23 @@
values << obj['object']
values << get_source_id
end
- listAttrs = sql.length > 0 ? ::Rhom::RhomDbAdapter.execute_sql(sql,values) : []
+ listAttrs = sql.length > 0 ? db.execute_sql(sql,values) : []
new_obj = self.new
new_obj.vars.merge!({:object=>"{#{obj['object']}}"})
if attribs && obj['attrib']
- new_obj.vars.merge!( {obj['attrib'].to_sym()=>obj['value'] })
+ new_obj.vars.merge!( {obj['attrib'].to_sym()=>obj['value'] }) if obj['value']
end
listAttrs.each do |attrValHash|
attrName = attrValHash['attrib']
attrVal = attrValHash['value']
- new_obj.vars.merge!( { attrName.to_sym()=>attrVal } )
+ new_obj.vars.merge!( { attrName.to_sym()=>attrVal } ) if attrVal
#nonExistAttrs.delete(attrName) if nonExistAttrs
end
#if nonExistAttrs
@@ -370,11 +464,11 @@
ret_list << new_obj
break if !order_attr && limit && ret_list.length >= limit
end
end
ensure
- ::Rhom::RhomDbAdapter.commit
+ db.commit
end
if order_attr
ret_list.sort! { |x,y|
vx = x.vars[order_attr.to_sym()]
@@ -397,37 +491,46 @@
return ret_list.length > 0 ? ret_list[0] : nil
end
ret_list
end
-
+
# retrieve a single record if object id provided, otherwise return
# full list corresponding to factory's source id
def find(*args, &block)
raise ::Rhom::RecordNotFound if args[0].nil? or args.length == 0
+
puts "Inside find: args - #{args.inspect}"
+
ret_list = []
conditions = {}
where_cond = nil
limit = nil
offset = nil
# first find all query objects
if args.first == :all || args.first == :first || args.first == :count
- #!args[1] || !args[1][:conditions] ||
- if args[1] && args[1][:conditions] && args[1][:conditions].is_a?(Hash)
- return find_bycondhash(args,&block)
- end
+ if !is_schema_source()
+ #!args[1] || !args[1][:conditions] ||
+ if args[1] && args[1][:conditions] && args[1][:conditions].is_a?(Hash)
+ return find_bycondhash(args,&block)
+ end
- if args[1] && args[1][:op] && args[1][:conditions] && args[1][:conditions].is_a?(Array)
- return find_bycondhash(args,&block)
- end
-
- where_cond = {"source_id"=>get_source_id}
+ if args[1] && args[1][:op] && args[1][:conditions] && args[1][:conditions].is_a?(Array)
+ return find_bycondhash(args,&block)
+ end
+
+ where_cond = {"source_id"=>get_source_id}
+ end
elsif args.first.is_a?(String)
- where_cond = {"object"=>strip_braces(args.first.to_s),"source_id"=>get_source_id}
+ if !is_schema_source()
+ where_cond = {"object"=>strip_braces(args.first.to_s),"source_id"=>get_source_id}
+ else
+ where_cond = {"object"=>strip_braces(args.first.to_s)}
+ end
+
limit = 1
offset = 0
end
if args.first == :count && !args[1]
@@ -440,21 +543,12 @@
select_arr = nil
condition_str = nil
order_dir=""
nSrcID = get_source_id.to_i
if args[1]
- if args[1][:conditions]
- condition_hash = args[1][:conditions] if args[1][:conditions].is_a?(Hash)
- # conditions are a string
- condition_str = args[1][:conditions] if args[1][:conditions].is_a?(String)
- # conditions are an array
- if args[1][:conditions].is_a? (Array)
- condition_str = args[1][:conditions][0].split(/\?/).each_with_index { |param,i|
- param << args[1][:conditions][i+1].to_s
- }.join(' ').to_s
- end
- end
+ condition_str = convertConditionToStr(args[1][:conditions], args[1][:op], condition_hash) if args[1][:conditions]
+
if args[1][:per_page] #and args[1][:offset]
limit = args[1][:per_page].to_i
offset = args[1][:offset] ? args[1][:offset].to_i : 0
end
@@ -480,41 +574,63 @@
# | 3560c0a0-ef58-2f40-68a5-48f39f63741b |A.G. Parr PLC 37862 |Entertainment|
if select_arr
attribs = select_arr
attribs = attribs | condition_hash.keys if condition_hash
else
- attribs = SyncEngine.get_src_attrs(nSrcID)
+ if !is_schema_source()
+ attribs = SyncEngine.get_src_attrs(Rho::RhoConfig.sources[get_source_name]['partition'].to_s, nSrcID)
+ else
+ attribs = '*'
+ end
end
if attribs and attribs.length > 0
sql = ""
- sql << "SELECT * FROM (\n" if condition_hash or condition_str
- sql << "SELECT object, \n"
- #attribs.reject! {|attrib| select_arr.index(attrib).nil?} if select_arr
- attribs.each do |attrib|
- unless attrib.nil? or attrib.length == 0 or ::Rhom::RhomObject.method_name_reserved?(attrib)
- sql << "MAX(CASE WHEN attrib = '#{attrib}' THEN value ELSE NULL END) AS \'#{attrib}\',\n"
- end
- end
- sql.chomp!
- sql.chop!
- sql << " FROM object_values ov \n"
- sql << "where " + ::Rhom::RhomDbAdapter.where_str(where_cond) + "\n" if where_cond and where_cond.length > 0
- sql << "group by object\n"
- sql << "order by \"#{order_attr}\" " + order_dir if !block_given? && order_attr
- sql << ") WHERE " + ::Rhom::RhomDbAdapter.where_str(condition_hash) if condition_hash
- sql << ") WHERE " + condition_str if condition_str
- sql << strLimit if strLimit
-
+
+ if !is_schema_source()
+ sql << "SELECT * FROM (\n" if condition_hash or condition_str
+ sql << "SELECT object, \n"
+ #attribs.reject! {|attrib| select_arr.index(attrib).nil?} if select_arr
+ attribs.each do |attrib|
+ unless attrib.nil? or attrib.length == 0 or ::Rhom::RhomObject.method_name_reserved?(attrib)
+ sql << "MAX(CASE WHEN attrib = '#{attrib}' THEN value ELSE NULL END) AS \'#{attrib}\',\n"
+ end
+ end
+ sql.chomp!
+ sql.chop!
+ sql << " FROM object_values ov \n"
+ sql << "where " + ::Rhom::RhomDbAdapter.where_str(where_cond) + "\n" if where_cond and where_cond.length > 0
+ sql << "group by object\n"
+ sql << "order by \"#{order_attr}\" " + order_dir if !block_given? && order_attr
+ #sql << ") WHERE " + ::Rhom::RhomDbAdapter.where_str(condition_hash) if condition_hash
+ sql << ") WHERE " + condition_str if condition_str
+ sql << strLimit if strLimit
+ else
+ attribs = attribs.join(',') if attribs.is_a?(Array)
+
+ sql << "SELECT #{attribs} FROM #{get_schema_table_name}"
+ if where_cond and where_cond.length > 0
+ sql << " WHERE " + ::Rhom::RhomDbAdapter.where_str(where_cond)
+ else
+ sql << " WHERE " + ::Rhom::RhomDbAdapter.where_str(condition_hash) if condition_hash
+ sql << " WHERE " + condition_str if condition_str
+ end
+
+ sql << " order by \"#{order_attr}\" " + order_dir if !block_given? && order_attr
+ sql << strLimit if strLimit
+ end
+
puts "Database query start"
- list = ::Rhom::RhomDbAdapter.execute_sql(sql)
+
+ db = ::Rho::RHO.get_src_db(get_source_name)
+ list = db.execute_sql(sql)
puts "Database query end"
if args.first != :count
list.each do |rowhash|
# always return object field with surrounding '{}'
- rowhash[:object] = "{#{rowhash['object']}}"
+ rowhash[:object] = "#{rowhash['object']}"
#rowhash[:source_id] = nSrcID
new_obj = self.new
#new_obj.vars.merge!(rowhash)
rowhash.each do |attrName, attrVal|
@@ -562,11 +678,12 @@
callbackParams += '&' + "search_params[#{Rho::RhoSupport.url_encode(key)}]" + '=' + Rho::RhoSupport.url_encode(value)
end
end
#set_notification(args[:callback], args[:callback_param]) if args[:callback]
- SyncEngine.dosearch_source(get_source_id.to_i(), args[:from] ? args[:from] : 'search',
+ src_ar = [self.name.to_s]
+ SyncEngine.dosearch(src_ar, args[:from] ? args[:from] : 'search',
searchParams, args[:sync_changes] ? args[:sync_changes] : false, args[:progress_step] ? args[:progress_step] : -1,
args[:callback], callbackParams )
end
def sync(callback=nil, callback_data="", show_status_popup=nil)
@@ -601,61 +718,49 @@
def clear_notification
SyncEngine.clear_notification(get_source_id.to_i)
end
- def ask(question)
- tmp_obj = self.new(:object =>djb_hash("#{question}#{rand.to_s}", 10).to_s)
- if question
- begin
- ::Rhom::RhomDbAdapter.start_transaction
-
- # We only support one ask at a time!
- result = ::Rhom::RhomDbAdapter.select_from_table('changed_values', 'object', {"source_id"=>get_source_id, "update_type"=>'ask'})
- if result && result.length > 0
- ::Rhom::RhomDbAdapter.delete_from_table('changed_values', {"source_id"=>get_source_id, "update_type"=>'ask', "sent"=>0 })
- ::Rhom::RhomDbAdapter.delete_from_table('object_values', {"object"=>result[0]['object'], "attrib"=>'question', "source_id"=>get_source_id} )
- end
-
- ::Rhom::RhomDbAdapter.insert_into_table('changed_values', {"source_id"=>get_source_id, "object"=>tmp_obj.object, "attrib"=>'question', "value"=>Rho::RhoSupport.url_encode(question), "update_type"=>'ask'} )
- ::Rhom::RhomDbAdapter.insert_into_table('object_values', {"source_id"=>get_source_id, "object"=>tmp_obj.object, "attrib"=>'question', "value"=>Rho::RhoSupport.url_encode(question)} )
-
- ::Rhom::RhomDbAdapter.commit
-
- rescue Exception => e
- puts 'Ask Exception: ' + e.inspect
- ::Rhom::RhomDbAdapter.rollback
- end
-
- SyncEngine.dosync
- end
- end
-
# deletes all records matching conditions (optionally nil)
def delete_all(conditions=nil)
+ db = ::Rho::RHO.get_src_db(get_source_name)
+ tableName = is_schema_source() ? get_schema_table_name : 'object_values'
begin
- ::Rhom::RhomDbAdapter.start_transaction
+ db.start_transaction
if conditions
- del_conditions = get_conditions_hash(conditions[:conditions])
-
# find all relevant objects, then delete them
- del_objects = ::Rhom::RhomDbAdapter.select_from_table('object_values', 'object', del_conditions.merge!({"source_id"=>get_source_id}), {"distinct"=>true})
- del_objects.each do |obj|
- ::Rhom::RhomDbAdapter.delete_from_table('object_values', {'object'=>obj['object']})
- ::Rhom::RhomDbAdapter.delete_from_table('changed_values', {'object'=>obj['object']})
+ if is_schema_source()
+ del_conditions = conditions[:conditions]
+ del_objects = db.select_from_table(tableName, 'object', del_conditions)
+ else
+ del_conditions = get_conditions_hash(conditions[:conditions])
+ del_objects = db.select_from_table(tableName, 'object', del_conditions.merge!({"source_id"=>get_source_id}), {"distinct"=>true})
end
+
+ puts "del_objects : #{del_objects}"
+ del_objects.each do |obj|
+ db.delete_from_table(tableName, {'object'=>obj['object']})
+ db.delete_from_table('changed_values', {'object'=>obj['object']})
+ end
else
- ::Rhom::RhomDbAdapter.delete_from_table('object_values', {"source_id"=>get_source_id})
- ::Rhom::RhomDbAdapter.delete_from_table('changed_values', {"source_id"=>get_source_id})
+ if is_schema_source()
+ db.delete_all_from_table(tableName)
+ else
+ db.delete_from_table(tableName, {"source_id"=>get_source_id})
+ end
+
+ db.delete_from_table('changed_values', {"source_id"=>get_source_id})
#TODO: add delete all update_type
end
- ::Rhom::RhomDbAdapter.commit
+ db.commit
rescue Exception => e
puts 'delete_all Exception: ' + e.inspect
- ::Rhom::RhomDbAdapter.rollback
+ db.rollback
+
+ raise
end
end
private
@@ -676,100 +781,145 @@
# if app server does not support oo in inserts.
# app client should check this method before update or delete
# overwise all modifications of unconfirmed created item will be lost
def can_modify
- obj = self.inst_strip_braces(self.object)
- result = ::Rhom::RhomDbAdapter.execute_sql("SELECT object FROM changed_values WHERE source_id=? and object=? and sent>1 LIMIT 1 OFFSET 0", get_inst_source_id().to_i(), obj )
+ db = ::Rho::RHO.get_src_db(get_inst_source_name)
+ obj = self.object #self.inst_strip_braces(self.object)
+ result = db.execute_sql("SELECT object FROM changed_values WHERE source_id=? and object=? and sent>1 LIMIT 1 OFFSET 0", get_inst_source_id().to_i(), obj )
return !(result && result.length > 0)
end
def self.changed?
- result = ::Rhom::RhomDbAdapter.execute_sql("SELECT object FROM changed_values WHERE source_id=? LIMIT 1 OFFSET 0", get_source_id().to_i )
+ db = ::Rho::RHO.get_src_db(get_source_name)
+ result = db.execute_sql("SELECT object FROM changed_values WHERE source_id=? LIMIT 1 OFFSET 0", get_source_id().to_i )
return result && result.length > 0
end
def changed?
- obj = self.inst_strip_braces(self.object)
- result = ::Rhom::RhomDbAdapter.execute_sql("SELECT object FROM changed_values WHERE source_id=? and object=? LIMIT 1 OFFSET 0", get_inst_source_id().to_i(), obj )
+ db = ::Rho::RHO.get_src_db(get_inst_source_name)
+ obj = self.object #self.inst_strip_braces(self.object)
+ result = db.execute_sql("SELECT object FROM changed_values WHERE source_id=? and object=? LIMIT 1 OFFSET 0", get_inst_source_id().to_i(), obj )
return result && result.length > 0
end
# deletes the record from the viewable list as well as
# adding a delete record to the list of sync operations
def destroy
- obj = self.inst_strip_braces(self.object)
- update_type=self.get_update_type_by_source('delete')
+ obj = self.object #self.inst_strip_braces(self.object)
+ update_type='delete'
if obj
+ db = ::Rho::RHO.get_src_db(get_inst_source_name)
begin
- ::Rhom::RhomDbAdapter.start_transaction
+ tableName = is_inst_schema_source() ? get_inst_schema_table_name() : 'object_values'
+
+ db.start_transaction
+ #save list of attrs
+ attrsList = db.select_from_table(tableName, '*', {"object"=>obj})
+
# first delete the record from viewable list
- result = ::Rhom::RhomDbAdapter.delete_from_table('object_values', {"object"=>obj})
+ db.delete_from_table(tableName, {"object"=>obj})
- resUpdateType = ::Rhom::RhomDbAdapter.select_from_table('changed_values', 'update_type', {"object"=>obj, "update_type"=>'create', "sent"=>0})
+ resUpdateType = db.select_from_table('changed_values', 'update_type', {"object"=>obj, "update_type"=>'create', "sent"=>0})
if resUpdateType && resUpdateType.length > 0
update_type = nil
end
- ::Rhom::RhomDbAdapter.delete_from_table('changed_values', {"object"=>obj, "sent"=>0})
+ db.delete_from_table('changed_values', {"object"=>obj, "sent"=>0})
- if update_type
+ if update_type and attrsList and attrsList.length() > 0
# now add delete operation
- result = ::Rhom::RhomDbAdapter.insert_into_table('changed_values', {"source_id"=>self.get_inst_source_id, "object"=>obj, "update_type"=>update_type})
+ if is_inst_schema_source()
+ attrsList[0].each do |attrName, attrValue|
+ next if attrName == 'object'
+
+ db.insert_into_table('changed_values',
+ {"source_id"=>self.get_inst_source_id, "object"=>obj,
+ "attrib"=> attrName, "value"=>attrValue,
+ "update_type"=>update_type})
+ end
+ else
+ attrsList.each do |attrName|
+ db.insert_into_table('changed_values',
+ {"source_id"=>self.get_inst_source_id, "object"=>obj,
+ "attrib"=> attrName['attrib'], "value"=>attrName['value'],
+ "update_type"=>update_type})
+ end
+ end
end
- ::Rhom::RhomDbAdapter.commit
+ db.commit
rescue Exception => e
puts 'destroy Exception: ' + e.inspect
- ::Rhom::RhomDbAdapter.rollback
+ db.rollback
+
+ raise
end
end
true
end
+ def is_blob_attrib(db_partition, nSrcID,attrib_name)
+ SyncEngine.is_blob_attr(db_partition, nSrcID.to_i,attrib_name)
+ #return attrib_name == "image_uri"
+ end
+
# saves the current object to the database as a create type
def save
# iterate over each instance variable and insert create row to table
- obj = self.inst_strip_braces(self.object)
+ obj = self.object #self.inst_strip_braces(self.object)
nSrcID = self.get_inst_source_id
+ db = ::Rho::RHO.get_src_db(get_inst_source_name)
+ db_partition = Rho::RhoConfig.sources[get_inst_source_name]['partition'].to_s
+ tableName = is_inst_schema_source() ? get_inst_schema_table_name() : 'object_values'
+ isSchemaSrc = is_inst_schema_source()
begin
-
- ::Rhom::RhomDbAdapter.start_transaction
+ db.start_transaction
- result = ::Rhom::RhomDbAdapter.execute_sql("SELECT object FROM object_values WHERE object=? AND source_id=? LIMIT 1 OFFSET 0",obj,nSrcID)
+ if isSchemaSrc
+ result = db.execute_sql("SELECT object FROM #{tableName} WHERE object=? LIMIT 1 OFFSET 0",obj)
+ else
+ result = db.execute_sql("SELECT object FROM #{tableName} WHERE object=? AND source_id=? LIMIT 1 OFFSET 0",obj,nSrcID)
+ end
+
if result && result.length > 0
- resUpdateType = ::Rhom::RhomDbAdapter.select_from_table('changed_values', 'update_type', {"object"=>obj, "source_id"=>nSrcID, 'sent'=>0})
+ resUpdateType = db.select_from_table('changed_values', 'update_type', {"object"=>obj, "source_id"=>nSrcID, 'sent'=>0})
if resUpdateType && resUpdateType.length > 0
update_type = resUpdateType[0]['update_type']
else
- update_type = self.get_update_type_by_source('update')
+ update_type = 'update'
end
else
- update_type = self.get_update_type_by_source('create')
+ update_type = 'create'
end
self.vars.each do |key_a,value|
key = key_a.to_s
next if ::Rhom::RhomObject.method_name_reserved?(key)
- val = self.inst_strip_braces(value.to_s)
+ val = value.to_s #self.inst_strip_braces(value.to_s)
# add rows excluding object, source_id and update_type
fields = {"source_id"=>nSrcID,
"object"=>obj,
"attrib"=>key,
"value"=>val,
"update_type"=>update_type}
- fields = key == "image_uri" ? fields.merge!({"attrib_type" => "blob.file"}) : fields
-
- resValue = ::Rhom::RhomDbAdapter.select_from_table('object_values', 'value,id', {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID})
+ fields = is_blob_attrib(db_partition, nSrcID, key) ? fields.merge!({"attrib_type" => "blob.file"}) : fields
+ resValue = nil
+ if isSchemaSrc
+ resValue = db.select_from_table(tableName, key, {"object"=>obj})
+ else
+ resValue = db.select_from_table(tableName, 'value', {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID})
+ end
+
if resValue && resValue.length > 0
- oldValue = resValue[0]['value']
+ oldValue = isSchemaSrc ? resValue[0][key] : resValue[0]['value']
isModified = oldValue != val
if isModified && val && oldValue.nil? && val.to_s().length == 0
isModified = false
end
@@ -777,57 +927,66 @@
isModified = false
end
if isModified
- resUpdateType = ::Rhom::RhomDbAdapter.select_from_table('changed_values', 'update_type', {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID, 'sent'=>0})
+ resUpdateType = db.select_from_table('changed_values', 'update_type', {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID, 'sent'=>0})
if resUpdateType && resUpdateType.length > 0
fields['update_type'] = resUpdateType[0]['update_type']
- ::Rhom::RhomDbAdapter.delete_from_table('changed_values', {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID, "sent"=>0})
+ db.delete_from_table('changed_values', {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID, "sent"=>0})
end
- fields['main_id'] = resValue[0]['id']
- ::Rhom::RhomDbAdapter.insert_into_table('changed_values', fields)
- result = ::Rhom::RhomDbAdapter.update_into_table('object_values', {"value"=>val}, {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID})
+ db.insert_into_table('changed_values', fields)
+ if isSchemaSrc
+ db.update_into_table(tableName, {key=>val}, {"object"=>obj})
+ else
+ db.update_into_table(tableName, {"value"=>val}, {"object"=>obj, "attrib"=>key, "source_id"=>nSrcID})
+ end
end
else
- tmp_id = generate_id()
- fields['main_id'] = tmp_id
- ::Rhom::RhomDbAdapter.insert_into_table('changed_values', fields)
+ db.insert_into_table('changed_values', fields)
fields.delete("update_type")
- fields.delete("main_id")
- fields['id'] = tmp_id
- result = ::Rhom::RhomDbAdapter.insert_into_table('object_values', fields)
+ fields.delete("attrib_type")
+
+ if isSchemaSrc
+ db.insert_into_table(tableName, {key=>val, "object"=>obj})
+ else
+ db.insert_into_table(tableName, fields)
+ end
end
end
- ::Rhom::RhomDbAdapter.commit
+ db.commit
rescue Exception => e
puts 'save Exception: ' + e.inspect
- ::Rhom::RhomDbAdapter.rollback
+ db.rollback
+
+ raise
end
true
end
-
+
# updates the current record in the viewable list and adds
# a sync operation to update
def update_attributes(attrs)
- obj = self.inst_strip_braces(self.object)
- update_type=self.get_update_type_by_source('update')
+ obj = self.object #self.inst_strip_braces(self.object)
+ update_type='update'
nSrcID = self.get_inst_source_id
+ db = ::Rho::RHO.get_src_db(get_inst_source_name)
+ tableName = is_inst_schema_source() ? get_inst_schema_table_name() : 'object_values'
begin
- ::Rhom::RhomDbAdapter.start_transaction
+ db.start_transaction
attrs.each do |attrib,val|
attrib = attrib.to_s.gsub(/@/,"")
next if ::Rhom::RhomObject.method_name_reserved?(attrib)
old_val = self.send attrib.to_sym unless ::Rhom::RhomObject.method_name_reserved?(attrib)
# Don't save objects with braces to database
- new_val = self.inst_strip_braces(val.to_s)
+ new_val = val.to_s #self.inst_strip_braces(val.to_s)
isModified = old_val != new_val
if isModified && new_val && old_val.nil? && new_val.to_s().length == 0
isModified = false
end
@@ -837,57 +996,75 @@
# if the object's value doesn't match the database record
# then we procede with update
if isModified
# only one update at a time
- resUpdateType = ::Rhom::RhomDbAdapter.select_from_table('changed_values', 'update_type', {"object"=>obj, "source_id"=>nSrcID, 'sent'=>0})
+ resUpdateType = db.select_from_table('changed_values', 'update_type', {"object"=>obj, "source_id"=>nSrcID, 'sent'=>0})
if resUpdateType && resUpdateType.length > 0
update_type = resUpdateType[0]['update_type']
- ::Rhom::RhomDbAdapter.delete_from_table('changed_values', {"object"=>obj, "attrib"=>attrib, "source_id"=>nSrcID, "sent"=>0})
+ db.delete_from_table('changed_values', {"object"=>obj, "attrib"=>attrib, "source_id"=>nSrcID, "sent"=>0})
end
# add to syncengine queue
-
- result = ::Rhom::RhomDbAdapter.select_from_table('object_values', 'id', {"object"=>obj, "attrib"=>attrib, "source_id"=>nSrcID})
+
+ if is_inst_schema_source()
+ result = db.select_from_table(tableName, 'object', {"object"=>obj})
+
+ if result && result.length > 0
+ db.update_into_table(tableName, {attrib=>new_val}, {"object"=>obj})
+ else
+ db.insert_into_table(tableName, {"object"=>obj, attrib=>new_val})
+ end
+
+ else
+ result = db.select_from_table(tableName, 'source_id', {"object"=>obj, "attrib"=>attrib, "source_id"=>nSrcID})
+
+ if result && result.length > 0
+ db.update_into_table(tableName, {"value"=>new_val}, {"object"=>obj, "attrib"=>attrib, "source_id"=>nSrcID})
+ else
+ db.insert_into_table(tableName, {"source_id"=>self.get_inst_source_id, "object"=>obj, "attrib"=>attrib, "value"=>new_val})
+ end
+
+ end
+
if result && result.length > 0
- ::Rhom::RhomDbAdapter.update_into_table('object_values', {"value"=>new_val}, {"object"=>obj, "attrib"=>attrib, "source_id"=>nSrcID})
- ::Rhom::RhomDbAdapter.insert_into_table('changed_values', {"main_id"=>result[0]['id'], "source_id"=>nSrcID, "object"=>obj, "attrib"=>attrib, "value"=>new_val, "update_type"=>update_type})
+ db.insert_into_table('changed_values', {"source_id"=>nSrcID, "object"=>obj, "attrib"=>attrib, "value"=>new_val, "update_type"=>update_type})
else
- tmp_id = generate_id()
- result = ::Rhom::RhomDbAdapter.insert_into_table('object_values', {"id"=>tmp_id,"source_id"=>self.get_inst_source_id, "object"=>obj, "attrib"=>attrib, "value"=>new_val})
- ::Rhom::RhomDbAdapter.insert_into_table('changed_values', {"main_id"=>tmp_id, "source_id"=>nSrcID, "object"=>obj, "attrib"=>attrib, "value"=>new_val, "update_type"=>update_type})
+ db.insert_into_table('changed_values', {"source_id"=>nSrcID, "object"=>obj, "attrib"=>attrib, "value"=>new_val, "update_type"=>update_type})
end
# update in-memory object
self.vars[attrib.to_sym()] = new_val
end
end
- ::Rhom::RhomDbAdapter.commit
+ db.commit
rescue Exception => e
puts 'update_attributes Exception: ' + e.inspect
- ::Rhom::RhomDbAdapter.rollback
+ db.rollback
+
+ raise
end
true
end
+
+ def get_inst_source_name
+ self.class.name.to_s
+ end
def get_inst_source_id
- Rho::RhoConfig.sources[self.class.name.to_s]['source_id'].to_s
+ Rho::RhoConfig.sources[get_inst_source_name]['source_id'].to_s
end
-
- def get_update_type_by_source(update_type)
- source_type = Rho::RhoConfig.sources[self.class.name.to_s]['type']
- if source_type and source_type == "ask" and update_type == 'delete'
- nil
- elsif source_type and source_type == "ask"
- "query"
- else
- update_type
- end
+
+ def is_inst_schema_source
+ Rho::RhoConfig.sources[get_inst_source_name]['schema'] != nil
end
-
+ def get_inst_schema_table_name
+ get_inst_source_name()
+ end
+
def inst_strip_braces(str=nil)
str ? str.gsub(/\{/,"").gsub(/\}/,"") : nil
end
end)
end #unless
\ No newline at end of file