[Gambas-user] hConnection = New Connection ( [ DatabaseURL As String ] ) Clarification

Bruce bbruen at ...2308...
Wed Aug 28 10:32:32 CEST 2013


First off, thanks everyone for your answers - all are correct.

My original question was purely about the syntax of the Connection
constructor, specifically about the optional DatabaseURL parameter.

Clarification 1: According to the help there are three ways to
initialise a connection:
a) by directly setting the connection properties, (as Paul describes)
b) by using a predefined connection, (as Fabian describes)
c) by using the constructor DatabasURL parameter (as Tobi describes).

Regarding the latter, the help is somewhat silent as to the syntax of
that parameter.  Thanks to Tobi, I now see it to be:

        $TYPE://$USER@$HOST:$PORT/$DB

Clarification 2: This syntax also provides a clue that this method can
only work where the underlying database is a server process e.g. mysql
or postgresql and I don't think it can work for sqlite. 

In fact, contrary to Tobi's experience, I still cannot get this to work
(for a postgresql database).  Here I have a server machine "bluecow"
that has three postgresql server processes running on it (called say
"prod", "dev" and "test" each using a separate port). So, if I
understand Tobi's answer:

        $conn = New Connection("postgresql://bb@...2711...:5432/horse")

where the database name is "horse" and the dev server is using port
5432, should create a valid connection, but I still get this "Malformed
URL" error??? 

I have no problem connecting to said database using the direct or
predefined methods. Further, looking at the CConnection.c code all the
constructor appears to do is split the string parameter and populate the
connection properties. It does not appear to validate the values
provided for those properties. In other words, I would expect that
something like:

        $conn = New Connection("badtype://xyz@...3178...:15432/nosuchdb")

should still work (albeit creating an invalid connection). However, this
still gives the malformed URL error.

Clarification 3: Fabian was able to read between the lines and provide
the clue to my real problem. For predefined connections, i.e. the ones
defined in the IDE, the proper way to set up the connection is:

        $conn = Connections["conn_name"]

In other words, the key is just the name of the connection as seen in
the IDE.  This works fine for connections defined in the current
project.

FINALLY(!) here's my real problem.

What I am trying to do is use a predefined connection in a different
gambas project.  In other words, I want to get at the
".../otherproject/.connection/someconn.connection" file inside another
gambas project.

Why would anyone in their right mind want to do that?  Well...
I have a library that is used by "many" apps that manages database
access.  The library is database independent, i.e. it does not know the
connection parameters that are to be used.  The apps define the
database. In other words, App1.gambas uses "this" database and
App2.gambas uses "that" database. So I need the library to be able to
use the connection files in the particular "AppX" project.  (This is
simplifying things a bit as when we get to executable stage, the
connection is defined inside the executable archive. But I have solved
that part of the problem by copying the file into the runtime /tmp
directory.)

I suppose the final answer is that I need to read the /tmp/.../xyz.
connection file and create the connection using the direct method.  What
I was hoping for was a way to do something like:

        sConnFile = File.Path(Temp()) &/ "connmain.connection"
        $conn = Connections[sConnFile]

In other words: 
   Connections[path as String]

but it looks like this is not possible.







More information about the User mailing list