Skip to content

Commit

Permalink
added sql scripts for seeding data, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyBorkovich committed May 24, 2024
1 parent f7d104d commit 340903b
Show file tree
Hide file tree
Showing 9 changed files with 42,161 additions and 0 deletions.
20,001 changes: 20,001 additions & 0 deletions scripts/BusesSeed.sql

Large diffs are not rendered by default.

1,003 changes: 1,003 additions & 0 deletions scripts/CompaniesSeed.sql

Large diffs are not rendered by default.

10,001 changes: 10,001 additions & 0 deletions scripts/DriversSeed.sql

Large diffs are not rendered by default.

10,001 changes: 10,001 additions & 0 deletions scripts/PassengerSeed.sql

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions scripts/QueryObjects.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
-- stored procedures
SELECT *
FROM BusStationDB.INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_NAME NOT LIKE 'sp%'
-- functions
SELECT *
FROM BusStationDB.INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'FUNCTION'
AND ROUTINE_NAME NOT LIKE 'fn%'

-- triggers
SELECT
sysobjects.name AS trigger_name
,USER_NAME(sysobjects.uid) AS trigger_owner
,s.name AS table_schema
,OBJECT_NAME(parent_obj) AS table_name
,OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS isupdate
,OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS isdelete
,OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS isinsert
,OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS isafter
,OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS isinsteadof
,OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS [disabled]
FROM sysobjects

INNER JOIN sysusers
ON sysobjects.uid = sysusers.uid

INNER JOIN sys.tables t
ON sysobjects.parent_obj = t.object_id

INNER JOIN sys.schemas s
ON t.schema_id = s.schema_id

WHERE sysobjects.type = 'TR'


-- indexes
SELECT '[' + s.NAME + '].[' + so.NAME + ']' AS 'table_name'
,+ i.NAME AS 'index_name'
,CASE
WHEN i.is_disabled = 1
THEN '<disabled> '
ELSE ''
END + LOWER(i.type_desc) + CASE
WHEN i.is_unique = 1
THEN ', unique' + CASE
WHEN i.is_unique_constraint = 1
THEN ' (constraint)'
ELSE '' END
ELSE ''
END + CASE
WHEN i.is_primary_key = 1
THEN ', primary key'
ELSE ''
END AS 'index_description'
,STUFF((
SELECT ', [' + sc.name + ']' AS "text()"
FROM sys.columns AS sc
INNER JOIN sys.index_columns AS ic ON ic.object_id = sc.object_id
AND ic.column_id = sc.column_id
WHERE sc.object_id = so.object_id
AND ic.index_id = i.index_id
AND ic.is_included_column = 0
ORDER BY key_ordinal
FOR XML PATH('')
), 1, 2, '') AS 'indexed_columns'
,STUFF((
SELECT ', [' + sc.name + ']' AS "text()"
FROM sys.columns AS sc
INNER JOIN sys.index_columns AS ic ON ic.object_id = sc.object_id AND ic.column_id = sc.column_id
WHERE sc.object_id = so.object_id
AND ic.index_id = i.index_id
AND ic.is_included_column = 1
FOR XML PATH('')
), 1, 2, '') AS 'included_columns'
FROM sys.indexes AS i
INNER JOIN sys.objects AS so ON so.object_id = i.object_id
AND so.is_ms_shipped = 0 -- Exclude objects created by internal component
INNER JOIN sys.schemas AS s ON s.schema_id = so.schema_id
WHERE so.type = 'U'
AND i.auto_created = 0 -- Don't show auto-created IXs
--AND i.is_unique_constraint = 0 -- Enable to exclude UQ constaint IXs
AND i.type <> 0 -- Exclude heaps
AND so.NAME <> 'sysdiagrams'
AND so.NAME <> '__EFMigrationsHistory'
ORDER BY 'table_name',
'index_name';
856 changes: 856 additions & 0 deletions scripts/RoutesSeed.sql

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions scripts/Statistic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
select top 10 c.Name as CompanyName, SUM(tk.Price) as TotalRevenue from
Companies As c
join Drivers as d on c.CompanyId = d.CompanyId
join Buses AS b on b.DriverId = d.DriverId
join BusScheduleEntries as bse on b.BusId = bse.BusId
join Trips as t on t.BusScheduleEntryId = bse.BusScheduleEntryId
join TicketPayments as tp on t.TripId = tp.TripId
join Tickets as tk on tp.TicketId = tk.TicketId
group by c.Name
order by TotalRevenue desc


select top 10
r.RouteId,
(r.Origin + ' - ' + r.Destination) RouteName,
ROUND(AVG(b.Rating), 2) as AvgBusRating
from Routes as r
join BusScheduleEntries as bse on r.RouteId = bse.RouteId
join BusDetailsView as b on b.BusId = bse.BusId
where Rating > 0.0
group by r.RouteId, r.Origin, r.Destination
order by AvgBusRating desc


select top 10
r.RouteId,
(r.Origin + ' - ' + r.Destination) RouteName,
SUM(tk.Price) as TotalRevenue
from Routes as r
join BusScheduleEntries as bse on r.RouteId = bse.RouteId
join Trips as t on t.BusScheduleEntryId = bse.BusScheduleEntryId
join TicketPayments as tp on t.TripId = tp.TripId
join Tickets as tk on tp.TicketId = tk.TicketId
group by r.RouteId, r.Origin, r.Destination
order by TotalRevenue desc

-- the most crossed distance among buses
select b.Number, SUM(r.OverallDistance) as DistanceTravelled
from Buses as b
join BusScheduleEntries as bse on b.BusId = bse.BusId
join Routes as r on r.RouteId = bse.RouteId
join Trips as t on t.BusScheduleEntryId = bse.BusScheduleEntryId
where t.Status != 'Canceled'
group by b.BusId
order by DistanceTravelled desc


WITH RankedDistances AS (
SELECT
b.BusId,
r.OverallDistance AS DistanceTravelled,
ROW_NUMBER() OVER (PARTITION BY r.OverallDistance ORDER BY b.BusId) AS RowNumber
FROM
Buses AS b
JOIN
BusScheduleEntries AS bse ON b.BusId = bse.BusId
JOIN
Routes AS r ON r.RouteId = bse.RouteId
JOIN
Trips AS t ON t.BusScheduleEntryId = bse.BusScheduleEntryId
WHERE
t.Status != 'Canceled'
)
SELECT
BusId,
DistanceTravelled
FROM
RankedDistances
WHERE
RowNumber = 1
ORDER BY
DistanceTravelled DESC;

--var rankedDistances = from b in dbContext.Buses
-- join bse in dbContext.BusScheduleEntries on b.BusId equals bse.BusId
-- join r in dbContext.Routes on bse.RouteId equals r.RouteId
-- join t in dbContext.Trips on bse.BusScheduleEntryId equals t.BusScheduleEntryId
-- where t.Status != "Canceled"
-- orderby r.OverallDistance descending, b.BusId
-- select new
-- {
-- b.BusId,
-- DistanceTravelled = r.OverallDistance
-- };

--var firstDistances = rankedDistances.GroupBy(rd => rd.DistanceTravelled)
-- .Select(g => g.First());

--var query = from fd in firstDistances
-- orderby fd.DistanceTravelled descending
-- select fd;
120 changes: 120 additions & 0 deletions scripts/authSP.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@


CREATE PROCEDURE AllowSwitchContextForOtherUsers
(@Username NVARCHAR(50))
AS
BEGIN
-- temporary table
CREATE TABLE #TempUsers
(PrincipalName NVARCHAR(100),RoleName NVARCHAR(100));

-- cte
WITH PrincipalsWithRoles AS (SELECT *
FROM (SELECT P.name AS PrincipalName,R.role_principal_id AS GH
FROM SYS.database_principals P,sys.database_role_members R
WHERE P.principal_id=R.member_principal_id OR P.principal_id=R.role_principal_id
AND type<>'R') S INNER JOIN (SELECT P.name AS RoleName,P.principal_id AS GHA
FROM SYS.database_principals P,sys.database_role_members R
WHERE P.principal_id=R.member_principal_id OR P.principal_id=R.role_principal_id
AND type='R') D
ON D.GHA=S.GH AND PrincipalName != 'dbo' AND PrincipalName != @Username)

INSERT INTO #TempUsers
SELECT DISTINCT PrincipalName,RoleName
FROM PrincipalsWithRoles

DECLARE @CurrentUser NVARCHAR(100), @QueryText NVARCHAR(MAX)=''
DECLARE UserCursor CURSOR FOR SELECT PrincipalName FROM #TempUsers
OPEN UserCursor
FETCH NEXT FROM UserCursor INTO @CurrentUser
WHILE(@@FETCH_STATUS = 0)
BEGIN
SET @QueryText += 'GRANT IMPERSONATE ON USER::[' + @CurrentUser + '] TO ' + @Username + '; '
FETCH NEXT FROM UserCursor INTO @CurrentUser
END
CLOSE UserCursor
DEALLOCATE UserCursor

DROP TABLE #TempUsers
EXEC(@QueryText)
END


CREATE PROCEDURE CreateUserWithRole
( @Username NVARCHAR(50),
@Password NVARCHAR(50),
@RoleName INT)
AS
BEGIN
DECLARE @LoginName NVARCHAR(50) = @Username + 'Login';
DECLARE @LoginCommand NVARCHAR(100) = 'CREATE LOGIN '+ @LoginName + ' WITH PASSWORD = '''+ @Password +'''';
EXEC(@LoginCommand)

DECLARE @UserCommand NVARCHAR(100) = 'CREATE USER ' + @Username + ' FOR LOGIN ' + @LoginName;
EXEC(@UserCommand)

IF @RoleName = 100 -- admin
BEGIN
EXEC sp_addrolemember 'db_owner', @Username
END
IF @RoleName = 200
BEGIN
EXEC sp_addrolemember 'Passenger', @Username
END
IF @RoleName = 300
BEGIN
EXEC sp_addrolemember 'Driver', @Username
END

EXEC AllowSwitchContextForOtherUsers @Username;
END
GO

DROP PROCEDURE IF EXISTS AllowSwitchContextForOtherUsers;
DROP PROCEDURE IF EXISTS SeedPermissionsForRoles;
DROP PROCEDURE IF EXISTS CreateUserWithRole;
EXEC SeedPermissionsForRoles;

EXEC CreateUserWithRole 'Pedro', 'pedro1234', 200

EXEC AllowSwitchContextForOtherUsers 'AndriiAdmin'
CREATE PROCEDURE SeedPermissionsForRoles
AS
BEGIN
CREATE ROLE Passenger;
GRANT SELECT ON Buses TO Passenger;
GRANT SELECT ON Routes TO Passenger;
GRANT SELECT ON Stops TO Passenger;
GRANT SELECT ON Trips TO Passenger;
GRANT SELECT ON TripStatuses TO Passenger;
GRANT SELECT ON BusDetailsView TO Passenger;
GRANT SELECT, INSERT, UPDATE, DELETE ON BusReviews TO Passenger;
GRANT SELECT, UPDATE ON Seats TO Passenger;
GRANT SELECT, UPDATE, INSERT, DELETE ON Tickets TO Passenger;
GRANT SELECT, UPDATE, INSERT, DELETE ON TicketStatuses TO Passenger;
GRANT SELECT, UPDATE, INSERT ON TicketPayments TO Passenger;

CREATE ROLE Driver;
GRANT SELECT, UPDATE ON Buses TO Driver;
GRANT SELECT, UPDATE ON Stops TO Driver;
GRANT SELECT ON Tickets TO Driver;
GRANT SELECT ON Companies TO Driver;
GRANT SELECT, UPDATE, INSERT, DELETE ON BusScheduleEntries TO Driver;
GRANT SELECT ON BusDetailsView TO Driver;
GRANT SELECT ON BusReviews TO Driver;
GRANT SELECT ON Passengers TO Driver;
GRANT SELECT, DELETE ON Seats TO Driver;
GRANT SELECT, UPDATE, INSERT, DELETE ON Trips TO Driver;
GRANT SELECT, UPDATE, INSERT ON TripStatuses TO Driver;
GRANT SELECT ON TicketPayments TO Driver;
END


REVERT
EXECUTE AS USER = 'Pedro'

select * from Users


DROP ROLE Passenger;
DROP ROLE Driver
Binary file added scripts/bsms-create.sql
Binary file not shown.

0 comments on commit 340903b

Please sign in to comment.