-- Copyright (c) 2015 gutlib.com - All rights reserved. Terms are in gutlib.sql
-- You are NOT ALLOWED to modify and/or use this code without author permission

if object_id('g.sp') is null
   exec('create procedure g.sp as
      RAISERROR (''g.sp.err.1: not implemented'' ,16 ,1) WITH NOWAIT')
go
alter procedure g.sp
    @name sysname
   ,@body nvarchar(max)=null
   ,@drop bit=null
as
declare @cmd nvarchar(max)
if @drop is null set @drop=0
set @name = replace(@name,'''','''''')

if @drop = 1
   set @cmd = 'drop procedure ' + @name
else
   set @cmd = 'create procedure ' + @name + '
      as 
         RAISERROR (''g.sp.err.2:' + @name + '.err.1: not implemented'' ,16 ,1) WITH NOWAIT
      '
if not exists(select 1 from g.obj where name = @name) begin
   if @drop = 0
      exec(@cmd)
end else
   if @drop = 1
      exec(@cmd)

if @body is not null and @drop=0
   exec(@body) -- @body must have alter function statement


go ------------------------------- unit tests ---------------------------------
exec g.sp 'g.unit_test'
if not exists(select 1 from g.obj where name='g.unit_test')
   RAISERROR ('g.sp.err.3' ,16 ,1)

exec g.sp 'g.unit_test','
   alter procedure g.unit_test as select z=1
'
exec('
   select z=0 into #ret
   insert into #ret exec g.unit_test
   if (select top 1 z from #ret where z=1) != 1
      RAISERROR (''g.sp.err.4'' ,16 ,1) WITH NOWAIT
')
begin try
   exec('exec g.sp ''g.unit_test'',@drop=1')
end try begin catch
   RAISERROR ('g.sp.err.5: drop failed' ,16 ,1)
end catch
