Guest User

Untitled

a guest
Apr 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. from drizzle import libdrizzle as _libdrizzle
  2.  
  3. libdrizzle = _libdrizzle.Drizzle()
  4.  
  5. def connect(*args, **kwargs):
  6. connection = Connection(*args, **kwargs)
  7. connection._connect()
  8. return connection
  9.  
  10. class Connection:
  11. def __init__(self, host=None, port=None, username=None, password=None, database=None):
  12. self.host = host
  13. self.port = port
  14. self.username = username
  15. self.password = password
  16. self.database = database
  17. self._drizzle_connection = None
  18.  
  19. def _connect(self):
  20. self._drizzle_connection = libdrizzle.create_connection()
  21. self._drizzle_connection.set_tcp(self.host, self.port)
  22. #self._drizzle_connection.set_auth(self.username, self.password)
  23. self._drizzle_connection.set_db(self.database)
  24. self._drizzle_connection.connect()
  25.  
  26. def cursor(self):
  27. return Cursor(self)
  28.  
  29. class Cursor:
  30. def __init__(self, connection):
  31. self._connection = connection
  32.  
  33. @property
  34. def connection(self):
  35. return self._connection
  36.  
  37. @property
  38. def _drizzle_connection(self):
  39. return self.connection._drizzle_connection
  40.  
  41.  
  42. def execute(self, sql):
  43. self._last_result = self._drizzle_connection.query(sql)
  44. self._columns = [column for column in iter(self._last_result.read_column, None)]
  45. # FIXME: self.description = ...
  46. # FIXME: need to cleanup column/libdrizzle buffer stuff if called multiple times
  47.  
  48. def fetchall(self):
  49. #rows = []
  50. #for row_buffer in iter(self._last_result.buffer_row, None):
  51. # row = []
  52. # print row_buffer
  53. #
  54. # pos = 0
  55. # for column in self._columns:
  56. # # FIXME use named_tuple
  57. # row.append(row_buffer[pos:pos+column.size()])
  58. # pos += column.column_size()
  59. # r.free_row(row_buffer)
  60. #
  61. # rows.append(row)
  62. #
  63. #return
  64. return [row for row in iter(self._last_result.row_next, None)]
Add Comment
Please, Sign In to add comment