sql.py 7.5 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:
61 62 63
            self.queryRows = 0
            self.queryCols = 0
            self.queryResult = None
S
Shuduo Sang 已提交
64
            tdLog.info("sql:%s, expect error occured" % (sql))
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

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

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

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

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

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

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

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

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


tdSql = TDSql()