This is an automated email from the ASF dual-hosted git repository.
dehowef pushed a commit to branch AGE_driver_swift
in repository https://gitbox.apache.org/repos/asf/age.git
The following commit(s) were added to refs/heads/AGE_driver_swift by this push:
new 4a51854 Setup Apache Age Extension in Swift and added test cases
(#655)
4a51854 is described below
commit 4a51854faa8b990e4af750c4125fd9f7091e3be3
Author: Fahad Zaheer <[email protected]>
AuthorDate: Thu Feb 9 11:52:48 2023 +0500
Setup Apache Age Extension in Swift and added test cases (#655)
---
.gitignore | 1 +
drivers/age-swift/Package.resolved | 32 ++++++
drivers/age-swift/Package.swift | 30 ++++++
drivers/age-swift/README.md | 20 ++++
drivers/age-swift/Sources/age-swift/age.swift | 116 +++++++++++++++++++++
.../Tests/age-swiftTests/age_swiftTests.swift | 43 ++++++++
6 files changed, 242 insertions(+)
diff --git a/.gitignore b/.gitignore
index 78e9912..5742a53 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@ build.sh
.idea
.deps
.DS_Store
+drivers/age-swift/.swiftpm
\ No newline at end of file
diff --git a/drivers/age-swift/Package.resolved
b/drivers/age-swift/Package.resolved
new file mode 100644
index 0000000..4b0ccc9
--- /dev/null
+++ b/drivers/age-swift/Package.resolved
@@ -0,0 +1,32 @@
+{
+ "pins" : [
+ {
+ "identity" : "bluesocket",
+ "kind" : "remoteSourceControl",
+ "location" : "https://github.com/Kitura/BlueSocket.git",
+ "state" : {
+ "revision" : "7b23a867008e0027bfd6f4d398d44720707bc8ca",
+ "version" : "2.0.4"
+ }
+ },
+ {
+ "identity" : "bluesslservice",
+ "kind" : "remoteSourceControl",
+ "location" : "https://github.com/Kitura/BlueSSLService",
+ "state" : {
+ "revision" : "b27a94d063962dfa1bba9f79814c4ef202cf33a4",
+ "version" : "2.0.2"
+ }
+ },
+ {
+ "identity" : "postgresclientkit",
+ "kind" : "remoteSourceControl",
+ "location" : "https://github.com/codewinsdotcom/PostgresClientKit",
+ "state" : {
+ "revision" : "356ffe0c43722f192d796300557b3b530a0baebc",
+ "version" : "1.5.0"
+ }
+ }
+ ],
+ "version" : 2
+}
diff --git a/drivers/age-swift/Package.swift b/drivers/age-swift/Package.swift
new file mode 100644
index 0000000..b2c9def
--- /dev/null
+++ b/drivers/age-swift/Package.swift
@@ -0,0 +1,30 @@
+// swift-tools-version: 5.7
+// The swift-tools-version declares the minimum version of Swift required to
build this package.
+
+import PackageDescription
+
+let package = Package(
+ name: "age-swift",
+ products: [
+ // Products define the executables and libraries a package produces,
and make them visible to other packages.
+ .library(
+ name: "age-swift",
+ targets: ["age-swift"]),
+ ],
+ dependencies: [
+ // Dependencies declare other packages that this package depends on.
+ // .package(url: /* package url */, from: "1.0.0"),
+ .package(url: "https://github.com/codewinsdotcom/PostgresClientKit",
from: "1.0.0"),
+
+ ],
+ targets: [
+ // Targets are the basic building blocks of a package. A target can
define a module or a test suite.
+ // Targets can depend on other targets in this package, and on
products in packages this package depends on.
+ .target(
+ name: "age-swift",
+ dependencies: ["PostgresClientKit"]),
+ .testTarget(
+ name: "age-swiftTests",
+ dependencies: ["age-swift"]),
+ ]
+)
diff --git a/drivers/age-swift/README.md b/drivers/age-swift/README.md
new file mode 100644
index 0000000..38db0c3
--- /dev/null
+++ b/drivers/age-swift/README.md
@@ -0,0 +1,20 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# age-swift
+
+A description of this package.
diff --git a/drivers/age-swift/Sources/age-swift/age.swift
b/drivers/age-swift/Sources/age-swift/age.swift
new file mode 100644
index 0000000..633c46f
--- /dev/null
+++ b/drivers/age-swift/Sources/age-swift/age.swift
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import PostgresClientKit
+
+
+func setUpAge(connection:Connection){
+ do{
+ try connection.prepareStatement(text: "Load 'age';").execute()
+ try connection.prepareStatement(text: "SET search_path = ag_catalog,
'$user', public;").execute()
+
+ let cursor = try connection.prepareStatement(text: "SELECT typelem
FROM pg_type WHERE typname='_agtype'").execute();
+ defer { cursor.close() }
+ var oid:Int? = nil;
+ for row in cursor {
+ let columns = try row.get().columns
+ oid = try columns[0].int()
+ break;
+ }
+
+
+ if oid == nil{
+ // Will raise exception over here
+ }
+
+ }catch{
+ print(error)
+ }
+}
+
+
+func connectDatabase(connectionParam:[String:Any]) -> Connection?{
+ do {
+ var configuration = PostgresClientKit.ConnectionConfiguration()
+ configuration.host = connectionParam["host"] as? String ?? "localhost"
+ configuration.port = connectionParam["port"] as? Int ?? 5432
+ configuration.user = connectionParam["user"] as? String ?? "postgres"
+ configuration.ssl = false
+
+ let key_exist = connectionParam["password"] != nil
+
+ if key_exist{
+ configuration.credential = .scramSHA256(password:
connectionParam["password"] as? String ?? "postgres")
+ }
+
+ configuration.database = connectionParam["dbname"] as? String ?? "postgres"
+
+ let connection = try PostgresClientKit.Connection(configuration:
configuration)
+
+ return connection
+ } catch {
+ print(error) // better error handling goes here
+ }
+ return nil;
+}
+
+
+func querySQL(statement:String, connection:Connection){
+ do{
+ let statement = try connection.prepareStatement(text: statement)
+ defer { statement.close() }
+
+ let cursor = try statement.execute()
+ defer { cursor.close() }
+
+ for row in cursor {
+ let columns = try row.get().columns
+ columns.forEach{column in
+ print(column)
+ }
+ }
+ }catch{
+ print(error)
+ }
+}
+
+class Age{
+ var connection:Connection?;
+ var graphName:String;
+
+ init(){
+ self.connection = nil;
+ self.graphName = "";
+ }
+
+
+ func connect(connectionParam:[String:Any], graph:String){
+ self.connection = connectDatabase(connectionParam: connectionParam)
+ setUpAge(connection:self.connection!)
+ }
+
+ func execSQL(statement:String){
+ if self.connection == nil{
+ return print("The connection is not yet established")
+ }
+ return querySQL(statement: statement, connection: self.connection!)
+ }
+
+
+}
diff --git a/drivers/age-swift/Tests/age-swiftTests/age_swiftTests.swift
b/drivers/age-swift/Tests/age-swiftTests/age_swiftTests.swift
new file mode 100644
index 0000000..fcbb72d
--- /dev/null
+++ b/drivers/age-swift/Tests/age-swiftTests/age_swiftTests.swift
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import XCTest
+@testable import age_swift
+
+let host = "127.0.0.1";
+let port = 5432 ;
+let user = "root";
+let db_name = "demo";
+
+final class age_swiftTests: XCTestCase {
+ func testExample() throws {
+ // This is an example of a functional test case.
+ // Use XCTAssert and related functions to verify your tests produce
the correct
+ // results.
+ let age = Age()
+
+ let connection_param:[String:Any] = ["host":host,"port":port,
"user":user, "dbname":db_name]
+
+ age.connect(connectionParam: connection_param, graph: "hello")
+
+ let text = "SELECT 1;"
+
+ //age.execSQL(statement: text)
+ }
+}