Created
August 6, 2014 09:57
-
-
Save AxGord/388682fcd9070364823d to your computer and use it in GitHub Desktop.
Pony Async MySQL, ready for nodejs, neko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import com.dongxiguo.continuation.Continuation; | |
import haxe.CallStack; | |
import haxe.Log; | |
import haxe.PosInfos; | |
#if nodejs | |
import js.Node; | |
#end | |
import pony.db.mysql.Flags; | |
import pony.db.mysql.MySQL; | |
import pony.db.mysql.Types; | |
/** | |
* Pony MySQL Example | |
* @author AxGord <axgord@gmail.com> | |
*/ | |
@:build(com.dongxiguo.continuation.Continuation.cpsByMeta(':async')) | |
class MySQLExample { | |
static function main() { | |
#if nodejs | |
Node.require('source-map-support').install(); | |
Log.trace = function(v:Dynamic, ?p:PosInfos):Void Node.console.log((p != null ? p.className+':' + p.lineNumber + ': ' : '') + v); | |
#end | |
init(function() trace('The end') ); | |
} | |
@:async static function init():Void { | |
//Create MySQL object and connect | |
var db = new MySQL( { user:'root', database:'ponytest' } ); | |
//Bind log and errors to trace | |
db.log << Log.trace; | |
//db.error << Log.trace; | |
db.error << function(e) throw e; | |
//Wait connect | |
@await db.connected.wait(); | |
//Prepare table | |
@await db.users.prepare( [ | |
{name: 'nam', type: Types.CHAR, length: 63, flags: [Flags.NOT_NULL]}, | |
{name: 'ide', type: Types.INT, flags: [Flags.NOT_NULL, Flags.PRI_KEY, Flags.AUTO_INCREMENT]} | |
]); | |
//Next version, correct errors after previos prepare | |
@await db.users.prepare( [ | |
{name: 'id', type: Types.INT, flags: [Flags.UNSIGNED, Flags.NOT_NULL, Flags.PRI_KEY, Flags.AUTO_INCREMENT]}, | |
{name: 'name', type: Types.CHAR, length: 64, flags: [Flags.NOT_NULL]} | |
]); | |
//Clear db users | |
@await db.users.clear(); | |
//Make actions | |
@await db.action("INSERT INTO `users` (`name`) VALUES ('masha')"); | |
@await db.action("INSERT INTO `users` (`name`) VALUES ('sasha')"); | |
@await db.action("INSERT INTO `users` (`name`) VALUES ('dasha')"); | |
//Stop if can't make action | |
if (!@await db.action("INSERT INTO `users` (`name`) VALUES ('lena')")) return; | |
//Streams | |
db.users.stream().data = function(d) trace(d); | |
//Get single field | |
trace(@await db.users.name.get()); | |
//WHERA Generation | |
trace(@await db.users.name.where(id > 2).get()); | |
//Var for value | |
var name = 'lena'; | |
trace(@await db.users.name.where(name == $name).get()); | |
//Var for field | |
var field = 'id'; | |
trace(@await db.users.name.where(~field <= 2).get()); | |
//Disconnect and dispose | |
db.destroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment