[DB-SIG] Building Cross-Platform DB Apps: MetaDB.py

M.-A. Lemburg mal@lemburg.com
Wed, 18 Oct 2000 11:42:15 +0200


This is a multi-part message in MIME format.
--------------7D0E317C8101C2943DEAD030
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Here's a start into the direction I wanted to push this
cross-database knowledge base.

Note that my intention wasn't to create some kind of FAQ-base,
but a Python module (or package in case the beast gets too large)
which can be used directly from within Python.

Improvements and suggestions for new APIs are welcome. I think
I'll put this module up on starship as soon as it reaches a
usable size.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/
--------------7D0E317C8101C2943DEAD030
Content-Type: text/python; charset=us-ascii;
 name="MetaDB.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="MetaDB.py"

""" Experimental cross-database knowledge base support.

"""#"

__copyright__ = """
(c) Copyright by Marc-Andre Lemburg (mailto:mal@lemburg.com)

    Permission to use, copy, modify, and distribute this software and its
    documentation for any purpose and without fee or royalty is hereby granted,
    provided that the above copyright notice appear in all copies and that
    both that copyright notice and this permission notice appear in
    supporting documentation or portions thereof, including modifications,
    that you make.

    THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO
    THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
    INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
    FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
    NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !

"""#"
import exceptions

class DatabaseTypeError(exceptions.TypeError):
    pass

class DatabaseInfo:

    def __init__(self):

        pass

    def stringcolumn(self, name, maxlength=254, padding=0, nullable=0,
                     default=None, casesensitive=0):

        """ Returns a column definition for a string like column
            type having the given properties.

            maxlength gives the maximum length of the column in bytes.

            padding means that values written to the database are
            right padded with spaces. When reading these values, the
            database may or may not remove the trailing spaces. When
            padding is true, the column will have a fixed size in the
            database.

            nullable defines whether the column should be allowed to
            have NULL values or not. Default is not to allows NULL
            values. Some databases also allow providing default values
            which are used in case a not nullable column would have to
            store a NULL value.

            casesensitive defines how sorting and searching should be
            done on the column. Binary data should always ba stored in
            casesensitive columns.

        """
        if padding:
            raise DatabaseTypeError,\
                  'padding of strings not supported'
        options = ''
        if maxlength < 256:
            if padding:
                coltype = 'char(%i)' % maxlength
            else:
                coltype = 'varchar(%i)' % maxlength
            if casesensitive:
                coltype = coltype + ' binary'
        elif maxlength < 65536:
            if casesensitive:
                coltype = 'blob'
            else:
                coltype = 'text'
        elif maxlength < 167772152:
            if casesensitive:
                coltype = 'mediumblob'
            else:
                coltype = 'mediumtext'
        elif maxlength <= 4294967296L:
            if casesensitive:
                coltype = 'mediumblob'
            else:
                coltype = 'mediumtext'
        else:
            raise DatabaseTypeError,\
                  'string too long'
        s = '%s %s' % (name, coltype)
        if not nullable:
            s = s + ' not null'
            if default is not None:
                s = s + ' default %s' % repr(default)
        return s

# The base class provides information valid for MySQL
MySQLInfo = DatabaseInfo

### Testing

if __name__ == '__main__':
    info = MySQLInfo()


--------------7D0E317C8101C2943DEAD030--