-
Notifications
You must be signed in to change notification settings - Fork 653
add support for SQLite Full Text Search #984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e83d290
Adding `isSearchable` to column schema
stigi 869900b
:sparkles: add support for Full Text Search match operator and implem…
Kenneth-KT be6549f
:bug: fix incorrectly quoted SQL NEW and OLD trigger references in Sq…
Kenneth-KT be8113d
:art: fix flow & lint issues in d3fc571
Kenneth-KT 3d6353d
:pencil: keep a changelog
Kenneth-KT 06d4166
Update src/adapters/sqlite/encodeSchema/index.js
radex c8d7ff3
FTS: Rebase + fix tests
sidferreira ae7c823
FTS improvements (uses `isFTS` and the tables are `_fts_${tableName}`)
sidferreira 140d4f4
FTS improvements (uses `isFTS` and the tables are `_fts_${tableName}`…
sidferreira 1371b4e
FTS improvements
sidferreira aa3d9b1
Update src/adapters/sqlite/encodeSchema/index.js
sidferreira 183ac16
FTS: improves tests and documentation
sidferreira 806f0d2
Merge branch 'master' into fts_new
sidferreira b6fca23
FTS: lint and tests
sidferreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import type { | |
| AddColumnsMigrationStep, | ||
| } from '../../../Schema/migrations' | ||
| import type { SQL } from '../index' | ||
| import { logger } from '../../../utils/common' | ||
|
|
||
| import encodeName from '../encodeName' | ||
| import encodeValue from '../encodeValue' | ||
|
|
@@ -38,8 +39,112 @@ const encodeTableIndicies: (TableSchema) => SQL = ({ name: tableName, columns }) | |
| const transform = (sql: string, transformer: ?(string) => string) => | ||
| transformer ? transformer(sql) : sql | ||
|
|
||
| const encodeTable: (TableSchema) => SQL = (table) => | ||
| transform(encodeCreateTable(table) + encodeTableIndicies(table), table.unsafeSql) | ||
| const encodeTable: TableSchema => SQL = table => | ||
| transform( | ||
| // eslint-disable-next-line no-use-before-define | ||
| encodeCreateTable(table) + encodeTableIndicies(table) + encodeFTSSearch(table), | ||
| table.unsafeSql, | ||
| ) | ||
|
|
||
| /** FTS Full Text Search */ | ||
|
|
||
| const encodeFTSTrigger: ({ | ||
| tableName: string, | ||
| ftsTableName: string, | ||
| event: 'delete' | 'insert' | 'update', | ||
| action: SQL, | ||
| }) => SQL = ({ tableName, ftsTableName, event, action }) => { | ||
| const triggerName = `${ftsTableName}_${event}` | ||
| return `create trigger ${encodeName(triggerName)} after ${event} on ${encodeName( | ||
| tableName, | ||
| )} begin ${action} end;` | ||
| } | ||
|
|
||
| const encodeFTSDeleteTrigger: ({ | ||
| tableName: string, | ||
| ftsTableName: string, | ||
| }) => SQL = ({ tableName, ftsTableName }) => | ||
| encodeFTSTrigger({ | ||
| tableName, | ||
| ftsTableName, | ||
| event: 'delete', | ||
| action: `delete from ${encodeName(ftsTableName)} where "rowid" = OLD.rowid;`, | ||
| }) | ||
|
|
||
| const encodeFTSInsertTrigger: ({ | ||
| tableName: string, | ||
| ftsTableName: string, | ||
| ftsColumns: ColumnSchema[], | ||
| }) => SQL = ({ tableName, ftsTableName, ftsColumns }) => { | ||
| const rawColumnNames = ['rowid', ...ftsColumns.map(column => column.name)] | ||
| const columns = rawColumnNames.map(encodeName) | ||
| const valueColumns = rawColumnNames.map(column => `NEW.${encodeName(column)}`) | ||
|
|
||
| const columnsSQL = columns.join(', ') | ||
| const valueColumnsSQL = valueColumns.join(', ') | ||
|
|
||
| return encodeFTSTrigger({ | ||
| tableName, | ||
| ftsTableName, | ||
| event: 'insert', | ||
| action: `insert into ${encodeName(ftsTableName)} (${columnsSQL}) values (${valueColumnsSQL});`, | ||
| }) | ||
| } | ||
|
|
||
| const encodeFTSUpdateTrigger: ({ | ||
| tableName: string, | ||
| ftsTableName: string, | ||
| ftsColumns: ColumnSchema[], | ||
| }) => SQL = ({ tableName, ftsTableName, ftsColumns }) => { | ||
| const rawColumnNames = ftsColumns.map(column => column.name) | ||
| const assignments = rawColumnNames.map( | ||
| column => `${encodeName(column)} = NEW.${encodeName(column)}`, | ||
| ) | ||
|
|
||
| const assignmentsSQL = assignments.join(', ') | ||
|
|
||
| return encodeFTSTrigger({ | ||
| tableName, | ||
| ftsTableName, | ||
| event: 'update', | ||
| action: `update ${encodeName(ftsTableName)} set ${assignmentsSQL} where "rowid" = NEW."rowid";`, | ||
| }) | ||
| } | ||
|
|
||
| const encodeFTSTriggers: ({ | ||
| tableName: string, | ||
| ftsTableName: string, | ||
| ftsColumns: ColumnSchema[], | ||
| }) => SQL = ({ tableName, ftsTableName, ftsColumns }) => { | ||
| return ( | ||
| encodeFTSDeleteTrigger({ tableName, ftsTableName }) + | ||
| encodeFTSInsertTrigger({ tableName, ftsTableName, ftsColumns }) + | ||
| encodeFTSUpdateTrigger({ tableName, ftsTableName, ftsColumns }) | ||
| ) | ||
| } | ||
|
|
||
| const encodeFTSTable: ({ | ||
| ftsTableName: string, | ||
| ftsColumns: ColumnSchema[], | ||
| }) => SQL = ({ ftsTableName, ftsColumns }) => { | ||
| const columnsSQL = ftsColumns.map(column => encodeName(column.name)).join(', ') | ||
| return `create virtual table ${encodeName(ftsTableName)} using fts4(${columnsSQL});` | ||
| } | ||
|
|
||
| const encodeFTSSearch: TableSchema => SQL = tableSchema => { | ||
| const { name: tableName, columnArray } = tableSchema | ||
| const ftsColumns = columnArray.filter(column => column.isFTS) | ||
| if (ftsColumns.length === 0) { | ||
| return '' | ||
| } | ||
| const ftsTableName = `_fts_${tableName}` | ||
| return ( | ||
| encodeFTSTable({ ftsTableName, ftsColumns }) + | ||
| encodeFTSTriggers({ tableName, ftsTableName, ftsColumns }) | ||
| ) | ||
| } | ||
|
|
||
| /** FTS END */ | ||
|
|
||
| export const encodeSchema: (AppSchema) => SQL = ({ tables, unsafeSql }) => { | ||
| const sql = Object.values(tables) | ||
|
|
@@ -65,6 +170,10 @@ const encodeAddColumnsMigrationStep: (AddColumnsMigrationStep) => SQL = ({ | |
| )} = ${encodeValue(nullValue(column))};` | ||
| const addIndex = encodeIndex(column, table) | ||
|
|
||
| if (column.isFTS) { | ||
| logger.warn('[DB][Worker] Support for migrations and isFTS is still to be implemented') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oof, that's pretty bad
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. err... #623 (comment) |
||
| } | ||
|
sidferreira marked this conversation as resolved.
Outdated
|
||
|
|
||
| return transform(addColumn + setDefaultValue + addIndex, unsafeSql) | ||
| }) | ||
| .join('') | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.