Skip to content

Commit

Permalink
fix: 修复z键查询的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Sep 10, 2022
1 parent 9662372 commit 6bd7351
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 42 deletions.
14 changes: 0 additions & 14 deletions Fire.xcodeproj/xcshareddata/xcschemes/TableBuilder.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@
ReferencedContainer = "container:Fire.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--create-dict"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "wb_dict"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "/Users/qwertyyb/db.sqlite3"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
32 changes: 21 additions & 11 deletions Fire/DictManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DictManager {
text,
type, min(query) as query
from wb_py_dict
where query >= :queryMin and query <= :queryMax \(
where query like :queryLike \(
codeMode == .wubi ? "and type = 'wb'"
: codeMode == .pinyin ? "and type = 'py'" : "")
group by text
Expand All @@ -63,6 +63,7 @@ class DictManager {
private func prepareStatement() {
if database == nil {
sqlite3_open_v2(getDatabaseURL().path, &database, SQLITE_OPEN_READWRITE, nil)
sqlite3_exec(database, "PRAGMA case_sensitive_like=ON;", nil, nil, nil)
}
if queryStatement != nil {
sqlite3_finalize(queryStatement)
Expand Down Expand Up @@ -112,14 +113,28 @@ class DictManager {
print("[replaceTextWithVars] \(text), \(newText)")
return newText
}


private func getQueryLike(_ origin: String) -> String {
if origin.isEmpty {
return origin
}

if !Defaults[.zKeyQuery] {
return origin + "%"
}

// z键查询,z不能放在首位
let first = origin.first!
return String(first) + (String(origin.suffix(origin.count - 1))
.replacingOccurrences(of: "z", with: "_")) + "%"
}

func getCandidates(query: String = String(), page: Int = 1) -> (candidates: [Candidate], hasNext: Bool) {
if query.count <= 0 {
return ([], false)
}
NSLog("get local candidate, origin: \(query), query: ", query)
let queryMin = query;
let queryMax = query + "zzzz";
let queryLike = getQueryLike(query)
var candidates: [Candidate] = []
sqlite3_reset(queryStatement)
sqlite3_clear_bindings(queryStatement)
Expand All @@ -129,13 +144,8 @@ class DictManager {
SQLITE_TRANSIENT
)
sqlite3_bind_text(queryStatement,
sqlite3_bind_parameter_index(queryStatement, ":queryMin"),
queryMin, -1,
SQLITE_TRANSIENT
)
sqlite3_bind_text(queryStatement,
sqlite3_bind_parameter_index(queryStatement, ":queryMax"),
queryMax, -1,
sqlite3_bind_parameter_index(queryStatement, ":queryLike"),
queryLike, -1,
SQLITE_TRANSIENT
)
sqlite3_bind_int(queryStatement,
Expand Down
18 changes: 1 addition & 17 deletions Fire/Fire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,12 @@ class Fire: NSObject {
])
}

private func getQueryFromOrigin(_ origin: String) -> String {
if origin.isEmpty {
return origin
}

if !Defaults[.zKeyQuery] {
return origin
}

// z键查询,z不能放在首位
let first = origin.first!
return String(first) + (String(origin.suffix(origin.count - 1))
.replacingOccurrences(of: "z", with: "_"))
}

var server: IMKServer = IMKServer.init(name: kConnectionName, bundleIdentifier: Bundle.main.bundleIdentifier)
func getCandidates(origin: String = String(), page: Int = 1) -> (candidates: [Candidate], hasNext: Bool) {
if origin.count <= 0 {
return ([], false)
}
let query = getQueryFromOrigin(origin)
let (candidates, hasNext) = DictManager.shared.getCandidates(query: query, page: page)
let (candidates, hasNext) = DictManager.shared.getCandidates(query: origin, page: page)
let transformed = candidates.map { (candidate) -> Candidate in
if candidate.type == .user {
return Candidate(code: candidate.code, text: candidate.text, type: .user)
Expand Down
4 changes: 4 additions & 0 deletions TableBuilder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ int main(int argc, const char * argv[]) {
std::cout << "Hello, World!\n";

cout << "argc: " << argc << endl;

if (argc <= 1) {
return 0;
}

for (int i = 0; i < argc; i++) {
cout << "argv(" << i << "): " << argv[i] << endl;
Expand Down

0 comments on commit 6bd7351

Please sign in to comment.