提交 e55213a5 编写于 作者: B Bruce Momjian

Re-add python.

上级 716b8e2d
PyGreSQL changelog.
===================
This software is copyright (c) 1995, Pascal Andre (andre@via.ecp.fr)
Further copyright 1997, 1998 and 1999 by D'Arcy J.M. Cain (darcy@druid.net)
See file README for copyright information.
Version 2.3
- connect.host returns "localhost" when connected to Unix socket
(torppa@tuhnu.cutery.fi)
- Use PyArg_ParseTupleAndKeywords in connect() (torppa@tuhnu.cutery.fi)
- fixes and cleanups (torppa@tuhnu.cutery.fi)
- Fixed memory leak in dictresult() (terekhov@emc.com)
- Deprecated pgext.py - functionality now in pg.py
- More cleanups to the tutorial
- Added fileno() method - terekhov@emc.com (Mikhail Terekhov)
- added money type to quoting function
- Compiles cleanly with more warnings turned on
- Returns PostgreSQL error message on error
- Init accepts keywords (Jarkko Torppa)
- Convenience functions can be overridden (Jarkko Torppa)
- added close() method
Version 2.2
- Added user and password support thanks to Ng Pheng Siong <ngps@post1.com>
- Insert queries return the inserted oid
- Add new pg wrapper (C modile renamed to _pg)
- Wrapped database connection in a class.
- Cleaned up some of the tutorial. (More work needed.)
- Added version and __version__. Thanks to thilo@eevolute.com for
the suggestion.
Version 2.1
- return fields as proper Python objects for field type
- Cleaned up pgext.py
- Added dictresult method
Version 2.0 (23/12/1997):
- updated code for PostgreSQL 6.2.1 and Python 1.5
- reformatted code and converted to ANSI
- Changed name to PyGreSQL (from PyGres95)
- changed order of arguments to connect function
- Created new type pgqueryobject and moved certain methods to it.
- Added a print function for pgqueryobject
Version 1.0b (4/11/1995):
- keyword support for connect function moved from library file to C code
and taken away from library.
- rewrote documentation
- bug fix in connect function
- enhancements in large objects interface methods
Version 1.0a (30/10/1995) (limited release):
- module adapted to standard Python syntax
- keyword support for connect function in library file
- rewrote default parameters interface (internal use of strings)
- fixed minor bugs in module interface
- redefinition of error messages
Version 0.9b (10/10/1995) (first public release):
- large objects implementation
- many bug fixes, enhancements, ...
Version 0.1a (7/10/1995):
- basic libpq functions (SQL access)
此差异已折叠。
Thanks to thilo@eevolute.com for this README and the RPM
INSTALLING PyGreSQL on Redhat Linux 5.1 or 5.2
==============================================
Things are pretty easy on Redhat Linux. You can either get a precompiled
RPM package from
ftp://www.eevolute.com/pub/python/
or try in compile and install it yourself:
bash$ make redhat # this just compiles the module as a shared object
cc -fpic -shared -o _pg.so -I/usr/include/python1.5 pgmodule.c -lpq
bash$ python # you can test it from your local directory
Python 1.5.1 (#1, May 6 1998, 01:48:27) [GCC 2.7.2.3] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import _pg
>>> db = _pg.connect('thilo', 'localhost')
>>> db.query("INSERT INTO test VALUES ('ping', 'pong')")
18304
>>> db.query("SELECT * FROM test")
eins|zwei
----+----
ping|pong
(1 row)
bash$ su # Yow! Seems to work - now install it properly
bash# cp _pg.so /usr/lib/python1.5/lib-dynload
done!
#! /usr/local/bin/python
import string
# change this if you have it somewhere else
for l in open("/usr/local/pgsql/src/include/catalog/pg_type.h").readlines():
tokens = string.split(l)
if len(tokens) == 0 or tokens[0] != "#define": continue
if tokens[1] in ('CASHOID', 'INT2OID', 'INT4OID', 'OIDOID', 'FLOAT4OID', 'FLOAT8OID'):
print l,
# pgutil.py
# Written by D'Arcy J.M. Cain
# This library implements some basic database management stuff
# It includes the pg module and builds on it
from _pg import *
import string, re, sys
# utility function
# We expect int, seq, decimal, text or date (more later)
def _quote(d, t):
if t in ['int', 'decimal', 'seq']:
if d == "": return 0
return "%s" % d
if t == 'money':
if d == "": return '0.00'
return "'%.2f'" % d
if t == 'bool':
if string.upper(d) in ['T', 'TRUE', 'Y', 'YES', 1, '1', 'ON']:
return "'t'"
else:
return "'f'"
if d == "": return "null"
return "'%s'" % string.strip(re.sub('\'', '\'\'', "%s" % d))
class DB:
"""This class wraps the pg connection type"""
def __init__(self, *args, **kw):
self.db = apply(connect, args, kw)
# Create convience methods, in a way that is still overridable.
for e in ( 'query', 'reset', 'close', 'getnotify', 'inserttable',
'putline', 'getline', 'endcopy',
'host', 'port', 'db', 'options',
'tty', 'error', 'status', 'user',
'locreate', 'getlo', 'loimport' ):
if not hasattr(self,e) and hasattr(self.db,e):
exec 'self.%s = self.db.%s' % ( e, e )
self.attnames = {}
self.pkeys = {}
self.debug = None # For debugging scripts, set to output format
# that takes a single string arg. For example
# in a CGI set to "%s<BR>"
# Get all the primary keys at once
for rel, att in self.db.query("""SELECT
pg_class.relname, pg_attribute.attname
FROM pg_class, pg_attribute, pg_index
WHERE pg_class.oid = pg_attribute.attrelid AND
pg_class.oid = pg_index.indrelid AND
pg_index.indkey[0] = pg_attribute.attnum AND
pg_index.indisprimary = 't'""").getresult():
self.pkeys[rel] = att
def pkey(self, cl):
# will raise an exception if primary key doesn't exist
return self.pkeys[cl]
def get_databases(self):
l = []
for n in self.db.query("SELECT datname FROM pg_database").getresult():
l.append(n[0])
return l
def get_tables(self):
l = []
for n in self.db.query("""SELECT relname FROM pg_class
WHERE relkind = 'r' AND
relname !~ '^Inv' AND
relname !~ '^pg_'""").getresult():
l.append(n[0])
return l
def get_attnames(self, cl):
# May as well cache them
if self.attnames.has_key(cl):
return self.attnames[cl]
query = """SELECT pg_attribute.attname, pg_type.typname
FROM pg_class, pg_attribute, pg_type
WHERE pg_class.relname = '%s' AND
pg_attribute.attnum > 0 AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.atttypid = pg_type.oid"""
l = {}
for attname, typname in self.db.query(query % cl).getresult():
if re.match("^int", typname):
l[attname] = 'int'
elif re.match("^oid", typname):
l[attname] = 'int'
elif re.match("^text", typname):
l[attname] = 'text'
elif re.match("^char", typname):
l[attname] = 'text'
elif re.match("^name", typname):
l[attname] = 'text'
elif re.match("^abstime", typname):
l[attname] = 'date'
elif re.match("^date", typname):
l[attname] = 'date'
elif re.match("^bool", typname):
l[attname] = 'bool'
elif re.match("^float", typname):
l[attname] = 'decimal'
elif re.match("^money", typname):
l[attname] = 'money'
else:
l[attname] = 'text'
self.attnames[cl] = l
return self.attnames[cl]
# return a tuple from a database
def get(self, cl, arg, keyname = None):
if keyname == None: # use the primary key by default
keyname = self.pkeys[cl]
fnames = self.get_attnames(cl)
if type(arg) == type({}):
# To allow users to work with multiple tables we munge the
# name when the key is "oid"
if keyname == 'oid': k = arg['oid_%s' % cl]
else: k = arg[keyname]
else:
k = arg
arg = {}
# We want the oid for later updates if that isn't the key
if keyname == 'oid':
q = "SELECT * FROM %s WHERE oid = %s" % (cl, k)
else:
q = "SELECT oid AS oid_%s, %s FROM %s WHERE %s = %s" % \
(cl, string.join(fnames.keys(), ','),\
cl, keyname, _quote(k, fnames[keyname]))
if self.debug != None: print self.debug % q
res = self.db.query(q).dictresult()
if res == []:
raise error, \
"No such record in %s where %s is %s" % \
(cl, keyname, _quote(k, fnames[keyname]))
return None
for k in res[0].keys():
arg[k] = res[0][k]
return arg
# Inserts a new tuple into a table
def insert(self, cl, a):
fnames = self.get_attnames(cl)
l = []
n = []
for f in fnames.keys():
if a.has_key(f):
if a[f] == "": l.append("null")
else: l.append(_quote(a[f], fnames[f]))
n.append(f)
try:
q = "INSERT INTO %s (%s) VALUES (%s)" % \
(cl, string.join(n, ','), string.join(l, ','))
if self.debug != None: print self.debug % q
a['oid_%s' % cl] = self.db.query(q)
except:
raise error, "Error inserting into %s: %s" % (cl, sys.exc_value)
# reload the dictionary to catch things modified by engine
# note that get() changes 'oid' below to oid_table
return self.get(cl, a, 'oid')
# update always works on the oid which get returns
def update(self, cl, a):
q = "SELECT oid FROM %s WHERE oid = %s" % (cl, a['oid_%s' % cl])
if self.debug != None: print self.debug % q
res = self.db.query(q).getresult()
if len(res) < 1:
raise error, "No record in %s where oid = %s (%s)" % \
(cl, a['oid_%s' % cl], sys.exc_value)
v = []
k = 0
fnames = self.get_attnames(cl)
for ff in fnames.keys():
if a.has_key(ff) and a[ff] != res[0][k]:
v.append("%s = %s" % (ff, _quote(a[ff], fnames[ff])))
if v == []:
return None
try:
q = "UPDATE %s SET %s WHERE oid = %s" % \
(cl, string.join(v, ','), a['oid_%s' % cl])
if self.debug != None: print self.debug % q
self.db.query(q)
except:
raise error, "Can't update %s: %s" % (cl, sys.exc_value)
# reload the dictionary to catch things modified by engine
return self.get(cl, a, 'oid')
# At some point we will need a way to get defaults from a table
def clear(self, cl, a = {}):
fnames = self.get_attnames(cl)
for ff in fnames.keys():
if fnames[ff] in ['int', 'decimal', 'seq', 'money']:
a[ff] = 0
elif fnames[ff] == 'date':
a[ff] = 'TODAY'
else:
a[ff] = ""
a['oid'] = 0
return a
# Like update, delete works on the oid
# one day we will be testing that the record to be deleted
# isn't referenced somewhere (or else PostgreSQL will)
def delete(self, cl, a):
try:
q = "DELETE FROM %s WHERE oid = %s" % (cl, a['oid_%s' % cl])
if self.debug != None: print self.debug % q
self.db.query(q)
except:
raise error, "Can't delete %s: %s" % (cl, sys.exc_value)
return None
此差异已折叠。
# pgsqldb.py
# Written by D'Arcy J.M. Cain
# This library implements the DB-SIG API
# It includes the pg module and builds on it
from _pg import *
import string
class _cursor:
"""For cursor object"""
def __init__(self, conn):
self.conn = conn
self.cursor = None
self.arraysize = 1
self.description = None
self.name = string.split(`self`)[3][:-1]
def close(self):
if self.conn == None: raise self.conn.error, "Cursor has been closed"
if self.cursor == None: raise self.conn.error, "No cursor created"
self.conn.query('CLOSE %s' % self.name)
self.conn = None
def __del__(self):
if self.cursor != None and self.conn != None:
self.conn.query('CLOSE %s' % self.name)
class pgsqldb:
"""This class wraps the pg connection type in a DB-SIG API interface"""
def __init__(self, *args, **kw):
self.db = apply(connect, args, kw)
# Create convience methods, in a way that is still overridable.
for e in ('query', 'reset', 'close', 'getnotify', 'inserttable',
'putline', 'getline', 'endcopy',
'host', 'port', 'db', 'options',
'tty', 'error', 'status', 'user',
'locreate', 'getlo', 'loimport'):
if not hasattr(self,e) and hasattr(self.db,e):
exec 'self.%s = self.db.%s' % ( e, e )
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册