Package picalo :: Module Database :: Class _Connection
[show private | hide private]
[frames | no frames]

Class _Connection

AbstractDecorator --+
                    |
                   _Connection

Known Subclasses:
_MySQLdbConnection, _OdbcConnection, _Psycopg2Connection

The base class of connections
Method Summary
  __init__(self, dbconnection, connect_func, connect_args)
  __getattr__(self, key)
Returns the given attribute of the class.
  __repr__(self)
Returns a string representation of this connection
  __str__(self)
Returns a string representation of this connection
  commit(self)
Commits any changes to the database.
  copy_table(self, source_connection, source_tablename, dest_tablename, replace)
Copies a table from a database into this connection.
_Cursor cursor(self)
Returns a cursor to the database.
object execute(self, sql, parameters, close_cursor)
Runs executable queries (INSERT, UPDATE, DELETE) that don't return tables.
InsertQueryBuilder insert_query_builder(self, tablename)
This method returns a helper class for creating INSERT queries.
  is_changed(self)
Returns whether the connection needs saving
  list_tables(self, refresh)
Returns the table names in this database as a list
  post_table(self, table, name, replace, add_if_empty)
Creates a new table in the database and posts the records in the given table.
Cursor query(self, sql, parameters)
Runs a SELECT query that returns a result set.
Record query1(self, sql, parameters)
Runs a SELECT query and returns only the first record from the result set.
  refresh(self, tablename)
Refreshes the given table name, if it has already been pulled from the database.
  save(self, filename)
Saves the database connection
SelectQueryBuilder select_query_builder(self, tablename, select_fields)
This method returns a helper class for creating simple SELECT queries.
Table table(self, sql, parameters)
This method is the primary access to databases within Picalo.
UpdateQueryBuilder update_query_builder(self, tablename)
This method returns a helper class for creating simple UPDATE queries.
  _create_database_table(self, table, name, replace)
Creates a databaes relation matching the column names and types of the given table.
  _get_columns(self, tablename)
Returns a list of the columns in the given table
  _set_table_types(self, cursor, table)
Helper method for _Connection.table().
    Inherited from AbstractDecorator
  __cmp__(self, other)
  __delattr__(self, name)
  __eq__(self, other)
  __ge__(self, other)
  __gt__(self, other)
  __hash__(self)
  __le__(self, other)
  __lt__(self, other)
  __ne__(self, other)
  __nonzero__(self)
  __setattr__(self, name, value)

Class Variable Summary
str QUERY_PARAMETER = '%s'

Method Details

__getattr__(self, key)
(Qualification operator)

Returns the given attribute of the class. In addition, you can type connection.table to access any table in the database. The connection will automatically "SELECT * FROM table" and pull the records in an efficient way as you need them.
Overrides:
picalo.base.Global.AbstractDecorator.__getattr__

__repr__(self)
(Representation operator)

Returns a string representation of this connection
Overrides:
picalo.base.Global.AbstractDecorator.__repr__

__str__(self)
(Informal representation operator)

Returns a string representation of this connection
Overrides:
picalo.base.Global.AbstractDecorator.__str__

commit(self)

Commits any changes to the database. If you make modifications of any kind to your database tables (INSERT, UPDATE, CREATE, etc.), you *must* commit those changes by calling commit().

If you close a database connection without committing, you'll lose all changes.

Analyses that only run SELECT calls on a database do not need to commit().

See the execute() method for an example of committing.

copy_table(self, source_connection, source_tablename, dest_tablename, replace=False)

Copies a table from a database into this connection. This method pulls records one by one from the source connection and posts them to this connection. It automatically creates the new destination table, optionally replacing any existing tables with the name.

This is a convenience method provided to make importing of data from other databases easy. This method is different from post_table because it is more memory-efficient. The post_table method requires that you have an existing Picalo Table, which means all of the data needs to be in memory at once. This method copies record by record, meaning it only needs to hold one record at a time. This method works only with database connections, not with CSV or other types of sources.

The method always tries to create a new table in the database -- it will not append records into an existing table. If the table cannot be created (if it exists, for example), an error is thrown. If the "replace" option is True, any existing tables by this name are dropped before the new table is created.

The method automatically commits the data to the database. You cannot use rollback after this method has finished. This is required by some databases after creation of a table, so it has to be done.

cursor(self)

Returns a cursor to the database. Since Picalo manages cursors automatically, most users don't need to access them directly. The method is provided for advanced users who want to explicitly manage cursors.
Returns:
A cursor to the database.
           (type=_Cursor)

execute(self, sql, parameters=None, close_cursor=True)

Runs executable queries (INSERT, UPDATE, DELETE) that don't return tables. This is the primary methods to use when posting information to a database.

IMPORTANT: Since most databases don't commit changes automatically, you *must* call commit() to make the changes permanent. If you close the connection without committing, you'll lose all of your changes.

Alternatively, you can call rollback() to undo all changes since the last commit() call.

Example:
>>> myconn.execute("CREATE TABLE mytable (id integer, name varchar(20))")
>>> myconn.execute("INSERT INTO mytable (id, name) VALUES (1, 'Dennis')")
>>> myconn.commit()
Parameters:
sql - The SQL string to execute.
           (type=string)
parameters - The parameters to be sent to the database.
           (type=List or Tuple)
close_cursor - Determines whether the cursor is closed automatically. Unless you need the cursor for something else, you should allow it to close automatically.
           (type=bool)
Returns:
The return value from the database. This varies from database to database and is probably not very useful. If errors occur in the operation, exceptions are thrown.
           (type=object)

insert_query_builder(self, tablename)

This method returns a helper class for creating INSERT queries. It allows you to create INSERT queries piece by piece and is available for advanced users. It is useful when creating an SQL query programatically in a script.

Example: >>> db = Database.PostgreSQLConnection('mydb') >>> q = db.insert_query_builder('test') >>> q.add('id', 5) >>> q.add('name', 'Sally') >>> print q QueryBuilder: <cursor>.execute("INSERT INTO test (id, name) VALUES (%s, %s)", [5, 'Sally']) >>> q.execute() >>> db.commit()
Parameters:
tablename - The database table name to insert the records into
           (type=str)
Returns:
An InsertQueryBuilder object.
           (type=InsertQueryBuilder)

is_changed(self)

Returns whether the connection needs saving

list_tables(self, refresh=True)

Returns the table names in this database as a list

post_table(self, table, name, replace=False, add_if_empty=False)

Creates a new table in the database and posts the records in the given table. If a table with the given name already exists in the table, the method throws an error unless the "replace" option is True.

This is a convenience method provided to make importing of data from text files (CSV, TSV, etc.) and other sources into databases easy. The example shows how a CSV file can be posted to database in just two lines of code.

The method always tries to create a new table in the database -- it will not append records into an existing table. If the table cannot be created (if it exists, for example), an error is thrown. If the "replace" option is True, any existing tables by this name are dropped before the new table is created.

The method automatically commits the data to the database. You cannot use rollback after this method has finished. This is required by some databases after creation of a table, so it has to be done.

Example:
>>> myconn = Database.PostgreSQLConnection('mydb')
>>> data = Table([('id', int), ('name', unicode)],[
...   [ 1, 'Benny' ],
...   [ 2, 'Vijay' ],
... ])
>>> myconn.post_table(data, 'mytable', True)
>>> myconn.table('SELECT * FROM mytable').view()
+----+-------+
| id |  name |
+----+-------+
|  1 | Benny |
|  2 | Vijay |
+----+-------+
Parameters:
table - The picalo table to post
           (type=Table)
name - The new database table name
           (type=str)
replace - Whether to replace any existing database tables with the given name. If the database table exists and replace is False (the default), the method will throw an error.
           (type=bool)
add_if_empty - Whether to add fields that are empty (None or '') or to simply leave them out of the query.
           (type=bool)

query(self, sql, parameters=None)

Runs a SELECT query that returns a result set. This method provides efficient access to extremely large data sets, but it is not as easy to use as table(). Most users should use table() unless you explicitly need to access only one record at a time. In other words, check out Database.Connection.table(sql) and use it unless you really need the efficiency offered by this method.

Stated differently, query() and table() accomplish the same task: query a database. The query() method is more efficient and the table() method is more powerful and easier to use.

If you need to efficiently use memory and must use this method, you should use an iterator in a for loop as is done in the example. This allows you to access one record at a time. Note that you can only go in forward direction one at a time. If you need to go backwards in your analysis, either store the records in variables or use table().

Example:
>>> import psycopg
>>> myconn = Database.Connection(psycopg.connect(database='mydb'))
>>> for record in myconn.query("SELECT * FROM mytable"):
...   print record['id']
...   print record['name']
...

1
Dennis
Parameters:
sql - The SQL string to execute.
           (type=string)
parameters - The parameters to be sent to the database.
           (type=List or Tuple)
Returns:
A cursor to the result set.
           (type=Cursor)

query1(self, sql, parameters=None)

Runs a SELECT query and returns only the first record from the result set. There are many times when you expect only a single record in the result set, such a key value query. In these circumstances, query1() provides easy access directly to the first record.

The method returns a single Record instance or None if no results are found.

Example:
>>> import psycopg
>>> myconn = Database.Connection(psycopg.connect(database='mydb'))
>>> rec = myconn.query1("SELECT min(id) FROM mytable")
>>> print rec
1
Parameters:
sql - The SQL string to execute.
           (type=string)
parameters - The parameters to be sent to the database.
           (type=List or Tuple)
Returns:
A single result set record.
           (type=Record)

refresh(self, tablename=None)

Refreshes the given table name, if it has already been pulled from the database. This has relation to the db.tablename syntax. If the table has been updated on the database, we won't pull the new records until this method is called. When tablename is None, all tables are refreshed.
Parameters:
tablename - The relation name to refresh, None to refresh all tables in this database.
           (type=str)

save(self, filename)

Saves the database connection

select_query_builder(self, tablename, select_fields=['*'])

This method returns a helper class for creating simple SELECT queries. It allows you to create SELECT queries piece by piece and is available for advanced users. It is useful when creating an SQL query programatically in a script.

Example: >>> db = Database.PostgreSQLConnection('mydb') >>> q = db.select_query_builder('test', ['id', 'name']) >>> q.add_where('id', 1) >>> print q QueryBuilder: <cursor>.execute("SELECT id, name FROM test WHERE id=%s", [1]) >>> results = q.table() >>> results.prettyprint() +----+--------+ | id | name | +----+--------+ | 1 | Joseph | +----+--------+
Parameters:
tablename - The database table name to update the records in.
           (type=str)
select_fields - The fields to be selected by the query.
           (type=list or tuple of strings)
Returns:
An SelectQueryBuilder object.
           (type=SelectQueryBuilder)

table(self, sql, parameters=None)

This method is the primary access to databases within Picalo. Most users will create a connection and then call only this method to retrieve data from tables.

Retrieves a Table to the results of a new query or the most recently-run query. Table objects have additional methods not found in this module, such as pretty printing, saving to text files, and mutability.

If your database driver is one of the enhanced support drivers (such as PythonWin's odbc, psycopg, and MySQLdb drivers), the method will automatically set column types.

The primary reason you would not want to use this method is that it loads all records into memory at query time, so it might use up your memory with super large tables. Example:
>>> import psycopg
>>> myconn = Database.Connection(psycopg.connect(database='conan'))
>>> data = myconn.table("SELECT * FROM mytable")
>>> data.view()
+----+--------+
| id |  name  |
+----+--------+
|  1 | Dennis |
+----+--------+
Parameters:
sql - The SQL string to execute.
           (type=string)
parameters - The parameters to be sent to the database.
           (type=List or Tuple)
Returns:
A picalo table containing the results.
           (type=Table)

update_query_builder(self, tablename)

This method returns a helper class for creating simple UPDATE queries. It allows you to create UPDATE queries piece by piece and is available for advanced users. It is useful when creating an SQL query programatically in a script.

Example: >>> db = Database.PostgreSQLConnection('mydb') >>> q = db.update_query_builder('test') >>> q.add_where('id', 3) >>> q.add('name', 'newname') >>> print q QueryBuilder: <cursor>.execute("UPDATE test SET name=%s WHERE id=%s", ['newname', 3]) >>> q.execute() >>> db.commit()
Parameters:
tablename - The database table name to update the records in.
           (type=str)
Returns:
An UpdateQueryBuilder object.
           (type=UpdateQueryBuilder)

_create_database_table(self, table, name, replace=False)

Creates a databaes relation matching the column names and types of the given table.

_get_columns(self, tablename)

Returns a list of the columns in the given table

_set_table_types(self, cursor, table)

Helper method for _Connection.table(). After the query is run, this method uses the cursor to set the column types appropriately.

Class Variable Details

QUERY_PARAMETER

Type:
str
Value:
'%s'                                                                   

Generated by Epydoc 2.1 on Mon Aug 20 05:38:17 2007 http://epydoc.sf.net