-- Parma1 is the thread type ,there are five types of thread you can create.
-- BusinessThreadUI
-- BusinessThreadDB
-- BusinessThreadLOGIC
-- BusinessThreadFILE
-- BusinessThreadIO
-- Param2 is the thread name
-- Result is new threadId which is the token you should hold to do further action
local newThreadId = lua.thread.createThread(BusinessThreadLOGIC,"newThread")
-- Parma1 is the threadId for which you want to perform method
-- Parma2 is the modelName
-- Parma3 is the methodName
-- The result is just like you run the below code on a specified thread async
-- require(modelName).methodName("params", 1.1, {1,2,3}, function (p)
-- end)
lua.thread.postToThread(threadId,modelName,methodName,"params", 1.1, {1,2,3}, function (p)
-- do something here
end)
-- Parma1 is the threadId for which you want to perform method
-- Parma2 is the modelName
-- Parma3 is the methodName
-- The result is just like you run the below code on a specified thread sync
-- local result = require(modelName).methodName("params", 1.1, {1,2,3}, function (p)
-- end)
local result = lua.thread.postToThreadSync(threadId,modelName,methodName,"params", 1.1, {1,2,3}, function (p)
-- do something here
end)
-- Add the define table to dbData.lua
-- Luakit provide 7 colum types
-- IntegerField to sqlite integer
-- RealField to sqlite real
-- BlobField to sqlite blob
-- CharField to sqlite varchar
-- TextField to sqlite text
-- BooleandField to sqlite bool
-- DateTimeField to sqlite integer
user = {
__dbname__ = "test.db",
__tablename__ = "user",
username = {"CharField",{max_length = 100, unique = true, primary_key = true}},
password = {"CharField",{max_length = 50, unique = true}},
age = {"IntegerField",{null = true}},
job = {"CharField",{max_length = 50, null = true}},
des = {"TextField",{null = true}},
time_create = {"DateTimeField",{null = true}}
-- when you use, you can do just like below
local Table = require('orm.class.table')
local userTable = Table("user")
local userTable = Table("user")
local user = userTable.get:primaryKey({"user1"}):first()
user.password = "efg"user.time_create = os.time()
user:save()
select 数据
local userTable = Table("user")
local users = userTable.get:all()
print("select all -----------")
local user = userTable.get:first()
print("select first -----------")
users = userTable.get:limit(3):offset(2):all()
print("select limit offset -----------")
users = userTable.get:order_by({desc('age'), asc('username')}):all()
print("select order_by -----------")
users = userTable.get:where({ age__lt = 30,
age__lte = 30,
age__gt = 10,
age__gte = 10,
username__in = {"first", "second", "creator"},
password__notin = {"testpasswd", "new", "hello"},
username__null = false
}):all()
print("select where -----------")
users = userTable.get:where({"scrt_tw",30},"password = ? AND age < ?"):all()
print("select where customs -----------")
users = userTable.get:primaryKey({"first","randomusername"}):all()
print("select primaryKey -----------")
联表查询
local userTable = Table("user")
local newsTable = Table("news")
local user_group = newsTable.get:join(userTable):all()
print("join foreign_key")
user_group = newsTable.get:join(userTable,"news.create_user_id = user.username AND user.age < ?", {20}):all()
print("join where ")
user_group = newsTable.get:join(userTable,nil,nil,nil,{create_user_id = "username", title = "username"}):all()
print("join matchColumns ")
http请求
Luakit提供了http请求接口,包括了请求队列调度控制
-- url , the request url
-- isPost, boolean value represent post or get
-- uploadContent, string value represent the post data
-- uploadPath, string value represent the file path to post
-- downloadPath, string value to tell where to save the response
-- headers, tables to tell the http header
-- socketWatcherTimeout, int value represent the socketTimeout
-- onResponse, function value represent the response callback
-- onProgress, function value represent the onProgress callback
lua.http.request({ url = "http://tj.nineton.cn/Heart/index/all?city=CHSH000000",
onResponse = function (response)
end})
local socket = lua.asyncSocket.create("127.0.0.1",4001)
socket.connectCallback = function (rv)
if rv >= 0 then
print("Connected")
socket:read()
socket.readCallback = function (str)
print(str)
timer = lua.timer.createTimer(0)
timer:start(2000,function ()
socket:write(str)
socket:read()
socket.writeCallback = function (rv)
print("write" .. rv)
socket:connect()
通知接口
app开发中经常会遇到需要一对多的通知场景,例如ios有系统提供Notification Center 来提供,为了跨平台的实现通知,Luakit也提供通知接口
Lua register and post notification
lua.notification.createListener(function (l)
local listener = l
listener:AddObserver(3,
function (data)
print("lua Observer")
if data then
for k,v in pairs(data) do
print("lua Observer"..k..v)
end);
lua.notification.postNotification(3,
lua1 = "lua123",
lua2 = "lua234"})