Package web2py :: Package gluon :: Module dal :: Class DAL
[hide private]
[frames] | no frames]

Class DAL

source code

object --+    
         |    
      dict --+
             |
            DAL

an instance of this class represents a database connection

Example:
  db = DAL('sqlite://test.db')
  db.define_table('tablename', Field('fieldname1'),
                               Field('fieldname2'))


Nested Classes [hide private]
  Field
an instance of this class represents a database field
  Table
an instance of this class represents a database table
Instance Methods [hide private]
 
__init__(self, uri='sqlite://dummy.db', pool_size=0, folder=1, db_codec='UTF-8', check_reserved=1, migrate=True, fake_migrate=True, migrate_enabled=True, fake_migrate_all=True, decode_credentials=True, driver_args=1, adapter_args={}, attempts=5, auto_import=True)
Creates a new Database Abstraction Layer instance.
source code
 
import_table_definitions(self, path, migrate=True, fake_migrate=True) source code
 
check_reserved_keyword(self, name)
Validates ``name`` against SQL keywords Uses self.check_reserve which is a list of operators to use.
source code
 
__contains__(self, tablename)
Returns: True if D has a key k, else False
source code
 
parse_as_rest(self, patterns, args, vars, query=1, nested_select=True)
EXAMPLE:...
source code
 
define_table(self, tablename, *fields, **args) source code
 
__iter__(self)
iter(x)
source code
 
__getitem__(self, key)
x[y]
source code
 
__setitem__(self, key, value)
x[i]=y
source code
 
__getattr__(self, key) source code
 
__setattr__(self, key, value)
x.__setattr__('name', value) <==> x.name = value
source code
 
__repr__(self)
repr(x)
source code
 
__call__(self, query=1) source code
 
commit(self) source code
 
rollback(self) source code
 
executesql(self, query, placeholders=1, as_dict=True)
placeholders is optional and will always be None when using DAL if using raw SQL with placeholders, placeholders may be a sequence of values to be substituted in or, *if supported by the DB driver*, a dictionary with keys matching named placeholders in your SQL.
source code
 
_update_referenced_by(self, other) source code
 
export_to_csv_file(self, ofile, *args, **kwargs) source code
 
import_from_csv_file(self, ifile, id_map={}, null='<NULL>', unique='uuid', *args, **kwargs) source code

Inherited from dict: __cmp__, __delitem__, __eq__, __ge__, __getattribute__, __gt__, __hash__, __le__, __len__, __lt__, __ne__, __new__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __str__

Static Methods [hide private]
 
set_folder(folder)
# ## this allows gluon to set a folder for this thread # ## <<<<<<<<< Should go away as new DAL replaces old sql.py
source code
 
distributed_transaction_begin(*instances) source code
 
distributed_transaction_commit(*instances) source code
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, uri='sqlite://dummy.db', pool_size=0, folder=1, db_codec='UTF-8', check_reserved=1, migrate=True, fake_migrate=True, migrate_enabled=True, fake_migrate_all=True, decode_credentials=True, driver_args=1, adapter_args={}, attempts=5, auto_import=True)
(Constructor)

source code 

Creates a new Database Abstraction Layer instance.

Keyword arguments:

:uri: string that contains information for connecting to a database.
       (default: 'sqlite://dummy.db')
:pool_size: How many open connections to make to the database object.
:folder: <please update me>
:db_codec: string encoding of the database (default: 'UTF-8')
:check_reserved: list of adapters to check tablenames and column names
                 against sql reserved keywords. (Default None)

* 'common' List of sql keywords that are common to all database types
        such as "SELECT, INSERT". (recommended)
* 'all' Checks against all known SQL keywords. (not recommended)
        <adaptername> Checks against the specific adapters list of keywords
        (recommended)
* '<adaptername>_nonreserved' Checks against the specific adapters
        list of nonreserved keywords. (if available)
:migrate (defaults to True) sets default migrate behavior for all tables
:fake_migrate (defaults to False) sets default fake_migrate behavior for all tables
:migrate_enabled (defaults to True). If set to False disables ALL migrations
:fake_migrate_all (defaults to False). If sets to True fake migrates ALL tables
:attempts (defaults to 5). Number of times to attempt connecting

Returns:
new empty dictionary

Overrides: dict.__init__

check_reserved_keyword(self, name)

source code 
Validates ``name`` against SQL keywords Uses self.check_reserve which is a list of operators to use. self.check_reserved ['common', 'postgres', 'mysql'] self.check_reserved ['all']

__contains__(self, tablename)
(In operator)

source code 
Returns:
True if D has a key k, else False

Overrides: dict.__contains__
(inherited documentation)

parse_as_rest(self, patterns, args, vars, query=1, nested_select=True)

source code 

        EXAMPLE:

db.define_table('person',Field('name'),Field('info'))
db.define_table('pet',Field('person',db.person),Field('name'),Field('info'))

@request.restful()
def index():
    def GET(*kargs,**kvars):
        patterns = [
            "/persons[person]",
            "/{person.name.startswith}",
            "/{person.name}/:field",
            "/{person.name}/pets[pet.person]",
            "/{person.name}/pet[pet.person]/{pet.name}",
            "/{person.name}/pet[pet.person]/{pet.name}/:field"
            ]
        parser = db.parse_as_rest(patterns,kargs,kvars)
        if parser.status == 200:
            return dict(content=parser.response)
        else:
            raise HTTP(parser.status,parser.error)
    def POST(table_name,**kvars):
        if table_name == 'person':
            return db.person.validate_and_insert(**kvars)
        elif table_name == 'pet':
            return db.pet.validate_and_insert(**kvars)
        else:
            raise HTTP(400)
    return locals()
        

__iter__(self)

source code 
iter(x)
Overrides: dict.__iter__
(inherited documentation)

__getitem__(self, key)
(Indexing operator)

source code 
x[y]
Overrides: dict.__getitem__
(inherited documentation)

__setitem__(self, key, value)
(Index assignment operator)

source code 
x[i]=y
Overrides: dict.__setitem__
(inherited documentation)

__setattr__(self, key, value)

source code 
x.__setattr__('name', value) <==> x.name = value
Overrides: object.__setattr__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: dict.__repr__
(inherited documentation)

executesql(self, query, placeholders=1, as_dict=True)

source code 

placeholders is optional and will always be None when using DAL if using raw SQL with placeholders, placeholders may be a sequence of values to be substituted in or, *if supported by the DB driver*, a dictionary with keys matching named placeholders in your SQL.

Added 2009-12-05 "as_dict" optional argument. Will always be None when using DAL. If using raw SQL can be set to True and the results cursor returned by the DB driver will be converted to a sequence of dictionaries keyed with the db field names. Tested with SQLite but should work with any database since the cursor.description used to get field names is part of the Python dbi 2.0 specs. Results returned with as_dict = True are the same as those returned when applying .to_list() to a DAL query.

[{field1: value1, field2: value2}, {field1: value1b, field2: value2b}]

--bmeredyk