zoukankan      html  css  js  c++  java
  • KONG DB抽象

    function DB.new(kong_config, strategy)
    if not kong_config then
    error("missing kong_config", 2)
    end

    if strategy ~= nil and type(strategy) ~= "string" then
    error("strategy must be a string", 2)
    end

    strategy = strategy or kong_config.database

    -- load errors

    local errors = Errors.new(strategy)

    local schemas = {}

    do
    -- load schemas
    -- core entities are for now the only source of schemas.
    -- TODO: support schemas from plugins entities as well.

    for _, entity_name in ipairs(constants.CORE_ENTITIES) do
    local entity_schema = require("kong.db.schema.entities." .. entity_name)

    -- validate core entities schema via metaschema
    local ok, err_t = MetaSchema:validate(entity_schema)
    if not ok then
    return nil, fmt("schema of entity '%s' is invalid: %s", entity_name,
    tostring(errors:schema_violation(err_t)))
    end
    local entity, err = Entity.new(entity_schema)
    if not entity then
    return nil, fmt("schema of entity '%s' is invalid: %s", entity_name,
    err)
    end
    schemas[entity_name] = entity
    -- load core entities subschemas
    local subschemas
    ok, subschemas = utils.load_module_if_exists("kong.db.schema.entities." .. entity_name .. "_subschemas")
    if ok then
    for name, subschema in pairs(subschemas) do
    local ok, err = entity:new_subschema(name, subschema)
    if not ok then
    return nil, ("error initializing schema for %s: %s"):format(entity_name, err)
    end
    end
    end
    end
    end

    -- load strategy
    //按照配置,构造返回特定类型数据库的连接器,针对每个实体对象的底层访问对像的字典strategies
    local connector, strategies, err = Strategies.new(kong_config, strategy,
    schemas, errors)
    if err then
    return nil, err
    end

    local daos = {}

    local self = {
    daos = daos, -- each of those has the connector singleton
    strategies = strategies,针对每个实体对象的底层访问对像的字典strategies
    connector = connector,
    strategy = strategy,
    errors = errors,
    infos = connector:infos(),
    kong_config = kong_config,
    }

    do
    -- load DAOs
    for _, schema in pairs(schemas) do
    local strategy = strategies[schema.name]
    if not strategy then
    return nil, fmt("no strategy found for schema '%s'", schema.name)
    end
          //DB中定义了daos的数据访问的抽象层字典,每个DAO封装了针对特定实体的底层数据访问对象strategy
    daos[schema.name] = DAO.new(self, schema, strategy, errors)
         
    end
    end

    -- we are 200 OK


    return setmetatable(self, DB)
    end
    db.plugins---当找不到时,就访问元表————index,方法内转发到self.daos[plugins] //新增插件时,在初始加载插件的时,会加载插件下的daos.lua文件,构建新的dao对象,添加到db的dao字典中
    注意schema定义中dao,扩展dao的访问方法(dao:new方法中会读取这个dao属性,并加载扩展当前dao对象
     
     
     
     
     
  • 相关阅读:
    【181】IDL 代码从 Windows 转移到 Linux
    异步加载图片到GridView上,防止OOM
    10881
    AngularJS的开发工具---yeoman 简易安装
    AIX操作oracle
    hdu2817 A sequence of numbers
    FZU 2104 (13.11.28)
    HDU 1231 (13.12.2)
    Java 23种设计模式详尽分析与实例解析之一--创建型模式
    JSP视频
  • 原文地址:https://www.cnblogs.com/justart/p/12443197.html
Copyright © 2011-2022 走看看