sql Module

This file is part of the web2py Web Framework
Developed by Massimo Di Pierro <mdipierro@cs.depaul.edu>,
limodou <limodou@gmail.com> and srackham <srackham@gmail.com>.

Just for backward compatibility

class gluon.sql.DAL(uri='sqlite://dummy.db', pool_size=0, folder=None, db_codec='UTF-8', check_reserved=None, migrate=True, fake_migrate=False, migrate_enabled=True, fake_migrate_all=False, decode_credentials=False, driver_args=None, adapter_args=None, attempts=5, auto_import=False, bigint_id=False, debug=False, lazy_tables=False, db_uid=None, do_connect=True, after_connection=None, tables=None, ignore_field_case=True, entity_quoting=False, table_hash=None)[source]

Bases: pydal.helpers.classes.Serializable, pydal.helpers.classes.BasicStorage

An instance of this class represents a database connection

Parameters:
  • uri (str) –

    contains information for connecting to a database. Defaults to ‘sqlite://dummy.db’

    Note

    experimental: you can specify a dictionary as uri parameter i.e. with:

    db = DAL({"uri": "sqlite://storage.sqlite",
              "tables": {...}, ...})
    

    for an example of dict input you can check the output of the scaffolding db model with

    db.as_dict()

    Note that for compatibility with Python older than version 2.6.5 you should cast your dict input keys to str due to a syntax limitation on kwarg names. for proper DAL dictionary input you can use one of:

    obj = serializers.cast_keys(dict, [encoding="utf-8"])
    #or else (for parsing json input)
    obj = serializers.loads_json(data, unicode_keys=False)
    
  • pool_size – How many open connections to make to the database object.
  • folder – where .table files will be created. Automatically set within web2py. Use an explicit path when using DAL outside web2py
  • db_codec – string encoding of the database (default: ‘UTF-8’)
  • table_hash – database identifier with .tables. If your connection hash change you can still using old .tables if they have db_hash as prefix
  • check_reserved

    list of adapters to check tablenames and column names against sql/nosql reserved keywords. Defaults to 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
    • ’<adaptername>’’ Checks against the specific adapters list of keywords
    • ’<adaptername>_nonreserved’ Checks against the specific adapters list of nonreserved keywords. (if available)
  • migrate – sets default migrate behavior for all tables
  • fake_migrate – sets default fake_migrate behavior for all tables
  • migrate_enabled – If set to False disables ALL migrations
  • fake_migrate_all – If set to True fake migrates ALL tables
  • attempts – Number of times to attempt connecting
  • auto_import – If set to True, tries import automatically table definitions from the databases folder (works only for simple models)
  • bigint_id – If set, turn on bigint instead of int for id and reference fields
  • lazy_tables – delays table definition until table access
  • after_connection – can a callable that will be executed after the connection

Example

Use as:

db = DAL('sqlite://test.db')

or:

db = DAL(**{"uri": ..., "tables": [...]...}) # experimental

db.define_table('tablename', Field('fieldname1'),
                             Field('fieldname2'))
class Field(fieldname, type='string', length=None, default=<function <lambda>>, required=False, requires=<function <lambda>>, ondelete='CASCADE', notnull=False, unique=False, uploadfield=True, widget=None, label=None, comment=None, writable=True, readable=True, update=None, authorize=None, autodelete=False, represent=None, uploadfolder=None, uploadseparate=False, uploadfs=None, compute=None, custom_store=None, custom_retrieve=None, custom_retrieve_file_properties=None, custom_delete=None, filter_in=None, filter_out=None, custom_qualifier=None, map_none=None, rname=None)

Bases: pydal.objects.Expression, pydal.helpers.classes.Serializable

Lazy

alias of FieldMethod

Method

alias of FieldMethod

Virtual

alias of FieldVirtual

as_dict(flat=False, sanitize=True)
clone(point_self_references_to=False, **args)
count(distinct=None)
formatter(value)
retrieve(name, path=None, nameonly=False)

If nameonly==True return (filename, fullfilename) instead of (filename, stream)

retrieve_file_properties(name, path=None)
set_attributes(*args, **attributes)
sqlsafe
sqlsafe_name
store(file, filename=None, path=None)
validate(value)
class Row(*args, **kwargs)

Bases: pydal.helpers.classes.BasicStorage

A dictionary that lets you do d[‘a’] as well as d.a this is only used to store a Row

as_dict(datetime_to_str=False, custom_types=None)
as_json(mode='object', default=None, colnames=None, serialize=True, **kwargs)

serializes the row to a JSON object kwargs are passed to .as_dict method only “object” mode supported

serialize = False used by Rows.as_json

TODO: return array mode with query column order

mode and colnames are not implemented

as_xml(row_name='row', colnames=None, indent=' ')
get(key, default=None)
class Table(db, tablename, *fields, **args)

Bases: pydal.helpers.classes.Serializable, pydal.helpers.classes.BasicStorage

Represents a database table

Example::
You can create a table as::
db = DAL(…) db.define_table(‘users’, Field(‘name’))

And then:

db.users.insert(name='me') # print db.users._insert(...) to see SQL
db.users.drop()
as_dict(flat=False, sanitize=True)
bulk_insert(items)

here items is a list of dictionaries

drop(mode='')
fields
import_from_csv_file(csvfile, id_map=None, null='<NULL>', unique='uuid', id_offset=None, *args, **kwargs)

Import records from csv file. Column headers must have same names as table fields. Field ‘id’ is ignored. If column names read ‘table.file’ the ‘table.’ prefix is ignored.

  • ‘unique’ argument is a field which must be unique (typically a uuid field)
  • ‘restore’ argument is default False; if set True will remove old values in table first.
  • ‘id_map’ if set to None will not map ids

The import will keep the id numbers in the restored table. This assumes that there is an field of type id that is integer and in incrementing order. Will keep the id numbers in restored table.

insert(**fields)
on(query)
sqlsafe
sqlsafe_alias
truncate(mode=None)
update(*args, **kwargs)
update_or_insert(_key=<function <lambda>>, **values)
validate_and_insert(**fields)
validate_and_update(_key=<function <lambda>>, **fields)
validate_and_update_or_insert(_key=<function <lambda>>, **fields)
with_alias(alias)
as_dict(flat=False, sanitize=True)[source]
can_join()[source]
check_reserved_keyword(name)[source]

Validates name against SQL keywords Uses self.check_reserve which is a list of operators to use.

close()[source]
commit()[source]
define_table(tablename, *fields, **args)[source]
static distributed_transaction_begin(*instances)[source]
static distributed_transaction_commit(*instances)[source]
executesql(query, placeholders=None, as_dict=False, fields=None, colnames=None, as_ordered_dict=False)[source]

Executes an arbitrary query

Parameters:
  • query (str) – the query to submit to the backend
  • placeholders – is optional and will always be None. 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.
  • as_dict – 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. Results returned with as_dict=True are the same as those returned when applying .to_list() to a DAL query. If “as_ordered_dict”=True the behaviour is the same as when “as_dict”=True with the keys (field names) guaranteed to be in the same order as returned by the select name executed on the database.
  • fields

    list of DAL Fields that match the fields returned from the DB. The Field objects should be part of one or more Table objects defined on the DAL object. The “fields” list can include one or more DAL Table objects in addition to or instead of including Field objects, or it can be just a single table (not in a list). In that case, the Field objects will be extracted from the table(s).

    Note

    if either fields or colnames is provided, the results will be converted to a DAL Rows object using the db._adapter.parse() method

  • colnames – list of field names in tablename.fieldname format

Note

It is also possible to specify both “fields” and the associated “colnames”. In that case, “fields” can also include DAL Expression objects in addition to Field objects. For Field objects in “fields”, the associated “colnames” must still be in tablename.fieldname format. For Expression objects in “fields”, the associated “colnames” can be any arbitrary labels.

DAL Table objects referred to by “fields” or “colnames” can be dummy tables and do not have to represent any real tables in the database. Also, note that the “fields” and “colnames” must be in the same order as the fields in the results cursor returned from the DB.

export_to_csv_file(ofile, *args, **kwargs)[source]
static get_instances()[source]

Returns a dictionary with uri as key with timings and defined tables:

{'sqlite://storage.sqlite': {
    'dbstats': [(select auth_user.email from auth_user, 0.02009)],
    'dbtables': {
        'defined': ['auth_cas', 'auth_event', 'auth_group',
            'auth_membership', 'auth_permission', 'auth_user'],
        'lazy': '[]'
        }
    }
}
has_representer(name)[source]
import_from_csv_file(ifile, id_map=None, null='<NULL>', unique='uuid', map_tablenames=None, ignore_missing_tables=False, *args, **kwargs)[source]
import_table_definitions(path, migrate=False, fake_migrate=False, tables=None)[source]
lazy_define_table(tablename, *fields, **args)[source]
logger = <logging.Logger object>
parse_as_rest(patterns, args, vars, queries=None, nested_select=True)[source]

Example

Use as:

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

@request.restful()
def index():
    def GET(*args,**vars):
        patterns = [
            "/friends[person]",
            "/{person.name}/:field",
            "/{person.name}/pets[pet.ownedby]",
            "/{person.name}/pets[pet.ownedby]/{pet.name}",
            "/{person.name}/pets[pet.ownedby]/{pet.name}/:field",
            ("/dogs[pet]", db.pet.info=='dog'),
            ("/dogs[pet]/{pet.name.startswith}", db.pet.info=='dog'),
            ]
        parser = db.parse_as_rest(patterns,args,vars)
        if parser.status == 200:
            return dict(content=parser.response)
        else:
            raise HTTP(parser.status,parser.error)

    def POST(table_name,**vars):
        if table_name == 'person':
            return db.person.validate_and_insert(**vars)
        elif table_name == 'pet':
            return db.pet.validate_and_insert(**vars)
        else:
            raise HTTP(400)
    return locals()
represent(name, *args, **kwargs)[source]
representers = {'rows_render': <function represent at 0x7ff70774eb18>, 'rows_xml': <class 'gluon.sqlhtml.SQLTABLE'>}
rollback()[source]
serializers = {'json': <function custom_json at 0x7ff7079ec488>, 'xml': <function xml at 0x7ff7079ec578>}
static set_folder(folder)[source]
smart_query(fields, text)[source]
tables
uuid()
validators = None
validators_method(field)

Field type validation, using web2py’s validators mechanism.

makes sure the content of a field is in line with the declared fieldtype

where(query=None, ignore_common_filters=None)[source]
class gluon.sql.Field(fieldname, type='string', length=None, default=<function <lambda>>, required=False, requires=<function <lambda>>, ondelete='CASCADE', notnull=False, unique=False, uploadfield=True, widget=None, label=None, comment=None, writable=True, readable=True, update=None, authorize=None, autodelete=False, represent=None, uploadfolder=None, uploadseparate=False, uploadfs=None, compute=None, custom_store=None, custom_retrieve=None, custom_retrieve_file_properties=None, custom_delete=None, filter_in=None, filter_out=None, custom_qualifier=None, map_none=None, rname=None)[source]

Bases: pydal.objects.Expression, pydal.helpers.classes.Serializable

Lazy

Represents a database field

Example

Usage:

a = Field(name, 'string', length=32, default=None, required=False,
    requires=IS_NOT_EMPTY(), ondelete='CASCADE',
    notnull=False, unique=False,
    uploadfield=True, widget=None, label=None, comment=None,
    uploadfield=True, # True means store on disk,
                      # 'a_field_name' means store in this field in db
                      # False means file content will be discarded.
    writable=True, readable=True, update=None, authorize=None,
    autodelete=False, represent=None, uploadfolder=None,
    uploadseparate=False # upload to separate directories by uuid_keys
                         # first 2 character and tablename.fieldname
                         # False - old behavior
                         # True - put uploaded file in
                         #   <uploaddir>/<tablename>.<fieldname>/uuid_key[:2]
                         #        directory)
    uploadfs=None        # a pyfilesystem where to store upload
    )

to be used as argument of DAL.define_table

alias of FieldMethod

Method

alias of FieldMethod

Virtual

alias of FieldVirtual

as_dict(flat=False, sanitize=True)[source]
clone(point_self_references_to=False, **args)[source]
count(distinct=None)[source]
formatter(value)[source]
retrieve(name, path=None, nameonly=False)[source]

If nameonly==True return (filename, fullfilename) instead of (filename, stream)

retrieve_file_properties(name, path=None)[source]
set_attributes(*args, **attributes)[source]
sqlsafe
sqlsafe_name
store(file, filename=None, path=None)[source]
validate(value)[source]