Database

DB

class dragonfly.db.database.DB(database_settings=<sphinx.ext.autodoc.importer._MockObject object>)

Bases: object

An easy way to interact with the configured database.

chunk(chunk_loc, chunk_size)

This will run the given query and return the given number of results at the given location.

Parameters:
  • chunk_loc (int) – The location to chunk e.g the first chunk or second chunk.
  • chunk_size (int) – The number of rows each chunk should contain.
comparison_operators = ['=', '<=>', '<>', '!=', '>', '>=', '<', '<=', 'IN()', 'NOT', 'BETWEEN', 'IS NULL', 'IS NOT NULL', 'LIKE', 'EXISTS']
custom_sql(sql, n_rows=None)

Execute the custom SQL passed to the function.

Parameters:
  • sql (str) – The SQL code to execute
  • n_rows (int) – The number of rows to retrieve. If set to None returns all rows
Returns:

The result of the SQL executed

Return type:

dict

delete()

Deletes the given row/rows baed on the developer defined query.

For this method to run the where method must have been called first.

first()

This will execute the developer defined query and return only the first result (uses LIMIT 1)

get()

This will execute the developer defined query and return all results.

insert(insert_dict)

Inserts the given values into the database.

Parameters:insert_dict (dict) – The dictionary containing the column and the value to insert into the specified table
multiple_where(where_dict)

Allows for multiple where clauses through one command. Note this only supports the = operator.

Parameters:where_dict (dict) – The values to match
select(*args)

Equivalent to the SELECT command in MySQL.

Pass all the columns you want to select to the function as normal arguments.

Example:DB().select('title', 'text')

Note

If you would like to select all (*) columns then simply do not use the select argument when

building your query.

table(table_name)

The table that the query should be run on. This method must be run for any query to be executed.

update(update_dict)

Updates the given row/rows based on the dictionary.

For this method to run the where method must have been called before this one.

Parameters:update_dict (dict) – The dictionary containing the column to update and the value to update it with.
where(condition_1, comparison_operator, condition_2)

Equivalent to the WHERE command in SQL.

Parameters:
  • condition_1 – The value to the left of the operator.
  • comparison_operator (str) – The comparison operator, e.g =
  • condition_2 – The value to the right of the operator.

Fields

Note

  • Please note that the majority of MySQL field types are available for usage. There name will just be the camel case of the MySQL type with Field appended.
  • All fields accept the following parameters: null, default, unique, primary_key. These values are, by default, False. Some fields will have extra parameters which can be seen below.
class dragonfly.db.models.fields.BigIntField(**kwargs)

Bases: dragonfly.db.models.fields.IntField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.BinaryField(**kwargs)

Bases: dragonfly.db.models.fields.StringField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.BitField(length=None, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.BoolField(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.CharField(**kwargs)

Bases: dragonfly.db.models.fields.StringField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

class dragonfly.db.models.fields.DateField(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.DateTimeField(fsp=None, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.DecimalField(digits=None, decimal_places=None, unsigned=False, zerofill=False, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.DoubleField(digits=None, decimal_places=None, unsigned=False, zerofill=False, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.Enum(*args, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.Field(name=None, null=False, blank=False, default=None, unique=False, primary_key=False)

Bases: abc.ABC

An abstract class that defines the interface each Field class should have.

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.FloatField(digits=None, unsigned=False, zerofill=False, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.ForeignKey(*args)

Bases: object

Provides a way to define foreign key relationships in a model.

Should be called in the following order:
ForeignKey(‘local_key’).refrences(‘foreign_key’).on(‘table’)
on(table)

The table the foreign keys are on.

Parameters:table – The table that the foreign keys are located on
Returns:This ForeignKey object.
Return type:ForeignKey
references(*args)

Defines the foreign keys that the local keys reference.

Parameters:args – A list of foreign keys that the defined local keys reference.
Returns:This ForeignKey object.
Return type:ForeignKey
class dragonfly.db.models.fields.IntField(length=None, unsigned=False, auto_increment=False, zerofill=False, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.LongBlob(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.MediumBlob(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.MediumIntField(**kwargs)

Bases: dragonfly.db.models.fields.IntField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.MediumText(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.PrimaryKey(*args)

Bases: object

Provides a way to make the given field(s) a primary key

class dragonfly.db.models.fields.Set(*args, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.SmallIntField(**kwargs)

Bases: dragonfly.db.models.fields.IntField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

class dragonfly.db.models.fields.StringField(length=None, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.TextField(**kwargs)

Bases: dragonfly.db.models.fields.StringField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

class dragonfly.db.models.fields.TimeField(fsp=None, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.TimestampField(fsp=None, on=None, **kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.TinyBlobField(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.TinyIntField(**kwargs)

Bases: dragonfly.db.models.fields.IntField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

class dragonfly.db.models.fields.TinyTextField(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert
class dragonfly.db.models.fields.Unique(*args)

Bases: object

Provides a way to make a field a model unique

class dragonfly.db.models.fields.VarCharField(**kwargs)

Bases: dragonfly.db.models.fields.StringField

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

class dragonfly.db.models.fields.YearField(**kwargs)

Bases: dragonfly.db.models.fields.Field

to_database_type()

This instructs the database migrator on how to generate the SQL for the field.

to_python_type(value)

This is how the value from the database should be converted to python. Note that at the moment this is not currently in use as the MySQLdb package does this automatically.

Parameters:value – The value to convert

Model

class dragonfly.db.models.model.Model(data=None)

Bases: object

A way to easily interact with rows in a table.

add_relationship(relationship_class, update=False)

Adds a relationship to another model to this model instance using the relationship classes. Note that the method will cache the values of a relationship unless update is set to true.

Parameters:
  • relationship_class (Relationship) – An instantiated relationship class.
  • update (bool) – If the method should retrieve fresh data from the database.
Returns:

The retrieved, related, model(s).

Return type:

Relationship

all()

Get all rows in the table .

Returns:A list object models
Return type:list
create(create_dict)

Creates a new row in the table and returns a model representation of this row.

Parameters:create_dict (dict) – The values to create the new row with
Returns:A list of object models
Return type:list
delete()

Delete this row from the database .

find(primary_key)

Find a row by passing in the value of the row’s primary key.

Note that if you would like to find a model with more than one key pass through a dictionary containing the column and value.

Example:Article().find({'id': 1, 'author': 1})
Parameters:primary_key (int) – The value of the primary key to find.
Returns:An object model that has the given value as its primary key
Return type:Model
first()

Get the first row in the table.

Returns:An object model
Return type:Model
get()

Get all rows in the table.

Returns:An object model
Return type:list
multiple_where(where_dict)

Select a row from the table using multiple values/columns

Parameters:where_dict (dict) – A dictionary where the key is the column in the table and the value is the value in the table.

:return This model object :rtype: Model

paginate(size, to_json=False)

Paginates the data in the table by the given size.

Note that if to_json is True a Response will be returned containing the appropriate JSON. Otherwise a list of rows that correspond to the page requested will be returned (the page number is known from the request object).

Parameters:
  • size (int) – The number of rows on each page
  • to_json (bool) – If the function should return a JSON response, default is False
Returns:

A tuple containing a dictionary if results and a dictionary contaning meta information

Return type:

tuple

save()

Permeate the changes made to the Python model to the database.

select(*args)

Same as the DB class select method. Note that a dictionary will be returned as a model cannot be represented with incomplete data.

Parameters:args – A list of columns to select
Returns:This model object
Return type:Model
to_dict()

Return a dictionary equivalent of this row.

Returns:A dictionary equivalent of this row
Return type:dict
update(update_dict)

Update this model with the given values.

Parameters:update_dict (dict) – Update the given columns (key) with the given values
Returns:This model object
Return type:Model
where(column, comparator, value)

Same as the DB class where method.

Parameters:
  • column (str) – The column in the database to check the value for
  • comparator (str) – The comparison operator e.g. =
  • value – The value to test for
Returns:

This model object

Return type:

Model

dragonfly.db.models.model.default(o)

Function from StackOverflow - https://stackoverflow.com/questions/12122007/python-json-encoder-to-support-datetime

Relationships

class dragonfly.db.models.relationships.BelongsTo(**kwargs)

Bases: dragonfly.db.models.relationships.Relationship

Retrieves the class that ‘owns’ the model this relationship is defined in.

delayed_init(values, meta)

This function is executed when data needs to be retrieved.

Parameters:
  • values (dict) – The database values of the given model
  • meta (dict) – The meta values of the model
Returns:

The relationship class

Return type:

Relationship

class dragonfly.db.models.relationships.HasMany(**kwargs)

Bases: dragonfly.db.models.relationships.Relationship

Retrieves all rows that have a have-many relationship with the model this class is initialised in.

delayed_init(values, meta)

This function is executed when data needs to be retrieved.

Parameters:
  • values (dict) – The database values of the given model
  • meta (dict) – The meta values of the model
Returns:

The relationship class

Return type:

Relationship

class dragonfly.db.models.relationships.ManyToMany(table=None, **kwargs)

Bases: dragonfly.db.models.relationships.Relationship

delayed_init(values, meta)

This function is executed when data needs to be retrieved.

Parameters:
  • values (dict) – The database values of the given model
  • meta (dict) – The meta values of the model
Returns:

The relationship class

Return type:

Relationship

class dragonfly.db.models.relationships.Relationship(target, this_key=None, target_key=None)

Bases: abc.ABC

An abstract class that defines the interface each Relationship class should have

delayed_init(values, meta)

This function is executed when data needs to be retrieved.

Parameters:
  • values (dict) – The database values of the given model
  • meta (dict) – The meta values of the model
Returns:

The relationship class

Return type:

Relationship

Warning

The following classes should not be called directly.

DatabaseMigrator

class dragonfly.db.database_migrator.DatabaseMigrator(path='models')

Bases: object

Generates the SQL to create a table that corresponds to the defined model(s)

Table

class dragonfly.db.table.Table

Bases: object

Returns the MySQL code to create a column in a table with the given type.

static bigint(*args, **kwargs)
static binary(*args, **kwargs)
static bit(*args, **kwargs)
static blob(*args, **kwargs)
static boolean(*args, **kwargs)
static char(*args, **kwargs)
static date(*args, **kwargs)
static datetime(*args, **kwargs)
static decimal(*args, **kwargs)
static double(*args, **kwargs)
static enum(*args, **kwargs)
static float(*args, **kwargs)
static foreign_key(constraint_name, table, local_keys, foreign_keys)
static integer(*args, **kwargs)
static longblob(*args, **kwargs)
static longtext(*args, **kwargs)
static mediumblob(*args, **kwargs)
static mediumint(*args, **kwargs)
static mediumtext(*args, **kwargs)
static primary_key(*args)
static set(*args, **kwargs)
static smallint(*args, **kwargs)
static text(*args, **kwargs)
static time(*args, **kwargs)
static timestamp(*args, **kwargs)
static tinyblob(*args, **kwargs)
static tinyint(*args, **kwargs)
static tinytext(*args, **kwargs)
static unique(*args, constraint_name=None)
static varbinary(*args, **kwargs)
static varchar(*args, **kwargs)
static year(*args, **kwargs)
dragonfly.db.table.handle_options(func)

Handles any extra options for the methods on the Table class