-
Notifications
You must be signed in to change notification settings - Fork 2
Add synonym-aware name resolution to SqlFormatter with SQL-style SqlSynonym semantics and formatter-level synonym overrides
#156
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
Open
Copilot
wants to merge
14
commits into
master
Choose a base branch
from
copilot/implement-synonym-parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1ffb2e8
Initial plan
Copilot 13524ec
feat: add SqlFormatter synonym resolution hooks
Copilot 6e84bfb
refactor: adjust resolvingName event payload fields
Copilot da5be42
refactor: simplify resolving event and map-based SqlSynonym service
Copilot 0d640d0
style: address follow-up review in SqlSynonym
Copilot 3179dfb
fix: harden SqlSynonym singleton and d.ts add signatures
Copilot c1ccfa8
perf: cache sorted synonym keys and tighten singleton/type signatures
Copilot e6e67a1
refactor: align SqlSynonym map semantics and resolve validation
Copilot cbfc2dc
refactor: remove redundant SqlSynonym static proxy methods
Copilot 9f308a3
refactor: simplify SqlSynonym map behavior
Copilot 915f1e7
refactor: reverse SqlSynonym mapping to SQL synonym direction
Copilot 1f63850
chore: align SqlSynonym naming and test readability
Copilot 176ef4e
feat: support formatter-level synonym instance resolution
Copilot ecf410f
refactor: deduplicate formatter name resolution logic
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import {OpenDataQueryFormatter, QueryExpression, SqlFormatter, SqlSynonym} from '../src/index'; | ||
|
|
||
| describe('SqlSynonym', () => { | ||
| const synonyms = SqlSynonym.getInstance(); | ||
|
|
||
| beforeEach(() => { | ||
| synonyms.clear(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| synonyms.clear(); | ||
| }); | ||
|
|
||
| it('should format query by using SQL synonym', () => { | ||
| synonyms.set('Products', 'sales.Products'); | ||
| const formatter = new SqlFormatter(); | ||
| formatter.settings.nameFormat = '`$1`'; | ||
| const query = new QueryExpression().select('id', 'name') | ||
| .from('Products').where('id').equal(100); | ||
| const sql = formatter.formatSelect(query); | ||
| const expectedSql = 'SELECT `sales`.`Products`.`id`, `sales`.`Products`.`name` FROM `sales`.`Products` WHERE (`id`=100)'; | ||
| expect(sql).toBe(expectedSql); | ||
| }); | ||
|
|
||
| it('should format qualified object names by using synonym', () => { | ||
| synonyms.set('Products', 'Production.Product'); | ||
| const formatter = new SqlFormatter(); | ||
| formatter.settings.nameFormat = '`$1`'; | ||
| expect(formatter.escapeEntity('Products')).toBe('`Production`.`Product`'); | ||
| expect(formatter.escapeName('Products.ProductID')).toBe('`Production`.`Product`.`ProductID`'); | ||
| }); | ||
|
|
||
| it('should apply synonyms in formatter subclasses', () => { | ||
| synonyms.set('Products', 'sales.Products'); | ||
| const formatter = new OpenDataQueryFormatter(); | ||
| expect(formatter.escapeName('Products.id')).toBe('sales/Products/id'); | ||
| }); | ||
|
|
||
| it('should prioritize formatter instance synonyms over static resolving handlers', () => { | ||
| synonyms.set('Products', 'sales.Products'); | ||
| const formatter = new SqlFormatter(); | ||
| formatter.settings.nameFormat = '`$1`'; | ||
| formatter.synonyms = new SqlSynonym([ | ||
| ['Products', 'inventory.Products'] | ||
| ]); | ||
| expect(formatter.escapeEntity('Products')).toBe('`inventory`.`Products`'); | ||
| }); | ||
|
|
||
| it('should construct synonyms from array entries', () => { | ||
| const localSynonyms = new SqlSynonym([ | ||
| ['synonym1', 'schema1.object1'], | ||
| ['synonym2', 'schema2.object2'] | ||
| ]); | ||
| expect(localSynonyms.get('synonym1')).toBe('schema1.object1'); | ||
| expect(localSynonyms.get('synonym2')).toBe('schema2.object2'); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export declare class SqlSynonym extends Map<string, string> { | ||
| constructor(); | ||
| constructor(entries?: readonly (readonly [string, string])[] | null); | ||
| resolve(name: string): string; | ||
| static getInstance(): SqlSynonym; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import {SqlFormatter} from './formatter'; | ||
|
|
||
| class SqlSynonym extends Map { | ||
| constructor(entries) { | ||
| super(entries); | ||
| } | ||
|
|
||
| set(synonym, name) { | ||
| if (typeof synonym !== 'string') { | ||
| throw new TypeError('Invalid synonym name. Expected string.'); | ||
| } | ||
| if (typeof name !== 'string') { | ||
| throw new TypeError('Invalid target name. Expected string.'); | ||
| } | ||
| return super.set(synonym, name); | ||
| } | ||
|
|
||
| get(synonym) { | ||
| return super.get(synonym); | ||
| } | ||
|
|
||
| delete(synonym) { | ||
| if (typeof synonym !== 'string') { | ||
| throw new TypeError('Invalid synonym name. Expected string.'); | ||
| } | ||
| return super.delete(synonym); | ||
| } | ||
|
|
||
| clear() { | ||
| return super.clear(); | ||
| } | ||
|
|
||
| resolve(name) { | ||
| if (typeof name !== 'string') { | ||
| throw new TypeError('Invalid synonym expression. Expected string.'); | ||
| } | ||
| const exactMatch = this.get(name); | ||
| if (typeof exactMatch === 'string') { | ||
| return exactMatch; | ||
| } | ||
| let candidate; | ||
| for (const key of this.keys()) { | ||
| if (name.startsWith(key + '.')) { | ||
| if (candidate === undefined || key.length > candidate.length) { | ||
| candidate = key; | ||
| } | ||
| } | ||
| } | ||
| if (typeof candidate === 'string') { | ||
| return this.get(candidate).concat(name.substring(candidate.length)); | ||
| } | ||
| return name; | ||
| } | ||
|
|
||
| static getInstance() { | ||
| if (this.instance === undefined) { | ||
| this.instance = new SqlSynonym(); | ||
| } | ||
| return this.instance; | ||
| } | ||
| } | ||
|
|
||
| SqlFormatter.resolvingName.subscribe((event) => { | ||
| if (typeof event.name !== 'string') { | ||
| return; | ||
| } | ||
| event.name = SqlSynonym.getInstance().resolve(event.name); | ||
| }); | ||
|
|
||
| export { | ||
| SqlSynonym | ||
| }; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SqlFormatter should have an option to use their own
SqlSynonyminstance. This instance, if exists, might be used before using the staticSqlFormatter.resolvingNameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in 176ef4e (refined in ecf410f):
SqlFormatternow supports a per-instancesynonymsresolver, and it is applied before staticresolvingNamehandlers. Added coverage to verify instance-level synonyms take precedence. No UI changes (screenshot N/A).