1
- const driver = require ( 'postgres ' ) ;
1
+ const { Client } = require ( 'pg ' ) ;
2
2
const { SError, WError} = require ( 'error' ) ;
3
3
const SqlSe = ( ( ) => ( class Sql extends SError { } ) ) ( ) ;
4
4
const SqlWe = ( ( ) => ( class Sql extends WError { } ) ) ( ) ;
5
5
6
6
7
- const predefinedSql = require ( './sqls/index.js' ) ;
7
+ const predefinedSql = require ( './sqls/index.js' ) ( ) ;
8
8
9
9
const conn = ( ( ) => {
10
10
let connection ;
11
11
return async ( config ) => {
12
12
if ( connection === undefined ) {
13
- connection = await driver ( config ) ;
13
+ connection = new Client ( config ) ;
14
+ await connection . connect ( ) ;
14
15
return connection ;
15
16
}
16
17
return connection ;
17
18
} ;
18
19
} ) ( ) ;
19
20
20
21
21
- const Link = async ( config ) => {
22
- const sql = await conn ( config ) ;
23
- return {
24
- sql
25
- } ;
26
- } ;
27
-
28
-
29
22
module . exports = async ( config ) => {
30
23
const { link : { gluePrefix = '.' , schemas} = { } } = config ;
31
- const link = await Link ( config . connect ) ;
24
+ const link = await conn ( config . connect ) ;
32
25
33
26
const predefinedQuery = async ( key ) => {
34
27
if ( ! key ) {
35
28
throw SqlSe . create ( 'noSuchSqlHelperFile' ) ;
36
29
}
37
30
try {
38
- const q = predefinedSql [ key ] ;
39
- const r = await q ( link ) ;
31
+ const q = ( await predefinedSql ) [ key ] ;
32
+ const r = await link . query ( q ) ;
40
33
return r ;
41
34
} catch ( e ) {
42
35
throw SqlWe . wrap (
@@ -54,13 +47,17 @@ module.exports = async(config) => {
54
47
let cache ;
55
48
return async ( ) => {
56
49
if ( ! cache ) {
57
- cache = await predefinedQuery ( 'methods ' ) ;
50
+ cache = await predefinedQuery ( 'types ' ) ;
58
51
}
59
52
return cache ;
60
53
} ;
61
54
} ) ( ) ;
62
55
// https://www.postgresql.org/docs/current/catalog-pg-proc.html
63
- const buildArgs = ( argnames , argmodes ) => {
56
+ const buildArgs = ( argnames , argmodesText ) => {
57
+ const argmodes = argmodesText
58
+ . split ( '{' ) . join ( '' )
59
+ . split ( '}' ) . join ( '' )
60
+ . split ( ',' ) ;
64
61
return argnames
65
62
. reduce ( ( args , arg , idx ) => {
66
63
const mode = ( argmodes && argmodes [ idx ] ) || 'i' ;
@@ -76,7 +73,8 @@ module.exports = async(config) => {
76
73
77
74
const build = async ( ) => {
78
75
const allTypes = await types ( ) ;
79
- return ( await predefinedQuery ( 'methods' ) )
76
+ const methods = ( await predefinedQuery ( 'methods' ) ) ;
77
+ return methods . rows
80
78
. filter ( ( { schema} ) => schemas . indexOf ( schema ) > - 1 )
81
79
. reduce ( ( methods , {
82
80
schema,
@@ -90,8 +88,10 @@ module.exports = async(config) => {
90
88
argmodes // An array of the modes of the function arguments, encoded as i for IN arguments, o for OUT arguments, b for INOUT arguments, v for VARIADIC arguments, t for TABLE arguments. If all the arguments are IN arguments, this field will be null. Note that subscripts correspond to positions of proallargtypes not proargtypes.
91
89
} ) => {
92
90
const jsName = [ schema , name ] . join ( gluePrefix ) ;
93
- const sqlName = [ `${ schema } ` , `${ name } ` ] . join ( '.' ) ;
91
+ const sqlName = [ `" ${ schema } " ` , `" ${ name } " ` ] . join ( '.' ) ;
94
92
const args = buildArgs ( argnames , argmodes ) ;
93
+ const fillArgs = args . input
94
+ . map ( ( v , idx ) => `$${ idx + 1 } ` ) . join ( ',' ) ;
95
95
return {
96
96
...methods ,
97
97
[ jsName ] : async ( arguments ) => {
@@ -102,11 +102,9 @@ module.exports = async(config) => {
102
102
{ fn : jsName , argument : name }
103
103
) ;
104
104
}
105
- return `' ${ arguments [ name ] } '` ;
105
+ return arguments [ name ] ;
106
106
} ) ;
107
- // const res = await link.sql`select * from ${link.sql(sqlName)}('abc')`
108
- const res = await link . sql `select * from ${ link . sql ( sqlName ) } (${ link . sql ( arguments ) } )`
109
- return await res ;
107
+ return await link . query ( `SELECT * FROM ${ sqlName } (${ fillArgs } )` , dynArgs ) ;
110
108
}
111
109
} ;
112
110
} , { } ) ;
0 commit comments