def testViews(self):
     """
     Testing functionality related to views.
     """
     conn = self._engine.connect()
     utils.createDb(conn, self._dbA)
     utils.useDb(conn, self._dbA)
     utils.createTable(conn, "t1", "(i int, j int)")
     conn.execute("CREATE VIEW t2 AS SELECT i FROM t1")
     self.assertFalse(utils.isView(conn, "t1"))
     self.assertFalse(utils.isView(conn, "dummyT"))
     self.assertTrue(utils.isView(conn, "t2"))
     utils.dropDb(conn, self._dbA)
예제 #2
0
 def testViews(self):
     """
     Testing functionality related to views.
     """
     conn = self._engine.connect()
     utils.createDb(conn, self._dbA)
     utils.useDb(conn, self._dbA)
     utils.createTable(conn, "t1", "(i int, j int)")
     conn.execute("CREATE VIEW t2 AS SELECT i FROM t1")
     self.assertFalse(utils.isView(conn, "t1"))
     self.assertFalse(utils.isView(conn, "dummyT"))
     self.assertTrue(utils.isView(conn, "t2"))
     utils.dropDb(conn, self._dbA)
 def testViews(self):
     """
     Testing functionality related to views.
     """
     conn = getEngineFromArgs(
         username=self._user, password=self._pass, host=self._host,
         port=self._port, query={"unix_socket": self._sock}).connect()
     utils.createDb(conn, self._dbA)
     utils.useDb(conn, self._dbA)
     utils.createTable(conn, "t1", "(i int, j int)")
     conn.execute("CREATE VIEW t2 AS SELECT i FROM t1")
     self.assertFalse(utils.isView(conn, "t1"))
     self.assertFalse(utils.isView(conn, "dummyT"))
     self.assertTrue(utils.isView(conn, "t2"))
     utils.dropDb(conn, self._dbA)
예제 #4
0
def createChunk(dbName, tblName):
    """
    Create new chunk, following parameters are expected to come in a request
    (in request body with application/x-www-form-urlencoded content like regular form):

    chunkId:        chunk ID, non-negative integer
    overlapFlag:    if true then create overlap table too (default is true),
                    accepted values: '0', '1', 'yes', 'no', 'false', 'true'

    This method is supposed to handle both regular tables and views.
    """

    _log.debug('request: %s', request)
    _log.debug('request.form: %s', request.form)
    _log.debug('POST => create chunk')

    # validate params
    _validateDbName(dbName)
    _validateTableName(tblName)

    # parse chunk number
    chunkId = request.form.get('chunkId', None)
    if chunkId is None:
        raise ExceptionResponse(400, "MissingArgument",
                                "Chunk ID argument (chunkId) is missing")
    try:
        chunkId = int(chunkId)
        if chunkId < 0:
            raise ExceptionResponse(400, "InvalidArgument",
                                    "Chunk ID argument (chunkId) is negative")
    except ValueError:
        raise ExceptionResponse(
            400, "InvalidArgument",
            "Chunk ID argument (chunkId) is not an integer")

    overlapFlag = _getArgFlag(request.form, 'overlapFlag', True)

    # check that table exists
    dbConn = Config.instance().dbEngine().connect()
    if not utils.tableExists(dbConn, tblName, dbName):
        raise ExceptionResponse(
            404, "TableMissing",
            "Table %s.%s does not exist" % (dbName, tblName))

    chunkRepr = _chunkDict(dbName, tblName, chunkId)

    # make chunk table names
    # TODO: we need some central location for things like this
    tables = {'chunkTable': tblName + '_' + str(chunkId)}
    if overlapFlag:
        tables['overlapTable'] = tblName + 'FullOverlap_' + str(chunkId)
    _log.debug('will create tables: %s', tables)
    for tblType, chunkTable in tables.items():

        # check if this table is actually a view
        if utils.isView(dbConn, tblName, dbName):

            # view needs more complicated algorithm to copy its definition, first copy
            # its current definition, then rename existing view and then re-create it again

            _log.debug('table %s is a view', tblName)

            # get its current definition
            query = "SHOW CREATE VIEW `{0}`.`{1}`".format(dbName, tblName)
            rows = dbConn.execute(query)
            viewDef = rows.first()[1]

            # rename it
            query = "RENAME TABLE `{0}`.`{1}` to `{0}`.`{2}`".format(
                dbName, tblName, chunkTable)
            dbConn.execute(query)

            # re-create it
            dbConn.execute(viewDef)

        else:

            # make table using DDL from non-chunked one
            _log.debug('make chunk table: %s', chunkTable)
            try:
                utils.createTableLike(dbConn, dbName, chunkTable, dbName,
                                      tblName)
            except utils.TableExistsError as exc:
                _log.error('Db exception when creating table: %s', exc)
                raise ExceptionResponse(
                    409, "TableExists",
                    "Table %s.%s already exists" % (dbName, chunkTable))

            if tblType == 'overlapTable':
                _fixOverlapIndices(dbConn, dbName, chunkTable)

        chunkRepr[tblType] = True

    response = json.jsonify(result=chunkRepr)
    response.status_code = 201
    return response
예제 #5
0
파일: dbMgr.py 프로젝트: achbal/qserv
def createChunk(dbName, tblName):
    """
    Create new chunk, following parameters are expected to come in a request
    (in request body with application/x-www-form-urlencoded content like regular form):

    chunkId:        chunk ID, non-negative integer
    overlapFlag:    if true then create overlap table too (default is true),
                    accepted values: '0', '1', 'yes', 'no', 'false', 'true'

    This method is supposed to handle both regular tables and views.
    """

    _log.debug('request: %s', request)
    _log.debug('request.form: %s', request.form)
    _log.debug('POST => create chunk')

    # validate params
    _validateDbName(dbName)
    _validateTableName(tblName)

    # parse chunk number
    chunkId = request.form.get('chunkId', None)
    if chunkId is None:
        raise ExceptionResponse(400, "MissingArgument", "Chunk ID argument (chunkId) is missing")
    try:
        chunkId = int(chunkId)
        if chunkId < 0:
            raise ExceptionResponse(400, "InvalidArgument", "Chunk ID argument (chunkId) is negative")
    except ValueError:
        raise ExceptionResponse(400, "InvalidArgument", "Chunk ID argument (chunkId) is not an integer")

    overlapFlag = _getArgFlag(request.form, 'overlapFlag', True)

    # check that table exists
    dbConn = Config.instance().dbEngine().connect()
    if not utils.tableExists(dbConn, tblName, dbName):
        raise ExceptionResponse(404, "TableMissing", "Table %s.%s does not exist" % (dbName, tblName))

    chunkRepr = _chunkDict(dbName, tblName, chunkId)

    # make chunk table names
    # TODO: we need some central location for things like this
    tables = {'chunkTable': tblName + '_' + str(chunkId)}
    if overlapFlag:
        tables['overlapTable'] = tblName + 'FullOverlap_' + str(chunkId)
    _log.debug('will create tables: %s', tables)
    for tblType, chunkTable in tables.items():

        # check if this table is actually a view
        if utils.isView(dbConn, tblName, dbName):

            # view needs more complicated algorithm to copy its definition, first copy
            # its current definition, then rename existing view and then re-create it again

            _log.debug('table %s is a view', tblName)

            # get its current definition
            query = "SHOW CREATE VIEW `{0}`.`{1}`".format(dbName, tblName)
            rows = dbConn.execute(query)
            viewDef = rows.first()[1]

            # rename it
            query = "RENAME TABLE `{0}`.`{1}` to `{0}`.`{2}`".format(dbName, tblName, chunkTable)
            dbConn.execute(query)

            # re-create it
            dbConn.execute(viewDef)

        else:

            # make table using DDL from non-chunked one
            _log.debug('make chunk table: %s', chunkTable)
            try:
                utils.createTableLike(dbConn, dbName, chunkTable, dbName, tblName)
            except utils.TableExistError as exc:
                _log.error('Db exception when creating table: %s', exc)
                raise ExceptionResponse(409, "TableExists",
                                        "Table %s.%s already exists" % (dbName, chunkTable))

            if tblType == 'overlapTable':
                _fixOverlapIndices(dbConn, dbName, chunkTable)

        chunkRepr[tblType] = True

    response = json.jsonify(result=chunkRepr)
    response.status_code = 201
    return response