Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. module ActiveRecord
  2. class Schema
  3.  
  4. def self.define(info={}, &block)
  5. yield block
  6. end
  7. end
  8. end
  9.  
  10. class PIQLTable
  11. attr_accessor :primary_keys, :foreign_keys, :had_fields
  12.  
  13. def initialize
  14. self.primary_keys = Array.new
  15. self.had_fields = false
  16. self.foreign_keys = Hash.new
  17. end
  18.  
  19. def method_missing(*args)
  20. type = args[0]
  21. name = args[1]
  22. options = args[2]
  23.  
  24. self.primary_keys << name if options and options[:primary_key]
  25.  
  26. print ",\n" if self.had_fields
  27. self.had_fields = true
  28.  
  29. if name == 'key' or name == 'value' or name == 'val' or name == 'type'
  30. print "ERROR: Cannot use \"#{name}\" as a field name\n"
  31. end
  32.  
  33. force_not_ref = (options and options.has_key?(:ref) and options[:ref] == nil)
  34.  
  35. if options and options[:ref]
  36. foreignKey(name, options[:ref])
  37. elsif type == :integer and m=name.match(/^(.+)_id$/) and not force_not_ref
  38. foreignKey(name, m[1])
  39. else
  40. type = :string if type == :text
  41. type = :int if type == :integer
  42. type = :int if type == :date
  43. type = :int if type == :datetime
  44. type = :bool if type == :boolean
  45. print "\t#{type} #{name}"
  46. end
  47. end
  48.  
  49. private
  50.  
  51. def foreignKey(name, ref)
  52. self.foreign_keys[name] = ref
  53. print "\tFOREIGN KEY #{name} REF #{singularize(capitalize(ref))}"
  54. end
  55. end
  56.  
  57. def camelize(name)
  58. words = name.split('_');
  59. return words[0] + (words[1, words.length-1].map {|w| w.capitalize}).join
  60. end
  61.  
  62. def capitalize(name)
  63. return (name.split('_').map {|w| w.capitalize}).join
  64. end
  65.  
  66. def singularize(name)
  67. m = name.match(/^(.*)s$/i)
  68. if m
  69. return m[1]
  70. else
  71. return name
  72. end
  73. end
  74.  
  75. def pluralize(name)
  76. return name + "s";
  77. end
  78.  
  79. $queries = Array.new
  80.  
  81. def create_table(name, options, &block)
  82. # We ignore all the options
  83.  
  84. entityName = capitalize(singularize(name))
  85.  
  86. t = PIQLTable.new
  87. print "ENTITY "+entityName+"\n"
  88. print "{\n"
  89. # print "\tint "+singularize(name)+"_id,\n"
  90. block.call(t)
  91.  
  92. print "\n"
  93.  
  94. if t.primary_keys.length > 0
  95. print "\tPRIMARY("+ t.primary_keys.join(', ') +")\n"
  96. else
  97. print "\tPRIMARY("+singularize(name)+"_id)\n"
  98. end
  99.  
  100. print "}\n\n"
  101.  
  102. t.foreign_keys.each_key do |name|
  103. if m=name.match(/^(.+)_id$/)
  104. queryName = m[1]
  105. else
  106. queryName = name
  107. end
  108. q = "\n" +
  109. "QUERY #{camelize(pluralize(queryName))}\n" +
  110. "FETCH #{capitalize(t.foreign_keys[name])}\n" +
  111. "\tOF #{entityName} BY #{name}\n" +
  112. "WHERE #{entityName} = [this]\n"
  113. $queries << q
  114. end
  115. end
  116.  
  117. def add_index(*args)
  118. # We ignore index because it's not used in PIQL
  119. end
  120.  
  121. require ARGV[0]
  122.  
  123. $queries.each {|q| print q}
Add Comment
Please, Sign In to add comment