sql.py 7.2 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
from util.log import *


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

    def init(self, cursor):
        self.cursor = cursor

    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 已提交
48 49 50 51
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
            tdLog.exit(
S
Shuduo Sang 已提交
52
                "%s failed: sql:%s, expect error not occured" %
S
Shuduo Sang 已提交
53
                (callerFilename, sql))
54
        else:
S
Shuduo Sang 已提交
55
            tdLog.info("sql:%s, expect error occured" % (sql))
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    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 已提交
71 72 73
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
74
            tdLog.exit(
S
Shuduo Sang 已提交
75
                "%s failed: sql:%s, queryRows:%d != expect:%d" %
S
Shuduo Sang 已提交
76
                (callerFilename, self.sql, self.queryRows, expectRows))
S
Shuduo Sang 已提交
77
        tdLog.info("sql:%s, queryRows:%d == expect:%d" %
78 79
                   (self.sql, self.queryRows, expectRows))

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    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)


105
    def checkData(self, row, col, data):
S
Shuduo Sang 已提交
106 107 108 109
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

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

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

    def getData(self, row, col):
S
Shuduo Sang 已提交
144 145 146 147
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

148 149
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
150
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
151
                (callerFilename, self.sql, row))
152 153
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
154
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
155
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
156
        if row > self.queryRows:
157
            tdLog.exit(
S
Shuduo Sang 已提交
158
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
159
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
160
        if col > self.queryCols:
161
            tdLog.exit(
S
Shuduo Sang 已提交
162
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
163
                (callerFilename, self.sql, col, self.queryCols))
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        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 已提交
181 182 183 184
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__

S
Shuduo Sang 已提交
185 186 187
            tdLog.exit(
                "%s failed: sql:%s, affectedRows:%d != expect:%d" %
                (callerFilename, self.sql, self.affectedRows, expectAffectedRows))
S
Shuduo Sang 已提交
188
        tdLog.info("sql:%s, affectedRows:%d == expect:%d" %
189 190 191 192
                   (self.sql, self.affectedRows, expectAffectedRows))


tdSql = TDSql()