2016-11-09 33 views
1

我有下面的代码片断,它为各种数据库生成一个JDBC连接字符串。在这种情况下,Dash DB和IBM DB2。以下是我用于在DB2中生成连接字符串的代码的结构。现在JDBC连接字符串中的类和方法的Ruby继承

def connection_creation_information 
    @connection_creation_information ||= Class.new(JDBCConnectionCreationInformation) do 

    private 

    def connection_string_primary_params(options) 
     params = [] 

     params << "currentSchema=#{options[:schema] || options[:username]}" 

     params << options[:jdbc_additional_params] if options[:jdbc_additional_params] 

     params 
    end 

    def connection_string_scheme 
     'db2' 
    end 

    def connection_string_params(options) 
     super + ';' 
    end 

    def primary_param_separator 
     ';' 
    end 

    def base_separator 
     ':' 
    end 
    end.new 
end 

,DashDB使用defaultSchema定义默认的模式,而在DB2,它使用currentSchema。我试图尽量减少冗余代码,所以现在,Dash DB的代码继承自DB2,使用class HTDialectDashDB < HTDialectDB2并覆盖重要的一行/方法 - connection_string_primary_params()

在我的Dash DB类中,我正在执行以下:

def connection_creation_information 
    @connection_creation_information ||= Class.new(JDBCConnectionCreationInformation) do 

    private 

    def connection_string_primary_params(options) 
     params = [] 

     params << "defaultSchema=#{options[:schema] || options[:username]}" 

     params << options[:jdbc_additional_params] if options[:jdbc_additional_params] 

     params 
    end 

    end.new 
end 

然而,错误被抛出,我必须实现DB2代码所描述的其他四个方法,这是我想避免的,因为它只是冗余代码。

我能在这种情况下做什么?

+2

无法帮助Ruby,但dashDB(基本上是DB2)对标准DB2连接字符串属性应该没问题。 – mustaccio

+0

你是对的!谢谢! – theGreenCabbage

回答