-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Elin Angelow
committed
Dec 18, 2023
1 parent
723aedb
commit 8b522df
Showing
10 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
.nyc_output/**/* | ||
coverage/**/* | ||
tests/unit/runtime/ledger/**/* | ||
examples/mssql.config.js | ||
examples/**/config.js |
This file contains 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 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 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,12 @@ | ||
module.exports = { | ||
connect: { | ||
server: 'localhost', | ||
user: 'sa', | ||
password: '***', | ||
database: 'some-test-example' | ||
}, | ||
link: { | ||
gluePrefix: '.', | ||
schema: ['a', 'b'] // schemas allowed | ||
} | ||
}; |
This file contains 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,32 @@ | ||
CREATE TYPE LessonType AS TABLE (LessonId INT, LessonName VARCHAR(100)) | ||
|
||
CREATE TABLE Lesson ( | ||
Id INT PRIMARY KEY, | ||
LName VARCHAR(50) | ||
) | ||
------------------------- | ||
CREATE PROCEDURE Usp_InsertLesson | ||
@ParLessonType LessonType READONLY, | ||
@ParLessonType2 LessonType READONLY | ||
AS | ||
INSERT INTO Lesson | ||
SELECT * FROM @ParLessonType; | ||
|
||
CREATE PROCEDURE Usp_InsertLesson2 | ||
@ParLessonType LessonType READONLY, | ||
@ParLessonType2 LessonType READONLY | ||
AS | ||
INSERT INTO Lesson | ||
SELECT * FROM @ParLessonType | ||
------------------------- | ||
DECLARE @VarLessonType AS LessonType | ||
|
||
INSERT INTO @VarLessonType VALUES ( 1, 'Math') | ||
INSERT INTO @VarLessonType VALUES ( 2, 'Science') | ||
INSERT INTO @VarLessonType VALUES ( 3, 'Geometry') | ||
|
||
|
||
EXECUTE Usp_InsertLesson @VarLessonType | ||
------------------------- | ||
------------------------- | ||
------------------------- |
This file contains 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,10 @@ | ||
(async() => { | ||
const methods = await require('../../lib/mssql/index.js')( | ||
require('../mssql.config.js') | ||
); | ||
try { | ||
await methods['dbo.Usp_InsertLesson']({ParLessonType: [{LessonId: 100, LessonName: 'example lesson'}]}); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
})(); |
This file contains 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,68 @@ | ||
const driver = require('postgresql'); | ||
const {SError, WError} = require('error'); | ||
const SqlSe = (() => (class Sql extends SError {}))(); | ||
const SqlWe = (() => (class Sql extends WError {}))(); | ||
|
||
|
||
const predefinedSql = require('./sqls/index.js'); | ||
|
||
const conn = (() => { | ||
let connection = null; | ||
return async(config) => { | ||
if (connection === null) { | ||
connection = await driver.connect(config); | ||
return connection; | ||
} | ||
return connection; | ||
}; | ||
})(); | ||
|
||
|
||
const Link = async(config) => { | ||
const { | ||
server, | ||
user, | ||
password, | ||
database, | ||
pool, | ||
options | ||
} = config; | ||
const cPool = await conn({ | ||
server, | ||
user, | ||
password, | ||
pool, | ||
database, | ||
options | ||
}); | ||
return { | ||
query: async(q) => { | ||
try { | ||
return await cPool.request().query(q); | ||
} catch (e) { | ||
console.warn(q); | ||
console.error(e); | ||
throw e; | ||
} | ||
}, | ||
request: () => cPool.request() | ||
}; | ||
}; | ||
|
||
|
||
module.exports = async(config) => { | ||
const {gluePrefix = '.'} = config; | ||
const link = await Link(config.connect); | ||
|
||
const predefinedQuery = async(key) => { | ||
if (!key) { | ||
throw SqlSe.create('noSuchSqlHelperFile'); | ||
} | ||
const q = (await predefinedSql[key]).toString('utf8'); | ||
return link.query( | ||
q | ||
); | ||
} | ||
|
||
return {}; | ||
}; |
This file contains 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,5 @@ | ||
const read = require('util').promisify(require('fs').readFile); | ||
module.exports = { | ||
internalMethod: read([__dirname, 'internalMethod.sql'].join('/')), | ||
tableTypes: read([__dirname, 'tableTypes.sql'].join('/')) | ||
}; |
This file contains 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,20 @@ | ||
SELECT | ||
OBJECT_SCHEMA_NAME([SYSPROC].object_id) [schema], | ||
[SYSPROC].name name, | ||
[SYSPROC].object_id objectId, | ||
[SYSPARAMS].name AS [column], | ||
TYPE_NAME([SYSPARAMS].user_type_id) AS [type], | ||
[SYSPARAMS].max_length AS length, | ||
CASE | ||
WHEN TYPE_NAME([SYSPARAMS].system_type_id) = 'uniqueidentifier' | ||
THEN [SYSPARAMS].PRECISION | ||
ELSE OdbcPrec([SYSPARAMS].system_type_id, [SYSPARAMS].max_length, [SYSPARAMS].PRECISION) | ||
END AS [precision], | ||
OdbcScale([SYSPARAMS].system_type_id, [SYSPARAMS].scale) AS [scale], | ||
[SYSPARAMS].parameter_id AS [order], | ||
[SYSPARAMS].user_type_id AS userTypeId, | ||
[SYSPARAMS].is_output AS isOutput | ||
FROM | ||
sys.procedures [SYSPROC] | ||
LEFT JOIN | ||
sys.parameters [SYSPARAMS] ON [SYSPARAMS].object_id = [SYSPROC].object_id |
This file contains 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,27 @@ | ||
SELECT | ||
SCHEMA_NAME(types.schema_id) + '.' + types.name name, | ||
c.name [column], | ||
st.name type, | ||
CASE | ||
WHEN st.name IN ('decimal', 'numeric') THEN CAST(c.[precision] AS VARCHAR) | ||
WHEN st.name IN ('datetime2', 'time', 'datetimeoffset') THEN CAST(c.[scale] AS VARCHAR) | ||
WHEN st.name IN ('varchar', 'varbinary', 'char', 'binary') AND c.max_length >= 0 THEN CAST(c.max_length AS VARCHAR) | ||
WHEN st.name IN ('nvarchar', 'nchar') AND c.max_length >= 0 THEN CAST(c.max_length / 2 AS VARCHAR) | ||
WHEN st.name IN ('varchar', 'varbinary', 'char', 'binary', 'nvarchar', 'nchar') AND c.max_length < 0 THEN 'max' | ||
END [length], | ||
CASE | ||
WHEN st.name IN ('decimal', 'numeric') THEN c.scale | ||
END scale, | ||
OBJECT_DEFINITION(c.default_object_id) [default], | ||
types.user_type_id AS userTypeId, | ||
c.is_nullable AS isNullable | ||
FROM | ||
sys.table_types types | ||
JOIN | ||
sys.columns c ON types.type_table_object_id = c.object_id | ||
JOIN | ||
sys.systypes AS st ON st.xtype = c.system_type_id | ||
WHERE | ||
types.is_user_defined = 1 AND st.name <> 'sysname' | ||
ORDER BY | ||
1, c.column_id |