Remove push.apply() as it crashes on very long lists

master
Vitaliy Filippov 2020-06-12 15:31:08 +03:00
parent 880fb10561
commit 809ddfad04
1 changed files with 12 additions and 12 deletions

View File

@ -1,6 +1,6 @@
// Простенький "селект билдер" по мотивам MediaWiki-овского, успешно юзаю подобный в PHP уже лет 8 // Простенький "селект билдер" по мотивам MediaWiki-овского, успешно юзаю подобный в PHP уже лет 8
// (c) Виталий Филиппов, 2019-2020 // (c) Виталий Филиппов, 2019-2020
// Версия 2020-03-25 // Версия 2020-06-12
// В PHP, правда, прикольнее - там в массиве можно смешивать строковые и численные ключи, // В PHP, правда, прикольнее - там в массиве можно смешивать строковые и численные ключи,
// благодаря чему можно писать $where = [ 't1.a=t2.a', 't2.b' => [ 1, 2, 3 ] ] // благодаря чему можно писать $where = [ 't1.a=t2.a', 't2.b' => [ 1, 2, 3 ] ]
@ -64,14 +64,14 @@ function select_builder(tables, fields, where, options)
sql += ' FROM '; sql += ' FROM ';
const t = tables_builder(tables); const t = tables_builder(tables);
sql += t.sql; sql += t.sql;
bind.push.apply(bind, t.bind); t.bind.forEach(v => bind.push(v));
where = where_builder(where); where = where_builder(where);
sql += ' WHERE '+(where.sql || '1=1'); sql += ' WHERE '+(where.sql || '1=1');
bind.push.apply(bind, where.bind); where.bind.forEach(v => bind.push(v));
if (t.moreWhere) if (t.moreWhere)
{ {
sql += ' AND '+t.moreWhere.sql; sql += ' AND '+t.moreWhere.sql;
bind.push.apply(bind, t.moreWhere.bind); t.moreWhere.bind.forEach(v => bind.push(v));
} }
if (options['GROUP BY'] || options.group_by) if (options['GROUP BY'] || options.group_by)
{ {
@ -137,7 +137,7 @@ function tables_builder(tables)
for (const row of table.rows) for (const row of table.rows)
{ {
sql += (i > 0 ? ', (' : '(') + table.keys.map(() => '?').join(', ')+')'; sql += (i > 0 ? ', (' : '(') + table.keys.map(() => '?').join(', ')+')';
bind.push.apply(bind, table.keys.map(k => row[k])); table.keys.forEach(k => bind.push(row[k]));
i++; i++;
} }
sql += ') AS '+k+'('+table.keys.join(', ')+')'; sql += ') AS '+k+'('+table.keys.join(', ')+')';
@ -145,7 +145,7 @@ function tables_builder(tables)
else if (table instanceof Text) else if (table instanceof Text)
{ {
sql += '(' + table.sql + ') ' + k; sql += '(' + table.sql + ') ' + k;
bind.push.apply(bind, table.bind); table.bind.forEach(v => bind.push(v));
} }
else if (typeof table == 'object') else if (typeof table == 'object')
{ {
@ -163,7 +163,7 @@ function tables_builder(tables)
{ {
sql += subjoin.sql; sql += subjoin.sql;
} }
bind.push.apply(subjoin.bind); subjoin.bind.forEach(v => bind.push(v));
} }
else else
{ {
@ -177,13 +177,13 @@ function tables_builder(tables)
else else
{ {
conds.sql += ' AND ' + more_on.sql; conds.sql += ' AND ' + more_on.sql;
conds.bind.push.apply(conds.bind, more_on.bind); more_on.bind.forEach(v => conds.bind.push(v));
} }
} }
if (!first) if (!first)
{ {
sql += ' ON ' + (conds.sql || '1=1'); sql += ' ON ' + (conds.sql || '1=1');
bind.push.apply(bind, conds.bind); conds.bind.forEach(v => bind.push(v));
} }
else else
{ {
@ -234,7 +234,7 @@ function where_or_set(fields, for_condition)
} }
// FIXME: check bind variable count // FIXME: check bind variable count
w.push(k); w.push(k);
bind.push.apply(bind, v); v.forEach(v => bind.push(v));
continue; continue;
} }
v = v instanceof Array ? v : [ v ]; v = v instanceof Array ? v : [ v ];
@ -253,7 +253,7 @@ function where_or_set(fields, for_condition)
{ {
// (a, b) in ((...), ...) // (a, b) in ((...), ...)
w.push(k + ' in (' + v.map(vi => '('+vi.map(() => '?').join(', ')+')') + ')'); w.push(k + ' in (' + v.map(vi => '('+vi.map(() => '?').join(', ')+')') + ')');
v.map(vi => bind.push.apply(bind, vi)); v.forEach(vi => vi.forEach(v => bind.push(v)));
} }
else if (!for_condition) else if (!for_condition)
{ {
@ -267,7 +267,7 @@ function where_or_set(fields, for_condition)
if (v.length > 0) if (v.length > 0)
{ {
w.push(k+' in (' + v.map(() => '?').join(', ') + ')' + (n > v.length ? ' or '+k+' is null' : '')); w.push(k+' in (' + v.map(() => '?').join(', ') + ')' + (n > v.length ? ' or '+k+' is null' : ''));
bind.push.apply(bind, v); v.forEach(v => bind.push(v));
} }
else if (n > 0) else if (n > 0)
{ {