sql.py 9.3 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
from util.log import *
20
import numpy as np
21 22 23 24 25 26 27 28


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

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

S
Shuduo Sang 已提交
32 33 34 35 36 37
        if (log):
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
            self.cursor.log(callerFilename + ".sql")

38 39 40 41 42
    def close(self):
        self.cursor.close()

    def prepare(self):
        tdLog.info("prepare database:db")
L
liu0x54 已提交
43 44 45 46 47 48 49 50 51 52 53 54
        s = 'reset query cache'
        print(s)
        self.cursor.execute(s)
        s = 'drop database if exists db'
        print(s)
        self.cursor.execute(s)
        s = 'create database db'
        print(s)
        self.cursor.execute(s)
        s = 'use db'
        print(s)
        self.cursor.execute(s)
55 56 57 58 59 60 61 62

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

    def query(self, sql):
        self.sql = sql
L
liu0x54 已提交
77
        print(sql)
78 79 80 81 82 83 84 85 86 87 88 89
        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 已提交
90 91 92
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
93
            tdLog.exit(
S
Shuduo Sang 已提交
94
                "%s failed: sql:%s, queryRows:%d != expect:%d" %
S
Shuduo Sang 已提交
95
                (callerFilename, self.sql, self.queryRows, expectRows))
S
Shuduo Sang 已提交
96
        tdLog.info("sql:%s, queryRows:%d == expect:%d" %
97 98
                   (self.sql, self.queryRows, expectRows))

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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)

123
    def checkData(self, row, col, data):
S
Shuduo Sang 已提交
124 125 126 127
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

128 129
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
130
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
131
                (callerFilename, self.sql, row))
132 133
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
134
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
135
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
136
        if row > self.queryRows:
137
            tdLog.exit(
S
Shuduo Sang 已提交
138
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
139
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
140
        if col > self.queryCols:
141
            tdLog.exit(
S
Shuduo Sang 已提交
142
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
143
                (callerFilename, self.sql, col, self.queryCols))
144
        if self.queryResult[row][col] != data:
S
Shuduo Sang 已提交
145
            tdLog.exit("%s failed: sql:%s row:%d col:%d data:%s != expect:%s" % (
S
Shuduo Sang 已提交
146
                callerFilename, self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
147 148

        if data is None:
S
Shuduo Sang 已提交
149
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
150
                       (self.sql, row, col, self.queryResult[row][col], data))
151
        elif isinstance(data, str):
S
Shuduo Sang 已提交
152
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
153
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
154
        elif isinstance(data, datetime.date):
S
Shuduo Sang 已提交
155
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
sangshuduo's avatar
sangshuduo 已提交
156
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
157
        else:
S
Shuduo Sang 已提交
158
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%d" %
159
                       (self.sql, row, col, self.queryResult[row][col], data))
160 161

    def getData(self, row, col):
S
Shuduo Sang 已提交
162 163 164 165
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

166 167
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
168
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
169
                (callerFilename, self.sql, row))
170 171
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
172
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
173
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
174
        if row > self.queryRows:
175
            tdLog.exit(
S
Shuduo Sang 已提交
176
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
177
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
178
        if col > self.queryCols:
179
            tdLog.exit(
S
Shuduo Sang 已提交
180
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
181
                (callerFilename, self.sql, col, self.queryCols))
182 183 184 185 186 187 188 189 190 191 192 193
        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
L
liu0x54 已提交
194
        print(sql)
195 196 197 198 199
        self.affectedRows = self.cursor.execute(sql)
        return self.affectedRows

    def checkAffectedRows(self, expectAffectedRows):
        if self.affectedRows != expectAffectedRows:
S
Shuduo Sang 已提交
200 201 202 203
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__

S
Shuduo Sang 已提交
204 205 206
            tdLog.exit(
                "%s failed: sql:%s, affectedRows:%d != expect:%d" %
                (callerFilename, self.sql, self.affectedRows, expectAffectedRows))
S
Shuduo Sang 已提交
207
        tdLog.info("sql:%s, affectedRows:%d == expect:%d" %
208 209
                   (self.sql, self.affectedRows, expectAffectedRows))

210 211 212 213 214 215 216 217
    def checkColumnSorted(self, col, order):
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

        if col < 0:
            tdLog.exit(
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
218
                (callerFilename, self.sql, col))
219 220 221 222
        if col > self.queryCols:
            tdLog.exit(
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
                (callerFilename, self.sql, col, self.queryCols))
S
Shuduo Sang 已提交
223

224
        matrix = np.array(self.queryResult)
S
Shuduo Sang 已提交
225
        list = matrix[:, 0]
226

S
Shuduo Sang 已提交
227 228 229 230
        if order == "" or order.upper() == "ASC":
            if all(sorted(list) == list):
                tdLog.info(
                    "sql:%s, column :%d is sorted in accending order as expected" %
231 232
                    (self.sql, col))
            else:
S
Shuduo Sang 已提交
233 234
                tdLog.exit(
                    "%s failed: sql:%s, col:%d is not sorted in accesnind order" %
235
                    (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
236 237 238 239
        elif order.upper() == "DESC":
            if all(sorted(list, reverse=True) == list):
                tdLog.info(
                    "sql:%s, column :%d is sorted in decending order as expected" %
240 241
                    (self.sql, col))
            else:
S
Shuduo Sang 已提交
242 243 244
                tdLog.exit(
                    "%s failed: sql:%s, col:%d is not sorted in decending order" %
                    (callerFilename, self.sql, col))
245
        else:
S
Shuduo Sang 已提交
246 247 248 249
            tdLog.exit(
                "%s failed: sql:%s, the order provided for col:%d is not correct" %
                (callerFilename, self.sql, col))

250 251

tdSql = TDSql()