Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Plugins/MongoDBDriverPlugin/MongoDBConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ final class MongoDBConnection: @unchecked Sendable {
projection: String? = nil,
skip: Int,
limit: Int
) async throws -> [[String: Any]] {
) async throws -> (docs: [[String: Any]], isTruncated: Bool) {
#if canImport(CLibMongoc)
resetCancellation()
return try await pluginDispatchAsync(on: queue) { [self] in
Expand All @@ -387,7 +387,7 @@ final class MongoDBConnection: @unchecked Sendable {
#endif
}

func aggregate(database: String, collection: String, pipeline: String) async throws -> [[String: Any]] {
func aggregate(database: String, collection: String, pipeline: String) async throws -> (docs: [[String: Any]], isTruncated: Bool) {
#if canImport(CLibMongoc)
resetCancellation()
return try await pluginDispatchAsync(on: queue) { [self] in
Expand Down Expand Up @@ -595,7 +595,7 @@ private extension MongoDBConnection {
func findSync(
client: OpaquePointer, database: String, collection: String,
filter: String, sort: String?, projection: String?, skip: Int, limit: Int
) throws -> [[String: Any]] {
) throws -> (docs: [[String: Any]], isTruncated: Bool) {
try checkCancelled()

guard let filterBson = jsonToBson(filter) else {
Expand Down Expand Up @@ -640,7 +640,7 @@ private extension MongoDBConnection {

func aggregateSync(
client: OpaquePointer, database: String, collection: String, pipeline: String
) throws -> [[String: Any]] {
) throws -> (docs: [[String: Any]], isTruncated: Bool) {
try checkCancelled()

guard let pipelineBson = jsonToBson(pipeline) else {
Expand Down Expand Up @@ -833,14 +833,15 @@ private extension MongoDBConnection {
}
defer { mongoc_cursor_destroy(cursor) }

return try iterateCursor(cursor)
return try iterateCursor(cursor).docs
}

func iterateCursor(_ cursor: OpaquePointer) throws -> [[String: Any]] {
func iterateCursor(_ cursor: OpaquePointer) throws -> (docs: [[String: Any]], isTruncated: Bool) {
try checkCancelled()

var results: [[String: Any]] = []
var docPtr: OpaquePointer?
var truncated = false

while mongoc_cursor_next(cursor, &docPtr) {
try checkCancelled()
Expand All @@ -850,6 +851,7 @@ private extension MongoDBConnection {
}

if results.count >= PluginRowLimits.defaultMax {
truncated = true
logger.warning("Result set truncated at \(PluginRowLimits.defaultMax) documents")
break
}
Expand All @@ -859,7 +861,7 @@ private extension MongoDBConnection {
if mongoc_cursor_error(cursor, &error) {
throw makeError(error)
}
return results
return (docs: results, isTruncated: truncated)
}
}
#endif
Expand Down
25 changes: 12 additions & 13 deletions Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ final class MongoDBPluginDriver: PluginDatabaseDriver {
case .findOne:
return 1
case .aggregate(let collection, let pipeline):
let docs = try await conn.aggregate(database: db, collection: collection, pipeline: pipeline)
return docs.count
let result = try await conn.aggregate(database: db, collection: collection, pipeline: pipeline)
return result.docs.count
case .countDocuments(let collection, let filter):
let count = try await conn.countDocuments(database: db, collection: collection, filter: filter)
return Int(count)
Expand All @@ -148,12 +148,12 @@ final class MongoDBPluginDriver: PluginDatabaseDriver {
case .find(let collection, let filter, var options):
options.skip = offset
options.limit = limit
let docs = try await conn.find(
let result = try await conn.find(
database: db, collection: collection, filter: filter,
sort: options.sort, projection: options.projection,
skip: offset, limit: limit
)
return buildPluginResult(from: docs, startTime: startTime)
return buildPluginResult(from: result.docs, startTime: startTime, isTruncated: result.isTruncated)
default:
return try await executeOperation(operation, connection: conn, startTime: startTime)
}
Expand All @@ -178,7 +178,7 @@ final class MongoDBPluginDriver: PluginDatabaseDriver {
let docs = try await conn.find(
database: currentDb, collection: table,
filter: "{}", sort: nil, projection: nil, skip: 0, limit: 500
)
).docs

if docs.isEmpty {
return [
Expand Down Expand Up @@ -527,30 +527,29 @@ final class MongoDBPluginDriver: PluginDatabaseDriver {

switch operation {
case .find(let collection, let filter, let options):
let docs = try await conn.find(
let result = try await conn.find(
database: db, collection: collection, filter: filter,
sort: options.sort, projection: options.projection,
skip: options.skip ?? 0, limit: options.limit ?? PluginRowLimits.defaultMax
)
if docs.isEmpty {
if result.docs.isEmpty {
return PluginQueryResult(
columns: ["_id"], columnTypeNames: ["ObjectId"],
rows: [], rowsAffected: 0, executionTime: Date().timeIntervalSince(startTime)
)
}
let truncated = docs.count >= PluginRowLimits.defaultMax
return buildPluginResult(from: docs, startTime: startTime, isTruncated: truncated)
return buildPluginResult(from: result.docs, startTime: startTime, isTruncated: result.isTruncated)

case .findOne(let collection, let filter):
let docs = try await conn.find(
let result = try await conn.find(
database: db, collection: collection, filter: filter,
sort: nil, projection: nil, skip: 0, limit: 1
)
return buildPluginResult(from: docs, startTime: startTime)
return buildPluginResult(from: result.docs, startTime: startTime)

case .aggregate(let collection, let pipeline):
let docs = try await conn.aggregate(database: db, collection: collection, pipeline: pipeline)
return buildPluginResult(from: docs, startTime: startTime)
let result = try await conn.aggregate(database: db, collection: collection, pipeline: pipeline)
return buildPluginResult(from: result.docs, startTime: startTime, isTruncated: result.isTruncated)

case .countDocuments(let collection, let filter):
let count = try await conn.countDocuments(database: db, collection: collection, filter: filter)
Expand Down
4 changes: 2 additions & 2 deletions Plugins/RedisDriverPlugin/RedisCommandParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ extension RedisParseError: PluginDriverError {
var pluginErrorMessage: String {
switch self {
case .emptySyntax: return String(localized: "Empty Redis command")
case .invalidArgument(let msg): return msg
case .missingArgument(let msg): return msg
case .invalidArgument(let msg): return String(localized: "Invalid argument: \(msg)")
case .missingArgument(let msg): return String(localized: "Missing argument: \(msg)")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Plugins/TableProPluginKit/PluginConcurrencySupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public func pluginDispatchAsync(
}
}

public func pluginDispatchAsync<T: Sendable>(
public func pluginDispatchAsyncCancellable<T: Sendable>(
on queue: DispatchQueue,
cancellationCheck: (@Sendable () -> Bool)? = nil,
execute work: @escaping @Sendable () throws -> T
Expand Down
4 changes: 2 additions & 2 deletions Plugins/TableProPluginKit/PluginDriverError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public extension PluginDriverError {

var errorDescription: String? {
var desc = pluginErrorMessage
if let code = pluginErrorCode {
if let code = pluginErrorCode, code != 0 {
desc = "[\(code)] \(desc)"
}
if let state = pluginSqlState {
desc += " (SQLSTATE: \(state))"
}
if let detail = pluginErrorDetail, !detail.isEmpty {
desc += "\n\(detail)"
desc += "\nDetail: \(detail)"
}
return desc
}
Expand Down
6 changes: 3 additions & 3 deletions TablePro/Views/Toolbar/TableProToolbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ struct TableProToolbar: ViewModifier {
: state.databaseType == .redis ? "Preview Commands (⌘⇧P)"
: "Preview SQL (⌘⇧P)")
.disabled(!state.hasPendingChanges || state.connectionState != .connected)
.popover(isPresented: $state.showSQLReviewPopover) {
SQLReviewPopover(statements: state.previewStatements, databaseType: state.databaseType)
}
}

ToolbarItem(placement: .primaryAction) {
Expand Down Expand Up @@ -179,9 +182,6 @@ struct TableProToolbar: ViewModifier {
}
}
}
.popover(isPresented: $state.showSQLReviewPopover) {
SQLReviewPopover(statements: state.previewStatements, databaseType: state.databaseType)
}
.onReceive(NotificationCenter.default.publisher(for: .openConnectionSwitcher)) { _ in
showConnectionSwitcher = true
}
Expand Down