(function(f,c,k){var v="Rho.ORMHelper";var u={};var e=function(A){var y=i(A.partition);var z="";if(!y.$is_table_exist(A.name)){var x=m(A.schema["property"]);z="CREATE TABLE "+A.name+" ("+x+")";y.$execute_sql(z);if(A.index!==undefined){f.each(A.index,function(B,C){y.$execute_sql("CREATE INDEX "+B+" ON "+A.name+" ("+C.join(",")+")")})}if(A.unique_index!==undefined){f.each(A.unique_index,function(B,C){y.$execute_sql("CREATE UNIQUE INDEX "+B+" ON "+A.name+" ("+C.join(",")+")")})}}};function m(y){var x="";f.each(y,function(z,A){x+=z+" "+n(A[0])+", "});x+='"object" varchar(255) PRIMARY KEY';return x}function n(x){var y="";switch(x){case"integer":case"time":y="integer";break;case"float":y="float";break;default:y="varchar";break}return y}function q(x,y){if(Opal.Rho._scope.RhoConfig.$sources().map.hasOwnProperty(x)&&!Opal.Rho._scope.RhoConfig.$sources().map[x]["loaded"]){console.log("inside of load modal ready to load")}}var s=function(){return u};function b(){var x=Opal.Rho._scope.RhoConfig.$sources().map;f.each(x,function(y,z){delete x[y]})}function t(A,z,y,x){if(x.is_table_exist(source.name)){call_migrate=true}}function i(y){var A=Rho.Application.databaseFilePath(y);var z=s();var x;if(z&&z[y]){x=z[y]}else{x=Opal.Database.$new(A,y);z[y]=x}return x}var g=function(y){var x=i(y.partition);x.$start_transaction;l(x,y);x.$commit};function l(A,D){var E="select sync_priority,source_id,partition, sync_type, schema_version, associations, blob_attribs, name from sources where name ='"+D.name+"'";var z=A.$execute_sql(E);var C;if(z.length>0){C=z[0].map}else{C=null}var y=p(D);var x=o(D);var B={};if(C!==null){d(A,D,C,B)}else{j(A,D,x,y)}}function o(A){var x=0;var z=A.source_id;var y=A.partition;if(z>x){x=z}if(x<Rho.ORM.getSourceId(y)){x=Rho.ORM.getSourceId(y)+2}else{x+=1}return x}function j(z,A,y,x){if(A.source_id===undefined||A.source_id===null){A.source_id=y;Opal.Rho._scope.RhoConfig.$sources().map[A.name]["source_id"]=y;y+=1}z.$insert_into_table("sources",__hash2({source_id:A.source_id,name:A.name,sync_priority:A.sync_priority,sync_type:A.sync_type,partition:A.partition,schema_version:A.schema_version,associations:A.associations,blob_attribs:x}))}function d(z,B,x,A){var y={name:B.name};if(x.sync_priority!==B.sync_priority){y.sync_priority=B.sync_priority}if(x.sync_type!==B.sync_type){y.sync_type=B.sync_type}if(x.schema_version!==B.schema_version){if(x.partition==B.partition){A[B.name]=x.schema_version}else{y.schema_version=B.schema_version}}if(x.partition!==B.partition){y.partition=B.partition}if(x.associations!==B.associations){y.associations=B.associations}if(x.blob_attribs!==B.blob_attribs){y.blob_attribs=B.blob_attribs}z.$update_into_table("sources",Opal.hash2(y),Opal.hash2({source_id:B.source_id}));if(B.source_id===undefined||B.source_id===null){B.source_id=x.source_id;Opal.Rho._scope.RhoConfig.$sources().map[B.name]["source_id"]=x.source_id}}function p(A){if(A.str_blob_attribs!==undefined){return A.str_blob_attribs}else{A.str_blob_attribs="";var z;if(A.schema){z=A.schema["property"]}else{z=A.property}if(z){var B="";var y="";for(var x in z){y=z[x];if(typeof z[x]==String){y=y.split(",")}if(y&&y[0]=="blob"){if(B.length>0){B+=","}B+=x+",";if(y[1]=="overwrite"){B+="1"}else{B+="0"}}}A.str_blob_attribs=B}return A.str_blob_attribs}}function r(y,z){z=typeof z!=="undefined"?z:true;var x;if(y===undefined||y===null){x=null}else{if(typeof y==="string"){x=y.replace(/'/g,"''")}else{if(z){x=String(y).replace(/'/g,"''")}else{x=y}}}return x}function h(y){var x="";if(y!==undefined){x+=string_from_key_vals(y,"and");x=x.substring(0,x.length-5)}return x}function a(x,z){var y="";var A;f.each(x,function(B,C){A="= ";y+=' "#{key}" #{op} #{get_value_for_sql_stmt(value)} #{delim}'});return y}var w={getAllSources:function(){return Opal.Rho._scope.RhoConfig.$sources().map},clearAllSources:function(){b()},getDbPartitions:function(){return s()},createTable:function(x){e(x)},syncSource:function(x){g(x)},dbConnection:function(x){return i(x)},getValueForSqlStmt:function(x,y){return r(x)},whereStr:function(x){return h(x)}};k.namespace(v,w,true)})(Rho.jQuery,Rho,Rho.util);