ll.orasql contains utilities for working with cx_Oracle:
It allows calling procedures with keyword arguments (via the
Procedureclass).Query results will be put into
Recordobjects, where database fields are accessible as object attributes.The
Connectionclass provides methods for iterating through the database metadata.
class SQLObjectNotFoundError(exceptions.Exception):
def __init__(self, obj):
selfdef __repr__(self):
selfdef __str__(self):
selfclass SQLNoSuchObjectError(exceptions.Exception):
def __init__(self, name, owner):
selfdef __repr__(self):
selfdef __str__(self):
selfclass UnknownModeError(exceptions.ValueError):
def __init__(self, mode):
selfdef __repr__(self):
selfdef __str__(self):
selfclass UnknownSchemaError(exceptions.ValueError):
def __init__(self, schema):
selfdef __repr__(self):
selfdef __str__(self):
selfclass ConflictError(exceptions.ValueError):
def __init__(self, object, message):
selfdef __repr__(self):
selfdef __str__(self):
selfclass Args(dict):
An Args object is a subclass of dict that is used for
passing arguments to procedures and functions. Both item and attribute access
(i.e. __getitem__ and __getattr__) are available. Names are
case insensitive.
def __init__(self, arg=None, **kwargs):
selfdef update(self, arg=None, **kwargs):
selfdef __getitem__(self, name):
selfdef __setitem__(self, name, value):
selfdef __delitem__(self, name):
selfdef __getattr__(self, name):
selfdef __setattr__(self, name, value):
selfdef __delattr__(self, name):
selfdef __xattrs__(self, mode='default'):
selfdef __xrepr__(self, mode):
selfdef __repr__(self):
selfclass RecordMaker(object):
def __init__(self, cursor):
selfdef _decode(self, value):
selfdef __call__(self, *row):
selfclass Record(tuple):
A Record is a subclass of tuple that is used for storing
results of database fetches and procedure and function calls. Both item and
attribute access (i.e. __getitem__ and __getattr__) are
available. Field names are case insensitive.
def __getitem__(self, arg):
selfdef __getattr__(self, name):
selfdef get(self, name, default=None):
selfReturn the value for the field named name. If this field doesn't
exist in self, return default instead.
def __contains__(self, name):
selfdef iterkeys(self):
selfReturn an iterator over field names
def keys(self):
selfReturn a list of field names
def items(self):
selfReturn a list of (field name, field value) tuples.
def iteritems(self):
selfReturn an iterator over (field name, field value) tuples.
def __xattrs__(self, mode='default'):
selfdef __xrepr__(self, mode):
selfdef __repr__(self):
selfclass _AllTypes(object):
def __init__(self, connection, class_, schema, count):
selfdef __xattrs__(self, mode='default'):
selfdef __xrepr__(self, mode):
selfdef __iter__(self):
selfclass SessionPool(OracleSessionPool):
SessionPool is a subclass of cx_Oracle.SessionPool.
def __init__(self, user, password, database, min, max, increment, connectiontype=None, threaded=False, getmode=1, homogeneous=True):
selfdef connectstring(self):
selfdef __repr__(self):
selfclass Connection(cx_Oracle.Connection):
Connection is a subclass of cx_Oracle.Connection.
def __init__(self, *args, **kwargs):
selfCreate a new connection. In addition to the parameters supported by
cx_Oracle.connect the following keyword argument is supported.
readlobs(bool or integer)If
readlobsisFalseall cursor fetch returnLOBobjects as usual. Ifreadlobsis anint(orlong)LOB`s with a maximum size of :var:`readlobswill be returned as strings. IfreadlobsisTrueallLOBvalues will be returned as strings.
def connectstring(self):
selfdef cursor(self, readlobs=None):
selfReturn a new cursor for this connection. For the meaning of
readlobs see __init__.
def __repr__(self):
selfdef __xrepr__(self, mode):
selfdef iterschema(self, schema='user'):
selfGenerator that returns the number of different object types for this
database. For the meaning of schema see iterobjects.
def itertables(self, schema='user', mode='flat'):
selfGenerator that yields all table definitions in the current users schema
(or all users schemas). mode specifies the order in which tables
will be yielded:
"drop"Drop order, deleting records from the table in this order will not violate foreign key onstraints.
"flat"Unordered.
schema specifies from which user tables should be yielded:
"user"Only tables belonging to the current user (and those objects these depend on) will be yielded.
"all"All tables from all users will be yielded.
Tables that are materialized view will be skipped in both casess.
def itersequences(self, schema='user'):
selfGenerator that yields all sequence in the current users schema (or all
users schemas). schema specifies from which user sequences should
be yielded:
"user"Only sequences belonging to the current user will be yielded.
"all"All sequences from all users will be yielded.
def iterfks(self, schema='user'):
selfGenerator that yields all foreign key constraints in the current users
schema (or all users schemas). schema specifies from which user
foreign keys should be yielded:
"user"Only tables belonging to the current user (and those objects these depend on) will be yielded.
"all"All tables from all users will be yielded.
def iterobjects(self, mode='create', schema='user'):
selfGenerator that yields the sequences, tables, primary keys, foreign keys, comments, unique constraints, indexes, views, functions, procedures, packages and types in the current users schema (or all users schemas) in a specified order.
mode specifies the order in which objects will be yielded:
"create"Create order, i.e. recreating the objects in this order will not lead to errors.
"drop"Drop order, i.e. dropping the objects in this order will not lead to errors.
"flat"Unordered.
schema specifies from which schema objects should be yielded:
"user"Only objects belonging to the current user (and those objects these depend on) will be yielded.
"all"All objects from all users will be yielded.
def _getobject(self, name, owner=None):
selfdef getobject(self, name, owner=None):
selfReturn the object named name from the schema. If owner is
None the current schema is queried, else the specified one is
used. name and owner are treated case insensitively.
def iterprivileges(self, schema='user'):
selfGenerator that yields object privileges for the current users (or all
users) objects. schema specifies which privileges should be
yielded:
"user"Only object privileges for objects belonging to the current user will be yielded.
"all"All object privileges will be yielded.
def connect(*args, **kwargs):
Create a connection to the database and return a Connection object.
class Cursor(OracleCursor):
A subclass of the cursor class in cx_Oracle. The "execute" methods
support a unicode statement and unicode parameters (they will be encoded in
the client encoding before being passed to the database). The "fetch" methods
will return records as Record objects and string values and
CLOB values, if the cursors readlobs attribute has the
appropriate value) will be returned as unicode objects (except for
BLOB values). (Note that strings in the national character set
(and NCLOB values) are not supported).
def __init__(self, connection, readlobs=None):
selfReturn a new cursor for the connection connection. For the meaning
of readlobs see Connection.__init__.
def _decode(self, value, isblob):
selfdef execute(self, statement, parameters=None, **kwargs):
selfdef executemany(self, statement, parameters):
selfdef __xrepr__(self, mode):
selfdef __repr__(self):
selfdef formatstring(value, latin1=False):
class MixinNormalDates(object):
Mixin class that provides methods for determining creation and modification dates for objects.
def cdate(self, connection=None):
selfdef udate(self, connection=None):
selfclass MixinCodeDDL(object):
Mixin class that provides methods returning the create and drop statements for various objects.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef getfullname(name, owner):
class Object(object):
The base class for all Python classes modelling schema objects in the database.
Subclasses are: Sequence, Table, PrimaryKey,
Comment, ForeignKey, Index, Unique,
Synonym, View, MaterializedView, Library,
Function, Package, Type, Trigger,
JavaSource and Column.
class __metaclass__(type):
def __init__(self, name, owner=None, connection=None):
selfdef __repr__(self):
selfdef __str__(self):
selfdef __eq__(self, other):
selfdef __ne__(self, other):
selfdef __hash__(self):
selfdef getfullname(self):
selfdef createddl(self, *args, **kwargs):
selfReturn SQL code to create this object.
def dropddl(self, *args, **kwargs):
selfReturn SQL code to drop this object
def cdate(self, *args, **kwargs):
selfReturn a datetime.datetime object with the creation date of
self in the database specified by connection (or
None if such information is not available).
def udate(self, *args, **kwargs):
selfReturn a datetime.datetime object with the last modification
date of self in the database specified by connection
(or None if such information is not available).
def iterreferences(self, connection=None):
selfObjects directly used by self.
If connection is not None it will be used as the database
connection from which to fetch data. If connection is None
the connection from which self has been extracted will be used. If
there is not such connection, you'll get an exception.
def iterreferencesall(self, connection=None, done=None):
selfAll objects used by self (recursively).
For the meaning of connection see iterreferences.
done is used internally and shouldn't be passed.
def iterreferencedby(self, connection=None):
selfObjects using self.
For the meaning of connection see iterreferences.
def iterreferencedbyall(self, connection=None, done=None):
selfAll objects depending on self (recursively).
For the meaning of connection see iterreferences.
done is used internally and shouldn't be passed.
def getconnection(self, connection):
selfdef getcursor(self, connection):
selfproperty connectstring:
def __get__(self):
def iterobjects(cls, connection, schema='user'):
clsGenerator that yields all objects of this type in the database schema
of cursor.
def __iter__(self):
selfdef __xrepr__(self, mode):
selfdef __xattrs__(self, mode='default'):
selfclass Sequence(MixinNormalDates, Object):
Models a sequence in the database.
def _createddl(self, connection, term, copyvalue):
selfdef createddl(self, connection=None, term=True):
selfdef createddlcopy(self, connection=None, term=True):
selfReturn SQL code to create an identical copy of this sequence.
def dropddl(self, connection=None, term=True):
selfdef iterreferences(self, connection=None, schema='all'):
selfdef __xattrs__(self, mode='default'):
selfdef _columntype(rec, data_precision=None, data_scale=None, char_length=None):
def _columndefault(rec):
class Table(MixinNormalDates, Object):
Models a table in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef mview(self, connection=None):
selfThe materialized view this table belongs to (or None if it's a
real table).
def ismview(self, connection=None):
selfIs this table a materialized view?
def iterobjects(cls, connection, schema='user'):
clsdef itercolumns(self, connection=None):
selfGenerator that yields all column objects of the Table self.
def iterrecords(self, connection=None):
selfGenerator that yields all records of the table self.
def itercomments(self, connection=None):
selfGenerator that yields all column comments of the table self.
def iterconstraints(self, connection=None):
selfGenerator that yields all constraints for this table.
def iterreferences(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfdef __xattrs__(self, mode='default'):
selfclass Constraint(Object):
Base class of all constraints (primary key constraints, foreign key constraints and unique constraints).
class PrimaryKey(Constraint):
Models a primary key constraint in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef cdate(self, connection=None):
selfdef udate(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfdef iterreferences(self, connection=None):
selfdef table(self, connection=None):
selfReturn the Table self belongs to.
def __xattrs__(self, mode='default'):
selfclass Comment(Object):
Models a column comment in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef cdate(self, connection=None):
selfdef udate(self, connection=None):
selfdef iterreferences(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfclass ForeignKey(Constraint):
Models a foreign key constraint in the database.
def createddl(self, connection=None, term=True):
selfdef _ddl(self, connection, cmd, term):
selfdef dropddl(self, connection=None, term=True):
selfdef enableddl(self, connection=None, term=True):
selfdef disableddl(self, connection=None, term=True):
selfdef cdate(self, connection=None):
selfdef udate(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfdef iterreferences(self, connection=None):
selfdef table(self, connection=None):
selfReturn the Table self belongs to.
def pk(self, connection=None):
selfReturn the primary key referenced by self.
def isenabled(self, connection=None):
selfReturn whether this constraint is enabled.
def __xattrs__(self, mode='default'):
selfclass Index(MixinNormalDates, Object):
Models an index in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef constraint(self, connection=None):
selfIf this index is generated by a constraint, return the constraint
otherwise return None.
def isconstraint(self, connection=None):
selfIs this index generated by a constraint?
def iterreferences(self, connection=None):
selfdef table(self, connection=None):
selfReturn the Table self belongs to.
def __xattrs__(self, mode='default'):
selfclass UniqueConstraint(Constraint):
Models a unique constraint in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef cdate(self, connection=None):
selfdef udate(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfdef iterreferences(self, connection=None):
selfdef table(self, connection=None):
selfReturn the Table self belongs to.
def __xattrs__(self, mode='default'):
selfclass Synonym(Object):
Models a synonym in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef cdate(self, connection=None):
selfdef udate(self, connection=None):
selfdef iterreferences(self, connection=None, schema='all'):
selfdef getobject(self, connection=None):
selfGet the object for which self is a synonym.
class View(MixinNormalDates, Object):
Models a view in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef iterrecords(self, connection=None):
selfclass MaterializedView(View):
Models a meterialized view in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef iterreferences(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfclass Library(Object):
Models a library in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfclass Argument(object):
Argument objects hold information about the arguments of a
stored procedure.
def __init__(self, name, position, datatype, isin, isout):
selfdef __repr__(self):
selfdef __xattrs__(self, mode='default'):
selfclass Callable(MixinNormalDates, MixinCodeDDL, Object):
Models a callable object in the database, i.e. functions and procedures.
def __init__(self, name, owner=None, connection=None):
selfdef _calcargs(self, cursor):
selfdef _getargs(self, cursor, *args, **kwargs):
selfdef _wraparg(self, cursor, arginfo, arg):
selfdef _unwraparg(self, cursor, arg):
selfdef _makerecord(self, cursor, args):
selfdef iterarguments(self, connection=None):
selfGenerator that yields all arguments of the function/procedure self.
def __xattrs__(self, mode='default'):
selfclass Procedure(Callable):
Models a procedure in the database. A Procedure object can be
used as a wrapper for calling the procedure with keyword arguments.
def __call__(self, cursor, *args, **kwargs):
selfCall the procedure with arguments args and keyword arguments
kwargs. cursor must be a ll.orasql cursor. This will
return a Record object containing the result of the call (i.e.
this record will contain all specified and all out parameters).
class Function(Callable):
Models a function in the database. A Function object can be
used as a wrapper for calling the function with keyword arguments.
def __call__(self, cursor, *args, **kwargs):
selfCall the function with arguments args and keyword arguments
kwargs. cursor must be an ll.orasql cursor.
This will return a tuple containing the result and a Record
object containing the modified parameters (i.e. this record will contain
all specified and out parameters).
class Package(MixinNormalDates, MixinCodeDDL, Object):
Models a package in the database.
class PackageBody(MixinNormalDates, MixinCodeDDL, Object):
Models a package body in the database.
class Type(MixinNormalDates, MixinCodeDDL, Object):
Models a type definition in the database.
class Trigger(MixinNormalDates, MixinCodeDDL, Object):
Models a trigger in the database.
class JavaSource(MixinNormalDates, Object):
Models Java source code in the database.
def createddl(self, connection=None, term=True):
selfdef dropddl(self, connection=None, term=True):
selfclass Privilege(object):
Models a database object privilege (i.e. a grant).
def __init__(self, privilege, name, grantor, grantee, owner=None, connection=None):
selfdef __repr__(self):
selfdef __str__(self):
selfdef getconnection(self, connection):
selfdef getcursor(self, connection):
selfproperty connectstring:
def __get__(self):
def iterobjects(self, connection, schema='user'):
selfGenerator that yields object privileges for the current users (or all users) objects.
schema specifies which privileges should be yielded:
"user"Only object privileges for objects belonging to the current user will be yielded.
"all"All object privileges will be yielded.
def grantddl(self, connection=None, term=True, mapgrantee=True):
selfReturn SQL code to grant this privilege. If mapgrantee is a list
or a dictionary and self.grantee is not in this list (or dictionary)
no command will returned. If it's a dictionary and self.grantee is
in it, the privilege will be granted to the user specified as the value
instead of the original one. If mapgrantee is true (the default)
the privilege will be granted to the original grantee.
def __xattrs__(self, mode='default'):
selfclass Column(Object):
Models a single column of a table in the database. This is used to output
ALTER TABLE ... statements for adding, dropping and modifying columns.
def _getcolumnrecord(self, cursor):
selfdef addddl(self, connection=None, term=True):
selfdef modifyddl(self, connection, cursorold, cursornew, term=True):
selfdef dropddl(self, connection=None, term=True):
selfdef cdate(self, connection=None):
selfdef udate(self, connection=None):
selfdef iterreferences(self, connection=None):
selfdef iterreferencedby(self, connection=None):
selfdef datatype(self, connection=None):
selfThe SQL type of this column.
def default(self, connection=None):
selfThe SQL default value for this column.
def nullable(self, connection=None):
selfIs this column nullable?
def comment(self, connection=None):
selfThe comment for this column.
def __xattrs__(self, mode='default'):
selfdef __iter__(self):
self