require 'rubygems' require 'sequel' require 'logger' require File.dirname(__FILE__) +'/../sequel/pervasive' Sequel::Model.plugin(:schema) $db = Sequel.connect('odbc:/FOS')#, :loggers => [ Logger.new( $stdout ) ] ) $db.extend(Sequel::Pervasive::DatabaseMethods) class XFILE < Sequel::Model( :'x$file' ) #~ xf$name, xf$loc, xf$flags, xf$reserved set_primary_key :'xf$id' one_to_many :fields, :class => :XFIELD, :key => :'xe$file' one_to_many :indices, :class => :XINDEX, :key => :'xi$file' end class XFIELD < Sequel::Model( :'x$field' ) #~ xe$file xe$name xe$datatype xe$offset xe$size xe$dec xe$flags set_primary_key :'xe$id' many_to_one :file, :class => :XFILE, :key => :'xe$file', :primary_key => :'xf$id' def <=>( other ) self[:'xe$name'] <=> other[:'xe$name'] end end class XINDEX < Sequel::Model( :'x$index' ) #~ xi$file xi$field xi$number xi$part xi$flags set_primary_key [:'xi$file', :'xi$field', :'xi$part'] many_to_one :field, :class => :XFIELD, :key => :'xi$field', :primary_key => :'xe$id' def <=>( other ) val = self[:'xi$number'] <=> other[:'xi$number'] val = self[:'xi$part'] <=> other[:'xi$part'] if val == 0 val end end class FOSClassGenerator CLASS_TEXT = <<-STR class CLASS_NAME < Sequel::Model(:'TABLE_NAME')\n SEQUEL_INFO end STR SEQUEL_TEXT = <<-STR #### BEGIN GENERATED SECTION #### \tset_primary_key [:'kid - user', :'kid - mult', :'kid - comm', :'kid - date', :'kid - time'] COLUMN_DEFINITIONS #### END GENERATED SECTION #### STR def initialize( file ) @file = file @table_name = file[:'xf$name'].downcase @escaped_table_name = @table_name.gsub('\'', "\\\\\\\\'") @class_name = make_pretty_table_name(@table_name) @file_name = make_pretty_file_name(@table_name) end def write_class_file file_name = File.join("..","..","models","#{@file_name}.rb") puts "filename=#{file_name}" file_str = File.exists?(file_name) ? File.read(file_name) : new_class_text sequel_part = build_sequel_section #~ puts sequel_part if @file_name == "aircraft" if( !file_str.sub!(/#### BEGIN GENERATED SECTION ####[^.|.]*#### END GENERATED SECTION ####/, sequel_part.chop) ) puts "CANNOT SUBSTITUTE NEW TEXT INTO OLD FILE for #{@file_name}" return end File.open(file_name,"w"){ |file| file.write(file_str) } end private def build_sequel_section text = SEQUEL_TEXT.dup column_info = @file.fields.sort.inject("") do |str, field| field_name = field[:'xe$name'].downcase str << "\n\tcolumn_alias :#{make_pretty_column_name(field_name)}, :'#{field_name.gsub("'", "\\\\\\\\\\\\\\\\'")}'" end text.sub!(/COLUMN_DEFINITIONS/,column_info) text end def new_class_text class_text = CLASS_TEXT.dup class_text.sub!(/CLASS_NAME/,@class_name) class_text.sub!(/TABLE_NAME/,@escaped_table_name) class_text.sub!(/SEQUEL_INFO/,SEQUEL_TEXT.dup) class_text end def make_pretty_table_name( ugly_name ) new_name = ugly_name.downcase.split( /[\s\/\'\-\&]/ ).collect { |v| v.capitalize }.join new_name.chop! if new_name.split( '' ).last.downcase == "s" new_name end def make_pretty_column_name( ugly_name ) better_name = ugly_name.gsub(' - ', ' ') slightly_better_name = better_name.gsub('.', '') even_better_name = slightly_better_name.gsub( /[\s\s]/, ' ' ) more_better_name = even_better_name.gsub('+', '_and_').gsub('<=', '_less_than_or_equal_to_').gsub('<', '_less_than_').gsub('>', '_more_than_').gsub('(text)', 'text').gsub('a/p', 'ap').gsub('a/c', 'ac') super_good_name = more_better_name.downcase.gsub( /[\s\/\'\-\&]/, "_" ) excellent_name = super_good_name.gsub(/_+/,"_") excellent_name end def make_pretty_file_name( ugly_name ) perfect_name = make_pretty_column_name(ugly_name) perfect_name.chop! if perfect_name[-1,1].downcase == "s" perfect_name.chop! if perfect_name[-1,1].downcase == "_" perfect_name = 'quote' if perfect_name == 'quotes_original' perfect_name = 'quote_leg' if perfect_name == 'quote_legs_original' perfect_name end end files = XFILE.filter(~:'xf$flags' => 16).and(:'xf$loc'.like('%.btr')).or(:'xf$loc'.like('%.BTR')) files.each do |file| p file[:'xf$name'] fos_class_gen = FOSClassGenerator.new(file) fos_class_gen.write_class_file end