Fix bug + add quote methods
parent
b4dd25148f
commit
50744ac6f8
|
@ -1,6 +1,6 @@
|
|||
// Простенький "селект билдер" по мотивам MediaWiki-овского, успешно юзаю подобный в PHP уже лет 8
|
||||
// (c) Виталий Филиппов, 2019
|
||||
// Версия 2019-05-09
|
||||
// Версия 2019-05-14
|
||||
|
||||
// В PHP, правда, прикольнее - там в массиве можно смешивать строковые и численные ключи,
|
||||
// благодаря чему можно писать $where = [ 't1.a=t2.a', 't2.b' => [ 1, 2, 3 ] ]
|
||||
|
@ -73,9 +73,9 @@ function select_builder(tables, fields, where, options)
|
|||
}
|
||||
if (options.OFFSET || options.offset)
|
||||
{
|
||||
sql += ' LIMIT '+((options.OFFSET || options.offset) | 0);
|
||||
sql += ' OFFSET '+((options.OFFSET || options.offset) | 0);
|
||||
}
|
||||
return { sql, bind };
|
||||
return new Text(sql, bind);
|
||||
}
|
||||
|
||||
function tables_builder(tables)
|
||||
|
@ -302,7 +302,8 @@ function split_using(tables)
|
|||
function _positional(sql)
|
||||
{
|
||||
let i = 0;
|
||||
sql = sql.replace(/\?/g, () => '$'+(++i));
|
||||
sql = sql.replace(/'(?:[^\']*|\'\')*'|"(?:[^\"]*|\"\")*"|(\?)/g, (m, m1) => (m1 ? '$'+(++i) : m));
|
||||
console.log('> '+sql);
|
||||
return sql;
|
||||
}
|
||||
|
||||
|
@ -313,7 +314,7 @@ function _inline(sql, bind)
|
|||
pg_escape = require('pg-escape');
|
||||
}
|
||||
let i = 0;
|
||||
sql = sql.replace(/\?/g, () => '\''+pg_escape.string(bind[i++])+'\'');
|
||||
sql = sql.replace(/'(?:[^\']*|\'\')*'|"(?:[^\"]*|\"\")*"|(\?)/g, (m, m1) => (m1 ? '\''+pg_escape.string(bind[i++])+'\'' : m));
|
||||
return sql;
|
||||
}
|
||||
|
||||
|
@ -321,7 +322,6 @@ function _inline(sql, bind)
|
|||
async function select(dbh, tables, fields, where, options, format)
|
||||
{
|
||||
let { sql, bind } = select_builder(tables, fields, where, options);
|
||||
//console.log(_inline(sql, bind));
|
||||
let data = await dbh.query(_positional(sql), bind);
|
||||
if ((format & MS_LIST) || (format & MS_COL))
|
||||
data = data.rows.map(r => Object.values(r));
|
||||
|
@ -422,14 +422,36 @@ class Pg_Values
|
|||
}
|
||||
}
|
||||
|
||||
class Text
|
||||
{
|
||||
constructor(sql, bind)
|
||||
{
|
||||
this.sql = sql;
|
||||
this.bind = bind || [];
|
||||
}
|
||||
|
||||
toString()
|
||||
{
|
||||
return _inline(this.sql, this.bind);
|
||||
}
|
||||
|
||||
concat(text)
|
||||
{
|
||||
return new Text(this.sql+text.sql, [ ...this.bind, ...text.bind ]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
select_builder,
|
||||
where_builder,
|
||||
quote_into: _inline,
|
||||
quote_positional: _positional,
|
||||
select,
|
||||
insert,
|
||||
delete: _delete,
|
||||
update,
|
||||
values,
|
||||
Text,
|
||||
MS_HASH,
|
||||
MS_LIST,
|
||||
MS_ROW,
|
||||
|
|
Loading…
Reference in New Issue