-
Notifications
You must be signed in to change notification settings - Fork 33
Generate RPG data structure from result metadata #368
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2d2eb92
feat: Generate RPG data structure from result metadata
janfh b82b981
cleanup/formatting
janfh d2c4608
Include statement in generated source as a comment
janfh 79d5820
RPG qualifier only allowed for select statements
janfh 2515d80
Move RPG code generation to another function
janfh 79d98c0
Move statement execution and ui stuff outside of rpg codegen function
janfh c954d45
fix formatting
janfh c797ced
Refactoring
janfh 64ab6c1
Column to RPG definition test
janfh 51e4adc
Rename script "language:test" to "test"
janfh 824e62b
QueryResult to RPG data structure test
janfh 3d3976b
Added assertions to "QueryResult to RPG data structure" test
janfh de78996
Merge branch 'codefori:main' into feature/rpg-codegen
janfh 9029619
Merge remote-tracking branch 'origin/main' into feature/rpg-codegen
janfh 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { assert, expect, test } from 'vitest' | ||
import { columnToRpgDefinition, queryResultToRpgDs } from './codegen'; | ||
import { QueryResult } from '@ibm/mapepire-js'; | ||
|
||
test('Column to RPG definition', () => { | ||
let rpgdef; | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'NUMERIC', precision: 11, scale: 0}); | ||
expect(rpgdef).toBe('zoned(11)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'DECIMAL', precision: 13, scale: 2}); | ||
expect(rpgdef).toBe('packed(13 : 2)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'VARCHAR', precision: 60, scale: 0}); | ||
expect(rpgdef).toBe('varchar(60)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'CHAR', precision: 10, scale: 0}); | ||
expect(rpgdef).toBe('char(10)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'DATE', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('date'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'TIME', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('time'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'TIMESTAMP', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('timestamp'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'SMALLINT', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('int(5)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'INTEGER', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('int(10)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'BIGINT', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('int(20)'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'BOOLEAN', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('ind'); | ||
|
||
rpgdef = columnToRpgDefinition({display_size: 0, label: '', name: '', type: 'SOME_UNKNOWN_TYPE', precision: 0, scale: 0}); | ||
expect(rpgdef).toBe('// type:SOME_UNKNOWN_TYPE precision:0 scale:0'); | ||
}); | ||
|
||
test('QueryResult to RPG data structure', () => { | ||
const queryResult: QueryResult<any> = { | ||
metadata: { | ||
column_count: 3, | ||
columns: [ | ||
{ | ||
display_size: 0, | ||
label: 'id', | ||
name: 'id', | ||
type: 'INTEGER', | ||
precision: 0, | ||
scale: 0 | ||
}, | ||
{ | ||
display_size: 0, | ||
label: 'name', | ||
name: 'name', | ||
type: 'VARCHAR', | ||
precision: 80, | ||
scale: 0 | ||
}, | ||
{ | ||
display_size: 0, | ||
label: 'salary', | ||
name: 'salary', | ||
type: 'DECIMAL', | ||
precision: 13, | ||
scale: 2 | ||
}, | ||
] | ||
}, | ||
is_done: true, | ||
has_results: true, | ||
update_count: 0, | ||
data: [], | ||
id: '', | ||
success: true, | ||
sql_rc: 0, | ||
sql_state: '', | ||
execution_time: 0 | ||
}; | ||
const ds = queryResultToRpgDs(queryResult); | ||
const lines = ds.split('\n').filter(l => l !== ''); | ||
expect(lines.length).toBe(5); | ||
expect(lines.at(0)).toBe('dcl-ds row_t qualified template;'); | ||
expect(lines.at(1).trim()).toBe('id int(10);'); | ||
expect(lines.at(2).trim()).toBe('name varchar(80);'); | ||
expect(lines.at(3).trim()).toBe('salary packed(13 : 2);'); | ||
expect(lines.at(4)).toBe('end-ds;'); | ||
}); | ||
|
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,40 @@ | ||
import { ColumnMetaData, QueryResult } from "@ibm/mapepire-js"; | ||
|
||
export function queryResultToRpgDs(result: QueryResult<any>) : string { | ||
let content = `dcl-ds row_t qualified template;\n`; | ||
for (let i = 0; i < result.metadata.column_count; i++) { | ||
const name = `${isNaN(+result.metadata.columns[i].label.charAt(0)) ? '' : 'col'}${result.metadata.columns[i].label.toLowerCase()}` | ||
content += ` ${name} ${columnToRpgDefinition(result.metadata.columns[i])};\n`; | ||
} | ||
content += `end-ds;\n`; | ||
return content; | ||
} | ||
|
||
export function columnToRpgDefinition(column: ColumnMetaData) : string { | ||
switch (column.type) { | ||
case `NUMERIC`: | ||
return `zoned(${column.precision}${column.scale > 0 ? ' : ' + column.scale : ''})`; | ||
case `DECIMAL`: | ||
return `packed(${column.precision}${column.scale > 0 ? ' : ' + column.scale : ''})`; | ||
case `CHAR`: | ||
return `char(${column.precision})`; | ||
case `VARCHAR`: | ||
return `varchar(${column.precision})`; | ||
case `DATE`: | ||
return `date`; | ||
case `TIME`: | ||
return `time`; | ||
case `TIMESTAMP`: | ||
return `timestamp`; | ||
case `SMALLINT`: | ||
return `int(5)`; | ||
case `INTEGER`: | ||
return `int(10)`; | ||
case `BIGINT`: | ||
return `int(20)`; | ||
case `BOOLEAN`: | ||
return `ind`; | ||
default: | ||
return `// type:${column.type} precision:${column.precision} scale:${column.scale}`; | ||
} | ||
} |
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
Oops, something went wrong.
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.