[DB-SIG] Re: Informixdb hooks: questions again

John Mitchell johnm@magnet.com
Fri, 30 Apr 1999 17:04:30 -0400


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---792678398-313455780-925506270=:4212
Content-Type: TEXT/PLAIN; charset=US-ASCII

On Fri, 30 Apr 1999, Deirdre Saoirse wrote:

> OK, now that I think we have everything set up...theres NO docs..

docs?  you mean, besides the code?  :)

> What do I pass in the informixdb.informixdb statement? Would it be the
> same order as in Javascript? ('INFORMIX', hostname, username, password,
> database)?

Check out enclosed file, which I use as the lowest-level Python object.  
Following this paragraph is the next higher stuff, which deals with
stage-vs-live setup issues.

SQL reminds me of "Worse is Better" by Richard Gabriel at
http://www.jwz.org/worse-is-better.html 

- j


# to test db connection:
#
# python -c 'import ad_db  ; db=ad_db.open() ; 
#   db.execute('select * from sponsor_slot') ; print db.fetchall()
#

# $Id: ad_db.py,v 1.2 1998/09/15 15:20:06 johnm Exp $


import mysite				# plays with sys.path
import sys
import informixdb, os

debug = 0

_informix_vars = [

# host-specific parameters:
#
    (['maroon','lingua'],	None,		    (
        "adserver@ifmx_maroon", "dbuser", 'dbpass')),
    (['maroon','lingua'],	'INFORMIXDIR',      '/www/informix'),
    (['maroon','lingua'],	'INFORMIXSQLHOSTS', '/www/informix/etc/sqlhosts.ifmx'),
    (['maroon','lingua'],	'INFORMIXSERVER',   'ifmx_maroon'), # shared-mem

    (['peach'],	None,		    (
        "ads@ifmx_peach_shm", "dbuserdb", 'dbpass')),
    (['peach'],	'INFORMIXDIR',      '/www/informix'),
    (['peach'],	'INFORMIXSQLHOSTS', '/www/informix/etc/sqlhosts.ifmx'),
    (['peach'],	'INFORMIXSERVER',   'ifmx_peach_shm'), # shared-mem
#    (['peach'],	'INFORMIXSERVER',   'ifmx_peach'),# normal

    (['violet'], None,               (
        "ads@ifmx_violet", "dbuserdb", 'dbpass')),
    (['violet'], 'INFORMIXDIR',      '/www/informix'),
    (['violet'], 'INFORMIXSQLHOSTS', '/www/informix/etc/sqlhosts.ifmx'),
    (['violet'], 'INFORMIXSERVER',   'ifmx_violet'), # shared-mem
#    (['violet'],        'INFORMIXSERVER',   'ifmx_violet'),# normal

# generic defaults (must be last):
#
     (None,	None,		    (
	 "ads@ifmx_dingdong", "magdev", 'dbpass')),
    (None,	'INFORMIXSERVER',   'ifmx_tsunami'),
    (None,	'INFORMIXDIR',      '/usr/local/informix'),
    (None,	'INFORMIXSQLHOSTS', '/usr/local/informix/etc/sqlhosts.ifmx'),
    (None,	'INFORMIXTERM',     'terminfo'),

]

global _informix_login			# XX ugh!
_informix_login = None


# X: global '_informix_login' modified
#
def setup(myHostname):
    # set any missing Informix environment variables.
    # Ack!
    #
    env = os.environ
    global _informix_login
    _informix_login	= None

    for hostlist, k,v in _informix_vars:
	if not hostlist or myHostname in hostlist:

	    # set login information
	    #
	    if not k:
		if not _informix_login:
		    _informix_login = v
		continue

	    # set environment info
	    # (allow shell's environment to override us):
	    #
	    if not env.has_key(k):
		if debug: sys.stderr.write("set: %s\t%s\n" % (k,v))
		env[k] = v
	    else:
		if debug: sys.stderr.write("env: %s\t%s\n" % (k,v))


# X: global '_informix_login' used
#
def open(hostname=None):

    if not hostname:
	import socket
	hostname = socket.gethostname()

    setup(hostname)				# XX: sigh

    db = None

    if _informix_login:		
	db = apply(informixdb.informixdb, _informix_login)
    else:
	raise KeyError, "%s: host not recognized" % hostname

    return db


def test(hostname=None):
    db = open(hostname)
    db.execute('select * from dummy')	# XX assumes this table exists!
    print db.fetchall()



if __name__=="__main__":
    print test()


---792678398-313455780-925506270=:4212
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="informixdb.py"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.SGI.4.05.9904301704300.4212@lemur.magnet.com>
Content-Description: informixdb.py
Content-Disposition: attachment; filename="informixdb.py"

IyAkSWQ6IGluZm9ybWl4ZGIucHksdiAxLjEuMS4xIDE5OTgvMDg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