Commits

Walter Dörwald committed 6d2269e

Add orasql.ForeignKey.itercolumns().

  • Participants
  • Parent commits d7cf9fd

Comments (0)

Files changed (3)

 Changes in 3.12 (released ??/??/2010)
 -------------------------------------
 
+*	:class:`ll.orasql.ForeignKey` has a new method :meth:`itercolumns` for
+	iterating over the columns the foreign key consists of.
+
 *	A new class :mod:`ll.misc.SysInfo` has been added that provides
 	host/user/python/script information. :mod:`ll.sisyphus` uses this new
 	class.

src/ll/orasql/__init__.py

 		if rec2 is None:
 			raise SQLObjectNotFoundError(self)
 		tablename = getfullname(rec2.table_name, rec2.owner)
-		cursor.execute("select column_name from all_cons_columns where owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name)
+		cursor.execute("select column_name from all_cons_columns where owner=nvl(:owner, user) and constraint_name=:name order by position", owner=self.owner, name=self.name)
 		for r in cursor:
 			yield Column(u"{}.{}".format(tablename, r.column_name))
 
 		rec2 = cursor.fetchone()
 		if rec2 is None:
 			raise SQLObjectNotFoundError(self)
-		cursor.execute("select column_name from all_cons_columns where owner=nvl(:owner, user) and constraint_name=:name", owner=self.owner, name=self.name)
+		cursor.execute("select column_name from all_cons_columns where owner=nvl(:owner, user) and constraint_name=:name order by position", owner=self.owner, name=self.name)
 		tablename = getfullname(rec2.table_name, rec2.owner)
 		pkname = getfullname(self.name, None)
 		code = u"alter table {} add constraint {} primary key({})".format(tablename, pkname, ", ".join(r.column_name for r in cursor))
 		rec = cursor.fetchone()
 		return PrimaryKey(rec.r_constraint_name, rec.r_owner, connection)
 
+	def itercolumns(self, connection=None):
+		"""
+		Return an iterator over the columns this foreign key consists of.
+		"""
+		(connection, cursor) = self.getcursor(connection)
+		cursor.execute("select decode(owner, user, null, owner) as owner, table_name, column_name from all_cons_columns where constraint_name=:name and owner=nvl(:owner, user) order by position", owner=self.owner, name=self.name)
+		for r in cursor:
+			yield Column(u"{}.{}".format(r.table_name, r.column_name), r.owner)
+
 	def isenabled(self, connection=None):
 		"""
 		Return whether this constraint is enabled.

test/test_orasql.py

 			obj.table()
 			if isinstance(obj, orasql.ForeignKey):
 				obj.pk()
+				list(obj.itercolumns())
 
 
 @py.test.mark.db