sql.py 9.1 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 43 44 45 46 47 48 49 50 51 52 53 54
    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 已提交
55 56 57 58
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
            tdLog.exit(
S
Shuduo Sang 已提交
59
                "%s failed: sql:%s, expect error not occured" %
S
Shuduo Sang 已提交
60
                (callerFilename, sql))
61
        else:
62 63 64
            self.queryRows = 0
            self.queryCols = 0
            self.queryResult = None
S
Shuduo Sang 已提交
65
            tdLog.info("sql:%s, expect error occured" % (sql))
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    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 已提交
81 82 83
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__
84
            tdLog.exit(
S
Shuduo Sang 已提交
85
                "%s failed: sql:%s, queryRows:%d != expect:%d" %
S
Shuduo Sang 已提交
86
                (callerFilename, self.sql, self.queryRows, expectRows))
S
Shuduo Sang 已提交
87
        tdLog.info("sql:%s, queryRows:%d == expect:%d" %
88 89
                   (self.sql, self.queryRows, expectRows))

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    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)

114
    def checkData(self, row, col, data):
S
Shuduo Sang 已提交
115 116 117 118
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

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

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

    def getData(self, row, col):
S
Shuduo Sang 已提交
153 154 155 156
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

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

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

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    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" %
                (callerFilename, self.sql, col))        
        if col > self.queryCols:
            tdLog.exit(
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
                (callerFilename, self.sql, col, self.queryCols))
        
        matrix = np.array(self.queryResult)
        list = matrix[:, 0]        

        if order == "" or order.upper() == "ASC": 
            if all(sorted(list) == list):                
                tdLog.info("sql:%s, column :%d is sorted in accending order as expected" %
                    (self.sql, col))
            else:
                tdLog.exit("%s failed: sql:%s, col:%d is not sorted in accesnind order" %
                    (callerFilename, self.sql, col))
        elif order.upper() == "DESC":            
            if all(sorted(list, reverse=True) == list):                
                tdLog.info("sql:%s, column :%d is sorted in decending order as expected" %
                    (self.sql, col))
            else:
                tdLog.exit("%s failed: sql:%s, col:%d is not sorted in decending order" %
                    (callerFilename, self.sql, col)) 
        else:
            tdLog.exit("%s failed: sql:%s, the order provided for col:%d is not correct" %
                    (callerFilename, self.sql, col)) 
234 235

tdSql = TDSql()