Last active
November 11, 2015 21:20
-
-
Save jasmo2/8d26042ca65b55cb1e22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pre { | |
var config = CONFIG!Config; | |
var DataBase = DB!Database; | |
var ParentTable = DB!Table; | |
} | |
--This Rule is creating and extra empty database | |
rule Config2Database | |
transform c: CONFIG!Config | |
to db: DB!Database{ | |
config = c ; | |
} | |
rule Document2Database | |
transform doc: JSON!Document | |
to db: DB!Database{ | |
DataBase = db; | |
db.name = "databaseTest"; | |
var objs = doc.objects.select(e|e.isTypeOf(JSON!Object)); | |
for(obj in objs){ | |
for(pair in obj.pairs){ | |
if(db.tables.size()==0){ | |
var table = obj.pairs[0].value.equivalent("ValueObject2Table"); | |
db.tables.add(table); | |
} | |
} | |
} | |
} | |
@lazy | |
rule ValueObject2Table | |
transform obj: JSON!ValueObject | |
to tb: DB!Table{ | |
tb.name = obj.eContainer().name.MergeFieldChecker(); | |
tb.columns = obj.value.pairs.equivalent("Pair2Column"); | |
tb.setPK(tb.columns.has_ID()); | |
tb.database = DataBase; | |
} | |
@lazy | |
rule ArrayValue2Table | |
transform a: JSON!ArrayValue | |
to tb: DB!Table{ | |
tb.name = a.eContainer().name.MergeFieldChecker(); | |
tb.columns = a.values[0].value.pairs.equivalent("Pair2Column"); | |
//System.out.println("t.primary_key: "+ tb.primary_key); | |
tb.setPK(tb.columns.has_ID()); | |
tb.database = DataBase; | |
} | |
@lazy | |
rule Pair2Column | |
transform p: JSON!Pair | |
to col: DB!Column{ | |
//Here MergeFieldChecker has to be called to change the name of the column. | |
//System.out.println("p.name: "+ p.name); | |
col.name = p.name.MergeFieldChecker(); | |
//System.out.println("col.name: "+ col.name); | |
if(p.value.isTypeOf(JSON!StringValue)){ | |
col.type = DB!DataType#varchar; | |
}else if(p.value.isTypeOf(JSON!NumberValue)){ | |
col.type = DB!DataType#int; | |
}else if(p.value.isTypeOf(JSON!DecimalValue)){ | |
col.type = DB!DataType#decimal; | |
}else if(p.value.isTypeOf(JSON!ArrayValue)){ | |
//System.out.println("p.name: "+ p.name); | |
var arrayValue = p.value; | |
DataBase.tables.add(arrayValue.equivalent("ArrayValue2Table")); | |
}else{ | |
col.type = DB!DataType#unknown; | |
} | |
} | |
@lazy | |
rule Column2Key | |
transform c: DB!Column | |
to k: DB!Key{ | |
k.name = c.name; | |
k.key_column.add(c); | |
} | |
operation OrderedSet has_ID(){ | |
//System.out.println("Original Column: "+ self); | |
if(self.name.select(e|e.equals('"_id"')).size()>0){ | |
"The Table Has ID: ".print(); | |
}else{ | |
var col = new DB!Column; | |
col.name = '"_id"'; | |
col.type = DB!DataType#int; | |
self.add(col); | |
//"add column \"_id\": ".print(); | |
//System.out.println("Modify Column: "+ self); | |
} | |
return self; | |
} | |
operation DB!Table setPK(columns){ | |
for(column in columns){ | |
if('"_id"' == column.name){ | |
"\n∆∆∆∆∆∆∆".println(); | |
self.primary_key.add(column.equivalent("Column2Key")); | |
System.out.println("Table.primary_key: "+ self); | |
} | |
} | |
} | |
operation String MergeFieldChecker(){ | |
var finalField = self; | |
//"".println(); | |
//System.out.println("MergeField from: "+ self); | |
for(attrNames in config.mergeFields.attributeNames){ | |
for(attrName in attrNames){ | |
if(attrName.oldField == self){ | |
finalField = attrName.finalField.name; | |
} | |
} | |
} | |
//System.out.println("MergeField to: "+ finalField); | |
return finalField; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment