HaxeFoundation / record-macros

Macro-based ORM (object-relational mapping)
MIT License
49 stars 24 forks source link

SEnum in SPOD not included in emitted SQL #19

Closed pixelsyntax closed 5 years ago

pixelsyntax commented 6 years ago

I am attempting to use SPOD via record-macros in neko. When I use sys.db.Types.SEnum (for example to set a value on a new object to be inserted into the database), the generated SQL query does not include that field. In the example below I initialise a SpodRec with someInt : SInt = 4 and val : SEnum = value1, but when I attempt to insert the record, the generated SQL is INSERT INTO SpodRec (someInt) VALUES (4) where I would expect it to be INSERT INTO SpodRec (someInt, val) VALUES (4,0) or similar. Am I using this incorrectly?

class SpodTest {
    public static function main(){
        trace("SpodTesting");
        var cnx = sys.db.Sqlite.open("spodtest.db");
        sys.db.Manager.cnx = cnx;
        sys.db.Manager.initialize();

        //Create the SpodRec table
        if ( !sys.db.TableCreate.exists( SpodRec.manager ) )
            sys.db.TableCreate.create( SpodRec.manager );

        var mySpodRec = new SpodRec();
        mySpodRec.someInt = 4;
        mySpodRec.val = value1;
        mySpodRec.insert();
    }
}

class SpodRec extends sys.db.Object {
    public var id : sys.db.Types.SId;
    public var someInt : sys.db.Types.SInt;
    public var val : sys.db.Types.SEnum<MyEnum>;
}

enum MyEnum {
    value1;
    value2;
    value3;
}

Output:

SpodTest.hx:5: SpodTesting                                                                                                                
Called from ? line 1                                                                                                                      
Called from SpodTest.hx line 17
Called from sys/db/Object.hx line 53
Called from sys/db/Manager.hx line 199
Called from sys/db/Manager.hx line 441
Called from /usr/share/haxe/std/neko/_std/sys/db/Sqlite.hx line 40
Uncaught exception - Error while executing INSERT INTO SpodRec (someInt) VALUES (4) (Sqlite error : SQL logic error)
pixelsyntax commented 6 years ago

I should note I have record-macros 1.0.0-alpha installed from haxelib, using haxe 3.4.7, neko 2.2.0

pixelsyntax commented 6 years ago

I've switched to the current git version of record-macros and am still experiencing this problem, but the included record-macro bundled tests complete successfully. Any hints as to where I'm going wrong greatly appreciated

jonasmalacofilho commented 6 years ago

The issue happens because RecordMacros.buildField only matches field types by name.

So by not recognizing sys.db.Types.SEnum as SEnum (it tries to match Types), the internal data_val field and its getters and setters are never created.


A workaround is to first import sys.db.Types, and then type the field simply as:

public var val : SEnum<MyEnum>;