-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* UserDbReg : add db_pool_ Signed-off-by: shewer <[email protected]> * 改用 script 製作 db_pool_ Signed-off-by: shewer <[email protected]> --------- Signed-off-by: shewer <[email protected]>
- Loading branch information
Showing
2 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#! /usr/bin/env lua | ||
-- | ||
-- userdb.lua | ||
-- Copyright (C) 2024 Shewer Lu <[email protected]> | ||
-- | ||
-- Distributed under terms of the MIT license. | ||
-- | ||
--[[ | ||
example: | ||
local userdb = require 'userdb' | ||
local ldb=userdb.LevelDb('ecdict') | ||
ldb:open() | ||
for k,v in ldb:query('a'):iter() do print(k,v) end | ||
--]] | ||
local db_pool_ = {} | ||
local vars_get= { | ||
_loaded=true, | ||
read_only=true, | ||
disabled=true, | ||
name=true, | ||
file_name=true, | ||
} | ||
local vars_set= {} | ||
local userdb_mt = {} | ||
function userdb_mt.__newindex(tab,key,value) | ||
local db = db_pool_[tab._db_key] | ||
if not db then | ||
db = UserDb(tab._db_name, tab._db_class) | ||
db_pool_[tab._db_key] = db | ||
end | ||
if db and vars_set[key] then | ||
db[key]= value | ||
end | ||
end | ||
|
||
function userdb_mt.__index(tab,key) | ||
local db = db_pool_[tab._db_key] | ||
if not db then | ||
db = UserDb(tab._db_name, tab._db_class) | ||
db_pool_[tab._db_key] = db | ||
end | ||
|
||
if db and vars_get[key] then | ||
return db[key] | ||
else | ||
return function (tab, ...) | ||
return db[key](db,...) | ||
end | ||
end | ||
end | ||
|
||
local userdb= {} | ||
|
||
function userdb.UserDb(db_name, db_class) | ||
local db_key = db_name .. "." .. db_class | ||
local db = { | ||
_db_key = db_key, | ||
_db_name= db_name, | ||
_db_class = db_class, | ||
} | ||
db_pool_[db_key] = UserDb(db_name, db_class) | ||
return setmetatable(db , userdb_mt) | ||
end | ||
|
||
function userdb.LevelDb(db_name) | ||
return userdb.UserDb(db_name, "userdb") | ||
end | ||
function userdb.TableDb(db_name) | ||
return userdb.UserDb(db_name, "plain_userdb") | ||
end | ||
|
||
return userdb |
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