Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Issue with sql-support plugin #1019

@mmohamud1

Description

@mmohamud1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions