sql.py 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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
###################################################################
#           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
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:
sangshuduo's avatar
sangshuduo 已提交
47
            tdLog.exit("failed: sql:%.40s, expect error not occured" % (sql))
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        else:
            tdLog.info("sql:%.40s, expect error occured" % (sql))

    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:
            tdLog.exit(
66
                "failed: sql:%.40s, queryRows:%d != expect:%d" %
67 68 69 70 71 72 73
                (self.sql, self.queryRows, expectRows))
        tdLog.info("sql:%.40s, queryRows:%d == expect:%d" %
                   (self.sql, self.queryRows, expectRows))

    def checkData(self, row, col, data):
        if row < 0:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
74
                "failed: sql:%.40s, row:%d is smaller than zero" %
75 76 77
                (self.sql, row))
        if col < 0:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
78
                "failed: sql:%.40s, col:%d is smaller than zero" %
79 80 81
                (self.sql, col))
        if row >= self.queryRows:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
82
                "failed: sql:%.40s, row:%d is larger than queryRows:%d" %
83 84 85
                (self.sql, row, self.queryRows))
        if col >= self.queryCols:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
86
                "failed: sql:%.40s, col:%d is larger than queryRows:%d" %
87 88 89
                (self.sql, col, self.queryCols))
        if self.queryResult[row][col] != data:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
90
                "failed: sql:%.40s row:%d col:%d data:%s != expect:%s" %
91
                (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
92 93 94

        if data is None:
            tdLog.info("sql:%.40s, row:%d col:%d data:%s == expect:%s" %
95
                       (self.sql, row, col, self.queryResult[row][col], data))
96 97 98
        elif isinstance(data, str):
            tdLog.info("sql:%.40s, row:%d col:%d data:%s == expect:%s" %
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
99 100 101
        elif isinstance(data, datetime.date):
            tdLog.info("sql:%.40s, row:%d col:%d data:%s == expect:%s" %
                       (self.sql, row, col, self.queryResult[row][col], data))
sangshuduo's avatar
sangshuduo 已提交
102 103
        else:
            tdLog.info("sql:%.40s, row:%d col:%d data:%s == expect:%d" %
104
                       (self.sql, row, col, self.queryResult[row][col], data))
105 106 107 108

    def getData(self, row, col):
        if row < 0:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
109
                "failed: sql:%.40s, row:%d is smaller than zero" %
110 111 112
                (self.sql, row))
        if col < 0:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
113
                "failed: sql:%.40s, col:%d is smaller than zero" %
114 115 116
                (self.sql, col))
        if row >= self.queryRows:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
117
                "failed: sql:%.40s, row:%d is larger than queryRows:%d" %
118 119 120
                (self.sql, row, self.queryRows))
        if col >= self.queryCols:
            tdLog.exit(
sangshuduo's avatar
sangshuduo 已提交
121
                "failed: sql:%.40s, col:%d is larger than queryRows:%d" %
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
                (self.sql, col, self.queryCols))
        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:
sangshuduo's avatar
sangshuduo 已提交
140
            tdLog.exit("failed: sql:%.40s, affectedRows:%d != expect:%d" %
141 142 143 144 145 146
                       (self.sql, self.affectedRows, expectAffectedRows))
        tdLog.info("sql:%.40s, affectedRows:%d == expect:%d" %
                   (self.sql, self.affectedRows, expectAffectedRows))


tdSql = TDSql()