Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » xText Grammer Scoping for SQL SELECT statement(Column Names Scoping for SQL Tables Using Content Assist)
xText Grammer Scoping for SQL SELECT statement [message #552073] Tue, 10 August 2010 17:27 Go to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Do you need to know Java coding to implement scoping in xText? Or it can be all implemented in a xText grammer alone.

For example, I have a SQL "SELECT" xText grammer (not to reinvent the wheel, I am adapting this xText grammer, please see link
http://www.eclipse.org/forums/index.php?t=msg&goto=55180 5&S=ae2e996116dc9c0a1864fa097bd3e0e2#msg_551805).

I have two tables, Customer and PhoneNumber. Table Customer has columns called CustomerID and CustomerName.
And Table PhoneNumber has two columns called CustomerID and WorkNumber.
The following SQL statement will retreive the work numbers for all the customers.


SELECT Customer.CustomerName, PhoneNumber.WorkNumber
FROM Customer
INNER JOIN PhoneNumber ON Customer.CustomerID = PhoneNumber.CustomerID

How would I implement scoping for the table Customer so that Content Assist will only show CustomerID and CustomerName?
Likewise, table PhoneNumber will show CustomerID and WorkNumber.

Any help would be greatly appreciated. Thanks.
Re: xText Grammer Scoping for SQL SELECT statement [message #552193 is a reply to message #552073] Wed, 11 August 2010 08:20 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
You do need to do some Java coding (in <DSLname>JavaValidator to be exact). The main reason for that (as I understood) is that this is much more performant than an earlier approach which used Xtend, which is interpreted. Another reason is that it's slightly easier to get access to "things living outside" than it is in Xtend. Also, not all algorithmic stuff can be comfortably expressed through functional programming.

If you really want, you can use the XtendFacade to call extensions to do the scoping Smile (In fact, for small and simple grammars that'd be a nice thing to have.)

Also, I don't see a table def. in your and the referenced grammar, so scoping wouldn't be invoked anyway since an EAttribute will be generated instead of an EReference.


Re: xText Grammer Scoping for SQL SELECT statement [message #552219 is a reply to message #552073] Wed, 11 August 2010 10:25 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

the grammar lets you restrict the *type* of the referenced objects.
Everything else has to be implemented via scoping.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552319 is a reply to message #552219] Wed, 11 August 2010 17:30 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Appeciated for your response. I learned how to construct a xtext grammer from your postings. Please also see my responses to Meinte Boersma.
Re: xText Grammer Scoping for SQL SELECT statement [message #552321 is a reply to message #552193] Wed, 11 August 2010 17:32 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Appeciated for your response. I learned how to construct a xtext grammer from Alex postings.

Table Definitions will be a cross reference file. For example,

TableDefinitions
Customer{
CustomerID
CustomerName
}
PhoneNumber{
CustomerID
WorkNumber
}

Using this approach, the Content Assist will show all the column names. What if I have 500+ tables? The list would be long.
That's why I want to implement scoping to limit the number of columns returning. In other words, scoping will only show the columns
returning from the "FROM", "INNER JOIN" and "OUTER JOIN" tables.

Unfortunately, I am a database backend developer and not a Java developer. However, I do know some Java scripting.

Should I give it up?

I want to figure out how to use XtendFacade to call extensions? Do you have any simple examples to explain the concepts?

On the other hand, I don't think I have enough Java and Eclipse knowledge to code in the JavaValidator.

I did look thru the xtext documentation (see link below) but it's all too technical for me to understand.
http://www.eclipse.org/Xtext/documentation/1_0_0/xtext.html# scoping

Please advise. Thanks.
Re: xText Grammer Scoping for SQL SELECT statement [message #552332 is a reply to message #552321] Wed, 11 August 2010 18:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Dennis,

without having a complete grammar giving more advice than to hint to the docs is hard to give.

Maybe this sentence How would I implement scoping for the table Customer was a bit misunderstanding since you define scoping for metaclasses which indicates that customer is a metaclass but i think its an instance of the metaclass TableDefinition.

Never the less having a simple grammar like this one

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	elements+=Element*
	mappings+=Mapping*
;
	
Element:
	"element" name=ID "{"
		attributes+=Attribute*
	"}"
;

Attribute:
	"attr" name=ID
;

Mapping:
	"mapping" from=MappingPart "->" to=MappingPart
;

MappingPart:
	element=[Element] "." attribute=[Attribute]
;



and a very very small ScopeProvider like this

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	public IScope scope_MappingPart_attribute(MappingPart mappingPart, EReference ref) {
		return Scopes.scopeFor(mappingPart.getElement().getAttributes());
	}

}


scoping works for a sample model like this

element A {
	attr a1
	attr a2
}

element B {
	attr b1
	attr b2
}

mapping A.a1 -> B.b2


Regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 11 August 2010 18:35]

Report message to a moderator

Re: xText Grammer Scoping for SQL SELECT statement [message #552361 is a reply to message #552332] Wed, 11 August 2010 21:58 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Wow! Your example is simple and very effective. I tried it out but failed.

Here are the error messages:
IScope cannot be resolved to a type
EReference cannot be resolved to a type
MappingPart cannot be resolved to a type
Scopes cannot be resolved

Did I miss something?
Re: xText Grammer Scoping for SQL SELECT statement [message #552369 is a reply to message #552361] Wed, 11 August 2010 23:04 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
To adapt to Christian's coding to my grammer, I think I will need to change QualifiedTableReference.
I hope it will work. I cannot try it yet until the "the IScope cannot be resolved to a type" error is fixed.

From this
QualifiedTableReference hidden():ID'.'ID;


To this
QualifiedTable:
name=ID;

QualifiedColumn:
name=ID;

QualifiedTableReference hidden():
	element=[QualifiedTable] "." attribute=[QualifiedColumn];


And ScopeProvider like this
public IScope scope_QualifiedTableReference_attribute(QualifiedTableReference qualifiedTableReference, EReference ref) {
	return Scopes.scopeFor(qualifiedTableReference.getQualifiedTable().getQualifiedColumn());
}


Here's the grammer.
grammar org.xtext.newdef.NewDef with org.eclipse.xtext.common.Terminals

generate newDef "http://www.xtext.org/newdef/NewDef"


Model:
TableDefinitions|NameSpaces;


TableDefinitions:{TableDefinition}'TableDefinitions' tables+=Table*;
Table: name=ID'{'columns+=Column*'}';
Column: name=ID;


NameSpaces:{NameSpaces}
(namespace+=NameSpace)*;

NameSpace:
'Namespace' name=ID "{"
(query+=Query)*
"}";

Query:
'Query' namespace=[NameSpace] ("." name=QualifiedName) "{"
(

("[" 'Description:' description=MYSTRING"]")
&
("[" 'Query:'queryName=[Query|QualifiedName]"]")?
&
("[" 'Filter:' filter+=Filter (',' filter+=Filter)*"]")?
&
//no return fragments yet
("[" 'Return:' return+=ReturnElement (',' return+=ReturnElement)* "]")?
&
("[" 'From:' from=[Table]
joinespression+=JoinExpression*
"]")?
&
("[" 'Where:' where=Where"]")?
&
("[" 'Order:' order+=Order (',' order+=Order)*"]")?)

"}";


Filter: column=ID (multi?='*')?;

ReturnElement: ref=[Column|QualifiedTableReference] ('As' asname=ID)?;

Where: column=[Column|QualifiedTableReference] (expression=WhereExpression)
(("And"|"Or") equals+=ColumnEqualsExpression)*;


Order: ref= [Column|QualifiedTableReference] (direction=Direction)?;
enum Direction: ascending="Asc"|descending="Desc";

JoinExpression: InnerJoinExpression|OuterJoinExpression;

InnerJoinExpression:
(optional?='Optional')? 'Inner' 'Join' jointablename=[Table]
"On" equals+=ColumnRefEqualsExpression
(("And"|"Or") equals+=ColumnEqualsExpression)*;

OuterJoinExpression:
('Optional')? 'Left' 'Join' jointablename=[Table]
"On" equals+=ColumnRefEqualsExpression
(("And"|"Or") equals+=ColumnEqualsExpression)*;


ColumnEqualsExpression:ColumnRefEqualsExpression|ColumnStringEqualsExpression|ColumnOtherExpression;

ColumnRefEqualsExpression: left=[Column|QualifiedTableReference] '=' 
right=[Column|QualifiedTableReference];
ColumnStringEqualsExpression: left=[Column|QualifiedTableReference] ('='|'<>') 
right=STRING;

ColumnOtherExpression: left=[Column|QualifiedTableReference] right=WhereExpression;

WhereExpression: 
IntCompareExpression|NullExpression|NotNullExpression|StringEqualsExpression;

NullExpression: "Is" "Null"{NullExpression};
NotNullExpression: "Is" "Not" "Null"{NotNullExpression};

IntCompareExpression: comparator=Comparator value=INT;
StringEqualsExpression: '=' value=STRING;
enum Comparator: equals = "="| greater=">"|less="<"| 
lessequals="<="|greaterequals= ">="|notequals= "<>";

MYSTRING hidden(): (ID|WS|INT|STRING|'not'|','|ANY_OTHER)*;
QualifiedName hidden():ID('.'ID)*;

//QualifiedTableReference hidden():ID'.'ID;
QualifiedTable:
name=ID;

QualifiedColumn:
name=ID;

QualifiedTableReference hidden():
	element=[QualifiedTable] "." attribute=[QualifiedColumn];
Re: xText Grammer Scoping for SQL SELECT statement [message #552397 is a reply to message #552369] Thu, 12 August 2010 06:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
The Class Scopes and the Interface IScope are Part of Xtext and EReferene is Part of EMF so a Simple Organize Imports should do the Trick. ~Christian

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552581 is a reply to message #552397] Thu, 12 August 2010 17:10 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Thanks again. Simple Organize Imports did resolve the Iscope error.

I am having issues with
mapping A.a1 -> B.b2  


After I typed in "mapping A.", Content Assist (control space) gave me a blank list to select from.

Also when I typed "mapping ", a red x appeared. When I clicked the red x, the message was "mismatched input '<EOF>' expecting RULE_ID".

The grammar was fine or"Done" when I generated xText Artifacts. Exporting was no issues either. What could go wrong here?

Here's the code again
public IScope scope_MappingPart_attribute(MappingPart mappingPart, EReference ref) {
	return Scopes.scopeFor(mappingPart.getElement().getAttributes());
}
Re: xText Grammer Scoping for SQL SELECT statement [message #552585 is a reply to message #552581] Thu, 12 August 2010 17:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi, does it work without changes to scoping. Have you doublechecked grammar and Model? ~ Christian

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552592 is a reply to message #552369] Thu, 12 August 2010 18:09 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
The modified definition of the QualifiedTableReference makes no sense the way you use it.

QualifiedTableReference hidden():
element=[QualifiedTable] "." attribute=[QualifiedColumn];

First you do not want to reference elements of type QualifiedTable of QualifiedColumn but Table and Column, otherwise your TableDefinitions will not be used.

Second the places where you used the rule before are now incorrect

Example:
ReturnElement: ref=[Column|QualifiedTableReference]

should be
ReturnElement: ref=QualifiedTableReference

Alex
Re: xText Grammer Scoping for SQL SELECT statement [message #552593 is a reply to message #552585] Thu, 12 August 2010 18:10 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Issue 1:
It does not work without Content Assist.
mapping A.a1 -> B.b1

The error message is: Multiple markers at this line: Couldn't resolve reference to Attribute 'b1', Couldn't resolve reference to Attribute 'a2'.

Issue 2:
Using Content Assist, it was able to verify the grammar.
For example, if I type in
mapping A.b1 -> B.b1

it will complaint about A.b1. Somehow it knows b1 or b2 does not belong to A. However, by pressing ctrl-space, content assist does not offer a list.
Re: xText Grammer Scoping for SQL SELECT statement [message #552594 is a reply to message #552593] Thu, 12 August 2010 18:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Can you please post the grammar you are actually using?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552598 is a reply to message #552594] Thu, 12 August 2010 18:17 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Here is the grammer
grammar org.xtext.scope.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/scope/mydsl/MyDsl"

Model:
	elements+=Element*
	mappings+=Mapping*
;
	
Element:
	"element" name=ID "{"
		attributes+=Attribute*
	"}"
;

Attribute:
	"attr" name=ID
;

Mapping:
	"mapping" from=MappingPart "->" to=MappingPart
;

MappingPart:
	element=[Element] "." attribute=[Attribute]
;

Re: xText Grammer Scoping for SQL SELECT statement [message #552601 is a reply to message #552598] Thu, 12 August 2010 18:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

right, i missed to tell that you have to use SimpleNameProvider in this case

public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
	
	@Override
	public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
		return SimpleNameProvider.class;
	}
	

}


or you change the grammar like this and don't have to implement any scoping at all.

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	elements+=Element*
	mappings+=Mapping*
;
	
Element:
	"element" name=ID "{"
		attributes+=Attribute*
	"}"
;

Attribute:
	"attr" name=ID
;

Mapping:
	"mapping" from=MappingPart "->" to=MappingPart
;

MappingPart:
	attribute=[Attribute|FQN]
;

FQN: ID ("." ID)*;


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552605 is a reply to message #552601] Thu, 12 August 2010 18:51 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Note however, that this is back to the original grammar where columns are already referenced using fully qualified names. In some cases columns are to be referenced by their simple names, which caused the screw-up of the QualifiedTableReference definition rather than just adapt the grammar where columns are to be referenced by their simple names and adapt the corresponding scoping.

Make sure you don't miss my earlier post in this thread, as it was published right in the middle of your exchange with Christian

Alex
Re: xText Grammer Scoping for SQL SELECT statement [message #552622 is a reply to message #552601] Thu, 12 August 2010 21:28 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
For scoping, I tried both ways and they worked. For content assist, it did not prompt any attributes for me to choose. Is there a way to do that?
Re: xText Grammer Scoping for SQL SELECT statement [message #552623 is a reply to message #552605] Thu, 12 August 2010 21:29 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Thanks, Alex. I get scoping working but not content assist. Is there a way to do that?
Re: xText Grammer Scoping for SQL SELECT statement [message #552636 is a reply to message #552592] Fri, 13 August 2010 01:08 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Hi Alex, I modified the grammar to
QualifiedTableReference hidden():
element=[Table]"."attribute=[Column];

ReturnElement: ref=QualifiedTableReference ('As' asname=ID)?;

Now, it didn't like the column name. However, content assist will prompt the table names.

Using the old grammar, content assist will prompt the column names with the table names.

Re: xText Grammer Scoping for SQL SELECT statement [message #552669 is a reply to message #552636] Fri, 13 August 2010 06:26 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

> Now, it didn't like the column name. However, content assist will
> prompt the table names.
> Using the old grammar, content assist will prompt the column names with
> the table names.

No wonder, the column names are not proposed, as the default
implementation will propose fully qualified names and not the simple
names. My adaptions to Pam's grammar already contain a comment to that
effect.
You will have to adapt the scoping for the column. Sample code is
already contained in this thread.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552678 is a reply to message #552636] Fri, 13 August 2010 06:34 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Also please debug whether your scoping function for the columns is
invoked at triggering content assist. It is well possible that the
context object is not of the type you expect it to be. See also
http://www.eclipse.org/forums/index.php?t=msg&th=172683& amp;start=0&S=5cbabccf11b6630a41fb285f7087dbad

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: xText Grammer Scoping for SQL SELECT statement [message #552715 is a reply to message #552073] Fri, 13 August 2010 09:14 Go to previous messageGo to next message
Cyril  is currently offline Cyril Friend
Messages: 18
Registered: July 2010
Junior Member
Dennis,

Alex is right, I just did the debug test on the context object.
It wasn't the type I expected it to be.

However I did not get in my thread the solution. Don't give up though!

Good luck, and let me know if you reach a solution.

Cyril
Re: xText Grammer Scoping for SQL SELECT statement [message #552863 is a reply to message #552601] Fri, 13 August 2010 20:55 Go to previous messageGo to next message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Thank you, Christian! It works using SimpleNameProvider. It will prompt me for the element's attributes. Now, I will need to adapt this to my grammar. Alex Nittka helped me quite a bit too. Many thanks again! Cheers!
Re: xText Grammer Scoping for SQL SELECT statement [message #552898 is a reply to message #552863] Sat, 14 August 2010 07:13 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
> Thank you, Christian! It works using SimpleNameProvider.
Note that you need to be careful if columns in different tables can have
the same name.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: xText Grammer Scoping for SQL SELECT statement [message #555510 is a reply to message #552592] Thu, 26 August 2010 21:15 Go to previous message
Dennis  is currently offline Dennis Friend
Messages: 19
Registered: August 2010
Junior Member
Alex, I was on vacation for a week. After I came back and I got scoping to work for my grammar. Thank you for all your assistance.
Previous Topic:no syntax highlighting and no code completion
Next Topic:Code generation like Domain Model?
Goto Forum:
  


Current Time: Tue Apr 23 06:37:57 GMT 2024

Powered by FUDForum. Page generated in 0.05518 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top