Class: CreateTable::Index

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/create_table/index.rb

Direct Known Subclasses

Unique

Constant Summary

Constants included from Parser

Parser::SQL_COMMENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#read, #read_quoted, remove_comments

Constructor Details

#initialize(parent) ⇒ Index

Returns a new instance of Index.



17
18
19
20
21
# File 'lib/create_table/index.rb', line 17

def initialize(parent)
  @parent = parent
  @column_names = []
  parent.indexes << self
end

Instance Attribute Details

#column_namesObject

Returns the value of attribute column_names.



14
15
16
# File 'lib/create_table/index.rb', line 14

def column_names
  @column_names
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/create_table/index.rb', line 15

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



13
14
15
# File 'lib/create_table/index.rb', line 13

def parent
  @parent
end

Instance Method Details

#parse(str) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/create_table/index.rb', line 27

def parse(str)
  data = Parser.remove_comments(str).unpack('c*')
  
class << self
	attr_accessor :_parser_actions
	private :_parser_actions, :_parser_actions=
end
self._parser_actions = [
	0, 1, 0, 1, 1, 1, 2, 1, 
	3
]

class << self
	attr_accessor :_parser_key_offsets
	private :_parser_key_offsets, :_parser_key_offsets=
end
self._parser_key_offsets = [
	0, 0, 10, 15, 27, 31, 40, 45, 
	55, 57, 67
]

class << self
	attr_accessor :_parser_trans_keys
	private :_parser_trans_keys, :_parser_trans_keys=
end
self._parser_trans_keys = [
	32, 34, 40, 96, 9, 13, 65, 90, 
	95, 122, 95, 65, 90, 97, 122, 32, 
	34, 40, 96, 9, 13, 48, 57, 65, 
	90, 95, 122, 32, 40, 9, 13, 32, 
	34, 96, 9, 13, 65, 90, 95, 122, 
	95, 65, 90, 97, 122, 34, 41, 44, 
	96, 48, 57, 65, 90, 95, 122, 41, 
	44, 32, 34, 41, 96, 9, 13, 65, 
	90, 95, 122, 32, 9, 13, 0
]

class << self
	attr_accessor :_parser_single_lengths
	private :_parser_single_lengths, :_parser_single_lengths=
end
self._parser_single_lengths = [
	0, 4, 1, 4, 2, 3, 1, 4, 
	2, 4, 1
]

class << self
	attr_accessor :_parser_range_lengths
	private :_parser_range_lengths, :_parser_range_lengths=
end
self._parser_range_lengths = [
	0, 3, 2, 4, 1, 3, 2, 3, 
	0, 3, 1
]

class << self
	attr_accessor :_parser_index_offsets
	private :_parser_index_offsets, :_parser_index_offsets=
end
self._parser_index_offsets = [
	0, 0, 8, 12, 21, 25, 32, 36, 
	44, 47, 55
]

class << self
	attr_accessor :_parser_indicies
	private :_parser_indicies, :_parser_indicies=
end
self._parser_indicies = [
	0, 2, 3, 2, 0, 4, 4, 1, 
	4, 4, 4, 1, 5, 5, 6, 5, 
	5, 7, 7, 7, 1, 8, 3, 8, 
	1, 3, 9, 9, 3, 10, 10, 1, 
	10, 10, 10, 1, 11, 12, 12, 11, 
	13, 13, 13, 1, 14, 14, 1, 14, 
	9, 15, 9, 14, 10, 10, 1, 15, 
	15, 1, 0
]

class << self
	attr_accessor :_parser_trans_targs
	private :_parser_trans_targs, :_parser_trans_targs=
end
self._parser_trans_targs = [
	1, 0, 2, 5, 3, 4, 5, 3, 
	4, 6, 7, 8, 9, 7, 9, 10
]

class << self
	attr_accessor :_parser_trans_actions
	private :_parser_trans_actions, :_parser_trans_actions=
end
self._parser_trans_actions = [
	0, 0, 0, 0, 1, 3, 3, 0, 
	0, 0, 5, 7, 7, 0, 0, 0
]

class << self
	attr_accessor :parser_start
end
self.parser_start = 1;
class << self
	attr_accessor :parser_first_final
end
self.parser_first_final = 10;
class << self
	attr_accessor :parser_error
end
self.parser_error = 0;

class << self
	attr_accessor :parser_en_main
end
self.parser_en_main = 1;


  # % (this fixes syntax highlighting)
  parens = 0
  p = item = 0
  pe = eof = data.length
  
begin
	p ||= 0
	pe ||= data.length
	cs = parser_start
end

  # % (this fixes syntax highlighting)
  
begin
	_klen, _trans, _keys, _acts, _nacts = nil
	_goto_level = 0
	_resume = 10
	_eof_trans = 15
	_again = 20
	_test_eof = 30
	_out = 40
	while true
	_trigger_goto = false
	if _goto_level <= 0
	if p == pe
		_goto_level = _test_eof
		next
	end
	if cs == 0
		_goto_level = _out
		next
	end
	end
	if _goto_level <= _resume
	_keys = _parser_key_offsets[cs]
	_trans = _parser_index_offsets[cs]
	_klen = _parser_single_lengths[cs]
	_break_match = false
	
	begin
	  if _klen > 0
  _lower = _keys
  _upper = _keys + _klen - 1

  loop do
     break if _upper < _lower
     _mid = _lower + ( (_upper - _lower) >> 1 )

     if data[p].ord < _parser_trans_keys[_mid]
        _upper = _mid - 1
     elsif data[p].ord > _parser_trans_keys[_mid]
        _lower = _mid + 1
     else
        _trans += (_mid - _keys)
        _break_match = true
        break
     end
  end # loop
  break if _break_match
  _keys += _klen
  _trans += _klen
	  end
	  _klen = _parser_range_lengths[cs]
	  if _klen > 0
  _lower = _keys
  _upper = _keys + (_klen << 1) - 2
  loop do
     break if _upper < _lower
     _mid = _lower + (((_upper-_lower) >> 1) & ~1)
     if data[p].ord < _parser_trans_keys[_mid]
       _upper = _mid - 2
     elsif data[p].ord > _parser_trans_keys[_mid+1]
       _lower = _mid + 2
     else
       _trans += ((_mid - _keys) >> 1)
       _break_match = true
       break
     end
  end # loop
  break if _break_match
  _trans += _klen
	  end
	end while false
	_trans = _parser_indicies[_trans]
	cs = _parser_trans_targs[_trans]
	if _parser_trans_actions[_trans] != 0
		_acts = _parser_trans_actions[_trans]
		_nacts = _parser_actions[_acts]
		_acts += 1
		while _nacts > 0
			_nacts -= 1
			_acts += 1
			case _parser_actions[_acts - 1]
when 0 then
		begin
 start_name = p                                    		end
when 1 then
		begin
 self.name = read(data, start_name, p)             		end
when 2 then
		begin
 start_column_name = p                             		end
when 3 then
		begin
 column_names << read(data, start_column_name, p)  		end
			end # action switch
		end
	end
	if _trigger_goto
		next
	end
	end
	if _goto_level <= _again
	if cs == 0
		_goto_level = _out
		next
	end
	p += 1
	if p != pe
		_goto_level = _resume
		next
	end
	end
	if _goto_level <= _test_eof
	end
	if _goto_level <= _out
		break
	end
	end
	end

  # % (this fixes syntax highlighting)
  self
end

#primary_keyObject



293
294
295
296
297
# File 'lib/create_table/index.rb', line 293

def primary_key
  if pk = parent.primary_key
    pk.column_names == column_names
  end
end

#quoted_column_names(options) ⇒ Object



309
310
311
312
313
# File 'lib/create_table/index.rb', line 309

def quoted_column_names(options)
  column_names.map do |column_name|
    CreateTable.quote_ident column_name, options
  end.join(', ')
end

#quoted_name(options) ⇒ Object



299
300
301
302
303
304
305
306
307
# File 'lib/create_table/index.rb', line 299

def quoted_name(options)
  if name
    CreateTable.quote_ident name, options
  elsif unique
    "uidx_#{parent.table_name}_on_#{name}"
  else
    "idx_#{parent.table_name}_on_#{name}"
  end
end

#to_sql(format, options) ⇒ Object



282
283
284
285
286
287
288
289
290
291
# File 'lib/create_table/index.rb', line 282

def to_sql(format, options)
  return if primary_key
  return if unique and name.nil?
  parts = []
  parts << 'CREATE'
  parts << 'UNIQUE' if (unique and name)
  parts << 'INDEX'
  parts += [ quoted_name(options), 'ON', parent.quoted_table_name(options), '(', quoted_column_names(options), ')' ]
  parts.join ' '
end

#uniqueObject



23
24
25
# File 'lib/create_table/index.rb', line 23

def unique
  false
end