Exemple #1
0
def genwherepred(types, valuess, length, db='mysql'):
    predss = []
    for i, (ttype, values) in enumerate(zip(types, valuess)):
        pred = list()
        for value in values:
            # type with concrete value
            for colid, (t, v) in enumerate(zip(ttype, value)):
                if extractValue(v) == '0' or extractValue(v) == '-0':
                    continue
                if db == 'mysql':
                    from sqlgen.dbtype.typeenum import DataType
                    if t is DataType.CHAR or t is DataType.VARCHAR \
                            or t is DataType.BLOB or t is DataType.TEXT:
                        # or t is DataType.TIMESTAMP \
                        # or t is DataType.DATE or t is DataType.DATETIME:
                        continue
                # rand `col op v`
                randv = getRandValueFromType(t, db)
                colandv = getRanExpFromTaV(i, colid, t, randv, db)
                randpreds = getTotalRandExp(length)
                # print(randpreds)
                pred.append(colandv)
                pred.append(randpreds)
        predss.append(pred)
    return predss
Exemple #2
0
def verifyEveryRow(values, types, db='mysql'):
    if db == 'mysql':
        from sqlgen.dbtype.typeenum import DataType
    if db == 'postgres':
        from sqlgen.dbtype.typeenumpsql import DataType
    sqls = []
    # in a table
    for tid, (ttype, value) in enumerate(zip(types, values)):
        # for a column 'select colx from tx where colx = foo;'
        for _, line in enumerate(value):
            for i, col in enumerate(line):
                t = ttype[i]
                if db == 'mysql':
                    if t is DataType.FLOAT or t is DataType.DOUBLE:
                        continue

                selectcol = getSelectWithColAndTable([i], [tid])

                v = extractValue(col)
                v = prettyformat(t, v, db)

                v = getWhereWithColAndValue(i, v)
                if db == 'mysql':
                    if t is DataType.BINARY or t is DataType.VARBINARY:
                        v = 'binary {}'.format(v)

                sql = getAddWhereToSelect(selectcol, v)
                sqls.append(sql)
    return sqls
Exemple #3
0
def getRandExp(length, db='mysql'):
    alleps = list()
    if db == 'postgres':
        from sqlgen.dbtype.typeenumpsql import DataValue, BiOperatorType, BitOperatorType
    if db == 'mysql':
        from sqlgen.dbtype.typeenum import DataValue, BiOperatorType, BitOperatorType

    # for i in list(LogicOperatorType):
    #     alleps.append(i.describe())
    for i in list(BitOperatorType):
        alleps.append(i.describe())
    for i in list(BiOperatorType):
        alleps.append(i.describe())

    if db == 'postgres':
        from sqlgen.dbtype.typeenumpsql import DataValue
    if db == 'mysql':
        from sqlgen.dbtype.typeenum import DataValue

    for i in list(DataValue):
        if i is DataValue.TEXT or i is DataValue.BLOB or i is DataValue.CHAR or i is DataValue.VARCHAR:
            continue
        v = i.randpickvalue()
        alleps.append(str(extractValue(v)))
    random.seed()
    return random.sample(alleps, length)
Exemple #4
0
    def genvalue(self, mytype):
        if self.db == 'postgres':
            from sqlgen.dbtype.typeenumpsql import DataValue
        else:
            from sqlgen.dbtype.typeenum import DataValue
        values = []
        for i in range(0, self.rownum):
            value = list()
            for j, t in enumerate(mytype):  # each column
                while True:
                    flag = 0
                    v = DataValue[t[0].describe()].randpickvalue()
                    vv = extractValue(v)
                    #  fix for constraint violation
                    if t[1] == 'NOT NULL':
                        if vv == 'NULL':
                            continue
                        break
                    if t[1] == 'PRIMARY KEY' or t[1] == 'UNIQUE':
                        if vv == 'NULL':
                            continue
                        for vs in values:  # :check for the constraint
                            vvs = extractValue(vs[j])

                            # 0 = -0
                            try:
                                if int(vv) == int(vvs):
                                    flag = 1
                                    break
                            except TypeError:
                                pass
                            except ValueError:
                                pass
                            # print(vvs, vv)
                            if vv == vvs:
                                flag = 1
                                break

                    if flag == 0:
                        break

                value.append(v)
            values.append(value)
        return values
Exemple #5
0
def getRanExpFromTaV(tid, colid, t, v, db):
    return getTableNameFromId(tid) + '.' + getColNameFromId(colid) + ' ' + \
           genRandBiOp(db) + ' ' + prettyformat(t, str(extractValue(v)))