Commit 11c0455b authored by François Perrad's avatar François Perrad

fix compatibility with Lua 5.2.0 work5

parent ae85db72
LUA := lua
VERSION := $(shell cd src && $(LUA) -e "require [[Coat.Persistent]]; print(Coat.Persistent._VERSION)")
VERSION := $(shell cd src && $(LUA) -e "m = require [[Coat.Persistent]]; print(m._VERSION)")
TARBALL := lua-coatpersistent-$(VERSION).tar.gz
ifndef REV
REV := 1
......
......@@ -12,18 +12,20 @@ local tostring = tostring
local type = type
local _G = _G
local Coat = require 'Coat'
local Meta = require 'Coat.Meta.Class'
local dado = require 'dado.sql'
local error = Coat.error
local argerror = Coat.argerror
local checktype = Coat.checktype
module 'Coat.Persistent'
_ENV = nil
local _M = {}
local drv = {}
local cnx = {}
function establish_connection (class, driver, ...)
local function establish_connection (class, driver, ...)
local function create_db_sequence_tables (conn)
if not conn:execute "select count(*) from db_sequence_state" then
local r, msg = conn:execute "create table db_sequence_state (dataset varchar(50), state_id int(11))"
......@@ -43,20 +45,23 @@ function establish_connection (class, driver, ...)
end
local msg
conn, msg = env:connect(...)
if not conn then
error(msg)
if not conn then
error(msg)
end
cnx[driver] = conn
end
create_db_sequence_tables(conn)
return conn
end
_M.establish_connection = establish_connection
function connection (class)
local function connection (class)
return cnx[drv[class]]
end
_M.connection = connection
local function execute (class, sql)
local trace = _M.trace
if trace then
trace('#', sql)
end
......@@ -102,7 +107,7 @@ local function attributes (class)
return t
end
function save (class, obj)
local function save (class, obj)
local primary_key = class._PRIMARY_KEY
local values = {}
......@@ -133,14 +138,16 @@ function save (class, obj)
return obj[primary_key]
end
_M.save = save
function delete (class, obj)
local function delete (class, obj)
local primary_key = class._PRIMARY_KEY
local cond = dado.AND { [primary_key] = obj[primary_key] }
return execute(class, dado.delete(class._TABLE_NAME, cond))
end
_M.delete = delete
function create (class, val)
local function create (class, val)
if type(val) == 'table' and #val > 0 then
local t = {}
for i = 1, #val do
......@@ -153,8 +160,9 @@ function create (class, val)
return obj
end
end
_M.create = create
function find_by_sql (class, sql)
local function find_by_sql (class, sql)
local cur = execute(class, sql)
return function ()
local row = cur:fetch({}, 'a')
......@@ -168,8 +176,9 @@ function find_by_sql (class, sql)
end
end
end
_M.find_by_sql = find_by_sql
function find (class, val)
local function find (class, val)
if val == nil then
return find_by_sql(class, dado.select('*', class._TABLE_NAME))
elseif type(val) == 'number' then
......@@ -181,8 +190,9 @@ function find (class, val)
argerror('find', 2, "number or string expected")
end
end
_M.find = find
function has_p (class, name, options)
local function has_p (class, name, options)
checktype('has_p', 1, name, 'string')
checktype('has_p', 2, options or {}, 'table')
......@@ -197,13 +207,14 @@ function has_p (class, name, options)
local t = class._ATTR_P; t[#t+1] = name
Coat.has(class, name, options)
end
_M.has_p = has_p
function has_one (class, name, options)
local function has_one (class, name, options)
checktype('has_one', 1, name, 'string')
options = options or {}
checktype('has_one', 2, options, 'table')
local owned_class_name = options.class_name or name
local owned_class = Coat.Meta.Class.class(owned_class_name)
local owned_class = Meta.class(owned_class_name)
if not owned_class then
error("Unknown class " .. owned_class_name)
end
......@@ -234,13 +245,14 @@ function has_one (class, name, options)
class._ACCESSOR = attr_name
end
_M.has_one = has_one
function has_many (class, name, options)
local function has_many (class, name, options)
checktype('has_one', 1, name, 'string')
options = options or {}
checktype('has_one', 2, options, 'table')
local owned_class_name = options.class_name or name
local owned_class = Coat.Meta.Class.class(owned_class_name)
local owned_class = Meta.class(owned_class_name)
if not owned_class then
error("Unknown class " .. owned_class_name)
end
......@@ -285,6 +297,7 @@ function has_many (class, name, options)
return t
end
end
_M.has_many = has_many
function _G.persistent (modname, options)
checktype('persistent', 1, modname, 'string')
......@@ -309,9 +322,10 @@ function _G.persistent (modname, options)
Coat.has(M, primary_key, { is = 'rw', isa = 'number' })
end
_VERSION = "0.1.0"
_DESCRIPTION = "lua-CoatPersistent : an ORM for lua-Coat"
_COPYRIGHT = "Copyright (c) 2010 Francois Perrad"
_M._VERSION = "0.1.0"
_M._DESCRIPTION = "lua-CoatPersistent : an ORM for lua-Coat"
_M._COPYRIGHT = "Copyright (c) 2010 Francois Perrad"
return _M
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
......
......@@ -2,7 +2,7 @@
require 'Test.More'
plan(9)
plan(8)
if not require_ok 'Coat.Persistent' then
BAIL_OUT "no lib"
......@@ -10,12 +10,11 @@ end
local m = require 'Coat.Persistent'
type_ok( m, 'table' )
is( m, Coat.Persistent )
is( m, package.loaded.Coat.Persistent )
is( m, package.loaded['Coat.Persistent'] )
like( m._COPYRIGHT, 'Perrad', "_COPYRIGHT" )
like( m._DESCRIPTION, 'ORM', "_DESCRIPTION" )
type_ok( m._VERSION, 'string', "_VERSION" )
like( m._VERSION, '^%d%.%d%.%d$' )
is( Coat.Persistent.math, nil, "check ns pollution" )
is( m.math, nil, "check ns pollution" )
......@@ -18,7 +18,7 @@ sql_create = [[
require 'Test.More'
plan(19)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 001.png", 'w')
......@@ -26,13 +26,15 @@ if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
f:close()
end
local mc = require 'Coat.Meta.Class'
os.remove 'test.db'
local conn = Person.establish_connection('sqlite3', 'test.db')
conn:execute(Person.sql_create)
ok( Coat.Meta.Class.has( Person, 'id' ), "field id" )
ok( Coat.Meta.Class.has( Person, 'name' ), "field name" )
ok( Coat.Meta.Class.has( Person, 'age' ), "field age" )
ok( mc.has( Person, 'id' ), "field id" )
ok( mc.has( Person, 'name' ), "field name" )
ok( mc.has( Person, 'age' ), "field age" )
john = Person { name = 'John', age = 23 }
is( john:type(), 'Person', "Person" )
......
......@@ -23,7 +23,7 @@ has_p.age = { is = 'rw', isa = 'number' }
sql_create = [[
CREATE TABLE person (
id INTEGER,
avatar_id INTEGER,
avatar_id INTEGER,
name CHAR(64),
age INTEGER
)
......@@ -48,7 +48,7 @@ Person.has_many.Car = {} -- avoid circular definition
require 'Test.More'
plan(16)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 002.png", 'w')
......
......@@ -18,7 +18,7 @@ sql_create = [[
require 'Test.More'
plan(9)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 005.png", 'w')
......
......@@ -18,7 +18,7 @@ sql_create = [[
require 'Test.More'
plan(9)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 006.png", 'w')
......@@ -34,7 +34,7 @@ Person.establish_connection('sqlite3', 'test.db')
local conn = Person.connection()
conn:execute(Person.sql_create)
Person.create {
Person.create {
{ name = 'Joe', age = 20 },
{ name = 'John', age = 20 },
{ name = 'Brenda', age = 20 },
......
......@@ -24,7 +24,7 @@ sql_create = [[
require 'Test.More'
plan(2)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 007.png", 'w')
......
......@@ -18,7 +18,7 @@ sql_create = [[
require 'Test.More'
plan(3)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 009.png", 'w')
......
......@@ -18,7 +18,7 @@ sql_create = [[
require 'Test.More'
plan(5)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 010.png", 'w')
......
......@@ -18,7 +18,7 @@ sql_create = [[
require 'Test.More'
plan(5)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 011.png", 'w')
......@@ -37,8 +37,8 @@ p1 = Person.find(p1.id)()
is( p1.name, 'John' )
Person.create {
{ name = 'Brenda', age = 31 },
{ name = 'Nate', age = 34 },
{ name = 'Brenda', age = 31 },
{ name = 'Nate', age = 34 },
{ name = 'Dave', age = 29 },
}
......
......@@ -9,7 +9,7 @@ has_p.max_speed = { is = 'rw', isa = 'number' }
sql_create = [[
CREATE TABLE vehicle (
c_id INTEGER,
c_id INTEGER,
color CHAR(64),
max_speed INTEGER
)
......@@ -52,7 +52,7 @@ Person.has_many.amigos = { class_name = 'Friend' }
require 'Test.More'
plan(12)
Coat.Persistent.trace = print
require 'Coat.Persistent'.trace = print
if os.getenv "GEN_PNG" and os.execute "dot -V" == 0 then
local f = io.popen("dot -T png -o 016.png", 'w')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment