cursor.py 9.4 KB
Newer Older
S
slguan 已提交
1 2
from .cinterface import CTaosInterface
from .error import *
3
from .constants import FieldType
S
slguan 已提交
4

5
# querySeqNum = 0
6

7

S
slguan 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class TDengineCursor(object):
    """Database cursor which is used to manage the context of a fetch operation.

    Attributes:
        .description: Read-only attribute consists of 7-item sequences:

            > name (mondatory)
            > type_code (mondatory)
            > display_size
            > internal_size
            > precision
            > scale
            > null_ok

            This attribute will be None for operations that do not return rows or
            if the cursor has not had an operation invoked via the .execute*() method yet.

        .rowcount:This read-only attribute specifies the number of rows that the last
26
            .execute*() produced (for DQL statements like SELECT) or affected
S
slguan 已提交
27 28 29
    """

    def __init__(self, connection=None):
30
        self._description = []
S
slguan 已提交
31 32 33 34 35 36 37
        self._rowcount = -1
        self._connection = None
        self._result = None
        self._fields = None
        self._block = None
        self._block_rows = -1
        self._block_iter = 0
38
        self._affected_rows = 0
S
Shuduo Sang 已提交
39
        self._logfile = ""
S
slguan 已提交
40 41 42 43 44 45 46 47

        if connection is not None:
            self._connection = connection

    def __iter__(self):
        return self

    def __next__(self):
48 49 50 51 52 53
        return self._taos_next()

    def next(self):
        return self._taos_next()

    def _taos_next(self):
S
slguan 已提交
54 55 56 57
        if self._result is None or self._fields is None:
            raise OperationalError("Invalid use of fetch iterator")

        if self._block_rows <= self._block_iter:
58
            block, self._block_rows = CTaosInterface.fetchRow(
59
                self._result, self._fields)
S
slguan 已提交
60 61 62 63 64
            if self._block_rows == 0:
                raise StopIteration
            self._block = list(map(tuple, zip(*block)))
            self._block_iter = 0

65
        data = self._block[self._block_iter]
S
slguan 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        self._block_iter += 1

        return data

    @property
    def description(self):
        """Return the description of the object.
        """
        return self._description

    @property
    def rowcount(self):
        """Return the rowcount of the object
        """
        return self._rowcount

82 83 84 85 86 87
    @property
    def affected_rows(self):
        """Return the rowcount of insertion
        """
        return self._affected_rows

S
slguan 已提交
88 89 90 91 92 93 94
    def callproc(self, procname, *args):
        """Call a stored database procedure with the given name.

        Void functionality since no stored procedures.
        """
        pass

S
Shuduo Sang 已提交
95 96 97
    def log(self, logfile):
        self._logfile = logfile

S
slguan 已提交
98 99 100 101 102
    def close(self):
        """Close the cursor.
        """
        if self._connection is None:
            return False
103

S
slguan 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117
        self._reset_result()
        self._connection = None

        return True

    def execute(self, operation, params=None):
        """Prepare and execute a database operation (query or command).
        """
        if not operation:
            return None

        if not self._connection:
            # TODO : change the exception raised here
            raise ProgrammingError("Cursor is not connected")
118

S
slguan 已提交
119 120 121 122 123
        self._reset_result()

        stmt = operation
        if params is not None:
            pass
124 125 126 127 128

        # global querySeqNum
        # querySeqNum += 1
        # localSeqNum = querySeqNum # avoid raice condition
        # print("   >> Exec Query ({}): {}".format(localSeqNum, str(stmt)))
129
        self._result = CTaosInterface.query(self._connection._conn, stmt)
130
        # print("   << Query ({}) Exec Done".format(localSeqNum))
S
Shuduo Sang 已提交
131 132 133 134
        if (self._logfile):
            with open(self._logfile, "a") as logfile:
                logfile.write("%s;\n" % operation)

T
Tao Liu 已提交
135 136
        errno = CTaosInterface.libtaos.taos_errno(self._result)
        if errno == 0:
137
            if CTaosInterface.fieldsCount(self._result) == 0:
138
                self._affected_rows += CTaosInterface.affectedRows(
139 140
                    self._result)
                return CTaosInterface.affectedRows(self._result)
S
slguan 已提交
141
            else:
142 143
                self._fields = CTaosInterface.useResult(
                    self._result)
S
slguan 已提交
144 145
                return self._handle_result()
        else:
146 147
            raise ProgrammingError(
                CTaosInterface.errStr(
148
                    self._result), errno)
S
slguan 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162

    def executemany(self, operation, seq_of_parameters):
        """Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters.
        """
        pass

    def fetchone(self):
        """Fetch the next row of a query result set, returning a single sequence, or None when no more data is available.
        """
        pass

    def fetchmany(self):
        pass

163 164 165 166 167 168 169
    def istype(self, col, dataType):
        if (dataType.upper() == "BOOL"):
            if (self._description[col][1] == FieldType.C_BOOL):
                return True
        if (dataType.upper() == "TINYINT"):
            if (self._description[col][1] == FieldType.C_TINYINT):
                return True
170 171 172 173 174 175 176 177 178
        if (dataType.upper() == "TINYINT UNSIGNED"):
            if (self._description[col][1] == FieldType.C_TINYINT_UNSIGNED):
                return True
        if (dataType.upper() == "SMALLINT"):
            if (self._description[col][1] == FieldType.C_SMALLINT):
                return True
        if (dataType.upper() == "SMALLINT UNSIGNED"):
            if (self._description[col][1] == FieldType.C_SMALLINT_UNSIGNED):
                return True
179 180 181
        if (dataType.upper() == "INT"):
            if (self._description[col][1] == FieldType.C_INT):
                return True
182 183 184
        if (dataType.upper() == "INT UNSIGNED"):
            if (self._description[col][1] == FieldType.C_INT_UNSIGNED):
                return True
185
        if (dataType.upper() == "BIGINT"):
186 187 188 189
            if (self._description[col][1] == FieldType.C_BIGINT):
                return True
        if (dataType.upper() == "BIGINT UNSIGNED"):
            if (self._description[col][1] == FieldType.C_BIGINT_UNSIGNED):
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                return True
        if (dataType.upper() == "FLOAT"):
            if (self._description[col][1] == FieldType.C_FLOAT):
                return True
        if (dataType.upper() == "DOUBLE"):
            if (self._description[col][1] == FieldType.C_DOUBLE):
                return True
        if (dataType.upper() == "BINARY"):
            if (self._description[col][1] == FieldType.C_BINARY):
                return True
        if (dataType.upper() == "TIMESTAMP"):
            if (self._description[col][1] == FieldType.C_TIMESTAMP):
                return True
        if (dataType.upper() == "NCHAR"):
            if (self._description[col][1] == FieldType.C_NCHAR):
                return True

        return False

209
    def fetchall_row(self):
S
slguan 已提交
210 211 212 213
        """Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples). Note that the cursor's arraysize attribute can affect the performance of this operation.
        """
        if self._result is None or self._fields is None:
            raise OperationalError("Invalid use of fetchall")
214

S
slguan 已提交
215 216 217
        buffer = [[] for i in range(len(self._fields))]
        self._rowcount = 0
        while True:
218 219
            block, num_of_fields = CTaosInterface.fetchRow(
                self._result, self._fields)
B
Bomin Zhang 已提交
220 221
            errno = CTaosInterface.libtaos.taos_errno(self._result)
            if errno != 0:
222 223 224
                raise ProgrammingError(
                    CTaosInterface.errStr(
                        self._result), errno)
225 226
            if num_of_fields == 0:
                break
S
slguan 已提交
227 228 229
            self._rowcount += num_of_fields
            for i in range(len(self._fields)):
                buffer[i].extend(block[i])
230
        return list(map(tuple, zip(*buffer)))
S
slguan 已提交
231

232
    def fetchall(self):
233 234 235 236 237 238
        if self._result is None or self._fields is None:
            raise OperationalError("Invalid use of fetchall")

        buffer = [[] for i in range(len(self._fields))]
        self._rowcount = 0
        while True:
239 240
            block, num_of_fields = CTaosInterface.fetchBlock(
                self._result, self._fields)
241 242
            errno = CTaosInterface.libtaos.taos_errno(self._result)
            if errno != 0:
243 244 245 246 247
                raise ProgrammingError(
                    CTaosInterface.errStr(
                        self._result), errno)
            if num_of_fields == 0:
                break
248 249 250 251
            self._rowcount += num_of_fields
            for i in range(len(self._fields)):
                buffer[i].extend(block[i])
        return list(map(tuple, zip(*buffer)))
252

S
slguan 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266
    def nextset(self):
        """
        """
        pass

    def setinputsize(self, sizes):
        pass

    def setutputsize(self, size, column=None):
        pass

    def _reset_result(self):
        """Reset the result to unused version.
        """
267
        self._description = []
S
slguan 已提交
268
        self._rowcount = -1
T
Tao Liu 已提交
269 270
        if self._result is not None:
            CTaosInterface.freeResult(self._result)
S
slguan 已提交
271 272 273 274 275
        self._result = None
        self._fields = None
        self._block = None
        self._block_rows = -1
        self._block_iter = 0
276
        self._affected_rows = 0
277

S
slguan 已提交
278 279 280 281 282
    def _handle_result(self):
        """Handle the return result from query.
        """
        self._description = []
        for ele in self._fields:
283 284 285
            self._description.append(
                (ele['name'], ele['type'], None, None, None, None, False))

286
        return self._result