Package biana :: Package BianaDB :: Module ConnectorDB :: Class DB
[hide private]
[frames] | no frames]

Class DB

source code


Class for establishing conexions to pianaDB and handling inserts and selects

Instance Methods [hide private]
 
__init__(self, dbname=None, dbhost=None, dbuser=None, dbpassword=None, dbport=None, dbsocket=None, buffer=True, lock_tables=False)
"dbname" is the database name to which you want to connect to (required)
source code
 
__getstate__(self) source code
 
__setstate__(self, dict) source code
 
__getnewargs__(self) source code
 
__str__(self)
str(x)
source code
 
close(self)
Closes the connection with the database
source code
 
check_consistency_with_given_source_version(self, source_code_version) source code
 
insert_db_content(self, sql_query, answer_mode=None)
Inserts values into a piana database (connection was established in self.db)
source code
 
select_db_content(self, sql_query=None, answer_mode='single', remove_duplicates='yes', number_of_selected_elems=1)
: Returns content from a piana database (connection was established in self.db)
source code
 
add_autoincrement_columns(self, table, attribute) source code
 
get_next_autoincrement(self, table, attribute) source code
 
_get_last_stable_autoincrement(self, table, attribute) source code
 
_get_current_autoincrement(self, table, attribute) source code
 
set_lock_tables(self, value) source code
 
_check_locked_table(self, table) source code
 
_get_source_code_version_in_db(self)
Fetches value in source_code_version field in BianaDatabase table
source code
 
_set_source_code_version_in_db(self, source_code_version)
Sets value in source_code_version field in BianaDatabase table
source code
 
_get_max_value(self, table, column)
OBSOLETE - last_ fields in BianaDatabase is used now
source code
 
_get_max_packet(self)
Returns the maximum packet size that the sql server accepts
source code
 
check_database(self, database, ignore_primary_keys=False, verbose=False)
Method for checking database
source code
 
GetTableNames(self)
Generates the SQL statement that gets all database table names
source code
 
_get_select_sql_query(self, tables, columns=None, fixed_conditions=None, join_conditions=None, group_conditions=None, distinct_columns=False)
Generates a general select sql statement
source code
 
_get_delete_sql_query(self, table, fixed_conditions=None)
Generates a general delete sql statement
source code
 
_get_union_queries(self, queries)
"queries" is the list of queries to make the union
source code
 
_get_cond_str(self, conditions) source code
 
_get_nested_insert_sql_query(self, table, columns, subquery) source code
 
_get_insert_sql_query(self, table, column_values, special_column_values=[], use_buffer=True, max_elements_in_buffer=None, on_duplicate_key='IGNORE')
Generates a general sql statement
source code
 
_get_buffer_multiple_queries(self, key_buffer=None)
Returns all queries of insert buffer and empties it
source code
 
_get_multiple_insert_query(self, table, columns, values)
"columns" must be a tuple with the name of the columns
source code
 
_uses_buffer(self)
Returns False if it is not using buffer
source code
 
_get_buffer_key(self, table, columns)
columns must be a list of the columns
source code
 
_lock_all_tables(self) source code
 
_lock_tables(self, table_list=None)
Method used to Lock tables.
source code
 
_unlock_tables(self)
Generates the SQL statement that unlocks tables previously locked
source code
 
_get_table_names(self) source code
 
_disable_indices(self, table_list=None) source code
 
_enable_indices(self, table_list=None) source code
 
_get_update_sql_query(self, table, update_column_values, fixed_conditions=None) source code
 
set_lock_frequency(self, frequency_value)
Method to change the lock/unlock frequency (used only in parsers, to speed up insertions and deletions
source code
 
_check_lock_frequency(self) source code
 
_get_drop_sql_query(self, table_list) source code
 
_empty_buffer(self)
It only can be used if insert buffer is being used
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, dbname=None, dbhost=None, dbuser=None, dbpassword=None, dbport=None, dbsocket=None, buffer=True, lock_tables=False)
(Constructor)

source code 

"dbname" is the database name to which you want to connect to (required)

"dbhost" is the machine with the mysql server that holds the piana database (required)

"dbuser" is the mysql user (not required in most systems)

"dbpassword" is the mysql password (not required in most systems)

"dbport" is the mysql port (not required in most systems)

"buffer" determines if an insert buffer is going to be used. Default is True. Alert! DB Object must be closed before in order to empty the buffer! Buffer is used due to performance when parsing data

"lock_tables" is used to lock the used tables

Overrides: object.__init__

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

insert_db_content(self, sql_query, answer_mode=None)

source code 

Inserts values into a piana database (connection was established in self.db)

Depending on argument "answer_mode", different things are returned.

This method is called by PianaDBaccess to then process the results and return them to the user

"slq_query" is normally obtained through classes implemented in PianaInsertSQL.py, which have a method get_sqlquery
that creates the sql query needed to retrieve the searched value.

"answer_mode" can be one of the following:

- None: nothing is returned
- 'last_id' : last id inserted is returned
- 'num_updated' : number of rows that were updated (used by UPDATE statements...)

  --> 'last_id' mode only works for those tables that have an auto_increment ID!!!!!!! Will not work with primary keys that are not
      auto_increment. Currently, following tables have auto_increment ids: protein (ID=proteinPiana) and interaction (ID=interactionPiana)

"buffer" indicates if this sql_query has to be inserted or not. It can be:
- None: sql_query will be treated as a sql_query and it will be executed (if possible)
- dictionary: the insert has been inserted into the dictionary buffer. So, the sql_query will not be executed. (So, sql_query can be None too. It will be ignored.

select_db_content(self, sql_query=None, answer_mode='single', remove_duplicates='yes', number_of_selected_elems=1)

source code 

:        Returns content from a piana database (connection was established in self.db)
        
        "slq_query" is normally obtained through classes implemented in PianaSelectSQL.py, which have a method get_sqlquery
        that creates the sql query needed to retrieve the searched value
        
        "answer_mode" is used to let the user choose what kind of answer he is expecting from the sql query
        answer_mode can take values (default is "single"):
        - "single": just one element (if nothing is found, returns None)
        - "list": a list of single elements (if nothing is found, returns empty list [])
        - "raw":  the raw result from the sql query (a matrix)
        
        "remove_duplicates" is used to let the user decide if the returning list can contain duplicates or not
        (only used when answer_mode="list")
        (this is useful to remove similar entries from one query, for example same uniprot accession numbers returned
        that are actually the same under different swissAccession source DB )
        
        - "yes" will return a list where all elements are unique
        - "no" will return the complete list returned by the sql query
        
        "number_of_selected_elems" sets the number of elements being selected in the sql query.
        - If 1, only the string of the element is returned. 
        - If >1, each group of elements is represented by a tuple (elem1, elem2, ...)
        - if you want to use a number_of_selected_elems higher than 3, you have to modify the code below
        to create keys of the number of elements you wish to select
        

check_database(self, database, ignore_primary_keys=False, verbose=False)

source code 

Method for checking database

It checks if all tables are existing

It does not drop any table

For the moment, it does not check default value of table fields

_get_select_sql_query(self, tables, columns=None, fixed_conditions=None, join_conditions=None, group_conditions=None, distinct_columns=False)

source code 

Generates a general select sql statement

"tables" is a list or tuple of tables where the value/s must be searched. If the elements of the list or tuple are tuples of length 2, the format taken will be the following:

              (table_name or table_object, alias to the table)

"columns" is a list or tuple of the columns searched (columns must be preceeded by the table where they are searched). If it is None, all values will be selected

"fixed_conditions" is a list or tuple of tuples with the following format: (column,type,restriction_value)

"join_conditions" is a list or tuple of tuples with the following format: (column,type,column) to restrict the selection to the joint

"type" can be "=",">","<",...

"group_conditions" is a list of columns where it must be grouped 

It returns the sql query.

_get_delete_sql_query(self, table, fixed_conditions=None)

source code 

Generates a general delete sql statement

"table" is a table name or table object

"fixed_conditions" is a list or tuple of tuples with the following format: (column,type,restriction_value)

"type" can be "=",">","<",...

It returns the sql query.

_get_insert_sql_query(self, table, column_values, special_column_values=[], use_buffer=True, max_elements_in_buffer=None, on_duplicate_key='IGNORE')

source code 

Generates a general sql statement

"column_values" must be a tuple of tuples with the following format: (column, value)

It returns the sql query. It does not use the buffer!

By default, if the buffer is active, it will use the buffer, unless the "use_buffer" atributte is set to False

It can return a list of queries instead a single query in the case the buffer is being used

"max_elements_in_buffer" is only used when buffer is used. It is not mandatory

"on_duplicate_key" specifies the query to be used when duplicate keys exists

_get_buffer_multiple_queries(self, key_buffer=None)

source code 

Returns all queries of insert buffer and empties it

If key_buffer is None, it generates all the queries to empty the buffer. Otherwise, it will insert only the key specified

_get_multiple_insert_query(self, table, columns, values)

source code 

"columns" must be a tuple with the name of the columns

"values" must be a Set of tuples of values to insert

_uses_buffer(self)

source code 

Returns False if it is not using buffer

Otherwise return true

_lock_tables(self, table_list=None)

source code 

Method used to Lock tables. Insertions are faster if tables are previously locked.

table_list is a list of table names

_unlock_tables(self)

source code 

Generates the SQL statement that unlocks tables previously locked

Method used to return the SQL query that locks access to mysql tables. Insertions are faster if tables are previously locked.

table_list is a list of table names