lib/framework/rhom/rhom_object_factory.rb in rhodes-2.2.4.beta.1 vs lib/framework/rhom/rhom_object_factory.rb in rhodes-2.2.5.beta.1
- old
+ new
@@ -86,35 +86,39 @@
@vars[name]
end
end
end
- @@metadata = {}
class << self
- def clean_cached_metadata
- src_name = get_source_name()
- @@metadata[src_name] = nil
- end
-
def metadata
src_name = get_source_name()
- return @@metadata[src_name] if @@metadata[src_name]
+ return Rho::RhoController.cached_metadata[src_name] if Rho::RhoController.cached_metadata.has_key?(src_name)
+
db = ::Rho::RHO.get_src_db(src_name)
result = db.select_from_table('sources', 'metadata', {"name"=>src_name} )
if result && result.length > 0 && result[0]['metadata']
- @@metadata[src_name] = Rho::JSON.parse(result[0]['metadata'])
+ Rho::RhoController.cached_metadata[src_name] = Rho::JSON.parse(result[0]['metadata'])
+ else
+ Rho::RhoController.cached_metadata[src_name] = nil
end
- @@metadata[src_name]
+ Rho::RhoController.cached_metadata[src_name]
end
def count
db = ::Rho::RHO.get_src_db(get_source_name)
if is_schema_source()
- res = db.select_from_table(get_schema_table_name(),'object').length
+ #res = db.select_from_table(get_schema_table_name(),'object').length
+ db_res = db.execute_sql("SELECT COUNT(*) FROM " + get_schema_table_name())
+ #puts "db_res : #{db_res}"
+ if db_res && db_res.length() > 0
+ res = db_res[0].values[0]
+ else
+ res = 0
+ end
else
res = db.select_from_table('object_values','object', {"source_id"=>get_source_id}, {"distinct"=>true}).length
end
res
@@ -274,11 +278,11 @@
end
return sql, vals
end
- def find_objects(condition_hash, op, limit, offset, order_attr)
+ def find_objects(condition_hash, op, limit, offset, order_attr, &block)
nulls_cond = {}
if op == 'AND'
condition_hash.each do |key,value|
if value.nil?
nulls_cond[key] = value
@@ -286,11 +290,11 @@
end
end
end
strLimit = nil
- if !order_attr
+ if !(block_given? || 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)
@@ -309,12 +313,12 @@
resObjs.each do |rec|
next if mapObjs[ rec['object'] ]
mapObjs[ rec['object'] ] = 1
listObjs << rec
-
- bStop = !order_attr && limit && offset && nulls_cond.length == 0 && listObjs.length >= offset + limit
+
+ bStop = !(block_given? || order_attr) && limit && offset && nulls_cond.length == 0 && listObjs.length >= offset + limit
break if bStop
end
break if bStop
end
@@ -355,11 +359,11 @@
nIndex = -1
res = []
listObjs.each do |obj|
nIndex += 1
- next if !order_attr && offset && nIndex < offset && !strLimit
+ next if !(block_given? || order_attr) && offset && nIndex < offset && !strLimit
bSkip = false
#obj_value = ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(obj['object'])
nulls_cond.each do |key,value|
sql = ""
@@ -381,15 +385,15 @@
end
res
end
- def find_objects_ex(condition_ar, op, limit, offset, order_attr)
+ def find_objects_ex(condition_ar, op, limit, offset, order_attr, &block)
mapObjs = {}
listObjs = []
condition_ar.each do |cond|
- res = find_objects(cond[:conditions], cond[:op], limit, offset, order_attr)
+ res = find_objects(cond[:conditions], cond[:op], limit, offset, order_attr, &block)
if listObjs.length() == 0
if condition_ar.length() > 1
res.each do |hash_attrs|
mapObjs[ hash_attrs['object'] ] = 1
@@ -421,11 +425,11 @@
listObjs
end
def find_bycondhash(args, &block)
- #puts 'find_bycondhash start'
+ #puts 'find_bycondhash start' + (block_given? ? 'with block' : "")
condition_hash = {}
select_arr = nil
limit = nil
offset = 0
@@ -440,11 +444,12 @@
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
select_arr = args[1][:select] if args[1][:select]
- order_dir = args[1][:orderdir].upcase() if args[1][:orderdir]
+
+ order_dir = args[1][:orderdir]
order_attr = args[1][:order]
op = args[1][:op].upcase if args[1][:op]
end
@@ -454,13 +459,18 @@
end
attribs = nil
if select_arr
attribs = select_arr
- if order_attr
+ if order_attr
order_attr_arr = []
- order_attr_arr.push(order_attr)
+ if order_attr.is_a?(Array)
+ order_attr_arr = order_attr
+ else
+ order_attr_arr.push(order_attr)
+ end
+
attribs = attribs | order_attr_arr
end
else
#attribs = SyncEngine.get_src_attrs(nSrcID)
end
@@ -470,13 +480,13 @@
db = ::Rho::RHO.get_src_db(get_source_name)
db.lock_db
begin
listObjs = []
if condition_hash.is_a?(Hash)
- listObjs = find_objects(condition_hash, op, limit, offset, order_attr)
+ listObjs = find_objects(condition_hash, op, limit, offset, order_attr, &block)
else
- listObjs = find_objects_ex(condition_hash, op, limit, offset, order_attr)
+ listObjs = find_objects_ex(condition_hash, op, limit, offset, order_attr, &block)
end
nCount = 0;
if args.first == :count
nCount = listObjs.length
@@ -536,28 +546,20 @@
# new_obj.vars.merge!( { attrName.to_sym()=>nil } )
# end
#end
ret_list << new_obj
- break if !order_attr && limit && ret_list.length >= limit
+ break if !(block_given? || order_attr) && limit && ret_list.length >= limit
end
end
ensure
db.unlock_db
end
- if order_attr
- ret_list.sort! { |x,y|
- vx = x.vars[order_attr.to_sym()]
- vy = y.vars[order_attr.to_sym()]
- res = vx && vy ? (block_given? ? yield(vx,vy): vx <=> vy) : 0
- res *= -1 if order_dir && order_dir == 'DESC'
- res
- }
- end
+ order_array(ret_list, order_attr,order_dir, &block)
- if order_attr && limit
+ if (block_given? || order_attr) && limit
ret_list = ret_list.slice(offset,limit)
end
#puts "find_bycondhash end: #{ret_list.length} objects"
@@ -568,10 +570,74 @@
end
ret_list
end
+ def order_array(ret_list, order_attr, order_dir, &block)
+ if order_attr
+ ret_list.sort! { |x,y|
+
+ res = 0
+ if order_attr.is_a?(Array)
+ order_attr.each_index { |i|
+ vx = x.vars[order_attr[i].to_sym()]
+ vy = y.vars[order_attr[i].to_sym()]
+ res = vx && vy ? (block_given? ? yield(vx,vy): vx <=> vy) : 0
+
+ dir = 'ASC'
+ if order_dir && order_dir.is_a?(Array) && i < order_dir.length()
+ dir = order_dir[i].upcase()
+ else
+ dir = order_dir.upcase() if order_dir && order_dir.is_a?(String)
+ end
+
+ res *= -1 if dir && dir == 'DESC'
+ break if res != 0
+ }
+ else
+ vx = x.vars[order_attr.to_sym()]
+ vy = y.vars[order_attr.to_sym()]
+ res = vx && vy ? (block_given? ? yield(vx,vy): vx <=> vy) : 0
+ res *= -1 if order_dir && order_dir == 'DESC'
+ end
+
+ res
+ }
+ elsif block_given?
+ ret_list.sort! { |x,y|
+ res = yield(x,y)
+ res *= -1 if order_dir && order_dir == 'DESC'
+ res
+ }
+ end
+
+ end
+
+ def make_sql_order(params)
+ order_attr = params[:order]
+ order_dir = params[:orderdir]
+
+ res = ""
+
+ if order_attr && order_attr.is_a?(Array)
+ order_attr.each_index do |i|
+ res += "," if i > 0
+
+ res += "\"#{order_attr[i]}\" "
+ if order_dir && order_dir.is_a?(Array) && i < order_dir.length()
+ res += order_dir[i].upcase()
+ else
+ res += order_dir && order_dir.is_a?(String) ? order_dir.upcase() : "ASC"
+ end
+ end
+ else
+ res = "\"#{order_attr}\" " + (order_dir ? order_dir.upcase() : "")
+ end
+
+ res
+ 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
@@ -627,21 +693,22 @@
limit = args[1][:per_page].to_i
offset = args[1][:offset] ? args[1][:offset].to_i : 0
end
select_arr = args[1][:select] if args[1][:select]
- order_dir = args[1][:orderdir].upcase() if args[1][:orderdir]
+ order_dir = args[1][:orderdir]
order_attr = args[1][:order]
+
end
if args.first == :first
limit = 1
offset = 0 unless offset
end
strLimit = nil
- if !(block_given? && order_attr)
+ if !block_given?
strLimit = " LIMIT " + limit.to_s + " OFFSET " + offset.to_s if limit && offset
end
# return horizontal resultset from database
# for example, an object that has attributes name,industry:
@@ -668,14 +735,14 @@
if args.first.is_a?(String)
objects = [ { 'object' => strip_braces(args.first.to_s) } ]
else
- if !block_given? && order_attr
+ if !block_given? && order_attr && order_attr.is_a?(String)
if !args[1][:dont_ignore_missed_attribs]
sql << "SELECT object FROM object_values WHERE source_id=? "
- sql << " AND attrib=? ORDER BY \"value\" " + order_dir
+ sql << " AND attrib=? ORDER BY \"value\" " + ( order_dir ? order_dir : "")
values << get_source_id
values << order_attr
end
else
#it is more effective to use old find here
@@ -751,11 +818,11 @@
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 << "order by " + make_sql_order(args[1]) 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
#puts "Database query start"
@@ -773,11 +840,11 @@
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 << " order by " + make_sql_order(args[1]) if !block_given? && order_attr
sql << strLimit if strLimit
#puts "Database query start" #: #{sql}"
list = db.execute_sql(sql)
#puts "Database query end"
@@ -802,21 +869,16 @@
#puts "Processing rhom objects end, #{ret_list.length} objects"
end
else
puts "Processing rhom objects end, no attributes found."
end
-
- if block_given? && order_attr
- ret_list.sort! { |x,y|
- vx = x.vars[order_attr.to_sym()]
- vy = y.vars[order_attr.to_sym()]
- res = vx && vy ? (block_given? ? yield(vx,vy): vx <=> vy) : 0
- res *= -1 if order_dir && order_dir == 'DESC'
- res
- }
+
+ if block_given?
+ order_array(ret_list, order_attr, order_dir, &block)
+ ret_list = ret_list.slice(offset,limit) if limit
end
-
+
return list.length if args.first == :count
if args.first == :first || args.first.is_a?(String)
return ret_list.length > 0 ? ret_list[0] : nil
end
@@ -880,12 +942,16 @@
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']}) if is_sync_source()
+ if is_schema_source()
+ db.delete_from_table(tableName, {'object'=>obj['object']})
+ else
+ db.delete_from_table(tableName, {'object'=>obj['object'], "source_id"=>get_source_id})
+ end
+ db.delete_from_table('changed_values', {'object'=>obj['object'], "source_id"=>get_source_id}) if is_sync_source()
end
else
if is_schema_source()
db.delete_all_from_table(tableName)
else
@@ -1014,22 +1080,21 @@
#save list of attrs
attrsList = nil
if is_inst_schema_source()
attrsList = db.select_from_table(tableName, '*', {"object"=>obj})
+ db.delete_from_table(tableName, {"object"=>obj})
else
attrsList = db.select_from_table(tableName, '*', {"object"=>obj, "source_id"=>self.get_inst_source_id()})
+ db.delete_from_table(tableName, {"object"=>obj, "source_id"=>self.get_inst_source_id()})
end
- # first delete the record from viewable list
- db.delete_from_table(tableName, {"object"=>obj})
-
resUpdateType = is_inst_sync_source() ? db.select_from_table('changed_values', 'update_type', {"object"=>obj, "update_type"=>'create', "sent"=>0}) : nil
if resUpdateType && resUpdateType.length > 0
update_type = nil
end
- db.delete_from_table('changed_values', {"object"=>obj, "sent"=>0}) if is_inst_sync_source()
+ db.delete_from_table('changed_values', {"object"=>obj, "source_id"=>self.get_inst_source_id(), "sent"=>0}) if is_inst_sync_source()
if is_inst_sync_source() && update_type and attrsList and attrsList.length() > 0
# now add delete operation
if is_inst_schema_source()
attrsList[0].each do |attrName, attrValue|
\ No newline at end of file