forked from anthony-tuininga/cx_PyOracleLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx_ExportData.py
84 lines (73 loc) · 3.11 KB
/
cx_ExportData.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Module for use in exporting data to a file."""
import cx_Logging
import cx_Oracle
import pickle
import sys
# define constant for pickle protocol
BINARY = 1
class Exporter:
"""Export data from a database in a cross platform manner."""
def __init__(self, outFile, cursor, reportPoint, prefix = ""):
self.outFile = outFile
self.cursor = cursor
self.cursor.numbersAsStrings = True
self.reportPoint = reportPoint
self.prefix = prefix
def __ExportTableHeader(self, tableName):
"""Export the table header to the file."""
cx_Logging.Trace("%sExporting table %s...", self.prefix, tableName)
self.cursor.execute("select * from " + tableName)
columns = [(r[0], self.__StringRepOfType(r[1], r[2])) \
for r in self.cursor.description]
pickle.dump(tableName, self.outFile, BINARY)
pickle.dump(columns, self.outFile, BINARY)
def __ExportTableRows(self, rowsToSkip, rowLimit):
"""Export the rows in the table to the file."""
numRows = 0
format = self.prefix + " %d rows exported."
cursor = self.cursor
outFile = self.outFile
reportPoint = self.reportPoint
for row in cursor:
numRows += 1
if numRows > rowLimit:
numRows -= 1
break
elif numRows > rowsToSkip:
pickle.dump(row, outFile, BINARY)
if reportPoint is not None and numRows % reportPoint == 0:
cx_Logging.Trace(format, numRows)
if reportPoint is None or numRows == 0 or numRows % reportPoint != 0:
cx_Logging.Trace(format, numRows)
pickle.dump(None, outFile, BINARY)
def __StringRepOfType(self, dataType, displaySize):
"""Return the string representation of the type."""
if dataType == cx_Oracle.NUMBER:
return "STRING,%d" % displaySize
for stringRep in ("BINARY", "STRING", "ROWID", "FIXED_CHAR", "NCHAR",
"FIXED_NCHAR"):
if getattr(cx_Oracle, stringRep) == dataType:
return "%s,%d" % (stringRep, displaySize)
for stringRep in ("BLOB", "CLOB", "NCLOB", "DATETIME", "LONG_BINARY",
"LONG_STRING", "TIMESTAMP"):
if getattr(cx_Oracle, stringRep) == dataType:
return stringRep
raise Exception("Unsupported type: %s!" % dataType)
def ExportTable(self, tableName, rowsToSkip = None, rowLimit = None):
"""Export the data in the table to the file."""
if rowsToSkip is None:
rowsToSkip = 0
if rowLimit is None:
rowLimit = sys.maxsize
self.__ExportTableHeader(tableName)
self.__ExportTableRows(rowsToSkip, rowLimit)
def FinalizeExport(self):
"""Finalize the export."""
pickle.dump(None, self.outFile, BINARY)
def TablesInSchema(self):
"""Return a list of tables found in the schema."""
self.cursor.execute("""
select table_name
from user_tables
where temporary = 'N'""")
return [n for n, in self.cursor.fetchall()]