• Python SQL говнокод Увидел сегодня в триггере таблицы просто феерический код. У кого-то из коллег явно в роду были индусы.

    if (TD['new']['board'] is None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place is NULL)and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is not None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place is NULL)and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place='%s')and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['place'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is not None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place='%s')and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['place'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is not None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place is NULL)and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is not None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is not None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place is NULL)and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is not None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place='%s')and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['place'], TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

    if (TD['new']['board'] is not None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is not None):
    if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place='%s')and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['place'], TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'
  • Минимум можно сократить до следующего:

    def formatField(field):
    return '(%s %s)' % (field, 'is NULL' if TD['new'][field] == None else "= '%s'" % TD['new'][field])

    if TD['new']['id'] != TD['old']['id']:
    flist = map(formatField, ['board', 'place', 'vendor_parts'])
    text = "SELECT count(*) FROM rma.component_specification where %s and (model_id=%d)", ('and'.join(flist), TD['new']['model_id'])
    if plpy.execute(text)[0]['count']:
    raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'
  • @Skyrzn, Отступы похерились, ну да и так понятно.