annotate Libs/AceDB-3.0/AceDB-3.0.lua @ 0:169f5211fc7f

First public revision. At this point ItemAuditor watches mail for auctions sold or purchased, watches for buy/sell (money and 1 item type change) and conversions/tradeskills. Milling isn't working yet because there is too much time between the first event and the last event.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Thu, 20 May 2010 19:22:19 -0700
parents
children
rev   line source
Asa@0 1 --- **AceDB-3.0** manages the SavedVariables of your addon.
Asa@0 2 -- It offers profile management, smart defaults and namespaces for modules.\\
Asa@0 3 -- Data can be saved in different data-types, depending on its intended usage.
Asa@0 4 -- The most common data-type is the `profile` type, which allows the user to choose
Asa@0 5 -- the active profile, and manage the profiles of all of his characters.\\
Asa@0 6 -- The following data types are available:
Asa@0 7 -- * **char** Character-specific data. Every character has its own database.
Asa@0 8 -- * **realm** Realm-specific data. All of the players characters on the same realm share this database.
Asa@0 9 -- * **class** Class-specific data. All of the players characters of the same class share this database.
Asa@0 10 -- * **race** Race-specific data. All of the players characters of the same race share this database.
Asa@0 11 -- * **faction** Faction-specific data. All of the players characters of the same faction share this database.
Asa@0 12 -- * **factionrealm** Faction and realm specific data. All of the players characters on the same realm and of the same faction share this database.
Asa@0 13 -- * **global** Global Data. All characters on the same account share this database.
Asa@0 14 -- * **profile** Profile-specific data. All characters using the same profile share this database. The user can control which profile should be used.
Asa@0 15 --
Asa@0 16 -- Creating a new Database using the `:New` function will return a new DBObject. A database will inherit all functions
Asa@0 17 -- of the DBObjectLib listed here. \\
Asa@0 18 -- If you create a new namespaced child-database (`:RegisterNamespace`), you'll get a DBObject as well, but note
Asa@0 19 -- that the child-databases cannot individually change their profile, and are linked to their parents profile - and because of that,
Asa@0 20 -- the profile related APIs are not available. Only `:RegisterDefaults` and `:ResetProfile` are available on child-databases.
Asa@0 21 --
Asa@0 22 -- For more details on how to use AceDB-3.0, see the [[AceDB-3.0 Tutorial]].
Asa@0 23 --
Asa@0 24 -- You may also be interested in [[libdualspec-1-0|LibDualSpec-1.0]] to do profile switching automatically when switching specs.
Asa@0 25 --
Asa@0 26 -- @usage
Asa@0 27 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("DBExample")
Asa@0 28 --
Asa@0 29 -- -- declare defaults to be used in the DB
Asa@0 30 -- local defaults = {
Asa@0 31 -- profile = {
Asa@0 32 -- setting = true,
Asa@0 33 -- }
Asa@0 34 -- }
Asa@0 35 --
Asa@0 36 -- function MyAddon:OnInitialize()
Asa@0 37 -- -- Assuming the .toc says ## SavedVariables: MyAddonDB
Asa@0 38 -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true)
Asa@0 39 -- end
Asa@0 40 -- @class file
Asa@0 41 -- @name AceDB-3.0.lua
Asa@0 42 -- @release $Id: AceDB-3.0.lua 877 2009-11-02 15:56:50Z nevcairiel $
Asa@0 43 local ACEDB_MAJOR, ACEDB_MINOR = "AceDB-3.0", 19
Asa@0 44 local AceDB, oldminor = LibStub:NewLibrary(ACEDB_MAJOR, ACEDB_MINOR)
Asa@0 45
Asa@0 46 if not AceDB then return end -- No upgrade needed
Asa@0 47
Asa@0 48 -- Lua APIs
Asa@0 49 local type, pairs, next, error = type, pairs, next, error
Asa@0 50 local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget
Asa@0 51
Asa@0 52 -- WoW APIs
Asa@0 53 local _G = _G
Asa@0 54
Asa@0 55 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
Asa@0 56 -- List them here for Mikk's FindGlobals script
Asa@0 57 -- GLOBALS: LibStub
Asa@0 58
Asa@0 59 AceDB.db_registry = AceDB.db_registry or {}
Asa@0 60 AceDB.frame = AceDB.frame or CreateFrame("Frame")
Asa@0 61
Asa@0 62 local CallbackHandler
Asa@0 63 local CallbackDummy = { Fire = function() end }
Asa@0 64
Asa@0 65 local DBObjectLib = {}
Asa@0 66
Asa@0 67 --[[-------------------------------------------------------------------------
Asa@0 68 AceDB Utility Functions
Asa@0 69 ---------------------------------------------------------------------------]]
Asa@0 70
Asa@0 71 -- Simple shallow copy for copying defaults
Asa@0 72 local function copyTable(src, dest)
Asa@0 73 if type(dest) ~= "table" then dest = {} end
Asa@0 74 if type(src) == "table" then
Asa@0 75 for k,v in pairs(src) do
Asa@0 76 if type(v) == "table" then
Asa@0 77 -- try to index the key first so that the metatable creates the defaults, if set, and use that table
Asa@0 78 v = copyTable(v, dest[k])
Asa@0 79 end
Asa@0 80 dest[k] = v
Asa@0 81 end
Asa@0 82 end
Asa@0 83 return dest
Asa@0 84 end
Asa@0 85
Asa@0 86 -- Called to add defaults to a section of the database
Asa@0 87 --
Asa@0 88 -- When a ["*"] default section is indexed with a new key, a table is returned
Asa@0 89 -- and set in the host table. These tables must be cleaned up by removeDefaults
Asa@0 90 -- in order to ensure we don't write empty default tables.
Asa@0 91 local function copyDefaults(dest, src)
Asa@0 92 -- this happens if some value in the SV overwrites our default value with a non-table
Asa@0 93 --if type(dest) ~= "table" then return end
Asa@0 94 for k, v in pairs(src) do
Asa@0 95 if k == "*" or k == "**" then
Asa@0 96 if type(v) == "table" then
Asa@0 97 -- This is a metatable used for table defaults
Asa@0 98 local mt = {
Asa@0 99 -- This handles the lookup and creation of new subtables
Asa@0 100 __index = function(t,k)
Asa@0 101 if k == nil then return nil end
Asa@0 102 local tbl = {}
Asa@0 103 copyDefaults(tbl, v)
Asa@0 104 rawset(t, k, tbl)
Asa@0 105 return tbl
Asa@0 106 end,
Asa@0 107 }
Asa@0 108 setmetatable(dest, mt)
Asa@0 109 -- handle already existing tables in the SV
Asa@0 110 for dk, dv in pairs(dest) do
Asa@0 111 if not rawget(src, dk) and type(dv) == "table" then
Asa@0 112 copyDefaults(dv, v)
Asa@0 113 end
Asa@0 114 end
Asa@0 115 else
Asa@0 116 -- Values are not tables, so this is just a simple return
Asa@0 117 local mt = {__index = function(t,k) return k~=nil and v or nil end}
Asa@0 118 setmetatable(dest, mt)
Asa@0 119 end
Asa@0 120 elseif type(v) == "table" then
Asa@0 121 if not rawget(dest, k) then rawset(dest, k, {}) end
Asa@0 122 if type(dest[k]) == "table" then
Asa@0 123 copyDefaults(dest[k], v)
Asa@0 124 if src['**'] then
Asa@0 125 copyDefaults(dest[k], src['**'])
Asa@0 126 end
Asa@0 127 end
Asa@0 128 else
Asa@0 129 if rawget(dest, k) == nil then
Asa@0 130 rawset(dest, k, v)
Asa@0 131 end
Asa@0 132 end
Asa@0 133 end
Asa@0 134 end
Asa@0 135
Asa@0 136 -- Called to remove all defaults in the default table from the database
Asa@0 137 local function removeDefaults(db, defaults, blocker)
Asa@0 138 -- remove all metatables from the db, so we don't accidentally create new sub-tables through them
Asa@0 139 setmetatable(db, nil)
Asa@0 140 -- loop through the defaults and remove their content
Asa@0 141 for k,v in pairs(defaults) do
Asa@0 142 if k == "*" or k == "**" then
Asa@0 143 if type(v) == "table" then
Asa@0 144 -- Loop through all the actual k,v pairs and remove
Asa@0 145 for key, value in pairs(db) do
Asa@0 146 if type(value) == "table" then
Asa@0 147 -- if the key was not explicitly specified in the defaults table, just strip everything from * and ** tables
Asa@0 148 if defaults[key] == nil and (not blocker or blocker[key] == nil) then
Asa@0 149 removeDefaults(value, v)
Asa@0 150 -- if the table is empty afterwards, remove it
Asa@0 151 if next(value) == nil then
Asa@0 152 db[key] = nil
Asa@0 153 end
Asa@0 154 -- if it was specified, only strip ** content, but block values which were set in the key table
Asa@0 155 elseif k == "**" then
Asa@0 156 removeDefaults(value, v, defaults[key])
Asa@0 157 end
Asa@0 158 end
Asa@0 159 end
Asa@0 160 elseif k == "*" then
Asa@0 161 -- check for non-table default
Asa@0 162 for key, value in pairs(db) do
Asa@0 163 if defaults[key] == nil and v == value then
Asa@0 164 db[key] = nil
Asa@0 165 end
Asa@0 166 end
Asa@0 167 end
Asa@0 168 elseif type(v) == "table" and type(db[k]) == "table" then
Asa@0 169 -- if a blocker was set, dive into it, to allow multi-level defaults
Asa@0 170 removeDefaults(db[k], v, blocker and blocker[k])
Asa@0 171 if next(db[k]) == nil then
Asa@0 172 db[k] = nil
Asa@0 173 end
Asa@0 174 else
Asa@0 175 -- check if the current value matches the default, and that its not blocked by another defaults table
Asa@0 176 if db[k] == defaults[k] and (not blocker or blocker[k] == nil) then
Asa@0 177 db[k] = nil
Asa@0 178 end
Asa@0 179 end
Asa@0 180 end
Asa@0 181 end
Asa@0 182
Asa@0 183 -- This is called when a table section is first accessed, to set up the defaults
Asa@0 184 local function initSection(db, section, svstore, key, defaults)
Asa@0 185 local sv = rawget(db, "sv")
Asa@0 186
Asa@0 187 local tableCreated
Asa@0 188 if not sv[svstore] then sv[svstore] = {} end
Asa@0 189 if not sv[svstore][key] then
Asa@0 190 sv[svstore][key] = {}
Asa@0 191 tableCreated = true
Asa@0 192 end
Asa@0 193
Asa@0 194 local tbl = sv[svstore][key]
Asa@0 195
Asa@0 196 if defaults then
Asa@0 197 copyDefaults(tbl, defaults)
Asa@0 198 end
Asa@0 199 rawset(db, section, tbl)
Asa@0 200
Asa@0 201 return tableCreated, tbl
Asa@0 202 end
Asa@0 203
Asa@0 204 -- Metatable to handle the dynamic creation of sections and copying of sections.
Asa@0 205 local dbmt = {
Asa@0 206 __index = function(t, section)
Asa@0 207 local keys = rawget(t, "keys")
Asa@0 208 local key = keys[section]
Asa@0 209 if key then
Asa@0 210 local defaultTbl = rawget(t, "defaults")
Asa@0 211 local defaults = defaultTbl and defaultTbl[section]
Asa@0 212
Asa@0 213 if section == "profile" then
Asa@0 214 local new = initSection(t, section, "profiles", key, defaults)
Asa@0 215 if new then
Asa@0 216 -- Callback: OnNewProfile, database, newProfileKey
Asa@0 217 t.callbacks:Fire("OnNewProfile", t, key)
Asa@0 218 end
Asa@0 219 elseif section == "profiles" then
Asa@0 220 local sv = rawget(t, "sv")
Asa@0 221 if not sv.profiles then sv.profiles = {} end
Asa@0 222 rawset(t, "profiles", sv.profiles)
Asa@0 223 elseif section == "global" then
Asa@0 224 local sv = rawget(t, "sv")
Asa@0 225 if not sv.global then sv.global = {} end
Asa@0 226 if defaults then
Asa@0 227 copyDefaults(sv.global, defaults)
Asa@0 228 end
Asa@0 229 rawset(t, section, sv.global)
Asa@0 230 else
Asa@0 231 initSection(t, section, section, key, defaults)
Asa@0 232 end
Asa@0 233 end
Asa@0 234
Asa@0 235 return rawget(t, section)
Asa@0 236 end
Asa@0 237 }
Asa@0 238
Asa@0 239 local function validateDefaults(defaults, keyTbl, offset)
Asa@0 240 if not defaults then return end
Asa@0 241 offset = offset or 0
Asa@0 242 for k in pairs(defaults) do
Asa@0 243 if not keyTbl[k] or k == "profiles" then
Asa@0 244 error(("Usage: AceDBObject:RegisterDefaults(defaults): '%s' is not a valid datatype."):format(k), 3 + offset)
Asa@0 245 end
Asa@0 246 end
Asa@0 247 end
Asa@0 248
Asa@0 249 local preserve_keys = {
Asa@0 250 ["callbacks"] = true,
Asa@0 251 ["RegisterCallback"] = true,
Asa@0 252 ["UnregisterCallback"] = true,
Asa@0 253 ["UnregisterAllCallbacks"] = true,
Asa@0 254 ["children"] = true,
Asa@0 255 }
Asa@0 256
Asa@0 257 local realmKey = GetRealmName()
Asa@0 258 local charKey = UnitName("player") .. " - " .. realmKey
Asa@0 259 local _, classKey = UnitClass("player")
Asa@0 260 local _, raceKey = UnitRace("player")
Asa@0 261 local factionKey = UnitFactionGroup("player")
Asa@0 262 local factionrealmKey = factionKey .. " - " .. realmKey
Asa@0 263 -- Actual database initialization function
Asa@0 264 local function initdb(sv, defaults, defaultProfile, olddb, parent)
Asa@0 265 -- Generate the database keys for each section
Asa@0 266
Asa@0 267 -- map "true" to our "Default" profile
Asa@0 268 if defaultProfile == true then defaultProfile = "Default" end
Asa@0 269
Asa@0 270 local profileKey
Asa@0 271 if not parent then
Asa@0 272 -- Make a container for profile keys
Asa@0 273 if not sv.profileKeys then sv.profileKeys = {} end
Asa@0 274
Asa@0 275 -- Try to get the profile selected from the char db
Asa@0 276 profileKey = sv.profileKeys[charKey] or defaultProfile or charKey
Asa@0 277
Asa@0 278 -- save the selected profile for later
Asa@0 279 sv.profileKeys[charKey] = profileKey
Asa@0 280 else
Asa@0 281 -- Use the profile of the parents DB
Asa@0 282 profileKey = parent.keys.profile or defaultProfile or charKey
Asa@0 283
Asa@0 284 -- clear the profileKeys in the DB, namespaces don't need to store them
Asa@0 285 sv.profileKeys = nil
Asa@0 286 end
Asa@0 287
Asa@0 288 -- This table contains keys that enable the dynamic creation
Asa@0 289 -- of each section of the table. The 'global' and 'profiles'
Asa@0 290 -- have a key of true, since they are handled in a special case
Asa@0 291 local keyTbl= {
Asa@0 292 ["char"] = charKey,
Asa@0 293 ["realm"] = realmKey,
Asa@0 294 ["class"] = classKey,
Asa@0 295 ["race"] = raceKey,
Asa@0 296 ["faction"] = factionKey,
Asa@0 297 ["factionrealm"] = factionrealmKey,
Asa@0 298 ["profile"] = profileKey,
Asa@0 299 ["global"] = true,
Asa@0 300 ["profiles"] = true,
Asa@0 301 }
Asa@0 302
Asa@0 303 validateDefaults(defaults, keyTbl, 1)
Asa@0 304
Asa@0 305 -- This allows us to use this function to reset an entire database
Asa@0 306 -- Clear out the old database
Asa@0 307 if olddb then
Asa@0 308 for k,v in pairs(olddb) do if not preserve_keys[k] then olddb[k] = nil end end
Asa@0 309 end
Asa@0 310
Asa@0 311 -- Give this database the metatable so it initializes dynamically
Asa@0 312 local db = setmetatable(olddb or {}, dbmt)
Asa@0 313
Asa@0 314 if not rawget(db, "callbacks") then
Asa@0 315 -- try to load CallbackHandler-1.0 if it loaded after our library
Asa@0 316 if not CallbackHandler then CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0", true) end
Asa@0 317 db.callbacks = CallbackHandler and CallbackHandler:New(db) or CallbackDummy
Asa@0 318 end
Asa@0 319
Asa@0 320 -- Copy methods locally into the database object, to avoid hitting
Asa@0 321 -- the metatable when calling methods
Asa@0 322
Asa@0 323 if not parent then
Asa@0 324 for name, func in pairs(DBObjectLib) do
Asa@0 325 db[name] = func
Asa@0 326 end
Asa@0 327 else
Asa@0 328 -- hack this one in
Asa@0 329 db.RegisterDefaults = DBObjectLib.RegisterDefaults
Asa@0 330 db.ResetProfile = DBObjectLib.ResetProfile
Asa@0 331 end
Asa@0 332
Asa@0 333 -- Set some properties in the database object
Asa@0 334 db.profiles = sv.profiles
Asa@0 335 db.keys = keyTbl
Asa@0 336 db.sv = sv
Asa@0 337 --db.sv_name = name
Asa@0 338 db.defaults = defaults
Asa@0 339 db.parent = parent
Asa@0 340
Asa@0 341 -- store the DB in the registry
Asa@0 342 AceDB.db_registry[db] = true
Asa@0 343
Asa@0 344 return db
Asa@0 345 end
Asa@0 346
Asa@0 347 -- handle PLAYER_LOGOUT
Asa@0 348 -- strip all defaults from all databases
Asa@0 349 local function logoutHandler(frame, event)
Asa@0 350 if event == "PLAYER_LOGOUT" then
Asa@0 351 for db in pairs(AceDB.db_registry) do
Asa@0 352 db.callbacks:Fire("OnDatabaseShutdown", db)
Asa@0 353 for section, key in pairs(db.keys) do
Asa@0 354 if db.defaults and db.defaults[section] and rawget(db, section) then
Asa@0 355 removeDefaults(db[section], db.defaults[section])
Asa@0 356 end
Asa@0 357 end
Asa@0 358 end
Asa@0 359 end
Asa@0 360 end
Asa@0 361
Asa@0 362 AceDB.frame:RegisterEvent("PLAYER_LOGOUT")
Asa@0 363 AceDB.frame:SetScript("OnEvent", logoutHandler)
Asa@0 364
Asa@0 365
Asa@0 366 --[[-------------------------------------------------------------------------
Asa@0 367 AceDB Object Method Definitions
Asa@0 368 ---------------------------------------------------------------------------]]
Asa@0 369
Asa@0 370 --- Sets the defaults table for the given database object by clearing any
Asa@0 371 -- that are currently set, and then setting the new defaults.
Asa@0 372 -- @param defaults A table of defaults for this database
Asa@0 373 function DBObjectLib:RegisterDefaults(defaults)
Asa@0 374 if defaults and type(defaults) ~= "table" then
Asa@0 375 error("Usage: AceDBObject:RegisterDefaults(defaults): 'defaults' - table or nil expected.", 2)
Asa@0 376 end
Asa@0 377
Asa@0 378 validateDefaults(defaults, self.keys)
Asa@0 379
Asa@0 380 -- Remove any currently set defaults
Asa@0 381 if self.defaults then
Asa@0 382 for section,key in pairs(self.keys) do
Asa@0 383 if self.defaults[section] and rawget(self, section) then
Asa@0 384 removeDefaults(self[section], self.defaults[section])
Asa@0 385 end
Asa@0 386 end
Asa@0 387 end
Asa@0 388
Asa@0 389 -- Set the DBObject.defaults table
Asa@0 390 self.defaults = defaults
Asa@0 391
Asa@0 392 -- Copy in any defaults, only touching those sections already created
Asa@0 393 if defaults then
Asa@0 394 for section,key in pairs(self.keys) do
Asa@0 395 if defaults[section] and rawget(self, section) then
Asa@0 396 copyDefaults(self[section], defaults[section])
Asa@0 397 end
Asa@0 398 end
Asa@0 399 end
Asa@0 400 end
Asa@0 401
Asa@0 402 --- Changes the profile of the database and all of it's namespaces to the
Asa@0 403 -- supplied named profile
Asa@0 404 -- @param name The name of the profile to set as the current profile
Asa@0 405 function DBObjectLib:SetProfile(name)
Asa@0 406 if type(name) ~= "string" then
Asa@0 407 error("Usage: AceDBObject:SetProfile(name): 'name' - string expected.", 2)
Asa@0 408 end
Asa@0 409
Asa@0 410 -- changing to the same profile, dont do anything
Asa@0 411 if name == self.keys.profile then return end
Asa@0 412
Asa@0 413 local oldProfile = self.profile
Asa@0 414 local defaults = self.defaults and self.defaults.profile
Asa@0 415
Asa@0 416 -- Callback: OnProfileShutdown, database
Asa@0 417 self.callbacks:Fire("OnProfileShutdown", self)
Asa@0 418
Asa@0 419 if oldProfile and defaults then
Asa@0 420 -- Remove the defaults from the old profile
Asa@0 421 removeDefaults(oldProfile, defaults)
Asa@0 422 end
Asa@0 423
Asa@0 424 self.profile = nil
Asa@0 425 self.keys["profile"] = name
Asa@0 426
Asa@0 427 -- if the storage exists, save the new profile
Asa@0 428 -- this won't exist on namespaces.
Asa@0 429 if self.sv.profileKeys then
Asa@0 430 self.sv.profileKeys[charKey] = name
Asa@0 431 end
Asa@0 432
Asa@0 433 -- populate to child namespaces
Asa@0 434 if self.children then
Asa@0 435 for _, db in pairs(self.children) do
Asa@0 436 DBObjectLib.SetProfile(db, name)
Asa@0 437 end
Asa@0 438 end
Asa@0 439
Asa@0 440 -- Callback: OnProfileChanged, database, newProfileKey
Asa@0 441 self.callbacks:Fire("OnProfileChanged", self, name)
Asa@0 442 end
Asa@0 443
Asa@0 444 --- Returns a table with the names of the existing profiles in the database.
Asa@0 445 -- You can optionally supply a table to re-use for this purpose.
Asa@0 446 -- @param tbl A table to store the profile names in (optional)
Asa@0 447 function DBObjectLib:GetProfiles(tbl)
Asa@0 448 if tbl and type(tbl) ~= "table" then
Asa@0 449 error("Usage: AceDBObject:GetProfiles(tbl): 'tbl' - table or nil expected.", 2)
Asa@0 450 end
Asa@0 451
Asa@0 452 -- Clear the container table
Asa@0 453 if tbl then
Asa@0 454 for k,v in pairs(tbl) do tbl[k] = nil end
Asa@0 455 else
Asa@0 456 tbl = {}
Asa@0 457 end
Asa@0 458
Asa@0 459 local curProfile = self.keys.profile
Asa@0 460
Asa@0 461 local i = 0
Asa@0 462 for profileKey in pairs(self.profiles) do
Asa@0 463 i = i + 1
Asa@0 464 tbl[i] = profileKey
Asa@0 465 if curProfile and profileKey == curProfile then curProfile = nil end
Asa@0 466 end
Asa@0 467
Asa@0 468 -- Add the current profile, if it hasn't been created yet
Asa@0 469 if curProfile then
Asa@0 470 i = i + 1
Asa@0 471 tbl[i] = curProfile
Asa@0 472 end
Asa@0 473
Asa@0 474 return tbl, i
Asa@0 475 end
Asa@0 476
Asa@0 477 --- Returns the current profile name used by the database
Asa@0 478 function DBObjectLib:GetCurrentProfile()
Asa@0 479 return self.keys.profile
Asa@0 480 end
Asa@0 481
Asa@0 482 --- Deletes a named profile. This profile must not be the active profile.
Asa@0 483 -- @param name The name of the profile to be deleted
Asa@0 484 -- @param silent If true, do not raise an error when the profile does not exist
Asa@0 485 function DBObjectLib:DeleteProfile(name, silent)
Asa@0 486 if type(name) ~= "string" then
Asa@0 487 error("Usage: AceDBObject:DeleteProfile(name): 'name' - string expected.", 2)
Asa@0 488 end
Asa@0 489
Asa@0 490 if self.keys.profile == name then
Asa@0 491 error("Cannot delete the active profile in an AceDBObject.", 2)
Asa@0 492 end
Asa@0 493
Asa@0 494 if not rawget(self.sv.profiles, name) and not silent then
Asa@0 495 error("Cannot delete profile '" .. name .. "'. It does not exist.", 2)
Asa@0 496 end
Asa@0 497
Asa@0 498 self.sv.profiles[name] = nil
Asa@0 499
Asa@0 500 -- populate to child namespaces
Asa@0 501 if self.children then
Asa@0 502 for _, db in pairs(self.children) do
Asa@0 503 DBObjectLib.DeleteProfile(db, name, true)
Asa@0 504 end
Asa@0 505 end
Asa@0 506
Asa@0 507 -- Callback: OnProfileDeleted, database, profileKey
Asa@0 508 self.callbacks:Fire("OnProfileDeleted", self, name)
Asa@0 509 end
Asa@0 510
Asa@0 511 --- Copies a named profile into the current profile, overwriting any conflicting
Asa@0 512 -- settings.
Asa@0 513 -- @param name The name of the profile to be copied into the current profile
Asa@0 514 -- @param silent If true, do not raise an error when the profile does not exist
Asa@0 515 function DBObjectLib:CopyProfile(name, silent)
Asa@0 516 if type(name) ~= "string" then
Asa@0 517 error("Usage: AceDBObject:CopyProfile(name): 'name' - string expected.", 2)
Asa@0 518 end
Asa@0 519
Asa@0 520 if name == self.keys.profile then
Asa@0 521 error("Cannot have the same source and destination profiles.", 2)
Asa@0 522 end
Asa@0 523
Asa@0 524 if not rawget(self.sv.profiles, name) and not silent then
Asa@0 525 error("Cannot copy profile '" .. name .. "'. It does not exist.", 2)
Asa@0 526 end
Asa@0 527
Asa@0 528 -- Reset the profile before copying
Asa@0 529 DBObjectLib.ResetProfile(self, nil, true)
Asa@0 530
Asa@0 531 local profile = self.profile
Asa@0 532 local source = self.sv.profiles[name]
Asa@0 533
Asa@0 534 copyTable(source, profile)
Asa@0 535
Asa@0 536 -- populate to child namespaces
Asa@0 537 if self.children then
Asa@0 538 for _, db in pairs(self.children) do
Asa@0 539 DBObjectLib.CopyProfile(db, name, true)
Asa@0 540 end
Asa@0 541 end
Asa@0 542
Asa@0 543 -- Callback: OnProfileCopied, database, sourceProfileKey
Asa@0 544 self.callbacks:Fire("OnProfileCopied", self, name)
Asa@0 545 end
Asa@0 546
Asa@0 547 --- Resets the current profile to the default values (if specified).
Asa@0 548 -- @param noChildren if set to true, the reset will not be populated to the child namespaces of this DB object
Asa@0 549 -- @param noCallbacks if set to true, won't fire the OnProfileReset callback
Asa@0 550 function DBObjectLib:ResetProfile(noChildren, noCallbacks)
Asa@0 551 local profile = self.profile
Asa@0 552
Asa@0 553 for k,v in pairs(profile) do
Asa@0 554 profile[k] = nil
Asa@0 555 end
Asa@0 556
Asa@0 557 local defaults = self.defaults and self.defaults.profile
Asa@0 558 if defaults then
Asa@0 559 copyDefaults(profile, defaults)
Asa@0 560 end
Asa@0 561
Asa@0 562 -- populate to child namespaces
Asa@0 563 if self.children and not noChildren then
Asa@0 564 for _, db in pairs(self.children) do
Asa@0 565 DBObjectLib.ResetProfile(db, nil, noCallbacks)
Asa@0 566 end
Asa@0 567 end
Asa@0 568
Asa@0 569 -- Callback: OnProfileReset, database
Asa@0 570 if not noCallbacks then
Asa@0 571 self.callbacks:Fire("OnProfileReset", self)
Asa@0 572 end
Asa@0 573 end
Asa@0 574
Asa@0 575 --- Resets the entire database, using the string defaultProfile as the new default
Asa@0 576 -- profile.
Asa@0 577 -- @param defaultProfile The profile name to use as the default
Asa@0 578 function DBObjectLib:ResetDB(defaultProfile)
Asa@0 579 if defaultProfile and type(defaultProfile) ~= "string" then
Asa@0 580 error("Usage: AceDBObject:ResetDB(defaultProfile): 'defaultProfile' - string or nil expected.", 2)
Asa@0 581 end
Asa@0 582
Asa@0 583 local sv = self.sv
Asa@0 584 for k,v in pairs(sv) do
Asa@0 585 sv[k] = nil
Asa@0 586 end
Asa@0 587
Asa@0 588 local parent = self.parent
Asa@0 589
Asa@0 590 initdb(sv, self.defaults, defaultProfile, self)
Asa@0 591
Asa@0 592 -- fix the child namespaces
Asa@0 593 if self.children then
Asa@0 594 if not sv.namespaces then sv.namespaces = {} end
Asa@0 595 for name, db in pairs(self.children) do
Asa@0 596 if not sv.namespaces[name] then sv.namespaces[name] = {} end
Asa@0 597 initdb(sv.namespaces[name], db.defaults, self.keys.profile, db, self)
Asa@0 598 end
Asa@0 599 end
Asa@0 600
Asa@0 601 -- Callback: OnDatabaseReset, database
Asa@0 602 self.callbacks:Fire("OnDatabaseReset", self)
Asa@0 603 -- Callback: OnProfileChanged, database, profileKey
Asa@0 604 self.callbacks:Fire("OnProfileChanged", self, self.keys["profile"])
Asa@0 605
Asa@0 606 return self
Asa@0 607 end
Asa@0 608
Asa@0 609 --- Creates a new database namespace, directly tied to the database. This
Asa@0 610 -- is a full scale database in it's own rights other than the fact that
Asa@0 611 -- it cannot control its profile individually
Asa@0 612 -- @param name The name of the new namespace
Asa@0 613 -- @param defaults A table of values to use as defaults
Asa@0 614 function DBObjectLib:RegisterNamespace(name, defaults)
Asa@0 615 if type(name) ~= "string" then
Asa@0 616 error("Usage: AceDBObject:RegisterNamespace(name, defaults): 'name' - string expected.", 2)
Asa@0 617 end
Asa@0 618 if defaults and type(defaults) ~= "table" then
Asa@0 619 error("Usage: AceDBObject:RegisterNamespace(name, defaults): 'defaults' - table or nil expected.", 2)
Asa@0 620 end
Asa@0 621 if self.children and self.children[name] then
Asa@0 622 error ("Usage: AceDBObject:RegisterNamespace(name, defaults): 'name' - a namespace with that name already exists.", 2)
Asa@0 623 end
Asa@0 624
Asa@0 625 local sv = self.sv
Asa@0 626 if not sv.namespaces then sv.namespaces = {} end
Asa@0 627 if not sv.namespaces[name] then
Asa@0 628 sv.namespaces[name] = {}
Asa@0 629 end
Asa@0 630
Asa@0 631 local newDB = initdb(sv.namespaces[name], defaults, self.keys.profile, nil, self)
Asa@0 632
Asa@0 633 if not self.children then self.children = {} end
Asa@0 634 self.children[name] = newDB
Asa@0 635 return newDB
Asa@0 636 end
Asa@0 637
Asa@0 638 --- Returns an already existing namespace from the database object.
Asa@0 639 -- @param name The name of the new namespace
Asa@0 640 -- @param silent if true, the addon is optional, silently return nil if its not found
Asa@0 641 -- @usage
Asa@0 642 -- local namespace = self.db:GetNamespace('namespace')
Asa@0 643 -- @return the namespace object if found
Asa@0 644 function DBObjectLib:GetNamespace(name, silent)
Asa@0 645 if type(name) ~= "string" then
Asa@0 646 error("Usage: AceDBObject:GetNamespace(name): 'name' - string expected.", 2)
Asa@0 647 end
Asa@0 648 if not silent and not (self.children and self.children[name]) then
Asa@0 649 error ("Usage: AceDBObject:GetNamespace(name): 'name' - namespace does not exist.", 2)
Asa@0 650 end
Asa@0 651 if not self.children then self.children = {} end
Asa@0 652 return self.children[name]
Asa@0 653 end
Asa@0 654
Asa@0 655 --[[-------------------------------------------------------------------------
Asa@0 656 AceDB Exposed Methods
Asa@0 657 ---------------------------------------------------------------------------]]
Asa@0 658
Asa@0 659 --- Creates a new database object that can be used to handle database settings and profiles.
Asa@0 660 -- By default, an empty DB is created, using a character specific profile.
Asa@0 661 --
Asa@0 662 -- You can override the default profile used by passing any profile name as the third argument,
Asa@0 663 -- or by passing //true// as the third argument to use a globally shared profile called "Default".
Asa@0 664 --
Asa@0 665 -- Note that there is no token replacement in the default profile name, passing a defaultProfile as "char"
Asa@0 666 -- will use a profile named "char", and not a character-specific profile.
Asa@0 667 -- @param tbl The name of variable, or table to use for the database
Asa@0 668 -- @param defaults A table of database defaults
Asa@0 669 -- @param defaultProfile The name of the default profile. If not set, a character specific profile will be used as the default.
Asa@0 670 -- You can also pass //true// to use a shared global profile called "Default".
Asa@0 671 -- @usage
Asa@0 672 -- -- Create an empty DB using a character-specific default profile.
Asa@0 673 -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB")
Asa@0 674 -- @usage
Asa@0 675 -- -- Create a DB using defaults and using a shared default profile
Asa@0 676 -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true)
Asa@0 677 function AceDB:New(tbl, defaults, defaultProfile)
Asa@0 678 if type(tbl) == "string" then
Asa@0 679 local name = tbl
Asa@0 680 tbl = _G[name]
Asa@0 681 if not tbl then
Asa@0 682 tbl = {}
Asa@0 683 _G[name] = tbl
Asa@0 684 end
Asa@0 685 end
Asa@0 686
Asa@0 687 if type(tbl) ~= "table" then
Asa@0 688 error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'tbl' - table expected.", 2)
Asa@0 689 end
Asa@0 690
Asa@0 691 if defaults and type(defaults) ~= "table" then
Asa@0 692 error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'defaults' - table expected.", 2)
Asa@0 693 end
Asa@0 694
Asa@0 695 if defaultProfile and type(defaultProfile) ~= "string" and defaultProfile ~= true then
Asa@0 696 error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'defaultProfile' - string or true expected.", 2)
Asa@0 697 end
Asa@0 698
Asa@0 699 return initdb(tbl, defaults, defaultProfile)
Asa@0 700 end
Asa@0 701
Asa@0 702 -- upgrade existing databases
Asa@0 703 for db in pairs(AceDB.db_registry) do
Asa@0 704 if not db.parent then
Asa@0 705 for name,func in pairs(DBObjectLib) do
Asa@0 706 db[name] = func
Asa@0 707 end
Asa@0 708 else
Asa@0 709 db.RegisterDefaults = DBObjectLib.RegisterDefaults
Asa@0 710 db.ResetProfile = DBObjectLib.ResetProfile
Asa@0 711 end
Asa@0 712 end