Mostly documentation updates, and some code cleanups

This commit is contained in:
adustman
2002-06-18 01:01:47 +00:00
parent 58866a76fa
commit 05199fb0f9
5 changed files with 364 additions and 120 deletions

View File

@@ -19,8 +19,8 @@ version_info = (
0,
9,
2,
"beta",
2)
"gamma",
1)
if version_info[3] == "final": __version__ = "%d.%d.%d" % version_info[:3]
else: __version__ = "%d.%d.%d%1.1s%d" % version_info[:5]

View File

@@ -18,35 +18,34 @@ def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
connection.messages.append(errorvalue)
raise errorclass, errorvalue
class Connection:
"""Create a connection to the database. It is strongly recommended
that you only use keyword parameters. "NULL pointer" indicates that
NULL will be passed to mysql_real_connect(); the value in parenthesis
indicates how MySQL interprets the NULL. Consult the MySQL C API
"""
Create a connection to the database. It is strongly recommended
that you only use keyword parameters. Consult the MySQL C API
documentation for more information.
host -- string, host to connect to or NULL pointer (localhost)
user -- string, user to connect as or NULL pointer (your username)
passwd -- string, password to use or NULL pointer (no password)
db -- string, database to use or NULL (no DB selected)
port -- integer, TCP/IP port to connect to or default MySQL port
unix_socket -- string, location of unix_socket to use or use TCP
client_flags -- integer, flags to use or 0 (see MySQL docs)
host -- string, host to connect
user -- string, user to connect as
passwd -- string, password to use
db -- string, database to use
port -- integer, TCP/IP port to connect to
unix_socket -- string, location of unix_socket to use
conv -- conversion dictionary, see MySQLdb.converters
connect_time -- number of seconds to wait before the connection
attempt fails.
compress -- if set, compression is enabled
named_pipe -- if set, a named pipe is used to connect (Windows only)
init_command -- command which is run once the connection is created
read_default_file -- see the MySQL documentation for mysql_options()
read_default_group -- see the MySQL documentation for mysql_options()
cursorclass -- class object, used to create cursors or cursors.Cursor.
This parameter MUST be specified as a keyword parameter.
Returns a Connection object.
read_default_file -- file from which default client values are read
read_default_group -- configuration group to use from the default file
cursorclass -- class object, used to create cursors (keyword only)
There are a number of undocumented, non-standard methods. See the
documentation for the MySQL C API for some hints on what they do.
"""
default_cursor = cursors.Cursor
@@ -73,84 +72,58 @@ class Connection:
def __del__(self):
if hasattr(self, '_db'): self.close()
def close(self):
"""Close the connection. No further activity possible."""
self._db.close()
def __getattr__(self, attr):
return getattr(self._db, attr)
def begin(self):
"""Explicitly begin a transaction. Non-standard."""
self._db.query("BEGIN")
self.query("BEGIN")
def commit(self):
"""Commit the current transaction."""
if self._transactional:
self._db.query("COMMIT")
self.query("COMMIT")
def rollback(self):
"""Rollback the current transaction."""
if self._transactional:
self._db.query("ROLLBACK")
self.query("ROLLBACK")
else:
raise NotSupportedError, "Not supported by server"
def cursor(self, cursorclass=None):
"""Create a cursor on which queries may be performed. The
"""
Create a cursor on which queries may be performed. The
optional cursorclass parameter is used to create the
Cursor. By default, self.cursorclass=cursors.Cursor is
used."""
used.
"""
return (cursorclass or self.cursorclass)(self)
# Non-portable MySQL-specific stuff
# Methods not included on purpose (use Cursors instead):
# query, store_result, use_result
def literal(self, o):
"""If o is a single object, returns an SQL literal as a string.
If o is a non-string sequences, the items of the sequence are
converted and returned as a sequence."""
"""
If o is a single object, returns an SQL literal as a string.
If o is a non-string sequence, the items of the sequence are
converted and returned as a sequence.
"""
import _mysql
return _mysql.escape(o, self._db.converter)
def unicode_literal(self, u, dummy=None):
"""Convert a unicode object u to a string using the current
"""
Convert a unicode object u to a string using the current
character set as the encoding. If that's not available,
use latin1."""
try: charset = self.character_set_name()
except: charset = 'latin1'
return self.literal(u.encode(charset))
def affected_rows(self): return self._db.affected_rows()
def dump_debug_info(self): return self._db.dump_debug_info()
def escape_string(self, s): return self._db.escape_string(s)
def get_host_info(self): return self._db.get_host_info()
def get_proto_info(self): return self._db.get_proto_info()
def get_server_info(self): return self._db.get_server_info()
def info(self): return self._db.info()
def kill(self, p): return self._db.kill(p)
def field_count(self): return self._db.field_count()
num_fields = field_count # used prior to MySQL-3.22.24
def ping(self): return self._db.ping()
def row_tell(self): return self._db.row_tell()
def select_db(self, db): return self._db.select_db(db)
def shutdown(self): return self._db.shutdown()
def stat(self): return self._db.stat()
def string_literal(self, s): return self._db.string_literal(s)
def thread_id(self): return self._db.thread_id()
def _try_feature(self, feature, *args, **kwargs):
try:
return apply(getattr(self._db, feature), args, kwargs)
except AttributeError:
self.errorhandler(self, None, NotSupportedError,
"not supported by MySQL library")
def character_set_name(self):
return self._try_feature('character_set_name')
def change_user(self, *args, **kwargs):
return apply(self._try_feature, ('change_user',)+args, kwargs)
latin1 is used.
"""
return self.literal(u.encode(self.character_set_name()))
Warning = Warning
Error = Error
InterfaceError = InterfaceError

View File

@@ -66,22 +66,34 @@ def Thing2Literal(o, d):
return string_literal(o, d)
def Instance2Str(o, d):
"""Convert an Instance to a string representation. If the
__str__() method produces acceptable output, then you don't need
to add the class to conversions; it will be handled by the default
converter. If the exact class is not found in d, it will use the
first class it can find for which o is an instance."""
"""
if d.has_key(o.__class__): return d[o.__class__](o, d)
Convert an Instance to a string representation. If the __str__()
method produces acceptable output, then you don't need to add the
class to conversions; it will be handled by the default
converter. If the exact class is not found in d, it will use the
first class it can find for which o is an instance.
"""
if d.has_key(o.__class__):
return d[o.__class__](o, d)
cl = filter(lambda x,o=o:
type(x)==types.ClassType and isinstance(o,x), d.keys())
type(x) is types.ClassType
and isinstance(o, x), d.keys())
if not cl and hasattr(types, 'ObjectType'):
cl = filter(lambda x,o=o:
type(x) is types.TypeType
and isinstance(o, x), d.keys())
if not cl:
return d[types.StringType](o,d)
d[o.__class__] = d[cl[0]]
return d[cl[0]](o, d)
conversions = {
types.IntType: Thing2Str,
types.LongType: Long2Int,
@@ -110,6 +122,9 @@ conversions = {
FIELD_TYPE.DATE: Date_or_None,
}
if hasattr(types, 'UnicodeType'):
conversions[types.UnicodeType] = Unicode2Str
if hasattr(types, 'ObjectType'):
conversions[types.ObjectType] = Instance2Str