Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- #$DBG = true
- require '/Users/john/build/cvs/nitro/nitro-0.40-own/glycerin'
- # a specific facets version
- require_gem 'facets', '= 1.4.5'
- require 'facets'
- require 'facets/core/string/camelize'
- require 'og'
- $psql = Og.run(
- :destroy => false,
- :evolve_schema => :add,
- :store => :postgresql,
- :name => 'oxyliquit',
- :user => 'john',
- :password => nil,
- :classes => [Class.new]
- )
- module RevOg
- ###################################################################
- # PostgreSQL side
- ###################################################################
- def get_og_tables
- tables = $psql.conn.query("
- SELECT c.oid, n.nspname, c.relname
- FROM pg_catalog.pg_class c
- LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
- WHERE c.relkind = 'r' AND
- n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
- pg_catalog.pg_table_is_visible(c.oid)
- ORDER BY 2, 3;
- ")
- ogtables = []
- ogreltables = []
- tables.each_row do |t, i|
- case t[tables.fields.index('relname')]
- when /^#{Og.table_prefix}j_/
- ogreltables << t
- when /^#{Og.table_prefix}/
- ogtables << t
- end
- end
- return th(tables, ogtables)
- end
- def get_klass_fields(oid)
- fields = $psql.conn.query %{
- SELECT a.attname AS column,
- pg_catalog.format_type(a.atttypid, a.atttypmod) AS type,
- (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
- FROM pg_catalog.pg_attrdef d
- WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum
- AND a.atthasdef) AS extrainfo,
- a.attnotnull, a.attnum
- FROM pg_catalog.pg_attribute a
- WHERE a.attrelid = '#{oid}' AND a.attnum > 0 AND NOT a.attisdropped
- ORDER BY a.attnum;}
- return th(fields)
- end
- ###################################################################
- # Ruby side
- ###################################################################
- def create_og_attribute(info)
- name = info['column']
- type = case info['type']
- when /timestamp/: Time
- when 'text': String
- when 'integer': Fixnum
- when 'boolean': TrueClass
- else Object
- end
- att = "attr_accessor :#{name}, #{type}"
- #att << ', :null => false' if info['attnotnull'] = 't'
- if info['extrainfo'] =~ /#{info['column']}_seq/
- att << ', :primary_key => true'
- end
- return att
- end
- ###################################################################
- # Aggregation
- ###################################################################
- def create_og_klasses
- klasses = []
- t = get_og_tables
- t.each do |info|
- klasses << create_og_klass(info)
- end
- return klasses
- end
- def create_og_klass(info)
- klass_name = info['relname'].scan(/^#{Og.table_prefix}(.*)/).to_s.camelize
- klass_flds = get_klass_fields(info['oid'])
- attrs = klass_flds.map {|info| create_og_attribute(info) }
- %{
- class #{klass_name}
- #{attrs.map{|x|" #{x}"}.join("\n")}
- end
- }
- end
- private
- def th(res, array = nil)
- unless array
- array = []
- res.each_row {|x,i| array << x }
- end
- out = []
- array.each do |row|
- rowh = {}
- row.each_with_index do |field,i|
- rowh[res.fields[i]] = field
- end
- out << rowh
- end
- return out
- end
- end
- if $0 == __FILE__
- include RevOg
- puts create_og_klasses
- end
Add Comment
Please, Sign In to add comment