Author: nunofsantos
Date: 2007-11-09 12:04:09 -0500 (Fri, 09 Nov 2007)
New Revision: 1279
Modified:
mgmt/mint/schemaparser.py
Log:
updted to reflect changes in xml schema
Modified: mgmt/mint/schemaparser.py
===================================================================
--- mgmt/mint/schemaparser.py 2007-11-08 22:31:09 UTC (rev 1278)
+++ mgmt/mint/schemaparser.py 2007-11-09 17:04:09 UTC (rev 1279)
@@ -8,20 +8,27 @@
# mapping between xml schema types and database column types
dataTypesMap = {}
sqlOutput = ""
+ indexes = ""
+ currentTable = ""
syle = None
def __init__(self, style):
self.sqlOutput = ""
+ self.indexes = ""
+ self.currentTable = ""
self.style = style
- self.dataTypesMap["string"] = "VARCHAR(2000)"
+ # see xml/MgmtTypes.xml
+ self.dataTypesMap["objId"] = "INT8"
+ self.dataTypesMap["uint8"] = self.dataTypesMap["hilo8"] =
self.dataTypesMap["count8"] = "INT2"
+ self.dataTypesMap["uint16"] = self.dataTypesMap["hilo16"] =
self.dataTypesMap["count16"] = "INT2"
+ self.dataTypesMap["uint32"] = self.dataTypesMap["hilo32"] =
self.dataTypesMap["count32"] = "INT4"
+ self.dataTypesMap["uint64"] = self.dataTypesMap["hilo64"] =
self.dataTypesMap["count64"] = "INT8"
self.dataTypesMap["bool"] = "BOOLEAN"
- self.dataTypesMap["uint16"] = "INT2"
- self.dataTypesMap["uint32"] = "INT4"
- self.dataTypesMap["uint32_wm"] = "INT4"
- self.dataTypesMap["uint64"] = "INT8"
- self.dataTypesMap["ipAddress"] = "VARCHAR(100)"
- self.dataTypesMap["enum"] = "VARCHAR(100) CHECK ("
- self.dataTypesMap["fieldTable"] = "VARCHAR(2000)"
+ self.dataTypesMap["sstr"] = "VARCHAR(1000)"
+ self.dataTypesMap["lstr"] = "VARCHAR(4000)"
+ #self.dataTypesMap["ipAddress"] = "VARCHAR(100)"
+ #self.dataTypesMap["enum"] = "VARCHAR(100) CHECK ("
+ #self.dataTypesMap["fieldTable"] = "VARCHAR(2000)"
def generate(self, name, elements, type):
self.startTable(name, type)
@@ -34,8 +41,10 @@
actualName = name
else:
actualName = name + "_stats"
- self.sqlOutput += "CREATE TABLE " + actualName + " (\n"
+ self.currentTable = actualName
+ self.sqlOutput += "\nCREATE TABLE " + actualName + " (\n"
self.sqlOutput += " id BIGSERIAL PRIMARY KEY,\n"
+ self.sqlOutput += " id_original BIGINT,\n"
if (type == "config"):
self.sqlOutput += " " + name + "_stats_id BIGINT,\n"
else:
@@ -48,8 +57,14 @@
def generateTimestampColumn(self, col):
self.sqlOutput += " " + col + "_time TIMESTAMP,\n"
+ def generateIndex(self, name):
+ self.indexes += "\nCREATE INDEX " + self.currentTable + "_" +
name + "_index ON " + self.currentTable + "(" + name +
");\n"
+
def generateColumn(self, elem, suffix=""):
actualName = self.style.pythonAttrToDBColumn(elem["@name"] + suffix)
+ if (actualName.endswith("_id")):
+ # handle case where XML attrib ends in "Id", which confuses SqlObject
into thinking it's a foreign key
+ actualName = actualName.replace("_id", "_ident")
if (elem["(a)type"].startswith("enum")):
actualType = "enum"
params = actualName + " IN ("
@@ -64,14 +79,18 @@
self.sqlOutput += " " + foreignKeyTable + "_id BIGINT REFERENCES
" + foreignKeyTable + ",\n"
else:
self.sqlOutput += " " + actualName + " " +
self.dataTypesMap[actualType] + params + ",\n"
- if (elem["(a)type"].endswith("_wm") and suffix == ""):
+ if (elem["(a)type"].startswith("hilo") and suffix ==
""):
self.generateColumn(elem, "High")
self.generateColumn(elem, "Low")
+ if (elem["@index"] == "y"):
+ self.generateIndex(actualName)
def endTable(self, name, type):
- self.sqlOutput = self.sqlOutput[:-2] + "\n);\n\n"
+ self.sqlOutput = self.sqlOutput[:-2] + "\n);\n" + self.indexes
if (type == "inst"):
- self.sqlOutput += "ALTER TABLE " + name + " ADD FOREIGN KEY ("
+ name + "_stats_id) REFERENCES " + name + "_stats;\n\n"
+ self.sqlOutput += "\nALTER TABLE " + name + " ADD FOREIGN KEY
(" + name + "_stats_id) REFERENCES " + name + "_stats;\n"
+ self.currentTable = ""
+ self.indexes = ""
def getCode(self):
return self.sqlOutput
@@ -82,12 +101,14 @@
"""generates Python code from broker XML schema"""
pythonOutput = ""
+ additional = ""
syle = None
-
+
def __init__(self, style, dsn):
self.pythonOutput = "from sqlobject import *\n\n" + \
"conn = connectionForURI('" + dsn +
"')\n" + \
"sqlhub.processConnection = conn\n\n"
+ self.additional = ""
self.style = style
def attrNameFromDbColumn(self, name, prefix="", removeSuffix=""):
@@ -104,18 +125,20 @@
self.generateMethod(elem)
self.endClass(name)
- def generateForeignKeys(self, name, elements):
- dbName = name
+ def generateAdditionalAttribs(self, name, elements):
name = self.attrNameFromDbColumn(name)
for elem in elements:
refName = elem["@name"]
if (refName.endswith("Ref")):
+ # generate foreign keys
refName = self.attrNameFromDbColumn(refName, "Mgmt", "Ref")
- self.pythonOutput += "\n" + refName +
".sqlmeta.addJoin(MultipleJoin('" + name + \
- "', joinMethodName='all" +
name.replace("Mgmt", "") + "s'))\n\n"
+ # add missing attribute (not added correctly with SqlObject 0.7.7; may need to be
removed in later versions)
+ self.pythonOutput += " _SO_class_" +
self.style.pythonAttrToDBColumn(refName).capitalize() + " = " + refName +
"()\n"
+ self.additional += "\n" + refName +
".sqlmeta.addJoin(MultipleJoin('" + name + \
+ "', joinMethodName='all" +
name.replace("Mgmt", "") + "s'))"
def startClass(self, name):
- self.pythonOutput += "class " + name + "(SQLObject):\n"
+ self.pythonOutput += "\nclass " + name + "(SQLObject):\n"
self.pythonOutput += " class sqlmeta:\n"
self.pythonOutput += " fromDatabase = True\n"
@@ -126,14 +149,16 @@
comment = ""
self.pythonOutput += " def " + elem["@name"] +
"():\n"
self.pythonOutput += comment
- self.pythonOutput += " pass\n\n"
+ self.pythonOutput += " pass\n"
def endClass(self, name=""):
+ if (self.additional != ""):
+ self.pythonOutput += self.additional + "\n"
+ self.additional = ""
if (name != "" and not name.endswith("Stats")):
# add missing attribute (not added correctly with SqlObject 0.7.7; may need to be
removed in later versions)
self.pythonOutput += " _SO_class_" +
self.style.pythonAttrToDBColumn(name).capitalize() + "_stats = " + name +
"Stats()\n"
- self.pythonOutput += "\n"
-
+
def getCode(self):
return self.pythonOutput
@@ -166,13 +191,13 @@
sqlFile = open(self.options["sqlOutput"], "w")
pythonFile = open(self.options["pythonOutput"], "w")
schema = qpid.mllib.xml_parse(self.options["schemaXML"])
- objects = schema.query["schema/object"]
- for obj in objects:
- actualName = "mgmt_" + obj["@name"]
- self.sqlGen.generate(actualName, obj.query["configElement"],
"config")
- self.sqlGen.generate(actualName, obj.query["instElement"],
"inst")
- self.pythonGen.generate(actualName, obj.query["method"])
- self.pythonGen.generateForeignKeys(actualName,
obj.query["configElement"])
+ classes = schema.query["schema/class"]
+ for cls in classes:
+ actualName = "mgmt_" + cls["@name"]
+ self.sqlGen.generate(actualName, cls.query["configElement"],
"config")
+ self.sqlGen.generate(actualName, cls.query["instElement"],
"inst")
+ self.pythonGen.generate(actualName, cls.query["method"])
+ self.pythonGen.generateAdditionalAttribs(actualName,
cls.query["configElement"])
sqlFile.write(self.sqlGen.getCode())
pythonFile.write(self.pythonGen.getCode())
sqlFile.close()