sql.py 9.9 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
L
liuyq-617 已提交
19 20
import psutil
import shutil
21 22 23
from util.log import *


L
liuyq-617 已提交
24

25 26 27 28 29 30
class TDSql:
    def __init__(self):
        self.queryRows = 0
        self.queryCols = 0
        self.affectedRows = 0

31
    def init(self, cursor, log=False):
32 33
        self.cursor = cursor

S
Shuduo Sang 已提交
34
        if (log):
35 36
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            self.cursor.log(caller.filename + ".sql")
S
Shuduo Sang 已提交
37

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
        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)
51 52 53 54 55 56 57 58

    def error(self, sql):
        expectErrNotOccured = True
        try:
            self.cursor.execute(sql)
        except BaseException:
            expectErrNotOccured = False
        if expectErrNotOccured:
59 60
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            tdLog.exit("%s(%d) failed: sql:%s, expect error not occured" % (caller.filename, caller.lineno, 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

    def query(self, sql):
        self.sql = sql
69 70 71 72 73 74 75 76
        try:
            self.cursor.execute(sql)
            self.queryResult = self.cursor.fetchall()
            self.queryRows = len(self.queryResult)
            self.queryCols = len(self.cursor.description)
        except Exception as e:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, repr(e))
77 78
            tdLog.notice("%s(%d) failed: sql:%s, %s" % args)
            raise Exception(repr(e))
79 80
        return self.queryRows

81 82 83 84 85 86 87 88 89
    def waitedQuery(self, sql, expectRows, timeout):
        tdLog.info("sql: %s, try to retrieve %d rows in %d seconds" % (sql, expectRows, timeout))
        self.sql = sql
        try:
            for i in range(timeout):
                self.cursor.execute(sql)
                self.queryResult = self.cursor.fetchall()
                self.queryRows = len(self.queryResult)
                self.queryCols = len(self.cursor.description)
D
dapan1121 已提交
90
                tdLog.info("sql: %s, try to retrieve %d rows,get %d rows" % (sql, expectRows, self.queryRows))
91 92 93 94 95 96
                if self.queryRows >= expectRows:
                    return (self.queryRows, i)
                time.sleep(1)
        except Exception as e:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, repr(e))
97 98
            tdLog.notice("%s(%d) failed: sql:%s, %s" % args)
            raise Exception(repr(e))
99
        return (self.queryRows, timeout)
100

101 102 103 104 105 106 107
    def checkRows(self, expectRows):
        if self.queryRows == expectRows:
            tdLog.info("sql:%s, queryRows:%d == expect:%d" % (self.sql, self.queryRows, expectRows))
        else:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, self.sql, self.queryRows, expectRows)
            tdLog.exit("%s(%d) failed: sql:%s, queryRows:%d != expect:%d" % args)
108

109 110 111 112 113 114 115 116
    def checkCols(self, expectCols):
        if self.queryCols == expectCols:
            tdLog.info("sql:%s, queryCols:%d == expect:%d" % (self.sql, self.queryCols, expectCols))
        else:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, self.sql, self.queryCols, expectCols)
            tdLog.exit("%s(%d) failed: sql:%s, queryCols:%d != expect:%d" % args)

117 118
    def checkRowCol(self, row, col):
        caller = inspect.getframeinfo(inspect.stack()[2][0])
119
        if row < 0:
120 121
            args = (caller.filename, caller.lineno, self.sql, row)
            tdLog.exit("%s(%d) failed: sql:%s, row:%d is smaller than zero" % args)
122
        if col < 0:
123 124
            args = (caller.filename, caller.lineno, self.sql, row)
            tdLog.exit("%s(%d) failed: sql:%s, col:%d is smaller than zero" % args)
125
        if row > self.queryRows:
126 127
            args = (caller.filename, caller.lineno, self.sql, row, self.queryRows)
            tdLog.exit("%s(%d) failed: sql:%s, row:%d is larger than queryRows:%d" % args)
128
        if col > self.queryCols:
129 130
            args = (caller.filename, caller.lineno, self.sql, col, self.queryCols)
            tdLog.exit("%s(%d) failed: sql:%s, col:%d is larger than queryCols:%d" % args)
131

132 133
    def checkDataType(self, row, col, dataType):
        self.checkRowCol(row, col)
134 135
        return self.cursor.istype(col, dataType)

136
    def checkData(self, row, col, data):
137
        self.checkRowCol(row, col) 
138 139
        if self.queryResult[row][col] != data:            
            if str(self.queryResult[row][col]) == str(data):
P
Ping Xiao 已提交
140 141 142 143
                tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
                            (self.sql, row, col, self.queryResult[row][col], data)) 
                return
            elif isinstance(data, float) and abs(self.queryResult[row][col] - data) <= 0.000001:                
P
Ping Xiao 已提交
144 145 146 147 148 149 150
                tdLog.info("sql:%s, row:%d col:%d data:%f == expect:%f" %
                            (self.sql, row, col, self.queryResult[row][col], data)) 
                return
            else:
                caller = inspect.getframeinfo(inspect.stack()[1][0])
                args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data)
                tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args)        
sangshuduo's avatar
sangshuduo 已提交
151 152

        if data is None:
S
Shuduo Sang 已提交
153
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
154
                       (self.sql, row, col, self.queryResult[row][col], data))
155
        elif isinstance(data, str):
S
Shuduo Sang 已提交
156
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
157
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
158
        elif isinstance(data, datetime.date):
P
Ping Xiao 已提交
159 160 161
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
                       (self.sql, row, col, self.queryResult[row][col], data))        
        elif isinstance(data, float):
S
Shuduo Sang 已提交
162
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%s" %
sangshuduo's avatar
sangshuduo 已提交
163
                       (self.sql, row, col, self.queryResult[row][col], data))
P
Ping Xiao 已提交
164
        else:            
S
Shuduo Sang 已提交
165
            tdLog.info("sql:%s, row:%d col:%d data:%s == expect:%d" %
166
                       (self.sql, row, col, self.queryResult[row][col], data))
167 168

    def getData(self, row, col):
169
        self.checkRowCol(row, col)
170 171 172 173 174 175 176 177 178 179 180 181
        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
182 183 184 185 186
        try:
            self.affectedRows = self.cursor.execute(sql)
        except Exception as e:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, repr(e))
187 188
            tdLog.notice("%s(%d) failed: sql:%s, %s" % args)
            raise Exception(repr(e))
189 190 191 192
        return self.affectedRows

    def checkAffectedRows(self, expectAffectedRows):
        if self.affectedRows != expectAffectedRows:
193 194 195 196 197
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, self.sql, self.affectedRows, expectAffectedRows)
            tdLog.exit("%s(%d) failed: sql:%s, affectedRows:%d != expect:%d" % args)

        tdLog.info("sql:%s, affectedRows:%d == expect:%d" % (self.sql, self.affectedRows, expectAffectedRows))
L
liuyq-617 已提交
198 199
    
    def taosdStatus(self, state):
L
liuyq-617 已提交
200
        tdLog.sleep(5)
L
liuyq-617 已提交
201 202
        pstate = 0
        for i in range(30):
L
liuyq-617 已提交
203
            pstate = 0
L
liuyq-617 已提交
204 205
            pl = psutil.pids()
            for pid in pl:
L
liuyq-617 已提交
206 207 208 209 210 211 212
                try:
                    if psutil.Process(pid).name() == 'taosd':
                        print('have already started')
                        pstate = 1
                        break
                except psutil.NoSuchProcess:
                    pass
213 214 215
            if pstate == state :break
            if state or pstate:
                tdLog.sleep(1)
L
liuyq-617 已提交
216 217 218 219
                continue
            pstate = 0
            break
            
L
liuyq-617 已提交
220
        args=(pstate,state)
L
liuyq-617 已提交
221
        if pstate == state:
L
liuyq-617 已提交
222
            tdLog.info("taosd state is %d == expect:%d" %args)
L
liuyq-617 已提交
223
        else:
L
liuyq-617 已提交
224
            tdLog.exit("taosd state is %d != expect:%d" %args)
L
liuyq-617 已提交
225 226 227 228 229 230
        pass

    def haveFile(self, dir, state):
        if os.path.exists(dir) and os.path.isdir(dir):
            if not os.listdir(dir):
                if state :
L
liuyq-617 已提交
231
                    tdLog.exit("dir: %s is empty, expect: not empty" %dir)
L
liuyq-617 已提交
232
                else:
L
liuyq-617 已提交
233
                    tdLog.info("dir: %s is empty, expect: empty" %dir)
L
liuyq-617 已提交
234 235
            else:  
                if state :
236
                    tdLog.info("dir: %s is not empty, expect: not empty" %dir)
L
liuyq-617 已提交
237
                else:
238
                    tdLog.exit("dir: %s is not empty, expect: empty" %dir)  
L
liuyq-617 已提交
239
        else:
L
liuyq-617 已提交
240
            tdLog.exit("dir: %s doesn't exist" %dir)
L
liuyq-617 已提交
241 242 243
    def createDir(self, dir):
        if os.path.exists(dir):
            shutil.rmtree(dir)
L
liuyq-617 已提交
244
            tdLog.info("dir: %s is removed" %dir)
L
liuyq-617 已提交
245
        os.makedirs( dir, 755 )
L
liuyq-617 已提交
246
        tdLog.info("dir: %s is created" %dir)
L
liuyq-617 已提交
247 248
        pass
        
249
tdSql = TDSql()