Mailing List Archive

Database connection in a file system product
Is it possible to store a database connection (i.e. a "Z MySQL Database
Connection") in a file system product ? How ?

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Database connection in a file system product [ In reply to ]
--On 27. April 2006 10:54:23 +0200 Jonas Nielsen <jonasn@mail.tele.dk>
wrote:

> Is it possible to store a database connection (i.e. a "Z MySQL Database
> Connection") in a file system product ? How ?


What is the sense? You can not store persistent objects. You might store
a reference to the object using some _v_ variables to avoid getting
the DA everytime through acquisition (this makes only sense
if you have performance issues, do you?).

-aj


--
ZOPYX Ltd. & Co. KG - Charlottenstr. 37/1 - 72070 Tübingen - Germany
Web: www.zopyx.com - Email: info@zopyx.com - Phone +49 - 7071 - 793376
E-Publishing, Python, Zope & Plone development, Consulting
Re: Database connection in a file system product [ In reply to ]
Andreas Jung wrote:
> Jonas Nielsen wrote:
>
>> Is it possible to store a database connection (i.e. a "Z MySQL Database
>> Connection") in a file system product ? How ?
>
> What is the sense? You can not store persistent objects. You might store
> a reference to the object using some _v_ variables to avoid getting
> the DA everytime through acquisition (this makes only sense
> if you have performance issues, do you?).

The sense is that I would like to find an easy way to install my product
without setting up the database connection manually. I guess the way to
do it is to add some code in install.py then ?

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Re: Database connection in a file system product [ In reply to ]
--On 27. April 2006 11:56:39 +0200 Jonas Nielsen <jonasn@mail.tele.dk>
wrote:

> Andreas Jung wrote:
>> Jonas Nielsen wrote:
>>
>>> Is it possible to store a database connection (i.e. a "Z MySQL Database
>>> Connection") in a file system product ? How ?
>>
>> What is the sense? You can not store persistent objects. You might store
>> a reference to the object using some _v_ variables to avoid getting
>> the DA everytime through acquisition (this makes only sense
>> if you have performance issues, do you?).
>
> The sense is that I would like to find an easy way to install my product
> without setting up the database connection manually. I guess the way to
> do it is to add some code in install.py then ?
>

huh? Either you expect a DA under a particular ID and use acquisition to
get hold of it or you except a certain meta_type and use superValues() to
search for instances of this particular meta_type.

-aj

--
ZOPYX Ltd. & Co. KG - Charlottenstr. 37/1 - 72070 Tübingen - Germany
Web: www.zopyx.com - Email: info@zopyx.com - Phone +49 - 7071 - 793376
E-Publishing, Python, Zope & Plone development, Consulting
Re: Database connection in a file system product [ In reply to ]
Andreas Jung wrote:
>
>
> --On 27. April 2006 11:56:39 +0200 Jonas Nielsen <jonasn@mail.tele.dk>
> wrote:
>
>> Andreas Jung wrote:
>>
>>> Jonas Nielsen wrote:
>>>
>>>> Is it possible to store a database connection (i.e. a "Z MySQL Database
>>>> Connection") in a file system product ? How ?
>>>
>>>
>>> What is the sense? You can not store persistent objects. You might store
>>> a reference to the object using some _v_ variables to avoid getting
>>> the DA everytime through acquisition (this makes only sense
>>> if you have performance issues, do you?).
>>
>>
>> The sense is that I would like to find an easy way to install my product
>> without setting up the database connection manually. I guess the way to
>> do it is to add some code in install.py then ?
>>
>
> huh? Either you expect a DA under a particular ID and use acquisition to
> get hold of it or you except a certain meta_type and use superValues()
> to search for instances of this particular meta_type.

Well, I want to make some python code that creates a DA under a
particular ID (and yes, this is not my first question, but it will solve
my problem).

_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Database connection in a file system product [ In reply to ]
Hi Jonas,

Here is some code I use to create a DA during product installation:

from Products.mxODBCZopeDA.ZopeDA import ZopeConnection

security.declarePrivate('manage_afterAdd')
def manage_afterAdd(self, item, container):
# add mxODBC connection

conn_string = readDatabaseConfig()
tmpConnection = ZopeConnection('dbConnection',
'Database Connection',
conn_string,
connection_check=True
)

self._setObject('dbConnection', tmpConnection)


It is in the manage_afterAdd method of my product, which basically
creates a site object to hang the rest of my code and skins on. In this
case it's creating an mxODBCDA connection, but the same principle would
apply for any DA. I think for most other DA's the "ZopeConnection"
method will be "Connection".

It uses a config file (using ZConfig) which looks like this to set the
conn_string. The conn_string will vary somewhat depending on the DA. A
typical config file looks like this:

dsn development
user master
password xxxxxxxx

and a ZConfig schema:

<schema prefix="DatabaseConfig.database">

<key name="dsn" datatype="string" required="yes"/>
<key name="user" datatype="string" required="yes"/>
<key name="password" datatype="string" required="yes"/>

</schema>


The readDatabaseConfig module is:

import ZConfig
import os
from Globals import package_home

def readDatabaseConfig(newlines=True):
SCHEMA = os.path.join(package_home(globals()), 'schema_odbc.xml')
config_file = os.path.join(package_home(globals()),
'database_odbc.conf')

schema = ZConfig.loadSchema(SCHEMA)
cfg, nil = ZConfig.loadConfig(schema, config_file)

if newlines:
nl = '\n'
else:
nl = ''
return 'DSN=%s;%sUID=%s;%sPWD=%s' \
% (cfg.dsn, nl, cfg.user, nl, cfg.password)

I use this because we set up many separate sites all using this product
and it saves the manual installation and also standardizes naming
conventions (all the DA's get the same id: dbConnection). I also
sometimes have sysadmins not familiar with Zope doing installs for me.

You should be able to modify this for your DA.

HTH

Bob Corriher
CTO
P-Wave Inc.
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: Re: Database connection in a file system product [ In reply to ]
Jonas Nielsen wrote at 2006-4-27 11:56 +0200:
> ...
>The sense is that I would like to find an easy way to install my product
>without setting up the database connection manually. I guess the way to
>do it is to add some code in install.py then ?

That would work if you have a persistent object you can (and want)
to attach the DA to.

--
Dieter
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db