Skip to content

Commit ece3b17

Browse files
committed
enable using primary keys of BigInt
1 parent f80e2a7 commit ece3b17

File tree

8 files changed

+44
-13
lines changed

8 files changed

+44
-13
lines changed

src/tink/sql/Query.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum Query<Db, Result> {
1212
// Multi(queries: Array<Query<Db, Any>>):Query<Db, Promise<Noise>>;
1313
Union<Row:{}>(union:UnionOperation<Db, Row>):Query<Db, RealStream<Row>>;
1414
Select<Row:{}>(select:SelectOperation<Db, Row>):Query<Db, RealStream<Row>>;
15-
Insert<Row:{}>(insert:InsertOperation<Db, Row>):Query<Db, Promise<Id<Row>>>;
15+
Insert<Row:{},IdType>(insert:InsertOperation<Db, Row>):Query<Db, Promise<IdType>>;
1616
Update<Row:{}>(update:UpdateOperation<Row>):Query<Db, Promise<{rowsAffected:Int}>>;
1717
Delete<Row:{}>(delete:DeleteOperation<Row>):Query<Db, Promise<{rowsAffected:Int}>>;
1818
CallProcedure<Row:{}>(call:CallOperation<Row>):Query<Db, RealStream<Row>>;

src/tink/sql/Table.hx

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Table<T> {
2020
}
2121
#end
2222

23-
class TableSource<Fields, Filter:(Fields->Condition), Row:{}, Db>
23+
class TableSource<Fields, Filter:(Fields->Condition), Row:{}, IdType, Db>
2424
extends Selectable<Fields, Filter, Row, Db>
2525
{
2626

@@ -78,17 +78,17 @@ class TableSource<Fields, Filter:(Fields->Condition), Row:{}, Db>
7878
);
7979
}
8080

81-
public function insertMany(rows:Array<Row>, ?options): Promise<Id<Row>>
81+
public function insertMany(rows:Array<Row>, ?options): Promise<IdType>
8282
return if (rows.length == 0) cast Promise #if (tink_core >= "2") .NOISE #else .NULL #end
8383
else insert(Literal(rows), options);
8484

85-
public function insertOne(row:Row, ?options): Promise<Id<Row>>
85+
public function insertOne(row:Row, ?options): Promise<IdType>
8686
return insert(Literal([row]), options);
8787

88-
public function insertSelect(selected:Selected<Dynamic, Dynamic, Row, Db>, ?options): Promise<Id<Row>>
88+
public function insertSelect(selected:Selected<Dynamic, Dynamic, Row, Db>, ?options): Promise<IdType>
8989
return insert(Select(selected.toSelectOp()), options);
9090

91-
function insert(data, ?options:{?ignore:Bool, ?replace:Bool, ?update:Fields->Update<Row>}): Promise<Id<Row>> {
91+
function insert(data, ?options:{?ignore:Bool, ?replace:Bool, ?update:Fields->Update<Row>}): Promise<IdType> {
9292
return cnx.execute(Insert({
9393
table: info,
9494
data: data,
@@ -122,12 +122,12 @@ class TableSource<Fields, Filter:(Fields->Condition), Row:{}, Db>
122122

123123
macro public function as(e:Expr, alias:String) {
124124
return switch haxe.macro.Context.typeof(e) {
125-
case TInst(_.get() => { pack: pack, name: name, superClass: _.params => [fields, _, row, _] }, _):
125+
case TInst(_.get() => { pack: pack, name: name, superClass: _.params => [fields, _, row, idType, _] }, _):
126126
var fieldsType = fields.toComplex({direct: true});
127127
var filterType = (macro function ($alias:$fieldsType):tink.sql.Expr.Condition return tink.sql.Expr.ExprData.EValue(true, tink.sql.Expr.ExprType.VBool)).typeof().sure();
128128
var path: haxe.macro.TypePath =
129129
'tink.sql.Table.TableSource'.asTypePath(
130-
[fields, filterType, row].map(function (type)
130+
[fields, filterType, row, idType].map(function (type)
131131
return TPType(type.toComplex({direct: true}))
132132
).concat([TPType(e.pos.makeBlankType())])
133133
);

src/tink/sql/Types.hx

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tink.sql;
22

3+
import haxe.*;
34
import tink.sql.Expr;
45

56
typedef Blob<@:const L> = haxe.io.Bytes;
@@ -63,10 +64,25 @@ abstract Id<T>(Int) to Int {
6364

6465
}
6566

67+
abstract Id64<T>(Int64) to Int64 {
6668

69+
public inline function new(v)
70+
this = v;
6771

72+
@:from static inline function ofInt64<T>(i:Int64):Id64<T>
73+
return new Id64(i);
6874

75+
@:to public inline function toString()
76+
return Int64.toStr(this);
6977

78+
@:to public function toExpr():Expr<Id64<T>>
79+
return tink.sql.Expr.ExprData.EValue(new Id64(this), cast VInt64);
7080

81+
@:op(A>B) static function gt<T>(a:Id64<T>, b:Id64<T>):Bool;
82+
@:op(A<B) static function lt<T>(a:Id64<T>, b:Id64<T>):Bool;
83+
@:op(A>=B) static function gte<T>(a:Id64<T>, b:Id64<T>):Bool;
84+
@:op(A>=B) static function lte<T>(a:Id64<T>, b:Id64<T>):Bool;
85+
@:op(A==B) static function eq<T>(a:Id64<T>, b:Id64<T>):Bool;
86+
@:op(A!=B) static function neq<T>(a:Id64<T>, b:Id64<T>):Bool;
7187

72-
88+
}

src/tink/sql/format/CockroachDbFormatter.hx

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class CockroachDbFormatter extends SqlFormatter<CockroachDbColumnInfo, Cockroach
103103
case DInt(Default, _, true, _):
104104
ident(column.name)
105105
.add(sql('SERIAL4'));
106+
case DInt(Big, _, true, _):
107+
ident(column.name)
108+
.add(sql('SERIAL8'));
106109
case _:
107110
super.defineColumn(column);
108111
}

src/tink/sql/format/PostgreSqlFormatter.hx

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class PostgreSqlFormatter extends SqlFormatter<PostgreSqlColumnInfo, PostgreSqlK
9292
case DInt(Default, _, true, _):
9393
ident(column.name)
9494
.add(sql('SERIAL'));
95+
case DInt(Big, _, true, _):
96+
ident(column.name)
97+
.add(sql('BIGSERIAL'));
9598
case _:
9699
super.defineColumn(column);
97100
}

src/tink/sql/macros/TableBuilder.hx

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import haxe.macro.Expr;
77
import tink.sql.schema.KeyStore;
88

99
using tink.MacroApi;
10+
using Lambda;
1011

1112
class TableBuilder {
1213

@@ -203,8 +204,16 @@ class TableBuilder {
203204
var fieldsAlias = define(fieldsType, 'FieldsOf_${readableName}');
204205
var rowAlias = define(rowType, 'ResultOf_${readableName}');
205206
var filterType = (macro function ($name:$fieldsAlias):tink.sql.Expr.Condition return tink.sql.Expr.ExprData.EValue(true, tink.sql.Expr.ExprType.VBool)).typeof().sure().toComplex({ direct: true });
206-
207-
macro class $cName<Db> extends tink.sql.Table.TableSource<$fieldsAlias, $filterType, $rowAlias, Db> {
207+
var idType = switch (keys.get()[0]) {
208+
case null:
209+
macro:tink.sql.Types.Id<$rowAlias>;
210+
case Primary([keyField]) if (fields.find(f -> f.name == keyField).meta.has(':autoIncrement')):
211+
final keyFieldType = fields.find(f -> f.name == keyField).type.toComplex();
212+
macro:$keyFieldType;
213+
case _:
214+
macro:tink.sql.Types.Id<$rowAlias>;
215+
};
216+
macro class $cName<Db> extends tink.sql.Table.TableSource<$fieldsAlias, $filterType, $rowAlias, $idType, Db> {
208217

209218
public function new(cnx, tableName, ?alias) {
210219
final name = new tink.sql.Table.TableName(tableName);

tests/BigIntTest.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BigIntTest extends TestWithDb {
3232
intMin: int64Min,
3333
intMax: int64Max,
3434
})
35-
.next(function(id:Int) return db.BigIntTypes.first())
35+
.next(function(id:tink.sql.Types.BigInt) return db.BigIntTypes.where(r -> r.id == id).first())
3636
.next(function(row:BigIntTypes) {
3737
asserts.assert(row.int0 == 0);
3838
asserts.assert(row.intMin == int64Min);

tests/Db.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ typedef Schema = {
103103
}
104104

105105
typedef BigIntTypes = {
106-
@:autoIncrement @:primary public var id(default, null):Id<User>;
106+
@:autoIncrement @:primary public var id(default, null):BigInt;
107107
public var int0(default, null): BigInt;
108108
public var intMin(default, null): BigInt;
109109
public var intMax(default, null): BigInt;

0 commit comments

Comments
 (0)