ll.vsql – Half an ORM
- class ll.vsql.sqlliteral[source]
Bases:
strInternal marker class that can be used to specify that its value should be treated as literal SQL.
- ll.vsql.comment(s: str) str[source]
Return an SQL comment with the content
s.I.e.
comment("foo")returns"/* foo */".
- class ll.vsql.Repr[source]
Bases:
objectBase class that provides functionality for implementing
__repr__()and_repr_pretty_()(used by IPython).
- class ll.vsql.DataType[source]
Bases:
EnumThe datatypes supported in vSQL expressions.
Possible values are:
NULLBOOLINTNUMBERSTRCLOBCOLORGEODATEDATETIMEDATEDELTADATETIMEDELTAMONTHDELTANULLLISTINTLISTNUMBERLISTSTRLISTCLOBLISTDATELISTDATETIMELISTNULLSETINTSETNUMBERSETSTRSETDATESETDATETIMESET
- classmethod compatible_to(given: DataType, required: DataType) Error | None[source]
Check whether the type
givenis compatible torequired.If
requiredisNoneeverygiventype is accepted. Otherwise the types must be compatible (for exampleDataType.INTis compatible toDataType.NUMBER, but not the other way around). Every type is compatible to itself.If
givenis not compatible torequiredthe appropriate error value is returned, otherwiseNoneis returned.
- class ll.vsql.NodeType[source]
Bases:
EnumThe different types of vSQL abstract syntax tree nodes.
This corresponds to the different subclasses of
AST.Possible values are:
FIELDCONST_NONECONST_BOOLCONST_INTCONST_NUMBERCONST_STRCONST_CLOBCONST_DATECONST_DATETIMECONST_TIMESTAMPCONST_COLORLISTSETCMP_EQCMP_NECMP_LTCMP_LECMP_GTCMP_GEBINOP_ADDBINOP_MULBINOP_SUBBINOP_FLOORDIVBINOP_TRUEDIVBINOP_MODBINOP_ANDBINOP_ORBINOP_CONTAINSBINOP_NOTCONTAINSBINOP_ISBINOP_ISNOTBINOP_ITEMBINOP_SHIFTLEFTBINOP_SHIFTRIGHTBINOP_BITANDBINOP_BITORBINOP_BITXORTERNOP_SLICEUNOP_NOTUNOP_NEGUNOP_BITNOTTERNOP_IFATTRFUNCMETH
- exception ll.vsql.VSQLError[source]
Bases:
ExceptionBase class of exceptions that can happend when compiling vSQL expressions.
- class ll.vsql.Error[source]
-
The types of errors that can lead to invalid vSQL AST nodes.
Note that some of those can not be produced by the Python implementation.
Possible values are:
SUBNODEERRORNODETYPEARITYSUBNODETYPESFIELDCONST_BOOLCONST_INTCONST_NUMBERCONST_DATECONST_DATETIMECONST_TIMESTAMPCONST_COLORNAMELISTUNSUPPORTEDTYPESSETMIXEDTYPESSETUNSUPPORTEDTYPESDATATYPE_NULLDATATYPE_BOOLDATATYPE_INTDATATYPE_NUMBERDATATYPE_STRDATATYPE_CLOBDATATYPE_COLORDATATYPE_DATEDATATYPE_DATETIMEDATATYPE_DATEDELTADATATYPE_DATETIMEDELTADATATYPE_MONTHDELTADATATYPE_NULLLISTDATATYPE_INTLISTDATATYPE_NUMBERLISTDATATYPE_STRLISTDATATYPE_CLOBLISTDATATYPE_DATELISTDATATYPE_DATETIMELISTDATATYPE_NULLSETDATATYPE_INTSETDATATYPE_NUMBERSETDATATYPE_STRSETDATATYPE_DATESETDATATYPE_DATETIMESET
- class ll.vsql.Field[source]
Bases:
ReprA
Fieldobject describes a database field.This field is either in a database table or view or a global variable.
As a table or view field it belongs to a
Groupobject.- __init__(identifier: str | None = None, datatype: DataType = <ll.vsql.DataType.NULL: 'null'>, fieldsql: str | None = None, joinsql: str | None = None, refgroup: Group | None = None)[source]
Create a
Fieldinstance.Argument are:
identifierThe UL4 identifier of the field
datatypeThe vSQL datatype of the field
fieldsqlThe SQL expression for that fields. This should include
{a}as a placeholder for the table alias.joinsqlIf this field is a foreign key to another table,
joinsqlis the join condition. This should include{m}and{d}placeholder for the table aliases of the master table (i.e. the one where this field is in) and the detail table (i.e. the one that will be joined).refgroupThe
Groupobject that represents the target table.
- class ll.vsql.Group[source]
Bases:
ReprA
Groupobject describes a group of database fields.These fields are part of a database table or view and are instances of
Field.
- class ll.vsql.Query[source]
Bases:
ReprA
Queryobject can be used to build an SQL query using vSQL expressions.- __init__(comment: str | None = None, **vars: Field)[source]
Create a new empty
Queryobject.Arguments are:
commentstrorNoneA comment that will be included in the generated SQL.
varsFieldThese are the top level variables that will be availabe for vSQL expressions added to this query. The argument name is the name of the variable. The argument value is a
Fieldobject that describes this variable.In most cases this
Fieldobject is a foreign key to another table, so it has ajoinsqland arefgroup.
- from_vsql(identifier: str) str | None[source]
Registers the field identifier
identifieras a table to select from.identifiermust belong to one of the fields passed to the constructor and it should reference a table.from_vsql()will then make sure that this referenced table will be added to the “from” list, even if it is never referenced explicitely in any of the “from” and “where” clauses.
- select_vsql(expr: str, comment: str | None = None, alias: str | None = None) VSQLSelectExpr[source]
Add the vSQL expression
exprto the list of expression to select.commentwill be added as a comment after the column expression.aliascan be used to give the expression a column alias.This compiles
exprand adds the resulting SQL. To add an SQL expression directly useselect_sql()instead.
- select_sql(expr: str, comment=None, alias=None) SQLSelectExpr[source]
Add the SQL expression
exprto the list of expression to select.commentcan be used to give the column a comment in the select list.aliascan be used to give the expression a column alias.Note that that adds
exprdirectly as “raw” SQL. To add a vSQL expression useselect_vsql()instead.
- aggregate_vsql(expr: str, comment: str | None = None, alias: str | None = None) VSQLAggregatedSelectExpr[source]
Add the aggregating vSQL expression
exprto the list of expression to select.commentwill be added as a comment after the column expression.aliascan be used to give the expression a column alias.Note that it’s not possible to mix aggregated and non-aggregated fields. For a vSQL expression to be an aggregating expression it must either be the function call
count()(without arguments), or call one of the functionsgroup(),min(), max()` orsum()with one argument. These function do the following:count()Return number of records in this group;
min(expr)Return the minimum value of the expressions
exprfor all records in this group;max(expr)Return the maximum value of the expressions
exprfor all records in this group;sum(expr)Return the sum of the values for the expressions
exprfor all records in this group.group(expr)Use the grouping value
expr(which is the same for all records in this group).
- aggregate_sql(expr: str, comment: str | None, alias: str | None = None) SQLAggregatedSelectExpr[source]
Add the aggregating SQL expression
exprto the list of expression to select.commentwill be added as a comment after the column expression.aliascan be used to give the expression a column alias.Note that it’s not possible to mix aggregated and non-aggregated fields.
Make sure that
expris an aggregating expression likecount(*)ormax(tbl.value)
- from_sql(tablename, comment=None, alias=None) SQLFromExpr[source]
Add a table to the list of tables to select from.
This adds the table in “raw” SQL form.
There’s no need to add to the “from” list in vSQL form, since this is done automatically in
select_vsql(),where_vsql()ororderby_vsql().
- where_vsql(expr: str) VSQLWhereExpr[source]
Add vSQL condition
exprto thewhereclause.Note that this compiles
exprand add the resulting SQL. To add an SQL expression directly usewhere_sql()instead.If
exprdoesn’t have the datatypeBOOLit will be automatically converted toBOOL.
- where_sql(expr: str, comment: str | None = None) SQLWhereExpr[source]
Add vSQL condition
exprto thewhereclause.Note that that adds
exprdirectly as “raw” SQL. To add a vSQL expression usewhere_vsql()instead.
- groupby_vsql(expr: str, comment: str | None = None) VSQLGroupByExpr[source]
Add the grouping vSQL expression
exprto the list of expression to group by.commentwill be added as a comment after the column expression.
- groupby_sql(expr: str, comment: str | None = None) SQLGroupByExpr[source]
Add the grouping SQL expression
exprto the list of expression to group by.commentwill be added as a comment after the column expression.
- orderby_vsql(expr: str, comment: str | None = None) VSQLOrderByExpr[source]
Add the “order by” vSQL expression
exprto this query.“order by” specifications will be output in the query in the order they have been added.
The format must be a vSQL expression optionally followed by
ascordescoptionally followed bynulls firstornulls lastascsorts in ascending order anddescsorts descending order. If neither is specified neitherascnordescwill be added to the query (which is equivalent toasc).nulls firstoutputsnullvalues first,nulls lastoutputs them last.Example:
>>> from ll import vsql >>> q = vsql.Query("Example query", user=la.User.vsqlfield()) >>> q.select_vsql("user.email") >>> q.orderby_vsql("user.firstname asc nulls first") >>> q.orderby_vsql("user.surname desc nulls last") >>> print(q.sqlsource()) /* Example query */ select t1.ide_account /* user.email */ from identity t1 /* user */ where livingapi_pkg.global_user = t1.ide_id(+) /* user */ order by t1.ide_firstname /* user.firstname */ asc nulls first, t1.ide_surname /* user.surname */ desc nulls last
- orderby_sql(expr: str, comment: str | None = None) SQLOrderByExpr[source]
Add the “order by” SQL expression
exprto this query.“order by” specifications will be output in the query in the order they have been added.
Note that that adds
exprdirectly as “raw” SQL. To add a vSQL expression useselect_vsql()instead.The format must be an SQL expression optionally followed by
ascordescoptionally followed bynulls firstornulls lastascsorts in ascending order anddescsorts descending order. If neither is specified neitherascnordescwill be added to the query (which is equivalent toasc).nulls firstoutputsnullvalues first,nulls lastoutputs them last.
- offset(offset: int | None) None[source]
Use
offsetas the offset value.This offset specifies how mnay records to skip before returing the first one. The default 0 or None doesn’t skip any records.
- class ll.vsql.Rule[source]
Bases:
ReprRuleis used to store a type specific vSQL grammar rule.I.e. one rule object stores the information that:
there’s and addition operator;
that adds two
INTvalues;with a result of type
INT;and the SQL code to generate for that operation.
For more information see
AST.add_rules().
- class ll.vsql.AST[source]
Bases:
ReprBase class of all vSQL abstract syntax tree node types.
The following class attribute is used:
- title: str
Contains a human readable name for the AST type.
Instance attributes are:
- nodetype: NodeType
Type of the node. There’s a one-to-one correspondence between
ASTsubclasses andNodeTypevalues (except for intermediate classes likeBinaryAST)
- nodevalue: str
The node value is an instance attribute that represents a string that isn’t represented by any child node. E.g. the values of constants or the names of functions, methods and attributes. Will be overwritten by properties in subclasses.
- datatype: DataType | None
The datatype is an instance attribute that represents the datatype of the expression.
If the datatype can’t be determined because of errors datatype will be None.
- __init__(*content: AST | str)[source]
Create a new
ASTnode from its content.contentis a mix ofstrobjects containing the UL4 source and childASTnodes.Normally the user doesn’t call
__init__()directly, but usesmake()to create the appropriateASTnode from child nodes.For example a function call to the function
datecould be created like this:FuncAST( "date", "(", IntAST("2000", 2000), ", ", IntAST("2", 2), ", ", IntAST("29", 29), ")", )
but more conveniently like this:
FuncAST.make( "date", ConstAST.make(2000), ConstAST.make(2), ConstAST.make(29), )
- abstractmethod classmethod make() AST[source]
Create an instance of this AST class from its child AST nodes.
This method is abstract and is overwritten in each subclass.
This is a very low level way of creating vSQL expressions.
For example a vSQL expression for
"foo".lower() + "bar".upper()can be constructed like this:vsql.AddAST.make( vsql.MethAST.make( vsql.StrAST.make("foo"), "lower", ), vsql.MethAST.make( vsql.StrAST.make("bar"), "upper", ), )
- classmethod fromsource(source: str, **vars: Field) AST[source]
Create a vSQL expression from it source code.
For example
"foo".lower() + "bar".upper()can be compiled like this:vsql.AST.fromsource("'foo'.lower() + 'bar'.upper()")
varscontains the “root” variables that can be referenced in the vSQL expression.
- fieldrefs() Generator[FieldRefAST, None, None][source]
Return all
FieldRefASTobjects in thisAST.This is a generator.
- classmethod all_types() Generator[Type[AST], None, None][source]
Return this class and all subclasses.
This is a generator.
- classmethod all_rules() Generator[Rule, None, None][source]
Return all grammar rules of this class and all its subclasses.
This is a generator.
- classmethod add_rules(spec: str, source: str) None[source]
Register new syntax rules for this AST class.
These rules are used for type checking and type inference and for converting the vSQL AST into SQL source code.
The arguments
specandsourcehave the following meaning:specspecspecifies the allowed combinations of operand types and the resulting type. It consists of the following:- Upper case words
These specify types (e.g.
INTorSTR; for a list of allowed values seeDataType). Also allowed are:Tfollowed by an integer, this is used to refer to another type in the spec anda combination of several types joined with
_. This is a union type, i.e. any of the types in the combination are allowed.
- Lower case words
They specify the names of functions, methods or attributes
- Any sequence of whitespace or other non-word characters
They are ignored, but can be used to separate types and names and to make the rule clearer.
The first word in the rule always is the result type.
Examples:
INT <- BOOL + BOOLAdding this rule to
AddASTspecifies that the typesBOOLandBOOLcan be added and the resulting type isINT. Note that using+is only syntactic sugar. This rule could also have been written asINT BOOL BOOLor even asINT?????BOOL#$%^&*BOOL.INT <- BOOL_INT + BOOL_INTThis is equivalent to the four rules:
INT <- BOOL + BOOL,INT <- INT + BOOL,INT <- BOOL + INTandINT <- INT + INT.T1 <- BOOL_INT + T1This is equivalent to the two rules
BOOL <- BOOL + BOOLandINT <- INT + INT.
Note that each rule will only be registered once. So the following code:
AddAST.add_rules( "INT <- BOOL_INT + BOOL_INT", "..." ) AddAST.add_rules( "NUMBER <- BOOL_INT_NUMBER + BOOL_INT_NUMBER", "..." )
will register the rule
INT <- BOOL + BOOL, but notNUMBER <- BOOL + BOOLsince the first call already registered a rule for the signatureBOOL BOOL.sourcesourcespecifies the SQL source that will be generated for this expression. Two types of placeholders are supported:{s1}means “embed the source code of the first operand in this spot” (and{s2}etc. accordingly) and{t1}embeds the type name (in lowercase) in this spot (and{t2}etc. accordingly).Example 1:
AttrAST.add_rules( f"INT <- DATE.year", "extract(year from {s1})" )
This specifies that a
DATEvalue has an attributeyearand that for such a valuevaluethe generated SQL source code will be:extract(year from value)
Example 2:
EQAST.add_rules( f"BOOL <- STR_CLOB == STR_CLOB", "vsqlimpl_pkg.eq_{t1}_{t2}({s1}, {s2})" )
This registers four rules for equality comparison between
STRandCLOBobjects. The generated SQL source code for comparisons betweenSTRandSTRwill bevsqlimpl_pkg.eq_str_str(value1, value2)
and for
CLOB/CLOBcomparison it will bevsqlimpl_pkg.eq_clob_clob(value1, value2)
- validate() None[source]
Validate the content of this AST node.
If this node turns out to be invalid
validate()will set the attributedatatypetoNoneanderrorto the appropriateErrorvalue.If this node turns out to be valid,
validate()will set the attributeerrortoNoneanddatatypeto the resulting data type of this node.
- check_valid(context: str | None = None) None[source]
Makes sure that
selfis valid.If
selfis invalid an appropriate exception will be raised.contextshould describe the context in which the expression is used. E.g."select"when used inQuery.select_vsql()or"where"when used inQuery.where_vsql().
- walknodes() Generator[AST, None, None][source]
Return the all child AST nodes of this node (recursively).
- walkpaths() Generator[list[AST], None, None][source]
Return an iterator for traversing the syntax tree rooted at
self.Items produced by the iterator paths are lists containing the path from the root
ASTobject toself.Note that the iterator will always produce the same list object that will be changed during the iteration. If you want to keep the value produced during the iteration, you have to make copies.
- class ll.vsql.BoolAST[source]
Bases:
_ConstWithValueASTA boolean constant (i.e.
TrueorFalse).
- class ll.vsql.IntAST[source]
Bases:
_ConstWithValueASTAn integer constant.
- class ll.vsql.NumberAST[source]
Bases:
_ConstWithValueASTA number constant (containing a decimal point).
- class ll.vsql.StrAST[source]
Bases:
_ConstWithValueASTA string constant.
- class ll.vsql.CLOBAST[source]
Bases:
_ConstWithValueASTA CLOB constant.
This normally will not be created by the Python implementation
- class ll.vsql.ColorAST[source]
Bases:
_ConstWithValueASTA color constant (e.g.
#fff).
- class ll.vsql.DateAST[source]
Bases:
_ConstWithValueASTA date constant (e.g.
@(2000-02-29)).
- class ll.vsql.DateTimeAST[source]
Bases:
_ConstWithValueASTA datetime constant (e.g.
@(2000-02-29T12:34:56)).
- class ll.vsql.ListAST[source]
Bases:
_SeqASTA list constant.
For this to work the list may only contain items of “compatible” types, i.e. types that con be converted to a common type without losing information.
- class ll.vsql.SetAST[source]
Bases:
_SeqASTA set constant.
For this to work the set may only contain items of “compatible” types, i.e. types that can be converted to a common type without losing information.
- class ll.vsql.FieldRefAST[source]
Bases:
ASTReference to a field defined in the database.
- __init__(parent: FieldRefAST | None, identifier: str, field: Field | None, *content: AST | str)[source]
Create a
FieldRefobject.There are three possible scenarios with respect to
identifierandfield:field is not None and field.identifier == identifierIn this case we have a valid
Fieldthat describes a real field.field is not None and field.identifier != identifier and field.identifier == "*"In this case
fieldis theFieldobject for the generic typed request parameters. E.g. when the vSQL expression isparams.str.foothenfieldreferences theFieldforparams.str.*, sofield.identifier == "*" and identifier == "foo".field is NoneIn this case the field is unknown.
- class ll.vsql.BinaryAST[source]
Bases:
ASTBase class of all binary expressions (i.e. expressions with two operands).
- class ll.vsql.UnaryAST[source]
Bases:
ASTBase class of all unary expressions (i.e. expressions with one operand).
- class ll.vsql.JavaSource[source]
Bases:
objectA
JavaSourceobject combines the source code of a Java class that implements a vSQL AST type with the Python class that implements that AST type.It is used to update the vSQL syntax rules in the Java implemenation of vSQL.
- new_lines() Generator[str, None, None][source]
Return an iterator over the new Java source code lines that should replace the static initialization block inside the Java source file.
- save() None[source]
Resave the Java source code incorporating the new vSQL type info from the Python AST class.
- ll.vsql.oracle_sql_procedure() str[source]
Return the SQL statement for creating the procedure
VSQLGRAMMAR_MAKE.