Skip to content

Instantly share code, notes, and snippets.

@cambiata
Created October 14, 2014 09:24
Show Gist options
  • Save cambiata/c0b26ad26772f9412836 to your computer and use it in GitHub Desktop.
Save cambiata/c0b26ad26772f9412836 to your computer and use it in GitHub Desktop.
SPOD example - Neko/SQLite - old.haxe.org/manual/spod
package ;
import neko.Lib;
import sys.db.Manager;
import sys.db.Object;
import sys.db.Sqlite;
import sys.db.Types.SDate;
import sys.db.Types.SId;
import sys.db.Types.SNull;
import sys.db.Types.SString;
import sys.db.Types.SText;
/**
* ...
* @author
*/
// SPOD example - old.haxe.org/manual/spod
// Neko/SQLite
class Main
{
static function main()
{
// Create the file my-database.sqlite if it doesn't exist and open a SQLite connection to it
var cnx = Sqlite.open('my-database.sqlite');
// Initialize the sys.db.Manager that handles the SPOD stuff behind the scenes
Manager.cnx = cnx;
// Create the User table in my-database.sqlite if it doesn't exist
if ( !sys.db.TableCreate.exists(User.manager) ) sys.db.TableCreate.create(User.manager);
// create a new user
var u = new User();
// set some parameters
u.name = "John Doe";
u.birthday = Date.now();
// store it in the db
u.insert();
// fetch user with id # 1
var u = User.manager.get(1);
if ( u == null ) throw "User #1 not found";
// trace it
trace(u.name);
}
}
// User SPOD objcet that extends sys.db.Object
class User extends Object
{
public var id : SId; // SPOD type for record Id
public var name : SString<32>; // SPOD type for strings
public var birthday : SDate; // SPOD type for date
public var phoneNumber : SNull<SText>; // SPOD tye for nullable variable-lenght text field
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment