Created
November 15, 2009 23:12
-
-
Save chromakode/235558 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from drizzle import libdrizzle as _libdrizzle | |
libdrizzle = _libdrizzle.Drizzle() | |
def connect(*args, **kwargs): | |
connection = Connection(*args, **kwargs) | |
connection._connect() | |
return connection | |
class Connection: | |
def __init__(self, host=None, port=None, username=None, password=None, database=None): | |
self.host = host | |
self.port = port | |
self.username = username | |
self.password = password | |
self.database = database | |
self._drizzle_connection = None | |
def _connect(self): | |
self._drizzle_connection = libdrizzle.create_connection() | |
self._drizzle_connection.set_tcp(self.host, self.port) | |
#self._drizzle_connection.set_auth(self.username, self.password) | |
self._drizzle_connection.set_db(self.database) | |
self._drizzle_connection.connect() | |
def cursor(self): | |
return Cursor(self) | |
class Cursor: | |
def __init__(self, connection): | |
self._connection = connection | |
@property | |
def connection(self): | |
return self._connection | |
@property | |
def _drizzle_connection(self): | |
return self.connection._drizzle_connection | |
def execute(self, sql): | |
self._last_result = self._drizzle_connection.query(sql) | |
self._columns = [column for column in iter(self._last_result.read_column, None)] | |
# FIXME: self.description = ... | |
# FIXME: need to cleanup column/libdrizzle buffer stuff if called multiple times | |
def fetchall(self): | |
#rows = [] | |
#for row_buffer in iter(self._last_result.buffer_row, None): | |
# row = [] | |
# print row_buffer | |
# | |
# pos = 0 | |
# for column in self._columns: | |
# # FIXME use named_tuple | |
# row.append(row_buffer[pos:pos+column.size()]) | |
# pos += column.column_size() | |
# r.free_row(row_buffer) | |
# | |
# rows.append(row) | |
# | |
#return | |
return [row for row in iter(self._last_result.row_next, None)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment