#!/usr/bin/env python
# 
# Copyright 2009 Mark Fiers, Plant & Food Research
# 
# This file is part of Moa - http://github.com/mfiers/Moa
# 
# Moa is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version.
# 
# Moa is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Moa.  If not, see <http://www.gnu.org/licenses/>.
# 
import MySQLdb
import sys

con = MySQLdb.connect(user=sys.argv[1], db=sys.argv[2])

typeid=int(sys.argv[3])
sql = [
    """DELETE FROM feature
        WHERE typeid = %d
        """ % typeid,
    """DELETE name
         FROM name
         LEFT JOIN feature
           ON name.id = feature.id
        WHERE feature.id IS NULL
        """,
    """DELETE attribute
         FROM attribute
         LEFT JOIN feature
           ON attribute.id = feature.id
        WHERE feature.id IS NULL
        """,
    """DELETE parent2child
         FROM parent2child
         LEFT JOIN feature
           ON parent2child.id = feature.id
        WHERE feature.id IS NULL;"""
    """DELETE parent2child
         FROM parent2child
         LEFT JOIN feature
           ON parent2child.child = feature.id
        WHERE feature.id IS NULL;""" ]

c = con.cursor()
for s in sql:
    print s
    c.execute(s)
