Tencent / wcdb

WCDB is a cross-platform database framework developed by WeChat.
Other
10.61k stars 1.39k forks source link

下面的查询方式是否正确?union前的括号该怎么实现? #1105

Closed jiyang7 closed 1 month ago

jiyang7 commented 1 month ago

The language of WCDB

Swift

The version of WCDB

v2.0.4

The platform of WCDB

iOS

The installation of WCDB

Cocoapods

What's the issue?

我在一个do catch内查询同一个表中两个不同条件的数据,发现结果1 result 和 结果2 otherResult 数据相同,理论上两个条件不同结果是不应该相同的,如果分开至两个do catch执行两次查询结果则不同。

    do {
        let condition = PCNotificationMessageInfo.Properties.notifyType.in([6, 7, 10])
        let select = try messageNoticeDB.prepareSelect(of: MessageNoticeTableType.messageInfo.tableType, fromTable: MessageNoticeTableType.messageInfo.rawValue).where(condition).limit(40)
        var result = try select.allObjects(of: PCNotificationMessageInfo.self)
        resultArray.append(contentsOf: result)

        let otherSelect = try messageNoticeDB.prepareSelect(of: MessageNoticeTableType.messageInfo.tableType, fromTable: MessageNoticeTableType.messageInfo.rawValue).where(!condition)
        let otherResult = try otherSelect.allObjects(of: PCNotificationMessageInfo.self)
        resultArray.append(contentsOf: otherResult)
    } catch {

    }

尝试使用union发现无法使用括号 let statementSelect = StatementSelect().select(properties).from(tableName).where(condition).limit(40).union().select(properties).from(tableName).where(!condition)

请问如下语句该如何实现 (SELECT FROM tableName WHERE condition1 LIMIT 40) union (SELECT FROM tableName WHERE condition2)

Qiuwen-chen commented 1 month ago

StatementSelect().select(Column.all()).from("tableName1").where(condition1).union().select(Column.all()).from("tableName2").where(condition2)

jiyang7 commented 1 month ago

请问 LIMIT 40是否添加 如果按回复添加 StatementSelect().select(Column.all()).from("tableName1").where(condition1).limit(40).union().select(Column.all()).from("tableName2").where(condition2)

通过输出 description 发现语句是,总数限制40,并非单条语句限制40条 SELECT FROM tableName1 WHERE condition1 union SELECT FROM tableName2 WHERE condition2 LIMIT 40 并非 (SELECT FROM tableName1 WHERE condition1 LIMIT 40) union (SELECT FROM tableName2 WHERE condition2)

Qiuwen-chen commented 1 month ago

SQLite's select syntax can only put limit at the end of the statement: https://www.sqlite.org/lang_select.html

Qiuwen-chen commented 1 month ago

May be you should use two queries.

jiyang7 commented 1 month ago

麻烦看下第一个问题

我在一个do catch内查询同一个表中两个不同条件的数据,发现结果1 result 和 结果2 otherResult 数据相同,理论上两个条件不同结果是不应该相同的,如果分开至两个do catch执行两次查询结果则不同。

do {
    let condition = PCNotificationMessageInfo.Properties.notifyType.in([6, 7, 10])
    let select = try messageNoticeDB.prepareSelect(of: MessageNoticeTableType.messageInfo.tableType, fromTable: MessageNoticeTableType.messageInfo.rawValue).where(condition).limit(40)
    var result = try select.allObjects(of: PCNotificationMessageInfo.self)
    resultArray.append(contentsOf: result)

    let otherSelect = try messageNoticeDB.prepareSelect(of: MessageNoticeTableType.messageInfo.tableType, fromTable: MessageNoticeTableType.messageInfo.rawValue).where(!condition)
    let otherResult = try otherSelect.allObjects(of: PCNotificationMessageInfo.self)
    resultArray.append(contentsOf: otherResult)
} catch {

}

请问这个问题有原因么?issues/wiki是否有相关内容?

Qiuwen-chen commented 1 month ago

You can give a demo project to reproduce it.

jiyang7 commented 1 month ago

简单建了一个测试工程,麻烦看一下。

https://github.com/jiyang7/WCDBTestSample.git

点击“SelectWrongTestData”按钮,“selectWrongTestData”方法中,“result”和“otherResult”的个数为什么相同???即使不加limit限制,“result”和“otherResult”的个数也会相同,这显然是不正确的。

Qiuwen-chen commented 1 month ago

Got it, it is indeed an obvious bug. I will fix it in the next version.

For now, you can use this interface to avoid this problem: database.getObjects(on: Sample.Properties.all, fromTable: tableName, where: condition, orderBy: [orderBy])

Qiuwen-chen commented 3 weeks ago

Fixed in v2.1.6