Python Datastore

Index Home MAE Python Interface > Python Index > Python Datastore

Module Datastore
Class Cache
Public Methods
__init__(self, objType, size)
__del__(self)
add(self, newKey, newOne)
removeKey(self, key)
remove(self, pos)
findPos(self, searchKey, fetchIfMissing)
exists(self, searchKey)
get(self, key)
__getitem__(self, pos)
__len__(self)
Class Datastore
Summary
Public Properties
Public Methods
__init__(self, tableName)
valid(self)
status(self)
statusOK(self)
isOpen(self)
statusMsg(self)
fieldNameToType(self, name)
fieldTypeToName(self, fieldtype)
errorMsg(self, rcToGetMsgFor)
open(self, tableName)
select(self, tableName)
connected(self)
pack(self)
close(self, specifiedTable)
getTableList(self)
addField(self, fieldName, type, size, table, isIndex)
getFieldId(self, fieldName, table)
addFieldEnum(self, fieldName, enumName, enumId, table)
getFieldType(self, fieldName, table)
addRecord(self, data, recno, table)
deleteRecord(self, recno, table)
lockRecord(self, recno, table)
unlockRecord(self, recno, table)
clearField(self, recno, fieldName, table)
setValue(self, recno, fieldName, value, table)
setRecord(self, recno, newValues, table)
getFieldInt(self, recno, fieldName, table)
getFieldString(self, recno, fieldName, table)
getFieldFloat(self, recno, fieldName, table)
getFieldEnumValue(self, recno, fieldName, table)
getRecord(self, recno, fields, table)
getFieldEnumValueAsInt(self, recno, fieldName, table)
queryRecordsEnum(self, fieldName, value, sortby, table)
queryRecords(self, fieldName, value, sortby, table)
queryRecords2(self, criteria, ANDed, sortby, table)
queryExpr(self, expression, fields, sortby, table)
queryAllRecords(self, sortby, table)
queryFieldNames(self, table)
queryFieldIds(self, table)
connect(self)
doCommand(self, cmd, params)
send(self, params)
receive(self)
encodeNewline(self, text)
unencodeNewline(self, text)

Module Datastore

Class Cache

Public Methods

__init__(self, objType, size)

objType

the class of the object to cache

size: int

the maximum size allowable for the cache

Default value: 0


Constructor.

__del__(self)

Destructor

add(self, newKey, newOne)

newKey: int

newOne


Add a record to the cache. It must be valid().

Return int value:

removeKey(self, key)

key


Remove a record from the cache

Return bool value:

remove(self, pos)

pos: int


Remove a record from the cache

Return bool value:

findPos(self, searchKey, fetchIfMissing)

searchKey

fetchIfMissing: bool

Default value: True


Locate the cache position (-1 if not found)

Note: Pull into cache if fetchIfNotFound is True

Note: key must be a positive (non-zero) number

Return int value:

exists(self, searchKey)

searchKey: int


Check if key exists in list

Return bool value:

get(self, key)

key


Get pointer to data for the key. Check if result is valid().

Note: key must be a positive (non-zero) number

__getitem__(self, pos)

pos: int

position in cache, from 0+


This method allows the class instance to be indexed, e.g. HelloCache[2] to get the second element (not key==2)

Return value: the instance at position

__len__(self)

Return the size of the cache when using the len() function

Class Datastore

Summary

This class abstracts database protocols and allows an app to use this interface to perform database table queries and changes.

This class connects to dbbroker, which actually performs the database manipulation.  Note that dbbroker can be configured to connect to a variety of databases; this gets rid of app responsibility for database connection and syntax; an app can simply use the database.

It is safe (and convenient) to create an instance of this class for each table your program uses.

If you have an existing database table and you want to create a class interface to it, consider using genmae like this:

genmae db2h tablename > classname.h

genmae py classname.h > classname.py

Likewise, if you create .h file with the core class variables, you can use genmae to create the SQL code to setup the table, like this:

genmae h2sql classname.h > tablename.sql

Note that database manipulation commands will block program execution until a response from dbbroker is received.

Public Properties

FieldType

Default value: Enum('FieldType', 'FieldInt', 'FieldTime', 'FieldEnum', 'FieldLogical', 'FieldString', 'FieldFloat', 'FieldDate', 'FieldBinary')

OK

DB operation was successful (no error)

Default value: 0

INVALID_NAME

DB operation failed - invalid name given

Default value: -1

OPEN

DB operation failed - cannot open table

Default value: -2

INVALID_TYPE

DB operation failed - invalid field type

Default value: -3

INVALID_FIELD

DB operation failed - invalid field name

Default value: -4

INVALID_REC

DB operation failed - invalid record id

Default value: -5

INVALID_ENUM

DB operation failed - invalid enum

Default value: -6

INVALID_PARAM

DB operation failed - invalid parameter

Default value: -7

LOCKED

DB operation failed - record locked

Default value: -8

NOT_SUPPORTED

DB operation failed - operation not supported

Default value: -9

FAILED

DB operation failed - (non-specific)

Default value: -10

REC_NOT_FOUND

DB operation failed - record not found

Default value: -11

ALREADY_EXISTS

DB operation failed - record already exists

Default value: -12

INVALID_SCHEMA

DB operation failed - invalid schema

Default value: -13

COMM_ERROR

DB operation failed - communications error

Default value: -14

CORRUPT_REPLY

DB operation failed - reply was corrupt (cannot parse)

Default value: -15

INVALID_CONN

DB operation failed - invalid connection

Default value: -16

INVALID_EXPR

DB operation failed - invalid expression

Default value: -17

LINEPAYLOADMAX

Maximum line length to send to dbbroker

Default value: 10000 - 100

PAYLOADMAX

Default value: 10000


Public Methods

__init__(self, tableName)

tableName: str

Default value:


Constructor to initialize our data structure.  This does not yet setup the connection to dbbroker.

valid(self)

Determine status of our connection to database - is it valid?

Return int value: True if connected to db

status(self)

Get the status of the last request

Return int value: the status of the last request (see codes at top of Datastore.py)

statusOK(self)

Determine if the status of the last operator was successful.

Return bool value: True if last operation successful, False otherwise.

isOpen(self)

Determine if the database connection is established and ready.

Return bool value: True if db connection ready

statusMsg(self)

Return human readable status of last request.

Return str value: msg corresponding to last DB request

fieldNameToType(self, name)

name: str


fieldTypeToName(self, fieldtype)

fieldtype


Return str value:

errorMsg(self, rcToGetMsgFor)

rcToGetMsgFor: int

the return code number to look up

Default value: 1


Get the status message of the specified return code.

Return str value: the text of the error message

open(self, tableName)

tableName: str

the name of the table to use as the current table.

If not successful, check status() for the code or statusMsg() for human readable description.


Set the currently active table by opening it.

Return int value: the status code of the operation.

select(self, tableName)

tableName: str

the name of the table to use as the current table.

If not successful, check status() for the code or statusMsg() for human readable description.


Identifical to open().  Set the currently active table by opening it.

Return int value: the status code of the operation.

connected(self)

Check to make sure the database connection is working.

Return bool value: True if db responding to our requests

pack(self)

Pack the database (remove deleted records), if the database supports this.

Otherwise, it is ignored.

Return bool value: True upon success, False upon failure (see statusMsg())

close(self, specifiedTable)

specifiedTable: str


Signal to close this database table.

@param table name of the table to close; if none, then last table used

Return bool value: True upon success, False upon failure (see statusMsg())

getTableList(self)

Get the list of defined tables.

Return value: the list of tables in the database

addField(self, fieldName, type, size, table, isIndex)

fieldName: str

name of field for operation

type

type of field (string, int, float)

size: int

(optional) size of field in bytes

table: str

name of table for operation

Default value:

isIndex: bool

True if this field is an index for the table

Default value: False


Obsolete. Add a field to the named database table.

Return 0 value: field id of new field

getFieldId(self, fieldName, table)

fieldName: str

name of the field for which to get the fieldid

table: str

name of table for operation

Default value:


Query field id from field name for the named table.

Return int value: nonzero on success, 0 on failure

addFieldEnum(self, fieldName, enumName, enumId, table)

fieldName: str

id of the enum field

enumName: str

name/identifier for enum

enumId: int

(optional) integer value to use for enum

table: str

name of table for operation

Default value:


Obsolete.  Add field enumeration to the nanmed table.

Return int value: new enum value

getFieldType(self, fieldName, table)

fieldName: str

id of the enum field

table: str

name of table for operation

Default value:


Query a field type (string, float, etc) for the provided field name in the provided table.

Return value: the field type

addRecord(self, data, recno, table)

data

any data to pre-populate the record

recno: int

(optional) id for the record

Default value: 0

table: str

name of table for operation

Default value:


Add/Append a new record to the table provided, populated with the data provided.

Return value: new record number added

deleteRecord(self, recno, table)

recno

number of the record to delete

table: str

name of table for operation

Default value:


Delete a record in the named table.

Return bool value: transaction status (see statusMsg())

lockRecord(self, recno, table)

recno

number of the record to lock

table: str

name of table for operation

Default value:


Lock a record in the named table; prevent other applications from modifying this record until it is unlocked.

Return bool value: transaction status (see statusMsg())

unlockRecord(self, recno, table)

recno

number of the record to unlock

table: str

name of table for operation

Default value:


Unlock a record in the named table, allowing other applications to now modify it.

Return bool value: transaction status (see statusMsg())

clearField(self, recno, fieldName, table)

recno

number of the record containing the field

fieldName: str

name of the field to clear

table: str

name of table for operation

Default value:


clear/empty the value for the field with this field name

Return bool value: transaction status (see statusMsg())

setValue(self, recno, fieldName, value, table)

recno

number of the record in which to set the value

fieldName: str

value

the value to set

table: str

name of table for operation

Default value:


change a string value identified by field ID

@param field name of the field in which to set the value

Return bool value: transaction status (see statusMsg())

setRecord(self, recno, newValues, table)

recno

number of the record in which to set the value

newValues

table: str

name of table for operation

Default value:


change several field values at once

@param newValue key/value pairs for new field/value changes

Return bool value: transaction status (see statusMsg())

getFieldInt(self, recno, fieldName, table)

recno

number of the record containing the value

fieldName: str

name of the field to get

table: str

name of table for operation

Default value:


get an int field value identified by field name

Return int value: the int value or -1 if unsuccessful

getFieldString(self, recno, fieldName, table)

recno

number of the record containing the value

fieldName: str

name of the field to get

table: str

name of table for operation

Default value:


get a string field value identified by field name

Return str value: the string value or zero length string if unsuccessful

getFieldFloat(self, recno, fieldName, table)

recno

number of the record containing the value

fieldName: str

name of the field to get

table: str

name of table for operation

Default value:


get a float field value identified by field name

Return float value: the float value or ??? if unsuccessful

getFieldEnumValue(self, recno, fieldName, table)

recno

number of the record containing the value

fieldName: str

name of the field to get

table: str

name of table for operation

Default value:


get an enum value identified by field name

Return str value: the enum value or zero length string if unsuccessful

getRecord(self, recno, fields, table)

recno

number of the record containing the value

fields

Default value: []

table: str

name of table for operation

Default value:


get an int field value identified by field name

@param fieldName name of the field to get

Return value: key/value pairs of field/value values from record.  Error if empty, check status()/statusMsg().

getFieldEnumValueAsInt(self, recno, fieldName, table)

recno

number of the record containing the value

fieldName: str

name of the field to get

table: str

name of table for operation

Default value:


get an enum value as an int identified by field name

Return int value: the enum value or zero length string if unsuccessful

queryRecordsEnum(self, fieldName, value, sortby, table)

fieldName: str

name of field to search

value: str

enum value to search for

sortby: str

Default value:

table: str

name of table for operation

Default value:


simple query for enum value by field name

Return list value: list of record numbers matching criteria

queryRecords(self, fieldName, value, sortby, table)

fieldName: str

Name of field to search, paired with the value; alternatively fieldName can be an XMLData object with key/value pairs to search for (if so, value is ignored)

value: str

string value to search for

Default value:

sortby: str

Default value:

table: str

name of table for operation

Default value:


simple query for string value by field name

Return list value: list of record numbers matching criteria

queryRecords2(self, criteria, ANDed, sortby, table)

criteria

ANDed: bool

Default value: True

sortby: str

Default value:

table: str

name of table for operation

Default value:


compound query for a field value

@param list list of field key/value pairs to be searched for

Return list value: list of record numbers matching criteria

queryExpr(self, expression, fields, sortby, table)

expression: str

using field names, comparison operators, and logic operators, specify which records match the query

fields: list

a list of fields to return

Default value: 'Id'

sortby: str

field name to use to sort the results start with + (default) for ascending sort, - for descending sort

Default value:

table: str

name of table for operation

Default value:


Using an expression, query records and return the requested fields, sorted if specified.

@param results - (output) a list of records and their fields that matched

Return bool value: True upon success

queryAllRecords(self, sortby, table)

sortby: str

Default value:

table: str

Default value:


query for all records in the named (or current) table

Return list value: recList with results which will be empty if unsuccessful

queryFieldNames(self, table)

table: str

the name of the table to query for fields

Default value:


query for all field names and types

Return value: XMLData which will be empty if unsuccessful. Otherwise key is field name, value is field type.

queryFieldIds(self, table)

table: str

the name of the table to query for fields

  1. @return XMLData which will be empty if unsuccessful. Otherwise key is field name, value is field id.

Default value:


query for all field names and types

connect(self)

Connect to database server.

In actuality, this only checks to make sure we're connected to the MAE daemon, commhub.

Return bool value: True on success; False on failure

doCommand(self, cmd, params)

cmd: str

params

Default value: None


Send a command for dbbroker that has no parameters. And wait for the reply.

Return bool value: True on success; False on failure

send(self, params)

params


Send data to commhub for DB operation

Return bool value: True if sent; False if failed

receive(self)

Wait for a dbbroker response from commhub.

Return bool value: True if reply received; False if not

encodeNewline(self, text)

text: str

the text to encode


commhub wants a comment to have new \n characters, so encode them

unencodeNewline(self, text)

text: str

the text to unencode


commhub didn't let dbbroker send any \n characters, so unencode them