hosted by sflogo.jpg

Considering a column as numeric to sort the database

From Bibus

It is sometimes convenient to have a field of numeric in a library. For example, in my library the field "Custom2" contains an integer corresponding to the article position in my desk... Unfortunately bibus fields are all considered as text (except "Id" and "Year" I think) so that when I sort my library by "Custom2" I get:

1;10;2;3;4;5;6;7;8;9

instead of 1;2;3;4;5;6;7;8;9;10

A solution consists in modifying the function "getRefKey" and "__getAllRef" in dbBibBase.py:

 def getRefKey(self,key_id,collist=BIB.LIST_DISPLAY,order=BIB.LIST_ORDER,how=BIB.LIST_HOW):
   """Return the references corresponding to the key with key_id"""
   try:
     if order=="Custom2":
       #sort as a numeric
       tmpstr = """SELECT %s FROM %s as t1,%s as t2 WHERE t1.%s=t2.%s AND t2.%s=%s ORDER BY %s+0 %s""" %('Id,' + ','.join(collist),self.tableRef,self.tableLink,'Id','ref_id','key_id',self.param,order,how)
     else:
       tmpstr = """SELECT %s FROM %s as t1,%s as t2 WHERE t1.%s=t2.%s AND t2.%s=%s ORDER BY %s %s""" %('Id,' + ','.join(collist),self.tableRef,self.tableLink,'Id','ref_id','key_id',self.param,order,how)
     self.dbCursor.execute(tmpstr, (key_id,))
     return self.dbCursor.fetchall()
   except Error,errorType:
     self.showError("dbBibBase.getRefKey " + `errorType.args`)
     return None

 def __getAllRef(self,table,collist,order,how=BIB.LIST_HOW):
   try:
     if order=="Custom2":
       #sort as a numeric
       self.dbCursor.execute("""SELECT %s FROM %s ORDER BY %s +0 %s"""% (','.join(('Id',)+collist),table,order,how))  # column Id added for SetData of refList
     else:
       self.dbCursor.execute("""SELECT %s FROM %s ORDER BY %s %s"""% (','.join(('Id',)+collist),table,order,how))  # column Id added for SetData of refList  
     return self.dbCursor.fetchall()
   except Error,errorType:
     self.showError("dbBibBase.__getAllRef " + `errorType.args`)
     return None

Personal tools