Fecha Inicial : 20/04/2018
Fecha Final :  20/04/2019
Hora Inicio: 04:25 PM
Hora Final:  08:40 PM
Horas invertidas(Minutos): 185
Colaboradores: Oscar Cortés

Actividad:
Terminar CRUD de la tabla eventoMantenimiento


create procedure SPR_EventoMantenimiento
@pId int null
as

begin

begin tran
select E.Tabla, E.Antes, E.Despues, E.Fecha
from EventoMantenimiento E
where E.ID = @pId and Visible = 'y'
commit tran

end

go

create procedure SPC_EventoMantenimiento
@pTabla nvarchar(50),
@pEvento nvarchar(50),
@pFecha datetime = null,
@pAntes XML,
@pDespues XML
as

begin

begin tran
insert into EventoMantenimiento (Tabla, Evento, Fecha, Antes, Despues)
values (@pTabla, @pEvento, isnull(@pFecha, GETDATE()), @pAntes, @pDespues)
commit

end

go


create procedure SPD_EventoMantenimiento
@pID int
as

begin
begin try

begin tran
update EventoMantenimiento
set Visible = 'n'
where ID = @pID
commit
end try

begin catch

rollback

end catch
end

go




Terminar script de CRUD 


def createHardcoreCRUDE(script):
    """Crea cuatro funciones por cada tabla, además
    de agregar a la tabla 'eventos' los cambios de cada tabla."""
    print("Hello world")
    
    tempo = ""
    jump = False
    for i in range(len(script)-1):
        if (script[i] != "\t"):
            tempo += script[i]

    
    tablas = []
    tabla = None
    
    for t in tempo.split("\n"):
        if len(t) == 0:
            continue
        
        if (t[-1] == "("):
            tabla = Tabla(t.split(" ")[2][:-1])
            tablas.append(tabla)
            
        elif (t[0] != ")" and tabla != None):
            tt = t.split(" ")
            tabla.agregarDato(tt[0], tt[1])

    for t in tablas:   
        t.toCRUD()
        print("-"*32,"\n\n\n")


class Tabla:
    nombre = ""
    columnas = []
    tipos = []
    parametros = []

    def __init__(self, pNombre):
        self.nombre = pNombre
        self.columnas = []
        self.tipos = []
        self.parametros = []

    def agregarDato(self, pNombre, pTipo):
        self.columnas.append(pNombre)
        intial = pNombre[0].upper()
        self.parametros.append("@p" + intial + pNombre[1:])
        self.tipos.append(pTipo)

    def toString(self):
        print(self.nombre)
        print(self.columnas)
        print(self.tipos)
        print(self.parametros)


    def toCRUD(self):
        #Create
        print("Create Procedure SPC_"+self.nombre)
        print("\t@pID int = null,")
        for i in range(1, len(self.parametros) - 1):
            if (i == len(self.parametros) - 2):
                print("\t"+self.parametros[i] + " " + self.tipos[i])
            else:
                print("\t"+self.parametros[i] + " " + self.tipos[i] + ", ")

        print("""as
begin
begin try
    begin tran

    declare @XmlAntes XML = (select * from """+ self.nombre + """ for XML auto, type)

    """)
        

        insertTable = "\tinsert into " + self.nombre + "("

        for i in range(len(self.parametros) - 1):
            insertTable += self.columnas[i] + ", "
        insertTable = insertTable[:-2] + ")"

        print(insertTable)
        values = "\t\tvalues (isNull(@pId, (select count(isnull(ID,0) + 1) from " + self.nombre + ")),\n\t\t\t"
        
        for i in range(1, len(self.parametros) - 1):
            values += self.parametros[i] + ", "

        values = values[:-2] + ")"
        print(values)

        print("\tdeclare @XmlDespues XML = (select * from " + self.nombre + " for XML auto, type)")
        print("""
        exec SPC_EventoMantenimiento """ + self.nombre + """, 'insertar', null, @XmlAntes, @XmlDespues 
        """)

        print("\tcommit\nend try")

        print("""
begin catch
    rollback
end catch

end
go

""")



        #Delete
        print("Create Procedure SPD_"+self.nombre)
        print("\t@pID int")

        print("""as
begin
begin try
    begin tran

    declare @XmlAntes XML = (select * from """+ self.nombre + """ for XML auto, type)
    """)
        

        print("\tUpdate  " + self.nombre)
        print("\tSet Visible = 'n'")
        print("\tWhere @pId = ID")
        
        print("\n\tdeclare @XmlDespues XML = (select * from " + self.nombre + " for XML auto, type)")
        print("""
        exec SPC_EventoMantenimiento """ + self.nombre + """, 'borrado', null, @XmlAntes, @XmlDespues 
        """)

        print("\tcommit\nend try")

        print("""
begin catch
    rollback
end catch

end
go

""")
        
        #Update
        print("Create Procedure SPU_"+self.nombre)
        print("\t@pID int,")
        for i in range(1, len(self.parametros) - 1):
            if (i == len(self.parametros) - 2):
                print("\t"+self.parametros[i] + " " + self.tipos[i] + " = null")
            else:
                print("\t"+self.parametros[i] + " " + self.tipos[i] + " = null, ")

        print("""as
begin
begin try
    begin tran

    declare @XmlAntes XML = (select * from """+ self.nombre + """ for XML auto, type)
    """)
        

        print("\tUpdate  " + self.nombre)
        
        updates = "\tSet "
        for i in range(1, len(self.parametros) - 1):
            updates += self.columnas[i] + " = isNull(" + self.parametros[i] + ", " + self.columnas[i] + "), "
        print(updates[:-2])
        print("\tWhere @pId = ID")
        
        print("\n\tdeclare @XmlDespues XML = (select * from " + self.nombre + " for XML auto, type)")
        print("""
        exec SPC_EventoMantenimiento """ + self.nombre + """, 'actualizacion', null, @XmlAntes, @XmlDespues 
        """)

        print("\tcommit\nend try")

        print("""
begin catch
    rollback
end catch

end
go


""")
        #Read

        print("Create Procedure SPR_"+self.nombre)
        print("\t@pID int = null")

        print("""as
begin
begin try
    begin tran
    """)
        
        select = "Select "
        for i in range(1, len(self.parametros) - 1):
            select += self.columnas[i] + ", "
        print(select[:-2])
        print("From " + self.nombre)
        print("\tWhere ID = isnull(@pID, ID) and Visible = 'y'")
        

        print("\tcommit\nend try")

        print("""
begin catch
    rollback
end catch

end
go

""")
        



Descanso: 6:23 a 7:33, 70min


Bibliografía:
https://stackoverflow.com/questions/2145879/check-whether-a-table-contains-rows-or-not-sql-server-2005


https://www.w3schools.com/sql/sql_update.asp

Comentarios