-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewModel.min.js
1 lines (1 loc) · 2.67 KB
/
ViewModel.min.js
1
var JQB;(function(n){var u=function(){function i(t){var i=this;this.schema=ko.observable(null),this.queryTables=ko.observableArray([]),this.schema(new n.Schema.Database(t.schemaCsv)),this.sql=ko.computed(function(){return i.computeSql()})}return i.prototype.queryTable=function(n){for(var t=0,i=this.queryTables();t<i.length;t++)if(i[t].name()===n)return i[t];return null},i.prototype.addTableToQuery=function(n){var i=new t(this,n);this.queryTables.push(i),this.queryTables().length>1&&i.tryToAutoJoin()},i.prototype.table=function(n){for(var t=0,i=this.queryTables();t<i.length;t++)if(i[t].name()===n)return i[t];return null},i.prototype.computeSql=function(){var n,i,t,r,u;if(this.queryTables().length===0)return"(nothing selected)";for(n=[],i=[],t=0,r=this.queryTables();t<r.length;t++)u=r[t],n=n.concat(u.getColumnSql()),i.push(u.getSourceSql());return"select "+n.join(", ")+"\n"+i.join("\n")},i}(),t,i,r;n.ViewModel=u,t=function(){function n(n,t){var u,r;for(this.root=n,this.table=t,this.name=ko.observable(""),this.name(t.name),u=[],r=0;r<t.columns.length;r++)u.push(new i(t.columns[r]));this.columns=ko.observableArray(u)}return n.prototype.column=function(n){for(var t=0,i=this.columns();t<i.length;t++)if(i[t].name()===n)return i[t];return null},n.prototype.tryToAutoJoin=function(){for(var e=[],f,i,s,h,n=0,u=this.root.queryTables();n<u.length;n++){if(this===u[n])break;e.push(u[n].table)}if(f=this.table.guessForeignKeys(e),f.length!==0)for(n=0;n<f.length;n++){var t=f[n],o=this.root.table(t.primary.name),c=this.root.table(t.fkOwner.name);for(i=0;i<t.primaryColumns.length;i++)s=o.column(t.primaryColumns[i].name),h=c.column(t.fkColumns[i].name),h.joins.push(new r(o,s))}},n.prototype.getColumnSql=function(){for(var r=[],i,n=0,t=this.columns();n<t.length;n++)(i=t[n],i.view())&&r.push(this.alias()+".`"+i.name()+"`");return r},n.prototype.getSourceSql=function(){var e,n,t,i,r,u,f,o;if(this.isRoot())return"from `"+this.name()+"` "+this.alias();for(e="join `"+this.name()+"` "+this.alias()+" on ",n=[],t=0,i=this.columns();t<i.length;t++)for(r=0,u=i[t].joins();r<u.length;r++)f=u[r],o=this.alias()+".`"+i[t].name()+"` = "+f.primary.alias()+".`"+f.primaryColumn.name()+"`",n.push(o);return n.length===0&&n.push("(NEED JOIN DEFINITION)"),e+n.join(" and ")},n.prototype.isRoot=function(){return this.root.queryTables.indexOf(this)===0},n.prototype.alias=function(){return"t"+this.root.queryTables.indexOf(this)},n}(),n.QueryTable=t,i=function(){function n(n){this.column=n,this.joins=ko.observableArray([]),this.name=ko.observable(n.name),this.view=ko.observable(!n.isPkOrFk())}return n}(),n.QueryColumn=i,r=function(){function n(n,t){this.primary=n,this.primaryColumn=t}return n}(),n.JoinCondition=r})(JQB||(JQB={}))