简体   繁体   English

如何基于参数以更高效、更好的方式分析数据库表中的字符串?

[英]How to analyze the strings from a database table in a more efficient and better way based on a parameter?

I have two databases which are hooked to two different backend application servers.我有两个数据库,它们连接到两个不同的后端应用程序服务器。 Both the servers talk to one single web application.两台服务器都与一个 Web 应用程序通信。 Now, one can simply change a url parameter on the UI to choose which service to communicate with ie, if you set serverUrl='abc' , it would point to server A with database A , and if you set serverUrl='cde' , it would point to server B with database B .现在,可以简单地更改 UI 上的 url 参数以选择要与哪个服务通信,即,如果您设置serverUrl='abc' ,它将指向server Adatabase A ,如果您设置serverUrl='cde' ,它会指向带有database B server B database B I have workflow tests on the front end which are basically common for both the services.我在前端进行了工作流测试,这两种服务基本上都是通用的。 But the field names in the db A and db B are different.但是db Adb B中的字段名称是不同的。 ie, lets say if I have a field name called as "webid" in db A , the same field name is called as " Webid " in db B and similarly there are more like " code " in db A is " CODE " in db B .即,可以说如果我有被称为“webid”的字段名db A ,相同的字段名称被称为“ Webid在” db B ,同样有更多像“ code中的” db A是“ CODE ”的db B So, instead of duplicating my tests, I simply created a function, where I simply check based on the serverUrl, if it's db A , return "code" else return " CODE ", and so on for all the field names in the database.因此,我没有重复我的测试,而是简单地创建了一个函数,我只是根据 serverUrl 检查,如果它是db A ,则返回“代码”,否则返回“ CODE ”,等等数据库中的所有字段名称。 So, in my tests, instead of simply creating the query with the normal fieldname , I pass in checkField("code") and it returns a string based on the condition ( serverUrl ) and executes the query.因此,在我的测试中,我不是简单地使用普通的fieldname创建查询,而是传入checkField("code")并根据条件 ( serverUrl ) 返回一个字符串并执行查询。

The function is as follows:功能如下:

function checkField(str: string) : string {
          let server = serverUrl === 'abc' ? true : false;
           switch (args[0].toLowerCase()) {
            case 'code':
                return serverUrl ? 'code' : 'CODE';
            case 'webid':
                return serverUrl ? 'webid' : 'Webid';
            case 'pkid':
                return serverUrl ? 'pkid' : 'PkId';
            case 'barcode':
                return serverUrl ? 'barcode' : 'Barcode';
            case 'price':
                return serverUrl ? 'price' : 'Price';
            case 'bestbefore':
                return serverUrl ? 'bestbefore' : 'BestBefore';
            case 'produce':
                return serverUrl ? 'produce' : 'Produce';
            case 'sales':
                return serverUrl ? 'sales' : 'Sales';
            case 'marketid':
                return serverUrl ? "marketid" : "MarketId";
            case 'regdate':
                return serverUrl ? "regdate" : "Regdate"; //and more fields
            default:
                return args[0]; 

}

So, I was wondering if there is a more elegant way to do this kind of comparison.所以,我想知道是否有更优雅的方法来进行这种比较。 Can we use static code analyzers here in anyway?[I am not really sure what they are] Any insights or pointers would be great.我们可以在这里使用静态代码分析器吗?[我不太确定它们是什么]任何见解或指针都会很棒。 Thank you so much.非常感谢。

The insight would be that when serverUrl is truthy, you don't need the switch at all - you always return the same value that was switched upon.洞察力是,当serverUrl为真时,您根本不需要switch - 您总是返回与打开的相同的值。 So don't do the test in every switch case , but do it once before that:所以不要在每个 switch case中都做测试,而是在此之前做一次:

function checkField(str: string) : string {
    if (serverUrl === 'abc')
        return str.toLowerCase();
    else
        switch (str.toLowerCase()) {
            case 'code': return 'CODE';
            case 'webid': return 'Webid';
            case 'pkid': return 'PkId';
            case 'barcode': return 'Barcode';
            case 'price': return 'Price';
            case 'bestbefore': return 'BestBefore';
            case 'produce': return 'Produce';
            case 'sales': return 'Sales';
            case 'marketid': return "MarketId";
            case 'regdate': return "Regdate";
            //and more fields
            default: return str;
        }
}

Instead of the switch statement, you can also use an object literal or a Map as a lookup table.除了switch语句,您还可以使用对象字面量或Map作为查找表。

Borrowing a bit from @Bergi's answer, I would create a mapping object to make it a little cleaner.从@Bergi 的回答中借用一点,我会创建一个映射对象以使其更简洁。 Eg:例如:

function checkField(str: string) : string {
    //create a mapping
    var myMapping = {
         'code'   : 'CODE',
         'webid'  : 'Webid',
         'pkid'   : 'PkId',
         'barcode': 'Barcode',
         //and more fields
    }

    if (serverUrl === 'abc') {
        return str.toLowerCase();
    } else {
        return myMapping[str.toLowerCase()] || str;
    }
}

This keeps the logic a little more separate from the mapping, so it feels cleaner to me.这使逻辑与映射更加分离,因此对我来说感觉更清晰。 But that's a personal preference.但这是个人喜好。

You can put all the values in a map.您可以将所有值放在地图中。

function checkField(String str) {
    let map = new Map([["code", "CODE"], ["webid", "Webid"]]);
    if (serverUrl === 'abc')
        return str.toLowerCase();
    else    
        return map[str]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM