pofider / node-odata-to-sql

Generate SQL from OData in node.js
MIT License
5 stars 4 forks source link

MS SQL doesn't support RegExps #1

Open holocron opened 7 years ago

holocron commented 7 years ago

MS SQL doesn't support RegExps, so the idea was to have opportunity to have "like" options for odata functions contains, startswith and endswith.

pofider commented 7 years ago

Thank you for your effort. I'm trying to remember this repository. :) It seems that this filter.js file transforms mongo like queries into sql. Such queries could include RegExp, but this is not supported in sql. So you propose to introduce a new properietary attribute like. I don't see this as a good way, because I wan't to be database agnostic in the libs I use this.

Wouldn't it be better to try to support regexp at least partially?

function eq (q, table, el, obj, op, type) {   
  if (obj instanceof RegExp) {
    var pattern = obj + ''  
    return q[op](table[el].like('%' + pattern.substring(1, pattern.length - 1) + '%'))
  }

  if (type.isPrimitive) {    
    return q[op](table[el].equals(obj))
  } else {    
    return q[op](table[el].like('%' + obj + '%'))
  }
}

This would make many common queries working

{ name: /somepart/ }
holocron commented 7 years ago

Hello, thanks for your answer! well, on one hand contains case will work. but startswith and endswith won't. BR, Denis

pofider commented 7 years ago

Yes, maybe it could be improved to support it and use % based on the occurrence of ^ and $ in regexp. It will never support full regexp, but we could potentially reach the point it quite safely recognizes and apply regexps for startsWith and endsWith?

holocron commented 7 years ago

I understand it never will support it... let's try at least :+1:

update: may be it's easier to convert to RegExp later on DB specific layer? at least there will be no need to parse it back

BR, Denis