sql.py 7.6 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
    def close(self):
        self.cursor.close()

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

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

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

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

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

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

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

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

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

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

203 204

tdSql = TDSql()