Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function(me) {
const lefts = await QUERY `
SELECT ${add_column(me)}
FROM ${TB(me.table)} AS ${TB(me.as)} ${me.query}`;
return go(
[lefts, me],
function recur([lefts, option]) {
return lefts.length && option.rels.length && go(option.rels, C.map(async function(me) {
const query = me.query();
if (query && query.text) query.text = query.text.replace(/^\s*WHERE/i, 'AND');
var fold_key = me.rel_type == 'x' ?
`_#_${me.where_key.split('.')[1]}_#_` : me.where_key;
const colums = uniq(add_column(me).originals.concat(
me.rel_type != 'x' ? me.as + '.' + me.where_key : me.where_key + ' AS ' + fold_key));
const in_vals = filter(a => a != null, pluck(me.left_key, lefts));
const is_row_num = me.row_number.length == 2;
const rights = (!in_vals.length ? [] : await (is_row_num ?
QUERY `
export default async function load_ljoin({
ready_sqls, add_column, tag, FxSQL_DEBUG,
connection_info, QUERY, VALUES, IN, NOT_IN, EQ, SET, COLUMN, CL, TABLE, TB, SQL, SQLS
}) {
const cmap = curry((f, arr) => Promise.all(arr.map(f)));
const table_columns = {};
const add_as_join = (me, as) =>
COLUMN(...go(
me.column.originals.concat(pluck('left_key', me.left_joins)),
map(c => me.as + '.' + c + ' AS ' + `${as}>_<${c}`),
uniq
));
Object.assign(table_columns, await go(
QUERY `
SELECT table_name, column_name
FROM information_schema.columns
WHERE
table_name in (
SELECT tablename
FROM pg_tables
WHERE
tableowner=${connection_info.user || process.env.PGUSER}
) ORDER BY table_name;`,
group_by((v) => v.table_name),
map(v => pluck('column_name', v))),
await go(QUERY `
SELECT *
FROM INFORMATION_SCHEMA.view_column_usage
WHERE view_catalog=${connection_info.database || process.env.PGDATABASE}
cmap(async function(me) {
const f_key_ids = uniq(filter((r) => !!r, pluck(me.left_key, results)));
if (me.rel_type == '-' || !f_key_ids.length) return recur([me, cat(map(r => r._ ? r._[me.as] : null, results))]);
return go(
(!me.left_join_over && me.left_joins.length ?
left_join_query : where_in_query)(me, SQL `WHERE ${IN(me.as + '.' + me.key, f_key_ids)}`, QUERY),
group_by((v) => v[me.key]),
function(groups) {
each(function(result) {
result._ = result._ || {};
result._[me.as] = (groups[result[me.left_key]] || []);
}, results);
return recur([me, cat(map(r => r._ ? r._[me.as] : null, results))]);
}
);
}),
() => results
const add_column = me =>
me.column == '*' ?
COLUMN(me.as + '.*') :
is_column(me.column) ?
COLUMN(...go(
me.column.originals.concat(pluck('left_key', me.rels)),
map(c => is_string(c) ? me.as + '.' + c : c),
uniq)) :
tag(SymbolInjection);
${COLUMN(...colums)},
ROW_NUMBER() OVER (PARTITION BY ${CL(me.where_key)} ORDER BY ${me.row_number[1]}) as "--row_number--"
FROM ${TB(me.table)} AS ${TB(me.as)}
${me.xjoin}
WHERE ${IN(me.as +'.'+me.where_key, in_vals)} ${me.poly_type} ${tag(query)}
) AS "--row_number_table--"
WHERE "--row_number_table--"."--row_number--"<=${me.row_number[0]}`
:
QUERY `
SELECT ${COLUMN(...colums)}
FROM ${TB(me.table)} AS ${TB(me.as)}
${me.xjoin}
WHERE ${IN(me.where_key, in_vals)} ${me.poly_type} ${tag(query)}`));
const [folder, default_value] = me.rel_type == '-' ? [uniq_index_by, () => ({})] : [group_by, () => []];
return go(
rights,
is_row_num ? map(r => delete r['--row_number--'] && r) : r => r,
folder(a => a[fold_key]),
folded => each(function(left) {
left._ = left._ || {};
left._[me.as] = folded[left[me.left_key]] || default_value();
}, lefts),
_ => recur([rights, me]),
_ => me.hook && each(left => go(me.hook(left._[me.as]), right => left._[me.as] = right), lefts));
}));
},
const add_as_join = (me, as) =>
COLUMN(...go(
me.column.originals.concat(pluck('left_key', me.left_joins)),
map(c => me.as + '.' + c + ' AS ' + `${as}>_<${c}`),
uniq
));
var prefix = as.substr(0, 2);
if (['- ', '< ', 'x '].includes(prefix)) {
rel_type = prefix.trim();
as = as.substr(1).trim();
return { depth, as, rel_type }
} else if (prefix == 'p ') {
rel_type = as[2];
as = as.substr(3).trim();
return { depth, as, rel_type, is_poly: true }
} else {
return { depth, as };
}
})
);
go(
tails2,
map(tail =>
is_tag(tail) ?
{ query: tail } :
Object.assign({}, tail, { query: tail.query || tag() })
),
Object.entries,
each(([i, t]) => go(
options[i],
last,
_ => Object.assign(_, t)
))
);
return options;
}
function left_join_query(left, where_in, QUERY) {
let i = 0;
left.lj_as = 'lj'+ i++ + "//"+left.depth;
const first_col = add_as_join(left, left.lj_as).originals.concat(left.as + '.id' + ' AS ' + `${left.lj_as}>__<${left.key}`);
const join_columns = [first_col];
const join_sqls = [];
return go(
left,
function recur(me) {
me.left_join_over = true;
each(right => {
const query = right.query();
right.lj_as = 'lj'+ i++ + "//"+right.depth;
if (query && query.text) query.text = query.text.replace(/^\s*WHERE/i, 'AND');
join_columns.push(
uniq(add_as_join(right, right.lj_as).originals
.concat(right.as + '.' + right.key + ' AS ' + `${right.lj_as}>_<${right.key}`)
.concat(right.as + '.id' + ' AS ' + `${right.lj_as}>_