import CSQLRelay class sqlrclient: """ A wrapper for the sqlrelay client API. Closely follows the C++ API, with the file functions missing. """ str_type = type("") def __init__(self, connection_params, user, password, retrytime=1, tries=1): """ Opens a connections to the sqlrelay server and authenticates with user and password. Failed connections are retried for tries times at retrytime interval. connection_parames can either be a string for a local unix socket socket or a tuple of host and port number for tcp sockets.""" if type(connection_params) == self.str_type: self.connection = CSQLRelay.connect("", 0, connection_params, user, password, retrytime, tries) else: self.connection = CSQLRelay.connect(connection_params[0], connection_params[1], "", user, password, retrytime, tries) def sendQuery(self, query): """ Send a SQL query to the server and gets a return set. """ return CSQLRelay.sendQuery(self.connection, query) def sendCachedQuery(self, ttl, query): """ Send a cached SQL query to the server and gets a return set. """ return CSQLRelay.sendCachedQuery(self.connection, ttl, query) def openCachedResultSet(self, filename): """ Open a result set after a sendCachedQeury """ return CSQLRelay.openCachedResultSet(self.connection, filename) def sendFinalQuery(self, query): """ Sends a query, gets a return set, and then ends the session. """ return CSQLRelay.sendFinalQuery(self.connection, query) def rowCount(self): """ Returns the number of rows in the current return set. """ return CSQLRelay.rowCount(self.connection) def colCount(self): """ Returns the number of columns in the current return set. """ return CSQLRelay.colCount(self.connection) def errorMessage(self): """ If the query failed, the error message will be returned from this method. Otherwise, it returns None. """ return CSQLRelay.errorMessage(self.connection) def sendEndOfSession(self): """ Depricated. use endOfSession. """ return self.endSession() def endSession(self): """ Ends the current session. """ return CSQLRelay.endSession(self.connection) def getField(self, row, col): """ Returns the value of the specified row and column. col may be a column name or number. """ if type(col) == self.str_type: return CSQLRelay.getFieldByName(self.connection, row, col) else: return CSQLRelay.getFieldByIndex(self.connection, row, col) def getRow(self, row): """ Returns a list of values in the given row. """ return CSQLRelay.getRow(self.connection, row) def getRowRange(self, beg, end): """ Returns a list of lists of the rows between beg and end. Note: this function is not in the standard sqlrelay API. """ max_rows = self.rowCount() if end >= max_rows: end = max_rows - 1 return CSQLRelay.getRowRange(self.connection, beg, end) def getColumnName(self, col): """ Returns the name of column number col. """ return CSQLRelay.getColumnName(self.connection, col) def getColumnNames(self): """ Returns a list of column names in the current return set. """ return CSQLRelay.getColumnNames(self.connection) def getColumnType(self, col): """ Returns the type of the specified column. col may be a name or number. """ if type(col) == self.str_type: return CSQLRelay.getColumnTypeByName(self.connection, col) else: return CSQLRelay.getColumnTypeByIndex(self.connection, col) def getColumnLength(self, col): """ Returns the length of the specified column. col may be a name or number. """ if type(col) == self.str_type: return CSQLRelay.getColumnLengthByName(self.connection, col) else: return CSQLRelay.getColumnLengthByIndex(self.connection, col) if __name__ == "__main__": a = sqlrclient(('dev.pointone.com', 8001), 'secure', 'gripsucks') foo = a.sendQuery('select * from petetest') print a.errorMessage() print a.getRowRange(0, 999) print a.errorMessage()