Skip to content

Commit

Permalink
Merge pull request #70 from benmerckx/master
Browse files Browse the repository at this point in the history
Define type aliases for fields and result
  • Loading branch information
benmerckx authored Jun 12, 2019
2 parents 41566a8 + b7c7688 commit 2ee521d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
["dev.hxml"]
],
"editor.tabSize": 2,
"editor.insertSpaces": true
"editor.insertSpaces": true,
"haxe.executable": {
"path": "node_modules/.bin/haxe",
"windows": {
"path": "node_modules/.bin/haxe.cmd"
}
}
}
2 changes: 1 addition & 1 deletion src/tink/sql/Table.hx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class TableSource<Fields, Filter:(Fields->Condition), Row:{}, Db>
).concat([TPType(e.pos.makeBlankType())])
);
var aliasFields = [];
switch fields {
switch haxe.macro.Context.follow(fields) {
case TAnonymous(_.get().fields => originalFields):
for (field in originalFields)
aliasFields.push({
Expand Down
21 changes: 18 additions & 3 deletions src/tink/sql/macros/TableBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class TableBuilder {
return
switch ctx.type {
case TAnonymous(_.get() => { fields: [{ kind: FVar(_, _), name: name, type: Context.followWithAbstracts(_) => TAnonymous(_.get().fields => fields) }] } ):

var cName = ctx.name;
var names = [for (f in fields) f.name];

Expand Down Expand Up @@ -180,9 +179,25 @@ class TableBuilder {
}
});
}
var filterType = (macro function ($name:$fieldsType):tink.sql.Expr.Condition return tink.sql.Expr.ExprData.EValue(true, tink.sql.Expr.ValueType.VBool)).typeof().sure().toComplex({ direct: true });

macro class $cName<Db> extends tink.sql.Table.TableSource<$fieldsType, $filterType, $rowType, Db> {
var module = Context.getLocalModule().split('.');
module.pop();
function define(type, name) {
Context.defineType({
fields: [],
name: name,
pack: module,
pos: ctx.pos,
kind: TDAlias(type)
});
return module.concat([name]).join('.').asComplexType();
}
// Typedef fields and result so we get readable error messages
var fieldsAlias = define(fieldsType, '${name}_Fields');
var rowAlias = define(rowType, '${name}_Result');
var filterType = (macro function ($name:$fieldsAlias):tink.sql.Expr.Condition return tink.sql.Expr.ExprData.EValue(true, tink.sql.Expr.ValueType.VBool)).typeof().sure().toComplex({ direct: true });

macro class $cName<Db> extends tink.sql.Table.TableSource<$fieldsAlias, $filterType, $rowAlias, Db> {

public function new(cnx, tableName, ?alias) {
super(cnx, new tink.sql.Table.TableName(tableName), alias, ${EObjectDecl(fieldsExprFields).at(ctx.pos)});
Expand Down

0 comments on commit 2ee521d

Please sign in to comment.