jQuery-QueryBuilder icon indicating copy to clipboard operation
jQuery-QueryBuilder copied to clipboard

Issue with sql-support plugin

Open mmohamud1 opened this issue 7 months ago • 0 comments

Hi I have the following function export function buildSQLQueryBuilder(columns,sqlBuilderID,sqlGeneratorID,sqlPreviewID,initialSql = null) { console.log(columns); // Destroy any previous instance $(#${sqlBuilderID}).queryBuilder('destroy'); $(#${sqlPreviewID}`).text('Click generate button to see SQL query');

const filters = [ { id: '*', label: 'All', type: 'string', operators: ['equal','not_equal','in','not_in','contains','not_contains','less','less_or_equal','greater','greater_or_equal','between','not_between','is_null','is_not_null'] }, ...columns.map(col => ({ id: col, label: col, type: 'string', operators: ['equal','not_equal','in','not_in','contains','not_contains','less','less_or_equal','greater','greater_or_equal','between','not_between','is_null','is_not_null'] })) ];

// Initialize with sql-parser support $(#${sqlBuilderID}).queryBuilder({ plugins: ['sql-support'], filters, allow_empty: true, default_condition:'AND', display_empty_group: true, allow_groups: true, lang: { operators: { like: 'LIKE', not_like: 'NOT LIKE' }, conditions:{ AND: 'AND', OR: 'OR' } } });

// If they passed in a preset SQL, import it into the builder: if (initialSql) { try { $(#${sqlBuilderID}) .queryBuilder('setRulesFromSQL', initialSql, { parser: 'MySQL', // or 'PostgreSQL' depending on your dialect skipValidation: true // you can tweak this }); } catch (e) { console.error('SQL import failed', e); } }

// Generate SQL button $(#${sqlGeneratorID}).off('click').on('click', function () { const result = $(#${sqlBuilderID}).queryBuilder('getSQL', false); let output = '-- No valid SQL generated --';

if (result && result.sql) {
  let sqlBody = result.sql
    .trim()
    .replace(/^(AND|OR)\s+/i, '');

  // your LIKE-capitalization logic...
  sqlBody = sqlBody.replace(
    /(\w+)\s+(NOT\s+)?LIKE\s+'(?:%?)([^'%]+)(?:%?)'/gi,
    (match, col, notPart, val) => {
      const op = (notPart||'').trim()==='NOT' ? 'NOT LIKE' : 'LIKE';
      if (op === 'LIKE') {
        return `(${col} LIKE '%${val}%' OR ${col} LIKE '%${val.toUpperCase()}%')`;
      } else {
        return `(${col} NOT LIKE '%${val}%' AND ${col} NOT LIKE '%${val.toUpperCase()}%')`;
      }
    }
  );

  output = 'WHERE ' + sqlBody;
}
$(`#${sqlPreviewID}`).text(output);

}); }` I get the console c.esc is not a function this leads me to believe there is an issue with the parser library that I have in my repo https://github.com/mistic100/sql-parser

mmohamud1 avatar May 28 '25 15:05 mmohamud1