CDS-Invenio: List DELETED RECORDS from collection name – python script

Some time ago I posted some cds invenio mysql useful queries. Some people wrote some days ago asking wether I could put this into a function. Here you are ;)

List all deleted records which belong(ed) to collection ‘colname’ (tested with Invenio 0.99.x)

def listDeletedFromCollname(colname, coltag='980__c'):
  from invenio.dbquery import run_sql
  query = """ SELECT distinct bibrec_bib98x.id_bibrec 
              FROM bib98x, bibrec_bib98x
              WHERE bib98x.tag='%s' AND bib98x.value = '%s' AND bib98x.id = bibrec_bib98x.id_bibxxx       
                    AND bibrec_bib98x.id_bibrec IN (SELECT DISTINCT bibrec_bib98x.id_bibrec 
                                      FROM bibrec_bib98x, bib98x 
                                      WHERE bibrec_bib98x.id_bibxxx=bib98x.id AND
                                            bib98x.tag = '980__c' AND
                                            bib98x.value = 'DELETED') """ %(coltag,colname)
  res = run_sql(query)
  deletedlist = []
  for i in res:
    deletedlist.append(i[0])
  return deletedlist

And it is used like:

# list deleted records from a collecion defined by the query '980__c:FH'
a = listDeletedFromCollname('FH')
print a
 
# if I have my collection defined in another way, for instance using this query: '980__a:TAZ'
a = listDeletedFromCollname('TAZ','980__a')
print a

In a similar fashion we can list the ones that have NOT been deleted, as in:

def listNotDeletedFromCollname(colname, coltag='980__c'):
  from invenio.dbquery import run_sql
  query = """ SELECT distinct bibrec_bib98x.id_bibrec 
              FROM bib98x, bibrec_bib98x
              WHERE bib98x.tag='%s' AND bib98x.value = '%s' AND bib98x.id = bibrec_bib98x.id_bibxxx       
                    AND bibrec_bib98x.id_bibrec NOT IN (SELECT DISTINCT bibrec_bib98x.id_bibrec 
                                      FROM bibrec_bib98x, bib98x 
                                      WHERE bibrec_bib98x.id_bibxxx=bib98x.id AND
                                            bib98x.tag = '980__c' AND
                                            bib98x.value = 'DELETED') """ %(coltag,colname)
  res = run_sql(query)
  notdeletedlist = []
  for i in res:
    notdeletedlist.append(i[0])
  return deletedlist

Hope it’s useful :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation