Module: Sequel::Sequence::Database::Server::Mariadb
- Defined in:
- lib/sequel/sequence/database/server/mariadb.rb
Instance Method Summary collapse
- #check_sequences ⇒ Object
- #create_sequence(name, options = {}) ⇒ Object
- #custom_sequence?(sequence_name) ⇒ Boolean
- #drop_sequence(name, options = {}) ⇒ Object
- #lastval(name) ⇒ Object (also: #currval)
- #nextval(name) ⇒ Object
- #quote_column_name(name) ⇒ Object
- #quote_sequence_name(name) ⇒ Object
- #set_column_default_nextval(table, column, sequence) ⇒ Object
- #setval(name, value) ⇒ Object
Instance Method Details
#check_sequences ⇒ Object
24 25 26 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 24 def check_sequences fetch("SHOW FULL TABLES WHERE Table_type = 'SEQUENCE';").all.to_a end |
#create_sequence(name, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 28 def create_sequence(name, = {}) minvalue = [:minvalue] maxvalue = [:maxvalue] start = [:start] cache = [:cache] cycle = [:cycle] increment = [:increment] || [:step] if_exists = build_exists_condition([:if_exists]) name = quote_name(name.to_s) sql = ["CREATE SEQUENCE #{if_exists || Sequel::Database::IF_NOT_EXISTS} #{name}"] sql << "INCREMENT BY #{increment}" if increment sql << "MINVALUE #{minvalue}" if minvalue sql << "MAXVALUE #{maxvalue}" if maxvalue sql << "START WITH #{start}" if start sql << "CACHE #{cache}" if cache sql << cycle.to_s if cycle sql << "COMMENT '#{Sequel::Database::SEQUENCE_COMMENT}'" sql << ';' run(sql.join("\n")) end |
#custom_sequence?(sequence_name) ⇒ Boolean
16 17 18 19 20 21 22 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 16 def custom_sequence?(sequence_name) db = name_of_current_database return false if db.empty? sql = "SHOW FULL TABLES WHERE Table_type = 'SEQUENCE' and Tables_in_#{db} = '#{sequence_name}';" fetch(sql).all.size.positive? end |
#drop_sequence(name, options = {}) ⇒ Object
52 53 54 55 56 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 52 def drop_sequence(name, = {}) if_exists = build_exists_condition([:if_exists]) name = quote_name(name.to_s) run drop_sequence_table(name, if_exists) end |
#lastval(name) ⇒ Object Also known as: currval
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 67 def lastval(name) quoted_name = quote(name.to_s) out = nil fetch("SELECT lastval(#{quoted_name});") do |row| out = row["lastval(#{quoted_name})".to_sym] end return nextval(name) if out.nil? out end |
#nextval(name) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 58 def nextval(name) name = quote(name.to_s) out = nil fetch("SELECT nextval(#{name});") do |row| out = row["nextval(#{name})".to_sym] end out end |
#quote_column_name(name) ⇒ Object
8 9 10 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 8 def quote_column_name(name) "`#{name.gsub('`', '``')}`" end |
#quote_sequence_name(name) ⇒ Object
12 13 14 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 12 def quote_sequence_name(name) "`#{name.gsub(/[`"']/, '')}`" end |
#set_column_default_nextval(table, column, sequence) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 97 def set_column_default_nextval(table, column, sequence) sql = %( ALTER TABLE IF EXISTS #{quote(table.to_s)} ALTER COLUMN #{quote_name(column.to_s)} SET DEFAULT nextval(#{quote(sequence.to_s)}) ).strip run sql end |
#setval(name, value) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sequel/sequence/database/server/mariadb.rb', line 80 def setval(name, value) current = lastval(name) if value <= current log_info Sequel::Database::DANGER_OPT_ID if value < current value = current else quoted_name = quote(name.to_s) value -= 1 out = nil fetch("SELECT setval(#{quoted_name}, #{value});") do |row| out = row["setval(#{quoted_name}, #{value})".to_sym] end value = nextval(name) end value end |