Skip to content
Closed
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
5 changes: 4 additions & 1 deletion drivers/age-swift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@

# age-swift

A description of this package.
PostgresClientKit provides a high-performance and scalable solution for accessing PostgreSQL databases, which are used as the storage backend for Apache Age. The driver leverages the features of PostgresClientKit to provide a reliable and efficient interface for accessing Apache Age's graph data.


With Apache Age Swift Driver, developers can leverage the power of Apache Age's graph processing capabilities to build graph-based applications in Swift. This can be particularly useful for applications that involve social networks, recommendation systems, fraud detection, and other use cases where relationships between entities are important.
57 changes: 44 additions & 13 deletions drivers/age-swift/Sources/age-swift/age.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,30 @@
import PostgresClientKit


func setUpAge(connection:Connection){
func setUpAge(connection:Connection, graphName: String){
do{
try connection.prepareStatement(text: "Load 'age';").execute()
try connection.prepareStatement(text: "SET search_path = ag_catalog, '$user', public;").execute()
querySQL(statement: "Load 'age';", connection: connection)
querySQL(statement: "SET search_path = ag_catalog, '$user', public;", connection: connection)


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
let columns = try row.get().columns
oid = try columns[0].int()
break;
}



if oid == nil{
// Will raise exception over here
}

}catch{
print(error)
}

checkGraphCreated(connection: connection, graphName: graphName)
}


Expand Down Expand Up @@ -71,7 +73,33 @@ func connectDatabase(connectionParam:[String:Any]) -> Connection?{
}


func checkGraphCreated(connection:Connection, graphName:String){
do{
let rows = fetchSQL(statement: "SELECT count(*) FROM ag_graph WHERE name='" + graphName + "'", connection: connection);
print(rows)
let row_counts = rows[0] as! [PostgresValue];
let count = try row_counts[0].int();
if count < 1{
querySQL(statement: "SELECT create_graph('" + graphName + "');", connection: connection);
}
}catch{
print(error)
}
}


func querySQL(statement:String, connection:Connection){
do{
try connection.prepareStatement(text: statement).execute()
}catch{
print(error)
}
}



func fetchSQL(statement:String, connection:Connection)-> [Any]{
var rows:[Any] = [];
do{
let statement = try connection.prepareStatement(text: statement)
defer { statement.close() }
Expand All @@ -80,14 +108,13 @@ func querySQL(statement:String, connection:Connection){
defer { cursor.close() }

for row in cursor {
let columns = try row.get().columns
columns.forEach{column in
print(column)
}
}
rows.append(try row.get().columns);
}
}catch{
print(error)
}

return rows;
}

class Age{
Expand All @@ -102,14 +129,18 @@ class Age{

func connect(connectionParam:[String:Any], graph:String){
self.connection = connectDatabase(connectionParam: connectionParam)
setUpAge(connection:self.connection!)
setUpAge(connection:self.connection!, graphName: graph);
}

func execSQL(statement:String){
if self.connection == nil{
return print("The connection is not yet established")
}
return querySQL(statement: statement, connection: self.connection!)
let rows = fetchSQL(statement: statement, connection: self.connection!)

for column in rows{
print(column)
}
}


Expand Down
8 changes: 4 additions & 4 deletions drivers/age-swift/Tests/age-swiftTests/age_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import XCTest
@testable import age_swift

let host = "127.0.0.1";
let port = 5432 ;
let port = 5432;
let user = "root";
let db_name = "demo";

Expand All @@ -34,10 +34,10 @@ final class age_swiftTests: XCTestCase {

let connection_param:[String:Any] = ["host":host,"port":port, "user":user, "dbname":db_name]

age.connect(connectionParam: connection_param, graph: "hello")
age.connect(connectionParam: connection_param, graph: "demo")

let text = "SELECT 1;"
let text = "SELECT * from ag_graph;"

//age.execSQL(statement: text)
age.execSQL(statement: text)
}
}