File: db.lua

package info (click to toggle)
eja 9.5.20-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 1,340 kB
  • ctags: 3,142
  • sloc: ansic: 15,010; makefile: 255
file content (88 lines) | stat: -rw-r--r-- 1,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- Copyright (C) 2007-2014 by Ubaldo Porcheddu <ubaldo@eja.it>


function ejaDbPath(name,id)
 local path=eja.pathVar
 if name and name:match('^/') then
  path=path..name:gsub('/([^/]*)$','/eja.%1')
 else
  path=path..'db/eja.'..name
 end
 path=path:gsub('//','/')
 if not ejaFileStat(path) then 
  ejaDirCreatePath(path:gsub('[^/]-$','')) 
 end
 if id then path=path..'.'..id end
 return path
end


function ejaDbPut(name,id,...)
 local o=''
 local a=ejaTablePack(...)
 for i=1,#a do
  o=o..tostring(a[i]):gsub('\t','eJaTaB')..'\t'
 end
 return ejaFileWrite(ejaDbPath(name,id),o)
end


function ejaDbDel(name,id)
 return ejaFileRemove(ejaDbPath(name,id))
end


function ejaDbNew(name,...)
 local last=ejaDbLast(name)+1
 if ejaDbPut(name,last,...) then
  return last
 else
  return nil
 end
end


function ejaDbGet(name,id,regex)
 local data=ejaFileRead(ejaDbPath(name,id))
 if data then
  if regex then
   return data:match(regex)
  else
   local i=0
   local a={}
   for v in data:gmatch('([^\t]*)\t?') do
    if v then a[#a+1]=v:gsub('eJaTaB','\t') end
   end
   a[#a]=nil
   return ejaTableUnpack(a)
  end
 else 
  return false
 end
end


function ejaDbLast(name)
 local last=0
 local path=ejaDbPath(name):match('(.+)/') or ''
 local file=name:match('([^/]-)$') or '' or ''
 local d=ejaDirList(path) or {}
 for k,v in next,d do
  local id=v:match('eja.'..file..'.([0-9]+)')
  if id and ejaNumber(id) > last then last=id end
 end
 return last
end


function ejaDbList(name)
 local a={}
 local path,name=ejaDbPath(name):match('(.-)/?eja%.(%w+)$')
 for k,v in next,ejaDirTable(path) do
  local id=v:match('^eja.'..name..'%.(%d*)$')
  if id then a[#a+1]=ejaNumber(id) end
 end
 return a
end