Spod macro

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Spod macro

postite
Hello list .. 
i'm very interested in the new macro features for SPOD now implemented in the last nightly build..
however , i didn't find any example on how to do it.

i would like to have a spod Object like this for example:

package;
import php.db.Object

class PeopleVo extends Object{

public var id:Int;
public var name:String;

public static var manager:Manager<PeopleVo>;
static var TABLE_NAME = "people";

function new (){
 super()
}
}


what if i wanted to generate the table "people" in my db and have the fields "id" and "name" auto created ?
is it possible with macros ????

any help and very simple example would be very appreciated 

ps:to go further , can we imagine a vo/Sql generator powered by macros?...

thx. 

Dav


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Nicolas Cannasse
Le 11/04/2011 15:42, dav kert a écrit :
> Hello list ..
> i'm very interested in the new macro features for SPOD now implemented
> in the last nightly build..
> however , i didn't find any example on how to do it.

First, you need to compile with -D spod_macro in order to enable it.
Then you can use basic types (Int,Float) or import neko.db.Types that
define additional types that can be used in neko.db.Object fields, for
instance SString<25> (for VARCHAR(25)).

You can use @:skip before a field to tell that this field will not be
stored in the database.

You can use @:relation(key) in front of a (dynamic,dynamic) field to
tell this is a relation.

You can use @:id(id1,id2) before a class to specify its primary key, and
@:index(idx1,idx2) to create an index.

For each of your classes, you will have a neko.db.SpodInfos structure
available at runtime by unserializing the "rtti" metadata of the class.

In order to perform requests, you use do the following methods on a
manager :

.get(id) // check if "id" type is the same as primary key
.get({ key1 : value1, key2 : value2 }) // multiple keys

.select(expr,?options,?lock) : return a single Object
.search(expr,?options,?lock) : return a list of Objects
.count(expr) : Int
.deleteCond(expr) : Void

"expr" is a SQL expression written in haXe, for instance :

var ageParam = 30;
manager.select(name == "Nico" && country.like("Fr%") && age > ageParam)

It will be strictly typed by the macro system.

"options" consists in two optional fields :

{ orderBy : id } // sort by id
{ orderBy : -id } // sort by id desc
{ orderBy : [a,b,-c] } // sort by a, b, and c desc

{ limit : 10 }
{ limit : [20,10] }

"lock" can be set to true or false (default is true) and is used for
transaction row-level locking.

You can also use manager.dynamicSearch (same a old "search") and unsafe*
methods that can be used to directly pass SQL string. Actually the macro
system type-check and build SQL for expressions then return a call to
the corresponding unsafe* method.

The is automatic DB creation yet but it can be added easily.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

clemos
Awesome !

Clément

On Wed, Apr 13, 2011 at 1:53 PM, Nicolas Cannasse
<[hidden email]> wrote:

> Le 11/04/2011 15:42, dav kert a écrit :
>>
>> Hello list ..
>> i'm very interested in the new macro features for SPOD now implemented
>> in the last nightly build..
>> however , i didn't find any example on how to do it.
>
> First, you need to compile with -D spod_macro in order to enable it.
> Then you can use basic types (Int,Float) or import neko.db.Types that define
> additional types that can be used in neko.db.Object fields, for instance
> SString<25> (for VARCHAR(25)).
>
> You can use @:skip before a field to tell that this field will not be stored
> in the database.
>
> You can use @:relation(key) in front of a (dynamic,dynamic) field to tell
> this is a relation.
>
> You can use @:id(id1,id2) before a class to specify its primary key, and
> @:index(idx1,idx2) to create an index.
>
> For each of your classes, you will have a neko.db.SpodInfos structure
> available at runtime by unserializing the "rtti" metadata of the class.
>
> In order to perform requests, you use do the following methods on a manager
> :
>
> .get(id) // check if "id" type is the same as primary key
> .get({ key1 : value1, key2 : value2 }) // multiple keys
>
> .select(expr,?options,?lock) : return a single Object
> .search(expr,?options,?lock) : return a list of Objects
> .count(expr) : Int
> .deleteCond(expr) : Void
>
> "expr" is a SQL expression written in haXe, for instance :
>
> var ageParam = 30;
> manager.select(name == "Nico" && country.like("Fr%") && age > ageParam)
>
> It will be strictly typed by the macro system.
>
> "options" consists in two optional fields :
>
> { orderBy : id } // sort by id
> { orderBy : -id } // sort by id desc
> { orderBy : [a,b,-c] } // sort by a, b, and c desc
>
> { limit : 10 }
> { limit : [20,10] }
>
> "lock" can be set to true or false (default is true) and is used for
> transaction row-level locking.
>
> You can also use manager.dynamicSearch (same a old "search") and unsafe*
> methods that can be used to directly pass SQL string. Actually the macro
> system type-check and build SQL for expressions then return a call to the
> corresponding unsafe* method.
>
> The is automatic DB creation yet but it can be added easily.
>
> Best,
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Jan_Flanders
In reply to this post by Nicolas Cannasse


On Wed, Apr 13, 2011 at 1:53 PM, Nicolas Cannasse <[hidden email]> wrote:
Le 11/04/2011 15:42, dav kert a écrit :
Hello list ..
i'm very interested in the new macro features for SPOD now implemented
in the last nightly build..
however , i didn't find any example on how to do it.

First, you need to compile with -D spod_macro in order to enable it.
Then you can use basic types (Int,Float) or import neko.db.Types that define additional types that can be used in neko.db.Object fields, for instance SString<25> (for VARCHAR(25)).

You can use @:skip before a field to tell that this field will not be stored in the database.

You can use @:relation(key) in front of a (dynamic,dynamic) field to tell this is a relation.

You can use @:id(id1,id2) before a class to specify its primary key, and @:index(idx1,idx2) to create an index.

For each of your classes, you will have a neko.db.SpodInfos structure available at runtime by unserializing the "rtti" metadata of the class.

In order to perform requests, you use do the following methods on a manager :

.get(id) // check if "id" type is the same as primary key
.get({ key1 : value1, key2 : value2 }) // multiple keys

.select(expr,?options,?lock) : return a single Object
.search(expr,?options,?lock) : return a list of Objects
.count(expr) : Int
.deleteCond(expr) : Void

"expr" is a SQL expression written in haXe, for instance :

var ageParam = 30;
manager.select(name == "Nico" && country.like("Fr%") && age > ageParam)

It will be strictly typed by the macro system.

"options" consists in two optional fields :

{ orderBy : id } // sort by id
{ orderBy : -id } // sort by id desc
{ orderBy : [a,b,-c] } // sort by a, b, and c desc

{ limit : 10 }
{ limit : [20,10] }

"lock" can be set to true or false (default is true) and is used for transaction row-level locking.

You can also use manager.dynamicSearch (same a old "search") and unsafe* methods that can be used to directly pass SQL string. Actually the macro system type-check and build SQL for expressions then return a call to the corresponding unsafe* method.

The is automatic DB creation yet but it can be added easily.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
 

I copied the above info to the wiki:

http://haxe.org/doc/neko/spod?lang=en
(bottom of the page)

Jan


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

postite
In reply to this post by Nicolas Cannasse
awesome !

now i have to understand all that stuff :)

thank you

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

clemos
In reply to this post by clemos
By the way, are there plans to adapt the new features to PHP yet ?
>From what I saw, there is a lot of duplicate code in neko and php
SPOD, maybe it would make sense to move it to a haxe.db package ?
And then, why not adapt it for javascript...

++++++
Clément

On Wed, Apr 13, 2011 at 2:34 PM, clemos <[hidden email]> wrote:

> Awesome !
>
> Clément
>
> On Wed, Apr 13, 2011 at 1:53 PM, Nicolas Cannasse
> <[hidden email]> wrote:
>> Le 11/04/2011 15:42, dav kert a écrit :
>>>
>>> Hello list ..
>>> i'm very interested in the new macro features for SPOD now implemented
>>> in the last nightly build..
>>> however , i didn't find any example on how to do it.
>>
>> First, you need to compile with -D spod_macro in order to enable it.
>> Then you can use basic types (Int,Float) or import neko.db.Types that define
>> additional types that can be used in neko.db.Object fields, for instance
>> SString<25> (for VARCHAR(25)).
>>
>> You can use @:skip before a field to tell that this field will not be stored
>> in the database.
>>
>> You can use @:relation(key) in front of a (dynamic,dynamic) field to tell
>> this is a relation.
>>
>> You can use @:id(id1,id2) before a class to specify its primary key, and
>> @:index(idx1,idx2) to create an index.
>>
>> For each of your classes, you will have a neko.db.SpodInfos structure
>> available at runtime by unserializing the "rtti" metadata of the class.
>>
>> In order to perform requests, you use do the following methods on a manager
>> :
>>
>> .get(id) // check if "id" type is the same as primary key
>> .get({ key1 : value1, key2 : value2 }) // multiple keys
>>
>> .select(expr,?options,?lock) : return a single Object
>> .search(expr,?options,?lock) : return a list of Objects
>> .count(expr) : Int
>> .deleteCond(expr) : Void
>>
>> "expr" is a SQL expression written in haXe, for instance :
>>
>> var ageParam = 30;
>> manager.select(name == "Nico" && country.like("Fr%") && age > ageParam)
>>
>> It will be strictly typed by the macro system.
>>
>> "options" consists in two optional fields :
>>
>> { orderBy : id } // sort by id
>> { orderBy : -id } // sort by id desc
>> { orderBy : [a,b,-c] } // sort by a, b, and c desc
>>
>> { limit : 10 }
>> { limit : [20,10] }
>>
>> "lock" can be set to true or false (default is true) and is used for
>> transaction row-level locking.
>>
>> You can also use manager.dynamicSearch (same a old "search") and unsafe*
>> methods that can be used to directly pass SQL string. Actually the macro
>> system type-check and build SQL for expressions then return a call to the
>> corresponding unsafe* method.
>>
>> The is automatic DB creation yet but it can be added easily.
>>
>> Best,
>> Nicolas
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Nicolas Cannasse
Le 13/04/2011 18:27, clemos a écrit :
> By the way, are there plans to adapt the new features to PHP yet ?
>> From what I saw, there is a lot of duplicate code in neko and php
> SPOD, maybe it would make sense to move it to a haxe.db package ?

Yes, at some point we will have some sys.db package I guess, and use
less neko-specific hacks.

> And then, why not adapt it for javascript...

Uhm, not in the browser I guess ?

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Dion Whitehead Amago


>
>> And then, why not adapt it for javascript...
>
> Uhm, not in the browser I guess ?
>
> Nicolas

Node.js works very nicely with Haxe, and it's very fast on the server.

Dion

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Andreas Mokros
In reply to this post by Nicolas Cannasse
Hi.

On Wed, 13 Apr 2011 13:53:44 +0200
Nicolas Cannasse <[hidden email]> wrote:
> You can use @:id(id1,id2) before a class to specify its primary key,
> and @:index(idx1,idx2) to create an index.

Just tried it out. It's pretty cool actually :-)
What about adding @:table("name") or something for setting a different
table name? I see that you don't use the TABLE_NAME var anymore ...

--
Mockey

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Nicolas Cannasse
Le 14/04/2011 09:58, Andreas Mokros a écrit :

> Hi.
>
> On Wed, 13 Apr 2011 13:53:44 +0200
> Nicolas Cannasse<[hidden email]>  wrote:
>> You can use @:id(id1,id2) before a class to specify its primary key,
>> and @:index(idx1,idx2) to create an index.
>
> Just tried it out. It's pretty cool actually :-)
> What about adding @:table("name") or something for setting a different
> table name? I see that you don't use the TABLE_NAME var anymore ...

Added ;)

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

clemos
In reply to this post by Nicolas Cannasse
On Wed, Apr 13, 2011 at 7:16 PM, Nicolas Cannasse
<[hidden email]> wrote:

> Le 13/04/2011 18:27, clemos a écrit :
>>
>> By the way, are there plans to adapt the new features to PHP yet ?
>>>
>>> From what I saw, there is a lot of duplicate code in neko and php
>>
>> SPOD, maybe it would make sense to move it to a haxe.db package ?
>
> Yes, at some point we will have some sys.db package I guess, and use less
> neko-specific hacks.
>
>> And then, why not adapt it for javascript...
>
> Uhm, not in the browser I guess ?

Why not ?
http://diveintohtml5.org/storage.html#future

++++
Clément

>
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Andreas Mokros
In reply to this post by Nicolas Cannasse
On Thu, 14 Apr 2011 10:11:50 +0200
Nicolas Cannasse <[hidden email]> wrote:
> Added ;)

Wow. Coding-to-go... :-)
Thanks.

--
Mockey

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Juraj Kirchheim
In reply to this post by Nicolas Cannasse
> Then you can use basic types (Int,Float) or import neko.db.Types that define
> additional types that can be used in neko.db.Object fields, for instance
> SString<25> (for VARCHAR(25)).

Hey Nicolas,

I see SString is defined as typedef SString<Const> = ... .
This is very interesting, but I couldn't really find much
documentation about it :P
>From SpodData I understand, that SString<25> is in fact translated to
SString<I25>, where I25 is a class (at least at compile time).
What exactly are the limitations?
Which values are accepted?

greetz
back2dos

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Nicolas Cannasse
Le 14/04/2011 11:23, Juraj Kirchheim a écrit :

>> Then you can use basic types (Int,Float) or import neko.db.Types that define
>> additional types that can be used in neko.db.Object fields, for instance
>> SString<25>  (for VARCHAR(25)).
>
> Hey Nicolas,
>
> I see SString is defined as typedef SString<Const>  = ... .
> This is very interesting, but I couldn't really find much
> documentation about it :P
>> From SpodData I understand, that SString<25>  is in fact translated to
> SString<I25>, where I25 is a class (at least at compile time).
> What exactly are the limitations?
> Which values are accepted?

"Const" is a special type parameter that can be used to embed some very
limited metadata directly into the type itself.

Currently it accepts Int, and will generate a  typedef Ixxxx = {},
Float, and will generate a typedef Fxxxx = {}, and String, and will
generate a typedef Sxxxx = {}

Not sure if we should not remove this at some point and only use metadata.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Andreas Mokros
In reply to this post by Nicolas Cannasse
Hi.

On Thu, 14 Apr 2011 10:11:50 +0200
Nicolas Cannasse <[hidden email]> wrote:
> and @:index(idx1,idx2) to create an index.

From looking at the code in SpodData.hx I think @:index(idx1,"unique")
is supposed to work for defining unique indices.

But then it should be (line 261 ff.):
case ":index":
  var unique = m.params[m.params.length - 1] == "unique";
  if (unique) m.params.pop();
  var idx = [];
  for( p in m.params )
    idx.push(makeIdent(p)
or so, shouldn't it?

--
Mockey

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Nicolas Cannasse
Le 15/04/2011 12:53, Andreas Mokros a écrit :
> Hi.
>
> On Thu, 14 Apr 2011 10:11:50 +0200
> Nicolas Cannasse<[hidden email]>  wrote:
>> and @:index(idx1,idx2) to create an index.
>
>  From looking at the code in SpodData.hx I think @:index(idx1,"unique")
> is supposed to work for defining unique indices.

It's  @index(idx1,unique)

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Spod macro

Andreas Mokros
On Fri, 15 Apr 2011 13:14:54 +0200
Nicolas Cannasse <[hidden email]> wrote:
> It's  @index(idx1,unique)

Hmm, you're right, it works. That
== "unique"
irritated me. How comes that makeIdent(p) for unique is
"unique", while other identifiers throw "field does not exist"?

--
Mockey

--
haXe - an open source web programming language
http://haxe.org