Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

511 lines
13 KiB

  1. /* First uninstall - this section is exactly the same as uninstall.sql */
  2. USE master
  3. GO
  4. /* Drop the database containing our sprocs */
  5. IF DB_ID('ASPState') IS NOT NULL BEGIN
  6. DROP DATABASE ASPState
  7. END
  8. GO
  9. /* Drop temporary tables */
  10. IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE name = 'ASPStateTempSessions' AND type = 'U') BEGIN
  11. DROP TABLE tempdb..ASPStateTempSessions
  12. END
  13. GO
  14. IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE name = 'ASPStateTempApplications' AND type = 'U') BEGIN
  15. DROP TABLE tempdb..ASPStateTempApplications
  16. END
  17. GO
  18. /* Drop the startup procedure */
  19. DECLARE @PROCID int
  20. SET @PROCID = OBJECT_ID('ASPState_Startup')
  21. IF @PROCID IS NOT NULL AND OBJECTPROPERTY(@PROCID, 'IsProcedure') = 1 BEGIN
  22. DROP PROCEDURE ASPState_Startup
  23. END
  24. GO
  25. /* Drop the obsolete startup enabler */
  26. DECLARE @PROCID int
  27. SET @PROCID = OBJECT_ID('EnableASPStateStartup')
  28. IF @PROCID IS NOT NULL AND OBJECTPROPERTY(@PROCID, 'IsProcedure') = 1 BEGIN
  29. DROP PROCEDURE EnableASPStateStartup
  30. END
  31. GO
  32. /* Drop the obsolete startup disabler */
  33. DECLARE @PROCID int
  34. SET @PROCID = OBJECT_ID('DisableASPStateStartup')
  35. IF @PROCID IS NOT NULL AND OBJECTPROPERTY(@PROCID, 'IsProcedure') = 1 BEGIN
  36. DROP PROCEDURE DisableASPStateStartup
  37. END
  38. GO
  39. /* Drop the ASPState_DeleteExpiredSessions_Job */
  40. DECLARE @JobID BINARY(16)
  41. SELECT @JobID = job_id
  42. FROM msdb.dbo.sysjobs
  43. WHERE (name = N'ASPState_Job_DeleteExpiredSessions')
  44. IF (@JobID IS NOT NULL)
  45. BEGIN
  46. -- Check if the job is a multi-server job
  47. IF (EXISTS (SELECT *
  48. FROM msdb.dbo.sysjobservers
  49. WHERE (job_id = @JobID) AND (server_id <> 0)))
  50. BEGIN
  51. -- There is, so abort the script
  52. RAISERROR (N'Unable to import job ''ASPState_Job_DeleteExpiredSessions'' since there is already a multi-server job with this name.', 16, 1)
  53. END
  54. ELSE
  55. -- Delete the [local] job
  56. EXECUTE msdb.dbo.sp_delete_job @job_name = N'ASPState_Job_DeleteExpiredSessions'
  57. END
  58. USE master
  59. GO
  60. /* Create and populate the ASPState database */
  61. CREATE DATABASE ASPState
  62. GO
  63. USE ASPstate
  64. GO
  65. SET QUOTED_IDENTIFIER OFF
  66. GO
  67. SET ANSI_NULLS ON
  68. GO
  69. CREATE PROCEDURE DropTempTables
  70. AS
  71. IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE name = 'ASPStateTempSessions' AND type = 'U') BEGIN
  72. DROP TABLE tempdb..ASPStateTempSessions
  73. END
  74. IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE name = 'ASPStateTempApplications' AND type = 'U') BEGIN
  75. DROP TABLE tempdb..ASPStateTempApplications
  76. END
  77. RETURN 0
  78. GO
  79. CREATE PROCEDURE CreateTempTables
  80. AS
  81. /*
  82. * Note that we cannot create user-defined data types in
  83. * tempdb because sp_addtype must be run in the context
  84. * of the current database, and we cannot switch to
  85. * tempdb from a stored procedure.
  86. */
  87. CREATE TABLE tempdb..ASPStateTempSessions (
  88. SessionId CHAR(32) NOT NULL PRIMARY KEY,
  89. Created DATETIME NOT NULL DEFAULT GETDATE(),
  90. Expires DATETIME NOT NULL,
  91. LockDate DATETIME NOT NULL,
  92. LockCookie INT NOT NULL,
  93. Timeout INT NOT NULL,
  94. Locked BIT NOT NULL,
  95. SessionItemShort VARBINARY(7000) NULL,
  96. SessionItemLong IMAGE NULL,
  97. )
  98. CREATE TABLE tempdb..ASPStateTempApplications (
  99. AppId INT NOT NULL IDENTITY PRIMARY KEY,
  100. AppName CHAR(280) NOT NULL,
  101. )
  102. CREATE NONCLUSTERED INDEX Index_AppName ON tempdb..ASPStateTempApplications(AppName)
  103. RETURN 0
  104. GO
  105. CREATE PROCEDURE ResetData
  106. AS
  107. EXECUTE DropTempTables
  108. EXECUTE CreateTempTables
  109. RETURN 0
  110. GO
  111. EXECUTE sp_addtype tSessionId, 'CHAR(32)', 'NOT NULL'
  112. GO
  113. EXECUTE sp_addtype tAppName, 'VARCHAR(280)', 'NOT NULL'
  114. GO
  115. EXECUTE sp_addtype tSessionItemShort, 'VARBINARY(7000)'
  116. GO
  117. EXECUTE sp_addtype tSessionItemLong, 'IMAGE'
  118. GO
  119. EXECUTE sp_addtype tTextPtr, 'VARBINARY(16)'
  120. GO
  121. CREATE PROCEDURE TempGetAppId
  122. @appName tAppName,
  123. @appId INT OUTPUT
  124. AS
  125. SELECT @appId = AppId
  126. FROM tempdb..ASPStateTempApplications
  127. WHERE AppName = @appName
  128. IF @appId IS NULL BEGIN
  129. INSERT tempdb..ASPStateTempApplications
  130. (AppName)
  131. VALUES
  132. (@appName)
  133. SELECT @appId = AppId
  134. FROM tempdb..ASPStateTempApplications
  135. WHERE AppName = @appName
  136. END
  137. RETURN 0
  138. GO
  139. CREATE PROCEDURE TempGetStateItem
  140. @id tSessionId,
  141. @itemShort tSessionItemShort OUTPUT,
  142. @locked BIT OUTPUT,
  143. @lockDate DATETIME OUTPUT,
  144. @lockCookie INT OUTPUT
  145. AS
  146. DECLARE @textptr AS tTextPtr
  147. DECLARE @length AS INT
  148. DECLARE @now as DATETIME
  149. SET @now = GETDATE()
  150. UPDATE tempdb..ASPStateTempSessions
  151. SET Expires = DATEADD(n, Timeout, @now),
  152. @locked = Locked,
  153. @lockDate = LockDate,
  154. @lockCookie = LockCookie,
  155. @itemShort = CASE @locked
  156. WHEN 0 THEN SessionItemShort
  157. ELSE NULL
  158. END,
  159. @textptr = CASE @locked
  160. WHEN 0 THEN TEXTPTR(SessionItemLong)
  161. ELSE NULL
  162. END,
  163. @length = CASE @locked
  164. WHEN 0 THEN DATALENGTH(SessionItemLong)
  165. ELSE NULL
  166. END
  167. WHERE SessionId = @id
  168. IF @length IS NOT NULL BEGIN
  169. READTEXT tempdb..ASPStateTempSessions.SessionItemLong @textptr 0 @length
  170. END
  171. RETURN 0
  172. GO
  173. CREATE PROCEDURE TempGetStateItemExclusive
  174. @id tSessionId,
  175. @itemShort tSessionItemShort OUTPUT,
  176. @locked BIT OUTPUT,
  177. @lockDate DATETIME OUTPUT,
  178. @lockCookie INT OUTPUT
  179. AS
  180. DECLARE @textptr AS tTextPtr
  181. DECLARE @length AS INT
  182. DECLARE @now as DATETIME
  183. SET @now = GETDATE()
  184. UPDATE tempdb..ASPStateTempSessions
  185. SET Expires = DATEADD(n, Timeout, @now),
  186. @lockDate = LockDate = CASE Locked
  187. WHEN 0 THEN @now
  188. ELSE LockDate
  189. END,
  190. @lockCookie = LockCookie = CASE Locked
  191. WHEN 0 THEN LockCookie + 1
  192. ELSE LockCookie
  193. END,
  194. @itemShort = CASE Locked
  195. WHEN 0 THEN SessionItemShort
  196. ELSE NULL
  197. END,
  198. @textptr = CASE Locked
  199. WHEN 0 THEN TEXTPTR(SessionItemLong)
  200. ELSE NULL
  201. END,
  202. @length = CASE Locked
  203. WHEN 0 THEN DATALENGTH(SessionItemLong)
  204. ELSE NULL
  205. END,
  206. @locked = Locked,
  207. Locked = 1
  208. WHERE SessionId = @id
  209. IF @length IS NOT NULL BEGIN
  210. READTEXT tempdb..ASPStateTempSessions.SessionItemLong @textptr 0 @length
  211. END
  212. RETURN 0
  213. GO
  214. CREATE PROCEDURE TempReleaseStateItemExclusive
  215. @id tSessionId,
  216. @lockCookie INT
  217. AS
  218. UPDATE tempdb..ASPStateTempSessions
  219. SET Expires = DATEADD(n, Timeout, GETDATE()),
  220. Locked = 0
  221. WHERE SessionId = @id AND LockCookie = @lockCookie
  222. RETURN 0
  223. GO
  224. CREATE PROCEDURE TempInsertStateItemShort
  225. @id tSessionId,
  226. @itemShort tSessionItemShort,
  227. @timeout INT
  228. AS
  229. DECLARE @now as DATETIME
  230. SET @now = GETDATE()
  231. INSERT tempdb..ASPStateTempSessions
  232. (SessionId,
  233. SessionItemShort,
  234. Timeout,
  235. Expires,
  236. Locked,
  237. LockDate,
  238. LockCookie)
  239. VALUES
  240. (@id,
  241. @itemShort,
  242. @timeout,
  243. DATEADD(n, @timeout, @now),
  244. 0,
  245. @now,
  246. 1)
  247. RETURN 0
  248. GO
  249. CREATE PROCEDURE TempInsertStateItemLong
  250. @id tSessionId,
  251. @itemLong tSessionItemLong,
  252. @timeout INT
  253. AS
  254. DECLARE @now as DATETIME
  255. SET @now = GETDATE()
  256. INSERT tempdb..ASPStateTempSessions
  257. (SessionId,
  258. SessionItemLong,
  259. Timeout,
  260. Expires,
  261. Locked,
  262. LockDate,
  263. LockCookie)
  264. VALUES
  265. (@id,
  266. @itemLong,
  267. @timeout,
  268. DATEADD(n, @timeout, @now),
  269. 0,
  270. @now,
  271. 1)
  272. RETURN 0
  273. GO
  274. CREATE PROCEDURE TempUpdateStateItemShort
  275. @id tSessionId,
  276. @itemShort tSessionItemShort,
  277. @timeout INT,
  278. @lockCookie INT
  279. AS
  280. UPDATE tempdb..ASPStateTempSessions
  281. SET Expires = DATEADD(n, Timeout, GETDATE()),
  282. SessionItemShort = @itemShort,
  283. Timeout = @timeout,
  284. Locked = 0
  285. WHERE SessionId = @id AND LockCookie = @lockCookie
  286. RETURN 0
  287. GO
  288. CREATE PROCEDURE TempUpdateStateItemShortNullLong
  289. @id tSessionId,
  290. @itemShort tSessionItemShort,
  291. @timeout INT,
  292. @lockCookie INT
  293. AS
  294. UPDATE tempdb..ASPStateTempSessions
  295. SET Expires = DATEADD(n, Timeout, GETDATE()),
  296. SessionItemShort = @itemShort,
  297. SessionItemLong = NULL,
  298. Timeout = @timeout,
  299. Locked = 0
  300. WHERE SessionId = @id AND LockCookie = @lockCookie
  301. RETURN 0
  302. GO
  303. CREATE PROCEDURE TempUpdateStateItemLong
  304. @id tSessionId,
  305. @itemLong tSessionItemLong,
  306. @timeout INT,
  307. @lockCookie INT
  308. AS
  309. UPDATE tempdb..ASPStateTempSessions
  310. SET Expires = DATEADD(n, Timeout, GETDATE()),
  311. SessionItemLong = @itemLong,
  312. Timeout = @timeout,
  313. Locked = 0
  314. WHERE SessionId = @id AND LockCookie = @lockCookie
  315. RETURN 0
  316. GO
  317. CREATE PROCEDURE TempUpdateStateItemLongNullShort
  318. @id tSessionId,
  319. @itemLong tSessionItemLong,
  320. @timeout INT,
  321. @lockCookie INT
  322. AS
  323. UPDATE tempdb..ASPStateTempSessions
  324. SET Expires = DATEADD(n, Timeout, GETDATE()),
  325. SessionItemLong = @itemLong,
  326. SessionItemShort = NULL,
  327. Timeout = @timeout,
  328. Locked = 0
  329. WHERE SessionId = @id AND LockCookie = @lockCookie
  330. RETURN 0
  331. GO
  332. CREATE PROCEDURE TempRemoveStateItem
  333. @id tSessionId,
  334. @lockCookie INT
  335. AS
  336. DELETE tempdb..ASPStateTempSessions
  337. WHERE SessionId = @id AND LockCookie = @lockCookie
  338. RETURN 0
  339. GO
  340. CREATE PROCEDURE TempResetTimeout
  341. @id tSessionId
  342. AS
  343. UPDATE tempdb..ASPStateTempSessions
  344. SET Expires = DATEADD(n, Timeout, GETDATE())
  345. WHERE SessionId = @id
  346. RETURN 0
  347. GO
  348. CREATE PROCEDURE DeleteExpiredSessions
  349. AS
  350. DECLARE @now DATETIME
  351. SET @now = GETDATE()
  352. DELETE tempdb..ASPStateTempSessions
  353. WHERE Expires < @now
  354. RETURN 0
  355. GO
  356. EXECUTE CreateTempTables
  357. GO
  358. /* Create the startup procedure */
  359. USE master
  360. GO
  361. CREATE PROCEDURE ASPState_Startup
  362. AS
  363. EXECUTE ASPState..CreateTempTables
  364. RETURN 0
  365. GO
  366. EXECUTE sp_procoption @procname='ASPState_Startup', @optionname='startup', @optionvalue='true'
  367. /* Create the job to delete expired sessions */
  368. BEGIN TRANSACTION
  369. DECLARE @JobID BINARY(16)
  370. DECLARE @ReturnCode INT
  371. SELECT @ReturnCode = 0
  372. -- Add job category
  373. IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'[Uncategorized (Local)]') < 1
  374. EXECUTE msdb.dbo.sp_add_category @name = N'[Uncategorized (Local)]'
  375. -- Add the job
  376. EXECUTE @ReturnCode = msdb.dbo.sp_add_job
  377. @job_id = @JobID OUTPUT,
  378. @job_name = N'ASPState_Job_DeleteExpiredSessions',
  379. @owner_login_name = NULL,
  380. @description = N'Deletes expired sessions from the session state database.',
  381. @category_name = N'[Uncategorized (Local)]',
  382. @enabled = 1,
  383. @notify_level_email = 0,
  384. @notify_level_page = 0,
  385. @notify_level_netsend = 0,
  386. @notify_level_eventlog = 0,
  387. @delete_level= 0
  388. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  389. -- Add the job steps
  390. EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep
  391. @job_id = @JobID,
  392. @step_id = 1,
  393. @step_name = N'ASPState_JobStep_DeleteExpiredSessions',
  394. @command = N'EXECUTE DeleteExpiredSessions',
  395. @database_name = N'ASPState',
  396. @server = N'',
  397. @database_user_name = N'',
  398. @subsystem = N'TSQL',
  399. @cmdexec_success_code = 0,
  400. @flags = 0,
  401. @retry_attempts = 0,
  402. @retry_interval = 1,
  403. @output_file_name = N'',
  404. @on_success_step_id = 0,
  405. @on_success_action = 1,
  406. @on_fail_step_id = 0,
  407. @on_fail_action = 2
  408. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  409. EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1
  410. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  411. -- Add the job schedules
  412. EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule
  413. @job_id = @JobID,
  414. @name = N'ASPState_JobSchedule_DeleteExpiredSessions',
  415. @enabled = 1,
  416. @freq_type = 4,
  417. @active_start_date = 20001016,
  418. @active_start_time = 0,
  419. @freq_interval = 1,
  420. @freq_subday_type = 4,
  421. @freq_subday_interval = 1,
  422. @freq_relative_interval = 0,
  423. @freq_recurrence_factor = 0,
  424. @active_end_date = 99991231,
  425. @active_end_time = 235959
  426. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  427. -- Add the Target Servers
  428. EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)'
  429. IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  430. COMMIT TRANSACTION
  431. GOTO EndSave
  432. QuitWithRollback:
  433. IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
  434. EndSave:
  435. GO