SPOD Left Join

74 views
Skip to first unread message

Jesus Boadas

unread,
Jan 20, 2014, 10:31:37 PM1/20/14
to haxe...@googlegroups.com
Hello,

I have 3 tables

table1:
id
field1

table2:
id
id2table1

table3:
id
id3table1

And I need to make the following SQL

SELECT t1.*, t2.cant_t2, t3.cant_t3
FROM table1 t1
LEFT JOIN
(
    SELECT id2table1, COUNT(*) as cant_t2
    FROM table2
    GROUP BY id2table1
) t2 on t2.id2table1 = t1.id
LEFT JOIN
(
    SELECT id3table1, COUNT(*) as cant_t3
    from table3
    GROUP BY id3table1
) v on t3.id3table1 = t1.id


then I have the table1 mapped to the following object using the php target


class table1 extends Object
{
    public var id : SString<36>;
    public var field1 : SString<30>;

    public static var manager = new Manager<table1>(table1);
}

and can run my query but with trace() I only can see truncated output and with php.Lib.print can only see "List", there is a better way
to get the php output or debug haxe/php?


public function getLeftJoin():List<table1> {
        return table1.manager.unsafeObjects(leftjoinquery);
}

because I need to serialize it, can I create an SPOD object to match my query ?
using unsafeObjects is the right way to do this? in an old message of the list, an answer point to
unsafeExecute but can't find that method
on the api.


thanks in advance


Jesus Boadas

unread,
Jan 21, 2014, 4:00:19 PM1/21/14
to haxe...@googlegroups.com
I can get a workaround creating a new object

class tableJoin extends Object

{
    public var id : SString<36>;
    public var field1 : SString<30>;
    public var cant_t2 : <SInt>;
    public var cant_t3 : <SInt>;

    public static var manager = new Manager<
tableJoin>(tableJoin);
}

and doing my query like this

public function getLeftJoin():List<tableJoin> {
        return
tableJoin.manager.unsafeObjects(leftjoinquery);
}


It works but I don't know if is the right way to do this.

Jason O'Neil

unread,
Jan 21, 2014, 8:09:16 PM1/21/14
to haxe...@googlegroups.com
Hi

This is what I currently do when using the DB Manager class - at the moment there is no easy option for doing SQL Join queries and mapping them to Haxe objects.

If you need the performance or flexibility of a join query, you can do the queries yourself with `var resultSet = cnx.request( someSqlQuery )`, but there is no automatic way to map that back to ORM objects yet.

Jason


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.

Jesus Boadas

unread,
Jan 21, 2014, 9:02:44 PM1/21/14
to haxe...@googlegroups.com, he...@jasono.co
Nice !

Thanks Jason very kind

co...@seasonsoftware.com

unread,
Jan 21, 2014, 9:33:34 PM1/21/14
to haxe...@googlegroups.com
Hi,
Could one of you macro experts help me out with defineType?
Bellow is sample code that does not compile. Should it compile and if not why?

Thanks,
Aaron

---------

class Main
{
public static function main()
{
Macros.makeType();
var aa:NewType; /*This compiles fine*/
}
private function foo(gg:NewType):Void{} /*Compiler error: Class not found : NewType*/
}

------

class Macros
{
macro public static function makeType()
{
var type = macro class NewType{};
Context.defineType(type);
return macro { };
}
}

Jason O'Neil

unread,
Jan 21, 2014, 9:58:50 PM1/21/14
to haxe...@googlegroups.com
Hi,

I'm guessing (I could be wrong) that Haxe might check the type / function signatures of the class before it parses the expressions in the function bodies, and so it encounters the "gg:NewType" argument before it encounters the macro which defines "NewType".

In your code, the flow might be like this:
  • Start compiling - (no initialization macros)
  • class Main - (no build macros)
  • get type of main() - (no args, returns void)
  • get type of foo() - (takes NewType, class not found)
  • parse function body of main()
    • Run Macros.makeType() - define NewType
    • var aa:NewType - fine, we just defined it

If you try call "Macros.makeType" as either a build macro (use `@:build(Macros.makeType())` metadata on your class), or an initialization macro (add `--macro Macros.makeType()` argument to your hxml file) then it should build the type before it parses the arguments, and I think you'll be okay.

There is also haxe.macro.MacroType, which I think is supposed to do something like `typedef NewType = MacroType<[Macros.makeType()]>`, but I'm not really sure of the exact usage of that one...

Jason



co...@seasonsoftware.com

unread,
Jan 22, 2014, 1:52:50 AM1/22/14
to haxe...@googlegroups.com
That was a great explanation.
Thank you.
Reply all
Reply to author
Forward
0 new messages