haxetink / tink_sql

SQL embedded into Haxe
MIT License
53 stars 16 forks source link

Indexes #47

Open benmerckx opened 6 years ago

benmerckx commented 6 years ago

Currently there's no way to create a primary key on multiple columns. Also the order of fields in an index are important, but right now the order is defined by how the fields are ordered in the definition. In spod they were defined on the table, instead of on the fields, like this: @:index(field1, field2). That takes care of ordering, but doesn't allow you to name the index. It's more verbose but I think something like this might be more suitable (name could be optional):

@:unique('name', field1, field2)
@:index('name', field1, field2)
@:primary(field1, field2)
typedef TableName = ...

Having the current metadata as shortcuts on fields could still be allowed, but it requires some checks to make sure you're not overwriting other indexes. If we do change the behaviour it's probably also best to not keeps these in the columns, but seperately as an array on a Table instance. The data types could then also be merged with what I have currently defined in Schema:

typedef Index = {
  name: String,
  type: IndexType,
  fields: Array<String>
}

enum IndexType {
  IPrimary;
  IUnique;
  IIndex;
}
kevinresol commented 6 years ago

Currently there's no way to create a primary key on multiple columns.

Well, you can put @:primary on several columns. But yes, the order is determined by the field order.

But I am afraid you can't put meta on typedef, iirc.

benmerckx commented 6 years ago

Well, you can put @:primary on several columns.

Completely missed that, thanks!

But I am afraid you can't put meta on typedef, iirc.

Looks like that has been changed: http://try-haxe.mrcdk.com/#17276

back2dos commented 6 years ago

You can put meta on typedefs, but it's a bit tricky to pick it up (you have to follow the type one by one).

back2dos commented 6 years ago

Ok, ignore that. Turns out I have dealt with this before: https://github.com/haxetink/tink_macro/blob/master/src/tink/macro/Types.hx#L52 :D

kevinresol commented 4 years ago

As for the naming, we can exploit the call syntax like what we did in tink_unittest:

@:index('index_name'(field1, field2))