-- 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.vw') is null
   exec('create procedure g.vw as
      RAISERROR (''g.vw.err.1: not implemented'' ,16 ,1) WITH NOWAIT')
go
alter procedure g.vw
    @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 view ' + @name
else
   set @cmd = 'create view ' + @name + ' as select err = cast(''g.vw.err.2.' + @name + '.err.1: not implemented.'' as int)'

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 begin
   set @body = 'alter view ' + @name + ' as 
      ' + @body
   exec(@body) -- @body must have select statement
end


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

exec g.vw 'g.test_vw','
   select * from g.obj
'
exec('
if not exists(select 1 from g.test_vw where name = ''g.test_vw'')
   RAISERROR (''g.vw.err.4'' ,16 ,1) WITH NOWAIT
')
exec('exec g.vw ''g.test_vw'',@drop=1')
