小编Cyb*_*die的帖子

为什么在sql语句中使用COALESCE()?

我正在重构一些旧代码并偶然发现了这个命名查询(它在 mysql 之上使用 hibernate):

delete F from
    foo F
inner join user DU on F.user_id = DU.id
where
(COALESCE(:userAlternateId,null) is null or DU.alternate_id like :userAlternateId )
    and ( COALESCE(:fooId,null) is null or F.foo_id like :fooId )
    and (
        ( COALESCE(:fromUpdated,null) is null or F.updated_at>=:fromUpdated )
        and ( COALESCE(:toUpdated,null) is null or F.updated_at<=:toUpdated )
)
Run Code Online (Sandbox Code Playgroud)

我不明白为什么COALESCE要以这种方式使用它: COALESCE(:userAlternateId,null) is null

这是性能黑客还是它使查询数据库独立还是......?

顺便说一句userAlternateId,是字符串/varchar,其他 id 是长整型和from-to日期

mysql sql coalesce

5
推荐指数
1
解决办法
1695
查看次数

在C++类的构造函数中抛出异常是一种好习惯吗?

我有这个构造函数抛出异常

GenericSocket::GenericSocket(const string& hostname, 
                             const string& servname):
                             _hostname(hostname),
                             _servname(servname)
{  
    initHints();
    int rv;
    if((rv = getaddrinfo(_hostname.c_str(), 
                    _servname.c_str(), 
                    &_hints, 
                    &_servinfo)) != 0) {  
        throw GenericSocketException();
    }  

} 
Run Code Online (Sandbox Code Playgroud)

initHints()执行_hints的memset并设置一些变量.

我用google测试框架测试它,如下所示:

TEST(CreateObject2, getaddrinfoException)
{
    mgs_addrinfo_return = 1; 
    ASSERT_THROW(new GenericSocket("testhost", "4242"), GenericSocketException);
}
Run Code Online (Sandbox Code Playgroud)

测试因核心转储失败:

[ RUN      ] CreateObject2.getaddrinfoException
socket creation failed
terminate called after throwing an instance of 'common::GenericSocketException'
  what():  Socket creation failed
[1]    43360 abort (core dumped)  ./bin/test_common
Run Code Online (Sandbox Code Playgroud)

除了我不知道到底出了什么问题的事实,我怀疑一些未初始化的对象被删除(?),很多似乎发生在幕后,所以我开始怀疑在构造函数中抛出异常是否是好习惯.是否可以更好地将此功能放在另一个我可以在创建对象后调用的函数中,然后处理异常?

c++ constructor googletest

4
推荐指数
3
解决办法
2106
查看次数

4
推荐指数
1
解决办法
1058
查看次数

标签 统计

c++ ×1

cassandra ×1

coalesce ×1

constructor ×1

cql ×1

googletest ×1

mysql ×1

nosql ×1

sql ×1