Module: Sequel::Sequence::Database::Server::Mysql

Defined in:
lib/sequel/sequence/database/server/mysql.rb

Instance Method Summary collapse

Instance Method Details

#check_sequencesObject



8
9
10
# File 'lib/sequel/sequence/database/server/mysql.rb', line 8

def check_sequences
  fetch('SELECT * FROM mysql_sequence;').all.to_a
end

#create_sequence(name, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sequel/sequence/database/server/mysql.rb', line 22

def create_sequence(name, options = {})
  check_options(options)
  if_exists = build_exists_condition(options[:if_exists])
  start_option = options[:start] || 1
  num_label = options[:numeric_label] || 0
  return if (current = lastval(name)) && (current >= start_option)

  run create_sequence_table(stringify(name), if_exists)
  run insert_into_sequence_table_init_values(stringify(name), start_option, num_label)
  run create_mysql_sequence
  table_matcher { run delete_from_mysql_sequence(stringify(name)) }
  run insert_into_mysql_sequence(stringify(name), start_option)
end

#custom_sequence?(sequence_name) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'lib/sequel/sequence/database/server/mysql.rb', line 12

def custom_sequence?(sequence_name)
  table_matcher do
    out = nil
    fetch(select_from_mysql_sequence_where(stringify(sequence_name))) do |row|
      out = row[:name]
    end
    !out.nil?
  end || false
end

#delete_to_currval(name) ⇒ Object



86
87
88
# File 'lib/sequel/sequence/database/server/mysql.rb', line 86

def delete_to_currval(name)
  run delete_to_current_seq(stringify(name))
end

#drop_sequence(name, options = {}) ⇒ Object



36
37
38
39
40
# File 'lib/sequel/sequence/database/server/mysql.rb', line 36

def drop_sequence(name, options = {})
  if_exists = build_exists_condition(options[:if_exists])
  run drop_sequence_table(stringify(name), if_exists)
  table_matcher { run delete_from_mysql_sequence(stringify(name)) }
end

#lastval(name) ⇒ Object Also known as: currval



56
57
58
# File 'lib/sequel/sequence/database/server/mysql.rb', line 56

def lastval(name)
  take_seq(stringify(name))
end

#nextval(name) ⇒ Object



42
43
44
45
46
47
# File 'lib/sequel/sequence/database/server/mysql.rb', line 42

def nextval(name)
  run insert_into_sequence_table(stringify(name), 0)
  table_matcher { run delete_from_mysql_sequence(stringify(name)) }
  run insert_last_insert_id_into_mysql_sequence(stringify(name))
  take_seq(stringify(name))
end

#nextval_with_label(name, num_label = 0) ⇒ Object



49
50
51
52
53
54
# File 'lib/sequel/sequence/database/server/mysql.rb', line 49

def nextval_with_label(name, num_label = 0)
  run insert_into_sequence_table(stringify(name), num_label)
  table_matcher { run delete_from_mysql_sequence(stringify(name)) }
  run insert_last_insert_id_into_mysql_sequence(stringify(name))
  take_seq(stringify(name))
end

#set_column_default_nextval(table, column, sequence) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/sequel/sequence/database/server/mysql.rb', line 77

def set_column_default_nextval(table, column, sequence)
  run trigger_create_sequenced_column(stringify(table),
                                      stringify(column),
                                      stringify(sequence))
  run trigger_update_sequenced_column(stringify(table),
                                      stringify(column),
                                      stringify(sequence))
end

#setval(name, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sequel/sequence/database/server/mysql.rb', line 62

def setval(name, value)
  current = lastval(name)
  if current.nil?
    create_sequence(stringify(name), { start: value })
  elsif value < current
    log_info Sequel::Database::DANGER_OPT_ID
    value = current
  elsif value > current
    run insert_into_sequence_table_init_values(stringify(name), value, 0)
    table_matcher { run delete_from_mysql_sequence(stringify(name)) }
    run insert_into_mysql_sequence(stringify(name), value)
  end
  value
end