RestOC.Record_MySQL module

Record SQL Module

Extends Record module to add support for SQL tables

class RestOC.Record_MySQL.Commands

Bases: object

Commands class

Used to directly interface with MySQL

classmethod escape(host, value)

Escape

Used to escape string values for the DB

Parameters
  • host (str) – The name of the connection to escape for

  • value (str) – The value to escape

  • rel (str) – The relationship of the server, master or slave

Returns

str

classmethod execute(host, sql, errcnt=0)

Execute

Used to run SQL that doesn’t return any rows

Parameters
  • host (str) – The name of the connection to execute on

  • sql (str|tuple) – The SQL (or SQL plus a list) statement to run

  • errcnt (uint) – DO NOT SET, used internally

Returns

uint

classmethod insert(host, sql, errcnt=0)

Insert

Handles INSERT statements and returns the new ID. To insert records without auto_increment it’s best to just stick to CSQL.execute()

Parameters
  • host (str) – The name of the connection to into on

  • sql (str) – The SQL statement to run

  • errcnt (uint) – DO NOT SET, used internally

Returns

mixed

classmethod select(host, sql, seltype=<ESelect.ALL: 1>, field=None, errcnt=0)

Select

Handles SELECT queries and returns the data

Parameters
  • host (str) – The name of the host to select from

  • sql (str) – The SQL statement to run

  • seltype (ESelect) – The format to return the data in

  • field (str) – Only used by HASH_ROWS since MySQLdb has no ordereddict for associative rows

  • errcnt (uint) – DO NOT SET, used internally

Returns

mixed

exception RestOC.Record_MySQL.DuplicateException

Bases: Exception

DuplicateException class

Used for raising issues with duplicate records

class RestOC.Record_MySQL.ESelect

Bases: enum.IntEnum

An enumeration.

ALL = 1
CELL = 2
COLUMN = 3
HASH = 4
HASH_ROWS = 5
ROW = 6
class RestOC.Record_MySQL.Literal(text)

Bases: object

Used as a value that won’t be escaped or parsed

get()
class RestOC.Record_MySQL.Record(record={}, custom={})

Bases: RestOC.Record_Base.Record

Extends the base Record class

classmethod addChanges(_id, changes, custom={})

Add Changes

Adds a record to the tables associated _changes table. Useful for Record types that can’t handle multiple levels and have children tables that shouldn’t be updated for every change in a single record

Parameters
  • _id (mixed) – The ID of the record the change is associated with

  • changes (dict) – The dictionary of changes to add

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

classmethod append(_id, array, item, custom={})

Append

Adds an item to a given array/list for a specific record

Parameters
  • _id (mixed) – The ID of the record to append to

  • array (str) – The name of the field with the array

  • item (mixed) – The value to append to the array

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

classmethod config()

Config

Returns the configuration data associated with the record type

Returns

dict

classmethod contains(_id, array, item, custom={})

Contains

Checks if a specific item exist inside a given array/list

Parameters
  • _id (mixed) – The ID of the record to check

  • array (str) – The name of the field with the array

  • item (mixed) – The value to check for in the array

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

classmethod count(_id=None, filter=None, custom={})

Count

Returns the number of records associated with index or filter

Parameters
  • _id (mixed) – The ID(s) to check

  • filter (dict) – Additional filter

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

create(conflict='error', changes=None)

Create

Adds the record to the DB and returns the primary key

Parameters
  • conflict (str|list) – Must be one of ‘error’, ‘ignore’, ‘replace’, or a list of fields to update

  • changes (dict) – Data needed to store a change record, is dependant on the ‘changes’ config value

Returns

mixed|None

classmethod createMany(records, conflict='error', custom={})

Create Many

Inserts multiple records at once, returning all their primary keys if auto_primary is true, else just returning the number of records inserted (or replaced if replace is set to True)

Parameters
  • records (Record_MySQL.Record[]) – A list of Record instances to insert

  • conflict (str) – Must be one of ‘error’, ‘ignore’, ‘replace’

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

delete(changes=None)

Delete

Deletes the record represented by the instance

Parameters

changes (dict) – Data needed to store a change record, is dependant on the ‘changes’ config value

Returns

bool

classmethod deleteGet(_id=None, index=None, custom={})

Delete Get

Deletes one or many records by primary key or index and returns how many were found/deleted

Parameters
  • _id (mixed|mixed[]) – The primary key(s) to delete or None for all records

  • index (str) – Used as the index instead of the primary key

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

int

classmethod escape(host, type_, value)

Escape

Takes a value and turns it into an acceptable string for SQL

Parameters
  • host (str) – The name of the host if we need to call the server

  • type_ (str) – The type of data to escape

  • value (mixed) – The value to escape

Returns

str

classmethod exists(_id, index=None, custom={})

Exists

Returns true if the specified primary key or unique index value exists

Parameters
  • _id (mixed) – The primary key to check

  • index (str) – Used as the index instead of the primary key

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

fieldSet(field, val)

Field Set

Overwrites Record_Base.Record.fieldSet to allow for setting Literals, values that are not verified and then sent to the server as is

Parameters
  • field (str) – The name of the field to set

  • val (mixed) – The value to set the field to

Returns

self for chaining

Raises
  • KeyError – field doesn’t exist in the structure of the record

  • ValueError – value is not valid for the field

classmethod filter(fields, raw=None, distinct=False, orderby=None, limit=None, custom={})

Filter

Finds records based on the specific fields and values passed

Parameters
  • fields (dict) – A dictionary of field names to the values they should match

  • raw (bool|list) – Return raw data (dict) for all or a set list of fields

  • distinct (bool) – Only return distinct data

  • orderby (str|str[]) – A field or fields to order the results by

  • limit (int|tuple) – The limit and possible starting point

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

Record[]|dict[]

classmethod generateConfig(tree, special='mysql', db=None)

Generate Config

Generates record specific config based on the Format-OC tree passed

Parameters
  • tree (FormatOC.Tree) – the tree associated with the record type

  • special (str) – The special section used to identify the child info

Returns

dict

classmethod get(_id=None, index=None, filter=None, match=None, raw=None, distinct=False, orderby=None, limit=None, custom={})

Get

Returns records by primary key or index, can also be given an extra filter

Parameters
  • _id (str|str[]) – The primary key(s) to fetch from the table

  • index (str) – N/A in MySQL

  • filter (dict) – Additional filter

  • match (tuple) – N/A in MySQL

  • raw (bool|list) – Return raw data (dict) for all or a set list of fields

  • distinct (bool) – Only return distinct data

  • orderby (str|str[]) – A field or fields to order the results by

  • limit (int|tuple) – The limit and possible starting point

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

Record|Record[]|dict|dict[]

classmethod getChanges(_id, orderby=None, custom={})

Get Changes

Returns the changes record associated with the primary record and table. Used by Record types that have the ‘changes’ flag set

Parameters
  • _id (mixed) – The of the primary record to fetch changes for

  • orderby (str|str[]) – A field or fields to order the results by

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

dict

classmethod processValue(struct, field, value)

Process Value

Takes a field and a value or values and returns the proper SQL to look up the values for the field

Parameters
  • struct (dict) – The structure associated with the record

  • field (str) – The name of the field

  • value (mixed) – The value as a single item, list, or dictionary

Returns

str

classmethod remove(_id, array, index, custom={})

Remove

Removes an item from a given array/list for a specific record

Parameters
  • _id (mixed) – The ID of the record to remove from

  • array (str) – The name of the field with the array

  • index (uint) – The index of the array to remove

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

save(replace=False, changes=None)

Save

Updates the record in the DB and returns true if anything has changed, or a new revision number of the record is revisionable

Parameters
  • replace (bool) – If true, replace all fields instead of updating

  • changes (dict) – Data needed to store a change record, is dependant on the ‘changes’ config value

Returns

bool

classmethod tableCreate(custom={})

Table Create

Creates the record’s table/collection/etc in the DB

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

classmethod tableDrop(custom={})

Table Drop

Deletes the record’s table/collection/etc in the DB

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

bool

classmethod updateField(field, value, _id=None, index=None, filter=None, custom={})

Updated Field

Updates a specific field to the value for an ID, many IDs, or the entire table

Parameters
  • field (str) – The name of the field to update

  • value (mixed) – The value to set the field to

  • _id (mixed) – Optional ID(s) to filter by

  • index (str) – Optional name of the index to use instead of primary

  • filter (dict) – Optional filter list to decide what records get updated

  • custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

uint – Number of records altered

classmethod uuid(custom={})

UUID

Returns a universal unique ID

Parameters

custom (dict) – Custom Host and DB info ‘host’ the name of the host to get/set data on ‘append’ optional postfix for dynamic DBs

Returns

str

RestOC.Record_MySQL.addHost(name, info, update=False)

Add Host

Add a host that can be used by Records

Parameters
  • name (str) – The name that will be used to fetch the host credentials

  • info (dict) – The necessary credentials to connect to the host

Returns

bool

RestOC.Record_MySQL.dbCreate(name, host='primary', charset=None, collate=None)

DB Create

Creates a DB on the given host

Parameters
  • name (str) – The name of the DB to create

  • host (str) – The name of the host the DB will be on

  • charset (str) – Optional default charset

  • collate (str) – Optional default collate, charset must be set to use

Returns

bool

RestOC.Record_MySQL.dbDrop(name, host='primary')

DB Drop

Drops a DB on the given host

Parameters
  • name (str) – The name of the DB to delete

  • host (str) – The name of the host the DB is on

Returns

bool

RestOC.Record_MySQL.verbose(set_=None)

Verbose

Sets/Gets the debug flag

Parameters

set_ (bool|None) – Ignore to get the current value

Returns

bool|None