tink_sql
tink_sql copied to clipboard
Generated(computed) Columns
MySQL/Postgres: (Postgres does not support VIRTUAL)
column_name data_type [GENERATED ALWAYS] AS (expression)
[VIRTUAL | STORED] [UNIQUE [KEY]]
SQL server:
column_name AS expression [PERSISTED]
Proposed Haxe syntax:
@:generated // computed on the fly
@:generated(Stored) or @:generated(true) // store computed value in table
var foo:Int = bar * 2;
A few notes on implementation:
- Generated field should not appear in a
insertquery - But it should appear in the result of a
selectquery - I think it shouldn't be too hard to convert the expression into CREATE/ALTER TABLE query since we already have the AST at compile time? @benmerckx
- How to implement function calls? (e.g. as illustrated in the age-from-dob example here)
Formatting/printing SQL has always been the easy part to tink_sql changes :) What's the added benefit over making it part of the select (and thereby also supporting databases without computed columns)?
Maybe my description was confusing, I meant this:
$type(db.Table.insertOne)=>{nonComputed:String}->Promise<Int>$type(db.Table.first())=>Promise<{nonComputed:String, computed:String}>db.Table.select($type({...}))=>{?nonComputed, ?computed}
So what I mean is simply: computed values should be supported as readonly.
I can see why this would be useful. OTOH I wonder if it's not easier and more portable to have the computation in application code rather than the database?
Computed columns can be used for (speeding up) aggregation. So no, I don't think they are equivalent.