How can I use LOOP macro with expression?

Hellow!

I want get code as follow:

Code
String query = "CREATE TABLE "
    + TABLE_NAME + " ("
    + COLUMN_NAME1 + " column_type," //where column_type is one of SQLite data type
    + COLUMN_NAME2 + " column_type,"
    + ... + ");";
prettyPrint();
TABLE_NAME is declared as static field through LOOP macro. COLUMN_NAME is declared as static field through double LOOP macro.

I created follow generator's template:
Снимок.PNG

I don't know how can I insert code with columns names and types in the expression. I try to use LOOP macro, but it does not work. Tell me, please, how can I do it.
5 comments
Hi Sergey,
for a working answer it would be good to know how your language is structured. For your goal I assume you have (or should have) a table concept with column concept children in it. Then you should create a template with the table name as property node macro to table.name and loop over table.columns. In the loop you can add property macros for the column names and column types.

Best regards,
Jens
0
I continued to develop my own project after the written tutorial http://code.google.com/p/mps-lwc11/wiki/lwc11step2At02 therefor structure likes the tutorial.
Structure.PNG

ColumnNameDeaclaration.PNG

I'm trying to add node LOOP macro to COLUMN by node.attributes
step1.png

But I see follow error:
Error: Target role for $LOOP$ macro can contain max 1 element (role: rightExpression)
step2.png

I want to get String value on Java-language with query to create new table in SQLite database, using Entity.name as TABLE and EntityAttribute as COLUMN.
0
Hi Sergey,
thanks, now I can see what you mean. The loop macro takes one input node and loops over a collection defined in the inspector. But you have a bunch of StaticFieldDeclarations (which are the COLUMNs), so this doesn't work and you have to find another solution. If I may suggest one: in your onCreate-method, use a StringBuilder instead of a String. Write builder.append("dummy"); and wrap this statement into the LOOP macro. Add a property macro to the "dummy" and build the column string as the property. I guess you need the name, the data type, and perhaps some constraints like NULL/NOT NULL, foreign key and so on.

Best regards,
Jens
0
Jens, thank you for your answer.
Are there any other solutions, which lead to more readable results?
0
Well, of course! The baseLanguage is a General Purpose Language, so there are many solutions. If a solution is readable depends on the reader, I found my solution in the given context reasonable readable. E.g. you could separate the concerns of a database schema structure and a SQLiteOpenHelper, and further separate an SQL query builder out. The you only have to generate the database schema structure and take the helper and query builder as platform. Currently, the SQLiteOpenHelper doesn't follow the SRP (single responsibility priciple, http://en.wikipedia.org/wiki/Single_responsibility_principle).

Best regards,
Jens
0

Please sign in to leave a comment.