Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created January 8, 2014 03:53
Show Gist options
  • Save dulichan/8311514 to your computer and use it in GitHub Desktop.
Save dulichan/8311514 to your computer and use it in GitHub Desktop.
Example of using string concat for for sql queries
var buildPlatformString = function(platform){
var platform = platform.toUpperCase();
if ( platform== 'ANDROID'){
platform = 'devices.platform_id=1';
}else if (platform == 'IOS'){
platform = '(devices.platform_id=2 or devices.platform_id=3 or devices.platform_id=4)';
}
return platform;
}
var buildDynamicQuery = function(platform, type){
var platform = buildPlatformString(platform);
var query;
if(type==1){
query ="select out_table.id, out_table.user_id, out_table.device_id, out_table.received_data, devices.platform_id from notifications as out_table , devices where out_table.`feature_code`= '"+GET_APP_FEATURE_CODE+"' and out_table.`status`='R' and out_table.`id` in (select MAX(inner_table.`id`) from notifications as inner_table where inner_table.`feature_code`= '"+GET_APP_FEATURE_CODE+"' and inner_table.`status`='R' and out_table.device_id =inner_table.device_id) and devices.id=out_table.device_id and "+platform+" and `received_data` like ?;";
}else if (type==2){
query ="select out_table.id, out_table.user_id, out_table.device_id, out_table.received_data, devices.platform_id from notifications as out_table , devices where out_table.`feature_code`= '"+GET_APP_FEATURE_CODE+"' and out_table.`status`='R' and out_table.`id` in (select MAX(inner_table.`id`) from notifications as inner_table where inner_table.`feature_code`= '"+GET_APP_FEATURE_CODE+"' and inner_table.`status`='R' and out_table.device_id =inner_table.device_id) and devices.id=out_table.device_id and "+platform+" and `received_data` not like ?;";
}
return query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment