Guest User

Untitled

a guest
Mar 1st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. #$DBG = true
  4.  
  5. require '/Users/john/build/cvs/nitro/nitro-0.40-own/glycerin'
  6.  
  7. # a specific facets version
  8. require_gem 'facets', '= 1.4.5'
  9. require 'facets'
  10. require 'facets/core/string/camelize'
  11.  
  12. require 'og'
  13.  
  14. $psql = Og.run(
  15. :destroy => false,
  16. :evolve_schema => :add,
  17. :store => :postgresql,
  18. :name => 'oxyliquit',
  19. :user => 'john',
  20. :password => nil,
  21. :classes => [Class.new]
  22. )
  23.  
  24. module RevOg
  25.  
  26. ###################################################################
  27. # PostgreSQL side
  28. ###################################################################
  29.  
  30. def get_og_tables
  31. tables = $psql.conn.query("
  32. SELECT c.oid, n.nspname, c.relname
  33. FROM pg_catalog.pg_class c
  34. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  35. WHERE c.relkind = 'r' AND
  36. n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
  37. pg_catalog.pg_table_is_visible(c.oid)
  38. ORDER BY 2, 3;
  39. ")
  40.  
  41. ogtables = []
  42. ogreltables = []
  43.  
  44. tables.each_row do |t, i|
  45. case t[tables.fields.index('relname')]
  46. when /^#{Og.table_prefix}j_/
  47. ogreltables << t
  48. when /^#{Og.table_prefix}/
  49. ogtables << t
  50. end
  51. end
  52.  
  53. return th(tables, ogtables)
  54. end
  55.  
  56. def get_klass_fields(oid)
  57. fields = $psql.conn.query %{
  58. SELECT a.attname AS column,
  59. pg_catalog.format_type(a.atttypid, a.atttypmod) AS type,
  60. (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
  61. FROM pg_catalog.pg_attrdef d
  62. WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum
  63. AND a.atthasdef) AS extrainfo,
  64. a.attnotnull, a.attnum
  65. FROM pg_catalog.pg_attribute a
  66. WHERE a.attrelid = '#{oid}' AND a.attnum > 0 AND NOT a.attisdropped
  67. ORDER BY a.attnum;}
  68.  
  69. return th(fields)
  70. end
  71.  
  72. ###################################################################
  73. # Ruby side
  74. ###################################################################
  75.  
  76. def create_og_attribute(info)
  77. name = info['column']
  78. type = case info['type']
  79. when /timestamp/: Time
  80. when 'text': String
  81. when 'integer': Fixnum
  82. when 'boolean': TrueClass
  83. else Object
  84. end
  85.  
  86. att = "attr_accessor :#{name}, #{type}"
  87.  
  88. #att << ', :null => false' if info['attnotnull'] = 't'
  89. if info['extrainfo'] =~ /#{info['column']}_seq/
  90. att << ', :primary_key => true'
  91. end
  92.  
  93. return att
  94. end
  95.  
  96. ###################################################################
  97. # Aggregation
  98. ###################################################################
  99.  
  100. def create_og_klasses
  101. klasses = []
  102.  
  103. t = get_og_tables
  104.  
  105. t.each do |info|
  106. klasses << create_og_klass(info)
  107. end
  108.  
  109. return klasses
  110. end
  111.  
  112. def create_og_klass(info)
  113. klass_name = info['relname'].scan(/^#{Og.table_prefix}(.*)/).to_s.camelize
  114. klass_flds = get_klass_fields(info['oid'])
  115.  
  116. attrs = klass_flds.map {|info| create_og_attribute(info) }
  117.  
  118. %{
  119. class #{klass_name}
  120. #{attrs.map{|x|" #{x}"}.join("\n")}
  121. end
  122. }
  123. end
  124.  
  125. private
  126. def th(res, array = nil)
  127. unless array
  128. array = []
  129. res.each_row {|x,i| array << x }
  130. end
  131.  
  132. out = []
  133.  
  134. array.each do |row|
  135. rowh = {}
  136. row.each_with_index do |field,i|
  137. rowh[res.fields[i]] = field
  138. end
  139. out << rowh
  140. end
  141.  
  142. return out
  143. end
  144. end
  145.  
  146.  
  147. if $0 == __FILE__
  148. include RevOg
  149.  
  150. puts create_og_klasses
  151. end
Add Comment
Please, Sign In to add comment