Author: justi9
Date: 2010-07-15 16:13:47 -0400 (Thu, 15 Jul 2010)
New Revision: 4120
Modified:
mgmt/newdata/cumin/model/cumin.xml
mgmt/newdata/cumin/model/rosemary.xml
mgmt/newdata/rosemary/bin/rosemary-model
mgmt/newdata/rosemary/python/rosemary/model.py
mgmt/newdata/rosemary/python/rosemary/sqlmodel.py
Log:
Add support for extra indexes and unique constraints
Modified: mgmt/newdata/cumin/model/cumin.xml
===================================================================
--- mgmt/newdata/cumin/model/cumin.xml 2010-07-14 17:17:03 UTC (rev 4119)
+++ mgmt/newdata/cumin/model/cumin.xml 2010-07-15 20:13:47 UTC (rev 4120)
@@ -12,7 +12,7 @@
<class name="User">
<property name="name" type="sstr" index="y"/>
- <property name="password" type="sstr"
index="y"/>
+ <property name="password" type="sstr"/>
</class>
<class name="Role">
Modified: mgmt/newdata/cumin/model/rosemary.xml
===================================================================
--- mgmt/newdata/cumin/model/rosemary.xml 2010-07-14 17:17:03 UTC (rev 4119)
+++ mgmt/newdata/cumin/model/rosemary.xml 2010-07-15 20:13:47 UTC (rev 4120)
@@ -1,4 +1,10 @@
<model>
+ <package name="com.redhat.cumin">
+ <class name="User">
+ <property name="name" unique="y"/>
+ </class>
+ </package>
+
<package name="com.redhat.cumin.grid">
<class name="JobSummary">
<property name="GlobalJobId">
@@ -12,7 +18,6 @@
<property name="JobStatus">
<title>Job Status</title>
</property>
-
</class>
</package>
@@ -265,6 +270,9 @@
</class>
<class name="Slot">
+ <index name="Pool" attributes="Pool"/>
+ <index name="Name" attributes="Name"/>
+
<property name="JobId">
<title>Job ID</title>
</property>
Modified: mgmt/newdata/rosemary/bin/rosemary-model
===================================================================
--- mgmt/newdata/rosemary/bin/rosemary-model 2010-07-14 17:17:03 UTC (rev 4119)
+++ mgmt/newdata/rosemary/bin/rosemary-model 2010-07-15 20:13:47 UTC (rev 4120)
@@ -28,6 +28,9 @@
for arg in meth.arguments:
print " arg %s" % arg.name
+ for idx in cls._indexes:
+ print " idx %s" % idx.name
+
def do_ddl(args):
model.sql_model.write_drop_ddl(sys.stdout)
model.sql_model.write_create_ddl(sys.stdout)
Modified: mgmt/newdata/rosemary/python/rosemary/model.py
===================================================================
--- mgmt/newdata/rosemary/python/rosemary/model.py 2010-07-14 17:17:03 UTC (rev 4119)
+++ mgmt/newdata/rosemary/python/rosemary/model.py 2010-07-15 20:13:47 UTC (rev 4120)
@@ -155,6 +155,9 @@
self._methods = list()
self._methods_by_name = dict()
+ self._indexes = list()
+ self._indexes_by_name = dict()
+
self.add_sql()
self._id = RosemaryAttribute(self, "_id")
@@ -298,6 +301,10 @@
meth = self._methods_by_name[child.get("name")]
meth.extend(child)
+ for child in elem.findall("index"):
+ idx = RosemaryIndex(self, child.get("name"))
+ idx.extend(child)
+
def init(self):
log.debug("Initializing %s", self)
@@ -319,6 +326,9 @@
for meth in self._methods:
meth.init()
+ for idx in self._indexes:
+ idx.init()
+
self.add_constraints()
def get_object(self, cursor, **criteria):
@@ -495,6 +505,7 @@
self.unit = None
self.index = None
self.optional = None
+ self.unique = None
self.title = None
self.description = None
@@ -512,6 +523,7 @@
def extend(self, elem):
self.title = elem.findtext("title")
+ self.unique = elem.get("unique", "n") == "y" and
True
def init(self):
if not self.title:
@@ -598,6 +610,10 @@
self.sql_column = SqlColumn(self.cls.sql_table, self.name, type)
self.sql_column.nullable = self.optional
+ if self.unique:
+ name = "%s_%s_uq" % (self.cls._name, self.name)
+ SqlUniqueConstraint(self.cls.sql_table, name, (self.sql_column,))
+
class RosemaryStatistic(RosemaryAttribute):
def __init__(self, cls, name):
super(RosemaryStatistic, self).__init__(cls, name)
@@ -668,6 +684,38 @@
def init(self):
pass
+class RosemaryIndex(object):
+ def __init__(self, cls, name):
+ self.cls = cls
+ self.name = name
+
+ self.cls._indexes.append(self)
+ self.cls._indexes_by_name[self.name] = self
+
+ self.attributes = list()
+
+ self.sql_index = None
+
+ def extend(self, elem):
+ names = elem.get("attributes")
+
+ for name in names.split():
+ attr = getattr(self.cls, name)
+ self.attributes.append(attr)
+
+ def init(self):
+ log.debug("Initializing %s", self)
+
+ schema = self.cls._package.sql_schema
+ name = "%s_%s_index" % (self.cls._name, self.name)
+ columns = [x.sql_column for x in self.attributes]
+
+ self.sql_index = SqlIndex(schema, name, columns)
+
+ def __repr__(self):
+ args = (self.__class__.__name__, self.name)
+ return "%s(%s)" % args
+
class RosemaryObject(object):
def __init__(self, cls, id):
for column in cls.sql_table._columns:
Modified: mgmt/newdata/rosemary/python/rosemary/sqlmodel.py
===================================================================
--- mgmt/newdata/rosemary/python/rosemary/sqlmodel.py 2010-07-14 17:17:03 UTC (rev 4119)
+++ mgmt/newdata/rosemary/python/rosemary/sqlmodel.py 2010-07-15 20:13:47 UTC (rev 4120)
@@ -237,9 +237,9 @@
def write_create_ddl(self, out):
cols = ", ".join(["\"%s\"" % x.name for x in
self.columns])
- args = (self.name, self.columns[0].table.name, cols)
+ args = (self.name, self.columns[0].table._name, cols)
- out.write(" create index \"%s\" on \"%s\"(%s)\n"
% args)
+ out.write(" create index \"%s\" on \"%s\"
(%s)\n" % args)
class SqlView(object):
def __init__(self, schema, name, query):