简体   繁体   中英

Ruby inheritance of class and method in JDBC connection string

I have the following code snippet that generates a JDBC connection string for various databases. In this case, Dash DB and IBM DB2. The following is the structure of the code I use to generate my connection string in DB2.

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

Now, DashDB uses defaultSchema to define the default schema, while in DB2, it uses currentSchema . I am trying to minimize redundant code as much as possible, so right now, Dash DB's code inherits from DB2 using class HTDialectDashDB < HTDialectDB2 and override the one line/method that matters -- connection_string_primary_params()

In my Dash DB class, I am doing the following:

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

However, errors are being thrown that I must implement the four other methods described in the DB2 code, which I want to avoid, because it's just redundant code.

What can I do in this case?

对Dash DB和DB2都使用currentSchema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM