Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

share open connection #28

Closed
AMBIENTE1 opened this issue Oct 14, 2020 · 8 comments
Closed

share open connection #28

AMBIENTE1 opened this issue Oct 14, 2020 · 8 comments

Comments

@AMBIENTE1
Copy link

Hi there,
in my swift project I call an example right at the beginning:

do {
var configuration = PostgresClientKit.ConnectionConfiguration()
configuration.host = "127.0.0.1"
configuration.database = "example"
configuration.user = "bob"
configuration.credential = .scramSHA256(password: "welcome1")

let connection = try PostgresClientKit.Connection(configuration: configuration)
let text = "SELECT start();"
try connection.prepareStatement(text: text).execute()

} catch {
print(error)
}

I need the connection not to be terminated and continue in another function:

func test() {
do {
let text = "SELECT * FROM foo();"
try connection.prepareStatement(text: text).execute(). // error: Cannot find 'connection' in scope
} catch {
print(error)
}

My problem: I don't know how set "connection" as global var
Can you please advise me how to solve this?

@pitfield
Copy link
Member

This is a general programming question that gets into object-oriented design. So you might want to consult Apple's Swift tutorial or other resources on the web. There are many approaches to do what you want. Here is one way:

  • create a Swift type (a class or struct)
  • in that type, add a stored property of type Connection
  • in that type, add an initializer that sets that stored property
  • move your test() function into that type

@AMBIENTE1
Copy link
Author

Thank you for answer. I will try to ask differently.
The question is: How declare variable for connection = ???
then use: connection = try PostgresClientKit.Connection(configuration: configuration)
and after I can use anywhere in project: try connection.prepareStatement(text: text).execute()

@AMBIENTE1
Copy link
Author

AMBIENTE1 commented Oct 17, 2020

I tried use struct, but still I have problem with declare conn - Value of type '[conn]' has no member 'prepareStatement'
Any idea?

import UIKit
import PostgresClientKit

struct conn {
let conn : Connection
}
var myconnection = conn

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    do {
        var configuration = PostgresClientKit.ConnectionConfiguration()
        configuration.host = "127.0.0.1"
        configuration.database = "example"
        configuration.user = "bob"
        configuration.credential = .scramSHA256(password: "welcome1")

        let aaa = try PostgresClientKit.Connection(configuration: configuration)
        let bbb = conn ( conn: aaa)
        myconnection.append(bbb)
        
        let text = "SELECT start();"
        try myconnection.prepareStatement(text: text).execute()
        
        
    } catch {
        print(error) // better error handling goes here
    }
    
    
}

@AMBIENTE1
Copy link
Author

Uff, try myconnection[0].conn.prepareStatement(text: text).execute()
my work is done :)

@AMBIENTE1
Copy link
Author

I'm sorry, my problem is still not resolved. My function start() opens session in Postgres database and I need to make this session still open. I checked the function prepareStatement, where is written "Any previous Cursor for this Connection is closed.". Is there a solution to keep the session open?

@pitfield pitfield reopened this Oct 19, 2020
@pitfield
Copy link
Member

Please keep the discussion in this issue, rather than opening a new issue.

@pitfield
Copy link
Member

Here is my understanding of what you want to do: You want to create a connection, and then be able to use that connection in various places in your application.

Here is a simple way to do this:

struct Database {

    private static var conn: Connection? = nil

    static func connection() throws -> Connection {

        if conn == nil {
            var configuration = PostgresClientKit.ConnectionConfiguration()
            configuration.host = "127.0.0.1"
            configuration.database = "example"
            configuration.user = "bob"
            configuration.credential = .scramSHA256(password: "welcome1")

            conn = try Connection(configuration: configuration)
        }
    
        return conn!
    }
}

func test() {
    do {
        let text = "SELECT * FROM weather;"
        let cursor = try Database.connection().prepareStatement(text: text).execute()
        // ...and process the results
    } catch {
        print(error)
    }
}

With this simple approach, you need to ensure that only one thread uses the connection at a time, and you need to handle the connection dying, for example because of a transient network issue.

Here is a more sophisticated approach:

https://github.com/pitfield/PostgresClientKit-iOS-Example

This uses a ConnectionPool. The README describes the benefits of doing this.

@AMBIENTE1
Copy link
Author

Thank you so much for the solution!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants