diff --git a/defs.bzl b/defs.bzl index eff3d9df2..ab042a9e1 100644 --- a/defs.bzl +++ b/defs.bzl @@ -4,6 +4,7 @@ load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories") load("@net_ankiweb_anki//cargo:crates.bzl", "raze_fetch_remote_crates") load(":python.bzl", "setup_local_python") load(":protobuf.bzl", "setup_protobuf_binary") +load("//rslib:clang_format.bzl", "setup_clang_format") load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install") load("@io_bazel_rules_sass//:defs.bzl", "sass_repositories") load("@build_bazel_rules_svelte//:defs.bzl", "rules_svelte_dependencies") @@ -28,6 +29,8 @@ def setup_deps(): setup_protobuf_binary(name = "com_google_protobuf") + setup_clang_format(name = "clang_format") + native.register_toolchains("@python//:python3_toolchain") pip_import( diff --git a/docs/development.md b/docs/development.md index 6d2e70078..b30f1ec55 100644 --- a/docs/development.md +++ b/docs/development.md @@ -115,6 +115,8 @@ in the relevant package: ``` bazel run //rslib:format +bazel run //rslib:sql_format +bazel run //rslib:proto_format bazel run //pylib:format bazel run //qt:format bazel run //ts:format diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index be56be53f..10761a36f 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -136,7 +136,6 @@ class DataModel(QAbstractTableModel): if role == Qt.FontRole: if self.activeCols[index.column()] not in ("question", "answer", "noteFld"): return - row = index.row() c = self.getCard(index) t = c.template() if not t.get("bfont"): @@ -298,7 +297,6 @@ class DataModel(QAbstractTableModel): return "%Y-%m-%d" def columnData(self, index): - row = index.row() col = index.column() type = self.columnType(col) c = self.getCard(index) @@ -393,7 +391,6 @@ class DataModel(QAbstractTableModel): if type != "noteFld": return False - row = index.row() c = self.getCard(index) nt = c.note().model() return nt["flds"][self.col.models.sortIdx(nt)]["rtl"] @@ -2073,7 +2070,7 @@ where id in %s""" self.model.beginReset() self.mw.checkpoint(tr(TR.BROWSING_TAG_DUPLICATES)) nids = set() - for s, nidlist in res: + for _, nidlist in res: nids.update(nidlist) self.col.tags.bulkAdd(list(nids), tr(TR.BROWSING_DUPLICATE)) self.mw.progress.finish() diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 740cd59e5..1ca0f11f9 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -270,7 +270,6 @@ class AnkiQt(QMainWindow): # called on .clear() return name = self.pm.profiles()[n] - f = self.profileForm self.pm.load(name) def openProfile(self): diff --git a/qt/aqt/taglimit.py b/qt/aqt/taglimit.py index 0fed27f56..cd79dedce 100644 --- a/qt/aqt/taglimit.py +++ b/qt/aqt/taglimit.py @@ -71,7 +71,6 @@ class TagLimit(QDialog): def accept(self): self.hide() - n = 0 # gather yes/no tags yes = [] no = [] diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 373695d3d..f9975e2ca 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -201,16 +201,11 @@ class ButtonedDialog(QMessageBox): self.help = help self.setIcon(QMessageBox.Warning) self.setText(text) - # v = QVBoxLayout() - # v.addWidget(QLabel(text)) - # box = QDialogButtonBox() - # v.addWidget(box) for b in buttons: self._buttons.append(self.addButton(b, QMessageBox.AcceptRole)) if help: self.addButton(tr(TR.ACTIONS_HELP), QMessageBox.HelpRole) buttons.append(tr(TR.ACTIONS_HELP)) - # self.setLayout(v) def run(self): self.exec_() diff --git a/qt/tests/run_format.py b/qt/tests/run_format.py index 28b180209..0a7cba933 100644 --- a/qt/tests/run_format.py +++ b/qt/tests/run_format.py @@ -22,7 +22,7 @@ if __name__ == "__main__": "black", "-t", "py38", - "--exclude=aqt/forms|colors", + "--exclude=aqt/forms|colors|_gen", "aqt", "tests", "tools", diff --git a/rslib/BUILD.bazel b/rslib/BUILD.bazel index 51d89ba7a..91284f4c1 100644 --- a/rslib/BUILD.bazel +++ b/rslib/BUILD.bazel @@ -2,6 +2,8 @@ load("@rules_proto//proto:defs.bzl", "proto_library") load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary", "rust_library", "rust_test") load("@io_bazel_rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") load(":rustfmt.bzl", "rustfmt_fix", "rustfmt_test") +load(":clang_format.bzl", "proto_format") +load("//ts:sql_format.bzl", "sql_format") # Build script ####################### @@ -143,6 +145,16 @@ rustfmt_fix( ]), ) +sql_format( + name = "sql_format", + srcs = glob(["**/*.sql"]), +) + +proto_format( + name = "proto_format", + srcs = ["backend.proto"], +) + # fluent.proto generation ########################### # This separate step is required to make the file available to downstream consumers. diff --git a/rslib/backend.proto b/rslib/backend.proto index 820b0a832..10597806f 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -8,216 +8,217 @@ package BackendProto; message Empty {} message OptionalInt32 { - sint32 val = 1; + sint32 val = 1; } message OptionalUInt32 { - uint32 val = 1; + uint32 val = 1; } message Int32 { - sint32 val = 1; + sint32 val = 1; } message UInt32 { - uint32 val = 1; + uint32 val = 1; } message Int64 { - int64 val = 1; + int64 val = 1; } message String { - string val = 1; + string val = 1; } message Json { - bytes json = 1; + bytes json = 1; } message Bool { - bool val = 1; + bool val = 1; } // IDs used in RPC calls /////////////////////////////////////////////////////////// message NoteTypeID { - int64 ntid = 1; + int64 ntid = 1; } message NoteID { - int64 nid = 1; + int64 nid = 1; } message CardID { - int64 cid = 1; + int64 cid = 1; } message CardIDs { - repeated int64 cids = 1; + repeated int64 cids = 1; } message DeckID { - int64 did = 1; + int64 did = 1; } message DeckConfigID { - int64 dcid = 1; + int64 dcid = 1; } // New style RPC definitions /////////////////////////////////////////////////////////// service BackendService { - rpc LatestProgress (Empty) returns (Progress); - rpc SetWantsAbort (Empty) returns (Empty); + rpc LatestProgress(Empty) returns (Progress); + rpc SetWantsAbort(Empty) returns (Empty); - // card rendering + // card rendering - rpc ExtractAVTags (ExtractAVTagsIn) returns (ExtractAVTagsOut); - rpc ExtractLatex (ExtractLatexIn) returns (ExtractLatexOut); - rpc GetEmptyCards (Empty) returns (EmptyCardsReport); - rpc RenderExistingCard (RenderExistingCardIn) returns (RenderCardOut); - rpc RenderUncommittedCard (RenderUncommittedCardIn) returns (RenderCardOut); - rpc StripAVTags (String) returns (String); + rpc ExtractAVTags(ExtractAVTagsIn) returns (ExtractAVTagsOut); + rpc ExtractLatex(ExtractLatexIn) returns (ExtractLatexOut); + rpc GetEmptyCards(Empty) returns (EmptyCardsReport); + rpc RenderExistingCard(RenderExistingCardIn) returns (RenderCardOut); + rpc RenderUncommittedCard(RenderUncommittedCardIn) returns (RenderCardOut); + rpc StripAVTags(String) returns (String); - // searching + // searching - rpc FilterToSearch (FilterToSearchIn) returns (String); - rpc NormalizeSearch (String) returns (String); - rpc SearchCards (SearchCardsIn) returns (SearchCardsOut); - rpc SearchNotes (SearchNotesIn) returns (SearchNotesOut); - rpc NegateSearch (String) returns (String); - rpc ConcatenateSearches (ConcatenateSearchesIn) returns (String); - rpc ReplaceSearchTerm (ReplaceSearchTermIn) returns (String); - rpc FindAndReplace (FindAndReplaceIn) returns (UInt32); + rpc FilterToSearch (FilterToSearchIn) returns (String); + rpc NormalizeSearch(String) returns (String); + rpc SearchCards(SearchCardsIn) returns (SearchCardsOut); + rpc SearchNotes(SearchNotesIn) returns (SearchNotesOut); + rpc NegateSearch(String) returns (String); + rpc ConcatenateSearches(ConcatenateSearchesIn) returns (String); + rpc ReplaceSearchTerm(ReplaceSearchTermIn) returns (String); + rpc FindAndReplace(FindAndReplaceIn) returns (UInt32); - // scheduling + // scheduling - rpc LocalMinutesWest (Int64) returns (Int32); - rpc SetLocalMinutesWest (Int32) returns (Empty); - rpc SchedTimingToday (Empty) returns (SchedTimingTodayOut); - rpc StudiedToday (Empty) returns (String); - rpc StudiedTodayMessage (StudiedTodayMessageIn) returns (String); - rpc UpdateStats (UpdateStatsIn) returns (Empty); - rpc ExtendLimits (ExtendLimitsIn) returns (Empty); - rpc CountsForDeckToday (DeckID) returns (CountsForDeckTodayOut); - rpc CongratsInfo (Empty) returns (CongratsInfoOut); - rpc RestoreBuriedAndSuspendedCards (CardIDs) returns (Empty); - rpc UnburyCardsInCurrentDeck (UnburyCardsInCurrentDeckIn) returns (Empty); - rpc BuryOrSuspendCards (BuryOrSuspendCardsIn) returns (Empty); - rpc EmptyFilteredDeck (DeckID) returns (Empty); - rpc RebuildFilteredDeck (DeckID) returns (UInt32); - rpc ScheduleCardsAsReviews (ScheduleCardsAsReviewsIn) returns (Empty); - rpc ScheduleCardsAsNew (ScheduleCardsAsNewIn) returns (Empty); - rpc SortCards (SortCardsIn) returns (Empty); - rpc SortDeck (SortDeckIn) returns (Empty); + rpc LocalMinutesWest(Int64) returns (Int32); + rpc SetLocalMinutesWest(Int32) returns (Empty); + rpc SchedTimingToday(Empty) returns (SchedTimingTodayOut); + rpc StudiedToday(Empty) returns (String); + rpc StudiedTodayMessage(StudiedTodayMessageIn) returns (String); + rpc UpdateStats(UpdateStatsIn) returns (Empty); + rpc ExtendLimits(ExtendLimitsIn) returns (Empty); + rpc CountsForDeckToday(DeckID) returns (CountsForDeckTodayOut); + rpc CongratsInfo(Empty) returns (CongratsInfoOut); + rpc RestoreBuriedAndSuspendedCards(CardIDs) returns (Empty); + rpc UnburyCardsInCurrentDeck(UnburyCardsInCurrentDeckIn) returns (Empty); + rpc BuryOrSuspendCards(BuryOrSuspendCardsIn) returns (Empty); + rpc EmptyFilteredDeck(DeckID) returns (Empty); + rpc RebuildFilteredDeck(DeckID) returns (UInt32); + rpc ScheduleCardsAsReviews(ScheduleCardsAsReviewsIn) returns (Empty); + rpc ScheduleCardsAsNew(ScheduleCardsAsNewIn) returns (Empty); + rpc SortCards(SortCardsIn) returns (Empty); + rpc SortDeck(SortDeckIn) returns (Empty); - // stats + // stats - rpc CardStats (CardID) returns (String); - rpc Graphs(GraphsIn) returns (GraphsOut); + rpc CardStats(CardID) returns (String); + rpc Graphs(GraphsIn) returns (GraphsOut); - // media + // media - rpc CheckMedia (Empty) returns (CheckMediaOut); - rpc TrashMediaFiles (TrashMediaFilesIn) returns (Empty); - rpc AddMediaFile (AddMediaFileIn) returns (String); - rpc EmptyTrash (Empty) returns (Empty); - rpc RestoreTrash (Empty) returns (Empty); + rpc CheckMedia(Empty) returns (CheckMediaOut); + rpc TrashMediaFiles(TrashMediaFilesIn) returns (Empty); + rpc AddMediaFile(AddMediaFileIn) returns (String); + rpc EmptyTrash(Empty) returns (Empty); + rpc RestoreTrash(Empty) returns (Empty); - // decks + // decks - rpc AddOrUpdateDeckLegacy (AddOrUpdateDeckLegacyIn) returns (DeckID); - rpc DeckTree (DeckTreeIn) returns (DeckTreeNode); - rpc DeckTreeLegacy (Empty) returns (Json); - rpc GetAllDecksLegacy (Empty) returns (Json); - rpc GetDeckIDByName (String) returns (DeckID); - rpc GetDeckLegacy (DeckID) returns (Json); - rpc GetDeckNames (GetDeckNamesIn) returns (DeckNames); - rpc NewDeckLegacy (Bool) returns (Json); - rpc RemoveDeck (DeckID) returns (Empty); + rpc AddOrUpdateDeckLegacy(AddOrUpdateDeckLegacyIn) returns (DeckID); + rpc DeckTree(DeckTreeIn) returns (DeckTreeNode); + rpc DeckTreeLegacy(Empty) returns (Json); + rpc GetAllDecksLegacy(Empty) returns (Json); + rpc GetDeckIDByName(String) returns (DeckID); + rpc GetDeckLegacy(DeckID) returns (Json); + rpc GetDeckNames(GetDeckNamesIn) returns (DeckNames); + rpc NewDeckLegacy(Bool) returns (Json); + rpc RemoveDeck(DeckID) returns (Empty); - // deck config + // deck config - rpc AddOrUpdateDeckConfigLegacy (AddOrUpdateDeckConfigLegacyIn) returns (DeckConfigID); - rpc AllDeckConfigLegacy (Empty) returns (Json); - rpc GetDeckConfigLegacy (DeckConfigID) returns (Json); - rpc NewDeckConfigLegacy (Empty) returns (Json); - rpc RemoveDeckConfig (DeckConfigID) returns (Empty); + rpc AddOrUpdateDeckConfigLegacy(AddOrUpdateDeckConfigLegacyIn) + returns (DeckConfigID); + rpc AllDeckConfigLegacy(Empty) returns (Json); + rpc GetDeckConfigLegacy(DeckConfigID) returns (Json); + rpc NewDeckConfigLegacy(Empty) returns (Json); + rpc RemoveDeckConfig(DeckConfigID) returns (Empty); - // cards + // cards - rpc GetCard (CardID) returns (Card); - rpc UpdateCard (Card) returns (Empty); - rpc AddCard (Card) returns (CardID); - rpc RemoveCards (RemoveCardsIn) returns (Empty); - rpc SetDeck (SetDeckIn) returns (Empty); + rpc GetCard(CardID) returns (Card); + rpc UpdateCard(Card) returns (Empty); + rpc AddCard(Card) returns (CardID); + rpc RemoveCards(RemoveCardsIn) returns (Empty); + rpc SetDeck(SetDeckIn) returns (Empty); - // notes + // notes - rpc NewNote (NoteTypeID) returns (Note); - rpc AddNote (AddNoteIn) returns (NoteID); - rpc UpdateNote (Note) returns (Empty); - rpc GetNote (NoteID) returns (Note); - rpc RemoveNotes (RemoveNotesIn) returns (Empty); - rpc AddNoteTags (AddNoteTagsIn) returns (UInt32); - rpc UpdateNoteTags (UpdateNoteTagsIn) returns (UInt32); - rpc ClozeNumbersInNote (Note) returns (ClozeNumbersInNoteOut); - rpc AfterNoteUpdates (AfterNoteUpdatesIn) returns (Empty); - rpc FieldNamesForNotes (FieldNamesForNotesIn) returns (FieldNamesForNotesOut); - rpc NoteIsDuplicateOrEmpty (Note) returns (NoteIsDuplicateOrEmptyOut); - rpc CardsOfNote (NoteID) returns (CardIDs); + rpc NewNote(NoteTypeID) returns (Note); + rpc AddNote(AddNoteIn) returns (NoteID); + rpc UpdateNote(Note) returns (Empty); + rpc GetNote(NoteID) returns (Note); + rpc RemoveNotes(RemoveNotesIn) returns (Empty); + rpc AddNoteTags(AddNoteTagsIn) returns (UInt32); + rpc UpdateNoteTags(UpdateNoteTagsIn) returns (UInt32); + rpc ClozeNumbersInNote(Note) returns (ClozeNumbersInNoteOut); + rpc AfterNoteUpdates(AfterNoteUpdatesIn) returns (Empty); + rpc FieldNamesForNotes(FieldNamesForNotesIn) returns (FieldNamesForNotesOut); + rpc NoteIsDuplicateOrEmpty(Note) returns (NoteIsDuplicateOrEmptyOut); + rpc CardsOfNote(NoteID) returns (CardIDs); - // note types + // note types - rpc AddOrUpdateNotetype (AddOrUpdateNotetypeIn) returns (NoteTypeID); - rpc GetStockNotetypeLegacy (GetStockNotetypeIn) returns (Json); - rpc GetNotetypeLegacy (NoteTypeID) returns (Json); - rpc GetNotetypeNames (Empty) returns (NoteTypeNames); - rpc GetNotetypeNamesAndCounts (Empty) returns (NoteTypeUseCounts); - rpc GetNotetypeIDByName (String) returns (NoteTypeID); - rpc RemoveNotetype (NoteTypeID) returns (Empty); + rpc AddOrUpdateNotetype(AddOrUpdateNotetypeIn) returns (NoteTypeID); + rpc GetStockNotetypeLegacy(GetStockNotetypeIn) returns (Json); + rpc GetNotetypeLegacy(NoteTypeID) returns (Json); + rpc GetNotetypeNames(Empty) returns (NoteTypeNames); + rpc GetNotetypeNamesAndCounts(Empty) returns (NoteTypeUseCounts); + rpc GetNotetypeIDByName(String) returns (NoteTypeID); + rpc RemoveNotetype(NoteTypeID) returns (Empty); - // collection + // collection - rpc OpenCollection (OpenCollectionIn) returns (Empty); - rpc CloseCollection (CloseCollectionIn) returns (Empty); - rpc CheckDatabase (Empty) returns (CheckDatabaseOut); + rpc OpenCollection(OpenCollectionIn) returns (Empty); + rpc CloseCollection(CloseCollectionIn) returns (Empty); + rpc CheckDatabase(Empty) returns (CheckDatabaseOut); - // sync + // sync - rpc SyncMedia (SyncAuth) returns (Empty); - rpc AbortSync (Empty) returns (Empty); - rpc AbortMediaSync (Empty) returns (Empty); - rpc BeforeUpload (Empty) returns (Empty); - rpc SyncLogin (SyncLoginIn) returns (SyncAuth); - rpc SyncStatus (SyncAuth) returns (SyncStatusOut); - rpc SyncCollection (SyncAuth) returns (SyncCollectionOut); - rpc FullUpload (SyncAuth) returns (Empty); - rpc FullDownload (SyncAuth) returns (Empty); + rpc SyncMedia(SyncAuth) returns (Empty); + rpc AbortSync(Empty) returns (Empty); + rpc AbortMediaSync(Empty) returns (Empty); + rpc BeforeUpload(Empty) returns (Empty); + rpc SyncLogin(SyncLoginIn) returns (SyncAuth); + rpc SyncStatus(SyncAuth) returns (SyncStatusOut); + rpc SyncCollection(SyncAuth) returns (SyncCollectionOut); + rpc FullUpload(SyncAuth) returns (Empty); + rpc FullDownload(SyncAuth) returns (Empty); - // translation/messages + // translation/messages - rpc TranslateString (TranslateStringIn) returns (String); - rpc FormatTimespan (FormatTimespanIn) returns (String); - rpc I18nResources (Empty) returns (Json); + rpc TranslateString(TranslateStringIn) returns (String); + rpc FormatTimespan(FormatTimespanIn) returns (String); + rpc I18nResources(Empty) returns (Json); - // tags + // tags - rpc RegisterTags (RegisterTagsIn) returns (Bool); - rpc AllTags (Empty) returns (AllTagsOut); + rpc RegisterTags(RegisterTagsIn) returns (Bool); + rpc AllTags(Empty) returns (AllTagsOut); - // config/preferences + // config/preferences - rpc GetConfigJson (String) returns (Json); - rpc SetConfigJson (SetConfigJsonIn) returns (Empty); - rpc RemoveConfig (String) returns (Empty); - rpc SetAllConfig (Json) returns (Empty); - rpc GetAllConfig (Empty) returns (Json); - rpc GetPreferences (Empty) returns (Preferences); - rpc SetPreferences (Preferences) returns (Empty); + rpc GetConfigJson(String) returns (Json); + rpc SetConfigJson(SetConfigJsonIn) returns (Empty); + rpc RemoveConfig(String) returns (Empty); + rpc SetAllConfig(Json) returns (Empty); + rpc GetAllConfig(Empty) returns (Json); + rpc GetPreferences(Empty) returns (Preferences); + rpc SetPreferences(Preferences) returns (Empty); } // Protobuf stored in .anki2 files @@ -225,916 +226,912 @@ service BackendService { /////////////////////////////////////////////////////////// message DeckConfigInner { - enum NewCardOrder { - NEW_CARD_ORDER_DUE = 0; - NEW_CARD_ORDER_RANDOM = 1; - } + enum NewCardOrder { + NEW_CARD_ORDER_DUE = 0; + NEW_CARD_ORDER_RANDOM = 1; + } - enum LeechAction { - LEECH_ACTION_SUSPEND = 0; - LEECH_ACTION_TAG_ONLY = 1; - } + enum LeechAction { + LEECH_ACTION_SUSPEND = 0; + LEECH_ACTION_TAG_ONLY = 1; + } - repeated float learn_steps = 1; - repeated float relearn_steps = 2; + repeated float learn_steps = 1; + repeated float relearn_steps = 2; - reserved 3 to 8; + reserved 3 to 8; - uint32 new_per_day = 9; - uint32 reviews_per_day = 10; + uint32 new_per_day = 9; + uint32 reviews_per_day = 10; - float initial_ease = 11; - float easy_multiplier = 12; - float hard_multiplier = 13; - float lapse_multiplier = 14; - float interval_multiplier = 15; + float initial_ease = 11; + float easy_multiplier = 12; + float hard_multiplier = 13; + float lapse_multiplier = 14; + float interval_multiplier = 15; - uint32 maximum_review_interval = 16; - uint32 minimum_review_interval = 17; + uint32 maximum_review_interval = 16; + uint32 minimum_review_interval = 17; - uint32 graduating_interval_good = 18; - uint32 graduating_interval_easy = 19; + uint32 graduating_interval_good = 18; + uint32 graduating_interval_easy = 19; - NewCardOrder new_card_order = 20; + NewCardOrder new_card_order = 20; - LeechAction leech_action = 21; - uint32 leech_threshold = 22; + LeechAction leech_action = 21; + uint32 leech_threshold = 22; - bool disable_autoplay = 23; - uint32 cap_answer_time_to_secs = 24; - uint32 visible_timer_secs = 25; - bool skip_question_when_replaying_answer = 26; + bool disable_autoplay = 23; + uint32 cap_answer_time_to_secs = 24; + uint32 visible_timer_secs = 25; + bool skip_question_when_replaying_answer = 26; - bool bury_new = 27; - bool bury_reviews = 28; + bool bury_new = 27; + bool bury_reviews = 28; - bytes other = 255; + bytes other = 255; } message DeckCommon { - bool study_collapsed = 1; - bool browser_collapsed = 2; + bool study_collapsed = 1; + bool browser_collapsed = 2; - uint32 last_day_studied = 3; - int32 new_studied = 4; - int32 review_studied = 5; - int32 milliseconds_studied = 7; + uint32 last_day_studied = 3; + int32 new_studied = 4; + int32 review_studied = 5; + int32 milliseconds_studied = 7; - // previously set in the v1 scheduler, - // but not currently used for anything - int32 learning_studied = 6; + // previously set in the v1 scheduler, + // but not currently used for anything + int32 learning_studied = 6; - bytes other = 255; + bytes other = 255; } message DeckKind { - oneof kind { - NormalDeck normal = 1; - FilteredDeck filtered = 2; - } + oneof kind { + NormalDeck normal = 1; + FilteredDeck filtered = 2; + } } message NormalDeck { - int64 config_id = 1; - uint32 extend_new = 2; - uint32 extend_review = 3; - string description = 4; + int64 config_id = 1; + uint32 extend_new = 2; + uint32 extend_review = 3; + string description = 4; } message FilteredDeck { - bool reschedule = 1; - repeated FilteredSearchTerm search_terms = 2; - // v1 scheduler only - repeated float delays = 3; - // v2 scheduler only - uint32 preview_delay = 4; + bool reschedule = 1; + repeated FilteredSearchTerm search_terms = 2; + // v1 scheduler only + repeated float delays = 3; + // v2 scheduler only + uint32 preview_delay = 4; } message FilteredSearchTerm { - enum FilteredSearchOrder { - FILTERED_SEARCH_ORDER_OLDEST_FIRST = 0; - FILTERED_SEARCH_ORDER_RANDOM = 1; - FILTERED_SEARCH_ORDER_INTERVALS_ASCENDING = 2; - FILTERED_SEARCH_ORDER_INTERVALS_DESCENDING = 3; - FILTERED_SEARCH_ORDER_LAPSES = 4; - FILTERED_SEARCH_ORDER_ADDED = 5; - FILTERED_SEARCH_ORDER_DUE = 6; - FILTERED_SEARCH_ORDER_REVERSE_ADDED = 7; - FILTERED_SEARCH_ORDER_DUE_PRIORITY = 8; - } + enum FilteredSearchOrder { + FILTERED_SEARCH_ORDER_OLDEST_FIRST = 0; + FILTERED_SEARCH_ORDER_RANDOM = 1; + FILTERED_SEARCH_ORDER_INTERVALS_ASCENDING = 2; + FILTERED_SEARCH_ORDER_INTERVALS_DESCENDING = 3; + FILTERED_SEARCH_ORDER_LAPSES = 4; + FILTERED_SEARCH_ORDER_ADDED = 5; + FILTERED_SEARCH_ORDER_DUE = 6; + FILTERED_SEARCH_ORDER_REVERSE_ADDED = 7; + FILTERED_SEARCH_ORDER_DUE_PRIORITY = 8; + } - string search = 1; - uint32 limit = 2; - FilteredSearchOrder order = 3; + string search = 1; + uint32 limit = 2; + FilteredSearchOrder order = 3; } message NoteFieldConfig { - bool sticky = 1; - bool rtl = 2; - string font_name = 3; - uint32 font_size = 4; + bool sticky = 1; + bool rtl = 2; + string font_name = 3; + uint32 font_size = 4; - bytes other = 255; + bytes other = 255; } message CardTemplateConfig { - string q_format = 1; - string a_format = 2; - string q_format_browser = 3; - string a_format_browser= 4; - int64 target_deck_id = 5; - string browser_font_name = 6; - uint32 browser_font_size = 7; + string q_format = 1; + string a_format = 2; + string q_format_browser = 3; + string a_format_browser = 4; + int64 target_deck_id = 5; + string browser_font_name = 6; + uint32 browser_font_size = 7; - bytes other = 255; + bytes other = 255; } message NoteTypeConfig { - enum Kind { - KIND_NORMAL = 0; - KIND_CLOZE = 1; - } - Kind kind = 1; - uint32 sort_field_idx = 2; - string css = 3; - int64 target_deck_id = 4; - string latex_pre = 5; - string latex_post = 6; - bool latex_svg = 7; - repeated CardRequirement reqs = 8; + enum Kind { + KIND_NORMAL = 0; + KIND_CLOZE = 1; + } + Kind kind = 1; + uint32 sort_field_idx = 2; + string css = 3; + int64 target_deck_id = 4; + string latex_pre = 5; + string latex_post = 6; + bool latex_svg = 7; + repeated CardRequirement reqs = 8; - bytes other = 255; + bytes other = 255; } message CardRequirement { - enum Kind { - KIND_NONE = 0; - KIND_ANY = 1; - KIND_ALL = 2; - } - uint32 card_ord = 1; - Kind kind = 2; - repeated uint32 field_ords = 3; + enum Kind { + KIND_NONE = 0; + KIND_ANY = 1; + KIND_ALL = 2; + } + uint32 card_ord = 1; + Kind kind = 2; + repeated uint32 field_ords = 3; } // Containers for passing around database objects /////////////////////////////////////////////////////////// message Deck { - int64 id = 1; - string name = 2; - uint32 mtime_secs = 3; - int32 usn = 4; - DeckCommon common = 5; - oneof kind { - NormalDeck normal = 6; - FilteredDeck filtered = 7; - } + int64 id = 1; + string name = 2; + uint32 mtime_secs = 3; + int32 usn = 4; + DeckCommon common = 5; + oneof kind { + NormalDeck normal = 6; + FilteredDeck filtered = 7; + } } message NoteType { - int64 id = 1; - string name = 2; - uint32 mtime_secs = 3; - sint32 usn = 4; - NoteTypeConfig config = 7; - repeated NoteField fields = 8; - repeated CardTemplate templates = 9; + int64 id = 1; + string name = 2; + uint32 mtime_secs = 3; + sint32 usn = 4; + NoteTypeConfig config = 7; + repeated NoteField fields = 8; + repeated CardTemplate templates = 9; } message NoteField { - OptionalUInt32 ord = 1; - string name = 2; - NoteFieldConfig config = 5; + OptionalUInt32 ord = 1; + string name = 2; + NoteFieldConfig config = 5; } message CardTemplate { - OptionalUInt32 ord = 1; - string name = 2; - uint32 mtime_secs = 3; - sint32 usn = 4; - CardTemplateConfig config = 5; + OptionalUInt32 ord = 1; + string name = 2; + uint32 mtime_secs = 3; + sint32 usn = 4; + CardTemplateConfig config = 5; } message Note { - int64 id = 1; - string guid = 2; - int64 notetype_id = 3; - uint32 mtime_secs = 4; - int32 usn = 5; - repeated string tags = 6; - repeated string fields = 7; + int64 id = 1; + string guid = 2; + int64 notetype_id = 3; + uint32 mtime_secs = 4; + int32 usn = 5; + repeated string tags = 6; + repeated string fields = 7; } message Card { - int64 id = 1; - int64 note_id = 2; - int64 deck_id = 3; - uint32 template_idx = 4; - int64 mtime_secs = 5; - sint32 usn = 6; - uint32 ctype = 7; - sint32 queue = 8; - sint32 due = 9; - uint32 interval = 10; - uint32 ease_factor = 11; - uint32 reps = 12; - uint32 lapses = 13; - uint32 remaining_steps = 14; - sint32 original_due = 15; - int64 original_deck_id = 16; - uint32 flags = 17; - string data = 18; + int64 id = 1; + int64 note_id = 2; + int64 deck_id = 3; + uint32 template_idx = 4; + int64 mtime_secs = 5; + sint32 usn = 6; + uint32 ctype = 7; + sint32 queue = 8; + sint32 due = 9; + uint32 interval = 10; + uint32 ease_factor = 11; + uint32 reps = 12; + uint32 lapses = 13; + uint32 remaining_steps = 14; + sint32 original_due = 15; + int64 original_deck_id = 16; + uint32 flags = 17; + string data = 18; } // Backend /////////////////////////////////////////////////////////// message BackendInit { - repeated string preferred_langs = 1; - string locale_folder_path = 2; - bool server = 3; + repeated string preferred_langs = 1; + string locale_folder_path = 2; + bool server = 3; } message I18nBackendInit { - repeated string preferred_langs = 4; - string locale_folder_path = 5; + repeated string preferred_langs = 4; + string locale_folder_path = 5; } // Errors /////////////////////////////////////////////////////////// message BackendError { - // localized error description suitable for displaying to the user - string localized = 1; - // error specifics - oneof value { - Empty invalid_input = 2; - Empty template_parse = 3; - Empty io_error = 4; - Empty db_error = 5; - NetworkError network_error = 6; - SyncError sync_error = 7; - // user interrupted operation - Empty interrupted = 8; - string json_error = 9; - string proto_error = 10; - Empty not_found_error = 11; - Empty exists = 12; - Empty deck_is_filtered = 13; - } + // localized error description suitable for displaying to the user + string localized = 1; + // error specifics + oneof value { + Empty invalid_input = 2; + Empty template_parse = 3; + Empty io_error = 4; + Empty db_error = 5; + NetworkError network_error = 6; + SyncError sync_error = 7; + // user interrupted operation + Empty interrupted = 8; + string json_error = 9; + string proto_error = 10; + Empty not_found_error = 11; + Empty exists = 12; + Empty deck_is_filtered = 13; + } } message NetworkError { - enum NetworkErrorKind { - OTHER = 0; - OFFLINE = 1; - TIMEOUT = 2; - PROXY_AUTH = 3; - } - NetworkErrorKind kind = 1; + enum NetworkErrorKind { + OTHER = 0; + OFFLINE = 1; + TIMEOUT = 2; + PROXY_AUTH = 3; + } + NetworkErrorKind kind = 1; } message SyncError { - enum SyncErrorKind { - OTHER = 0; - CONFLICT = 1; - SERVER_ERROR = 2; - CLIENT_TOO_OLD = 3; - AUTH_FAILED = 4; - SERVER_MESSAGE = 5; - MEDIA_CHECK_REQUIRED = 6; - RESYNC_REQUIRED = 7; - CLOCK_INCORRECT = 8; - DATABASE_CHECK_REQUIRED = 9; - } - SyncErrorKind kind = 1; + enum SyncErrorKind { + OTHER = 0; + CONFLICT = 1; + SERVER_ERROR = 2; + CLIENT_TOO_OLD = 3; + AUTH_FAILED = 4; + SERVER_MESSAGE = 5; + MEDIA_CHECK_REQUIRED = 6; + RESYNC_REQUIRED = 7; + CLOCK_INCORRECT = 8; + DATABASE_CHECK_REQUIRED = 9; + } + SyncErrorKind kind = 1; } // Progress /////////////////////////////////////////////////////////// message Progress { - oneof value { - Empty none = 1; - MediaSyncProgress media_sync = 2; - string media_check = 3; - FullSyncProgress full_sync = 4; - NormalSyncProgress normal_sync = 5; - DatabaseCheckProgress database_check = 6; - } + oneof value { + Empty none = 1; + MediaSyncProgress media_sync = 2; + string media_check = 3; + FullSyncProgress full_sync = 4; + NormalSyncProgress normal_sync = 5; + DatabaseCheckProgress database_check = 6; + } } message MediaSyncProgress { - string checked = 1; - string added = 2; - string removed = 3; + string checked = 1; + string added = 2; + string removed = 3; } message FullSyncProgress { - uint32 transferred = 1; - uint32 total = 2; + uint32 transferred = 1; + uint32 total = 2; } message MediaSyncUploadProgress { - uint32 files = 1; - uint32 deletions = 2; + uint32 files = 1; + uint32 deletions = 2; } message NormalSyncProgress { - string stage = 1; - string added = 2; - string removed = 3; + string stage = 1; + string added = 2; + string removed = 3; } message DatabaseCheckProgress { - string stage = 1; - uint32 stage_total = 2; - uint32 stage_current = 3; + string stage = 1; + uint32 stage_total = 2; + uint32 stage_current = 3; } - // Messages /////////////////////////////////////////////////////////// - message SchedTimingTodayOut { - uint32 days_elapsed = 1; - int64 next_day_at = 2; + uint32 days_elapsed = 1; + int64 next_day_at = 2; } message DeckTreeIn { - // if non-zero, counts for the provided timestamp will be included - int64 now = 1; - int64 top_deck_id = 2; + // if non-zero, counts for the provided timestamp will be included + int64 now = 1; + int64 top_deck_id = 2; } message DeckTreeNode { - int64 deck_id = 1; - string name = 2; - repeated DeckTreeNode children = 3; - uint32 level = 4; - bool collapsed = 5; + int64 deck_id = 1; + string name = 2; + repeated DeckTreeNode children = 3; + uint32 level = 4; + bool collapsed = 5; - uint32 review_count = 6; - uint32 learn_count = 7; - uint32 new_count = 8; + uint32 review_count = 6; + uint32 learn_count = 7; + uint32 new_count = 8; - bool filtered = 16; + bool filtered = 16; } message RenderExistingCardIn { - int64 card_id = 1; - bool browser = 2; + int64 card_id = 1; + bool browser = 2; } message RenderUncommittedCardIn { - Note note = 1; - uint32 card_ord = 2; - bytes template = 3; - bool fill_empty = 4; + Note note = 1; + uint32 card_ord = 2; + bytes template = 3; + bool fill_empty = 4; } message RenderCardOut { - repeated RenderedTemplateNode question_nodes = 1; - repeated RenderedTemplateNode answer_nodes = 2; + repeated RenderedTemplateNode question_nodes = 1; + repeated RenderedTemplateNode answer_nodes = 2; } message RenderedTemplateNode { - oneof value { - string text = 1; - RenderedTemplateReplacement replacement = 2; - } + oneof value { + string text = 1; + RenderedTemplateReplacement replacement = 2; + } } message RenderedTemplateReplacement { - string field_name = 1; - string current_text = 2; - repeated string filters = 3; + string field_name = 1; + string current_text = 2; + repeated string filters = 3; } message ExtractAVTagsIn { - string text = 1; - bool question_side = 2; + string text = 1; + bool question_side = 2; } message ExtractAVTagsOut { - string text = 1; - repeated AVTag av_tags = 2; + string text = 1; + repeated AVTag av_tags = 2; } message AVTag { - oneof value { - string sound_or_video = 1; - TTSTag tts = 2; - } + oneof value { + string sound_or_video = 1; + TTSTag tts = 2; + } } message TTSTag { - string field_text = 1; - string lang = 2; - repeated string voices = 3; - float speed = 4; - repeated string other_args = 5; + string field_text = 1; + string lang = 2; + repeated string voices = 3; + float speed = 4; + repeated string other_args = 5; } message ExtractLatexIn { - string text = 1; - bool svg = 2; - bool expand_clozes = 3; + string text = 1; + bool svg = 2; + bool expand_clozes = 3; } message ExtractLatexOut { - string text = 1; - repeated ExtractedLatex latex = 2; + string text = 1; + repeated ExtractedLatex latex = 2; } message ExtractedLatex { - string filename = 1; - string latex_body = 2; + string filename = 1; + string latex_body = 2; } message AddMediaFileIn { - string desired_name = 1; - bytes data = 2; + string desired_name = 1; + bytes data = 2; } message CheckMediaOut { - repeated string unused = 1; - repeated string missing = 2; - string report = 3; - bool have_trash = 4; + repeated string unused = 1; + repeated string missing = 2; + string report = 3; + bool have_trash = 4; } message TrashMediaFilesIn { - repeated string fnames = 1; + repeated string fnames = 1; } message TranslateStringIn { - int32 key = 2; - map args = 3; + int32 key = 2; + map args = 3; } message TranslateArgValue { - oneof value { - string str = 1; - double number = 2; - } + oneof value { + string str = 1; + double number = 2; + } } message FormatTimespanIn { - enum Context { - PRECISE = 0; - ANSWER_BUTTONS = 1; - INTERVALS = 2; - } + enum Context { + PRECISE = 0; + ANSWER_BUTTONS = 1; + INTERVALS = 2; + } - float seconds = 1; - Context context = 2; + float seconds = 1; + Context context = 2; } message StudiedTodayMessageIn { - uint32 cards = 1; - double seconds = 2; + uint32 cards = 1; + double seconds = 2; } message CongratsLearnMessageIn { - float next_due = 1; - uint32 remaining = 2; + float next_due = 1; + uint32 remaining = 2; } message OpenCollectionIn { - string collection_path = 1; - string media_folder_path = 2; - string media_db_path = 3; - string log_path = 4; + string collection_path = 1; + string media_folder_path = 2; + string media_db_path = 3; + string log_path = 4; } message SearchCardsIn { - string search = 1; - SortOrder order = 2; + string search = 1; + SortOrder order = 2; } message SearchCardsOut { - repeated int64 card_ids = 1; - + repeated int64 card_ids = 1; } message SortOrder { - oneof value { - Empty from_config = 1; - Empty none = 2; - string custom = 3; - BuiltinSearchOrder builtin = 4; - } + oneof value { + Empty from_config = 1; + Empty none = 2; + string custom = 3; + BuiltinSearchOrder builtin = 4; + } } message SearchNotesIn { - string search = 1; + string search = 1; } message SearchNotesOut { - repeated int64 note_ids = 2; + repeated int64 note_ids = 2; } message BuiltinSearchOrder { - enum BuiltinSortKind { - NOTE_CREATION = 0; - NOTE_MOD = 1; - NOTE_FIELD = 2; - NOTE_TAGS = 3; - NOTE_TYPE = 4; - CARD_MOD = 5; - CARD_REPS = 6; - CARD_DUE = 7; - CARD_EASE = 8; - CARD_LAPSES = 9; - CARD_INTERVAL = 10; - CARD_DECK = 11; - CARD_TEMPLATE = 12; - } - BuiltinSortKind kind = 1; - bool reverse = 2; + enum BuiltinSortKind { + NOTE_CREATION = 0; + NOTE_MOD = 1; + NOTE_FIELD = 2; + NOTE_TAGS = 3; + NOTE_TYPE = 4; + CARD_MOD = 5; + CARD_REPS = 6; + CARD_DUE = 7; + CARD_EASE = 8; + CARD_LAPSES = 9; + CARD_INTERVAL = 10; + CARD_DECK = 11; + CARD_TEMPLATE = 12; + } + BuiltinSortKind kind = 1; + bool reverse = 2; } message FilterToSearchIn { - enum NamedFilter { - WHOLE_COLLECTION = 0; - CURRENT_DECK = 1; - ADDED_TODAY = 2; - STUDIED_TODAY = 3; - AGAIN_TODAY = 4; - NEW = 5; - LEARN = 6; - REVIEW = 7; - DUE = 8; - SUSPENDED = 9; - BURIED = 10; - RED_FLAG = 11; - ORANGE_FLAG = 12; - GREEN_FLAG = 13; - BLUE_FLAG = 14; - NO_FLAG = 15; - ANY_FLAG = 16; - } - oneof filter { - NamedFilter name = 1; - string tag = 2; - string deck = 3; - string note = 4; - uint32 template = 5; - } + enum NamedFilter { + WHOLE_COLLECTION = 0; + CURRENT_DECK = 1; + ADDED_TODAY = 2; + STUDIED_TODAY = 3; + AGAIN_TODAY = 4; + NEW = 5; + LEARN = 6; + REVIEW = 7; + DUE = 8; + SUSPENDED = 9; + BURIED = 10; + RED_FLAG = 11; + ORANGE_FLAG = 12; + GREEN_FLAG = 13; + BLUE_FLAG = 14; + NO_FLAG = 15; + ANY_FLAG = 16; + } + oneof filter { + NamedFilter name = 1; + string tag = 2; + string deck = 3; + string note = 4; + uint32 template = 5; + } } message ConcatenateSearchesIn { - enum Separator { - AND = 0; - OR = 1; - } - Separator sep = 1; - repeated string searches = 2; + enum Separator { + AND = 0; + OR = 1; + } + Separator sep = 1; + repeated string searches = 2; } message ReplaceSearchTermIn { - string search = 1; - string replacement = 2; + string search = 1; + string replacement = 2; } message CloseCollectionIn { - bool downgrade_to_schema11 = 1; + bool downgrade_to_schema11 = 1; } message AddOrUpdateDeckConfigLegacyIn { - bytes config = 1; - bool preserve_usn_and_mtime = 2; + bytes config = 1; + bool preserve_usn_and_mtime = 2; } message RegisterTagsIn { - string tags = 1; - bool preserve_usn = 2; - int32 usn = 3; - bool clear_first = 4; + string tags = 1; + bool preserve_usn = 2; + int32 usn = 3; + bool clear_first = 4; } message AllTagsOut { - repeated TagUsnTuple tags = 1; + repeated TagUsnTuple tags = 1; } message TagUsnTuple { - string tag = 1; - sint32 usn = 2; + string tag = 1; + sint32 usn = 2; } message GetChangedTagsOut { - repeated string tags = 1; + repeated string tags = 1; } message SetConfigJsonIn { - string key = 1; - bytes value_json = 2; + string key = 1; + bytes value_json = 2; } enum StockNoteType { - STOCK_NOTE_TYPE_BASIC = 0; - STOCK_NOTE_TYPE_BASIC_AND_REVERSED = 1; - STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED = 2; - STOCK_NOTE_TYPE_BASIC_TYPING = 3; - STOCK_NOTE_TYPE_CLOZE = 4; + STOCK_NOTE_TYPE_BASIC = 0; + STOCK_NOTE_TYPE_BASIC_AND_REVERSED = 1; + STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED = 2; + STOCK_NOTE_TYPE_BASIC_TYPING = 3; + STOCK_NOTE_TYPE_CLOZE = 4; } message GetStockNotetypeIn { - StockNoteType kind = 1; + StockNoteType kind = 1; } message NoteTypeNames { - repeated NoteTypeNameID entries = 1; + repeated NoteTypeNameID entries = 1; } message NoteTypeUseCounts { - repeated NoteTypeNameIDUseCount entries = 1; + repeated NoteTypeNameIDUseCount entries = 1; } message NoteTypeNameID { - int64 id = 1; - string name = 2; - + int64 id = 1; + string name = 2; } message NoteTypeNameIDUseCount { - int64 id = 1; - string name = 2; - uint32 use_count = 3; + int64 id = 1; + string name = 2; + uint32 use_count = 3; } message AddOrUpdateNotetypeIn { - bytes json = 1; - bool preserve_usn_and_mtime = 2; + bytes json = 1; + bool preserve_usn_and_mtime = 2; } message AddNoteIn { - Note note = 1; - int64 deck_id = 2; + Note note = 1; + int64 deck_id = 2; } message EmptyCardsReport { - string report = 1; - repeated NoteWithEmptyCards notes = 2; + string report = 1; + repeated NoteWithEmptyCards notes = 2; } message NoteWithEmptyCards { - int64 note_id = 1; - repeated int64 card_ids = 2; - bool will_delete_note = 3; + int64 note_id = 1; + repeated int64 card_ids = 2; + bool will_delete_note = 3; } message DeckNames { - repeated DeckNameID entries = 1; + repeated DeckNameID entries = 1; } message DeckNameID { - int64 id = 1; - string name = 2; + int64 id = 1; + string name = 2; } message AddOrUpdateDeckLegacyIn { - bytes deck = 1; - bool preserve_usn_and_mtime = 2; + bytes deck = 1; + bool preserve_usn_and_mtime = 2; } message FieldNamesForNotesIn { - repeated int64 nids = 1; + repeated int64 nids = 1; } message FieldNamesForNotesOut { - repeated string fields = 1; + repeated string fields = 1; } message FindAndReplaceIn { - repeated int64 nids = 1; - string search = 2; - string replacement = 3; - bool regex = 4; - bool match_case = 5; - string field_name = 6; + repeated int64 nids = 1; + string search = 2; + string replacement = 3; + bool regex = 4; + bool match_case = 5; + string field_name = 6; } message AfterNoteUpdatesIn { - repeated int64 nids = 1; - bool mark_notes_modified = 2; - bool generate_cards = 3; + repeated int64 nids = 1; + bool mark_notes_modified = 2; + bool generate_cards = 3; } message AddNoteTagsIn { - repeated int64 nids = 1; - string tags = 2; + repeated int64 nids = 1; + string tags = 2; } message UpdateNoteTagsIn { - repeated int64 nids = 1; - string tags = 2; - string replacement = 3; - bool regex = 4; + repeated int64 nids = 1; + string tags = 2; + string replacement = 3; + bool regex = 4; } message CheckDatabaseOut { - repeated string problems = 1; + repeated string problems = 1; } message CollectionSchedulingSettings { - enum NewReviewMix { - DISTRIBUTE = 0; - REVIEWS_FIRST = 1; - NEW_FIRST = 2; - } + enum NewReviewMix { + DISTRIBUTE = 0; + REVIEWS_FIRST = 1; + NEW_FIRST = 2; + } - uint32 scheduler_version = 1; - uint32 rollover = 2; - uint32 learn_ahead_secs = 3; - NewReviewMix new_review_mix = 4; - bool show_remaining_due_counts = 5; - bool show_intervals_on_buttons = 6; - uint32 time_limit_secs = 7; + uint32 scheduler_version = 1; + uint32 rollover = 2; + uint32 learn_ahead_secs = 3; + NewReviewMix new_review_mix = 4; + bool show_remaining_due_counts = 5; + bool show_intervals_on_buttons = 6; + uint32 time_limit_secs = 7; - // v2 only - bool new_timezone = 8; - bool day_learn_first = 9; + // v2 only + bool new_timezone = 8; + bool day_learn_first = 9; } message Preferences { - CollectionSchedulingSettings sched = 1; + CollectionSchedulingSettings sched = 1; } message ClozeNumbersInNoteOut { - repeated uint32 numbers = 1; + repeated uint32 numbers = 1; } message GetDeckNamesIn { - bool skip_empty_default = 1; - // if unset, implies skip_empty_default - bool include_filtered = 2; + bool skip_empty_default = 1; + // if unset, implies skip_empty_default + bool include_filtered = 2; } message NoteIsDuplicateOrEmptyOut { - enum State { - NORMAL = 0; - EMPTY = 1; - DUPLICATE = 2; - } - State state = 1; + enum State { + NORMAL = 0; + EMPTY = 1; + DUPLICATE = 2; + } + State state = 1; } message SyncLoginIn { - string username = 1; - string password = 2; + string username = 1; + string password = 2; } message SyncStatusOut { - enum Required { - NO_CHANGES = 0; - NORMAL_SYNC = 1; - FULL_SYNC = 2; - } - Required required = 1; + enum Required { + NO_CHANGES = 0; + NORMAL_SYNC = 1; + FULL_SYNC = 2; + } + Required required = 1; } message SyncCollectionOut { - enum ChangesRequired { - NO_CHANGES = 0; - NORMAL_SYNC = 1; - FULL_SYNC = 2; - // local collection has no cards; upload not an option - FULL_DOWNLOAD = 3; - // remote collection has no cards; download not an option - FULL_UPLOAD = 4; - } + enum ChangesRequired { + NO_CHANGES = 0; + NORMAL_SYNC = 1; + FULL_SYNC = 2; + // local collection has no cards; upload not an option + FULL_DOWNLOAD = 3; + // remote collection has no cards; download not an option + FULL_UPLOAD = 4; + } - uint32 host_number = 1; - string server_message = 2; - ChangesRequired required = 3; + uint32 host_number = 1; + string server_message = 2; + ChangesRequired required = 3; } message SyncAuth { - string hkey = 1; - uint32 host_number = 2; + string hkey = 1; + uint32 host_number = 2; } message RemoveNotesIn { - repeated int64 note_ids = 1; - repeated int64 card_ids = 2; + repeated int64 note_ids = 1; + repeated int64 card_ids = 2; } message RemoveCardsIn { - repeated int64 card_ids = 1; + repeated int64 card_ids = 1; } message UpdateStatsIn { - int64 deck_id = 1; - int32 new_delta = 2; - int32 review_delta = 4; - int32 millisecond_delta = 5; + int64 deck_id = 1; + int32 new_delta = 2; + int32 review_delta = 4; + int32 millisecond_delta = 5; } message ExtendLimitsIn { - int64 deck_id = 1; - int32 new_delta = 2; - int32 review_delta = 3; + int64 deck_id = 1; + int32 new_delta = 2; + int32 review_delta = 3; } message CountsForDeckTodayOut { - int32 new = 1; - int32 review = 2; + int32 new = 1; + int32 review = 2; } message GraphsIn { - string search = 1; - uint32 days = 2; + string search = 1; + uint32 days = 2; } message GraphsOut { - repeated Card cards = 1; - repeated RevlogEntry revlog = 2; - uint32 days_elapsed = 3; - // Based on rollover hour - uint32 next_day_at_secs = 4; - uint32 scheduler_version = 5; - /// Seconds to add to UTC timestamps to get local time. - int32 local_offset_secs = 7; + repeated Card cards = 1; + repeated RevlogEntry revlog = 2; + uint32 days_elapsed = 3; + // Based on rollover hour + uint32 next_day_at_secs = 4; + uint32 scheduler_version = 5; + /// Seconds to add to UTC timestamps to get local time. + int32 local_offset_secs = 7; } message RevlogEntry { - enum ReviewKind { - LEARNING = 0; - REVIEW = 1; - RELEARNING = 2; - EARLY_REVIEW = 3; - MANUAL = 4; - } - int64 id = 1; - int64 cid = 2; - int32 usn = 3; - uint32 button_chosen = 4; - int32 interval = 5; - int32 last_interval = 6; - uint32 ease_factor = 7; - uint32 taken_millis = 8; - ReviewKind review_kind = 9; + enum ReviewKind { + LEARNING = 0; + REVIEW = 1; + RELEARNING = 2; + EARLY_REVIEW = 3; + MANUAL = 4; + } + int64 id = 1; + int64 cid = 2; + int32 usn = 3; + uint32 button_chosen = 4; + int32 interval = 5; + int32 last_interval = 6; + uint32 ease_factor = 7; + uint32 taken_millis = 8; + ReviewKind review_kind = 9; } message CongratsInfoOut { - uint32 learn_remaining = 1; - uint32 secs_until_next_learn = 2; - bool review_remaining = 3; - bool new_remaining = 4; - bool have_sched_buried = 5; - bool have_user_buried = 6; - bool is_filtered_deck = 7; - bool bridge_commands_supported = 8; + uint32 learn_remaining = 1; + uint32 secs_until_next_learn = 2; + bool review_remaining = 3; + bool new_remaining = 4; + bool have_sched_buried = 5; + bool have_user_buried = 6; + bool is_filtered_deck = 7; + bool bridge_commands_supported = 8; } message UnburyCardsInCurrentDeckIn { - enum Mode { - ALL = 0; - SCHED_ONLY = 1; - USER_ONLY = 2; - } - Mode mode = 1; + enum Mode { + ALL = 0; + SCHED_ONLY = 1; + USER_ONLY = 2; + } + Mode mode = 1; } message BuryOrSuspendCardsIn { - enum Mode { - SUSPEND = 0; - BURY_SCHED = 1; - BURY_USER = 2; - } - repeated int64 card_ids = 1; - Mode mode = 2; + enum Mode { + SUSPEND = 0; + BURY_SCHED = 1; + BURY_USER = 2; + } + repeated int64 card_ids = 1; + Mode mode = 2; } message ScheduleCardsAsReviewsIn { - repeated int64 card_ids = 1; - uint32 min_interval = 2; - uint32 max_interval = 3; + repeated int64 card_ids = 1; + uint32 min_interval = 2; + uint32 max_interval = 3; } message ScheduleCardsAsNewIn { - repeated int64 card_ids = 1; - bool log = 2; + repeated int64 card_ids = 1; + bool log = 2; } message SortCardsIn { - repeated int64 card_ids = 1; - uint32 starting_from = 2; - uint32 step_size = 3; - bool randomize = 4; - bool shift_existing = 5; + repeated int64 card_ids = 1; + uint32 starting_from = 2; + uint32 step_size = 3; + bool randomize = 4; + bool shift_existing = 5; } message SortDeckIn { - int64 deck_id = 1; - bool randomize = 2; + int64 deck_id = 1; + bool randomize = 2; } message SetDeckIn { - repeated int64 card_ids = 1; - int64 deck_id = 2; + repeated int64 card_ids = 1; + int64 deck_id = 2; } diff --git a/rslib/clang_format.bzl b/rslib/clang_format.bzl new file mode 100644 index 000000000..a2fd92a69 --- /dev/null +++ b/rslib/clang_format.bzl @@ -0,0 +1,73 @@ +""" +Exposes a clang-format binary for formatting protobuf. +""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("@rules_python//python:defs.bzl", "py_test") + +def _impl(rctx): + rctx.file("BUILD.bazel", """ +alias( + name = "clang_format", + actual = select({ + "@net_ankiweb_anki//platforms:windows_x86_64": "@clang_format_windows_x86_64//:clang-format.exe", + "@net_ankiweb_anki//platforms:macos_x86_64": "@clang_format_macos_x86_64//:clang-format", + "@net_ankiweb_anki//platforms:linux_x86_64": "@clang_format_linux_x86_64//:clang-format", + }), + visibility = ["//visibility:public"] +) +""") + +_setup_clang_format = repository_rule( + attrs = {}, + local = True, + implementation = _impl, +) + +def setup_clang_format(name): + maybe( + http_archive, + name = "clang_format_macos_x86_64", + build_file_content = """exports_files(["clang-format"])""", + sha256 = "238be68d9478163a945754f06a213483473044f5a004c4125d3d9d8d3556466e", + urls = [ + "https://github.com/ankitects/clang-format-binaries/releases/download/anki-2021-01-09/clang-format_macos_x86_64.zip", + ], + ) + + maybe( + http_archive, + name = "clang_format_linux_x86_64", + build_file_content = """exports_files(["clang-format"])""", + sha256 = "64060bc4dbca30d0d96aab9344e2783008b16e1cae019a2532f1126ca5ec5449", + urls = [ + "https://github.com/ankitects/clang-format-binaries/releases/download/anki-2021-01-09/clang-format_linux_x86_64.zip", + ], + ) + + maybe( + http_archive, + name = "clang_format_windows_x86_64", + build_file_content = """exports_files(["clang-format.exe"])""", + sha256 = "7d9f6915e3f0fb72407830f0fc37141308d2e6915daba72987a52f309fbeaccc", + urls = [ + "https://github.com/ankitects/clang-format-binaries/releases/download/anki-2021-01-09/clang-format_windows_x86_64.zip", + ], + ) + + if not native.existing_rule(name): + _setup_clang_format( + name = name, + ) + +def proto_format(name, srcs, **kwargs): + py_test( + name = name, + srcs = [ + "proto_format.py", + ], + data = ["@clang_format//:clang_format"] + srcs, + args = ["$(location @clang_format//:clang_format)"] + [native.package_name() + "/" + f for f in srcs], + **kwargs + ) diff --git a/rslib/proto_format.py b/rslib/proto_format.py new file mode 100755 index 000000000..306146a5d --- /dev/null +++ b/rslib/proto_format.py @@ -0,0 +1,36 @@ +import sys, subprocess, os, difflib + +clang_format = sys.argv[1] +workspace = os.environ.get("BUILD_WORKSPACE_DIRECTORY", "") +want_fix = bool(workspace) + +found_bad = False +for path in sys.argv[2:]: + with open(path) as file: + orig = file.read() + new = subprocess.check_output( + # [clang_format, "--style={'BasedOnStyle': 'google', 'IndentWidth': 4}", path] + [clang_format, "--style=google", path] + ).decode("utf-8") + if orig != new: + if want_fix: + with open(os.path.join(workspace, path), "w") as file: + file.write(new) + print("fixed", path) + else: + print(f"Bad formatting in {path}") + print( + "\n".join( + difflib.unified_diff( + orig.splitlines(), + new.splitlines(), + fromfile="bad", + tofile="good", + lineterm="", + ) + ) + ) + found_bad = True + +if found_bad: + sys.exit(1) \ No newline at end of file diff --git a/rslib/src/media/schema.sql b/rslib/src/media/schema.sql index 236ebe216..46d6354b2 100644 --- a/rslib/src/media/schema.sql +++ b/rslib/src/media/schema.sql @@ -1,10 +1,13 @@ -create table media ( - fname text not null primary key, - csum text, -- null indicates deleted file - mtime int not null, -- zero if deleted - dirty int not null +CREATE TABLE media ( + fname text NOT NULL PRIMARY KEY, + -- null indicates deleted file + csum text, + -- zero if deleted + mtime int NOT NULL, + dirty int NOT NULL ) without rowid; - -create index idx_media_dirty on media (dirty) where dirty=1; - -create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); +CREATE INDEX idx_media_dirty ON media (dirty) +WHERE dirty = 1; +CREATE TABLE meta (dirMod int, lastUsn int); +INSERT INTO meta +VALUES (0, 0); \ No newline at end of file diff --git a/rslib/src/sched/new.rs b/rslib/src/sched/new.rs index 4043fb1b1..b19b9eacf 100644 --- a/rslib/src/sched/new.rs +++ b/rslib/src/sched/new.rs @@ -4,7 +4,6 @@ use crate::{ card::{Card, CardID, CardQueue, CardType}, collection::Collection, - deckconf::INITIAL_EASE_FACTOR_THOUSANDS, decks::DeckID, err::Result, notes::NoteID, @@ -21,11 +20,7 @@ impl Card { self.ctype = CardType::New; self.queue = CardQueue::New; self.interval = 0; - if self.ease_factor == 0 { - // unlike the old Python code, we leave the ease factor alone - // if it's already set - self.ease_factor = INITIAL_EASE_FACTOR_THOUSANDS; - } + self.ease_factor = 0; } /// If the card is new, change its position. diff --git a/rslib/src/search/deck_order.sql b/rslib/src/search/deck_order.sql index 9a679195b..823f6dada 100644 --- a/rslib/src/search/deck_order.sql +++ b/rslib/src/search/deck_order.sql @@ -1,11 +1,9 @@ -drop table if exists sort_order; -create temporary table sort_order ( - pos integer primary key, - did integer not null unique +DROP TABLE IF EXISTS sort_order; +CREATE TEMPORARY TABLE sort_order ( + pos integer PRIMARY KEY, + did integer NOT NULL UNIQUE ); -insert into sort_order (did) -select - id -from decks -order by - name; \ No newline at end of file +INSERT INTO sort_order (did) +SELECT id +FROM decks +ORDER BY name; \ No newline at end of file diff --git a/rslib/src/search/notetype_order.sql b/rslib/src/search/notetype_order.sql index 756418ec8..a5e07b086 100644 --- a/rslib/src/search/notetype_order.sql +++ b/rslib/src/search/notetype_order.sql @@ -1,11 +1,9 @@ -drop table if exists sort_order; -create temporary table sort_order ( - pos integer primary key, - ntid integer not null unique +DROP TABLE IF EXISTS sort_order; +CREATE TEMPORARY TABLE sort_order ( + pos integer PRIMARY KEY, + ntid integer NOT NULL UNIQUE ); -insert into sort_order (ntid) -select - id -from notetypes -order by - name; \ No newline at end of file +INSERT INTO sort_order (ntid) +SELECT id +FROM notetypes +ORDER BY name; \ No newline at end of file diff --git a/rslib/src/search/template_order.sql b/rslib/src/search/template_order.sql index c120b24bb..96b7ecd1d 100644 --- a/rslib/src/search/template_order.sql +++ b/rslib/src/search/template_order.sql @@ -1,14 +1,12 @@ -drop table if exists sort_order; -create temporary table sort_order ( - pos integer primary key, - ntid integer not null, - ord integer not null, - unique(ntid, ord) +DROP TABLE IF EXISTS sort_order; +CREATE TEMPORARY TABLE sort_order ( + pos integer PRIMARY KEY, + ntid integer NOT NULL, + ord integer NOT NULL, + UNIQUE(ntid, ord) ); -insert into sort_order (ntid, ord) -select - ntid, +INSERT INTO sort_order (ntid, ord) +SELECT ntid, ord -from templates -order by - name \ No newline at end of file +FROM templates +ORDER BY name \ No newline at end of file diff --git a/rslib/src/storage/card/add_card.sql b/rslib/src/storage/card/add_card.sql index 5da46ac1f..659723648 100644 --- a/rslib/src/storage/card/add_card.sql +++ b/rslib/src/storage/card/add_card.sql @@ -1,4 +1,4 @@ -insert into cards ( +INSERT INTO cards ( id, nid, did, @@ -18,21 +18,18 @@ insert into cards ( flags, data ) -values - ( +VALUES ( ( - case - when ?1 in ( - select - id - from cards - ) then ( - select - max(id) + 1 - from cards + CASE + WHEN ?1 IN ( + SELECT id + FROM cards + ) THEN ( + SELECT max(id) + 1 + FROM cards ) - else ?1 - end + ELSE ?1 + END ), ?, ?, diff --git a/rslib/src/storage/card/add_or_update.sql b/rslib/src/storage/card/add_or_update.sql index 641e0de18..646ce3595 100644 --- a/rslib/src/storage/card/add_or_update.sql +++ b/rslib/src/storage/card/add_or_update.sql @@ -1,5 +1,5 @@ -insert - or replace into cards ( +INSERT + OR REPLACE INTO cards ( id, nid, did, @@ -19,8 +19,7 @@ insert flags, data ) -values - ( +VALUES ( ?, ?, ?, diff --git a/rslib/src/storage/card/at_or_above_position.sql b/rslib/src/storage/card/at_or_above_position.sql index 2621aa55a..73ed0f475 100644 --- a/rslib/src/storage/card/at_or_above_position.sql +++ b/rslib/src/storage/card/at_or_above_position.sql @@ -1,5 +1,5 @@ -insert into search_cids -select id -from cards -where due >= ? - and type = ? \ No newline at end of file +INSERT INTO search_cids +SELECT id +FROM cards +WHERE due >= ? + AND type = ? \ No newline at end of file diff --git a/rslib/src/storage/card/congrats.sql b/rslib/src/storage/card/congrats.sql index b62d8cc81..e720feb04 100644 --- a/rslib/src/storage/card/congrats.sql +++ b/rslib/src/storage/card/congrats.sql @@ -1,28 +1,28 @@ -select coalesce( - sum( - queue in (:review_queue, :day_learn_queue) - and due <= :today - ), - 0 - ) as review_count, - coalesce(sum(queue = :new_queue), 0) as new_count, - coalesce(sum(queue = :sched_buried_queue), 0) as sched_buried, - coalesce(sum(queue = :user_buried_queue), 0) as user_buried, - coalesce(sum(queue = :learn_queue), 0) as learn_count, - max( - 0, - coalesce( - min( - case - when queue = :learn_queue then due - else null - end - ), - 0 - ) - ) as first_learn_due -from cards -where did in ( - select id - from active_decks - ) \ No newline at end of file +SELECT coalesce( + sum( + queue IN (:review_queue, :day_learn_queue) + AND due <= :today + ), + 0 + ) AS review_count, + coalesce(sum(queue = :new_queue), 0) AS new_count, + coalesce(sum(queue = :sched_buried_queue), 0) AS sched_buried, + coalesce(sum(queue = :user_buried_queue), 0) AS user_buried, + coalesce(sum(queue = :learn_queue), 0) AS learn_count, + max( + 0, + coalesce( + min( + CASE + WHEN queue = :learn_queue THEN due + ELSE NULL + END + ), + 0 + ) + ) AS first_learn_due +FROM cards +WHERE did IN ( + SELECT id + FROM active_decks + ) \ No newline at end of file diff --git a/rslib/src/storage/card/fix_due_new.sql b/rslib/src/storage/card/fix_due_new.sql index d24f9fad4..327c5522e 100644 --- a/rslib/src/storage/card/fix_due_new.sql +++ b/rslib/src/storage/card/fix_due_new.sql @@ -1,20 +1,18 @@ -update cards -set - due = ( - case - when type = 0 - and queue != 4 then 1000000 + due % 1000000 - else due - end +UPDATE cards +SET due = ( + CASE + WHEN type = 0 + AND queue != 4 THEN 1000000 + due % 1000000 + ELSE due + END ), mod = ?1, usn = ?2 -where - due != ( - case - when type = 0 - and queue != 4 then 1000000 + due % 1000000 - else due - end +WHERE due != ( + CASE + WHEN type = 0 + AND queue != 4 THEN 1000000 + due % 1000000 + ELSE due + END ) - and due >= 1000000; \ No newline at end of file + AND due >= 1000000; \ No newline at end of file diff --git a/rslib/src/storage/card/fix_due_other.sql b/rslib/src/storage/card/fix_due_other.sql index 8d71c1941..6c072e5b2 100644 --- a/rslib/src/storage/card/fix_due_other.sql +++ b/rslib/src/storage/card/fix_due_other.sql @@ -1,19 +1,17 @@ -update cards -set - due = ( - case - when queue = 2 - and due > 100000 then ?1 - else min(max(round(due), -2147483648), 2147483647) - end +UPDATE cards +SET due = ( + CASE + WHEN queue = 2 + AND due > 100000 THEN ?1 + ELSE min(max(round(due), -2147483648), 2147483647) + END ), mod = ?2, usn = ?3 -where - due != ( - case - when queue = 2 - and due > 100000 then ?1 - else min(max(round(due), -2147483648), 2147483647) - end +WHERE due != ( + CASE + WHEN queue = 2 + AND due > 100000 THEN ?1 + ELSE min(max(round(due), -2147483648), 2147483647) + END ); \ No newline at end of file diff --git a/rslib/src/storage/card/fix_ivl.sql b/rslib/src/storage/card/fix_ivl.sql index a77c1793b..d8a1894be 100644 --- a/rslib/src/storage/card/fix_ivl.sql +++ b/rslib/src/storage/card/fix_ivl.sql @@ -1,7 +1,5 @@ -update cards -set - ivl = min(max(round(ivl), 0), 2147483647), +UPDATE cards +SET ivl = min(max(round(ivl), 0), 2147483647), mod = ?1, usn = ?2 -where - ivl != min(max(round(ivl), 0), 2147483647) \ No newline at end of file +WHERE ivl != min(max(round(ivl), 0), 2147483647) \ No newline at end of file diff --git a/rslib/src/storage/card/fix_low_ease.sql b/rslib/src/storage/card/fix_low_ease.sql index d905613d0..23e5a97e4 100644 --- a/rslib/src/storage/card/fix_low_ease.sql +++ b/rslib/src/storage/card/fix_low_ease.sql @@ -1,10 +1,10 @@ -update cards -set factor = 2500, - usn = ?, - mod = ? -where factor != 0 - and factor <= 2000 - and ( - did in DECK_IDS - or odid in DECK_IDS - ) \ No newline at end of file +UPDATE cards +SET factor = 2500, + usn = ?, + mod = ? +WHERE factor != 0 + AND factor <= 2000 + AND ( + did IN DECK_IDS + OR odid IN DECK_IDS + ) \ No newline at end of file diff --git a/rslib/src/storage/card/fix_odue.sql b/rslib/src/storage/card/fix_odue.sql index dc121333e..87b636984 100644 --- a/rslib/src/storage/card/fix_odue.sql +++ b/rslib/src/storage/card/fix_odue.sql @@ -1,27 +1,25 @@ -update cards -set - odue = ( - case - when odue > 0 - and ( +UPDATE cards +SET odue = ( + CASE + WHEN odue > 0 + AND ( type = 1 - or queue = 2 + OR queue = 2 ) - and not odid then 0 - else min(max(round(odue), -2147483648), 2147483647) - end + AND NOT odid THEN 0 + ELSE min(max(round(odue), -2147483648), 2147483647) + END ), mod = ?1, usn = ?2 -where - odue != ( - case - when odue > 0 - and ( +WHERE odue != ( + CASE + WHEN odue > 0 + AND ( type = 1 - or queue = 2 + OR queue = 2 ) - and not odid then 0 - else min(max(round(odue), -2147483648), 2147483647) - end + AND NOT odid THEN 0 + ELSE min(max(round(odue), -2147483648), 2147483647) + END ); \ No newline at end of file diff --git a/rslib/src/storage/card/get_card.sql b/rslib/src/storage/card/get_card.sql index 227c925ab..0ba5d9b89 100644 --- a/rslib/src/storage/card/get_card.sql +++ b/rslib/src/storage/card/get_card.sql @@ -1,14 +1,13 @@ -select - id, +SELECT id, nid, did, ord, - cast(mod as integer), + cast(mod AS integer), usn, type, queue, due, - cast(ivl as integer), + cast(ivl AS integer), factor, reps, lapses, @@ -17,4 +16,4 @@ select odid, flags, data -from cards \ No newline at end of file +FROM cards \ No newline at end of file diff --git a/rslib/src/storage/card/search_cids_setup.sql b/rslib/src/storage/card/search_cids_setup.sql index 485b63ec7..7da26c813 100644 --- a/rslib/src/storage/card/search_cids_setup.sql +++ b/rslib/src/storage/card/search_cids_setup.sql @@ -1,2 +1,2 @@ -drop table if exists search_cids; -create temporary table search_cids (cid integer primary key not null); \ No newline at end of file +DROP TABLE IF EXISTS search_cids; +CREATE TEMPORARY TABLE search_cids (cid integer PRIMARY KEY NOT NULL); \ No newline at end of file diff --git a/rslib/src/storage/card/search_cids_setup_ordered.sql b/rslib/src/storage/card/search_cids_setup_ordered.sql index 34cb16647..e3671338d 100644 --- a/rslib/src/storage/card/search_cids_setup_ordered.sql +++ b/rslib/src/storage/card/search_cids_setup_ordered.sql @@ -1,2 +1,2 @@ -drop table if exists search_cids; -create temporary table search_cids (cid integer not null); \ No newline at end of file +DROP TABLE IF EXISTS search_cids; +CREATE TEMPORARY TABLE search_cids (cid integer NOT NULL); \ No newline at end of file diff --git a/rslib/src/storage/card/update_card.sql b/rslib/src/storage/card/update_card.sql index 3c0f6bfc1..8a9a68531 100644 --- a/rslib/src/storage/card/update_card.sql +++ b/rslib/src/storage/card/update_card.sql @@ -1,6 +1,5 @@ -update cards -set - nid = ?, +UPDATE cards +SET nid = ?, did = ?, ord = ?, mod = ?, @@ -17,5 +16,4 @@ set odid = ?, flags = ?, data = ? -where - id = ? \ No newline at end of file +WHERE id = ? \ No newline at end of file diff --git a/rslib/src/storage/config/add.sql b/rslib/src/storage/config/add.sql index a0663035c..16531c36e 100644 --- a/rslib/src/storage/config/add.sql +++ b/rslib/src/storage/config/add.sql @@ -1,4 +1,3 @@ -insert - or replace into config (key, usn, mtime_secs, val) -values - (?, ?, ?, ?) \ No newline at end of file +INSERT + OR REPLACE INTO config (KEY, usn, mtime_secs, val) +VALUES (?, ?, ?, ?) \ No newline at end of file diff --git a/rslib/src/storage/config/get.sql b/rslib/src/storage/config/get.sql index ae7ca50a6..73a86c2df 100644 --- a/rslib/src/storage/config/get.sql +++ b/rslib/src/storage/config/get.sql @@ -1,5 +1,3 @@ -select - val -from config -where - key = ? \ No newline at end of file +SELECT val +FROM config +WHERE KEY = ? \ No newline at end of file diff --git a/rslib/src/storage/deck/alloc_id.sql b/rslib/src/storage/deck/alloc_id.sql index 80004ad56..ec3512702 100644 --- a/rslib/src/storage/deck/alloc_id.sql +++ b/rslib/src/storage/deck/alloc_id.sql @@ -1,13 +1,10 @@ -select - case - when ?1 in ( - select - id - from decks - ) then ( - select - max(id) + 1 - from decks +SELECT CASE + WHEN ?1 IN ( + SELECT id + FROM decks + ) THEN ( + SELECT max(id) + 1 + FROM decks ) - else ?1 - end; \ No newline at end of file + ELSE ?1 + END; \ No newline at end of file diff --git a/rslib/src/storage/deck/cards_for_deck.sql b/rslib/src/storage/deck/cards_for_deck.sql index 6c9cfb1f3..973affc28 100644 --- a/rslib/src/storage/deck/cards_for_deck.sql +++ b/rslib/src/storage/deck/cards_for_deck.sql @@ -1,9 +1,7 @@ -select - id -from cards -where - did = ?1 - or ( +SELECT id +FROM cards +WHERE did = ?1 + OR ( odid != 0 - and odid = ?1 + AND odid = ?1 ) \ No newline at end of file diff --git a/rslib/src/storage/deck/due_counts.sql b/rslib/src/storage/deck/due_counts.sql index 5d94aeee7..f93be6523 100644 --- a/rslib/src/storage/deck/due_counts.sql +++ b/rslib/src/storage/deck/due_counts.sql @@ -1,41 +1,41 @@ -select did, +SELECT did, sum(queue = :new_queue), sum( queue = :review_queue - and due <= :day_cutoff + AND due <= :day_cutoff ), -- learning sum( ( - case + CASE :sched_ver - when 2 then ( + WHEN 2 THEN ( -- v2 scheduler ( queue = :learn_queue - and due < :learn_cutoff + AND due < :learn_cutoff ) - or ( + OR ( queue = :daylearn_queue - and due <= :day_cutoff + AND due <= :day_cutoff ) - or ( + OR ( queue = :preview_queue - and due <= :learn_cutoff + AND due <= :learn_cutoff ) ) - else ( + ELSE ( -- v1 scheduler - case - when queue = :learn_queue - and due < :learn_cutoff then left / 1000 - when queue = :daylearn_queue - and due <= :day_cutoff then 1 - else 0 - end + CASE + WHEN queue = :learn_queue + AND due < :learn_cutoff THEN left / 1000 + WHEN queue = :daylearn_queue + AND due <= :day_cutoff THEN 1 + ELSE 0 + END ) - end + END ) ) -from cards -where queue >= 0 \ No newline at end of file +FROM cards +WHERE queue >= 0 \ No newline at end of file diff --git a/rslib/src/storage/deck/get_deck.sql b/rslib/src/storage/deck/get_deck.sql index b456367b5..8afd87aa1 100644 --- a/rslib/src/storage/deck/get_deck.sql +++ b/rslib/src/storage/deck/get_deck.sql @@ -1,8 +1,7 @@ -select - id, +SELECT id, name, mtime_secs, usn, common, kind -from decks \ No newline at end of file +FROM decks \ No newline at end of file diff --git a/rslib/src/storage/deck/missing-decks.sql b/rslib/src/storage/deck/missing-decks.sql index dfba53afc..e2c79691c 100644 --- a/rslib/src/storage/deck/missing-decks.sql +++ b/rslib/src/storage/deck/missing-decks.sql @@ -1,9 +1,6 @@ -select - distinct did -from cards -where - did not in ( - select - id - from decks +SELECT DISTINCT did +FROM cards +WHERE did NOT IN ( + SELECT id + FROM decks ); \ No newline at end of file diff --git a/rslib/src/storage/deck/update_active.sql b/rslib/src/storage/deck/update_active.sql index 54f49bf7f..7da982e47 100644 --- a/rslib/src/storage/deck/update_active.sql +++ b/rslib/src/storage/deck/update_active.sql @@ -1,8 +1,8 @@ -insert into active_decks -select id -from decks -where name = ? - or ( - name >= ? - and name < ? - ) \ No newline at end of file +INSERT INTO active_decks +SELECT id +FROM decks +WHERE name = ? + OR ( + name >= ? + AND name < ? + ) \ No newline at end of file diff --git a/rslib/src/storage/deck/update_deck.sql b/rslib/src/storage/deck/update_deck.sql index d9e8a1d8c..7e35c1902 100644 --- a/rslib/src/storage/deck/update_deck.sql +++ b/rslib/src/storage/deck/update_deck.sql @@ -1,4 +1,3 @@ -insert - or replace into decks (id, name, mtime_secs, usn, common, kind) -values - (?, ?, ?, ?, ?, ?) \ No newline at end of file +INSERT + OR REPLACE INTO decks (id, name, mtime_secs, usn, common, kind) +VALUES (?, ?, ?, ?, ?, ?) \ No newline at end of file diff --git a/rslib/src/storage/deckconf/add.sql b/rslib/src/storage/deckconf/add.sql index 280e826db..e21f9e932 100644 --- a/rslib/src/storage/deckconf/add.sql +++ b/rslib/src/storage/deckconf/add.sql @@ -1,19 +1,16 @@ -insert into deck_config (id, name, mtime_secs, usn, config) -values - ( +INSERT INTO deck_config (id, name, mtime_secs, usn, config) +VALUES ( ( - case - when ?1 in ( - select - id - from deck_config - ) then ( - select - max(id) + 1 - from deck_config + CASE + WHEN ?1 IN ( + SELECT id + FROM deck_config + ) THEN ( + SELECT max(id) + 1 + FROM deck_config ) - else ?1 - end + ELSE ?1 + END ), ?, ?, diff --git a/rslib/src/storage/deckconf/add_or_update.sql b/rslib/src/storage/deckconf/add_or_update.sql index b81bcdfb9..d0def6223 100644 --- a/rslib/src/storage/deckconf/add_or_update.sql +++ b/rslib/src/storage/deckconf/add_or_update.sql @@ -1,4 +1,3 @@ -insert - or replace into deck_config (id, name, mtime_secs, usn, config) -values - (?, ?, ?, ?, ?); \ No newline at end of file +INSERT + OR REPLACE INTO deck_config (id, name, mtime_secs, usn, config) +VALUES (?, ?, ?, ?, ?); \ No newline at end of file diff --git a/rslib/src/storage/deckconf/get.sql b/rslib/src/storage/deckconf/get.sql index 18eb27c2c..99ad604c4 100644 --- a/rslib/src/storage/deckconf/get.sql +++ b/rslib/src/storage/deckconf/get.sql @@ -1,7 +1,6 @@ -select - id, +SELECT id, name, mtime_secs, usn, config -from deck_config +FROM deck_config \ No newline at end of file diff --git a/rslib/src/storage/deckconf/update.sql b/rslib/src/storage/deckconf/update.sql index ccebea6f8..b43a430ea 100644 --- a/rslib/src/storage/deckconf/update.sql +++ b/rslib/src/storage/deckconf/update.sql @@ -1,8 +1,6 @@ -update deck_config -set - name = ?, +UPDATE deck_config +SET name = ?, mtime_secs = ?, usn = ?, config = ? -where - id = ?; \ No newline at end of file +WHERE id = ?; \ No newline at end of file diff --git a/rslib/src/storage/graves/add.sql b/rslib/src/storage/graves/add.sql index d01f13c95..05ca02417 100644 --- a/rslib/src/storage/graves/add.sql +++ b/rslib/src/storage/graves/add.sql @@ -1,4 +1,3 @@ -insert - or ignore into graves (usn, oid, type) -values - (?, ?, ?) \ No newline at end of file +INSERT + OR IGNORE INTO graves (usn, oid, type) +VALUES (?, ?, ?) \ No newline at end of file diff --git a/rslib/src/storage/note/add.sql b/rslib/src/storage/note/add.sql index d4b12b6b6..ceac21533 100644 --- a/rslib/src/storage/note/add.sql +++ b/rslib/src/storage/note/add.sql @@ -1,4 +1,4 @@ -insert into notes ( +INSERT INTO notes ( id, guid, mid, @@ -11,21 +11,18 @@ insert into notes ( flags, data ) -values - ( +VALUES ( ( - case - when ?1 in ( - select - id - from notes - ) then ( - select - max(id) + 1 - from notes + CASE + WHEN ?1 IN ( + SELECT id + FROM notes + ) THEN ( + SELECT max(id) + 1 + FROM notes ) - else ?1 - end + ELSE ?1 + END ), ?, ?, diff --git a/rslib/src/storage/note/add_or_update.sql b/rslib/src/storage/note/add_or_update.sql index 08e521f34..dd8008c78 100644 --- a/rslib/src/storage/note/add_or_update.sql +++ b/rslib/src/storage/note/add_or_update.sql @@ -1,5 +1,5 @@ -insert - or replace into notes ( +INSERT + OR REPLACE INTO notes ( id, guid, mid, @@ -12,8 +12,7 @@ insert flags, data ) -values - ( +VALUES ( ?, ?, ?, diff --git a/rslib/src/storage/note/get.sql b/rslib/src/storage/note/get.sql index 76f2905da..fb1ba3619 100644 --- a/rslib/src/storage/note/get.sql +++ b/rslib/src/storage/note/get.sql @@ -1,9 +1,8 @@ -select - id, +SELECT id, guid, mid, mod, usn, tags, flds -from notes \ No newline at end of file +FROM notes \ No newline at end of file diff --git a/rslib/src/storage/note/is_orphaned.sql b/rslib/src/storage/note/is_orphaned.sql index 7b27c1ee9..bd7dfe39f 100644 --- a/rslib/src/storage/note/is_orphaned.sql +++ b/rslib/src/storage/note/is_orphaned.sql @@ -1,5 +1,3 @@ -select - count(id) = 0 -from cards -where - nid = ?; \ No newline at end of file +SELECT COUNT(id) = 0 +FROM cards +WHERE nid = ?; \ No newline at end of file diff --git a/rslib/src/storage/note/update.sql b/rslib/src/storage/note/update.sql index a3d86587a..8adf9dd69 100644 --- a/rslib/src/storage/note/update.sql +++ b/rslib/src/storage/note/update.sql @@ -1,6 +1,5 @@ -update notes -set - guid = ?, +UPDATE notes +SET guid = ?, mid = ?, mod = ?, usn = ?, @@ -8,5 +7,4 @@ set flds = ?, sfld = ?, csum = ? -where - id = ? \ No newline at end of file +WHERE id = ? \ No newline at end of file diff --git a/rslib/src/storage/notetype/add_notetype.sql b/rslib/src/storage/notetype/add_notetype.sql index 4578ab3b4..9cb83ae21 100644 --- a/rslib/src/storage/notetype/add_notetype.sql +++ b/rslib/src/storage/notetype/add_notetype.sql @@ -1,19 +1,16 @@ -insert into notetypes (id, name, mtime_secs, usn, config) -values - ( +INSERT INTO notetypes (id, name, mtime_secs, usn, config) +VALUES ( ( - case - when ?1 in ( - select - id - from notetypes - ) then ( - select - max(id) + 1 - from notetypes + CASE + WHEN ?1 IN ( + SELECT id + FROM notetypes + ) THEN ( + SELECT max(id) + 1 + FROM notetypes ) - else ?1 - end + ELSE ?1 + END ), ?, ?, diff --git a/rslib/src/storage/notetype/add_or_update.sql b/rslib/src/storage/notetype/add_or_update.sql index ec05af6d8..d8054f7bd 100644 --- a/rslib/src/storage/notetype/add_or_update.sql +++ b/rslib/src/storage/notetype/add_or_update.sql @@ -1,4 +1,3 @@ -insert - or replace into notetypes (id, name, mtime_secs, usn, config) -values - (?, ?, ?, ?, ?); \ No newline at end of file +INSERT + OR REPLACE INTO notetypes (id, name, mtime_secs, usn, config) +VALUES (?, ?, ?, ?, ?); \ No newline at end of file diff --git a/rslib/src/storage/notetype/delete_cards_for_template.sql b/rslib/src/storage/notetype/delete_cards_for_template.sql index 38748ebf5..97c5ca0b1 100644 --- a/rslib/src/storage/notetype/delete_cards_for_template.sql +++ b/rslib/src/storage/notetype/delete_cards_for_template.sql @@ -1,10 +1,7 @@ -delete from cards -where - nid in ( - select - id - from notes - where - mid = ? +DELETE FROM cards +WHERE nid IN ( + SELECT id + FROM notes + WHERE mid = ? ) - and ord = ?; \ No newline at end of file + AND ord = ?; \ No newline at end of file diff --git a/rslib/src/storage/notetype/existing_cards.sql b/rslib/src/storage/notetype/existing_cards.sql index 5f4658e05..82bc71836 100644 --- a/rslib/src/storage/notetype/existing_cards.sql +++ b/rslib/src/storage/notetype/existing_cards.sql @@ -1,27 +1,26 @@ -select - id, +SELECT id, nid, ord, -- original deck ( - case + CASE odid - when 0 then did - else odid - end + WHEN 0 THEN did + ELSE odid + END ), -- new position if card is empty ( - case + CASE type - when 0 then ( - case + WHEN 0 THEN ( + CASE odue - when 0 then max(0, due) - else max(odue, 0) - end + WHEN 0 THEN max(0, due) + ELSE max(odue, 0) + END ) - else null - end + ELSE NULL + END ) -from cards c \ No newline at end of file +FROM cards c \ No newline at end of file diff --git a/rslib/src/storage/notetype/field_names_for_notes.sql b/rslib/src/storage/notetype/field_names_for_notes.sql index a805044e3..3aa20c787 100644 --- a/rslib/src/storage/notetype/field_names_for_notes.sql +++ b/rslib/src/storage/notetype/field_names_for_notes.sql @@ -1,10 +1,6 @@ -select - distinct name -from fields -where - ntid in ( - select - mid - from notes - where - id in \ No newline at end of file +SELECT DISTINCT name +FROM FIELDS +WHERE ntid IN ( + SELECT mid + FROM notes + WHERE id IN \ No newline at end of file diff --git a/rslib/src/storage/notetype/get_fields.sql b/rslib/src/storage/notetype/get_fields.sql index aef718770..0213207bf 100644 --- a/rslib/src/storage/notetype/get_fields.sql +++ b/rslib/src/storage/notetype/get_fields.sql @@ -1,9 +1,6 @@ -select - ord, +SELECT ord, name, config -from fields -where - ntid = ? -order by - ord \ No newline at end of file +FROM FIELDS +WHERE ntid = ? +ORDER BY ord \ No newline at end of file diff --git a/rslib/src/storage/notetype/get_notetype.sql b/rslib/src/storage/notetype/get_notetype.sql index f405ed76d..91ed61f59 100644 --- a/rslib/src/storage/notetype/get_notetype.sql +++ b/rslib/src/storage/notetype/get_notetype.sql @@ -1,7 +1,6 @@ -select - id, +SELECT id, name, mtime_secs, usn, config -from notetypes +FROM notetypes \ No newline at end of file diff --git a/rslib/src/storage/notetype/get_notetype_names.sql b/rslib/src/storage/notetype/get_notetype_names.sql index a4393765b..ebbbb61e2 100644 --- a/rslib/src/storage/notetype/get_notetype_names.sql +++ b/rslib/src/storage/notetype/get_notetype_names.sql @@ -1,4 +1,3 @@ -select - id, +SELECT id, name -from notetypes \ No newline at end of file +FROM notetypes \ No newline at end of file diff --git a/rslib/src/storage/notetype/get_templates.sql b/rslib/src/storage/notetype/get_templates.sql index a6fcadb10..dc42818f4 100644 --- a/rslib/src/storage/notetype/get_templates.sql +++ b/rslib/src/storage/notetype/get_templates.sql @@ -1,11 +1,8 @@ -select - ord, +SELECT ord, name, mtime_secs, usn, config -from templates -where - ntid = ? -order by - ord \ No newline at end of file +FROM templates +WHERE ntid = ? +ORDER BY ord \ No newline at end of file diff --git a/rslib/src/storage/notetype/get_use_counts.sql b/rslib/src/storage/notetype/get_use_counts.sql index 7a00bb99f..dc28ce1c9 100644 --- a/rslib/src/storage/notetype/get_use_counts.sql +++ b/rslib/src/storage/notetype/get_use_counts.sql @@ -1,13 +1,9 @@ -select - nt.id, +SELECT nt.id, nt.name, ( - select - count(*) - from notes n - where - nt.id = n.mid + SELECT COUNT(*) + FROM notes n + WHERE nt.id = n.mid ) -from notetypes nt -order by - nt.name \ No newline at end of file +FROM notetypes nt +ORDER BY nt.name \ No newline at end of file diff --git a/rslib/src/storage/notetype/highest_card_ord.sql b/rslib/src/storage/notetype/highest_card_ord.sql index 68c8dc22a..5cefaf39f 100644 --- a/rslib/src/storage/notetype/highest_card_ord.sql +++ b/rslib/src/storage/notetype/highest_card_ord.sql @@ -1,7 +1,7 @@ -select coalesce(max(ord), 0) -from cards -where nid in ( - select id - from notes - where mid = ? - ) \ No newline at end of file +SELECT coalesce(max(ord), 0) +FROM cards +WHERE nid IN ( + SELECT id + FROM notes + WHERE mid = ? + ) \ No newline at end of file diff --git a/rslib/src/storage/notetype/update_fields.sql b/rslib/src/storage/notetype/update_fields.sql index c007f25e4..86eeb85b0 100644 --- a/rslib/src/storage/notetype/update_fields.sql +++ b/rslib/src/storage/notetype/update_fields.sql @@ -1,3 +1,2 @@ -insert into fields (ntid, ord, name, config) -values - (?, ?, ?, ?); \ No newline at end of file +INSERT INTO FIELDS (ntid, ord, name, config) +VALUES (?, ?, ?, ?); \ No newline at end of file diff --git a/rslib/src/storage/notetype/update_notetype_config.sql b/rslib/src/storage/notetype/update_notetype_config.sql index 2ddabad63..19a034222 100644 --- a/rslib/src/storage/notetype/update_notetype_config.sql +++ b/rslib/src/storage/notetype/update_notetype_config.sql @@ -1,4 +1,3 @@ -insert - or replace into notetype_config (ntid, config) -values - (?, ?) \ No newline at end of file +INSERT + OR REPLACE INTO notetype_config (ntid, config) +VALUES (?, ?) \ No newline at end of file diff --git a/rslib/src/storage/notetype/update_notetype_core.sql b/rslib/src/storage/notetype/update_notetype_core.sql index ceb55c55f..84e5f49e9 100644 --- a/rslib/src/storage/notetype/update_notetype_core.sql +++ b/rslib/src/storage/notetype/update_notetype_core.sql @@ -1,4 +1,3 @@ -insert - or replace into notetypes (id, name, mtime_secs, usn, config) -values - (?, ?, ?, ?, ?) \ No newline at end of file +INSERT + OR REPLACE INTO notetypes (id, name, mtime_secs, usn, config) +VALUES (?, ?, ?, ?, ?) \ No newline at end of file diff --git a/rslib/src/storage/notetype/update_templates.sql b/rslib/src/storage/notetype/update_templates.sql index 08d3022fc..b60f2e577 100644 --- a/rslib/src/storage/notetype/update_templates.sql +++ b/rslib/src/storage/notetype/update_templates.sql @@ -1,3 +1,2 @@ -insert into templates (ntid, ord, name, mtime_secs, usn, config) -values - (?, ?, ?, ?, ?, ?) \ No newline at end of file +INSERT INTO templates (ntid, ord, name, mtime_secs, usn, config) +VALUES (?, ?, ?, ?, ?, ?) \ No newline at end of file diff --git a/rslib/src/storage/revlog/add.sql b/rslib/src/storage/revlog/add.sql index b63301200..13f12e073 100644 --- a/rslib/src/storage/revlog/add.sql +++ b/rslib/src/storage/revlog/add.sql @@ -1,5 +1,5 @@ -insert - or ignore into revlog ( +INSERT + OR IGNORE INTO revlog ( id, cid, usn, @@ -10,18 +10,18 @@ insert time, type ) -values ( +VALUES ( ( - case - when ?1 in ( - select id - from revlog - ) then ( - select max(id) + 1 - from revlog + CASE + WHEN ?1 IN ( + SELECT id + FROM revlog + ) THEN ( + SELECT max(id) + 1 + FROM revlog ) - else ?1 - end + ELSE ?1 + END ), ?, ?, diff --git a/rslib/src/storage/revlog/fix_props.sql b/rslib/src/storage/revlog/fix_props.sql index 1a4ceebd0..28bffb431 100644 --- a/rslib/src/storage/revlog/fix_props.sql +++ b/rslib/src/storage/revlog/fix_props.sql @@ -1,7 +1,7 @@ -update revlog -set ivl = min(max(round(ivl), -2147483648), 2147483647), +UPDATE revlog +SET ivl = min(max(round(ivl), -2147483648), 2147483647), lastIvl = min(max(round(lastIvl), -2147483648), 2147483647), time = min(max(round(time), 0), 2147483647) -where ivl != min(max(round(ivl), -2147483648), 2147483647) - or lastIvl != min(max(round(lastIvl), -2147483648), 2147483647) - or time != min(max(round(time), 0), 2147483647) \ No newline at end of file +WHERE ivl != min(max(round(ivl), -2147483648), 2147483647) + OR lastIvl != min(max(round(lastIvl), -2147483648), 2147483647) + OR time != min(max(round(time), 0), 2147483647) \ No newline at end of file diff --git a/rslib/src/storage/revlog/get.sql b/rslib/src/storage/revlog/get.sql index 671eb1846..647ea5f55 100644 --- a/rslib/src/storage/revlog/get.sql +++ b/rslib/src/storage/revlog/get.sql @@ -1,5 +1,4 @@ -select - id, +SELECT id, cid, usn, ease, @@ -8,4 +7,4 @@ select factor, time, type -from revlog \ No newline at end of file +FROM revlog \ No newline at end of file diff --git a/rslib/src/storage/revlog/studied_today.sql b/rslib/src/storage/revlog/studied_today.sql index 73f8cfba3..2a341f632 100644 --- a/rslib/src/storage/revlog/studied_today.sql +++ b/rslib/src/storage/revlog/studied_today.sql @@ -1,5 +1,5 @@ -select count(), - coalesce(sum(time) / 1000.0, 0.0) -from revlog -where id > ? - and type != ? \ No newline at end of file +SELECT COUNT(), + coalesce(sum(time) / 1000.0, 0.0) +FROM revlog +WHERE id > ? + AND type != ? \ No newline at end of file diff --git a/rslib/src/storage/schema11.sql b/rslib/src/storage/schema11.sql index e5d1b7f4d..633a796d2 100644 --- a/rslib/src/storage/schema11.sql +++ b/rslib/src/storage/schema11.sql @@ -1,88 +1,92 @@ -create table col -( - id integer primary key, - crt integer not null, - mod integer not null, - scm integer not null, - ver integer not null, - dty integer not null, - usn integer not null, - ls integer not null, - conf text not null, - models text not null, - decks text not null, - dconf text not null, - tags text not null +CREATE TABLE col ( + id integer PRIMARY KEY, + crt integer NOT NULL, + mod integer NOT NULL, + scm integer NOT NULL, + ver integer NOT NULL, + dty integer NOT NULL, + usn integer NOT NULL, + ls integer NOT NULL, + conf text NOT NULL, + models text NOT NULL, + decks text NOT NULL, + dconf text NOT NULL, + tags text NOT NULL ); - -create table notes -( - id integer primary key, - guid text not null, - mid integer not null, - mod integer not null, - usn integer not null, - tags text not null, - flds text not null, - sfld integer not null, - csum integer not null, - flags integer not null, - data text not null +CREATE TABLE notes ( + id integer PRIMARY KEY, + guid text NOT NULL, + mid integer NOT NULL, + mod integer NOT NULL, + usn integer NOT NULL, + tags text NOT NULL, + flds text NOT NULL, + sfld integer NOT NULL, + csum integer NOT NULL, + flags integer NOT NULL, + data text NOT NULL ); - -create table cards -( - id integer primary key, - nid integer not null, - did integer not null, - ord integer not null, - mod integer not null, - usn integer not null, - type integer not null, - queue integer not null, - due integer not null, - ivl integer not null, - factor integer not null, - reps integer not null, - lapses integer not null, - left integer not null, - odue integer not null, - odid integer not null, - flags integer not null, - data text not null +CREATE TABLE cards ( + id integer PRIMARY KEY, + nid integer NOT NULL, + did integer NOT NULL, + ord integer NOT NULL, + mod integer NOT NULL, + usn integer NOT NULL, + type integer NOT NULL, + queue integer NOT NULL, + due integer NOT NULL, + ivl integer NOT NULL, + factor integer NOT NULL, + reps integer NOT NULL, + lapses integer NOT NULL, + left integer NOT NULL, + odue integer NOT NULL, + odid integer NOT NULL, + flags integer NOT NULL, + data text NOT NULL ); - -create table revlog -( - id integer primary key, - cid integer not null, - usn integer not null, - ease integer not null, - ivl integer not null, - lastIvl integer not null, - factor integer not null, - time integer not null, - type integer not null +CREATE TABLE revlog ( + id integer PRIMARY KEY, + cid integer NOT NULL, + usn integer NOT NULL, + ease integer NOT NULL, + ivl integer NOT NULL, + lastIvl integer NOT NULL, + factor integer NOT NULL, + time integer NOT NULL, + type integer NOT NULL ); - -create table graves -( - usn integer not null, - oid integer not null, - type integer not null +CREATE TABLE graves ( + usn integer NOT NULL, + oid integer NOT NULL, + type integer NOT NULL ); - -- syncing -create index ix_notes_usn on notes (usn); -create index ix_cards_usn on cards (usn); -create index ix_revlog_usn on revlog (usn); +CREATE INDEX ix_notes_usn ON notes (usn); +CREATE INDEX ix_cards_usn ON cards (usn); +CREATE INDEX ix_revlog_usn ON revlog (usn); -- card spacing, etc -create index ix_cards_nid on cards (nid); +CREATE INDEX ix_cards_nid ON cards (nid); -- scheduling and deck limiting -create index ix_cards_sched on cards (did, queue, due); +CREATE INDEX ix_cards_sched ON cards (did, queue, due); -- revlog by card -create index ix_revlog_cid on revlog (cid); +CREATE INDEX ix_revlog_cid ON revlog (cid); -- field uniqueness -create index ix_notes_csum on notes (csum); - -insert into col values (1,0,0,0,0,0,0,0,'{}','{}','{}','{}','{}'); +CREATE INDEX ix_notes_csum ON notes (csum); +INSERT INTO col +VALUES ( + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + '{}', + '{}', + '{}', + '{}', + '{}' + ); \ No newline at end of file diff --git a/rslib/src/storage/tag/add.sql b/rslib/src/storage/tag/add.sql index 8fee01a4c..211807a5f 100644 --- a/rslib/src/storage/tag/add.sql +++ b/rslib/src/storage/tag/add.sql @@ -1,4 +1,3 @@ -insert - or ignore into tags (tag, usn) -values - (?, ?) \ No newline at end of file +INSERT + OR IGNORE INTO tags (tag, usn) +VALUES (?, ?) \ No newline at end of file diff --git a/rslib/src/storage/upgrades/schema11_downgrade.sql b/rslib/src/storage/upgrades/schema11_downgrade.sql index ed7694ad7..03b73c5d5 100644 --- a/rslib/src/storage/upgrades/schema11_downgrade.sql +++ b/rslib/src/storage/upgrades/schema11_downgrade.sql @@ -1,12 +1,11 @@ -drop table config; -drop table deck_config; -drop table tags; -drop table fields; -drop table templates; -drop table notetypes; -drop table decks; -drop index idx_cards_odid; -drop index idx_notes_mid; -update col -set - ver = 11; \ No newline at end of file +DROP TABLE config; +DROP TABLE deck_config; +DROP TABLE tags; +DROP TABLE FIELDS; +DROP TABLE templates; +DROP TABLE notetypes; +DROP TABLE decks; +DROP INDEX idx_cards_odid; +DROP INDEX idx_notes_mid; +UPDATE col +SET ver = 11; \ No newline at end of file diff --git a/rslib/src/storage/upgrades/schema14_upgrade.sql b/rslib/src/storage/upgrades/schema14_upgrade.sql index ccd9352a4..cd602ce17 100644 --- a/rslib/src/storage/upgrades/schema14_upgrade.sql +++ b/rslib/src/storage/upgrades/schema14_upgrade.sql @@ -1,20 +1,19 @@ -create table deck_config ( - id integer primary key not null, - name text not null collate unicase, - mtime_secs integer not null, - usn integer not null, - config blob not null +CREATE TABLE deck_config ( + id integer PRIMARY KEY NOT NULL, + name text NOT NULL COLLATE unicase, + mtime_secs integer NOT NULL, + usn integer NOT NULL, + config blob NOT NULL ); -create table config ( - key text not null primary key, - usn integer not null, - mtime_secs integer not null, - val blob not null +CREATE TABLE config ( + KEY text NOT NULL PRIMARY KEY, + usn integer NOT NULL, + mtime_secs integer NOT NULL, + val blob NOT NULL ) without rowid; -create table tags ( - tag text not null primary key collate unicase, - usn integer not null +CREATE TABLE tags ( + tag text NOT NULL PRIMARY KEY COLLATE unicase, + usn integer NOT NULL ) without rowid; -update col -set - ver = 14; \ No newline at end of file +UPDATE col +SET ver = 14; \ No newline at end of file diff --git a/rslib/src/storage/upgrades/schema15_upgrade.sql b/rslib/src/storage/upgrades/schema15_upgrade.sql index 5af87298c..4fa23c8c6 100644 --- a/rslib/src/storage/upgrades/schema15_upgrade.sql +++ b/rslib/src/storage/upgrades/schema15_upgrade.sql @@ -1,45 +1,43 @@ -create table fields ( - ntid integer not null, - ord integer not null, - name text not null collate unicase, - config blob not null, - primary key (ntid, ord) +CREATE TABLE FIELDS ( + ntid integer NOT NULL, + ord integer NOT NULL, + name text NOT NULL COLLATE unicase, + config blob NOT NULL, + PRIMARY KEY (ntid, ord) ) without rowid; -create unique index idx_fields_name_ntid on fields (name, ntid); -create table templates ( - ntid integer not null, - ord integer not null, - name text not null collate unicase, - mtime_secs integer not null, - usn integer not null, - config blob not null, - primary key (ntid, ord) +CREATE UNIQUE INDEX idx_fields_name_ntid ON FIELDS (name, ntid); +CREATE TABLE templates ( + ntid integer NOT NULL, + ord integer NOT NULL, + name text NOT NULL COLLATE unicase, + mtime_secs integer NOT NULL, + usn integer NOT NULL, + config blob NOT NULL, + PRIMARY KEY (ntid, ord) ) without rowid; -create unique index idx_templates_name_ntid on templates (name, ntid); -create index idx_templates_usn on templates (usn); -create table notetypes ( - id integer not null primary key, - name text not null collate unicase, - mtime_secs integer not null, - usn integer not null, - config blob not null +CREATE UNIQUE INDEX idx_templates_name_ntid ON templates (name, ntid); +CREATE INDEX idx_templates_usn ON templates (usn); +CREATE TABLE notetypes ( + id integer NOT NULL PRIMARY KEY, + name text NOT NULL COLLATE unicase, + mtime_secs integer NOT NULL, + usn integer NOT NULL, + config blob NOT NULL ); -create unique index idx_notetypes_name on notetypes (name); -create index idx_notetypes_usn on notetypes (usn); -create table decks ( - id integer primary key not null, - name text not null collate unicase, - mtime_secs integer not null, - usn integer not null, - common blob not null, - kind blob not null +CREATE UNIQUE INDEX idx_notetypes_name ON notetypes (name); +CREATE INDEX idx_notetypes_usn ON notetypes (usn); +CREATE TABLE decks ( + id integer PRIMARY KEY NOT NULL, + name text NOT NULL COLLATE unicase, + mtime_secs integer NOT NULL, + usn integer NOT NULL, + common blob NOT NULL, + kind blob NOT NULL ); -create unique index idx_decks_name on decks (name); -create index idx_notes_mid on notes (mid); -create index idx_cards_odid on cards (odid) -where - odid != 0; -update col -set - ver = 15; -analyze; \ No newline at end of file +CREATE UNIQUE INDEX idx_decks_name ON decks (name); +CREATE INDEX idx_notes_mid ON notes (mid); +CREATE INDEX idx_cards_odid ON cards (odid) +WHERE odid != 0; +UPDATE col +SET ver = 15; +ANALYZE; \ No newline at end of file diff --git a/ts/BUILD.bazel b/ts/BUILD.bazel index e3692fb73..53b97d3cd 100644 --- a/ts/BUILD.bazel +++ b/ts/BUILD.bazel @@ -1,7 +1,18 @@ -load("//ts:prettier.bzl", "prettier") +load("//ts:prettier.bzl", "prettier", "prettier_test") +load("//ts:sql_format.bzl", "sql_format_setup") prettier() +prettier_test( + name = "format_check", + srcs = glob([ + "*.ts", + "*.js", + ]), +) + +sql_format_setup() + # Exported files ################# @@ -12,6 +23,7 @@ exports_files([ "rollup.config.js", ".eslintrc.js", "licenses.json", + "sql_format.ts", ]) alias( diff --git a/ts/graphs/card-counts.ts b/ts/graphs/card-counts.ts index 7451466c1..f45bca720 100644 --- a/ts/graphs/card-counts.ts +++ b/ts/graphs/card-counts.ts @@ -197,7 +197,7 @@ export function renderCards( x.range([bounds.marginLeft, bounds.width - bounds.marginRight]); - const tableData = (data as any).flatMap((d: SummedDatum, idx: number) => { + const tableData = data.flatMap((d: SummedDatum, idx: number) => { const percent = ((d.count / xMax) * 100).toFixed(1); return d.show ? ({ diff --git a/ts/graphs/ease.ts b/ts/graphs/ease.ts index ed512d6b7..872efa77d 100644 --- a/ts/graphs/ease.ts +++ b/ts/graphs/ease.ts @@ -9,7 +9,7 @@ import type pb from "anki/backend_proto"; import { extent, histogram, sum } from "d3-array"; import { scaleLinear, scaleSequential } from "d3-scale"; -import { CardQueue } from "anki/cards"; +import { CardType } from "anki/cards"; import type { HistogramData } from "./histogram-graph"; import { interpolateRdYlGn } from "d3-scale-chromatic"; import type { I18n } from "anki/i18n"; @@ -21,7 +21,7 @@ export interface GraphData { export function gatherData(data: pb.BackendProto.GraphsOut): GraphData { const eases = (data.cards as pb.BackendProto.Card[]) - .filter((c) => c.queue == CardQueue.Review) + .filter((c) => [CardType.Review, CardType.Relearn].includes(c.ctype)) .map((c) => c.easeFactor / 10); return { eases }; } diff --git a/ts/graphs/intervals.ts b/ts/graphs/intervals.ts index 4b4fd3661..624701f60 100644 --- a/ts/graphs/intervals.ts +++ b/ts/graphs/intervals.ts @@ -67,33 +67,48 @@ export function prepareIntervalData( return [null, []]; } - const [_xMinOrig, origXMax] = extent(allIntervals); - let xMax = origXMax; + const xMin = 0; + let [, xMax] = extent(allIntervals); + let niceNecessary = false; // cap max to selected range switch (range) { case IntervalRange.Month: - xMax = Math.min(xMax!, 31); + xMax = Math.min(xMax!, 30); break; case IntervalRange.Percentile50: xMax = quantile(allIntervals, 0.5); + niceNecessary = true; break; case IntervalRange.Percentile95: xMax = quantile(allIntervals, 0.95); + niceNecessary = true; break; case IntervalRange.All: + niceNecessary = true; break; } - const xMin = 0; + xMax = xMax! + 1; + // do not show the zero interval + const increment = (x: number): number => x + 1; + + const adjustTicks = (x: number, idx: number, ticks: number[]): number[] => + idx === ticks.length - 1 ? [x - (ticks[0] - 1), x + 1] : [x - (ticks[0] - 1)]; + // cap bars to available range const desiredBars = Math.min(70, xMax! - xMin!); - const scale = scaleLinear().domain([xMin!, xMax!]).nice(); + const prescale = scaleLinear().domain([xMin!, xMax!]); + + const scale = scaleLinear().domain( + (niceNecessary ? prescale.nice() : prescale).domain().map(increment) + ); + const bins = histogram() - .domain(scale.domain() as any) - .thresholds(scale.ticks(desiredBars))(allIntervals); + .domain(scale.domain() as [number, number]) + .thresholds(scale.ticks(desiredBars).flatMap(adjustTicks))(allIntervals); // empty graph? const totalInPeriod = sum(bins, (bin) => bin.length); diff --git a/ts/package.json b/ts/package.json index d04226ca7..b31a47bc4 100644 --- a/ts/package.json +++ b/ts/package.json @@ -12,6 +12,7 @@ "@pyoner/svelte-types": "^3.4.4-2", "@rollup/plugin-commonjs": "^15.1.0", "@rollup/plugin-node-resolve": "^9.0.0", + "@sqltools/formatter": "^1.2.2", "@tsconfig/svelte": "^1.0.10", "@types/d3-array": "^2.0.0", "@types/d3-axis": "^1.0.12", @@ -22,15 +23,18 @@ "@types/d3-shape": "^1.3.2", "@types/d3-time": "^2.0.0", "@types/d3-transition": "^1.1.6", + "@types/diff": "^5.0.0", "@types/jquery": "^3.5.0", "@types/jqueryui": "^1.12.13", "@types/lodash": "^4.14.162", "@types/long": "^4.0.1", + "@types/node": "^14.14.20", "@types/react": "^16.9.53", "@types/react-dom": "^16.9.8", "@typescript-eslint/eslint-plugin": "^2.11.0", "@typescript-eslint/parser": "^2.11.0", "cross-env": "^7.0.2", + "diff": "^5.0.0", "eslint": "^6.7.2", "license-checker-rseidelsohn": "=1.1.2", "patch-package": "^6.2.2", diff --git a/ts/sql_format.bzl b/ts/sql_format.bzl new file mode 100644 index 000000000..812d101e8 --- /dev/null +++ b/ts/sql_format.bzl @@ -0,0 +1,24 @@ +load("@npm//@bazel/typescript:index.bzl", "ts_library") +load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test") + +def sql_format_setup(): + ts_library( + name = "sql_format_lib", + srcs = ["//ts:sql_format.ts"], + deps = [ + "@npm//@sqltools/formatter", + "@npm//@types/node", + "@npm//@types/diff", + "@npm//diff", + ], + visibility = ["//visibility:public"], + ) + +def sql_format(name = "sql_format", srcs = [], **kwargs): + nodejs_test( + name = name, + entry_point = "//ts:sql_format.ts", + args = [native.package_name() + "/" + f for f in srcs], + data = ["//ts:sql_format_lib", "@npm//tslib", "@npm//diff"] + srcs, + **kwargs + ) diff --git a/ts/sql_format.ts b/ts/sql_format.ts new file mode 100644 index 000000000..56722eeda --- /dev/null +++ b/ts/sql_format.ts @@ -0,0 +1,46 @@ +import sqlFormatter from "@sqltools/formatter"; +import * as Diff from "diff"; +import process from "process"; +import path from "path"; +import fs from "fs"; + +const workspace = process.env.BUILD_WORKSPACE_DIRECTORY; +const wantFix = workspace !== undefined; + +function fixFile(relpath: string, newText: string): void { + const workspacePath = path.join(workspace!, relpath); + fs.writeFileSync(workspacePath, newText); +} + +function formatText(text: string): string { + const newText: string = sqlFormatter.format(text, { + indent: " ", + reservedWordCase: "upper", + }); + // 'type' is treated as a reserved word, but Anki uses it in various sql + // tables, so we don't want it uppercased + return newText.replace(/\bTYPE\b/g, "type"); +} + +let errorFound = false; +for (const path of process.argv.slice(2)) { + const orig = fs.readFileSync(path).toString(); + const formatted = formatText(orig); + if (orig !== formatted) { + if (wantFix) { + fixFile(path, formatted); + console.log(`Fixed ${path}`); + } else { + if (!errorFound) { + errorFound = true; + console.log("SQL formatting issues found:"); + } + console.log(Diff.createPatch(path, orig, formatted)); + } + } +} +if (errorFound) { + console.log("Use 'bazel run //rslib:sql_format' to fix."); + console.log(process.env.BUILD_WORKSPACE_DIRECTORY); + process.exit(1); +} diff --git a/ts/tsconfig.json b/ts/tsconfig.json index e359b52a0..d25f28372 100644 --- a/ts/tsconfig.json +++ b/ts/tsconfig.json @@ -3,7 +3,7 @@ "compilerOptions": { "target": "es6", "module": "es6", - "lib": ["es2016", "dom"], + "lib": ["es2016", "es2019.array", "dom"], "baseUrl": ".", "paths": { "anki/*": ["../bazel-bin/ts/lib/*"] @@ -24,7 +24,7 @@ "allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */, "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, "jsx": "react", - "types": ["svelte", "long"], + "types": ["svelte", "long", "node"], "noEmitHelpers": true, "importHelpers": true } diff --git a/ts/yarn.lock b/ts/yarn.lock index 89b22551e..68f194725 100644 --- a/ts/yarn.lock +++ b/ts/yarn.lock @@ -145,6 +145,11 @@ estree-walker "^1.0.1" picomatch "^2.2.2" +"@sqltools/formatter@^1.2.2": + version "1.2.2" + resolved "https://registry.yarnpkg.com/@sqltools/formatter/-/formatter-1.2.2.tgz#9390a8127c0dcba61ebd7fdcc748655e191bdd68" + integrity sha512-/5O7Fq6Vnv8L6ucmPjaWbVG1XkP4FO+w5glqfkIsq3Xw4oyNAdJddbnYodNDAfjVUvo/rrSCTom4kAND7T1o5Q== + "@tsconfig/svelte@^1.0.10": version "1.0.10" resolved "https://registry.yarnpkg.com/@tsconfig/svelte/-/svelte-1.0.10.tgz#30ec7feeee0bdf38b12a50f0686f8a2e7b6b9dc0" @@ -220,6 +225,11 @@ dependencies: "@types/d3-selection" "^1" +"@types/diff@^5.0.0": + version "5.0.0" + resolved "https://registry.yarnpkg.com/@types/diff/-/diff-5.0.0.tgz#eb71e94feae62548282c4889308a3dfb57e36020" + integrity sha512-jrm2K65CokCCX4NmowtA+MfXyuprZC13jbRuwprs6/04z/EcFg/MCwYdsHn+zgV4CQBiATiI7AEq7y1sZCtWKA== + "@types/eslint-visitor-keys@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d" @@ -279,6 +289,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.38.tgz#66a7c068305dbd64cf167d0f6b6b6be71dd453e1" integrity sha512-oxo8j9doh7ab9NwDA9bCeFfjHRF/uzk+fTljCy8lMjZ3YzZGAXNDKhTE3Byso/oy32UTUQIXB3HCVHu3d2T3xg== +"@types/node@^14.14.20": + version "14.14.20" + resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.20.tgz#f7974863edd21d1f8a494a73e8e2b3658615c340" + integrity sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A== + "@types/prop-types@*": version "15.7.3" resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7" @@ -959,6 +974,11 @@ dezalgo@^1.0.0: asap "^2.0.0" wrappy "1" +diff@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/diff/-/diff-5.0.0.tgz#7ed6ad76d859d030787ec35855f5b1daf31d852b" + integrity sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w== + doctrine@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-3.0.0.tgz#addebead72a6574db783639dc87a121773973961"