mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Update sqlite
This commit is contained in:
parent
bb8b64d365
commit
8a21e1eed5
@ -1,223 +0,0 @@
|
||||
import Foundation
|
||||
import sqlcipher
|
||||
|
||||
private final class SqliteInterfaceStatement {
|
||||
let statement: OpaquePointer?
|
||||
|
||||
init(statement: OpaquePointer?) {
|
||||
self.statement = statement
|
||||
}
|
||||
|
||||
func bind(_ index: Int, data: UnsafeRawPointer, length: Int) {
|
||||
sqlite3_bind_blob(statement, Int32(index), data, Int32(length), nil)
|
||||
}
|
||||
|
||||
func bind(_ index: Int, number: Int64) {
|
||||
sqlite3_bind_int64(statement, Int32(index), number)
|
||||
}
|
||||
|
||||
func bindNull(_ index: Int) {
|
||||
sqlite3_bind_null(statement, Int32(index))
|
||||
}
|
||||
|
||||
func bind(_ index: Int, number: Int32) {
|
||||
sqlite3_bind_int(statement, Int32(index), number)
|
||||
}
|
||||
|
||||
func reset() {
|
||||
sqlite3_reset(statement)
|
||||
sqlite3_clear_bindings(statement)
|
||||
}
|
||||
|
||||
func step() -> Bool {
|
||||
let result = sqlite3_step(statement)
|
||||
if result != SQLITE_ROW && result != SQLITE_DONE {
|
||||
assertionFailure("Sqlite error \(result)")
|
||||
}
|
||||
return result == SQLITE_ROW
|
||||
}
|
||||
|
||||
func int32At(_ index: Int) -> Int32 {
|
||||
return sqlite3_column_int(statement, Int32(index))
|
||||
}
|
||||
|
||||
func int64At(_ index: Int) -> Int64 {
|
||||
return sqlite3_column_int64(statement, Int32(index))
|
||||
}
|
||||
|
||||
func valueAt(_ index: Int) -> ReadBuffer {
|
||||
let valueLength = sqlite3_column_bytes(statement, Int32(index))
|
||||
let valueData = sqlite3_column_blob(statement, Int32(index))
|
||||
|
||||
let valueMemory = malloc(Int(valueLength))!
|
||||
memcpy(valueMemory, valueData, Int(valueLength))
|
||||
return ReadBuffer(memory: valueMemory, length: Int(valueLength), freeWhenDone: true)
|
||||
}
|
||||
|
||||
func keyAt(_ index: Int) -> ValueBoxKey {
|
||||
let valueLength = sqlite3_column_bytes(statement, Int32(index))
|
||||
let valueData = sqlite3_column_blob(statement, Int32(index))
|
||||
|
||||
let key = ValueBoxKey(length: Int(valueLength))
|
||||
memcpy(key.memory, valueData, Int(valueLength))
|
||||
return key
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
sqlite3_finalize(statement)
|
||||
}
|
||||
}
|
||||
|
||||
public final class SqliteStatementCursor {
|
||||
private let statement: SqliteInterfaceStatement
|
||||
|
||||
fileprivate init(statement: SqliteInterfaceStatement) {
|
||||
self.statement = statement
|
||||
}
|
||||
|
||||
public func getInt32(at index: Int) -> Int32 {
|
||||
return self.statement.int32At(index)
|
||||
}
|
||||
|
||||
public func getInt64(at index: Int) -> Int64 {
|
||||
return self.statement.int64At(index)
|
||||
}
|
||||
|
||||
public func getString(at index: Int) -> String {
|
||||
let value = self.statement.valueAt(index)
|
||||
if let string = String(data: value.makeData(), encoding: .utf8) {
|
||||
return string
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
public func getData(at index: Int) -> Data {
|
||||
return self.statement.valueAt(index).makeData()
|
||||
}
|
||||
}
|
||||
|
||||
public enum SqliteInterfaceStatementKey {
|
||||
case int32(Int32)
|
||||
case int64(Int64)
|
||||
case data(Data)
|
||||
}
|
||||
|
||||
public final class SqliteInterface {
|
||||
private let database: Database
|
||||
|
||||
public init?(databasePath: String) {
|
||||
if let database = Database(databasePath) {
|
||||
self.database = database
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
public func unlock(password: Data) -> Bool {
|
||||
return password.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Bool in
|
||||
if sqlite3_key(self.database.handle, bytes, Int32(password.count)) != SQLITE_OK {
|
||||
return false
|
||||
}
|
||||
var statement: OpaquePointer? = nil
|
||||
let status = sqlite3_prepare_v2(self.database.handle, "SELECT * FROM SQLITE_MASTER", -1, &statement, nil)
|
||||
if status != SQLITE_OK {
|
||||
return false
|
||||
}
|
||||
let preparedStatement = SqlitePreparedStatement(statement: statement)
|
||||
if !preparedStatement.step(handle: self.database.handle, true, path: "") {
|
||||
return false
|
||||
}
|
||||
preparedStatement.destroy()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
public func withStatement(_ query: String, _ f: (([SqliteInterfaceStatementKey], (SqliteStatementCursor) -> Bool) -> Void) -> Void) {
|
||||
var statement: OpaquePointer? = nil
|
||||
if sqlite3_prepare_v2(database.handle, query, -1, &statement, nil) != SQLITE_OK {
|
||||
return
|
||||
}
|
||||
let preparedStatement = SqliteInterfaceStatement(statement: statement)
|
||||
|
||||
f({ keys, iterate in
|
||||
preparedStatement.reset()
|
||||
var index = 1
|
||||
for key in keys {
|
||||
switch key {
|
||||
case let .data(data):
|
||||
let dataCount = data.count
|
||||
data.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Void in
|
||||
preparedStatement.bind(index, data: bytes, length: dataCount)
|
||||
}
|
||||
case let .int32(value):
|
||||
preparedStatement.bind(index, number: value)
|
||||
case let .int64(value):
|
||||
preparedStatement.bind(index, number: value)
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
let cursor = SqliteStatementCursor(statement: preparedStatement)
|
||||
while preparedStatement.step() {
|
||||
if !iterate(cursor) {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
preparedStatement.reset()
|
||||
preparedStatement.destroy()
|
||||
}
|
||||
|
||||
public func selectWithKeys(_ query: String, keys: [(Int, Data)], _ f: (SqliteStatementCursor) -> Bool) {
|
||||
var statement: OpaquePointer? = nil
|
||||
if sqlite3_prepare_v2(database.handle, query, -1, &statement, nil) != SQLITE_OK {
|
||||
return
|
||||
}
|
||||
let preparedStatement = SqliteInterfaceStatement(statement: statement)
|
||||
for (index, key) in keys {
|
||||
key.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Void in
|
||||
preparedStatement.bind(index, data: bytes, length: key.count)
|
||||
}
|
||||
}
|
||||
let cursor = SqliteStatementCursor(statement: preparedStatement)
|
||||
while preparedStatement.step() {
|
||||
if !f(cursor) {
|
||||
break
|
||||
}
|
||||
}
|
||||
preparedStatement.reset()
|
||||
preparedStatement.destroy()
|
||||
}
|
||||
|
||||
public func select(_ query: String, _ f: (SqliteStatementCursor) -> Bool) {
|
||||
var statement: OpaquePointer? = nil
|
||||
if sqlite3_prepare_v2(database.handle, query, -1, &statement, nil) != SQLITE_OK {
|
||||
return
|
||||
}
|
||||
if statement == nil {
|
||||
return
|
||||
}
|
||||
let preparedStatement = SqliteInterfaceStatement(statement: statement)
|
||||
let cursor = SqliteStatementCursor(statement: preparedStatement)
|
||||
while preparedStatement.step() {
|
||||
if !f(cursor) {
|
||||
break
|
||||
}
|
||||
}
|
||||
preparedStatement.reset()
|
||||
preparedStatement.destroy()
|
||||
}
|
||||
|
||||
public func explain(_ query: String) -> String {
|
||||
var result = ""
|
||||
self.select("EXPLAIN QUERY PLAN \(query)", { cursor in
|
||||
if !result.isEmpty {
|
||||
result.append("\n")
|
||||
}
|
||||
result.append("\(cursor.getInt32(at: 0)) \(cursor.getInt32(at: 1)) \(cursor.getInt32(at: 2)) \(cursor.getString(at: 3))")
|
||||
return true
|
||||
})
|
||||
return result
|
||||
}
|
||||
}
|
@ -167,7 +167,6 @@ swift_library(
|
||||
"//third-party/opus:opus",
|
||||
"//submodules/WatchBridgeAudio:WatchBridgeAudio",
|
||||
"//submodules/WatchBridge:WatchBridge",
|
||||
"//submodules/LegacyDataImport:LegacyDataImport",
|
||||
"//submodules/ShareItems:ShareItems",
|
||||
"//submodules/ShareItems/Impl:ShareItemsImpl",
|
||||
"//submodules/ReactionSelectionNode:ReactionSelectionNode",
|
||||
|
@ -22,7 +22,6 @@ import UndoUI
|
||||
import LegacyUI
|
||||
import PassportUI
|
||||
import WatchBridge
|
||||
import LegacyDataImport
|
||||
import SettingsUI
|
||||
import AppBundle
|
||||
import UrlHandling
|
||||
@ -906,68 +905,7 @@ final class SharedApplicationContext {
|
||||
Logger.shared.logToConsole = loggingSettings.logToConsole
|
||||
Logger.shared.redactSensitiveData = loggingSettings.redactSensitiveData
|
||||
|
||||
return importedLegacyAccount(basePath: appGroupUrl.path, accountManager: sharedApplicationContext.sharedContext.accountManager, encryptionParameters: encryptionParameters, present: { controller in
|
||||
self.window?.rootViewController?.present(controller, animated: true, completion: nil)
|
||||
})
|
||||
|> `catch` { _ -> Signal<ImportedLegacyAccountEvent, NoError> in
|
||||
return Signal { subscriber in
|
||||
let alertView = UIAlertView(title: "", message: "An error occured while trying to upgrade application data. Would you like to logout?", delegate: self, cancelButtonTitle: "No", otherButtonTitles: "Yes")
|
||||
self.alertActions = (primary: {
|
||||
let statusPath = appGroupUrl.path + "/Documents/importcompleted"
|
||||
let _ = try? FileManager.default.createDirectory(atPath: appGroupUrl.path + "/Documents", withIntermediateDirectories: true, attributes: nil)
|
||||
let _ = try? Data().write(to: URL(fileURLWithPath: statusPath))
|
||||
subscriber.putNext(.result(nil))
|
||||
subscriber.putCompletion()
|
||||
}, other: {
|
||||
exit(0)
|
||||
})
|
||||
alertView.show()
|
||||
|
||||
return EmptyDisposable
|
||||
} |> runOn(Queue.mainQueue())
|
||||
}
|
||||
|> mapToSignal { event -> Signal<SharedApplicationContext, NoError> in
|
||||
switch event {
|
||||
case let .progress(type, value):
|
||||
Queue.mainQueue().async {
|
||||
if self.dataImportSplash == nil {
|
||||
self.dataImportSplash = makeLegacyDataImportSplash(theme: nil, strings: nil)
|
||||
self.dataImportSplash?.serviceAction = {
|
||||
self.debugPressed()
|
||||
}
|
||||
self.mainWindow.coveringView = self.dataImportSplash
|
||||
}
|
||||
self.dataImportSplash?.progress = (type, value)
|
||||
}
|
||||
return .complete()
|
||||
case let .result(temporaryId):
|
||||
Queue.mainQueue().async {
|
||||
if let _ = self.dataImportSplash {
|
||||
self.dataImportSplash = nil
|
||||
self.mainWindow.coveringView = nil
|
||||
}
|
||||
}
|
||||
if let temporaryId = temporaryId {
|
||||
Queue.mainQueue().after(1.0, {
|
||||
let statusPath = appGroupUrl.path + "/Documents/importcompleted"
|
||||
let _ = try? FileManager.default.createDirectory(atPath: appGroupUrl.path + "/Documents", withIntermediateDirectories: true, attributes: nil)
|
||||
let _ = try? Data().write(to: URL(fileURLWithPath: statusPath))
|
||||
})
|
||||
return sharedApplicationContext.sharedContext.accountManager.transaction { transaction -> SharedApplicationContext in
|
||||
transaction.setCurrentId(temporaryId)
|
||||
transaction.updateRecord(temporaryId, { record in
|
||||
if let record = record {
|
||||
return AccountRecord(id: record.id, attributes: record.attributes, temporarySessionId: nil)
|
||||
}
|
||||
return record
|
||||
})
|
||||
return sharedApplicationContext
|
||||
}
|
||||
} else {
|
||||
return .single(sharedApplicationContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let watchManagerArgumentsPromise = Promise<WatchManagerArguments?>()
|
||||
|
95
submodules/TelegramUI/Sources/LegacyDataImportSplash.swift
Normal file
95
submodules/TelegramUI/Sources/LegacyDataImportSplash.swift
Normal file
@ -0,0 +1,95 @@
|
||||
import Foundation
|
||||
import Display
|
||||
import AsyncDisplayKit
|
||||
import TelegramPresentationData
|
||||
import RadialStatusNode
|
||||
|
||||
public enum AccountImportProgressType {
|
||||
case generic
|
||||
case messages
|
||||
case media
|
||||
}
|
||||
|
||||
public protocol LegacyDataImportSplash: WindowCoveringView {
|
||||
var progress: (AccountImportProgressType, Float) { get set }
|
||||
var serviceAction: (() -> Void)? { get set }
|
||||
}
|
||||
|
||||
private final class LegacyDataImportSplashImpl: WindowCoveringView, LegacyDataImportSplash {
|
||||
private let theme: PresentationTheme?
|
||||
private let strings: PresentationStrings?
|
||||
|
||||
public var progress: (AccountImportProgressType, Float) = (.generic, 0.0) {
|
||||
didSet {
|
||||
if self.progress.0 != oldValue.0 {
|
||||
if let size = self.validSize {
|
||||
switch self.progress.0 {
|
||||
case .generic:
|
||||
self.textNode.attributedText = NSAttributedString(string: self.strings?.AppUpgrade_Running ?? "Optimizing...", font: Font.regular(17.0), textColor: self.theme?.list.itemPrimaryTextColor ?? .black)
|
||||
case .media:
|
||||
self.textNode.attributedText = NSAttributedString(string: "Optimizing cache", font: Font.regular(17.0), textColor: self.theme?.list.itemPrimaryTextColor ?? .black)
|
||||
case .messages:
|
||||
self.textNode.attributedText = NSAttributedString(string: "Optimizing database", font: Font.regular(17.0), textColor: self.theme?.list.itemPrimaryTextColor ?? .black)
|
||||
}
|
||||
self.updateLayout(size)
|
||||
}
|
||||
}
|
||||
self.progressNode.transitionToState(.progress(color: self.theme?.list.itemAccentColor ?? UIColor(rgb: 0x007ee5), lineWidth: 2.0, value: CGFloat(max(0.025, self.progress.1)), cancelEnabled: false, animateRotation: true), animated: false, completion: {})
|
||||
}
|
||||
}
|
||||
|
||||
public var serviceAction: (() -> Void)?
|
||||
|
||||
private let progressNode: RadialStatusNode
|
||||
private let textNode: ImmediateTextNode
|
||||
|
||||
private var validSize: CGSize?
|
||||
|
||||
public init(theme: PresentationTheme?, strings: PresentationStrings?) {
|
||||
self.theme = theme
|
||||
self.strings = strings
|
||||
|
||||
self.progressNode = RadialStatusNode(backgroundNodeColor: theme?.list.plainBackgroundColor ?? .white)
|
||||
self.textNode = ImmediateTextNode()
|
||||
self.textNode.maximumNumberOfLines = 0
|
||||
self.textNode.textAlignment = .center
|
||||
self.textNode.attributedText = NSAttributedString(string: self.strings?.AppUpgrade_Running ?? "Optimizing...", font: Font.regular(17.0), textColor: self.theme?.list.itemPrimaryTextColor ?? .black)
|
||||
|
||||
super.init(frame: CGRect())
|
||||
|
||||
self.backgroundColor = self.theme?.list.plainBackgroundColor ?? .white
|
||||
|
||||
self.addSubnode(self.progressNode)
|
||||
self.progressNode.isUserInteractionEnabled = false
|
||||
self.addSubnode(self.textNode)
|
||||
self.textNode.isUserInteractionEnabled = false
|
||||
|
||||
self.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGesture(_:))))
|
||||
}
|
||||
|
||||
required public init?(coder aDecoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override public func updateLayout(_ size: CGSize) {
|
||||
self.validSize = size
|
||||
|
||||
let progressSize = CGSize(width: 60.0, height: 60.0)
|
||||
|
||||
let textSize = self.textNode.updateLayout(CGSize(width: size.width - 20.0, height: .greatestFiniteMagnitude))
|
||||
|
||||
let progressFrame = CGRect(origin: CGPoint(x: floor((size.width - progressSize.width) / 2.0), y: floor((size.height - progressSize.height - 15.0 - textSize.height) / 2.0)), size: progressSize)
|
||||
self.progressNode.frame = progressFrame
|
||||
self.textNode.frame = CGRect(origin: CGPoint(x: floor((size.width - textSize.width) / 2.0), y: progressFrame.maxY + 15.0), size: textSize)
|
||||
}
|
||||
|
||||
@objc private func longPressGesture(_ recognizer: UILongPressGestureRecognizer) {
|
||||
if case .began = recognizer.state {
|
||||
self.serviceAction?()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func makeLegacyDataImportSplash(theme: PresentationTheme?, strings: PresentationStrings?) -> LegacyDataImportSplash {
|
||||
return LegacyDataImportSplashImpl(theme: theme, strings: strings)
|
||||
}
|
11
submodules/sqlcipher/BUILD
vendored
11
submodules/sqlcipher/BUILD
vendored
@ -16,11 +16,18 @@ objc_library(
|
||||
"PublicHeaders",
|
||||
],
|
||||
copts = [
|
||||
"-DDSQLITE_HAS_CODEC=1",
|
||||
"-DSQLITE_HAS_CODEC=1",
|
||||
"-DSQLCIPHER_CRYPTO_CC=1",
|
||||
"-DSQLITE_TEMP_STORE=2",
|
||||
"-DSQLITE_ENABLE_FTS5",
|
||||
"-DHAVE_USLEEP=1",
|
||||
"-DSQLITE_DEFAULT_MEMSTATUS=0",
|
||||
"-DNDEBUG",
|
||||
"-DSQLITE_OMIT_LOAD_EXTENSION",
|
||||
"-DSQLITE_OMIT_DECLTYPE",
|
||||
"-DSQLITE_OMIT_PROGRESS_CALLBACK",
|
||||
"-DSQLITE_OMIT_DEPRECATED",
|
||||
"-DNDEBUG=1",
|
||||
"-DSQLITE_MAX_MMAP_SIZE=0",
|
||||
],
|
||||
sdk_frameworks = [
|
||||
"Foundation",
|
||||
|
@ -1,11 +0,0 @@
|
||||
#ifndef sqlcipher_config_h
|
||||
#define sqlcipher_config_h
|
||||
|
||||
#define SQLITE_HAS_CODEC 1
|
||||
#define SQLCIPHER_CRYPTO_CC 1
|
||||
#define SQLITE_ENABLE_FTS5 1
|
||||
#define SQLITE_DEFAULT_MEMSTATUS 0
|
||||
#define NDEBUG 1
|
||||
#define SQLITE_MAX_MMAP_SIZE 0
|
||||
|
||||
#endif /* sqlcipher_config_h */
|
File diff suppressed because it is too large
Load Diff
@ -17,8 +17,7 @@
|
||||
*/
|
||||
#ifndef SQLITE3EXT_H
|
||||
#define SQLITE3EXT_H
|
||||
|
||||
#include <sqlcipher/sqlite3.h>
|
||||
#include "sqlite3.h"
|
||||
|
||||
/*
|
||||
** The following structure holds pointers to all of the SQLite API
|
||||
@ -320,6 +319,22 @@ struct sqlite3_api_routines {
|
||||
void(*xDestroy)(void*));
|
||||
/* Version 3.26.0 and later */
|
||||
const char *(*normalized_sql)(sqlite3_stmt*);
|
||||
/* Version 3.28.0 and later */
|
||||
int (*stmt_isexplain)(sqlite3_stmt*);
|
||||
int (*value_frombind)(sqlite3_value*);
|
||||
/* Version 3.30.0 and later */
|
||||
int (*drop_modules)(sqlite3*,const char**);
|
||||
/* Version 3.31.0 and later */
|
||||
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
|
||||
const char *(*uri_key)(const char*,int);
|
||||
const char *(*filename_database)(const char*);
|
||||
const char *(*filename_journal)(const char*);
|
||||
const char *(*filename_wal)(const char*);
|
||||
/* Version 3.32.0 and later */
|
||||
char *(*create_filename)(const char*,const char*,const char*,
|
||||
int,const char**);
|
||||
void (*free_filename)(char*);
|
||||
sqlite3_file *(*database_file_object)(const char*);
|
||||
};
|
||||
|
||||
/*
|
||||
@ -609,6 +624,21 @@ typedef int (*sqlite3_loadext_entry)(
|
||||
#define sqlite3_create_window_function sqlite3_api->create_window_function
|
||||
/* Version 3.26.0 and later */
|
||||
#define sqlite3_normalized_sql sqlite3_api->normalized_sql
|
||||
/* Version 3.28.0 and later */
|
||||
#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
|
||||
#define sqlite3_value_frombind sqlite3_api->value_frombind
|
||||
/* Version 3.30.0 and later */
|
||||
#define sqlite3_drop_modules sqlite3_api->drop_modules
|
||||
/* Version 3.31.0 and later */
|
||||
#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
|
||||
#define sqlite3_uri_key sqlite3_api->uri_key
|
||||
#define sqlite3_filename_database sqlite3_api->filename_database
|
||||
#define sqlite3_filename_journal sqlite3_api->filename_journal
|
||||
#define sqlite3_filename_wal sqlite3_api->filename_wal
|
||||
/* Version 3.32.0 and later */
|
||||
#define sqlite3_create_filename sqlite3_api->create_filename
|
||||
#define sqlite3_free_filename sqlite3_api->free_filename
|
||||
#define sqlite3_database_file_object sqlite3_api->database_file_object
|
||||
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
|
||||
|
||||
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
|
||||
|
1660
submodules/sqlcipher/PublicHeaders/sqlcipher/sqlite3session.h
Normal file
1660
submodules/sqlcipher/PublicHeaders/sqlcipher/sqlite3session.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,56 +0,0 @@
|
||||
//
|
||||
// SQLite.swift
|
||||
// https://github.com/stephencelis/SQLite.swift
|
||||
// Copyright (c) 2014-2015 Stephen Celis.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
@import Foundation;
|
||||
|
||||
typedef struct SQLiteHandle SQLiteHandle;
|
||||
typedef struct SQLiteContext SQLiteContext;
|
||||
typedef struct SQLiteValue SQLiteValue;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
typedef int (^_SQLiteBusyHandlerCallback)(int times);
|
||||
int _SQLiteBusyHandler(SQLiteHandle * handle, _SQLiteBusyHandlerCallback __nullable callback);
|
||||
|
||||
typedef void (^_SQLiteTraceCallback)(const char * SQL);
|
||||
void _SQLiteTrace(SQLiteHandle * handle, _SQLiteTraceCallback __nullable callback);
|
||||
|
||||
typedef void (^_SQLiteUpdateHookCallback)(int operation, const char * db, const char * table, long long rowid);
|
||||
void _SQLiteUpdateHook(SQLiteHandle * handle, _SQLiteUpdateHookCallback __nullable callback);
|
||||
|
||||
typedef int (^_SQLiteCommitHookCallback)();
|
||||
void _SQLiteCommitHook(SQLiteHandle * handle, _SQLiteCommitHookCallback __nullable callback);
|
||||
|
||||
typedef void (^_SQLiteRollbackHookCallback)();
|
||||
void _SQLiteRollbackHook(SQLiteHandle * handle, _SQLiteRollbackHookCallback __nullable callback);
|
||||
|
||||
typedef void (^_SQLiteCreateFunctionCallback)(SQLiteContext * context, int argc, SQLiteValue * __nonnull * __nonnull argv);
|
||||
int _SQLiteCreateFunction(SQLiteHandle * handle, const char * name, int argc, int deterministic, _SQLiteCreateFunctionCallback __nullable callback);
|
||||
|
||||
typedef int (^_SQLiteCreateCollationCallback)(const char * lhs, const char * rhs);
|
||||
int _SQLiteCreateCollation(SQLiteHandle * handle, const char * name, _SQLiteCreateCollationCallback __nullable callback);
|
||||
|
||||
typedef NSString * __nullable (^_SQLiteTokenizerNextCallback)(const char * input, int * inputOffset, int * inputLength);
|
||||
int _SQLiteRegisterTokenizer(SQLiteHandle * db, const char * module, const char * tokenizer, __nullable _SQLiteTokenizerNextCallback callback);
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@ -1,217 +0,0 @@
|
||||
//
|
||||
// SQLite.swift
|
||||
// https://github.com/stephencelis/SQLite.swift
|
||||
// Copyright (c) 2014-2015 Stephen Celis.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
#import "SQLite-Bridging.h"
|
||||
|
||||
#import "fts3_tokenizer.h"
|
||||
|
||||
static int __SQLiteBusyHandler(void * context, int tries) {
|
||||
return ((__bridge _SQLiteBusyHandlerCallback)context)(tries);
|
||||
}
|
||||
|
||||
int _SQLiteBusyHandler(SQLiteHandle * handle, _SQLiteBusyHandlerCallback callback) {
|
||||
if (callback) {
|
||||
return sqlite3_busy_handler((sqlite3 *)handle, __SQLiteBusyHandler, (__bridge void *)callback);
|
||||
} else {
|
||||
return sqlite3_busy_handler((sqlite3 *)handle, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void __SQLiteTrace(void * context, const char * SQL) {
|
||||
((__bridge _SQLiteTraceCallback)context)(SQL);
|
||||
}
|
||||
|
||||
void _SQLiteTrace(SQLiteHandle * handle, _SQLiteTraceCallback callback) {
|
||||
if (callback) {
|
||||
sqlite3_trace((sqlite3 *)handle, __SQLiteTrace, (__bridge void *)callback);
|
||||
} else {
|
||||
sqlite3_trace((sqlite3 *)handle, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void __SQLiteUpdateHook(void * context, int operation, const char * db, const char * table, long long rowid) {
|
||||
((__bridge _SQLiteUpdateHookCallback)context)(operation, db, table, rowid);
|
||||
}
|
||||
|
||||
void _SQLiteUpdateHook(SQLiteHandle * handle, _SQLiteUpdateHookCallback callback) {
|
||||
sqlite3_update_hook((sqlite3 *)handle, __SQLiteUpdateHook, (__bridge void *)callback);
|
||||
}
|
||||
|
||||
static int __SQLiteCommitHook(void * context) {
|
||||
return ((__bridge _SQLiteCommitHookCallback)context)();
|
||||
}
|
||||
|
||||
void _SQLiteCommitHook(SQLiteHandle * handle, _SQLiteCommitHookCallback callback) {
|
||||
sqlite3_commit_hook((sqlite3 *)handle, __SQLiteCommitHook, (__bridge void *)callback);
|
||||
}
|
||||
|
||||
static void __SQLiteRollbackHook(void * context) {
|
||||
((__bridge _SQLiteRollbackHookCallback)context)();
|
||||
}
|
||||
|
||||
void _SQLiteRollbackHook(SQLiteHandle * handle, _SQLiteRollbackHookCallback callback) {
|
||||
sqlite3_rollback_hook((sqlite3 *)handle, __SQLiteRollbackHook, (__bridge void *)callback);
|
||||
}
|
||||
|
||||
static void __SQLiteCreateFunction(sqlite3_context * context, int argc, sqlite3_value ** argv) {
|
||||
((__bridge _SQLiteCreateFunctionCallback)sqlite3_user_data(context))((SQLiteContext *)context, argc, (SQLiteValue **)argv);
|
||||
}
|
||||
|
||||
int _SQLiteCreateFunction(SQLiteHandle * handle, const char * name, int argc, int deterministic, _SQLiteCreateFunctionCallback callback) {
|
||||
if (callback) {
|
||||
int flags = SQLITE_UTF8;
|
||||
if (deterministic) {
|
||||
#ifdef SQLITE_DETERMINISTIC
|
||||
flags |= SQLITE_DETERMINISTIC;
|
||||
#endif
|
||||
}
|
||||
return sqlite3_create_function_v2((sqlite3 *)handle, name, -1, flags, (__bridge void *)callback, &__SQLiteCreateFunction, 0, 0, 0);
|
||||
} else {
|
||||
return sqlite3_create_function_v2((sqlite3 *)handle, name, 0, 0, 0, 0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static int __SQLiteCreateCollation(void * context, int len_lhs, const void * lhs, int len_rhs, const void * rhs) {
|
||||
return ((__bridge _SQLiteCreateCollationCallback)context)(lhs, rhs);
|
||||
}
|
||||
|
||||
int _SQLiteCreateCollation(SQLiteHandle * handle, const char * name, _SQLiteCreateCollationCallback callback) {
|
||||
if (callback) {
|
||||
return sqlite3_create_collation_v2((sqlite3 *)handle, name, SQLITE_UTF8, (__bridge void *)callback, &__SQLiteCreateCollation, 0);
|
||||
} else {
|
||||
return sqlite3_create_collation_v2((sqlite3 *)handle, name, 0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - FTS
|
||||
|
||||
typedef struct __SQLiteTokenizer {
|
||||
sqlite3_tokenizer base;
|
||||
__unsafe_unretained _SQLiteTokenizerNextCallback callback;
|
||||
} __SQLiteTokenizer;
|
||||
|
||||
typedef struct __SQLiteTokenizerCursor {
|
||||
void * base;
|
||||
const char * input;
|
||||
int inputOffset;
|
||||
int inputLength;
|
||||
int idx;
|
||||
} __SQLiteTokenizerCursor;
|
||||
|
||||
static NSMutableDictionary * __SQLiteTokenizerMap;
|
||||
|
||||
static int __SQLiteTokenizerCreate(int argc, const char * const * argv, sqlite3_tokenizer ** ppTokenizer) {
|
||||
__SQLiteTokenizer * tokenizer = (__SQLiteTokenizer *)sqlite3_malloc(sizeof(__SQLiteTokenizer));
|
||||
if (!tokenizer) {
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
memset(tokenizer, 0, sizeof(* tokenizer)); // FIXME: needed?
|
||||
|
||||
NSString * key = [NSString stringWithUTF8String:argv[0]];
|
||||
tokenizer->callback = [__SQLiteTokenizerMap objectForKey:key];
|
||||
if (!tokenizer->callback) {
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
*ppTokenizer = &tokenizer->base;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int __SQLiteTokenizerDestroy(sqlite3_tokenizer * pTokenizer) {
|
||||
sqlite3_free(pTokenizer);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int __SQLiteTokenizerOpen(sqlite3_tokenizer * pTokenizer, const char * pInput, int nBytes, sqlite3_tokenizer_cursor ** ppCursor) {
|
||||
__SQLiteTokenizerCursor * cursor = (__SQLiteTokenizerCursor *)sqlite3_malloc(sizeof(__SQLiteTokenizerCursor));
|
||||
if (!cursor) {
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
|
||||
cursor->input = pInput;
|
||||
cursor->inputOffset = 0;
|
||||
cursor->inputLength = 0;
|
||||
cursor->idx = 0;
|
||||
|
||||
*ppCursor = (sqlite3_tokenizer_cursor *)cursor;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int __SQLiteTokenizerClose(sqlite3_tokenizer_cursor * pCursor) {
|
||||
sqlite3_free(pCursor);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int __SQLiteTokenizerNext(sqlite3_tokenizer_cursor * pCursor, const char ** ppToken, int * pnBytes, int * piStartOffset, int * piEndOffset, int * piPosition) {
|
||||
__SQLiteTokenizerCursor * cursor = (__SQLiteTokenizerCursor *)pCursor;
|
||||
__SQLiteTokenizer * tokenizer = (__SQLiteTokenizer *)cursor->base;
|
||||
|
||||
cursor->inputOffset += cursor->inputLength;
|
||||
const char * input = cursor->input + cursor->inputOffset;
|
||||
const char * token = [tokenizer->callback(input, &cursor->inputOffset, &cursor->inputLength) cStringUsingEncoding:NSUTF8StringEncoding];
|
||||
if (!token) {
|
||||
return SQLITE_DONE;
|
||||
}
|
||||
|
||||
*ppToken = token;
|
||||
*pnBytes = (int)strlen(token);
|
||||
*piStartOffset = cursor->inputOffset;
|
||||
*piEndOffset = cursor->inputOffset + cursor->inputLength;
|
||||
*piPosition = cursor->idx++;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static const sqlite3_tokenizer_module __SQLiteTokenizerModule = {
|
||||
0,
|
||||
__SQLiteTokenizerCreate,
|
||||
__SQLiteTokenizerDestroy,
|
||||
__SQLiteTokenizerOpen,
|
||||
__SQLiteTokenizerClose,
|
||||
__SQLiteTokenizerNext
|
||||
};
|
||||
|
||||
int _SQLiteRegisterTokenizer(SQLiteHandle * db, const char * moduleName, const char * submoduleName, _SQLiteTokenizerNextCallback callback) {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
__SQLiteTokenizerMap = [NSMutableDictionary new];
|
||||
});
|
||||
|
||||
sqlite3_stmt * stmt;
|
||||
int status = sqlite3_prepare_v2((sqlite3 *)db, "SELECT fts3_tokenizer(?, ?)", -1, &stmt, 0);
|
||||
if (status != SQLITE_OK ){
|
||||
return status;
|
||||
}
|
||||
const sqlite3_tokenizer_module * pModule = &__SQLiteTokenizerModule;
|
||||
sqlite3_bind_text(stmt, 1, moduleName, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_blob(stmt, 2, &pModule, sizeof(pModule), SQLITE_STATIC);
|
||||
sqlite3_step(stmt);
|
||||
status = sqlite3_finalize(stmt);
|
||||
if (status != SQLITE_OK ){
|
||||
return status;
|
||||
}
|
||||
|
||||
[__SQLiteTokenizerMap setObject:[callback copy] forKey:[NSString stringWithUTF8String:submoduleName]];
|
||||
|
||||
return SQLITE_OK;
|
||||
}
|
@ -1,161 +0,0 @@
|
||||
/*
|
||||
** 2006 July 10
|
||||
**
|
||||
** The author disclaims copyright to this source code.
|
||||
**
|
||||
*************************************************************************
|
||||
** Defines the interface to tokenizers used by fulltext-search. There
|
||||
** are three basic components:
|
||||
**
|
||||
** sqlite3_tokenizer_module is a singleton defining the tokenizer
|
||||
** interface functions. This is essentially the class structure for
|
||||
** tokenizers.
|
||||
**
|
||||
** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
|
||||
** including customization information defined at creation time.
|
||||
**
|
||||
** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
|
||||
** tokens from a particular input.
|
||||
*/
|
||||
#ifndef _FTS3_TOKENIZER_H_
|
||||
#define _FTS3_TOKENIZER_H_
|
||||
|
||||
/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
|
||||
** If tokenizers are to be allowed to call sqlite3_*() functions, then
|
||||
** we will need a way to register the API consistently.
|
||||
*/
|
||||
#include "sqlite3.h"
|
||||
|
||||
/*
|
||||
** Structures used by the tokenizer interface. When a new tokenizer
|
||||
** implementation is registered, the caller provides a pointer to
|
||||
** an sqlite3_tokenizer_module containing pointers to the callback
|
||||
** functions that make up an implementation.
|
||||
**
|
||||
** When an fts3 table is created, it passes any arguments passed to
|
||||
** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
|
||||
** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
|
||||
** implementation. The xCreate() function in turn returns an
|
||||
** sqlite3_tokenizer structure representing the specific tokenizer to
|
||||
** be used for the fts3 table (customized by the tokenizer clause arguments).
|
||||
**
|
||||
** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
|
||||
** method is called. It returns an sqlite3_tokenizer_cursor object
|
||||
** that may be used to tokenize a specific input buffer based on
|
||||
** the tokenization rules supplied by a specific sqlite3_tokenizer
|
||||
** object.
|
||||
*/
|
||||
typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
|
||||
typedef struct sqlite3_tokenizer sqlite3_tokenizer;
|
||||
typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
|
||||
|
||||
struct sqlite3_tokenizer_module {
|
||||
|
||||
/*
|
||||
** Structure version. Should always be set to 0 or 1.
|
||||
*/
|
||||
int iVersion;
|
||||
|
||||
/*
|
||||
** Create a new tokenizer. The values in the argv[] array are the
|
||||
** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
|
||||
** TABLE statement that created the fts3 table. For example, if
|
||||
** the following SQL is executed:
|
||||
**
|
||||
** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
|
||||
**
|
||||
** then argc is set to 2, and the argv[] array contains pointers
|
||||
** to the strings "arg1" and "arg2".
|
||||
**
|
||||
** This method should return either SQLITE_OK (0), or an SQLite error
|
||||
** code. If SQLITE_OK is returned, then *ppTokenizer should be set
|
||||
** to point at the newly created tokenizer structure. The generic
|
||||
** sqlite3_tokenizer.pModule variable should not be initialized by
|
||||
** this callback. The caller will do so.
|
||||
*/
|
||||
int (*xCreate)(
|
||||
int argc, /* Size of argv array */
|
||||
const char *const*argv, /* Tokenizer argument strings */
|
||||
sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
|
||||
);
|
||||
|
||||
/*
|
||||
** Destroy an existing tokenizer. The fts3 module calls this method
|
||||
** exactly once for each successful call to xCreate().
|
||||
*/
|
||||
int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
|
||||
|
||||
/*
|
||||
** Create a tokenizer cursor to tokenize an input buffer. The caller
|
||||
** is responsible for ensuring that the input buffer remains valid
|
||||
** until the cursor is closed (using the xClose() method).
|
||||
*/
|
||||
int (*xOpen)(
|
||||
sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
|
||||
const char *pInput, int nBytes, /* Input buffer */
|
||||
sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
|
||||
);
|
||||
|
||||
/*
|
||||
** Destroy an existing tokenizer cursor. The fts3 module calls this
|
||||
** method exactly once for each successful call to xOpen().
|
||||
*/
|
||||
int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
|
||||
|
||||
/*
|
||||
** Retrieve the next token from the tokenizer cursor pCursor. This
|
||||
** method should either return SQLITE_OK and set the values of the
|
||||
** "OUT" variables identified below, or SQLITE_DONE to indicate that
|
||||
** the end of the buffer has been reached, or an SQLite error code.
|
||||
**
|
||||
** *ppToken should be set to point at a buffer containing the
|
||||
** normalized version of the token (i.e. after any case-folding and/or
|
||||
** stemming has been performed). *pnBytes should be set to the length
|
||||
** of this buffer in bytes. The input text that generated the token is
|
||||
** identified by the byte offsets returned in *piStartOffset and
|
||||
** *piEndOffset. *piStartOffset should be set to the index of the first
|
||||
** byte of the token in the input buffer. *piEndOffset should be set
|
||||
** to the index of the first byte just past the end of the token in
|
||||
** the input buffer.
|
||||
**
|
||||
** The buffer *ppToken is set to point at is managed by the tokenizer
|
||||
** implementation. It is only required to be valid until the next call
|
||||
** to xNext() or xClose().
|
||||
*/
|
||||
/* TODO(shess) current implementation requires pInput to be
|
||||
** nul-terminated. This should either be fixed, or pInput/nBytes
|
||||
** should be converted to zInput.
|
||||
*/
|
||||
int (*xNext)(
|
||||
sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
|
||||
const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
|
||||
int *piStartOffset, /* OUT: Byte offset of token in input buffer */
|
||||
int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
|
||||
int *piPosition /* OUT: Number of tokens returned before this one */
|
||||
);
|
||||
|
||||
/***********************************************************************
|
||||
** Methods below this point are only available if iVersion>=1.
|
||||
*/
|
||||
|
||||
/*
|
||||
** Configure the language id of a tokenizer cursor.
|
||||
*/
|
||||
int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
|
||||
};
|
||||
|
||||
struct sqlite3_tokenizer {
|
||||
const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
|
||||
/* Tokenizer implementations will typically add additional fields */
|
||||
};
|
||||
|
||||
struct sqlite3_tokenizer_cursor {
|
||||
sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
|
||||
/* Tokenizer implementations will typically add additional fields */
|
||||
};
|
||||
|
||||
int fts3_global_term_cnt(int iTerm, int iCol);
|
||||
int fts3_term_cnt(int iTerm, int iCol);
|
||||
|
||||
|
||||
#endif /* _FTS3_TOKENIZER_H_ */
|
@ -1,11 +0,0 @@
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
//! Project version number for sqlcipher.
|
||||
FOUNDATION_EXPORT double sqlcipherVersionNumber;
|
||||
|
||||
//! Project version string for sqlcipher.
|
||||
FOUNDATION_EXPORT const unsigned char sqlcipherVersionString[];
|
||||
|
||||
#import <sqlcipher/sqlcipher_config.h>
|
||||
#import <sqlcipher/sqlite3.h>
|
||||
#import <sqlcipher/sqlite3ext.h>
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user