sql.py 7.4 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 32 33 34 35 36
        if (log):
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
            self.cursor.log(callerFilename + ".sql")

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    def close(self):
        self.cursor.close()

    def prepare(self):
        tdLog.info("prepare database:db")
        self.cursor.execute('reset query cache')
        self.cursor.execute('drop database if exists db')
        self.cursor.execute('create database db')
        self.cursor.execute('use db')

    def error(self, sql):
        expectErrNotOccured = True
        try:
            self.cursor.execute(sql)
        except BaseException:
            expectErrNotOccured = False
        if expectErrNotOccured:
S
Shuduo Sang 已提交
54 55 56 57
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
            tdLog.exit(
S
Shuduo Sang 已提交
58
                "%s failed: sql:%s, expect error not occured" %
S
Shuduo Sang 已提交
59
                (callerFilename, sql))
60
        else:
S
Shuduo Sang 已提交
61
            tdLog.info("sql:%s, expect error occured" % (sql))
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    def query(self, sql):
        self.sql = sql
        self.cursor.execute(sql)
        self.queryResult = self.cursor.fetchall()
        self.queryRows = len(self.queryResult)
        self.queryCols = len(self.cursor.description)
        # if self.queryRows == 1 and self.queryCols == 1:
        #	tdLog.info("sql:%s, rows:%d cols:%d data:%s" % (self.sql, self.queryRows, self.queryCols, self.queryResult[0][0]))
        # else:
        #	tdLog.info("sql:%s, rows:%d cols:%d" % (self.sql, self.queryRows, self.queryCols))
        return self.queryRows

    def checkRows(self, expectRows):
        if self.queryRows != expectRows:
S
Shuduo Sang 已提交
77 78 79
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
80
            tdLog.exit(
S
Shuduo Sang 已提交
81
                "%s failed: sql:%s, queryRows:%d != expect:%d" %
S
Shuduo Sang 已提交
82
                (callerFilename, self.sql, self.queryRows, expectRows))
S
Shuduo Sang 已提交
83
        tdLog.info("sql:%s, queryRows:%d == expect:%d" %
84 85
                   (self.sql, self.queryRows, expectRows))

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    def checkDataType(self, row, col, dataType):
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

        if row < 0:
            tdLog.exit(
                "%s failed: sql:%s, row:%d is smaller than zero" %
                (callerFilename, self.sql, row))
        if col < 0:
            tdLog.exit(
                "%s failed: sql:%s, col:%d is smaller than zero" %
                (callerFilename, self.sql, col))
        if row > self.queryRows:
            tdLog.exit(
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
                (callerFilename, self.sql, row, self.queryRows))
        if col > self.queryCols:
            tdLog.exit(
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
                (callerFilename, self.sql, col, self.queryCols))

        return self.cursor.istype(col, dataType)

110
    def checkData(self, row, col, data):
S
Shuduo Sang 已提交
111 112 113 114
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

115 116
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
117
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
118
                (callerFilename, self.sql, row))
119 120
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
121
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
122
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
123
        if row > self.queryRows:
124
            tdLog.exit(
S
Shuduo Sang 已提交
125
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
126
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
127
        if col > self.queryCols:
128
            tdLog.exit(
S
Shuduo Sang 已提交
129
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
130
                (callerFilename, self.sql, col, self.queryCols))
131
        if self.queryResult[row][col] != data:
S
Shuduo Sang 已提交
132
            tdLog.exit("%s failed: sql:%s row:%d col:%d data:%s != expect:%s" % (
S
Shuduo Sang 已提交
133
                callerFilename, self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
134 135

        if data is None:
S
Shuduo Sang 已提交
136
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
137
                       (self.sql, row, col, self.queryResult[row][col], data))
138
        elif isinstance(data, str):
S
Shuduo Sang 已提交
139
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
140
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
141
        elif isinstance(data, datetime.date):
S
Shuduo Sang 已提交
142
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
sangshuduo's avatar
sangshuduo 已提交
143
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
144
        else:
S
Shuduo Sang 已提交
145
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%d" %
146
                       (self.sql, row, col, self.queryResult[row][col], data))
147 148

    def getData(self, row, col):
S
Shuduo Sang 已提交
149 150 151 152
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

153 154
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
155
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
156
                (callerFilename, self.sql, row))
157 158
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
159
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
160
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
161
        if row > self.queryRows:
162
            tdLog.exit(
S
Shuduo Sang 已提交
163
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
164
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
165
        if col > self.queryCols:
166
            tdLog.exit(
S
Shuduo Sang 已提交
167
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
168
                (callerFilename, self.sql, col, self.queryCols))
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        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
        self.affectedRows = self.cursor.execute(sql)
        return self.affectedRows

    def checkAffectedRows(self, expectAffectedRows):
        if self.affectedRows != expectAffectedRows:
S
Shuduo Sang 已提交
186 187 188 189
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__

S
Shuduo Sang 已提交
190 191 192
            tdLog.exit(
                "%s failed: sql:%s, affectedRows:%d != expect:%d" %
                (callerFilename, self.sql, self.affectedRows, expectAffectedRows))
S
Shuduo Sang 已提交
193
        tdLog.info("sql:%s, affectedRows:%d == expect:%d" %
194 195 196 197
                   (self.sql, self.affectedRows, expectAffectedRows))


tdSql = TDSql()