sql.py 6.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 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 80
                   (self.sql, self.queryRows, expectRows))

    def checkData(self, row, col, data):
S
Shuduo Sang 已提交
81 82 83 84
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

85 86
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
87
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
88
                (callerFilename, self.sql, row))
89 90
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
91
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
92
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
93
        if row > self.queryRows:
94
            tdLog.exit(
S
Shuduo Sang 已提交
95
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
96
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
97
        if col > self.queryCols:
98
            tdLog.exit(
S
Shuduo Sang 已提交
99
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
100
                (callerFilename, self.sql, col, self.queryCols))
101
        if self.queryResult[row][col] != data:
S
Shuduo Sang 已提交
102
            tdLog.exit("%s failed: sql:%s row:%d col:%d data:%s != expect:%s" % (
S
Shuduo Sang 已提交
103
                callerFilename, self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
104 105

        if data is None:
S
Shuduo Sang 已提交
106
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
107
                       (self.sql, row, col, self.queryResult[row][col], data))
108
        elif isinstance(data, str):
S
Shuduo Sang 已提交
109
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
110
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
111
        elif isinstance(data, datetime.date):
S
Shuduo Sang 已提交
112
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
sangshuduo's avatar
sangshuduo 已提交
113
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
114
        else:
S
Shuduo Sang 已提交
115
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%d" %
116
                       (self.sql, row, col, self.queryResult[row][col], data))
117 118

    def getData(self, row, col):
S
Shuduo Sang 已提交
119 120 121 122
        frame = inspect.stack()[1]
        callerModule = inspect.getmodule(frame[0])
        callerFilename = callerModule.__file__

123 124
        if row < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
125
                "%s failed: sql:%s, row:%d is smaller than zero" %
S
Shuduo Sang 已提交
126
                (callerFilename, self.sql, row))
127 128
        if col < 0:
            tdLog.exit(
S
Shuduo Sang 已提交
129
                "%s failed: sql:%s, col:%d is smaller than zero" %
S
Shuduo Sang 已提交
130
                (callerFilename, self.sql, col))
S
Shuduo Sang 已提交
131
        if row > self.queryRows:
132
            tdLog.exit(
S
Shuduo Sang 已提交
133
                "%s failed: sql:%s, row:%d is larger than queryRows:%d" %
S
Shuduo Sang 已提交
134
                (callerFilename, self.sql, row, self.queryRows))
S
Shuduo Sang 已提交
135
        if col > self.queryCols:
136
            tdLog.exit(
S
Shuduo Sang 已提交
137
                "%s failed: sql:%s, col:%d is larger than queryCols:%d" %
S
Shuduo Sang 已提交
138
                (callerFilename, self.sql, col, self.queryCols))
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        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 已提交
156 157 158 159
            frame = inspect.stack()[1]
            callerModule = inspect.getmodule(frame[0])
            callerFilename = callerModule.__file__

S
Shuduo Sang 已提交
160 161 162
            tdLog.exit(
                "%s failed: sql:%s, affectedRows:%d != expect:%d" %
                (callerFilename, self.sql, self.affectedRows, expectAffectedRows))
S
Shuduo Sang 已提交
163
        tdLog.info("sql:%s, affectedRows:%d == expect:%d" %
164 165 166 167
                   (self.sql, self.affectedRows, expectAffectedRows))


tdSql = TDSql()