Skip to Content Skip to Search

Anki21Database represents the collection.anki21 Anki SQLite database in the Anki Package

Methods
F
P
Included Modules

Instance Public methods

find_deck_by(name: nil, id: nil)

Returns the deck found by either name or id, or nil if it is not found.

# File lib/anki_record/anki21_database/anki21_database.rb, line 83
def find_deck_by(name: nil, id: nil)
  if (id && name) || (id.nil? && name.nil?)
    raise ArgumentError,
          "You must pass either an id or name keyword argument to find_deck_by."
  end

  name ? find_deck_by_name(name:) : find_deck_by_id(id:)
end

find_deck_options_group_by(id:)

Returns the deck options group object found by id, or nil if it is not found.

# File lib/anki_record/anki21_database/anki21_database.rb, line 94
def find_deck_options_group_by(id:)
  deck_options_groups.find { |deck_options_group| deck_options_group.id == id }
end

find_note_by(sfld: nil, id: nil)

rubocop:disable Metrics/MethodLength

Returns the note found by either +sfld“ or id, or nil if it is not found.

# File lib/anki_record/anki21_database/anki21_database.rb, line 36
def find_note_by(sfld: nil, id: nil)
  if (id && sfld) || (id.nil? && sfld.nil?)
    raise ArgumentError,
          "You must pass either an id or sfld keyword argument to find_note_by."
  end
  note_cards_data = if id
                      note_cards_data_for_note(id:)
                    else
                      note_cards_data_for_note(sfld:)
                    end
  return nil unless note_cards_data

  AnkiRecord::Note.new(anki21_database: self, data: note_cards_data)
end

find_note_type_by(name: nil, id: nil)

Returns the note type found by either name or id, or nil if it is not found.

# File lib/anki_record/anki21_database/anki21_database.rb, line 72
def find_note_type_by(name: nil, id: nil)
  if (id && name) || (id.nil? && name.nil?)
    raise ArgumentError,
          "You must pass either an id or name keyword argument to find_note_type_by."
  end

  name ? find_note_type_by_name(name:) : find_note_type_by_id(id:)
end

find_notes_by_exact_text_match(text:)

Returns an array of notes that have any field value matching text

# File lib/anki_record/anki21_database/anki21_database.rb, line 54
def find_notes_by_exact_text_match(text:)
  return [] if text == ""

  like_matcher = "%#{text}%"
  note_datas = prepare("select * from notes where flds LIKE ?").execute([like_matcher])

  note_datas.map do |note_data|
    id = note_data["id"]

    cards_data = prepare("select * from cards where nid = ?").execute([id]).to_a
    note_cards_data = { note_data:, cards_data: }

    AnkiRecord::Note.new(anki21_database: self, data: note_cards_data)
  end
end

prepare(sql)

Returns an SQLite3::Statement object (sqlite3 gem) to be executed against the collection.anki21 database.

Statement#execute executes the statement.

# File lib/anki_record/anki21_database/anki21_database.rb, line 29
def prepare(sql)
  database.prepare sql
end