vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 local ACE_DB_TIMESTAMP_FMT = "%Y%m%d%H%M%S"
3  
4 -- Class Setup
5 AceDatabase = AceCore:new()
6 -- Compatibility reference, deprecated use
7 AceDbClass = AceDatabase
8  
9 -- Object constructor
10 function AceDatabase:new(val, seed)
11 o = ace:new(self, {seed=seed})
12 if( type(val)=="string" ) then
13 o.name = val
14 else
15 o._table = val or {}
16 o.initialized = TRUE
17 end
18 return o
19 end
20  
21 function AceDatabase:Initialize()
22 if( self.initialized ) then return end
23 self.initialized = TRUE
24  
25 self._table = getglobal(self.name)
26 if( self._table ) then return end
27  
28 self._table = {}
29 setglobal(self.name, self._table)
30 if( self.seed ) then
31 ace.CopyTable(self._table, self.seed)
32 end
33 self.created = TRUE
34  
35 return self.created
36 end
37  
38 function AceDatabase:_DelvePath(node, path)
39 local key, parent
40 for _, val in ipairs(path) do
41 parent = node
42 key = val
43 if( type(val)=="table" ) then
44 node, parent, key = self:_DelvePath(node, val)
45 if( not node ) then return end
46 elseif( not node[val] ) then
47 -- If we're not creating the path and the node doesn't exist, we're done.
48 if( not self.create ) then return end
49 node[val] = {}
50 node = node[val]
51 else
52 node = node[val]
53 end
54 end
55 return node, parent, key
56 end
57  
58 function AceDatabase:_GetNode(path, create)
59 if( not path ) then return self._table end
60 self.create = create
61 local node, parent, key = self:_DelvePath(self._table, path)
62 self.create = FALSE
63 return node, parent, key
64 end
65  
66 function AceDatabase:_GetArgs(arg)
67 if( type(arg[1]) == "table" ) then
68 return arg[1], arg[2], arg[3], arg[4]
69 end
70 return nil, arg[1], arg[2], arg[3]
71 end
72  
73 function AceDatabase:get(...)
74 if( getn(arg) < 1 ) then return self._table end
75 local path, key = self:_GetArgs(arg)
76 if (type(self:_GetNode(path) or {}) == "string") then
77 error("Bad Path", 2);
78 end
79 return (self:_GetNode(path) or {})[key]
80 end
81  
82 function AceDatabase:set(...)
83 local path, key, val = self:_GetArgs(arg)
84 local node = self:_GetNode(path, TRUE)
85 if( not key ) then error("No key supplied to AceDatabase:set.", 2) end
86 node[key] = val
87 return node[key]
88 end
89  
90 function AceDatabase:toggle(...)
91 local path, key = self:_GetArgs(arg)
92 local node = self:_GetNode(path, TRUE)
93 if( not key ) then error("No key supplied to AceDatabase:toggle.", 2) end
94 node[key] = ace.toggle(node[key])
95 return node[key]
96 end
97  
98 function AceDatabase:insert(...)
99 local path, key, val, pos = self:_GetArgs(arg)
100 local node = self:_GetNode(path, TRUE)
101 if( not key ) then error("No key supplied to AceDatabase:insert.", 2) end
102 if( not node[key] ) then node[key] = {} end
103 if( pos ) then tinsert(node[key], pos, val)
104 else tinsert(node[key], val)
105 end
106 end
107  
108 function AceDatabase:remove(...)
109 local path, key, pos = self:_GetArgs(arg)
110 local node = self:_GetNode(path, TRUE)
111 if( not key ) then error("No key supplied to AceDatabase:remove.", 2) end
112 return tremove(node[key], pos)
113 end
114  
115 function AceDatabase:reset(path, seed)
116 if( path ) then
117 local _, parent, key = self:_GetNode(path)
118 if( (not parent) or (not key) ) then return end
119 parent[key] = {}
120 if( seed ) then ace.CopyTable(parent[key], seed) end
121 return parent[key]
122 else
123 self._table = {}
124 if( self.name ) then setglobal(self.name, self._table) end
125 if( seed or self.seed ) then
126 ace.CopyTable(self._table, seed or self.seed)
127 end
128 return self._table
129 end
130 end
131  
132 function AceDatabase:timestamp()
133 return date(ACE_DB_TIMESTAMP_FMT)
134 end