forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoClient.swift
102 lines (86 loc) · 2.45 KB
/
MongoClient.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// MongoClient.swift
// MongoDB
//
// Created by Kyle Jessup on 2015-11-19.
// Copyright © 2015 PerfectlySoft. All rights reserved.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
import libmongoc
public enum MongoResult {
case Success
case Error(UInt32, UInt32, String)
case ReplyDoc(BSON)
case ReplyInt(Int)
case ReplyCollection(MongoCollection)
static func fromError(error: bson_error_t) -> MongoResult {
var vError = error
let message = withUnsafePointer(&vError.message) {
String.fromCString(UnsafePointer($0))!
}
return .Error(error.domain, error.code, message)
}
}
public enum MongoClientError: ErrorType {
case InitError(String)
}
public class MongoClient {
var ptr: COpaquePointer
public typealias Result = MongoResult
public init(uri: String) throws {
self.ptr = mongoc_client_new(uri)
if ptr == nil {
throw MongoClientError.InitError("Could not parse URI '\(uri)'")
}
}
deinit {
close()
}
public func close() {
if self.ptr != nil {
mongoc_client_destroy(self.ptr)
self.ptr = nil
}
}
public func getCollection(databaseName: String, collectionName: String) -> MongoCollection {
return MongoCollection(client: self, databaseName: databaseName, collectionName: collectionName)
}
public func getDatabase(databaseName: String) -> MongoDatabase {
return MongoDatabase(client: self, databaseName: databaseName)
}
public func serverStatus() -> Result {
var error = bson_error_t()
let readPrefs = mongoc_read_prefs_new(MONGOC_READ_PRIMARY)
defer {
mongoc_read_prefs_destroy(readPrefs)
}
let bson = BSON()
guard mongoc_client_get_server_status(self.ptr, readPrefs, bson.doc, &error) else {
return Result.fromError(error)
}
return .ReplyDoc(bson)
}
public func databaseNames() -> [String] {
let names = mongoc_client_get_database_names(self.ptr, nil)
var ret = [String]()
if names != nil {
var curr = names
while curr[0] != nil {
ret.append(String.fromCString(curr.memory)!)
curr = curr.successor()
}
bson_strfreev(names)
}
return ret
}
}