WIP: write database schema to file JSON
This commit is contained in:
parent
53628d6531
commit
0fd3ab06f9
|
@ -59,15 +59,20 @@ class DBSchema:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def writeFingerprint(self):
|
def debugFingerprint(self):
|
||||||
keys = self.tables.keys()
|
keys = self.tables.keys()
|
||||||
for key in keys:
|
for key in keys:
|
||||||
print "[[ TABLE: <" + key + "> ]]"
|
print "[[ TABLE: <" + key + "> ]]"
|
||||||
tableDef = self.tables[key]
|
tableDef = self.tables[key]
|
||||||
# hehehe = tableDef.SQLstr()
|
|
||||||
print str(tableDef.SQLstr())
|
print str(tableDef.SQLstr())
|
||||||
tableDef.toJSON()
|
tableDef.toJSON()
|
||||||
return
|
|
||||||
|
|
||||||
|
def writeFingerprint(self, filehandle):
|
||||||
|
keys = self.tables.keys()
|
||||||
|
for key in keys:
|
||||||
|
tableDef = self.tables[key]
|
||||||
|
tableDef.toFile(filehandle);
|
||||||
|
|
||||||
|
|
||||||
def getErrorString(self, errorCode):
|
def getErrorString(self, errorCode):
|
||||||
|
@ -94,8 +99,7 @@ class TableDefinition:
|
||||||
self.tableName = ""
|
self.tableName = ""
|
||||||
self.sqlStr = ""
|
self.sqlStr = ""
|
||||||
self.fields = {}
|
self.fields = {}
|
||||||
# self.pkeys = []
|
|
||||||
return
|
|
||||||
|
|
||||||
def loadTable(self, tableName, sqlStr):
|
def loadTable(self, tableName, sqlStr):
|
||||||
self.tableName = tableName
|
self.tableName = tableName
|
||||||
|
@ -104,16 +108,12 @@ class TableDefinition:
|
||||||
if results:
|
if results:
|
||||||
colstr = results.group(1)
|
colstr = results.group(1)
|
||||||
print "[[ TABLE: <" + tableName + "> ]]"
|
print "[[ TABLE: <" + tableName + "> ]]"
|
||||||
# print "FIELDS: " + colstr
|
|
||||||
columns = colstr.split(',')
|
columns = colstr.split(',')
|
||||||
for col in columns:
|
for col in columns:
|
||||||
newField = self.__parseCreateStr(col.strip())
|
newField = self.__parseCreateStr(col.strip())
|
||||||
if newField:
|
if newField:
|
||||||
self.fields[newField['name']] = newField
|
self.fields[newField['name']] = newField
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
# Table Definition
|
# Table Definition
|
||||||
#
|
#
|
||||||
# CREATE TABLE contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT,name_raw_contact_id INTEGER REFERENCES raw_contacts(_id),
|
# CREATE TABLE contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT,name_raw_contact_id INTEGER REFERENCES raw_contacts(_id),
|
||||||
|
@ -131,7 +131,7 @@ class TableDefinition:
|
||||||
newField = {}
|
newField = {}
|
||||||
# photo_id INTEGER REFERENCES data(_id)
|
# photo_id INTEGER REFERENCES data(_id)
|
||||||
# name_raw_contact_id INTEGER REFERENCES raw_contacts(_id)
|
# name_raw_contact_id INTEGER REFERENCES raw_contacts(_id)
|
||||||
results = re.match(r'(\w+)\s+(\w+)\s+REFERENCESS\s+(\W+)', sqltext)
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)\s+REFERENCESS\s+(\W+)', sqltext)
|
||||||
if results:
|
if results:
|
||||||
newField['name'] = results.group(1)
|
newField['name'] = results.group(1)
|
||||||
newField['datatype'] = results.group(2)
|
newField['datatype'] = results.group(2)
|
||||||
|
@ -140,7 +140,16 @@ class TableDefinition:
|
||||||
return newField
|
return newField
|
||||||
# pinned INTEGER NOT NULL DEFAULT 2147483647
|
# pinned INTEGER NOT NULL DEFAULT 2147483647
|
||||||
# send_to_voicemail INTEGER NOT NULL DEFAULT 0
|
# send_to_voicemail INTEGER NOT NULL DEFAULT 0
|
||||||
results = re.match(r'(\w+)\s+(\w+)\s+NOT NULL\s+DEFAULT\s+(\w+)', sqltext)
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)\s+NOT NULL\s+DEFAULT\s+(\w+)', sqltext)
|
||||||
|
if results:
|
||||||
|
newField['name'] = results.group(1)
|
||||||
|
newField['datatype'] = results.group(2)
|
||||||
|
newField['notnull'] = True
|
||||||
|
newField['default'] = results.group(3)
|
||||||
|
return newField
|
||||||
|
# pinned INTEGER DEFAULT 2147483647
|
||||||
|
# send_to_voicemail INTEGER DEFAULT 0
|
||||||
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)\s+DEFAULT\s+(\w+)', sqltext)
|
||||||
if results:
|
if results:
|
||||||
newField['name'] = results.group(1)
|
newField['name'] = results.group(1)
|
||||||
newField['datatype'] = results.group(2)
|
newField['datatype'] = results.group(2)
|
||||||
|
@ -148,7 +157,15 @@ class TableDefinition:
|
||||||
newField['default'] = results.group(3)
|
newField['default'] = results.group(3)
|
||||||
return newField
|
return newField
|
||||||
# _id INTEGER PRIMARY KEY AUTOINCREMENT
|
# _id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||||
results = re.match(r'(\w+)\s+(\w+)\s+PRIMARY KEY\s+AUTOINCREMENT', sqltext)
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)\s+PRIMARY KEY\s+AUTOINCREMENT', sqltext)
|
||||||
|
if results:
|
||||||
|
newField['name'] = results.group(1)
|
||||||
|
newField['datatype'] = results.group(2)
|
||||||
|
newField['primarykey'] = True
|
||||||
|
newField['autoincrement'] = True
|
||||||
|
return newField
|
||||||
|
# _id INTEGER PRIMARY KEY
|
||||||
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)\s+PRIMARY KEY', sqltext)
|
||||||
if results:
|
if results:
|
||||||
newField['name'] = results.group(1)
|
newField['name'] = results.group(1)
|
||||||
newField['datatype'] = results.group(2)
|
newField['datatype'] = results.group(2)
|
||||||
|
@ -156,26 +173,26 @@ class TableDefinition:
|
||||||
newField['autoincrement'] = True
|
newField['autoincrement'] = True
|
||||||
return newField
|
return newField
|
||||||
# FileID INTEGER NOT NULL
|
# FileID INTEGER NOT NULL
|
||||||
results = re.match(r'(\w+)\s+(\w+)\s+NOT NULL', sqltext)
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)\s+NOT NULL', sqltext)
|
||||||
if results:
|
if results:
|
||||||
newField['name'] = results.group(1)
|
newField['name'] = results.group(1)
|
||||||
newField['datatype'] = results.group(2)
|
newField['datatype'] = results.group(2)
|
||||||
newField['notnull'] = True
|
newField['notnull'] = True
|
||||||
return newField
|
return newField
|
||||||
# PRIMARY KEY (field_name,
|
# PRIMARY KEY (field_name,
|
||||||
results = re.match(r'PRIMARY KEY \((\w+)\,?', sqltext)
|
results = re.match(r'PRIMARY KEY \((?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\,?', sqltext)
|
||||||
if results:
|
if results:
|
||||||
field = self.fields[results.group(1)]
|
field = self.fields[results.group(1)]
|
||||||
field['primarykey'] = True
|
field['primarykey'] = True
|
||||||
return False
|
return False
|
||||||
# custom_ringtone TEXT
|
# custom_ringtone TEXT
|
||||||
results = re.match(r'(\w+)\s+(\w+)', sqltext)
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\s+(\w+)', sqltext)
|
||||||
if results:
|
if results:
|
||||||
newField['name'] = results.group(1)
|
newField['name'] = results.group(1)
|
||||||
newField['datatype'] = results.group(2)
|
newField['datatype'] = results.group(2)
|
||||||
return newField
|
return newField
|
||||||
# field_name)
|
# field_name)
|
||||||
results = re.match(r'\,?(\w+)\)', sqltext)
|
results = re.match(r'(?:[`|\"|\'])*(\w+)(?:[`|\"|\'])*\)', sqltext)
|
||||||
if results:
|
if results:
|
||||||
field = self.fields[results.group(1)]
|
field = self.fields[results.group(1)]
|
||||||
field['primarykey'] = True
|
field['primarykey'] = True
|
||||||
|
@ -198,6 +215,10 @@ class TableDefinition:
|
||||||
print json.dumps(self.fields)
|
print json.dumps(self.fields)
|
||||||
|
|
||||||
|
|
||||||
|
def toFile(self, filehandle):
|
||||||
|
json.dump(self.fields, filehandle, sort_keys=True, indent=4)
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
global delimeter
|
global delimeter
|
||||||
retstr = ""
|
retstr = ""
|
||||||
|
|
11
main.py
11
main.py
|
@ -8,15 +8,17 @@ from libs import fingerprint
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
(filein, verbose) = parseArgs()
|
(filein, fileout, verbose) = parseArgs()
|
||||||
#retVal = fingerprint.scanDB(filein)
|
#retVal = fingerprint.scanDB(filein)
|
||||||
|
|
||||||
db = fingerprint.DBSchema()
|
db = fingerprint.DBSchema()
|
||||||
retVal = db.scanDBFile(filein)
|
retVal = db.scanDBFile(filein)
|
||||||
|
|
||||||
if (retVal > 0):
|
if (retVal > 0):
|
||||||
print "\n\n"
|
fh = open(fileout, "w")
|
||||||
db.writeFingerprint()
|
db.debugFingerprint()
|
||||||
|
db.writeFingerprint(fh)
|
||||||
|
fh.close()
|
||||||
else:
|
else:
|
||||||
print db.getErrorString(retVal)
|
print db.getErrorString(retVal)
|
||||||
|
|
||||||
|
@ -31,7 +33,8 @@ def parseArgs():
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if (args.verbose):
|
if (args.verbose):
|
||||||
verbose = args.verbose
|
verbose = args.verbose
|
||||||
return (args.file, verbose)
|
fileout = args.file + "_" + timestr
|
||||||
|
return (args.file, fileout, verbose)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue