mybatis-mapper icon indicating copy to clipboard operation
mybatis-mapper copied to clipboard

Can you add a suffix in TRIM tag?

Open zkxil opened this issue 1 year ago • 3 comments

some tools generate mybatis template sql like this: <trim prefix="values (" suffix=")" suffixOverrides=","> So I need to add suffix and suffixOverrides in TRIM tag, I already changed code in my machine, and it run smoothly. can you update this code?

var convertTrimWhere = function(children, param, namespace, myBatisMapper) {  
  var convertString = '';
  var prefix = null;
  var prefixOverrides = null;
  var suffix = null;
  var suffixOverrides = null;
  var globalSet = null;
  
  try{
    switch (children.name.toLowerCase()) {
    case 'trim':
      prefix = children.attrs.prefix;
      prefixOverrides = children.attrs.prefixOverrides;
      suffix = children.attrs.suffix;
      suffixOverrides = children.attrs.suffixOverrides;
      globalSet = 'g';
      break;
    case 'where':    
      prefix = 'WHERE';
      prefixOverrides = 'and|or';
      globalSet = 'gi';
      break;
    default:
      throw new Error("Error occurred during convert <trim/where> element.");
    }
    
    // Convert children first.
    for (var j=0, nextChildren; nextChildren=children.children[j]; j++){
      convertString += convertChildren(nextChildren, param, namespace, myBatisMapper);
    }
    
    // Remove prefixOverrides
    var trimRegex = new RegExp('(^)([\\s]*?)(' + prefixOverrides + ')', globalSet);
    convertString = convertString.replace(trimRegex, '');
    // Remove suffixOverrides
    trimRegex = new RegExp('(' + suffixOverrides + ')([\\s]*?)($)', globalSet);
    convertString = convertString.replace(trimRegex, '');
	
    if (children.name.toLowerCase() != 'trim'){
      var trimRegex = new RegExp('(' + prefixOverrides + ')([\\s]*?)($)', globalSet);
      convertString = convertString.replace(trimRegex, '');
    } 
    
    // Add Prefix if String is not empty.
    var trimRegex = new RegExp('([a-zA-Z])', 'g');
    var w = convertString.match(trimRegex);
  
    if (w != null && w.length > 0) {
      convertString = prefix + ' '+ convertString + ' ' + suffix;
    }
    
    // Remove comma(,) before WHERE
    if (children.name.toLowerCase() != 'where'){
      var regex = new RegExp('(,)([\\s]*?)(where)', 'gi');
      convertString = convertString.replace(regex, ' WHERE ');
    }
    
    return convertString;
  } catch (err) {
    throw new Error("Error occurred during convert <" + children.name.toLowerCase() + "> element.");
  }
}

zkxil avatar Aug 23 '22 04:08 zkxil

hi, could you please make a Pull request for this?

OldBlackJoe avatar Aug 24 '22 02:08 OldBlackJoe

I created a pull request. #23

zkxil avatar Aug 28 '22 14:08 zkxil

can you merge the pull request #23

zkxil avatar Sep 05 '22 11:09 zkxil