Module: Sequel::Sequence::Database::SQLite
- Defined in:
- lib/sequel/sequence/database/sqlite.rb
Instance Method Summary collapse
- #check_sequences ⇒ Object
- #create_sequence(name, options = {}) ⇒ Object
- #custom_sequence?(sequence_name) ⇒ Boolean
- #delete_to_currval(name) ⇒ Object
- #drop_sequence(name, options = {}) ⇒ Object
- #lastval(name) ⇒ Object (also: #currval)
- #nextval(name) ⇒ Object
- #nextval_with_label(name, num_label = 0) ⇒ Object
- #set_column_default_nextval(table, column, sequence) ⇒ Object
- #setval(name, value) ⇒ Object
Instance Method Details
#check_sequences ⇒ Object
14 15 16 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 14 def check_sequences fetch('SELECT * FROM `sqlite_sequence`;').all.to_a end |
#create_sequence(name, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 18 def create_sequence(name, = {}) () if_exists = build_exists_condition([:if_exists]) start_option = [:start] || 1 num_label = [:numeric_label] || 0 return if (current = lastval(name)) && (current >= start_option) sql = [create_sequence_table(stringify(name), if_exists)] sql << insert_into_sequence_table_init_values(stringify(name), start_option, num_label) run(sql.join("\n")) end |
#custom_sequence?(sequence_name) ⇒ Boolean
9 10 11 12 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 9 def custom_sequence?(sequence_name) sql = "SELECT * FROM `sqlite_sequence` WHERE name = '#{stringify(sequence_name)}';" fetch(sql).all.size.positive? end |
#delete_to_currval(name) ⇒ Object
70 71 72 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 70 def delete_to_currval(name) run delete_to_current_seq(stringify(name)) end |
#drop_sequence(name, options = {}) ⇒ Object
30 31 32 33 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 30 def drop_sequence(name, = {}) if_exists = build_exists_condition([:if_exists]) run(drop_sequence_table(stringify(name), if_exists)) end |
#lastval(name) ⇒ Object Also known as: currval
45 46 47 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 45 def lastval(name) take_seq(stringify(name)) end |
#nextval(name) ⇒ Object
35 36 37 38 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 35 def nextval(name) run(insert_into_sequence_table(stringify(name), 0)) take_seq(stringify(name)) end |
#nextval_with_label(name, num_label = 0) ⇒ Object
40 41 42 43 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 40 def nextval_with_label(name, num_label = 0) run(insert_into_sequence_table(stringify(name), num_label)) take_seq(stringify(name)) end |
#set_column_default_nextval(table, column, sequence) ⇒ Object
64 65 66 67 68 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 64 def set_column_default_nextval(table, column, sequence) run(trigger_create_sequenced_column(stringify(table), stringify(column), stringify(sequence))) end |
#setval(name, value) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sequel/sequence/database/sqlite.rb', line 51 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 else run(update_sqlite_sequence(stringify(name), value)) end value end |