+def get_or_set_keyring_id (keyring):
+ """
+ If keyring does not have an entry in the keyring table yet, create one
+ and return its id.
+ If keyring already has an entry, simply return its id.
+
+ Result is cached.
+
+ """
+ global keyring_id_cache
+
+ if keyring_id_cache.has_key(keyring):
+ return keyring_id_cache[keyring]
+
+ q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
+ if not q.getresult():
+ projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
+ q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
+ keyring_id = q.getresult()[0][0]
+ keyring_id_cache[keyring] = keyring_id
+
+ return keyring_id
+
+################################################################################
+