Author: justi9
Date: 2010-02-02 16:43:22 -0500 (Tue, 02 Feb 2010)
New Revision: 3836
Added:
mgmt/trunk/rosemary/python/rosemary/sqlmodel.py
Modified:
mgmt/trunk/rosemary/bin/rosemary-test
mgmt/trunk/rosemary/python/rosemary/model.py
Log:
Add modeling of sql objects and support for generating ddl
Modified: mgmt/trunk/rosemary/bin/rosemary-test
===================================================================
--- mgmt/trunk/rosemary/bin/rosemary-test 2010-02-01 20:42:03 UTC (rev 3835)
+++ mgmt/trunk/rosemary/bin/rosemary-test 2010-02-02 21:43:22 UTC (rev 3836)
@@ -4,10 +4,7 @@
from rosemary.model import *
from rosemary.sql import *
-if __name__ == "__main__":
- model = RosemaryModel()
- model.load_qmf_dir(os.path.join(os.environ["ROSEMARY_HOME"],
"xml"))
-
+def do_model(args):
for package in model.packages:
print "package %s" % package.name
@@ -15,13 +12,11 @@
print " class %s" % cls.name
select = SelectObject(cls)
- print select.emit()
+ #print select.emit()
update = UpdateObject(cls)
- print update.emit()
+ #print update.emit()
- continue
-
for prop in cls.properties:
print " prop %s %s" % (prop.name, prop.title or
"")
@@ -33,3 +28,13 @@
for arg in meth.arguments:
print " arg %s" % arg.name
+
+def do_schema(args):
+ model.sql_model.write_ddl(sys.stdout)
+
+if __name__ == "__main__":
+ model = RosemaryModel()
+ model.load_qmf_dir(os.path.join(os.environ["ROSEMARY_HOME"],
"xml"))
+
+ #do_model(sys.argv)
+ do_schema(sys.argv)
Modified: mgmt/trunk/rosemary/python/rosemary/model.py
===================================================================
--- mgmt/trunk/rosemary/python/rosemary/model.py 2010-02-01 20:42:03 UTC (rev 3835)
+++ mgmt/trunk/rosemary/python/rosemary/model.py 2010-02-02 21:43:22 UTC (rev 3836)
@@ -9,12 +9,15 @@
from elementtree.ElementTree import *
from sql import *
+from sqlmodel import *
class RosemaryModel(object):
def __init__(self):
self.packages = list()
self.packages_by_name = dict()
+ self.sql_model = SqlModel()
+
def load_qmf_dir(self, dir):
assert os.path.isdir(dir)
@@ -46,14 +49,15 @@
class RosemaryPackage(object):
def __init__(self, model, name):
self.model = model
-
self.name = name
+ self.model.packages.append(self)
+ self.model.packages_by_name[self.name] = self
+
self.classes = list()
self.classes_by_name = dict()
- self.model.packages.append(self)
- self.model.packages_by_name[self.name] = self
+ self.sql_schema = SqlSchema(self.model.sql_model, self.name)
def load(self, elem):
for child in elem.findall("class"):
@@ -65,12 +69,17 @@
cls = self.classes_by_name[child.get("name")]
cls.extend(child)
+ def init(self):
+ pass
+
class RosemaryClass(object):
def __init__(self, package, name):
self.package = package
-
self.name = name
+ self.package.classes.append(self)
+ self.package.classes_by_name[self.name] = self
+
self.properties = list()
self.properties_by_name = dict()
@@ -80,9 +89,11 @@
self.methods = list()
self.methods_by_name = dict()
- self.package.classes.append(self)
- self.package.classes_by_name[self.name] = self
+ stats_name = "%s_stats" % self.name
+ self.sql_table = SqlTable(self.package.sql_schema, self.name)
+ self.sql_stats_table = SqlTable(self.package.sql_schema, stats_name)
+
def load(self, elem):
for child in elem.findall("property"):
prop = RosemaryProperty(self, child.get("name"))
@@ -112,8 +123,11 @@
class RosemaryProperty(object):
def __init__(self, cls, name):
self.cls = cls
+ self.name = name
- self.name = name
+ self.cls.properties.append(self)
+ self.cls.properties_by_name[self.name] = self
+
self.title = None
self.type = None
self.references = None
@@ -123,8 +137,7 @@
self.is_optional = None
self.description = None
- self.cls.properties.append(self)
- self.cls.properties_by_name[self.name] = self
+ self.sql_column = SqlColumn(self.cls.sql_table, self.name)
def load(self, elem):
self.type = elem.get("type")
@@ -138,6 +151,9 @@
def extend(self, elem):
self.title = elem.findtext("title")
+ def init(self):
+ pass
+
class RosemaryStatistic(object):
def __init__(self, cls, name):
self.cls = cls
@@ -150,6 +166,8 @@
self.cls.statistics.append(self)
self.cls.statistics_by_name[self.name] = self
+ self.sql_column = SqlColumn(self.cls.sql_stats_table, self.name)
+
def load(self, elem):
self.type = elem.get("type")
self.unit = elem.get("unit")
@@ -158,6 +176,9 @@
def extend(self, elem):
pass
+ def init(self):
+ self.column = SqlColumn(self.cls.stats_table, self.name)
+
class RosemaryMethod(object):
def __init__(self, cls, name):
self.cls = cls
Added: mgmt/trunk/rosemary/python/rosemary/sqlmodel.py
===================================================================
--- mgmt/trunk/rosemary/python/rosemary/sqlmodel.py (rev 0)
+++ mgmt/trunk/rosemary/python/rosemary/sqlmodel.py 2010-02-02 21:43:22 UTC (rev 3836)
@@ -0,0 +1,72 @@
+from util import *
+
+class SqlModel(object):
+ def __init__(self):
+ self.schemas = list()
+ self.schemas_by_name = dict()
+
+ self.relations = list()
+ self.relations_by_name = dict()
+
+ def write_ddl(self, out):
+ for schema in self.schemas:
+ schema.write_ddl(out)
+
+class SqlSchema(object):
+ def __init__(self, model, name):
+ self.model = model
+ self.name = name
+
+ self.model.schemas.append(self)
+ self.model.schemas_by_name[self.name] = self
+
+ self.tables = list()
+ self.tables_by_name = dict()
+
+ def write_ddl(self, out):
+ out.write("create schema \"%s\"\n" % self.name)
+
+ for table in self.tables:
+ if table.columns:
+ table.write_ddl(out)
+
+ out.write(" ;\n")
+
+class SqlTable(object):
+ def __init__(self, schema, name):
+ self.schema = schema
+ self.name = name
+
+ self.schema.tables.append(self)
+ self.schema.tables_by_name[self.name] = self
+
+ self.columns = list()
+ self.columns_by_name = dict()
+
+ def write_ddl(self, out):
+ out.write(" create table \"%s\" (\n" % self.name)
+
+ exprs = list()
+
+ for column in self.columns:
+ exprs.append("\"%s\" text" % column.name)
+
+ out.write(" ")
+ out.write(",\n ".join(exprs))
+ out.write("\n )\n")
+
+class SqlColumn(object):
+ def __init__(self, table, name):
+ self.table = table
+ self.name = name
+
+ self.table.columns.append(self)
+ self.table.columns_by_name[self.name] = self
+
+class SqlRelation(object):
+ def __init__(self, model, name, columns):
+ self.name = name
+ self.columns = columns
+
+ self.model.relations.append(self)
+ self.model.relations_by_name[self.name] = self