Guest User

Untitled

a guest
May 29th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. use FUBER
  2. go
  3.  
  4. alter function dbo.getChainID (@username nvarchar(50))
  5. returns int
  6. as
  7. begin
  8. declare @result int
  9. select top 1 @result = c.idC
  10. from Chaining c
  11. where c.username=@username
  12. return @result
  13. end
  14.  
  15. alter proc dbo.addDriver @username nvarchar(50), @password nvarchar(50), @name nvarchar(50), @email nvarchar(50), @phoneNo nvarchar(20), @carType int
  16. as
  17. set nocount on
  18. begin tran
  19. begin try
  20. insert into Login values (@username,@password)
  21. insert into Chaining(username) values (@username)
  22. declare @chainingID int
  23. set @chainingID = dbo.getChainID(@username)
  24. insert into Driver(idD,name,email,phoneNo,carType) values (@chainingID,@name,@email,@phoneNo,@carType)
  25. commit tran
  26. end try
  27. begin catch
  28. rollback tran
  29. raiserror('Driver already exists',16,1)
  30. end catch
  31. go
  32.  
  33. alter proc dbo.addClient @username nvarchar(50), @password nvarchar(50), @name nvarchar(50), @email nvarchar(50), @phoneNo nvarchar(20), @isPro bit
  34. as
  35. set nocount on
  36. begin tran
  37. begin try
  38. insert into Login values (@username,@password)
  39. insert into Chaining(username) values (@username)
  40. declare @chainingID int
  41. set @chainingID = dbo.getChainID(@username)
  42. insert into Client(idC,name,email,phoneNo,isPro) values (@chainingID,@name,@email,@phoneNo,@isPro)
  43. commit tran
  44. end try
  45. begin catch
  46. rollback tran
  47. raiserror('Client already exists',16,1)
  48. end catch
  49. go
  50.  
  51.  
  52. create function dbo.loginUser (@username nvarchar(50), @password nvarchar(50))
  53. returns int
  54. as
  55. begin
  56. if (exists(select * from Login l where l.username=@username and l.password=@password))
  57. return 2
  58. if (exists(select * from Login l where l.username=@username))
  59. return 1
  60. return 0
  61. end
  62.  
  63.  
  64. alter function dbo.getDriver (@username nvarchar(50))
  65. returns table
  66. as
  67. return select d.name, d.email, d.phoneNo, d.carType
  68. from Chaining c inner join Driver d on c.idC=d.idD
  69. where c.username=@username
  70. go
  71.  
  72. create function dbo.getDriverWithName (@name nvarchar(50))
  73. returns table
  74. as
  75. return select d.email, d.phoneNo, d.carType
  76. from Driver d
  77. where d.name=@name
  78. go
  79. create function dbo.getClient (@username nvarchar(50))
  80. returns table
  81. as
  82. return select cl.name, cl.email, cl.phoneNo, cl.isPro
  83. from Chaining ch inner join Client cl on ch.idC=cl.idC
  84. where ch.username=@username
  85.  
  86. alter function dbo.isDriver (@username nvarchar(50))
  87. returns int
  88. as
  89. begin
  90. if (exists(select d.idD from Chaining c inner join Driver d on c.idC=d.idD where c.username=@username))
  91. return 1
  92. return 0
  93. end
  94.  
  95. create function dbo.isClient (@username nvarchar(50))
  96. returns int
  97. as
  98. begin
  99. if (exists(select cl.idC from Chaining c inner join Client cl on c.idC=cl.idC where c.username=@username))
  100. return 1
  101. return 0
  102. end
  103.  
  104. drop function dbo.loginDriver
  105.  
  106. exec dbo.addDriver 'username','password','name',1,'1234567890'
  107. exec dbo.addDriver 'usernam1e','password','name',1,'1234567890'
  108. exec dbo.addClient 'username','password','name','tudor@myemail.com','1345678',0
  109.  
  110. print(dbo.loginDriver('username','password'))
  111. drop procedure dbo.addUser
  112. delete from Login
  113. delete from Chaining
  114. delete from Driver
  115. delete from Client
  116. delete from
  117. select * from Login
  118. select * from Chaining
  119. select * from Driver
  120. select * from Client
  121.  
  122. print(dbo.getChainID('username'))
  123. print(dbo.isDriver('username'))
  124. print(dbo.isDriver('Pro5'))
  125. print(dbo.isDriver('tudor'))
  126. print(dbo.isDriver('balastudor'))
  127. print(dbo.isClientPart('Pro2'))
Add Comment
Please, Sign In to add comment