Skip to content
Vitaliy Fedorchenko edited this page May 17, 2019 · 5 revisions

Relex stands for 'relational expression'. This is compact string representation of abstract Query structure. Relex'es are useful when some data query (possibly user-configured) should be saved in database or passed as a parameter for REST API.

relex examples

load record by 'Id':

Users(Id="5":int)[*]

in and explicit list of columns:

Users(Id in "2,65,98":int[])[Id,Name]

and/or and order by:

Users(AddedDate > "2011-Jan-01":datetime and (Expired = "true":boolean or Balance<"0":decimal) )[*;id desc] 

subquery and table alias:

Users( CompanyId in Companies.c(c.Id="5":int)[c.Id] )[*]

variables and is null:

Users( Expired="expired":var or Expired=null )[*]

delimited identifiers (table name or column name with spaces or other special symbols):

"Purchase Orders":table( "PO Number":field = null )[*]

how to parse relex

var relex = @"Employees(BirthDate>""1960-01-01"":datetime)[Name,BirthDate]"
var relexParser = new NReco.Data.Relex.RelexParser();
Query q = relexParser.Parse(relex);

how to build relex by Query

var q = new Query("Users", (QField)"Id"==new QConst(5) );
var relexBuilder = new NReco.Data.Relex.RelexBuilder();
var relexStr = relexBuilder.BuildRelex(q);

relex syntax (BNF)

<RelEx> ::= <table_name> ["("<conditions>")"] "["<select_fields> [";"<sort_fields>] "]" ["{"<start>","<limit>"}"];
<table_name> ::= <name_part>|<name_part>"."<table_name>|"\"{<const_char>}\":table";
<select_fields> ::= <field_name>|<field_name>","<select_fields>;
<sort_fields> ::= <sort_field_name>|<sort_field_name>","<sort_fields>;
<sort_field_name> ::= <field_name>[" asc"|" desc"];
<field name> ::= <name_part>|<name_part>"."<field_name>|"\"{<const_char>}\":field";
<conditions> ::= <condition>|<condition><group_type><conditions>;
<group_type> ::= " or " | " and " | "&&" | "||";
<condition> ::= <condition_operand><condition_type><condition_operand>;
<condition_operand> ::= <Relex> | <constant> | <field_name> | "null";
<condition_type> ::= "=" | "==" | "!=" | "<>" | ">=" | "<=" | " in " | " !in " | " like ";
<constant> ::= "\"{<const_char>}\"" [":"<const_type>];
<const_type> ::= <const_simple_type> ["[]"];
<const_simple_type> ::=  "int32" | "int64" | "boolean" | "string" | "datetime" | "decimal" | "double" | "var" | "sql";
<start> ::= <number>;
<limit> ::= <number>;
<number> ::= <digit>[<number>]; 
<name_part> ::= <letter>|<digit>|"_"|"-";
<const_char> ::=  ? all visible characters ?; 
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
Clone this wiki locally