sql.py 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import os
import time
import datetime
S
Shuduo Sang 已提交
18
import inspect
19 20 21 22 23 24 25 26 27
from util.log import *


class TDSql:
    def __init__(self):
        self.queryRows = 0
        self.queryCols = 0
        self.affectedRows = 0

S
Shuduo Sang 已提交
28
    def init(self, cursor, log=True):
29 30
        self.cursor = cursor

S
Shuduo Sang 已提交
31
        if (log):
32 33
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            self.cursor.log(caller.filename + ".sql")
S
Shuduo Sang 已提交
34

35 36 37 38 39
    def close(self):
        self.cursor.close()

    def prepare(self):
        tdLog.info("prepare database:db")
L
liu0x54 已提交
40 41 42 43 44 45 46 47
        s = 'reset query cache'
        self.cursor.execute(s)
        s = 'drop database if exists db'
        self.cursor.execute(s)
        s = 'create database db'
        self.cursor.execute(s)
        s = 'use db'
        self.cursor.execute(s)
48 49 50 51 52 53 54 55

    def error(self, sql):
        expectErrNotOccured = True
        try:
            self.cursor.execute(sql)
        except BaseException:
            expectErrNotOccured = False
        if expectErrNotOccured:
56 57
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            tdLog.exit("%s(%d) failed: sql:%s, expect error not occured" % (caller.filename, caller.lineno, sql))
58
        else:
59 60 61
            self.queryRows = 0
            self.queryCols = 0
            self.queryResult = None
S
Shuduo Sang 已提交
62
            tdLog.info("sql:%s, expect error occured" % (sql))
63 64 65

    def query(self, sql):
        self.sql = sql
66 67 68 69 70 71 72 73 74
        try:
            self.cursor.execute(sql)
            self.queryResult = self.cursor.fetchall()
            self.queryRows = len(self.queryResult)
            self.queryCols = len(self.cursor.description)
        except Exception as e:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, repr(e))
            tdLog.exit("%s(%d) failed: sql:%s, %s" % args)
75 76
        return self.queryRows

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    def waitedQuery(self, sql, expectRows, timeout):
        tdLog.info("sql: %s, try to retrieve %d rows in %d seconds" % (sql, expectRows, timeout))
        self.sql = sql
        try:
            for i in range(timeout):
                self.cursor.execute(sql)
                self.queryResult = self.cursor.fetchall()
                self.queryRows = len(self.queryResult)
                self.queryCols = len(self.cursor.description)
                if self.queryRows >= expectRows:
                    return (self.queryRows, i)
                time.sleep(1)
        except Exception as e:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, repr(e))
            tdLog.exit("%s(%d) failed: sql:%s, %s" % args)
        return (self.queryRows, timeout)
94

95 96 97 98 99 100 101
    def checkRows(self, expectRows):
        if self.queryRows == expectRows:
            tdLog.info("sql:%s, queryRows:%d == expect:%d" % (self.sql, self.queryRows, expectRows))
        else:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, self.sql, self.queryRows, expectRows)
            tdLog.exit("%s(%d) failed: sql:%s, queryRows:%d != expect:%d" % args)
102

103 104
    def checkRowCol(self, row, col):
        caller = inspect.getframeinfo(inspect.stack()[2][0])
105
        if row < 0:
106 107
            args = (caller.filename, caller.lineno, self.sql, row)
            tdLog.exit("%s(%d) failed: sql:%s, row:%d is smaller than zero" % args)
108
        if col < 0:
109 110
            args = (caller.filename, caller.lineno, self.sql, row)
            tdLog.exit("%s(%d) failed: sql:%s, col:%d is smaller than zero" % args)
111
        if row > self.queryRows:
112 113
            args = (caller.filename, caller.lineno, self.sql, row, self.queryRows)
            tdLog.exit("%s(%d) failed: sql:%s, row:%d is larger than queryRows:%d" % args)
114
        if col > self.queryCols:
115 116
            args = (caller.filename, caller.lineno, self.sql, col, self.queryCols)
            tdLog.exit("%s(%d) failed: sql:%s, col:%d is larger than queryCols:%d" % args)
117

118 119
    def checkDataType(self, row, col, dataType):
        self.checkRowCol(row, col)
120 121
        return self.cursor.istype(col, dataType)

122
    def checkData(self, row, col, data):
123
        self.checkRowCol(row, col)
124
        if self.queryResult[row][col] != data:
125 126 127
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data)
            tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args)
sangshuduo's avatar
sangshuduo 已提交
128 129

        if data is None:
S
Shuduo Sang 已提交
130
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
131
                       (self.sql, row, col, self.queryResult[row][col], data))
132
        elif isinstance(data, str):
S
Shuduo Sang 已提交
133
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
134
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
135
        elif isinstance(data, datetime.date):
S
Shuduo Sang 已提交
136
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
sangshuduo's avatar
sangshuduo 已提交
137
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
138
        else:
S
Shuduo Sang 已提交
139
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%d" %
140
                       (self.sql, row, col, self.queryResult[row][col], data))
141 142

    def getData(self, row, col):
143
        self.checkRowCol(row, col)
144 145 146 147 148 149 150 151 152 153 154 155
        return self.queryResult[row][col]

    def executeTimes(self, sql, times):
        for i in range(times):
            try:
                return self.cursor.execute(sql)
            except BaseException:
                time.sleep(1)
                continue

    def execute(self, sql):
        self.sql = sql
156 157 158 159 160 161
        try:
            self.affectedRows = self.cursor.execute(sql)
        except Exception as e:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, repr(e))
            tdLog.exit("%s(%d) failed: sql:%s, %s" % args)
162 163 164 165
        return self.affectedRows

    def checkAffectedRows(self, expectAffectedRows):
        if self.affectedRows != expectAffectedRows:
166 167 168 169 170
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, self.sql, self.affectedRows, expectAffectedRows)
            tdLog.exit("%s(%d) failed: sql:%s, affectedRows:%d != expect:%d" % args)

        tdLog.info("sql:%s, affectedRows:%d == expect:%d" % (self.sql, self.affectedRows, expectAffectedRows))
S
Shuduo Sang 已提交
171

172 173

tdSql = TDSql()