예제 #1
0
def handleGet(request, response, table, _id=None):
    obj = getclass(table)
    if _id is None:
        log.debug('Get all rows in table %s' % obj._table)
        # all rows (put some sane limit here maybe?)
        dbquery = db.query(obj)
    else:
        # one row, identified by rowID
        dbquery = db.query().filter(obj.q._id, '=', _id)
        log.debug("Get one row in table '%s' matching query %s" % (obj._table, dbquery.toSql()))
    
    resp = bc.Response()
    try:
        resp.data = []
        for row in db.driver.select(dbquery):  # we call driver directly for efficiency reason
            tmp = {}
            for colname in obj._iterName():
                tmp[colname] = row[colname]
            resp.data.append(tmp)
    except db.Error as e:
        msg = "Could not load objects from table '%s'. %s" % (obj._table, e)
        log.debug(msg)
        response.setError(1, msg)
        response.status_code = '404 ' + msg
        return
        
    if _id is not None and len(resp.data) == 0:
        msg = "Unknown ID %s in table '%s'" % (_id, obj._table)
        log.debug(msg)
        resp.setError(1, msg)
        resp.status_code = '404 ' + msg
        return
    writejson(resp)
예제 #2
0
def handleHead(request, response, table, _id=None):
    """
    Count the number of rows matching a query
    Return data in a HTML header
    """
    obj = getclass(table)
    if _id is None:
        log.debug('Count all rows in table %s' % obj._table)
        # all rows (put some sane limit here maybe?)
        dbquery = db.query(obj)
    elif _id is 'filter':
        # filter out specific rows
        dbquery = db.query()
        dbquery.decode(request.querystr)
        log.debug("Count all rows in table '%s' matching query %s" % (obj._table, dbquery.toSql()))

    resp = bc.Response()
    try:
        resp.data = db.driver.count(dbquery)  # we call driver direct for efficiency reason
    except db.Error as e:
        msg = "Could not count objects in table '%s'. %s" % (obj._table, e)
        log.debug(msg)
        # self.status_code = '404 ' + msg
        return
    response.addHeader('X-Result-Count', str(resp.data))
예제 #3
0
def database(request, response, dbname=None):
    resp = bc.Response()
    try:
        resp.data = db.driver.isDatabase(dbname)
    except db.Error as e:
        resp.errno = e.errno
        resp.errmsg = e.errmsg
    writejson(resp)
예제 #4
0
def _table(request, response, table):
    resp = bc.Response()
    try:
        resp.data = db.driver.isTable(table)
    except db.Error as e:
        resp.errno = e.errno
        resp.errmsg = e.errmsg
    writejson(resp)
예제 #5
0
def handlePost(request, response, table):
    obj = getclass(table)
    log.debug("Insert one row in table '%s'" % (obj._table))
    postdata = getData(obj)
    resp = bc.Response()
    try:
        resp.data = db.driver.insert(obj._table, postdata) # we call driver direct for efficiency reason
    except db.Error as e:
        resp.errno = e.errno
        resp.errmsg = e.errmsg
    writejson(resp)
예제 #6
0
def handlePut(request, response, table, _id):
    obj = getclass(table)
    log.debug("Update one row in table '%s'" % (obj._table))
    putdata = getData(obj)
    putdata['_id'] = _id
    resp = bc.Response()
    try:
        resp.data = db.driver.update(obj._table, putdata) # we call driver direct for efficiency reason
    except db.Error as e:
        resp.errno = e.errno
        resp.errmsg = e.errmsg
    writejson(resp)
예제 #7
0
def handleDelete(request, response, table, _id):
    obj = getclass(table)
    if _id == 'filter':
        # filter out specific rows
        dbquery = db.query(obj)
        dbquery.decode(request.querystr)
        log.debug("Delete all rows in table '%s' matching query %s" % (obj._table, dbquery.toSql()))
    else:
        # one row, identified by _id
        dbquery = db.query().filter(obj.q._id, '=', _id)
        log.debug("Delete one row in table '%s' matching id %s" % (obj._table, _id))
    resp = bc.Response()
    try:
        resp.data = db.driver.delete(dbquery)
    except db.Error as e:
        resp.errno = e.errno
        resp.errmsg = e.errmsg
    writejson(resp)
예제 #8
0
def handleGetFilter(request, response, table):
    obj = getclass(table)
    dbquery = db.query(obj)
    dbquery.decode(request.query_string)
    log.debug("Get all rows in table '%s' matching query %s" % (obj._table, dbquery.toSql()))
    
    resp = bc.Response()
    try:
        resp.data = []
        for row in db.driver.select(dbquery):  # we call driver directly for efficiency reason
            tmp = {}
            for colname in obj._iterName():
                tmp[colname] = row[colname]
            resp.data.append(tmp)
    except db.Error as e:
        msg = "Could not load objects from table '%s'. %s" % (obj._table, e)
        log.debug(msg)
        response.setError(1, msg)
        response.status_code = '404 ' + msg
        return
        
    writejson(resp)