On Oct 21, 2:42 pm, "sam.rawlins" <[email protected]> wrote:
> Similar 
> tohttp://groups.google.com/group/sequel-talk/browse_thread/thread/8f343...
> but I don't have a scheme for pairing a DB column name and a
> Sequel::Model's or Dataset's key.
>
> I'm using a legacy database with horrible, old table names and column
> names. Table names can be fixed with set_dataset (grep for set_dataset
> onhttp://sequel.rubyforge.org/rdoc/files/README_rdoc.html).
>
> Column names, however, are completely arbitrary. For examples:
>     I want :vendor_name to map to the VNDR_NM column,
>     I want :vendor_duns_number to map to the VNDR_DUNS_NBR column,
>     I want :vendor_payment_terms_code to map to the VNDR_PMT_TERM_CD
> column.
>
> In this way I could call Vendor.filter(:vendor_name => 'IBM') to
> access VNDR_T where VNDR_NM = 'IBM'
>
> So these would have to be pulled from a configuration file or some
> such (This is in fact how the java web app that uses the database maps
> to the crazy-name columns: with many lengthy XML files. The fun
> configuration-over-convention method :) ).
>
> I can worry about that side of this problem. I just want something
> similar to Sequel::Model#set_dataset for column names.

You would have to use an identifier_input_method to do the column
translation from vendor_name to VNDR_NM in the dataset.  And usually,
you would want a similar identifier_output_method that takes VNDR_NM
and returns vendor_name.

There a a two usual ways to do this (you could also use a combination
of the two):

  DB.identifier_input_method = :legacy_db_in
  DB.identifier_output_method = :legacy_db_out

  # Configuration Way
  LEGACY_DB_IN = {'vendor_name'=>'VNDR_NM', ...}
  LEGACY_DB_OUT = LEGACY_DB_IN.invert
  class String
    def legacy_db_in
      LEGACY_DB_IN.fetch(self, self)
    end
    def legacy_db_out
      LEGACY_DB_OUT.fetch(self, self)
    end
  end

  # Convention Way
  LEGACY_DB_IN = {'name'=>'NM', 'vendor'=>'VNDR', 'number'=>'NBR',
    'payment'=>'PMT', 'terms'=>'TERM', 'code'=>'CD', ...}
  LEGACY_DB_OUT = LEGACY_DB_IN.invert
  class String
    def legacy_db_in
      self.gsub(/[^_]+/){|s| LEGACY_DB_IN[s] || s.upcase}
    end
    def legacy_db_out
      self.gsub(/[^_]+/){|s| LEGACY_DB_OUT[s] || s.downcase}
    end
  end

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to