diff --git a/terraform/environments/tribunals/README.md b/terraform/environments/tribunals/README.md index 9aa2658704c..0dd6630bd4e 100644 --- a/terraform/environments/tribunals/README.md +++ b/terraform/environments/tribunals/README.md @@ -1,4 +1,4 @@ -# Service Runbook +## Service Runbook @@ -74,3 +74,5 @@ Modernisation Platform ### **How to resolve specific issues:** + + diff --git a/terraform/environments/tribunals/application_variables.json b/terraform/environments/tribunals/application_variables.json index 6b52bfe9b30..254e496fe1b 100644 --- a/terraform/environments/tribunals/application_variables.json +++ b/terraform/environments/tribunals/application_variables.json @@ -1,16 +1,102 @@ { "accounts": { "development": { - "example_var": "dev-data" + "allocated_storage": "20", + "storage_type": "gp2", + "db_identifier": "tribunals-db-dev", + "engine": "sqlserver-se", + "engine_version": "15.00.4198.2.v1", + "instance_class": "db.m5.large", + "username": "admin", + "dms_source_db": "ec2-34-243-192-28.eu-west-1.compute.amazonaws.com", + "curserver": "DEVELOPMENT", + "support_team": "DTS Legacy Apps Support Team", + "support_email": "dts-legacy-apps-support-team@hmcts.net", + "server_port_1": "80", + "lb_listener_protocol_1": "HTTP", + "server_port_2": "443", + "lb_listener_protocol_2": "HTTPS", + "server_port_3": "22", + "lb_listener_protocol_3": "TCP", + "ami_image_id": "ami-0d20b6fc5007adcb3", + "task_definition_volume": "tribunals", + "server_port": 8080, + "app_count": 1, + "appscaling_min_capacity": 1, + "appscaling_max_capacity": 2, + "ec2_scaling_cpu_threshold": 75, + "ec2_scaling_mem_threshold": 61, + "ecs_scaling_cpu_threshold": 80, + "ecs_scaling_mem_threshold": 80 }, "test": { "example_var": "test-data" }, "preproduction": { - "example_var": "preproduction-data" + "allocated_storage": "20", + "storage_type": "gp2", + "db_identifier": "tribunals-db-preprod", + "engine": "sqlserver-se", + "engine_version": "15.00.4198.2.v1", + "instance_class": "db.m5.large", + "username": "admin", + "dms_source_db": "ec2-34-243-192-28.eu-west-1.compute.amazonaws.com", + "curserver": "PREPRODUCTION", + "support_team": "DTS Legacy Apps Support Team", + "support_email": "dts-legacy-apps-support-team@hmcts.net", + "server_port_1": "80", + "lb_listener_protocol_1": "HTTP", + "server_port_2": "443", + "lb_listener_protocol_2": "HTTPS", + "server_port_3": "22", + "lb_listener_protocol_3": "TCP", + "ami_image_id": "ami-0d20b6fc5007adcb3", + "task_definition_volume": "tribunals", + "server_port": 8080, + "app_count": 1, + "appscaling_min_capacity": 1, + "appscaling_max_capacity": 6, + "ecs_scaling_cpu_threshold": 80, + "ecs_scaling_mem_threshold": 80 }, "production": { - "example_var": "production-data" + "allocated_storage": "20", + "storage_type": "gp2", + "db_identifier": "tribunals-db-prod", + "engine": "sqlserver-se", + "engine_version": "15.00.4198.2.v1", + "instance_class": "db.m5.large", + "username": "admin", + "dms_source_db": "ec2-34-243-192-28.eu-west-1.compute.amazonaws.com", + "curserver": "PRODUCTION", + "support_team": "DTS Legacy Apps Support Team", + "support_email": "dts-legacy-apps-support-team@hmcts.net", + "server_port_1": "80", + "lb_listener_protocol_1": "HTTP", + "server_port_2": "443", + "lb_listener_protocol_2": "HTTPS", + "server_port_3": "22", + "lb_listener_protocol_3": "TCP", + "ami_image_id": "ami-0d233dc36193b1c63", + "task_definition_volume": "tribunals", + "server_port": 8080, + "app_count": 2, + "appscaling_min_capacity": 2, + "appscaling_max_capacity": 6, + "ecs_scaling_cpu_threshold": 80, + "ecs_scaling_mem_threshold": 80 + } + }, + "ec2_sg_rules": { + "TCP_80": { + "from_port": 80, + "to_port": 80, + "protocol": "TCP" + }, + "TCP_443": { + "from_port": 443, + "to_port": 443, + "protocol": "TCP" } } } diff --git a/terraform/environments/tribunals/asg-shared.tf b/terraform/environments/tribunals/asg-shared.tf new file mode 100644 index 00000000000..de620e5406f --- /dev/null +++ b/terraform/environments/tribunals/asg-shared.tf @@ -0,0 +1,263 @@ +locals { + app_name = "tribunals-shared" + instance_role_name = join("-", [local.app_name, "ec2-instance-role"]) + instance_profile_name = join("-", [local.app_name, "ec2-instance-profile"]) + ec2_instance_policy = join("-", [local.app_name, "ec2-instance-policy"]) + tags_common = local.tags +} + +# Create an IAM policy for the custom permissions required by the EC2 hosting instance +resource "aws_iam_policy" "ec2_instance_policy" { #tfsec:ignore:aws-iam-no-policy-wildcards + name = local.ec2_instance_policy + tags = merge( + local.tags_common, + { + Name = local.ec2_instance_policy + } + ) + policy = < Amazon ECS Sample App

Amazon ECS Sample App

Congratulations!

Your application is now running on a container in Amazon ECS.

'; C:\\ServiceMonitor.exe w3svc" + ], + "entryPoint": [ + "powershell", + "-Command" + ], + "name" : "${app_name}-container", + "image" : "${container_definition_image}", + "cpu" : 512, + "memory" : 1024, + "essential" : true, + "portMappings" : [ + { + "hostPort": 0, + "containerPort": 80, + "protocol": "tcp" + } + ], + "logConfiguration" : { + "logDriver" : "awslogs", + "options" : { + "awslogs-group" : "${awslogs-group}", + "awslogs-region" : "eu-west-2", + "awslogs-stream-prefix" : "ecs" + } + }, + "mountPoints": [ + { + "sourceVolume": "tribunals", + "containerPath": "C:/inetpub/wwwroot/${documents_location}" + } + ], + + "environment" : [ + { + "name" : "supportEmail", + "value" : "${supportEmail}" + }, + { + "name" : "supportTeam", + "value" : "${supportTeam}" + }, + { + "name" : "CurServer", + "value" : "${CurServer}" + }, + { + "name" : "RDS_PASSWORD", + "value" : "${rds_password}" + } + ] + } +] diff --git a/terraform/environments/tribunals/container_definition_ftp.json b/terraform/environments/tribunals/container_definition_ftp.json new file mode 100644 index 00000000000..e35f18ba229 --- /dev/null +++ b/terraform/environments/tribunals/container_definition_ftp.json @@ -0,0 +1,42 @@ +[ + { + "command": [ + "C:\\ServiceMonitor.exe w3svc" + ], + "entryPoint": [ + "powershell", + "-Command" + ], + "name" : "${app_name}-container", + "image" : "${container_definition_image}", + "cpu" : 512, + "memory" : 1024, + "essential" : true, + "portMappings" : [ + { + "hostPort": 0, + "containerPort": 22, + "protocol": "tcp" + }, + { + "hostPort": 0, + "containerPort": 80, + "protocol": "tcp" + } + ], + "logConfiguration" : { + "logDriver" : "awslogs", + "options" : { + "awslogs-group" : "${awslogs-group}", + "awslogs-region" : "eu-west-2", + "awslogs-stream-prefix" : "ecs" + } + }, + "mountPoints": [ + { + "sourceVolume": "tribunals", + "containerPath": "C:/inetpub/wwwroot/${documents_location}" + } + ] + } +] diff --git a/terraform/environments/tribunals/data.tf b/terraform/environments/tribunals/data.tf index 96a2521d17e..8ea6e912f74 100644 --- a/terraform/environments/tribunals/data.tf +++ b/terraform/environments/tribunals/data.tf @@ -1 +1,22 @@ #### This file can be used to store data specific to the member account #### +# data "aws_secretsmanager_secret" "rds-secrets" { +# arn = "arn:aws:secretsmanager:eu-west-2:263310006819:secret:tribunals-db-dev-credentials-WIKA7c" +# } + +# data "aws_secretsmanager_secret_version" "rds" { +# secret_id = data.aws_secretsmanager_secret.rds-secrets.id +# } + +# data "aws_secretsmanager_secret" "source-db-secrets" { +# arn = "arn:aws:secretsmanager:eu-west-2:263310006819:secret:tribunals-source-db-credentials-dev-BHvvLl" +# } + +# data "aws_secretsmanager_secret_version" "source-db" { +# secret_id = data.aws_secretsmanager_secret.source-db-secrets.id +# } + +# data "aws_route53_zone" "application_zone" { +# provider = aws.core-network-services +# name = "transport.service.justice.gov.uk." +# private_zone = false +# } \ No newline at end of file diff --git a/terraform/environments/tribunals/dms.tf b/terraform/environments/tribunals/dms.tf new file mode 100644 index 00000000000..09087a776f6 --- /dev/null +++ b/terraform/environments/tribunals/dms.tf @@ -0,0 +1,140 @@ +resource "aws_dms_replication_instance" "tribunals_replication_instance" { + allocated_storage = 300 + apply_immediately = true + availability_zone = "eu-west-2a" + engine_version = "3.5.1" + multi_az = false + publicly_accessible = true + auto_minor_version_upgrade = true + replication_instance_class = "dms.t3.large" + replication_instance_id = "tribunals-replication-instance" + vpc_security_group_ids = [aws_security_group.vpc_dms_replication_instance_group.id] + replication_subnet_group_id = aws_dms_replication_subnet_group.dms_replication_subnet_group.id +} + +resource "aws_dms_replication_subnet_group" "dms_replication_subnet_group" { + replication_subnet_group_id = "dms-replication-subnet-group" + subnet_ids = data.aws_subnets.shared-public.ids + replication_subnet_group_description = "DMS replication subnet group" +} + +resource "aws_security_group" "vpc_dms_replication_instance_group" { + vpc_id = data.aws_vpc.shared.id + name = "vpc-dms-replication-instance-group" + description = "allow dms replication instance access to the shared vpc on the modernisation platform" + + ingress { + from_port = 1433 + to_port = 1433 + protocol = "tcp" + description = "Allow all inbound traffic" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + description = "Allow all outbound traffic" + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_iam_role" "dms_vpc_role" { + name = "dms-vpc-role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Effect = "Allow" + Principal = { + Service = "dms.amazonaws.com" + } + Action = "sts:AssumeRole" + } + ] + }) +} + +resource "aws_iam_role_policy" "dms_vpc_management_policy" { + name = "dms-vpc-management-policy" + role = aws_iam_role.dms_vpc_role.name + + policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Effect = "Allow" + Action = [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:DescribeLogStreams", + "logs:PutLogEvents", + "rds:*", + "dms:*", + "ec2:*" + ] + Resource = "*" + } + ] + }) +} + +resource "aws_dms_endpoint" "source" { + database_name = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["dbname"] + endpoint_id = "tribunals-source" + endpoint_type = "source" + engine_name = "sqlserver" + password = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["password"] + port = 1433 + server_name = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["host"] + ssl_mode = "none" + + username = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["username"] +} + +# Uncomment modernisation_dms_access for first time creation of the Security Group in AWS DSD Account +resource "aws_security_group" "modernisation_dms_access" { + provider = aws.mojdsd + name = "modernisation_dms_access" + description = "allow dms access to the database for the modernisation platform" + + ingress { + from_port = 1433 + to_port = 1433 + protocol = "tcp" + description = "Allow DMS to connect to source database" + cidr_blocks = ["${aws_dms_replication_instance.tribunals_replication_instance.replication_instance_public_ips[0]}/32"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +// Uncomment setup_target_rds_security_group for first time setup of DMS +// executes a local script to set up the security group for the target RDS instance in the source db aws account +resource "null_resource" "setup_target_rds_security_group" { + depends_on = [aws_dms_replication_instance.tribunals_replication_instance] + + provisioner "local-exec" { + interpreter = ["bash", "-c"] + command = "ifconfig -a; chmod +x ./setup-security-group.sh; ./setup-security-group.sh" + + environment = { + DMS_SECURITY_GROUP = aws_security_group.modernisation_dms_access.id + EC2_INSTANCE_ID = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["ec2-instance-id"] + DMS_SOURCE_ACCOUNT_ACCESS_KEY = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["dms_source_account_access_key"] + DMS_SOURCE_ACCOUNT_SECRET_KEY = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["dms_source_account_secret_key"] + AWS_REGION = "eu-west-1" + + } + } + triggers = { + always_run = "${timestamp()}" + } +} diff --git a/terraform/environments/tribunals/dns_ssl.tf b/terraform/environments/tribunals/dns_ssl.tf new file mode 100644 index 00000000000..571bf206e50 --- /dev/null +++ b/terraform/environments/tribunals/dns_ssl.tf @@ -0,0 +1,425 @@ +# ACM certificate validation +resource "aws_acm_certificate_validation" "external" { + certificate_arn = aws_acm_certificate.external.arn + validation_record_fqdns = [for record in aws_route53_record.external_validation : record.fqdn] +} + +# One route53 record required for each domain listed in the external certificate +resource "aws_route53_record" "external_validation" { + provider = aws.core-vpc + for_each = { + for dvo in aws_acm_certificate.external.domain_validation_options : dvo.domain_name => { + name = dvo.resource_record_name + record = dvo.resource_record_value + type = dvo.resource_record_type + } + } + + allow_overwrite = true + name = each.value.name + records = [each.value.record] + ttl = 60 + type = each.value.type + zone_id = data.aws_route53_zone.external.zone_id +} + +# // Create one Route 53 record for each entry in the list of tribunals (assigned in platform_locals.tf) +resource "aws_route53_record" "external_appeals" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "administrativeappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.appeals.tribunals_lb.dns_name + zone_id = module.appeals.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_ahmlr" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "landregistrationdivision.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.ahmlr.tribunals_lb.dns_name + zone_id = module.ahmlr.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +# resource "aws_route53_record" "external_care_standards" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "carestandards.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.care_standards.tribunals_lb.dns_name +# zone_id = module.care_standards.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_cicap" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "cicap.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.cicap.tribunals_lb.dns_name +# zone_id = module.cicap.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_eat" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "employmentappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.employment_appeals.tribunals_lb.dns_name +# zone_id = module.employment_appeals.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_ftt" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "financeandtax.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.finance_and_tax.tribunals_lb.dns_name +# zone_id = module.finance_and_tax.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_imset" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "immigrationservices.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.immigration_services.tribunals_lb.dns_name +# zone_id = module.immigration_services.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_it" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "informationrights.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.information_tribunal.tribunals_lb.dns_name +# zone_id = module.information_tribunal.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_lands" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "landschamber.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.lands_tribunal.tribunals_lb.dns_name +# zone_id = module.lands_tribunal.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +# resource "aws_route53_record" "external_transport" { +# provider = aws.core-vpc +# zone_id = data.aws_route53_zone.external.zone_id +# name = "transportappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" +# type = "A" + +# alias { +# name = module.transport.tribunals_lb.dns_name +# zone_id = module.transport.tribunals_lb.zone_id +# evaluate_target_health = true +# } +# } + +// Records for FTP sites +resource "aws_route53_record" "external_charity" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "charitytribunal.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.charity_tribunal_decisions.tribunals_lb.dns_name + zone_id = module.charity_tribunal_decisions.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_charity_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.charitytribunal.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.charity_tribunal_decisions.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_claims_management" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "claimsmanagement.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.claims_management_decisions.tribunals_lb.dns_name + zone_id = module.claims_management_decisions.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_claims_management_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.claimsmanagement.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.claims_management_decisions.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_consumer_credit_appeals" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "consumercreditappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.consumer_credit_appeals.tribunals_lb.dns_name + zone_id = module.consumer_credit_appeals.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_consumer_credit_appeals_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.consumercreditappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.consumer_credit_appeals.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_estate_agent_appeals" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "estateagentappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.estate_agent_appeals.tribunals_lb.dns_name + zone_id = module.estate_agent_appeals.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_estate_agent_appeals_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.estateagentappeals.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.estate_agent_appeals.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_primary_health_lists" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "primaryhealthlists.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.primary_health_lists.tribunals_lb.dns_name + zone_id = module.primary_health_lists.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_primary_health_lists_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.primaryhealthlists.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.primary_health_lists.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_siac" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "siac.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.siac.tribunals_lb.dns_name + zone_id = module.siac.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_siac_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.siac.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.siac.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_sscs_venue_pages" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sscsvenues.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.sscs_venue_pages.tribunals_lb.dns_name + zone_id = module.sscs_venue_pages.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_sscs_venue_pages_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.sscsvenues.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.sscs_venue_pages.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_tax_chancery_decisions" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "taxchancerydecisions.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.tax_chancery_decisions.tribunals_lb.dns_name + zone_id = module.tax_chancery_decisions.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_tax_chancery_decisions_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.taxchancerydecisions.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.tax_chancery_decisions.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_tax_tribunal_decisions" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "taxtribunaldecisions.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.tax_tribunal_decisions.tribunals_lb.dns_name + zone_id = module.tax_tribunal_decisions.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_tax_tribunal_decisions_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.taxtribunaldecisions.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.tax_tribunal_decisions.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +resource "aws_route53_record" "external_ftp_admin_appeals" { + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "adminappealsreports.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "A" + + alias { + name = module.ftp-admin-appeals.tribunals_lb.dns_name + zone_id = module.ftp-admin-appeals.tribunals_lb.zone_id + evaluate_target_health = true + } +} + +resource "aws_route53_record" "external_admin_appeals_sftp" { + allow_overwrite = true + provider = aws.core-vpc + zone_id = data.aws_route53_zone.external.zone_id + name = "sftp.adminappealsreports.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + type = "CNAME" + + records = [module.ftp-admin-appeals.tribunals_lb_ftp[0].dns_name] + ttl = 60 +} + +# Define a wildcard ACM certificate for sandbox/dev +resource "aws_acm_certificate" "external" { + domain_name = "${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + validation_method = "DNS" + + subject_alternative_names = [ + "*.${var.networking[0].business-unit}-${local.environment}.modernisation-platform.service.justice.gov.uk" + ] + tags = { + Environment = local.environment + } + + lifecycle { + create_before_destroy = true + } +} + +output "acm_certificate_arn" { + value = aws_acm_certificate.external.arn +} + +output "acm_certificate_validation_dns" { + value = [for dvo in aws_acm_certificate.external.domain_validation_options : dvo.resource_record_name] +} + +output "acm_certificate_validation_route53" { + value = [for dvo in aws_acm_certificate.external.domain_validation_options : dvo.resource_record_value] +} \ No newline at end of file diff --git a/terraform/environments/tribunals/ebs-shared.tf b/terraform/environments/tribunals/ebs-shared.tf new file mode 100644 index 00000000000..310793c0f02 --- /dev/null +++ b/terraform/environments/tribunals/ebs-shared.tf @@ -0,0 +1,12 @@ +resource "aws_ebs_volume" "tribunals-ebs" { + availability_zone = "eu-west-2a" + type = "gp2" + size = 10 + + tags = merge( + local.tags, + { + Name = "tribunals-all-storage" + } + ) +} \ No newline at end of file diff --git a/terraform/environments/tribunals/ec2-shared-user-data.sh b/terraform/environments/tribunals/ec2-shared-user-data.sh new file mode 100644 index 00000000000..d4dfafd395c --- /dev/null +++ b/terraform/environments/tribunals/ec2-shared-user-data.sh @@ -0,0 +1,143 @@ + +$logFile = "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\userdata.log" +$linkPath = "C:\ProgramData\docker\volumes\tribunals\" +$targetDrive = "D" +$targetPath = $targetDrive + ":\storage\tribunals\" +$ecsCluster = "tribunals-all-cluster" +$ebsVolumeTag = "tribunals-all-storage" +$tribunalNames = "appeals","transport","care-standards","cicap","employment-appeals","finance-and-tax","immigration-services","information-tribunal","hmlands","lands-chamber", "ftp-admin-appeals", "ftp-tax-tribunal", "ftp-tax-chancery", "ftp-sscs-venues", "ftp-siac", "ftp-primary-health", "ftp-estate-agents", "ftp-consumer-credit", "ftp-claims-management", "ftp-charity-tribunals" +$monitorLogFile = "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" +$monitorScriptFile = "C:\ProgramData\Amazon\EC2-Windows\Launch\monitor-ebs.ps1" + +"Starting userdata execution" > $logFile + +# Get the volumeid based on its tag +$instanceId = Get-EC2InstanceMetadata -Path '/instance-id' +"Got instanceid " + $instanceid >> $logFile + +$volumeid = Get-EC2Volume -Filter @{ Name="tag:Name"; Values=$ebsVolumeTag } -Select Volumes.VolumeId +"Got volumeid " + $volumeid >> $logFile + +if ([string]::IsNullorEmpty($volumeid)) { + "No volume exists with the tag " + $ebsVolumeTag >> $logFile +} +else { + "Adding volume $volumeid" >> $logFile + Add-EC2Volume -VolumeId $volumeid -InstanceId $instanceid -Device /dev/xvdf + "result of Adding volume is " + $? >> $logfile + + # Does the attached volume contain a raw disk? + $rawdisks = Get-Disk | Where PartitionStyle -eq 'raw' + + "get-disk result is " + $rawdisks >> $logfile + + # If not, put the disk online and assign the drive letter + if ([string]::IsNullorEmpty($rawdisks)) { + Get-Disk >> $logFile + Set-Disk -Number 1 -IsOffline $False -IsReadOnly $False + Set-Partition -DiskNumber 1 -PartitionNumber 1 -NewDriveLetter $targetDrive + } + else { + "Formatting volume..." >> $logFile + + # If it does have a raw disk, format it and create the partition and assign + Get-Disk | Where PartitionStyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -DriveLetter D -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Tribunals" -Confirm:$false + } + + for ($i=0; $i -lt $tribunalNames.Length; $i++) { + $subDirPath = ($targetPath + $tribunalNames[$i]) + if (!(Test-Path $subDirPath)) { + New-Item -ItemType Directory -Path $subDirPath + "created " + $subDirPath >> $logFile + } + } +} + +if (Test-Path $linkPath) { + "Link exists for " + $linkPath >> $logFile + Get-Item $linkPath >> $logFile + if ((Get-Item $linkPath).LinkType -eq "SymbolicLink") { + "It is a symbolic link " >> $logFile + } else { + "It is not a symbolic link" >> $logFile + } +} else { + "Linking " + $linkPath + " to " + $targetPath >> $logFile + New-Item -Path $linkPath -ItemType SymbolicLink -Value $targetPath +} + +"Set Environment variable to enable awslogs attribute" >> $logFile +Import-Module ECSTools +[Environment]::SetEnvironmentVariable("ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE", "true", "Machine") + +"Link instance to shared tribunals cluster " + $ecsCluster >> $logFile +Initialize-ECSAgent -Cluster $ecsCluster -EnableTaskIAMRole -LoggingDrivers '["json-file","awslogs"]' + +"Finished launch.ps1" >> $logFile + +# Check if AWS CLI is installed +$awsCliInstalled = $null -ne (Get-Command aws -ErrorAction SilentlyContinue) + +if (-not $awsCliInstalled) { + # Download and install the AWS CLI + "AWS CLI not found. Installing..." > $monitorLogFile + $installerPath = "$env:TEMP\AWSCLIV2.msi" + Invoke-WebRequest -Uri "https://awscli.amazonaws.com/AWSCLIV2.msi" -OutFile $installerPath + Start-Process -FilePath "msiexec.exe" -ArgumentList "/i", $installerPath, "/quiet", "/qn", "/norestart" -Wait + Remove-Item -Path $installerPath + $env:Path += ";C:\Program Files\Amazon\AWSCLIV2" + "AWS CLI installed successfully." >> $monitorLogFile +} else { + "AWS CLI is already installed." >> $monitorLogFile +} + +$scriptContent = @' +function MonitorAndSyncToS3 { + "Script started at $(Get-Date)" >> "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" + # Create a FileSystemWatcher object + $watcher = New-Object System.IO.FileSystemWatcher + $watcher.Path = "D:\storage\tribunals\" + $watcher.IncludeSubdirectories = $true + $watcher.EnableRaisingEvents = $true + + # Define the action to take when a file is created + $action = { + param($source, $event) + $filePath = $event.FullPath + $relativePath = $filePath -replace '^D:\\storage\\tribunals\\', '' -replace '\\', '/' + "A file was created at $filePath. Uploading to S3..." >> "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" + aws s3 cp $filePath "s3://tribunals-ebs-backup/$relativePath" >> "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" + } + + # Register the event + Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action + + # Keep the script running + while ($true) { + Start-Sleep -Seconds 10 + } +} + +function InitialSyncToS3 { + "Initial sync to S3 started at $(Get-Date)" >> "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" + aws s3 sync D:\storage\tribunals\ s3://tribunals-ebs-backup >> "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" + "Initial sync to S3 completed at $(Get-Date)" >> "C:\ProgramData\Amazon\EC2-Windows\Launch\Log\monitorLogFile.log" +} + +# Call the function +InitialSyncToS3 +MonitorAndSyncToS3 +'@ + +Set-ExecutionPolicy RemoteSigned -Scope LocalMachine + +# Save the script to a file on the EC2 instance +$scriptContent | Out-File -FilePath "C:\MonitorAndSyncToS3.ps1" + +$Action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-ExecutionPolicy Bypass -File "C:\MonitorAndSyncToS3.ps1"' + +$Trigger = New-ScheduledTaskTrigger -Once -At ((Get-Date).AddMinutes(5)) + +Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "MonitorAndSyncToS3" -Description "Monitor EBS Volume and copy newly added files to S3" -RunLevel Highest -User "SYSTEM" -Force + + \ No newline at end of file diff --git a/terraform/environments/tribunals/ec2_bastion_linux.tf b/terraform/environments/tribunals/ec2_bastion_linux.tf new file mode 100644 index 00000000000..7ad70fd9937 --- /dev/null +++ b/terraform/environments/tribunals/ec2_bastion_linux.tf @@ -0,0 +1,36 @@ +# tfsec:ignore:aws-s3-enable-bucket-encryption tfsec:ignore:aws-s3-encryption-customer-key tfsec:ignore:aws-s3-enable-bucket-logging tfsec:ignore:aws-s3-enable-versioning +module "bastion_linux" { + source = "github.com/ministryofjustice/modernisation-platform-terraform-bastion-linux?ref=v4.0.0" + + providers = { + aws.share-host = aws.core-vpc # core-vpc-(environment) holds the networking for all accounts + aws.share-tenant = aws # The default provider (unaliased, `aws`) is the tenant + } + # s3 - used for logs and user ssh public keys + bucket_name = "bastion-example" + bucket_versioning = true + bucket_force_destroy = true + # public keys + public_key_data = local.public_key_data.keys[local.environment] + # logs + log_auto_clean = "Enabled" + log_standard_ia_days = 30 # days before moving to IA storage + log_glacier_days = 60 # days before moving to Glacier + log_expiry_days = 180 # days before log expiration + # bastion + allow_ssh_commands = false + app_name = var.networking[0].application + business_unit = local.vpc_name + subnet_set = local.subnet_set + environment = local.environment + region = "eu-west-2" + + # Tags + tags_common = local.tags + tags_prefix = terraform.workspace +} + + +locals { + public_key_data = jsondecode(file("./bastion_linux.json")) +} \ No newline at end of file diff --git a/terraform/environments/tribunals/ecs-cluster-shared.tf b/terraform/environments/tribunals/ecs-cluster-shared.tf new file mode 100644 index 00000000000..dfa739e07f2 --- /dev/null +++ b/terraform/environments/tribunals/ecs-cluster-shared.tf @@ -0,0 +1,191 @@ +resource "aws_ecs_cluster" "tribunals_cluster" { + name = "tribunals-all-cluster" + setting { + name = "containerInsights" + value = "enabled" + } +} + +resource "aws_cloudwatch_log_group" "tribunalsFamily_logs" { + name = "/ecs/tribunalsFamily" +} + +resource "aws_iam_role" "app_execution" { + name = "execution-${var.networking[0].application}" + + assume_role_policy = < { + name = dvo.resource_record_name + record = dvo.resource_record_value + type = dvo.resource_record_type + } + } + domain_name_main = [for k, v in local.domain_types : v.name if k == "modernisation-platform.service.justice.gov.uk"] + domain_name_sub = [for k, v in local.domain_types : v.name if k != "modernisation-platform.service.justice.gov.uk"] + domain_record_main = [for k, v in local.domain_types : v.record if k == "modernisation-platform.service.justice.gov.uk"] + domain_record_sub = [for k, v in local.domain_types : v.record if k != "modernisation-platform.service.justice.gov.uk"] + domain_type_main = [for k, v in local.domain_types : v.type if k == "modernisation-platform.service.justice.gov.uk"] + domain_type_sub = [for k, v in local.domain_types : v.type if k != "modernisation-platform.service.justice.gov.uk"] + +} diff --git a/terraform/environments/tribunals/main.tf b/terraform/environments/tribunals/main.tf new file mode 100644 index 00000000000..49d05e8cde9 --- /dev/null +++ b/terraform/environments/tribunals/main.tf @@ -0,0 +1,611 @@ +locals { + rds_url = "${aws_db_instance.rdsdb.address}" + rds_user = jsondecode(data.aws_secretsmanager_secret_version.data_rds_secret_current.secret_string)["username"] + rds_port = "1433" + rds_password = jsondecode(data.aws_secretsmanager_secret_version.data_rds_secret_current.secret_string)["password"] + source_db_url = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["host"] + source_db_user = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["username"] + source_db_password = jsondecode(data.aws_secretsmanager_secret_version.source_db_secret_current.secret_string)["password"] + waf_arn = "${aws_wafv2_web_acl.tribunals_web_acl.arn}" +} + +module "appeals" { + is_ftp_app = false + source = "./modules/tribunal" + # The app_name needs to match the folder name in the volume + app_name = "appeals" + app_url = "administrativeappeals" + sql_migration_path = "../scripts/administrative_appeals" + app_db_name = "ossc" + app_db_login_name = "ossc-app" + app_source_db_name = "Ossc" + app_rds_url = local.rds_url + app_rds_user = local.rds_user + app_rds_port = local.rds_port + app_rds_password = local.rds_password + app_source_db_url = local.source_db_url + app_source_db_user = local.source_db_user + app_source_db_password = local.source_db_password + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "JudgmentFiles" + waf_arn = local.waf_arn +} + +module "ahmlr" { + is_ftp_app = false + source = "./modules/tribunal" + app_name = "hmlands" + app_url = "landregistrationdivision" + sql_migration_path = "../scripts/ahmlr" + app_db_name = "hmlands" + app_db_login_name = "hmlands-app" + app_source_db_name = "hmlands" + app_rds_url = local.rds_url + app_rds_user = local.rds_user + app_rds_port = local.rds_port + app_rds_password = local.rds_password + app_source_db_url = local.source_db_url + app_source_db_user = local.source_db_user + app_source_db_password = local.source_db_password + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Judgments" + waf_arn = local.waf_arn +} + +# module "care_standards" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "care-standards" +# app_url = "carestandards" +# sql_migration_path = "../scripts/care_standards" +# app_db_name = "carestandards" +# app_db_login_name = "carestandards-app" +# app_source_db_name = "carestandards" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "Judgments" +# waf_arn = local.waf_arn +# } + +# module "cicap" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "cicap" +# app_url = "cicap" +# sql_migration_path = "../scripts/cicap" +# app_db_name = "cicap" +# app_db_login_name = "cicap-app" +# app_source_db_name = "cicap" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "CaseFiles" +# waf_arn = local.waf_arn +# } + +# module "employment_appeals" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "employment-appeals" +# app_url = "employmentappeals" +# sql_migration_path = "../scripts/employment_appeals" +# app_db_name = "eat" +# app_db_login_name = "eat-app" +# app_source_db_name = "eat" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "Public/Upload" +# waf_arn = local.waf_arn +# } + +# module "finance_and_tax" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "finance-and-tax" +# app_url = "financeandtax" +# sql_migration_path = "../scripts/finance_and_tax" +# app_db_name = "ftt" +# app_db_login_name = "ftt-app" +# app_source_db_name = "ftt" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "JudgmentFiles" +# waf_arn = local.waf_arn +# } + +# module "immigration_services" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "immigration-services" +# app_url = "immigrationservices" +# sql_migration_path = "../scripts/immigration_services" +# app_db_name = "imset" +# app_db_login_name = "imset-app" +# app_source_db_name = "imset" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "JudgmentFiles" +# waf_arn = local.waf_arn +# } + +# module "information_tribunal" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "information-tribunal" +# app_url = "informationrights" +# sql_migration_path = "../scripts/information_tribunal" +# app_db_name = "it" +# app_db_login_name = "it-app" +# app_source_db_name = "it" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "DBFiles" +# waf_arn = local.waf_arn +# } + +# module "lands_tribunal" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "lands-chamber" +# app_url = "landschamber" +# sql_migration_path = "../scripts/lands_chamber" +# app_db_name = "lands" +# app_db_login_name = "lands-app" +# app_source_db_name = "lands" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "JudgmentFiles" +# waf_arn = local.waf_arn +# } + +# module "transport" { +# is_ftp_app = false +# source = "./modules/tribunal" +# app_name = "transport" +# app_url = "transportappeals" +# sql_migration_path = "../scripts/transport" +# app_db_name = "transport" +# app_db_login_name = "transport-app" +# app_source_db_name = "Transport" +# app_rds_url = local.rds_url +# app_rds_user = local.rds_user +# app_rds_port = local.rds_port +# app_rds_password = local.rds_password +# app_source_db_url = local.source_db_url +# app_source_db_user = local.source_db_user +# app_source_db_password = local.source_db_password +# environment = local.environment +# application_data = local.application_data.accounts[local.environment] +# tags = local.tags +# dms_instance_arn = aws_dms_replication_instance.tribunals_replication_instance.replication_instance_arn +# task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume +# appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity +# appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity +# ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold +# ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold +# app_count = local.application_data.accounts[local.environment].app_count +# server_port = local.application_data.accounts[local.environment].server_port_1 +# cluster_id = aws_ecs_cluster.tribunals_cluster.id +# cluster_name = aws_ecs_cluster.tribunals_cluster.name +# vpc_shared_id = data.aws_vpc.shared.id +# subnets_shared_public_ids = data.aws_subnets.shared-public.ids +# aws_acm_certificate_external = aws_acm_certificate.external +# documents_location = "JudgmentFiles" +# waf_arn = local.waf_arn +# } + +module "charity_tribunal_decisions" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-charity-tribunals" + app_url = "charitytribunal" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "documents" + waf_arn = local.waf_arn +} + +module "claims_management_decisions" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-claims-management" + app_url = "claimsmanagement" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "consumer_credit_appeals" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-consumer-credit" + app_url = "consumercreditappeals" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "estate_agent_appeals" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-estate-agents" + app_url = "estateagentappeals" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "primary_health_lists" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-primary-health" + app_url = "primaryhealthlists" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "siac" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-siac" + app_url = "siac" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "sscs_venue_pages" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-sscs-venues" + app_url = "sscsvenues" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "tax_chancery_decisions" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-tax-chancery" + app_url = "taxchancerydecisions" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "tax_tribunal_decisions" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-tax-tribunal" + app_url = "taxtribunaldecisions" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} + +module "ftp-admin-appeals" { + is_ftp_app = true + source = "./modules/tribunal_ftp" + app_name = "ftp-admin-appeals" + app_url = "adminappealsreports" + environment = local.environment + application_data = local.application_data.accounts[local.environment] + tags = local.tags + task_definition_volume = local.application_data.accounts[local.environment].task_definition_volume + appscaling_min_capacity = local.application_data.accounts[local.environment].appscaling_min_capacity + appscaling_max_capacity = local.application_data.accounts[local.environment].appscaling_max_capacity + ecs_scaling_cpu_threshold = local.application_data.accounts[local.environment].ecs_scaling_cpu_threshold + ecs_scaling_mem_threshold = local.application_data.accounts[local.environment].ecs_scaling_mem_threshold + app_count = local.application_data.accounts[local.environment].app_count + server_port = local.application_data.accounts[local.environment].server_port_1 + cluster_id = aws_ecs_cluster.tribunals_cluster.id + cluster_name = aws_ecs_cluster.tribunals_cluster.name + vpc_shared_id = data.aws_vpc.shared.id + subnets_shared_public_ids = data.aws_subnets.shared-public.ids + aws_acm_certificate_external = aws_acm_certificate.external + documents_location = "Documents" + waf_arn = local.waf_arn +} \ No newline at end of file diff --git a/terraform/environments/tribunals/modules/dms/main.tf b/terraform/environments/tribunals/modules/dms/main.tf new file mode 100644 index 00000000000..facd764ea5d --- /dev/null +++ b/terraform/environments/tribunals/modules/dms/main.tf @@ -0,0 +1,70 @@ +resource "aws_dms_endpoint" "target" { + database_name = var.target_database_name + endpoint_id = var.target_endpoint_id + endpoint_type = "target" + engine_name = "sqlserver" + username = var.target_username + password = var.target_password + port = 1433 + server_name = var.target_server_name + ssl_mode = "none" +} + +resource "aws_dms_endpoint" "source" { + database_name = var.source_database_name + endpoint_id = var.source_endpoint_id + endpoint_type = "source" + engine_name = "sqlserver" + password = var.source_password + port = 1433 + server_name = var.source_server_name + ssl_mode = "none" + + username = var.source_username +} + +resource "aws_dms_replication_task" "migration-task" { + migration_type = "full-load" + replication_instance_arn = var.replication_instance_arn + replication_task_id = var.replication_task_id + source_endpoint_arn = aws_dms_endpoint.source.endpoint_arn + target_endpoint_arn = aws_dms_endpoint.target.endpoint_arn + start_replication_task = false + + replication_task_settings = jsonencode({ + TargetMetadata = { + FullLobMode = true, + LobChunkSize = 64 + }, + FullLoadSettings = { + TargetTablePrepMode = "DO_NOTHING" + }, + ControlTablesSettings = { + historyTimeslotInMinutes = 5 + }, + ErrorBehavior = { + DataErrorPolicy = "LOG_ERROR" + ApplyErrorDeletePolicy = "LOG_ERROR" + ApplyErrorInsertPolicy = "LOG_ERROR" + ApplyErrorUpdatePolicy = "LOG_ERROR" + ApplyErrorEscalationCount = 0 + ApplyErrorEscalationPolicy = "LOG_ERROR" + } + }) + + table_mappings = jsonencode({ + rules = [ + { + "rule-type" = "selection" + "rule-id" = "1" + "rule-name" = "1" + "object-locator" = { + "schema-name" = "dbo" + "table-name" = "%" + } + "rule-action" = "include" + } + ] + }) + +} \ No newline at end of file diff --git a/terraform/environments/tribunals/modules/dms/vars.tf b/terraform/environments/tribunals/modules/dms/vars.tf new file mode 100644 index 00000000000..e895597259e --- /dev/null +++ b/terraform/environments/tribunals/modules/dms/vars.tf @@ -0,0 +1,42 @@ +variable "replication_instance_arn" { +} + +variable "replication_task_id" { +} + +#################################################### + +variable "target_endpoint_id" { +} + +variable "target_database_name" { +} + +variable "target_server_name" { +} + +variable "target_username" { +} + +variable "target_password" { +} + +#################################################### + +variable "source_endpoint_id" { +} + +variable "source_database_name" { +} + +variable "source_server_name" { +} + +variable "source_username" { +} + +variable "source_password" { +} + + + diff --git a/terraform/environments/tribunals/modules/ecs_loadbalancer/main.tf b/terraform/environments/tribunals/modules/ecs_loadbalancer/main.tf new file mode 100644 index 00000000000..7844a7af3fb --- /dev/null +++ b/terraform/environments/tribunals/modules/ecs_loadbalancer/main.tf @@ -0,0 +1,225 @@ +resource "aws_security_group" "tribunals_lb_sc" { + name = "${var.app_name}-load-balancer-sg" + description = "${var.app_name} control access to the load balancer" + vpc_id = var.vpc_shared_id + + ingress { + description = "allow all traffic on HTTPS port 443" + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "allow all traffic on HTTP port 80" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + description = "allow all outbound traffic from the load balancer - needed due to dynamic port mapping on ec2 instance" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_security_group" "tribunals_lb_sc_sftp" { + count = var.is_ftp_app ? 1 : 0 + name = "${var.app_name}-load-balancer-sg-sftp" + description = "${var.app_name} control access to the network load balancer" + vpc_id = var.vpc_shared_id + + ingress { + description = "allow all traffic on HTTPS port 22" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "allow all traffic on port 10022" + from_port = 10022 + to_port = 10022 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + description = "allow all outbound traffic" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +//noinspection HILUnresolvedReference +resource "aws_lb" "tribunals_lb" { + name = "${var.app_name}-lb" + load_balancer_type = "application" + security_groups = [aws_security_group.tribunals_lb_sc.id] + subnets = var.subnets_shared_public_ids + enable_deletion_protection = false + internal = false + depends_on = [aws_security_group.tribunals_lb_sc] +} + +resource "aws_lb" "tribunals_lb_ftp" { + count = var.is_ftp_app ? 1 : 0 + name = "${var.app_name}-ftp-lb" + load_balancer_type = "network" + security_groups = [aws_security_group.tribunals_lb_sc_sftp[0].id] + subnets = var.subnets_shared_public_ids + enable_deletion_protection = false + internal = false + depends_on = [aws_security_group.tribunals_lb_sc_sftp[0]] +} + +resource "aws_lb_target_group" "tribunals_target_group" { + name = "${var.app_name}-tg" + port = 80 + protocol = "HTTP" + vpc_id = var.vpc_shared_id + target_type = "instance" + deregistration_delay = 30 + + stickiness { + type = "lb_cookie" + } + + health_check { + healthy_threshold = "3" + interval = "15" + protocol = "HTTP" + unhealthy_threshold = "3" + matcher = "200-499" + timeout = "10" + } + +} + +resource "aws_lb_target_group" "tribunals_target_group_sftp" { + count = var.is_ftp_app ? 1 : 0 + name = "${var.app_name}-sftp-tg" + port = 22 + protocol = "TCP" + vpc_id = var.vpc_shared_id + target_type = "instance" + deregistration_delay = 30 + + health_check { + healthy_threshold = "3" + interval = "15" + protocol = "TCP" + unhealthy_threshold = "3" + timeout = "10" + } +} + +resource "aws_lb_listener" "tribunals_lb" { + depends_on = [ + var.aws_acm_certificate_external + ] + certificate_arn = var.aws_acm_certificate_external.arn + load_balancer_arn = aws_lb.tribunals_lb.arn + port = var.application_data.server_port_2 + protocol = var.application_data.lb_listener_protocol_2 + ssl_policy = var.application_data.lb_listener_protocol_2 == "HTTP" ? "" : "ELBSecurityPolicy-2016-08" + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.tribunals_target_group.arn + } +} + +resource "aws_wafv2_web_acl_association" "web_acl_association_my_lb" { + resource_arn = aws_lb.tribunals_lb.arn + web_acl_arn = var.waf_arn +} + +resource "aws_lb_listener" "tribunals_lb_ftp" { + count = var.is_ftp_app ? 1 : 0 + load_balancer_arn = aws_lb.tribunals_lb_ftp[0].arn + port = 10022 + protocol = var.application_data.lb_listener_protocol_3 + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.tribunals_target_group_sftp[0].arn + } +} + +resource "aws_lb_listener" "tribunals_lb_health" { + load_balancer_arn = aws_lb.tribunals_lb.arn + port = "80" + protocol = "HTTP" + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.tribunals_target_group.arn + } +} + +resource "aws_lb_listener_rule" "admin_access" { + listener_arn = aws_lb_listener.tribunals_lb.arn + priority = 1 + action { + type = "forward" + target_group_arn = aws_lb_target_group.tribunals_target_group.arn + } + condition { + path_pattern { + values = ["*/Admin*", "*/admin*"] + } + } + + condition { + source_ip { + values = ["194.33.19*", "109.154.193.219/32", "82.1.119.170/32"] + } + } +} + +resource "aws_lb_listener_rule" "secure_access" { + listener_arn = aws_lb_listener.tribunals_lb.arn + priority = 2 + action { + type = "forward" + target_group_arn = aws_lb_target_group.tribunals_target_group.arn + } + condition { + path_pattern { + values = ["*/Secure*", "*/secure*"] + } + } + + condition { + source_ip { + values = ["194.33.19*", "109.154.193.219/32", "82.1.119.170/32"] + } + } +} + +resource "aws_lb_listener_rule" "admin_secure_fixed_response" { + listener_arn = aws_lb_listener.tribunals_lb.arn + priority = 3 + action { + type = "fixed-response" + fixed_response { + content_type = "text/html" + message_body = "

Secure Page

This area of the website now requires elevated security.


If you believe you should be able to access this page please send an email to: - dts-legacy-apps-support-team@hmcts.net

" + status_code = "403" + } + } + condition { + path_pattern { + values = ["/Admin*", "/admin*", "/Secure*", "/secure*"] + } + } +} diff --git a/terraform/environments/tribunals/modules/ecs_loadbalancer/outputs.tf b/terraform/environments/tribunals/modules/ecs_loadbalancer/outputs.tf new file mode 100644 index 00000000000..8defc3c5ea6 --- /dev/null +++ b/terraform/environments/tribunals/modules/ecs_loadbalancer/outputs.tf @@ -0,0 +1,34 @@ +output "tribunals_lb_sc_id" { + description = "LB Security Group Id" + value = aws_security_group.tribunals_lb_sc.id +} + +output "tribunals_target_group_arn" { + description = "LB Target Group arn" + value = aws_lb_target_group.tribunals_target_group.arn +} + +output "tribunals_lb_listener" { + description = "LB Listener" + value = aws_lb_listener.tribunals_lb +} + +output "tribunals_lb" { + description = "LB" + value = aws_lb.tribunals_lb +} + +output "tribunals_lb_ftp" { + description = "SFTP LB" + value = aws_lb.tribunals_lb_ftp +} + +output "sftp_tribunals_target_group_arn" { + description = "Network Load Balancer Target Group ARN for SFTP connections" + value = length(aws_lb_target_group.tribunals_target_group_sftp) > 0 ? aws_lb_target_group.tribunals_target_group_sftp[0].arn : "" +} + +output "tribunals_lb_sc_id_sftp" { + description = "Network LB Security Group Id for sftp" + value = length(aws_security_group.tribunals_lb_sc_sftp) > 0 ? aws_security_group.tribunals_lb_sc_sftp[0].id : "" +} diff --git a/terraform/environments/tribunals/modules/ecs_loadbalancer/variables.tf b/terraform/environments/tribunals/modules/ecs_loadbalancer/variables.tf new file mode 100644 index 00000000000..c8a410ffc80 --- /dev/null +++ b/terraform/environments/tribunals/modules/ecs_loadbalancer/variables.tf @@ -0,0 +1,28 @@ +variable "app_name" { + type = string + description = "Name of the application" +} + +variable "tags_common" { + type = map(string) + description = "Common tags to be used by all resources" +} + +variable "vpc_shared_id" { +} + +variable "application_data" { +} + +variable "subnets_shared_public_ids" { +} + +variable "aws_acm_certificate_external" { +} + +variable "is_ftp_app" { + description = "Determines if it is an ftp app or not" +} + +variable "waf_arn" { +} \ No newline at end of file diff --git a/terraform/environments/tribunals/modules/ecs_task/main.tf b/terraform/environments/tribunals/modules/ecs_task/main.tf new file mode 100644 index 00000000000..c9469337bdc --- /dev/null +++ b/terraform/environments/tribunals/modules/ecs_task/main.tf @@ -0,0 +1,236 @@ +resource "aws_ecs_task_definition" "ecs_task_definition" { + family = "${var.app_name}Family" + execution_role_arn = aws_iam_role.ecs_task_execution_role.arn + task_role_arn = aws_iam_role.ecs_task_execution_role.arn + requires_compatibilities = [ + "EC2", + ] + + volume { + name = var.task_definition_volume + host_path = "D:/storage/tribunals/${var.app_name}" + } + + container_definitions = var.container_definition + + runtime_platform { + operating_system_family = "WINDOWS_SERVER_2019_CORE" + # cpu_architecture = "X86_64" + } + + tags = merge( + var.tags_common, + { + Name = "${var.app_name}-windows-task-definition" + } + ) +} + + +# ECS task execution role data +# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html +data "aws_iam_policy_document" "ecs_task_execution_role" { + version = "2012-10-17" + statement { + sid = "" + effect = "Allow" + actions = [ + "sts:AssumeRole", + ] + + principals { + type = "Service" + identifiers = [ + "ecs-tasks.amazonaws.com", + ] + } + } +} + +resource "aws_iam_policy" "ecs_task_execution_s3_policy" { #tfsec:ignore:aws-iam-no-policy-wildcards + name = "${var.app_name}-ecs-task-execution-s3-policy" + tags = merge( + var.tags_common, + { + Name = "${var.app_name}-ecs-task-execution-s3-policy" + } + ) + policy = < 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(varchar(255), name) from sysobjects where type = 'P' and name not like 'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName varchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId nvarchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(nvarchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(nvarchar(255), name) from sysobjects where type = N'P' and name not like N'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName nvarchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText varchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText nvarchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode nvarchar(255)) + + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select N'VCS'=sourcecode from #commenttext order by id + select N'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput nvarchar(255) +declare @hr int +declare @vchSource nvarchar(255) +declare @vchDescription nvarchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select uvalue + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID nvarchar(100) + select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText nvarchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR(N'Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist nvarchar(255)) + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the Microsoft Visual Database Tools. +** Current version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue nvarchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + N'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + + + +CREATE PROCEDURE [dbo].[spAddCategory] + +@description varchar(100) + +AS + +INSERT + Category + ( + [description] + ) + +VALUES + ( + @description + ) + +go + + + +CREATE PROCEDURE [dbo].[spAddCommissioner] + +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +INSERT + Commissioner + ( + prefix, + surname, + suffix + ) + +VALUES + ( + @Prefix, + @surname, + @suffix + ) +go + + +CREATE PROCEDURE [dbo].[spAddCommissionerJudgmentMap] + +@CommissionerID int, +@JudgmentID int + +AS + +INSERT + CommissionerJudgmentMap + ( + Commissioner_ID, + Judgment_ID + ) + + VALUES + ( + @CommissionerID, + @JudgmentID + ) +go + + + + +CREATE PROCEDURE [dbo].[spAddJudgment] + +@Id int OUTPUT, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(4), +@Reported_no_1 varchar(3), +@Reported_no_2 varchar(3), +@Reported_no_3 varchar(2), +@NCN_year int, +@NCN_code1 varchar(20), +@NCN_citation int, +@NCN_code2 varchar(20) + +AS + +DECLARE @Publication_datetime datetime + +IF @Is_published = 1 + SELECT @Publication_datetime = getdate() + + + INSERT + Judgment + ( + Is_published, + File_no_1, + File_no_2, + Decision_datetime, + Claimants, + Respondent, + Main_subcategory_id, + Sec_subcategory_id, + Headnote_summary, + Created_datetime, + Last_updatedtime, + Publication_datetime, + File_no_3, + Reported_no_1, + Reported_no_2, + Reported_no_3, + ncn_year, + ncn_code1, + ncn_citation, + ncn_code2 + ) + + VALUES + ( + @Is_published, + @File_no_1, + @File_no_2, + @Decision_datetime, + @Claimants, + @Respondent, + @Main_subcategory_id, + @Sec_subcategory_id, + @Headnote_summary, + getdate(), + getdate(), + @Publication_datetime, + @File_no_3, + @Reported_no_1, + @Reported_no_2, + @Reported_no_3, + @NCN_year, + @NCN_code1, + @NCN_citation, + @NCN_code2 + + ) + + SELECT @Id = SCOPE_IDENTITY() +go + + + +CREATE PROCEDURE [dbo].[spAddSubCategory] + +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +INSERT + Subcategory + ( + parent_num, + [description], + num + ) + +VALUES + ( + @parent_num, + @description, + @num + ) + +go + + +CREATE PROCEDURE [dbo].[spAddUser] + +@UserID integer OUTPUT, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +INSERT + Users + + ( + Username, + [Password], + Firstname, + Lastname + ) + +VALUES + + ( + @Username, + @Password, + @Firstname, + @Lastname + ) + +SELECT @UserID = SCOPE_IDENTITY() +go + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsByCommissioner] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + CommissionerJudgmentMap + +WHERE + commissioner_id = @Id +go + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsBySubCategory] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + Judgment + +WHERE + main_subcategory_id = @Id + + OR + + sec_subcategory_id = @Id + +go + + + +CREATE PROCEDURE [dbo].[spCountSubCategoryByCategory] + +@CategoryId int + +AS + +SELECT + COUNT(*) + +FROM + Subcategory + +WHERE + parent_num = @CategoryId + +go + + + +CREATE PROCEDURE [dbo].[spDeleteCategory] + +@Id int + +AS + +DELETE + Category + +WHERE + Num = @Id + +go + + + +CREATE PROCEDURE [dbo].[spDeleteCommissioner] + +@Id int + +AS + +DELETE + Commissioner + +WHERE + [Id] = @Id +go + + +CREATE PROCEDURE [dbo].[spDeleteCommissionerJudgmentMap] + +@JudgmentID int + +AS + +DELETE + CommissionerJudgmentMap + +WHERE + Judgment_Id = @JudgmentID +go + + + +CREATE PROCEDURE [dbo].[spDeleteSubCategory] + +@Id int + +AS + +DELETE + SubCategory + +WHERE + [Id] = @Id + +go + + +CREATE PROCEDURE [dbo].[spDeleteUser] + +@UserID int + +AS + +DECLARE +@Count int + +SELECT @Count = COUNT(*) FROM Users + +IF @Count > 1 + BEGIN + DELETE + Users + + WHERE + UserID = @UserID + END +go + + + + +CREATE PROCEDURE [dbo].[spGetCategoryList] + +AS + +SELECT num, [description] +FROM category +ORDER BY category.[description] +go + + +CREATE PROCEDURE [dbo].[spGetCommissionerList] + +AS + +select [id], prefix, surname, suffix +from commissioner +order by surname +go + + +CREATE PROCEDURE [dbo].[spGetCommissionerListSelected] + +@DecisionId int + +AS + +select [id], prefix, surname, suffix, isnull(m.commid, 0) as commid +from commissioner c +left join + ( + select commissioner_id as commid + from commissionerjudgmentmap cjm + where judgment_id = @DecisionId + ) as m +on c.[id] = m.commid +order by commid desc, c.surname +go + + + +CREATE PROCEDURE [dbo].[spGetDecision] + +@DecisionId int + +AS + +-- Main result set - ds.Tables(0) +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid, + j.file_no_1 + ' ' + j.file_no_2 + ' ' + j.file_no_3 as 'filenumber' +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId + +-- Selected commissioners result set - ds.Tables(1) +select cm.prefix, cm.surname, cm.suffix, cm.[id] as 'commissionerid', + cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as 'commissioner' +from judgment j +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +where cjm.judgment_id = @DecisionId +go + + + + +CREATE PROCEDURE [dbo].[spGetDecisionPublished] + +@DecisionId int, +@Is_Published bit + +AS + +-- Main result set - ds.Tables(0) +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid, + j.file_no_1 + ' ' + j.file_no_2 + ' ' + j.file_no_3 as 'filenumber', + cast( j.ncn_year as varchar(4)) + ' ' + j.ncn_code1 + ' ' + cast( j.ncn_citation as varchar(6)) + ' ' + j.ncn_code2 as ncn_number + +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +and is_published = @Is_Published + +-- Selected commissioners result set - ds.Tables(1) +select cm.prefix, cm.surname, cm.suffix, cm.[id] as 'commissionerid', + cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as 'commissioner' +from judgment j +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +where cjm.judgment_id = @DecisionId +go + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryList] + +AS + +SELECT [id], parent_num, num, [description] +FROM subcategory +ORDER BY parent_num, num + +go + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] + +@CategoryId int + +AS + +SELECT [id], parent_num, s.num, s.[description], c.[description] as categoryname +FROM subcategory s +inner join category c on s.parent_num = c.num +WHERE parent_num = @CategoryId +ORDER BY s. num + +go + + +CREATE PROCEDURE [dbo].[spGetUser] + +@UserID int + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +WHERE + UserID = @UserID +go + + +CREATE PROCEDURE [dbo].[spGetUserList] + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users +go + + + +CREATE PROCEDURE [dbo].[spLoginUser] + +@Username varchar(50), +@Password varchar(50) + +AS + +SELECT + * + +FROM + Users + +WHERE + Username = @Username + +AND + [Password] = @Password + +go + + + +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@id int, +@description varchar(100) + +AS + +UPDATE + Category + +SET + [description] = @description + +WHERE + num = @id + +go + + + +CREATE PROCEDURE [dbo].[spUpdateCommissioner] + +@id int, +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +UPDATE + Commissioner + +SET + prefix = @Prefix, + surname = @surname, + suffix = @suffix + +WHERE + [id] = @id +go + + +CREATE PROCEDURE [dbo].[spUpdateCommissionerJudgmentMap] + +@CommissionerID int, +@JudgmentID int + +AS + +UPDATE + CommissionerJudgmentMap + +SET + Commissioner_ID = @CommissionerID + +WHERE + Judgment_ID = @JudgmentID + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateJudgment] + +@Id int, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(4), +@Reported_no_1 varchar(3), +@Reported_no_2 varchar(3), +@Reported_no_3 varchar(2), +@NCN_year int, +@NCN_code1 varchar(20), +@NCN_citation int, +@NCN_code2 varchar(20) +AS + +DECLARE +@Publication_datetime datetime, +@CurrentlyPublished bit + +SELECT @CurrentlyPublished = Is_Published FROM Judgment WHERE [id] = @Id + +IF (@CurrentlyPublished = 0 AND @Is_published = 1) + BEGIN + SELECT @Publication_datetime = getdate() + END + +IF @CurrentlyPublished = 1 + BEGIN + SELECT @Publication_datetime = Publication_datetime FROM Judgment WHERE [id] = @Id + END + +IF @Is_published = 0 + BEGIN + SELECT @Publication_datetime = NULL + END + + + UPDATE + Judgment + + SET + Is_Published = @Is_Published, + File_no_1 = @File_no_1, + File_no_2 = @File_no_2, + File_no_3 = @File_no_3, + Decision_datetime = @Decision_datetime, + Claimants = @Claimants, + Respondent = @Respondent, + Main_subcategory_id = @Main_subcategory_id, + Sec_subcategory_id = @Sec_subcategory_id, + Headnote_summary = @Headnote_summary, + Last_updatedtime = getdate(), + Publication_datetime = @Publication_datetime, + Reported_no_1 = @Reported_no_1, + Reported_no_2 = @Reported_no_2, + Reported_no_3 = @Reported_no_3, + ncn_year = @NCN_year, + ncn_code1= @NCN_code1, + ncn_citation = @NCN_citation, + ncn_code2 = @NCN_code2 + + WHERE + [Id] = @Id +go + + + +CREATE PROCEDURE [dbo].[spUpdateSubCategory] + +@id int, +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +UPDATE + Subcategory + +SET + parent_num = @parent_num, + [description] = @description, + num = @num + +WHERE + [id] = @id + +go + + +CREATE PROCEDURE [dbo].[spUpdateUser] + +@UserID int, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +UPDATE + Users + +SET + Username = @Username, + Firstname = @Firstname, + Lastname = @Lastname + +WHERE + UserID = @UserID + +-- Update the password ONLY if provided +IF @Password IS NOT NULL AND 0 < LEN(@Password) + UPDATE Users + SET [Password] = @Password + WHERE UserID = @UserID +go + diff --git a/terraform/environments/tribunals/scripts/ahmlr/sp_migration.sql b/terraform/environments/tribunals/scripts/ahmlr/sp_migration.sql new file mode 100644 index 00000000000..cde280c0226 --- /dev/null +++ b/terraform/environments/tribunals/scripts/ahmlr/sp_migration.sql @@ -0,0 +1,1683 @@ +use hmlands +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + select @iObjectCount = 0; + +CleanUp: + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + -- This procedure should no longer be called; dt_addtosourcecontrol should be called instead. + -- Calls are forwarded to dt_addtosourcecontrol to maintain backward compatibility + set nocount on + exec dbo.dt_addtosourcecontrol + @vchSourceSafeINI, + @vchProjectName, + @vchComment, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', /* create stream */ + @txStream3 Text = '' /* grant stream */ + + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + declare @iStreamObjectId int + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + declare @iReturnValue int + declare @pos int + declare @vchProcLinePiece varchar(255) + + + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + + declare @iStreamLength int + + select @pos=1 + select @iStreamLength = datalength(@txStream2) + + if @iStreamLength > 0 + begin + + while @pos < @iStreamLength + begin + + select @vchProcLinePiece = substring(@txStream2, @pos, 255) + + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + + select @pos = @pos + 255 + + end + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + + end + end + else + begin + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 text = '', /* create stream */ + @txStream3 text = '' /* grant stream */ + +as + -- This procedure should no longer be called; dt_checkinobject should be called instead. + -- Calls are forwarded to dt_checkinobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkinobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag, + @txStream1, + @txStream2, + @txStream3 + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId =0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @vchTempText varchar(255) + + /* this is for our strings */ + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + -- This procedure should no longer be called; dt_checkoutobject should be called instead. + -- Calls are forwarded to dt_checkoutobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkoutobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = master.dbo.sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + -- This procedure should no longer be called; dt_displayoaerror should be called instead. + -- Calls are forwarded to dt_displayoaerror to maintain backward compatibility. + set nocount on + exec dbo.dt_displayoaerror + @iObject, + @iresult + + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + -- This procedure should no longer be called; dt_getpropertiesbyid_vcsshould be called instead. + -- Calls are forwarded to dt_getpropertiesbyid_vcs to maintain backward compatibility. + set nocount on + exec dbo.dt_getpropertiesbyid_vcs + @id, + @property, + @value output + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName = '') set @vchProjectName = null + if (@vchSourceSafeINI = '') set @vchSourceSafeINI = null + if (@vchServerName = '') set @vchServerName = null + if (@vchDatabaseName = '') set @vchDatabaseName = null + + if (@vchProjectName is null) or (@vchSourceSafeINI is null) or (@vchServerName is null) or (@vchDatabaseName is null) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + -- This procedure should no longer be called; dt_isundersourcecontrol should be called instead. + -- Calls are forwarded to dt_isundersourcecontrol to maintain backward compatibility. + set nocount on + exec dbo.dt_isundersourcecontrol + @vchLoginName, + @vchPassword, + @iWhoToo + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + + -- This procedure should no longer be called; dt_validateloginparams should be called instead. + -- Calls are forwarded to dt_validateloginparams to maintain backward compatibility. + set nocount on + exec dbo.dt_validateloginparams + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by legacy versions of the Microsoft +** Visual Database Tools. Version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the the Microsoft Visual Database Tools. +** Version is 7.0.05. +*/ +create procedure dbo.dt_verstamp007 +as + select 7005 +go + +grant execute on dbo.dt_verstamp007 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + + -- This procedure should no longer be called; dt_whocheckedout should be called instead. + -- Calls are forwarded to dt_whocheckedout to maintain backward compatibility. + set nocount on + exec dbo.dt_whocheckedout + @chObjectType, + @vchObjectName, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + +-- adds category +CREATE PROCEDURE [dbo].[spAddCat] + +@catID int OUTPUT, +@catdescription varchar(200) + +AS BEGIN TRANSACTION + +INSERT INTO CATEGORY (catdescription) +VALUES (@catdescription) +SELECT @catID = SCOPE_IDENTITY() + +INSERT INTO CATEGORY1 (catdescription) +VALUES (@catdescription) +SELECT @catID = SCOPE_IDENTITY() + +IF @@ERROR <>0 +BEGIN ROLLBACK TRANSACTION +RETURN 1 +END + +COMMIT TRANSACTION +go + +-- adds deputy adjudicator +CREATE PROCEDURE [dbo].[spAddDepAdj] + +@depadjID int OUTPUT, +@depadjtitle varchar(50), +@depadjfirst varchar(100), +@depadjsecond varchar(100) + +AS INSERT INTO DEPUTYADJUDICATORS (depadjtitle,depadjfirst,depadjsecond) +VALUES (@depadjtitle,@depadjfirst,@depadjsecond) +SELECT @depadjID = SCOPE_IDENTITY() + +go + +-- adds sub category +CREATE PROCEDURE [dbo].[spAddSubCat] + +@catID int OUTPUT, +@subdescription varchar(200), +@num int + +AS BEGIN TRANSACTION +INSERT INTO SUBCATEGORY (catID,subdescription,num) +VALUES (@catID,@subdescription,@num) + +INSERT INTO SUBCATEGORY1 (catID,subdescription,num) +VALUES (@catID,@subdescription,@num) + +IF @@ERROR<>0 +BEGIN +ROLLBACK TRANSACTION +RETURN 1 +END + +COMMIT TRANSACTION +go + +-- adds user +CREATE PROCEDURE [dbo].[spAddUser] + +@userID int OUTPUT, +@firstname varchar(250), +@lastname varchar(250), +@username varchar(250), +@password varchar(250) + +AS INSERT INTO USERS (firstname,lastname,username,[password]) +VALUES (@firstname,@lastname,@username,@password) +SELECT @userID = SCOPE_IDENTITY() +go + +CREATE PROCEDURE [dbo].[spAdjList] +AS +SELECT [depadjID],[depadjtitle],[depadjfirst],[depadjsecond] FROM DEPUTYADJUDICATORS +ORDER BY depadjID +go + +-- delete category +CREATE PROCEDURE [dbo].[spDeleteCat] + +@catID int --OUTPUT + +AS BEGIN TRANSACTION + +DELETE FROM CATEGORY WHERE catID = @catID +--SELECT @catID = SCOPE_IDENTITY() + +DELETE FROM CATEGORY1 WHERE catID = @catID +--SELECT @catID = SCOPE_IDENTITY() + +IF @@ERROR <>0 +BEGIN +ROLLBACK TRANSACTION +RETURN 1 +END + +COMMIT TRANSACTION +go + +-- delete decision +CREATE PROCEDURE [dbo].[spDeleteDecision] + +@id int OUTPUT, +@caseref1 varchar(50), +@caseref2 varchar(50), +@caseref3 varchar(50), +@hearingdate datetime, +@depadj int, +@applicant varchar(200), +@respondent varchar(200), +@cat1 int, +@subcat1 int, +@cat2 int, +@subcat2 int, +@notes ntext + +AS DELETE FROM DECISIONS + +WHERE [id] = @id + +go + +-- delete deputy adjudicator +CREATE PROCEDURE [dbo].[spDeleteDepAdj] + +@depadjID int OUTPUT, +@depadjtitle varchar(50), +@depadjfirst varchar(100), +@depadjsecond varchar(100) + +AS DELETE FROM DEPUTYADJUDICATORS WHERE depadjID = @depadjID +SELECT @depadjID = SCOPE_IDENTITY() + +go + +-- delete sub category +CREATE PROCEDURE [dbo].[spDeleteSubCat] + +@ID int + +AS BEGIN TRANSACTION + +DELETE SUBCATEGORY WHERE subcatID = @ID + +DELETE SUBCATEGORY1 WHERE subcatID = @ID + +IF @@ERROR <>0 +BEGIN +ROLLBACK TRANSACTION +RETURN 1 +END + +COMMIT TRANSACTION +go + +-- delete user +CREATE PROCEDURE [dbo].[spDeleteUser] + +@userID int + +AS DECLARE @Count int +SELECT @Count = Count(*) FROM USERS +IF @Count >1 BEGIN DELETE USERS WHERE userID = @userID END + +go + +-- gets all decisions +CREATE PROCEDURE [dbo].[spGetAllDecisions] AS + +SELECT [id],[caseref1],[caseref2],[caseref3], +CONVERT(char(24),[hearingdate],103) AS [hearingdate],[depadj],[applicant],[respondent],[cat1],[subcat1],[cat2],[subcat2],[depadjtitle], +[depadjfirst],[depadjsecond],[catdescription],[subdescription],[hearingdate] AS HD +FROM DECISIONS AS d +inner join DeputyAdjudicators AS da on (d.depadj = da.depadjID) +inner join Category AS cat on (d.cat1 = cat.catID) +inner join SubCategory AS scat on (d.subcat1 = scat.subcatID) +ORDER BY HD DESC +go + +-- returns list of categories from the catergory1 table +CREATE PROCEDURE [dbo].[spGetCat1List] AS + +SELECT [catID],[catdescription] FROM CATEGORY1 +ORDER BY catID +go + +-- returns list of categories from the category table +CREATE PROCEDURE [dbo].[spGetCatList] AS + +SELECT [catID],[catdescription] FROM CATEGORY +ORDER BY catID +go + +-- get a decision based on it ID for the decision summary page +CREATE PROCEDURE dbo.spGetDecisionByID + +@id int + +AS + +SELECT id, caseref1, caseref2, caseref3, hearingdate, depadjtitle, depadjfirst, depadjsecond, applicant, respondent, d.depadj, d.cat1, d.subcat1, d.cat2, d.subcat2, +c.catdescription AS firstCategory, +s.subdescription AS fisrtSubCategory, +cc.catdescription AS secondCategory, +ss.subdescription AS secondSubCategory, +notes + +FROM DECISIONS AS d + +INNER JOIN DEPUTYADJUDICATORS AS da ON (d.depadj = da.depadjID) + +INNER JOIN CATEGORY AS c ON (d.cat1 = c.catID) +INNER JOIN SUBCATEGORY AS s ON (d.subcat1 = s.subcatID) + +INNER JOIN CATEGORY1 AS cc ON (d.cat2 = cc.catID) +INNER JOIN SUBCATEGORY1 AS ss ON (d.subcat2 = ss.subcatID) + +WHERE [id] = @id + +go + +-- get a decisions details based on its ID the result is used to populate the edit page for updating +CREATE PROCEDURE [dbo].[spGetDecisionForUpdate] + +@decisionID int + +AS SELECT id,caseref1,caseref2,caseref3,hearingdate,depadjtitle,depadjfirst,depadjsecond,applicant,respondent,cat1,subcat1,cat2,subcat2,notes +FROM DECISIONS AS d +INNER JOIN DEPUTYADJUDICATORS AS da ON (d.depadj = da.depadjID) + +WHERE id = @decisionID +go + +-- returns deput adjudicators +CREATE PROCEDURE [dbo].[spGetDepAdjList] + +AS +SELECT depadjID,depadjtitle,depadjfirst,depadjsecond +FROM DEPUTYADJUDICATORS + +go + +-- returns list of sub categories from the subcategory1 table +CREATE PROCEDURE [dbo].[spGetSubCat1List] AS + +SELECT [subcatID],[catID],[subdescription],[num] FROM SUBCATEGORY1 +ORDER BY subcatID +go + +CREATE PROCEDURE [dbo].[spGetSubCatByCatID] + +@catID int + +AS + +SELECT sc.subdescription, sc.subcatID,sc.catID, num +FROM SUBCATEGORY AS sc INNER JOIN CATEGORY AS c +ON sc.catID = c.catID +WHERE sc.catID = @catID + +go + +-- returns list of sub categories from the category table +CREATE PROCEDURE [dbo].[spGetSubCatList] AS + +SELECT [subcatID],[catID],[subdescription],[num] FROM SUBCATEGORY +ORDER BY subcatID +go + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] + +@catID int + +AS + +SELECT subcatID, s.catID, s.num, s.subdescription, c.catID, c.catdescription as categoryname +FROM SUBCATEGORY s +inner join CATEGORY c on s.catID = c.catID +WHERE s.catID = @catID +ORDER BY s.num + +go + +CREATE PROCEDURE [dbo].[spGetUser] + +@userID int + +AS +SELECT userID, username, [password], firstname, lastname FROM USERS WHERE userID = @userID + +go + +-- returns list of users +CREATE PROCEDURE [dbo].[spGetUserList] + +AS SELECT userID,firstname,lastname,username,[password] FROM USERS + +ORDER BY firstname +go + +-- insert decision +CREATE PROCEDURE [dbo].[spInsertDecision] + +@id int OUTPUT, +@caseref1 varchar(50), +@caseref2 varchar(50), +@caseref3 varchar(50), +@hearingdate datetime, +@depadj int, +@applicant varchar(200), +@respondent varchar(200), +@cat1 int, +@subcat1 int, +@cat2 int, +@subcat2 int, +@notes ntext + +AS BEGIN TRANSACTION +INSERT INTO DECISIONS +(caseref1,caseref2,caseref3,hearingdate,depadj,applicant,respondent,cat1,subcat1,cat2,subcat2,notes) +VALUES +(@caseref1,@caseref2,@caseref3,@hearingdate,@depadj,@applicant,@respondent,@cat1,@subcat1,@cat2,@subcat2,@notes) +IF @@ERROR<>0 +BEGIN ROLLBACK TRANSACTION +RETURN 1 END + +SELECT @id = SCOPE_IDENTITY() + +COMMIT TRANSACTION + +go + +CREATE PROCEDURE [dbo].[spLoginUser] + +@username varchar (250), +@password varchar (250) + +AS + +SELECT * FROM USERS + +WHERE username = @username AND [password] = @password +go + +-- update category +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@catID int, +@catdescription varchar(200) + +AS BEGIN TRANSACTION +UPDATE CATEGORY +SET catdescription = @catdescription +WHERE catID = @catID + +UPDATE CATEGORY1 +SET catdescription = @catdescription +WHERE catID = @catID + +IF @@ERROR<>0 +BEGIN +ROLLBACK TRANSACTION +RETURN 1 +END + +COMMIT TRANSACTION +go + +-- update decision +CREATE PROCEDURE [dbo].[spUpdateDecision] + +@id int, +@caseref1 varchar(50), +@caseref2 varchar(50), +@caseref3 varchar(50), +@hearingdate datetime, +@depadj int, +@applicant varchar(200), +@respondent varchar(200), +@cat1 int, +@subcat1 int, +@cat2 int, +@subcat2 int, +@notes ntext + +AS UPDATE DECISIONS SET + +caseref1 = @caseref1, +caseref2 = @caseref2, +caseref3 = @caseref3, +hearingdate = @hearingdate, +depadj = @depadj, +applicant = @applicant, +respondent = @respondent, +cat1 = @cat1, +subcat1 = @subcat1, +cat2 = @cat2, +subcat2 = @subcat2, +notes = @notes + +WHERE [id] = @id + +go + +-- update deputy adjudicators entry +CREATE PROCEDURE [dbo].[spUpdateDepAdj] + +@depadjID int, +@depadjtitle varchar(50), +@depadjfirst varchar(100), +@depadjsecond varchar(100) + +AS UPDATE DEPUTYADJUDICATORS +SET depadjtitle = @depadjtitle,depadjfirst = @depadjfirst,depadjsecond = @depadjsecond +WHERE depadjID = @depadjID + +go + +-- update sub category +CREATE PROCEDURE [dbo].[spUpdateSubCategory] + +@subcatID int, +@subdescription varchar(200), +@num int + +AS BEGIN TRANSACTION +UPDATE SUBCATEGORY +SET subdescription = @subdescription, num = @num +WHERE subcatID = @subcatID + +UPDATE SUBCATEGORY1 +SET subdescription = @subdescription, num = @num +WHERE subcatID = @subcatID + +IF @@ERROR<>0 +BEGIN +ROLLBACK TRANSACTION +RETURN 1 +END + +COMMIT TRANSACTION +go + +-- update users entry +CREATE PROCEDURE [dbo].[spUpdateUser] + +@userID int, +@firstname varchar(250), +@lastname varchar(250), +@username varchar(250), +@password varchar(250) + +AS UPDATE USERS +SET firstname = @firstname,lastname=@lastname,username = @username +WHERE userID = @userID + +-- update the password if provided +IF @password IS NOT NULL AND 0 < LEN(@password) +UPDATE USERS SET [password] = @password +WHERE userID = @userID +go + diff --git a/terraform/environments/tribunals/scripts/care_standards/sp_migration.sql b/terraform/environments/tribunals/scripts/care_standards/sp_migration.sql new file mode 100644 index 00000000000..eb490b51eb7 --- /dev/null +++ b/terraform/environments/tribunals/scripts/care_standards/sp_migration.sql @@ -0,0 +1,2075 @@ +use carestandards +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + select @iObjectCount = 0; + +CleanUp: + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + -- This procedure should no longer be called; dt_addtosourcecontrol should be called instead. + -- Calls are forwarded to dt_addtosourcecontrol to maintain backward compatibility + set nocount on + exec dbo.dt_addtosourcecontrol + @vchSourceSafeINI, + @vchProjectName, + @vchComment, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', /* create stream */ + @txStream3 Text = '' /* grant stream */ + + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + declare @iStreamObjectId int + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + declare @iReturnValue int + declare @pos int + declare @vchProcLinePiece varchar(255) + + + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + + declare @iStreamLength int + + select @pos=1 + select @iStreamLength = datalength(@txStream2) + + if @iStreamLength > 0 + begin + + while @pos < @iStreamLength + begin + + select @vchProcLinePiece = substring(@txStream2, @pos, 255) + + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + + select @pos = @pos + 255 + + end + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + + end + end + else + begin + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 text = '', /* create stream */ + @txStream3 text = '' /* grant stream */ + +as + -- This procedure should no longer be called; dt_checkinobject should be called instead. + -- Calls are forwarded to dt_checkinobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkinobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag, + @txStream1, + @txStream2, + @txStream3 + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId =0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @vchTempText varchar(255) + + /* this is for our strings */ + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + -- This procedure should no longer be called; dt_checkoutobject should be called instead. + -- Calls are forwarded to dt_checkoutobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkoutobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = master.dbo.sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + -- This procedure should no longer be called; dt_displayoaerror should be called instead. + -- Calls are forwarded to dt_displayoaerror to maintain backward compatibility. + set nocount on + exec dbo.dt_displayoaerror + @iObject, + @iresult + + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + -- This procedure should no longer be called; dt_getpropertiesbyid_vcsshould be called instead. + -- Calls are forwarded to dt_getpropertiesbyid_vcs to maintain backward compatibility. + set nocount on + exec dbo.dt_getpropertiesbyid_vcs + @id, + @property, + @value output + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName = '') set @vchProjectName = null + if (@vchSourceSafeINI = '') set @vchSourceSafeINI = null + if (@vchServerName = '') set @vchServerName = null + if (@vchDatabaseName = '') set @vchDatabaseName = null + + if (@vchProjectName is null) or (@vchSourceSafeINI is null) or (@vchServerName is null) or (@vchDatabaseName is null) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + -- This procedure should no longer be called; dt_isundersourcecontrol should be called instead. + -- Calls are forwarded to dt_isundersourcecontrol to maintain backward compatibility. + set nocount on + exec dbo.dt_isundersourcecontrol + @vchLoginName, + @vchPassword, + @iWhoToo + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + + -- This procedure should no longer be called; dt_validateloginparams should be called instead. + -- Calls are forwarded to dt_validateloginparams to maintain backward compatibility. + set nocount on + exec dbo.dt_validateloginparams + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by legacy versions of the Microsoft +** Visual Database Tools. Version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the the Microsoft Visual Database Tools. +** Version is 7.0.05. +*/ +create procedure dbo.dt_verstamp007 +as + select 7005 +go + +grant execute on dbo.dt_verstamp007 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + + -- This procedure should no longer be called; dt_whocheckedout should be called instead. + -- Calls are forwarded to dt_whocheckedout to maintain backward compatibility. + set nocount on + exec dbo.dt_whocheckedout + @chObjectType, + @vchObjectName, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + +CREATE PROCEDURE [dbo].[spAddCategory] + +@description varchar(100) + +AS + +INSERT + Category + ( + [description] + ) + +VALUES + ( + @description + ) + + +go + +CREATE PROCEDURE [dbo].[spAddChairman] + +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +INSERT + Chairman + ( + prefix, + surname, + suffix + ) + +VALUES + ( + @Prefix, + @surname, + @suffix + ) + + +go + +CREATE PROCEDURE [dbo].[spAddChairmanDecisionMap] + +@ChairmanID int, +@DecisionID int + +AS + +INSERT + ChairmanDecisionMap + ( + Chairman_ID, + Decision_ID + ) + + VALUES + ( + @ChairmanID, + @DecisionID + ) + + +go + +CREATE PROCEDURE [dbo].[spAddDecision] + + @id int OUTPUT, + @file_no_1 varchar(50), + @file_no_2 varchar(50), + @file_no_3 varchar(50), + @decision_datetime datetime, + @appellant varchar(500), + @respondent varchar(500), +@chairman_id bigint, + @schedule_id bigint, + @main_category_id bigint, + @main_subcategory_id bigint, + @headnote_summary ntext, + @Is_public bit +AS + +DECLARE @publication_datetime datetime + +IF @Is_public = 1 + SELECT @publication_datetime = getdate() + + + +INSERT + DECISION + ( + + file_no_1, + file_no_2, + file_no_3, + decision_datetime, + appellant, + respondent, + chairman_id, + schedule_id, + main_category_id, + main_subcategory_id, + headnote_summary, + created_datetime, + last_updatedtime, + publication_datetime, + Is_public + + ) + VALUES + ( + + @file_no_1, + @file_no_2, + @file_no_3, + @decision_datetime, + @appellant, + @respondent, + @chairman_id, + @schedule_id, + @main_category_id, + @main_subcategory_id, + @headnote_summary, + getdate(), + getdate(), + --getdate(), + @publication_datetime, + @Is_public + + ) +SELECT @id = SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spAddSchedule + +@description varchar(100) + +AS + +INSERT + Schedule + ( + [description] + ) + +VALUES + ( + @description + ) + + +go + +CREATE PROCEDURE [dbo].[spAddSubCategory] +@parent_num tinyint, +@description varchar(100), +@num tinyint +AS +INSERT + Subcategory + ( + parent_num, + [description], + num + ) +VALUES + ( + @parent_num, + @description, + @num + ) + +go + + + +CREATE PROCEDURE [dbo].[spAddUser] + +@UserID integer OUTPUT, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +INSERT + Users + + ( + Username, + [Password], + Firstname, + Lastname + ) + +VALUES + + ( + @Username, + @Password, + @Firstname, + @Lastname + ) + +SELECT @UserID = SCOPE_IDENTITY() + + +go + +CREATE PROCEDURE dbo.spAllDecisionPublicArea +@CurrentPage int, + @PageSize int, + @TotalRecords int output + AS +-- Create a temp table to hold the current page of data +-- Add and ID Column to count the decisions +Create TABLE #TempTable +( +ID int PRIMARY KEY, +decision_datetime datetime, +file_no_1 varchar(100), +file_no_2 varchar(100), +file_no_3 varchar(100), +respondent varchar(500), +appellant varchar(500), +headnote_summary ntext, +scheduleDescription varchar(100), +scheduleNum int +) +-- Fill the temp table with Decisions Data +INSERT INTO #TempTable +( +ID, +decision_datetime, +file_no_1, +file_no_2, +file_no_3, +respondent, +appellant, +headnote_summary, +scheduleDescription, +scheduleNum +) +SELECT ID, +decision_datetime, +file_no_1, +file_no_2, +file_no_3, +respondent, +appellant, +headnote_summary, +description AS scheduleDescription, +num As scheduleNum +From dbo.DECISION inner join dbo.SCHEDULE ON dbo.DECISION.schedule_id = dbo.SCHEDULE.num +-- Create variable to identify the first and last record that should be selected +DECLARE @FirstRec int, @LastRec int +SELECT @FirstRec = (@CurrentPage - 1) * @PageSize +SELECT @LastRec = (@CurrentPage * @PageSize + 1) + -- Select one page of data based on the record numbers above +Select +ID, +decision_datetime, +file_no_1, +file_no_2, +file_no_3, +respondent, +appellant, +headnote_summary, +scheduleDescription, +scheduleNum +From #TempTable +WHERE +ID > @FirstRec +AND +ID < @LastRec +-- Return the total number of records availbale as an output parameter +SELECT @TotalRecords = COUNT(*) FROM dbo.DECISION inner join dbo.SCHEDULE ON dbo.DECISION.schedule_id = dbo.SCHEDULE.num + /* SET NOCOUNT ON */ +go + +CREATE PROCEDURE [dbo].[spCountCategoryBySchedule] +@ScheduleId int +AS +SELECT + COUNT(*) +FROM + Category +WHERE + parent_num = @ScheduleId + +go + +CREATE PROCEDURE [dbo].[spCountDecisionsByChairman] + +@Id int + +AS + +SELECT + COUNT(*) as chairman_id + +FROM + Decision inner join Chairman ON(Decision.chairman_id = Chairman.id) + +WHERE + Decision.chairman_id = @Id + +go + +CREATE PROCEDURE [dbo].[spCountSubCategoryByCategory] +@CategoryId int +AS +SELECT + COUNT(*) +FROM + Subcategory +WHERE + parent_num = @CategoryId + +go + +CREATE PROCEDURE [dbo].[spDefaultResults] +AS +SELECT d.id AS ID , respondent, appellant, CONVERT(char(24),decision_datetime,101)AS decision_datetime,file_no_1, file_no_2, file_no_3,Description + FROM DECISION d INNER JOIN Schedule ON (d.schedule_id= Schedule.num) + ORDER BY d.decision_datetime DESC + +go + +CREATE PROCEDURE [dbo].[spDeleteCategory] + +@Id int + +AS + +DELETE + Category + +WHERE + Num = @Id + + +go + +CREATE PROCEDURE [dbo].[spDeleteChairman] + +@Id int + +AS + +DELETE + Chairman + +WHERE + [Id] = @Id + + +go + +CREATE PROCEDURE [dbo].[spDeleteChairmanDecisionMap] + +@DecisionID int + +AS + +DELETE + ChairmanDecisionMap + +WHERE + Decision_Id = @DecisionID + + +go + +CREATE PROCEDURE [dbo].[spDeleteSchedule] + +@Id int + +AS + +DELETE + Schedule + +WHERE + Num = @Id + + +go + +CREATE PROCEDURE [dbo].[spDeleteUser] + +@UserID int + +AS + +DECLARE +@Count int + +SELECT @Count = COUNT(*) FROM Users + +IF @Count > 1 + BEGIN + DELETE + Users + + WHERE + UserID = @UserID + END + +go + +CREATE PROCEDURE [dbo].[spGetCategoryList] +AS +SELECT [id], parent_num, num, [description] +FROM category +ORDER BY parent_num , num +go + +CREATE PROCEDURE dbo.spGetCategoryListBySchedule +@ScheduleId int +AS +SELECT [id], parent_num, c.num, c.[description], s.[description] as schedulename +FROM category c +inner join schedule s on c.parent_num = s.num +WHERE parent_num = @ScheduleId +ORDER BY c. num + + +go + +CREATE PROCEDURE [dbo].[spGetChairmanList] + +AS + +select [id], prefix, surname, suffix +from chairman +order by surname +go + +CREATE PROCEDURE [dbo].[spGetChairmanSelected] +@DecisionId int +AS +select [id], prefix, surname, suffix, isnull(m.commid, 0) as commid +from chairman c +left join + ( + select chairman_id as commid + from chairmandecisionmap cdm + where decision_id = @DecisionId + ) as m +on c.[id] = m.commid +order by commid desc, c.surname + +go + +CREATE PROCEDURE dbo.spGetDecision +@DecisionId int +As +select Decision.schedule_id,main_category_id,main_subcategory_id,chairman_id, Is_public,last_updatedtime,created_datetime,publication_datetime, Decision.id,appellant,respondent, Schedule.description As SchDescription,Category.description As CatDescription,file_no_1,file_no_2,file_no_3,decision_datetime,headnote_summary,SubCategory.description As SubDescription From Decision +inner join SCHEDULE ON (Decision.schedule_id = Schedule.num) inner join +Category ON (Decision.main_category_id = Category.id) inner join +SubCategory ON (Decision.main_subcategory_id = SubCategory.id) inner join +Chairman ON (Decision.chairman_id = Chairman.[id]) +WHERE Decision.id = @DecisionId +go + +CREATE PROCEDURE dbo.spGetDecisionAllWithID +@DecisionId int +As +select Users.Firstname,Users.Lastname,Decision.schedule_id,main_category_id,main_subcategory_id,chairman_id, Is_public,last_updatedtime,created_datetime,publication_datetime, Decision.id,appellant,respondent, Schedule.description As SchDescription,Category.description As CatDescription,file_no_1,file_no_2,file_no_3,CONVERT(char(24),decision_datetime,101)AS decision_datetime,headnote_summary,SubCategory.description As SubDescription From Decision +inner join SCHEDULE ON (Decision.schedule_id = Schedule.num) inner join +Category ON (Decision.main_category_id = Category.id) inner join +SubCategory ON (Decision.main_subcategory_id = SubCategory.id) inner join +Chairman ON (Decision.chairman_id = Chairman.[id]) inner join Users ON (Decision.usersID = Users.UserID) +WHERE Decision.id = @DecisionId +go + +CREATE PROCEDURE dbo.spGetDecisionAllWithoutID +As +select Users.Firstname,Users.Lastname,Decision.schedule_id,main_category_id,main_subcategory_id,chairman_id, Is_public,last_updatedtime,created_datetime,publication_datetime, Decision.id,appellant,respondent, Schedule.description As SchDescription,Category.description As CatDescription,file_no_1,file_no_2,file_no_3,CONVERT(char(24),decision_datetime,101)AS decision_datetime,headnote_summary,SubCategory.description As SubDescription From Decision +inner join SCHEDULE ON (Decision.schedule_id = Schedule.num) inner join +Category ON (Decision.main_category_id = Category.id) inner join +SubCategory ON (Decision.main_subcategory_id = SubCategory.id) inner join +Chairman ON (Decision.chairman_id = Chairman.[id]) inner join Users ON (Decision.usersID = Users.UserID) +go + +CREATE PROCEDURE [dbo].[spGetDecisionPublished] + +@DecisionId int, +@Is_Public bit + +AS + +select * from decision d + +where d.[id] = @DecisionId +and is_public = @Is_Public + +go + +CREATE PROCEDURE [dbo].[spGetDecisionsByChairman] + +@Id int + +AS + +SELECT + Decision.id,respondent, appellant, decision_datetime, Description + +FROM + Decision inner join Chairman ON(Decision.chairman_id = Chairman.id) inner join Schedule ON (Decision.schedule_id = Schedule.num) + +WHERE + Decision.chairman_id = @Id + +go + +CREATE PROCEDURE [dbo].[spGetScheduleList] + +AS + +SELECT num, [description] +FROM Schedule +ORDER BY num +go + +CREATE PROCEDURE [dbo].[spGetSubCategoryList] +AS +SELECT [id], parent_num, num, [description] +FROM subcategory +ORDER BY parent_num, num + +go + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] +@CategoryId int, +@ScheduleId int +AS +select s.[id], s.parent_id,s.num,s.[description], c.[description] as categoryname, c.num as categorynumber,s.schedule_id as schedulenumber +From subcategory s +inner join category c on s.parent_id = c.[id] +WHERE schedule_id = @ScheduleId AND c.num = @CategoryId +-----c.[id] = @CategoryId +-- AND c.parent_num = @ScheduleId +Order By s.num +go + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory1] +@CategoryId int + +AS +SELECT s.[id], s.parent_id, s.num, s.[description], c.[description] as categoryname +FROM subcategory s +-- changes made to the equality sign of the join, changed c.num to c.id +inner join category c on s.parent_id = c.id +WHERE s.parent_id = @CategoryId +ORDER BY s. num +go + +CREATE PROCEDURE [dbo].[spGetUser] + +@UserID int + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +WHERE + UserID = @UserID + + +go + +CREATE PROCEDURE [dbo].[spGetUserList] + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + + +go + +CREATE PROCEDURE [dbo].[spLoginUser] + +@Username varchar(50), +@Password varchar(50) + +AS + +SELECT + * + +FROM + Users + +WHERE + Username = @Username + +AND + [Password] = @Password + + +go + +CREATE PROCEDURE [dbo].[spRecentDecisions] +AS +SELECT d.id AS ID , respondent, appellant, CONVERT(char(24),decision_datetime,101)AS decision_datetime,file_no_1, file_no_2, file_no_3,Description +FROM DECISION d INNER JOIN Schedule ON (d.schedule_id = Schedule.num) +where decision_datetime > DATEADD(MONTH, -2, GETDATE()) +ORDER BY d.decision_datetime DESC +go + +CREATE PROCEDURE [dbo].[spSearchDecision] +@DecisionDate datetime, +@FromDate datetime, +@ToDate datetime, +@Appellant varchar(500), +@Respondent varchar(500), +@clerkName varchar(200), +@Category varchar(150), +@Year varchar(50), +@caseNo varchar(50), +@Prefix varchar(50) +As +select * From Decision +WHERE file_no_1 = @Prefix OR +file_no_2 = @CaseNo OR +file_no_3 = @Year OR +clerkName LIKE '%@clerkName%' OR +main_subcategory_id = @Category +OR decision_datetime Between @FromDate AND @ToDate +go + +Create PROC [spSearchDecisionPage] +As +Select distinct d.id, d.decision_datetime As [Decision Datetime],sch.description AS [Schedule], d.respondent,d.appellant From +Decision As d inner join Chairman ch ON (d.chairman_id = ch.id) inner join +Schedule As sch ON (d.schedule_id = sch.num) inner join Category As c ON (sch.num = c.parent_num) inner join +Subcategory As s ON (c.id = s.parent_id) + +go + +CREATE PROC [spSearchDecisionPage1] + +@Prefix varchar(100), +@CaseNo varchar(100), +@Year varchar(100), +@decision_datetime datetime, +@FromDate datetime, +@ToDate datetime, +@appellant varchar(500), +@respondent varchar(500), +@chairman_id int, +@schedule_id int, +@main_category_id int, +@main_subcategory_id int, +@startIndex int, +@endIndex int +As +Select distinct d.id, d.decision_datetime As [decision_datetime],sch.description AS [Schedule], d.respondent,d.appellant From +Decision As d inner join Chairman ch ON (d.chairman_id = ch.id) inner join +Schedule As sch ON (d.schedule_id = sch.num) inner join Category As c ON (sch.num = c.parent_num) inner join +Subcategory As s ON (c.id = s.parent_id) +WHERE ( d.id > @startIndex AND d.id <= @endIndex) OR +file_no_1 = @Prefix OR +file_no_2 = @CaseNo OR +file_no_3 = @Year OR +appellant LIKE @appellant +OR +respondent LIKE @respondent OR +d.chairman_id = @chairman_id OR +d.schedule_id = @schedule_id OR +d.main_category_id = @main_category_id OR +d.main_subcategory_id = @main_subcategory_id OR +d.decision_datetime Between @FromDate AND @ToDate +ORDER BY d.id +go + +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@id int, +@description varchar(100) + +AS + +UPDATE + Category + +SET + [description] = @description + +WHERE + num = @id + + +go + +CREATE PROCEDURE [dbo].[spUpdateChairman] + +@id int, +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +UPDATE + Chairman + +SET + prefix = @Prefix, + surname = @surname, + suffix = @suffix + +WHERE + [id] = @id + + +go + +CREATE PROCEDURE [dbo].[spUpdateDecision] +@Id int, +@file_no_1 varchar(50), +@file_no_2 varchar(50), +@file_no_3 varchar(50), +@decision_datetime datetime, +@appellant varchar(500), +@respondent varchar(500), +@chairman_id int, +@schedule_id int, +@main_category_id int, +@main_subcategory_id int, +@headnote_summary ntext, +@Is_public bit + +AS + +DECLARE +@Publication_datetime datetime, +@CurrentlyPublished bit +SELECT @CurrentlyPublished = Is_public FROM Decision WHERE [id] = @Id +IF (@CurrentlyPublished = 0 AND @Is_public = 1) + BEGIN + SELECT @Publication_datetime = getdate() + END +IF @CurrentlyPublished = 1 + BEGIN + SELECT @Publication_datetime = Publication_datetime FROM Decision WHERE [id] = @Id + END +IF @Is_public = 0 + BEGIN + SELECT @Publication_datetime = NULL + END + UPDATE + Decision + + SET + Is_public = @Is_public, + File_no_1 = @File_no_1, + File_no_2 = @File_no_2, + File_no_3 = @File_no_3, + decision_datetime = @decision_datetime, + Appellant = @Appellant, + Respondent = @Respondent, + chairman_id = @chairman_id , + schedule_id = @schedule_id, + main_category_id = @main_category_id, + main_subcategory_id = @main_subcategory_id, + Headnote_summary = @Headnote_summary, + Last_updatedtime = getdate(), + Publication_datetime = @Publication_datetime + + + + WHERE + [Id] = @Id +go + +CREATE PROCEDURE [dbo].[spUpdateSchedule] + +@id int, +@description varchar(100) + +AS + +UPDATE + Schedule + +SET + [description] = @description + +WHERE + num = @id + + +go + +CREATE PROCEDURE [dbo].[spUpdateSubCategory] +@id int, +@parent_num tinyint, +@description varchar(100), +@num tinyint +AS +UPDATE + Subcategory +SET + parent_num = @parent_num, + [description] = @description, + num = @num +WHERE + [id] = @id + +go + +CREATE PROCEDURE [dbo].[spUpdateUser] + +@UserID int, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +UPDATE + Users + +SET + Username = @Username, + Firstname = @Firstname, + Lastname = @Lastname + +WHERE + UserID = @UserID + +-- Update the password ONLY if provided +IF @Password IS NOT NULL AND 0 < LEN(@Password) + UPDATE Users + SET [Password] = @Password + WHERE UserID = @UserID + + +go + diff --git a/terraform/environments/tribunals/scripts/cicap/sp_migration.sql b/terraform/environments/tribunals/scripts/cicap/sp_migration.sql new file mode 100644 index 00000000000..1519051545c --- /dev/null +++ b/terraform/environments/tribunals/scripts/cicap/sp_migration.sql @@ -0,0 +1,1662 @@ +use cicap +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + select @iObjectCount = 0; + +CleanUp: + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + -- This procedure should no longer be called; dt_addtosourcecontrol should be called instead. + -- Calls are forwarded to dt_addtosourcecontrol to maintain backward compatibility + set nocount on + exec dbo.dt_addtosourcecontrol + @vchSourceSafeINI, + @vchProjectName, + @vchComment, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', /* create stream */ + @txStream3 Text = '' /* grant stream */ + + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + declare @iStreamObjectId int + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + declare @iReturnValue int + declare @pos int + declare @vchProcLinePiece varchar(255) + + + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + + declare @iStreamLength int + + select @pos=1 + select @iStreamLength = datalength(@txStream2) + + if @iStreamLength > 0 + begin + + while @pos < @iStreamLength + begin + + select @vchProcLinePiece = substring(@txStream2, @pos, 255) + + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + + select @pos = @pos + 255 + + end + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + + end + end + else + begin + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 text = '', /* create stream */ + @txStream3 text = '' /* grant stream */ + +as + -- This procedure should no longer be called; dt_checkinobject should be called instead. + -- Calls are forwarded to dt_checkinobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkinobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag, + @txStream1, + @txStream2, + @txStream3 + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId =0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @vchTempText varchar(255) + + /* this is for our strings */ + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + -- This procedure should no longer be called; dt_checkoutobject should be called instead. + -- Calls are forwarded to dt_checkoutobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkoutobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = master.dbo.sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + -- This procedure should no longer be called; dt_displayoaerror should be called instead. + -- Calls are forwarded to dt_displayoaerror to maintain backward compatibility. + set nocount on + exec dbo.dt_displayoaerror + @iObject, + @iresult + + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + -- This procedure should no longer be called; dt_getpropertiesbyid_vcsshould be called instead. + -- Calls are forwarded to dt_getpropertiesbyid_vcs to maintain backward compatibility. + set nocount on + exec dbo.dt_getpropertiesbyid_vcs + @id, + @property, + @value output + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName = '') set @vchProjectName = null + if (@vchSourceSafeINI = '') set @vchSourceSafeINI = null + if (@vchServerName = '') set @vchServerName = null + if (@vchDatabaseName = '') set @vchDatabaseName = null + + if (@vchProjectName is null) or (@vchSourceSafeINI is null) or (@vchServerName is null) or (@vchDatabaseName is null) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + -- This procedure should no longer be called; dt_isundersourcecontrol should be called instead. + -- Calls are forwarded to dt_isundersourcecontrol to maintain backward compatibility. + set nocount on + exec dbo.dt_isundersourcecontrol + @vchLoginName, + @vchPassword, + @iWhoToo + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + + -- This procedure should no longer be called; dt_validateloginparams should be called instead. + -- Calls are forwarded to dt_validateloginparams to maintain backward compatibility. + set nocount on + exec dbo.dt_validateloginparams + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by legacy versions of the Microsoft +** Visual Database Tools. Version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the the Microsoft Visual Database Tools. +** Version is 7.0.05. +*/ +create procedure dbo.dt_verstamp007 +as + select 7005 +go + +grant execute on dbo.dt_verstamp007 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + + -- This procedure should no longer be called; dt_whocheckedout should be called instead. + -- Calls are forwarded to dt_whocheckedout to maintain backward compatibility. + set nocount on + exec dbo.dt_whocheckedout + @chObjectType, + @vchObjectName, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + + +CREATE PROCEDURE dbo.spCaseDetailsAdd + @CaseID int OUTPUT, + @Citation1 varchar(50), + @Citation2 varchar(50), + @Citation3 varchar(50), + @Citation4 varchar(50), + @DecisionDate datetime, + @CaseName varchar(500), + @Scheme int, + @ParaNumber varchar(50), + @Keywords ntext, + @Summary ntext, + @Disabled bit +AS + +INSERT INTO CaseDetails ( + [Citation1], + [Citation2], + [Citation3], + [Citation4], + [DecisionDate], + [CaseName], + [Scheme], + [ParaNumber], + [Keywords], + [Summary], + [Disabled] +) VALUES ( + @Citation1, + @Citation2, + @Citation3, + @Citation4, + @DecisionDate, + @CaseName, + @Scheme, + @ParaNumber, + @Keywords, + @Summary, + @Disabled +) + +SELECT @CaseID = SCOPE_IDENTITY() +go + + +CREATE PROCEDURE dbo.spCaseDetailsDelete + @CaseID int +AS + +DELETE FROM CaseDetails +WHERE + [CaseID] = @CaseID +go + + +CREATE PROCEDURE dbo.spCaseDetailsGet + @CaseID int + +AS + +SELECT + [CaseID], + [Citation1], + [Citation2], + [Citation3], + [Citation4], + [CaseName], + [Scheme], + [ParaNumber], + [Keywords], + [Summary], + [Disabled] +FROM CaseDetails +WHERE + [CaseID] = @CaseID + +go + + +CREATE PROCEDURE dbo.spCaseDetailsGetList +AS + +SELECT + [CaseID], + [Citation1], + [Citation2], + [Citation3], + [Citation4], + [CaseName], + [Scheme], + [ParaNumber], + [Keywords], + [Summary], + [Disabled] +FROM CaseDetails +go + + +CREATE PROCEDURE dbo.spCaseDetailsUpdate + @CaseID int, + @Citation1 varchar(50), + @Citation2 varchar(50), + @Citation3 varchar(50), + @Citation4 varchar(50), + @DecisionDate datetime, + @CaseName varchar(500), + @Scheme int, + @ParaNumber varchar(50), + @Keywords ntext, + @Summary ntext, + @Disabled bit +AS + +UPDATE CaseDetails SET + [Citation1] = @Citation1, + [Citation2] = @Citation2, + [Citation3] = @Citation3, + [Citation4] = @Citation4, + [DecisionDate] = @DecisionDate, + [CaseName] = @CaseName, + [Scheme] = @Scheme, + [ParaNumber] = @ParaNumber, + [Keywords] = @Keywords, + [Summary] = @Summary, + [Disabled] = @Disabled +WHERE + [CaseID] = @CaseID +go + +CREATE PROCEDURE [dbo].[spGetAll] AS +SELECT [CaseID],[Citation1],[Citation2],[Citation3],[Citation4], +CONVERT(char(24),[DecisionDate],103) AS [DecisionDate],[CaseName],[Scheme],[SchemeDesc],[ParaNumber], +[Keywords],[Summary],[DecisionDate] AS DD +FROM CASEDETAILS AS cd +INNER JOIN SCHEMES AS s ON (cd.Scheme = s.SchemeID) +ORDER BY DD DESC +go + +CREATE PROCEDURE [dbo].[spGetAllPublic] AS +SELECT [CaseID],[Citation1],[Citation2],[Citation3],[Citation4], +CONVERT(char(24),[DecisionDate],103) AS [DecisionDate],[CaseName],[Scheme],[SchemeDesc],[ParaNumber], +[Keywords],[Summary], cd.[Disabled],[DecisionDate] AS DD +FROM CASEDETAILS AS cd +INNER JOIN SCHEMES AS s ON (cd.Scheme = s.SchemeID) +WHERE cd.Disabled = 0 +ORDER BY DD DESC + +go + +CREATE PROCEDURE dbo.spGetByCaseID + +@CaseID int + +AS + +SELECT [CaseID],[Citation1],[Citation2],[Citation3],[Citation4], +CONVERT(char(24),[DecisionDate],103) AS [DecisionDate],[CaseName],cd.[Scheme],[ParaNumber],cd.[Disabled], +[Keywords],[Summary],[DecisionDate] AS DD, s.[SchemeDesc] AS SchemeDesc +FROM CASEDETAILS AS cd +INNER JOIN SCHEMES AS s ON (cd.Scheme = s.SchemeID) +WHERE [CaseID] = @CaseID +go + + +CREATE PROCEDURE dbo.spSchemesAdd + @SchemeID int OUTPUT, + @SchemeDesc varchar(300), + @Disabled bit +AS + +INSERT INTO Schemes ( + [SchemeDesc], + [Disabled] +) VALUES ( + @SchemeDesc, + @Disabled +) + +select SCOPE_IDENTITY() +go + + +CREATE PROCEDURE dbo.spSchemesDelete + @SchemeID int +AS + +DELETE FROM Schemes +WHERE + [SchemeID] = @SchemeID +go + + +CREATE PROCEDURE dbo.spSchemesGet + @SchemeID int + +AS + +SELECT + [SchemeID], + [SchemeDesc], + [Disabled] +FROM Schemes +WHERE + [SchemeID] = @SchemeID + +go + + +CREATE PROCEDURE dbo.spSchemesGetList +AS + +SELECT + [SchemeID], + [SchemeDesc], + [Disabled] +FROM Schemes +ORDER BY SchemeID +go + +CREATE PROCEDURE dbo.spSchemesGetListForUse +AS + +SELECT + [SchemeID], + [SchemeDesc], + [Disabled] +FROM Schemes +WHERE Disabled = 0 +ORDER BY SchemeID +go + + +CREATE PROCEDURE dbo.spSchemesUpdate + @SchemeID int, + @SchemeDesc varchar(300), + @Disabled bit +AS + +UPDATE Schemes SET + [SchemeDesc] = @SchemeDesc, + [Disabled] = @Disabled +WHERE + [SchemeID] = @SchemeID +go + + +CREATE PROCEDURE dbo.spUsersAdd + @UserID int OUTPUT, + @FirstName varchar(250), + @LastName varchar(250), + @UserName varchar(250), + @Password varchar (250), + @Disabled bit +AS + +INSERT INTO Users ( + [FirstName], + [LastName], + [UserName], + [Password], + [Disabled] +) VALUES ( + @FirstName, + @LastName, + @UserName, + @Password, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE [dbo].[spUsersAuthenticate] + +@UserName varchar (250), +@Password varchar (250) + +AS SELECT * FROM USERS +WHERE UserName = @UserName AND [Password] = @Password +go + + +CREATE PROCEDURE dbo.spUsersDelete + @UserID int +AS + +DELETE FROM Users +WHERE + [UserID] = @UserID +go + + +CREATE PROCEDURE dbo.spUsersGet + @UserID int + +AS + +SELECT + [UserID], + [FirstName], + [LastName], + [Login], + [Password], + [Disabled] +FROM Users +WHERE + [UserID] = @UserID + +go + + +CREATE PROCEDURE dbo.spUsersGetByID + @UserID int + +AS + +SELECT + [UserID], + [FirstName], + [LastName], + [UserName], + [Password], + [Disabled] +FROM Users +WHERE + [UserID] = @UserID +go + + +CREATE PROCEDURE dbo.spUsersGetList +AS + +SELECT + [UserID], + [FirstName], + [LastName], + [UserName], + [Password], + [Disabled] +FROM Users +go + + +CREATE PROCEDURE dbo.spUsersUpdate + @UserID int, + @FirstName varchar(250), + @LastName varchar(250), + @UserName varchar(250), + @Password varchar(250), + @Disabled bit +AS + +UPDATE Users SET + [FirstName] = @FirstName, + [LastName] = @LastName, + [UserName] = @UserName, + [Password] = @Password, + [Disabled] = @Disabled +WHERE + [UserID] = @UserID +go + + +CREATE PROCEDURE dbo.spkeywordsAdd + @KeywordID int OUTPUT, + @Keyword varchar(500), + @Disabled bit +AS + +INSERT INTO keywords ( + [Keyword], + [Disabled] +) VALUES ( + @Keyword, + @Disabled +) + +select SCOPE_IDENTITY() +go + + +CREATE PROCEDURE dbo.spkeywordsDelete + @KeywordID int +AS + +DELETE FROM keywords +WHERE + [KeywordID] = @KeywordID +go + + +CREATE PROCEDURE dbo.spkeywordsGet + @KeywordID int + +AS + +SELECT + [KeywordID], + [Keyword], + [Disabled] +FROM keywords +WHERE + [KeywordID] = @KeywordID + +go + + +CREATE PROCEDURE dbo.spkeywordsGetList +AS + +SELECT + [KeywordID], + [Keyword], + [Disabled] +FROM keywords +ORDER BY Keyword +go + +CREATE PROCEDURE dbo.spkeywordsGetListForUse +AS + +SELECT + [KeywordID], + [Keyword], + [Disabled] +FROM keywords +WHERE Disabled = 0 +ORDER BY Keyword +go + + +CREATE PROCEDURE dbo.spkeywordsUpdate + @KeywordID int, + @Keyword varchar(500), + @Disabled bit +AS + +UPDATE keywords SET + [Keyword] = @Keyword, + [Disabled] = @Disabled +WHERE + [KeywordID] = @KeywordID +go + diff --git a/terraform/environments/tribunals/scripts/employment_appeals/sp_migration.sql b/terraform/environments/tribunals/scripts/employment_appeals/sp_migration.sql new file mode 100644 index 00000000000..77389963be5 --- /dev/null +++ b/terraform/environments/tribunals/scripts/employment_appeals/sp_migration.sql @@ -0,0 +1,1423 @@ +use eat +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + select @iObjectCount = 0; + +CleanUp: + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + -- This procedure should no longer be called; dt_addtosourcecontrol should be called instead. + -- Calls are forwarded to dt_addtosourcecontrol to maintain backward compatibility + set nocount on + exec dbo.dt_addtosourcecontrol + @vchSourceSafeINI, + @vchProjectName, + @vchComment, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', /* create stream */ + @txStream3 Text = '' /* grant stream */ + + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + declare @iStreamObjectId int + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + declare @iReturnValue int + declare @pos int + declare @vchProcLinePiece varchar(255) + + + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + + declare @iStreamLength int + + select @pos=1 + select @iStreamLength = datalength(@txStream2) + + if @iStreamLength > 0 + begin + + while @pos < @iStreamLength + begin + + select @vchProcLinePiece = substring(@txStream2, @pos, 255) + + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + + select @pos = @pos + 255 + + end + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + + end + end + else + begin + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 text = '', /* drop stream */ /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 text = '', /* create stream */ + @txStream3 text = '' /* grant stream */ + +as + -- This procedure should no longer be called; dt_checkinobject should be called instead. + -- Calls are forwarded to dt_checkinobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkinobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag, + @txStream1, + @txStream2, + @txStream3 + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId =0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @vchTempText varchar(255) + + /* this is for our strings */ + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + + -- This procedure should no longer be called; dt_checkoutobject should be called instead. + -- Calls are forwarded to dt_checkoutobject to maintain backward compatibility. + set nocount on + exec dbo.dt_checkoutobject + @chObjectType, + @vchObjectName, + @vchComment, + @vchLoginName, + @vchPassword, + @iVCSFlags, + @iActionFlag + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = master.dbo.sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + -- This procedure should no longer be called; dt_displayoaerror should be called instead. + -- Calls are forwarded to dt_displayoaerror to maintain backward compatibility. + set nocount on + exec dbo.dt_displayoaerror + @iObject, + @iresult + + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + -- This procedure should no longer be called; dt_getpropertiesbyid_vcsshould be called instead. + -- Calls are forwarded to dt_getpropertiesbyid_vcs to maintain backward compatibility. + set nocount on + exec dbo.dt_getpropertiesbyid_vcs + @id, + @property, + @value output + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID varchar(100) + select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName = '') set @vchProjectName = null + if (@vchSourceSafeINI = '') set @vchSourceSafeINI = null + if (@vchServerName = '') set @vchServerName = null + if (@vchDatabaseName = '') set @vchDatabaseName = null + + if (@vchProjectName is null) or (@vchSourceSafeINI is null) or (@vchServerName is null) or (@vchDatabaseName is null) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText is not null + begin + exec @iReturn = master.dbo.sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText = '') set @vchTempText = null + if (@vchTempText is not null) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + -- This procedure should no longer be called; dt_isundersourcecontrol should be called instead. + -- Calls are forwarded to dt_isundersourcecontrol to maintain backward compatibility. + set nocount on + exec dbo.dt_isundersourcecontrol + @vchLoginName, + @vchPassword, + @iWhoToo + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + + -- This procedure should no longer be called; dt_validateloginparams should be called instead. + -- Calls are forwarded to dt_validateloginparams to maintain backward compatibility. + set nocount on + exec dbo.dt_validateloginparams + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by legacy versions of the Microsoft +** Visual Database Tools. Version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the the Microsoft Visual Database Tools. +** Version is 7.0.05. +*/ +create procedure dbo.dt_verstamp007 +as + select 7005 +go + +grant execute on dbo.dt_verstamp007 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = master.dbo.sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = master.dbo.sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + + -- This procedure should no longer be called; dt_whocheckedout should be called instead. + -- Calls are forwarded to dt_whocheckedout to maintain backward compatibility. + set nocount on + exec dbo.dt_whocheckedout + @chObjectType, + @vchObjectName, + @vchLoginName, + @vchPassword + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + + +CREATE PROCEDURE usp_Create_list_table + @cslist VARCHAR(8000), + @tablename SYSNAME AS +BEGIN + DECLARE @spot SMALLINT, @str VARCHAR(8000), @sql VARCHAR(8000) + + WHILE @cslist <> '' + BEGIN + SET @spot = CHARINDEX(',', @cslist) + IF @spot>0 + BEGIN + SET @str = CAST(LEFT(@cslist, @spot-1) AS INT) + SET @cslist = RIGHT(@cslist, LEN(@cslist)-@spot) + END + ELSE + BEGIN + SET @str = CAST(@cslist AS INT) + SET @cslist = '' + END + SET @sql = 'INSERT INTO '+@tablename+' VALUES('+CONVERT(VARCHAR(100),@str)+')' + EXEC(@sql) + END +END + + +go + + +CREATE PROCEDURE usp_Delete_Judgment +(@ID INT,@Filename varChar(150) OUTPUT) +AS +BEGIN TRAN + + SELECT @FILENAME = Filename FROM JUDGMENT + WHERE ID = @ID + IF @@ERROR <> 0 + BEGIN + ROLLBACK TRAN + RETURN 1 + END + + DELETE FROM judgment_jurisdictions + WHERE id = @ID + IF @@ERROR <> 0 + BEGIN + ROLLBACK TRAN + RETURN 1 + END + + DELETE FROM judgment WHERE ID = @ID + + + IF @@ERROR <> 0 + BEGIN + ROLLBACK TRAN + RETURN 1 + END +COMMIT TRAN + + +go + + +CREATE PROCEDURE usp_Get_Cause_List +AS +SELECT id,title, CONVERT(VARCHAR(11), [date],103) AS _DATE, + Upload_date, uploaded_by, filename, convert(datetime, upload_date, 103) AS SORT_DATE +FROM CAUSELIST +ORDER BY SORT_DATE DESC + + +go + + +CREATE PROCEDURE usp_Get_Jurisdictions +AS +SELECT l2.id,(SELECT l1.title as l1title +FROM jurisdiction_level1 l1 where l1.id = l2.parent_id) + ' - ' + L2.title as CON_title, rank FROM +jurisdiction_level2 l2 +WHERE L2.TITLE <> '' AND l2.obsolete = 0 +order by 2,3 asc + + +go + + +CREATE PROCEDURE usp_Insert_Cause_List +(@title varChar(200),@date DateTime,@uploaded_by varChar(50),@filename varChar(100)) +AS +INSERT INTO causelist (title, [date], upload_date, uploaded_by, filename) + VALUES (@title, @date,CONVERT(VARCHAR(11),GETDATE(),103), @uploaded_by,@filename) + +go + + +CREATE PROCEDURE usp_Insert_Judgment +(@EatNum varChar(80), + @Judge varChar(100), + @appellant varChar(400), + @respondent varChar(400), + @date datetime, + @uploaded_by varChar(50), + @filename varChar(150), + @starred char(1), + @jurisdiction_list varChar(200), + @Jurisdiction_Main INT) +AS +DECLARE @RETURN_ID INT +BEGIN TRAN + + INSERT INTO JUDGMENT(eatnum,judge,appellant,respondent,[date], + upload_date,uploaded_by, filename, starred) + VALUES (@eatnum,@judge,@appellant,@respondent,@date, + convert(varchar(10), GETDATE(),103),@uploaded_by, @filename, @starred) + + IF @@ERROR <> 0 + BEGIN + ROLLBACK TRAN + RETURN 1 + END + + SELECT @RETURN_ID = @@IDENTITY + + IF @jurisdiction_list <> '' + BEGIN + CREATE TABLE #VALS (ID int) + EXEC usp_Create_list_table @jurisdiction_list,'#VALS' + + INSERT INTO judgment_jurisdictions + ( + id, + jurisdiction, + primary_jurisdiction + ) + SELECT @RETURN_ID, ID,0 FROM #VALS + DROP TABLE #VALS + END + + IF @@ERROR <> 0 + BEGIN + ROLLBACK TRAN + RETURN 1 + END + + --Finally, add the main jurisdiction + + INSERT INTO judgment_jurisdictions + ( + id, + jurisdiction, + primary_jurisdiction + ) + VALUES ( + @RETURN_ID, + @Jurisdiction_Main, + 1) + + IF @@ERROR <> 0 + BEGIN + ROLLBACK TRAN + RETURN 1 + END + +COMMIT TRAN + +go + + +CREATE PROCEDURE usp_get_judgmentForEdit +(@ID INT) +AS +SELECT Judgment.id, Judgment.eatnum, Judgment.judge, Judgment.appellant, Judgment.respondent, CONVERT(VARCHAR(11), Judgment.date, 103) AS _Date, + Judgment.filename, Judgment.STARRED, jurisdiction_level2.title, jurisdiction_level1.title AS l1title, jurisdiction_level2.id AS l2ID +FROM Judgment LEFT OUTER JOIN + judgment_jurisdictions INNER JOIN + jurisdiction_level1 INNER JOIN + jurisdiction_level2 ON jurisdiction_level1.id = jurisdiction_level2.parent_id ON judgment_jurisdictions.jurisdiction = jurisdiction_level2.id ON + Judgment.id = judgment_jurisdictions.id +WHERE (judgment_jurisdictions.primary_jurisdiction = 1) +AND Judgment.id = @ID +go + + + +Create Proc usp_get_jurisdiction_detail +(@ID int) +AS +select j.id, eatnum, l1.title AS l1title, l2.title as l2title, judge, appellant, respondent, CONVERT(varChar(11),[upload_date],103) as _date, +upload_date, uploaded_by, filename, +CASE + WHEN DATEDIFF(dd,[Date], GETDATE()) < 31 THEN '* New *' +END AS RECENT +from judgment j, + jurisdiction_level1 l1, + jurisdiction_level2 l2, + judgment_jurisdictions jj + +where j.id = jj.id +and jj.jurisdiction = l2.id +and l2.parent_id = l1.id +and j.id = @id + + +go + diff --git a/terraform/environments/tribunals/scripts/finance_and_tax/sp_migration.sql b/terraform/environments/tribunals/scripts/finance_and_tax/sp_migration.sql new file mode 100644 index 00000000000..38d054db77f --- /dev/null +++ b/terraform/environments/tribunals/scripts/finance_and_tax/sp_migration.sql @@ -0,0 +1,2509 @@ +use ftt +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(varchar(255), name) from sysobjects where type = 'P' and name not like 'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName varchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId nvarchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(nvarchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(nvarchar(255), name) from sysobjects where type = N'P' and name not like N'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName nvarchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText varchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText nvarchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode nvarchar(255)) + + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select N'VCS'=sourcecode from #commenttext order by id + select N'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput nvarchar(255) +declare @hr int +declare @vchSource nvarchar(255) +declare @vchDescription nvarchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select uvalue + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID nvarchar(100) + select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText nvarchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR(N'Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist nvarchar(255)) + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the Microsoft Visual Database Tools. +** Current version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue nvarchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + N'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + + + + +CREATE PROCEDURE [dbo].[spAddCategory] + +@description varchar(100) + +AS + +INSERT + Category + ( + [description] + ) + +VALUES + ( + @description + ) + + +go + + + + +CREATE PROCEDURE [dbo].[spAddCommissioner] + +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +INSERT + Commissioner + ( + prefix, + surname, + suffix + ) + +VALUES + ( + @Prefix, + @surname, + @suffix + ) + +go + + + +CREATE PROCEDURE [dbo].[spAddCommissionerJudgmentMap] + +@CommissionerID int, +@JudgmentID int + +AS + +INSERT + CommissionerJudgmentMap + ( + Commissioner_ID, + Judgment_ID + ) + + VALUES + ( + @CommissionerID, + @JudgmentID + ) + +go + + + + +CREATE PROCEDURE [dbo].[spAddJudgment] + +@Id int OUTPUT, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(4), +@Reported_no_1 varchar(3), +@Reported_no_2 varchar(3), +@Reported_no_3 varchar(2) + +AS + +DECLARE @Publication_datetime datetime + +IF @Is_published = 1 + SELECT @Publication_datetime = getdate() + + + INSERT + Judgment + ( + Is_published, + File_no_1, + File_no_2, + Decision_datetime, + Claimants, + Respondent, + Main_subcategory_id, + Sec_subcategory_id, + Headnote_summary, + Created_datetime, + Last_updatedtime, + Publication_datetime, + File_no_3, + Reported_no_1, + Reported_no_2, + Reported_no_3 + ) + + VALUES + ( + @Is_published, + @File_no_1, + @File_no_2, + @Decision_datetime, + @Claimants, + @Respondent, + @Main_subcategory_id, + @Sec_subcategory_id, + @Headnote_summary, + getdate(), + getdate(), + @Publication_datetime, + @File_no_3, + @Reported_no_1, + @Reported_no_2, + @Reported_no_3 + ) + + SELECT @Id = SCOPE_IDENTITY() + +go + + + + +CREATE PROCEDURE [dbo].[spAddSubCategory] + +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +INSERT + Subcategory + ( + parent_num, + [description], + num + ) + +VALUES + ( + @parent_num, + @description, + @num + ) + + +go + + + +CREATE PROCEDURE [dbo].[spAddUser] + +@UserID integer OUTPUT, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +INSERT + Users + + ( + Username, + [Password], + Firstname, + Lastname + ) + +VALUES + + ( + @Username, + @Password, + @Firstname, + @Lastname + ) + +SELECT @UserID = SCOPE_IDENTITY() + +go + + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsByCommissioner] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + CommissionerJudgmentMap + +WHERE + commissioner_id = @Id + +go + + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsBySubCategory] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + Judgment + +WHERE + main_subcategory_id = @Id + + OR + + sec_subcategory_id = @Id + + +go + + + + +CREATE PROCEDURE [dbo].[spCountSubCategoryByCategory] + +@CategoryId int + +AS + +SELECT + COUNT(*) + +FROM + Subcategory + +WHERE + parent_num = @CategoryId + + +go + + + + +CREATE PROCEDURE [dbo].[spDeleteCategory] + +@Id int + +AS + +DELETE + Category + +WHERE + Num = @Id + + +go + + + + +CREATE PROCEDURE [dbo].[spDeleteCommissioner] + +@Id int + +AS + +DELETE + Commissioner + +WHERE + [Id] = @Id + +go + + + +CREATE PROCEDURE [dbo].[spDeleteCommissionerJudgmentMap] + +@JudgmentID int + +AS + +DELETE + CommissionerJudgmentMap + +WHERE + Judgment_Id = @JudgmentID + +go + + + + +CREATE PROCEDURE [dbo].[spDeleteSubCategory] + +@Id int + +AS + +DELETE + SubCategory + +WHERE + [Id] = @Id + + +go + + + +CREATE PROCEDURE [dbo].[spDeleteUser] + +@UserID int + +AS + +DECLARE +@Count int + +SELECT @Count = COUNT(*) FROM Users + +IF @Count > 1 + BEGIN + DELETE + Users + + WHERE + UserID = @UserID + END + +go + + + + + +CREATE PROCEDURE [dbo].[spGetAllDecisions] + +AS + +-- Main result set - ds.Tables(0) +select j.id, j.decision_datetime as DecisionDate +,j.file_no_1 + ' ' + j.file_no_2 + ' ' + j.file_no_3 as 'FileNumber' +, c.[description] as Category +, s.[description] as Subcategory +, c2.[description] as SecondaryCategory +,s2.[description] as SecondarySubcategory +,j.claimants as Claimants +, j.respondent as Respondent +,cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as Commissioner +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +go + + + + +CREATE PROCEDURE [dbo].[spGetCategoryList] + +AS + +SELECT num, [description] +FROM category +ORDER BY num + + +go + + + +CREATE PROCEDURE [dbo].[spGetCommissionerList] + +AS + +select [id], prefix, surname, suffix +from commissioner +order by surname + +go + + + +CREATE PROCEDURE [dbo].[spGetCommissionerListSelected] + +@DecisionId int + +AS + +select [id], prefix, surname, suffix, isnull(m.commid, 0) as commid +from commissioner c +left join + ( + select commissioner_id as commid + from commissionerjudgmentmap cjm + where judgment_id = @DecisionId + ) as m +on c.[id] = m.commid +order by commid desc, c.surname + +go + + + + +CREATE PROCEDURE [dbo].[spGetDecision] + +@DecisionId int + +AS + +-- Main result set - ds.Tables(0) +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid, + j.file_no_1 + ' ' + j.file_no_2 + ' ' + j.file_no_3 as 'filenumber' +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId + +-- Selected commissioners result set - ds.Tables(1) +select cm.prefix, cm.surname, cm.suffix, cm.[id] as 'commissionerid', + cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as 'commissioner' +from judgment j +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +where cjm.judgment_id = @DecisionId +go + + + + +CREATE PROCEDURE [dbo].[spGetDecisionPublished] + +@DecisionId int, +@Is_Published bit + +AS + +-- Main result set - ds.Tables(0) +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid, + j.file_no_1 + ' ' + j.file_no_2 + ' ' + j.file_no_3 as 'filenumber' +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +and is_published = @Is_Published + +-- Selected commissioners result set - ds.Tables(1) +select cm.prefix, cm.surname, cm.suffix, cm.[id] as 'commissionerid', + cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as 'commissioner' +from judgment j +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +where cjm.judgment_id = @DecisionId +go + + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryList] + +AS + +SELECT [id], parent_num, num, [description] +FROM subcategory +ORDER BY parent_num, num + + +go + + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] + +@CategoryId int + +AS + +SELECT [id], parent_num, s.num, s.[description], c.[description] as categoryname +FROM subcategory s +inner join category c on s.parent_num = c.num +WHERE parent_num = @CategoryId +ORDER BY s. num + + +go + + + +CREATE PROCEDURE [dbo].[spGetUser] + +@UserID int + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +WHERE + UserID = @UserID + +go + + + +CREATE PROCEDURE [dbo].[spGetUserList] + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +go + + + + +CREATE PROCEDURE [dbo].[spLoginUser] + +@Username varchar(50), +@Password varchar(50) + +AS + +SELECT + * + +FROM + Users + +WHERE + Username = @Username + +AND + [Password] = @Password + + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@id int, +@description varchar(100) + +AS + +UPDATE + Category + +SET + [description] = @description + +WHERE + num = @id + + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateCommissioner] + +@id int, +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +UPDATE + Commissioner + +SET + prefix = @Prefix, + surname = @surname, + suffix = @suffix + +WHERE + [id] = @id + +go + + + +CREATE PROCEDURE [dbo].[spUpdateCommissionerJudgmentMap] + +@CommissionerID int, +@JudgmentID int + +AS + +UPDATE + CommissionerJudgmentMap + +SET + Commissioner_ID = @CommissionerID + +WHERE + Judgment_ID = @JudgmentID + + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateJudgment] + +@Id int, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(4), +@Reported_no_1 varchar(3), +@Reported_no_2 varchar(3), +@Reported_no_3 varchar(2) + +AS + +DECLARE +@Publication_datetime datetime, +@CurrentlyPublished bit + +SELECT @CurrentlyPublished = Is_Published FROM Judgment WHERE [id] = @Id + +IF (@CurrentlyPublished = 0 AND @Is_published = 1) + BEGIN + SELECT @Publication_datetime = getdate() + END + +IF @CurrentlyPublished = 1 + BEGIN + SELECT @Publication_datetime = Publication_datetime FROM Judgment WHERE [id] = @Id + END + +IF @Is_published = 0 + BEGIN + SELECT @Publication_datetime = NULL + END + + + UPDATE + Judgment + + SET + Is_Published = @Is_Published, + File_no_1 = @File_no_1, + File_no_2 = @File_no_2, + File_no_3 = @File_no_3, + Decision_datetime = @Decision_datetime, + Claimants = @Claimants, + Respondent = @Respondent, + Main_subcategory_id = @Main_subcategory_id, + Sec_subcategory_id = @Sec_subcategory_id, + Headnote_summary = @Headnote_summary, + Last_updatedtime = getdate(), + Publication_datetime = @Publication_datetime, + Reported_no_1 = @Reported_no_1, + Reported_no_2 = @Reported_no_2, + Reported_no_3 = @Reported_no_3 + + WHERE + [Id] = @Id + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateSubCategory] + +@id int, +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +UPDATE + Subcategory + +SET + parent_num = @parent_num, + [description] = @description, + num = @num + +WHERE + [id] = @id + + +go + + + +CREATE PROCEDURE [dbo].[spUpdateUser] + +@UserID int, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +UPDATE + Users + +SET + Username = @Username, + Firstname = @Firstname, + Lastname = @Lastname + +WHERE + UserID = @UserID + +-- Update the password ONLY if provided +IF @Password IS NOT NULL AND 0 < LEN(@Password) + UPDATE Users + SET [Password] = @Password + WHERE UserID = @UserID + +go + diff --git a/terraform/environments/tribunals/scripts/immigration_services/sp_migration.sql b/terraform/environments/tribunals/scripts/immigration_services/sp_migration.sql new file mode 100644 index 00000000000..05f3623ad87 --- /dev/null +++ b/terraform/environments/tribunals/scripts/immigration_services/sp_migration.sql @@ -0,0 +1,2230 @@ +use imset +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(varchar(255), name) from sysobjects where type = 'P' and name not like 'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName varchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol to [public] +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId nvarchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(nvarchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(nvarchar(255), name) from sysobjects where type = N'P' and name not like N'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName nvarchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_addtosourcecontrol_u to [public] +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +grant execute on dbo.dt_adduserobject to [public] +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +grant execute on dbo.dt_adduserobject_vcs to [public] +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject to [public] +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_checkinobject_u to [public] +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText varchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject to [public] +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText nvarchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode nvarchar(255)) + + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select N'VCS'=sourcecode from #commenttext order by id + select N'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_checkoutobject_u to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror to [public] +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput nvarchar(255) +declare @hr int +declare @vchSource nvarchar(255) +declare @vchDescription nvarchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +grant execute on dbo.dt_displayoaerror_u to [public] +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +grant execute on dbo.dt_droppropertiesbyid to [public] +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +grant execute on dbo.dt_dropuserobjectbyid to [public] +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +grant execute on dbo.dt_generateansiname to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +grant execute on dbo.dt_getobjwithprop to [public] +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +grant execute on dbo.dt_getobjwithprop_u to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid to [public] +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +grant execute on dbo.dt_getpropertiesbyid_u to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs to [public] +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select uvalue + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +grant execute on dbo.dt_getpropertiesbyid_vcs_u to [public] +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol to [public] +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID nvarchar(100) + select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText nvarchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR(N'Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist nvarchar(255)) + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +grant execute on dbo.dt_isundersourcecontrol_u to [public] +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +grant execute on dbo.dt_removefromsourcecontrol to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +grant execute on dbo.dt_setpropertybyid to [public] +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +grant execute on dbo.dt_setpropertybyid_u to [public] +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams to [public] +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_validateloginparams_u to [public] +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +grant execute on dbo.dt_vcsenabled to [public] +go + +/* +** This procedure returns the version number of the stored +** procedures used by the Microsoft Visual Database Tools. +** Current version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +grant execute on dbo.dt_verstamp006 to [public] +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout to [public] +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue nvarchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + N'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +grant execute on dbo.dt_whocheckedout_u to [public] +go + + + +CREATE PROCEDURE [dbo].[spAddCategory] + +@description varchar(100) + +AS + +INSERT + Category + ( + [description] + ) + +VALUES + ( + @description + ) + +go + + + +CREATE PROCEDURE [dbo].[spAddJudgment] + +@Id int OUTPUT, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(5) + +AS + +DECLARE @Publication_datetime datetime + +IF @Is_published = 1 + SELECT @Publication_datetime = getdate() + +INSERT + Judgment + ( + Is_published, + File_no_1, + File_no_2, + Decision_datetime, + Claimants, + Respondent, + Main_subcategory_id, + Sec_subcategory_id, + Headnote_summary, + Created_datetime, + Last_updatedtime, + Publication_datetime, + File_no_3 + ) + +VALUES + ( + @Is_published, + @File_no_1, + @File_no_2, + @Decision_datetime, + @Claimants, + @Respondent, + @Main_subcategory_id, + @Sec_subcategory_id, + @Headnote_summary, + getdate(), + getdate(), + @Publication_datetime, + @File_no_3 + ) + +SELECT @Id = SCOPE_IDENTITY() +go + + + +CREATE PROCEDURE [dbo].[spAddSubCategory] + +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +INSERT + Subcategory + ( + parent_num, + [description], + num + ) + +VALUES + ( + @parent_num, + @description, + @num + ) + +go + + + +CREATE PROCEDURE [dbo].[spAddUser] + +@UserID integer OUTPUT, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +INSERT + Users + + ( + Username, + [Password], + Firstname, + Lastname + ) + +VALUES + + ( + @Username, + @Password, + @Firstname, + @Lastname + ) + +SELECT @UserID = SCOPE_IDENTITY() + +go + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsBySubCategory] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + Judgment + +WHERE + main_subcategory_id = @Id + + OR + + sec_subcategory_id = @Id + +go + + + +CREATE PROCEDURE [dbo].[spCountSubCategoryByCategory] + +@CategoryId int + +AS + +SELECT + COUNT(*) + +FROM + Subcategory + +WHERE + parent_num = @CategoryId + +go + + + +CREATE PROCEDURE [dbo].[spDeleteCategory] + +@Id int + +AS + +DELETE + Category + +WHERE + Num = @Id + +go + + + +CREATE PROCEDURE [dbo].[spDeleteSubCategory] + +@Id int + +AS + +DELETE + SubCategory + +WHERE + [Id] = @Id + +go + + + +CREATE PROCEDURE [dbo].[spDeleteUser] + +@UserID int + +AS + +DECLARE +@Count int + +SELECT @Count = COUNT(*) FROM Users + +IF @Count > 1 + BEGIN + DELETE + Users + + WHERE + UserID = @UserID + END + +go + + + +CREATE PROCEDURE [dbo].[spGetCategoryList] + +AS + +SELECT num, [description] +FROM category +ORDER BY num + +go + + + +CREATE PROCEDURE [dbo].[spGetDecision] + +@DecisionId int + +AS + +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +go + + + +CREATE PROCEDURE [dbo].[spGetDecisionPublished] + +@DecisionId int, +@Is_Published bit + +AS + +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +and is_published = @Is_Published +go + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryList] + +AS + +SELECT [id], parent_num, num, [description] +FROM subcategory +ORDER BY parent_num, num + +go + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] + +@CategoryId int + +AS + +SELECT [id], parent_num, s.num, s.[description], c.[description] as categoryname +FROM subcategory s +inner join category c on s.parent_num = c.num +WHERE parent_num = @CategoryId +ORDER BY s. num + +go + + +CREATE PROCEDURE [dbo].[spGetUser] + +@UserID int + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +WHERE + UserID = @UserID + +go + + + +CREATE PROCEDURE [dbo].[spGetUserList] + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +go + + + +CREATE PROCEDURE [dbo].[spLoginUser] + +@Username varchar(50), +@Password varchar(50) + +AS + +SELECT + * + +FROM + Users + +WHERE + Username = @Username + +AND + [Password] = @Password + +go + + + +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@id int, +@description varchar(100) + +AS + +UPDATE + Category + +SET + [description] = @description + +WHERE + num = @id + +go + + + +CREATE PROCEDURE [dbo].[spUpdateJudgment] + +@Id int, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(5) + +AS + +DECLARE +@Publication_datetime datetime, +@CurrentlyPublished bit + +SELECT @CurrentlyPublished = Is_Published FROM Judgment WHERE [id] = @Id + +IF (@CurrentlyPublished = 0 AND @Is_published = 1) + BEGIN + SELECT @Publication_datetime = getdate() + END + +IF @CurrentlyPublished = 1 + BEGIN + SELECT @Publication_datetime = Publication_datetime FROM Judgment WHERE [id] = @Id + END + +IF @Is_published = 0 + BEGIN + SELECT @Publication_datetime = NULL + END + +UPDATE + Judgment + +SET + Is_Published = @Is_Published, + File_no_1 = @File_no_1, + File_no_2 = @File_no_2, + Decision_datetime = @Decision_datetime, + Claimants = @Claimants, + Respondent = @Respondent, + Main_subcategory_id = @Main_subcategory_id, + Sec_subcategory_id = @Sec_subcategory_id, + Headnote_summary = @Headnote_summary, + Last_updatedtime = getdate(), + Publication_datetime = @Publication_datetime, + File_no_3 = @File_no_3 + +WHERE + [Id] = @Id +go + + + +CREATE PROCEDURE [dbo].[spUpdateSubCategory] + +@id int, +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +UPDATE + Subcategory + +SET + parent_num = @parent_num, + [description] = @description, + num = @num + +WHERE + [id] = @id + +go + + + +CREATE PROCEDURE [dbo].[spUpdateUser] + +@UserID int, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +UPDATE + Users + +SET + Username = @Username, + Firstname = @Firstname, + Lastname = @Lastname + +WHERE + UserID = @UserID + +-- Update the password ONLY if provided +IF @Password IS NOT NULL AND 0 < LEN(@Password) + UPDATE Users + SET [Password] = @Password + WHERE UserID = @UserID + +go + diff --git a/terraform/environments/tribunals/scripts/information_tribunal/sp_migration.sql b/terraform/environments/tribunals/scripts/information_tribunal/sp_migration.sql new file mode 100644 index 00000000000..aed75164961 --- /dev/null +++ b/terraform/environments/tribunals/scripts/information_tribunal/sp_migration.sql @@ -0,0 +1,2748 @@ +use it +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(varchar(255), name) from sysobjects where type = 'P' and name not like 'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName varchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId nvarchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(nvarchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(nvarchar(255), name) from sysobjects where type = N'P' and name not like N'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName nvarchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText varchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText nvarchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode nvarchar(255)) + + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select N'VCS'=sourcecode from #commenttext order by id + select N'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput nvarchar(255) +declare @hr int +declare @vchSource nvarchar(255) +declare @vchDescription nvarchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select uvalue + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID nvarchar(100) + select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText nvarchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR(N'Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist nvarchar(255)) + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +/* +** This procedure returns the version number of the stored +** procedures used by the Microsoft Visual Database Tools. +** Current version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue nvarchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + N'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +CREATE PROCEDURE [dbo].[spDelete] + +@DecisionID int + +AS + +DELETE FROM DECISIONS WHERE DecisionID = @DecisionID + +go + +CREATE PROCEDURE dbo.spGetAllDCs + +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [Disabled] +FROM MEMBERS +WHERE IsDC = 1 +ORDER BY Member +go + +CREATE PROCEDURE [dbo].[spGetAllDecisions] + +AS + +SELECT + [DecisionID], + D.[TypeID], + [Reference], + CONVERT(char(24),[PromulgatedDate],103) AS [PromulgatedDate], + [PresidentID], + M.Member AS P, + [QC1ID], + M1.Member AS QC1, + [QC2ID], + M2.Member AS QC2, + [DepChairID], + M3.Member AS DC, + [LayMember1ID], + M4.Member AS LM1, + [LayMember2ID], + M5.Member AS LM2, + [Appellant], + [Respondent], + [Parties], + [Summary], + D.[Disabled], + [PromulgatedDate] AS PromulgatedDate, + [Type] + +FROM DECISIONS AS D + +INNER JOIN TYPES AS T ON (D.TypeID = T.TypeID) +LEFT OUTER JOIN MEMBERS AS M ON (D.PresidentID = M.MemberID) +LEFT OUTER JOIN MEMBERS AS M1 ON (D.QC1ID = M1.MemberID) +LEFT OUTER JOIN MEMBERS AS M2 ON (D.QC2ID = M2.MemberID) +LEFT OUTER JOIN MEMBERS AS M3 ON (D.DepChairID = M3.MemberID) +LEFT OUTER JOIN MEMBERS AS M4 ON (D.LayMember1ID = M4.MemberID) +LEFT OUTER JOIN MEMBERS AS M5 ON (D.LayMember2ID = M5.MemberID) + +ORDER BY PromulgatedDate DESC + +go + +CREATE PROCEDURE dbo.spGetAllLMs + +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [Disabled] +FROM MEMBERS +WHERE IsLM = 1 +ORDER BY Member +go + +CREATE PROCEDURE dbo.spGetAllNSAPs + +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [Disabled] +FROM MEMBERS +WHERE IsNSAP = 1 +ORDER BY Member +go + +CREATE PROCEDURE [dbo].[spGetAllPublicDecisions] + +AS + +SELECT + [DecisionID], + D.[TypeID], + [Reference], + CONVERT(char(24),[PromulgatedDate],103) AS [PromulgatedDate], + [PresidentID], + M.Member AS P, + [QC1ID], + M1.Member AS QC1, + [QC2ID], + M2.Member AS QC2, + [DepChairID], + M3.Member AS DC, + [LayMember1ID], + M4.Member AS LM1, + [LayMember2ID], + M5.Member AS LM2, + [Appellant], + [Respondent], + [Parties], + [Summary], + D.[Disabled], + [PromulgatedDate] AS PromulgatedDate, + [Type] + +FROM DECISIONS AS D + +INNER JOIN TYPES AS T ON (D.TypeID = T.TypeID) +LEFT OUTER JOIN MEMBERS AS M ON (D.PresidentID = M.MemberID) +LEFT OUTER JOIN MEMBERS AS M1 ON (D.QC1ID = M1.MemberID) +LEFT OUTER JOIN MEMBERS AS M2 ON (D.QC2ID = M2.MemberID) +LEFT OUTER JOIN MEMBERS AS M3 ON (D.DepChairID = M3.MemberID) +LEFT OUTER JOIN MEMBERS AS M4 ON (D.LayMember1ID = M4.MemberID) +LEFT OUTER JOIN MEMBERS AS M5 ON (D.LayMember2ID = M5.MemberID) + +WHERE D.[Disabled] = 0 + +ORDER BY PromulgatedDate DESC + + +go + +CREATE PROCEDURE dbo.spGetAllTypes + +AS + +SELECT + [TypeID], + [Type], + [Disabled] +FROM TYPES + +ORDER BY Type ASC +go + +CREATE PROCEDURE dbo.spGetDCsForUse + +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [Disabled] +FROM MEMBERS +WHERE IsDC = 1 AND Disabled = 0 +ORDER BY Member +go + +CREATE PROCEDURE [dbo].[spGetDecisionByID] + + @DecisionId int + +AS + +SELECT + [DecisionID], + D.[TypeID], + [Reference], + CONVERT(char(24),[PromulgatedDate],103) AS [PromulgatedDate], + [PresidentID], + M.Member AS P, + [QC1ID], + M1.Member AS QC1, + [QC2ID], + M2.Member AS QC2, + [DepChairID], + M3.Member AS DC, + [LayMember1ID], + M4.Member AS LM1, + [LayMember2ID], + M5.Member AS LM2, + [Appellant], + [Respondent], + [Parties], + [Summary], + D.[Disabled], + [HigherCourt], + [PromulgatedDate] AS PromulgatedDate, + [Type] + +FROM DECISIONS AS D + +INNER JOIN TYPES AS T ON (D.TypeID = T.TypeID) +LEFT OUTER JOIN MEMBERS AS M ON (D.PresidentID = M.MemberID) +LEFT OUTER JOIN MEMBERS AS M1 ON (D.QC1ID = M1.MemberID) +LEFT OUTER JOIN MEMBERS AS M2 ON (D.QC2ID = M2.MemberID) +LEFT OUTER JOIN MEMBERS AS M3 ON (D.DepChairID = M3.MemberID) +LEFT OUTER JOIN MEMBERS AS M4 ON (D.LayMember1ID = M4.MemberID) +LEFT OUTER JOIN MEMBERS AS M5 ON (D.LayMember2ID = M5.MemberID) + +WHERE [DecisionId]=@DecisionID + +ORDER BY PromulgatedDate DESC +go + +CREATE PROCEDURE dbo.spGetLMsForUse + +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [Disabled] +FROM MEMBERS +WHERE IsLM = 1 AND Disabled = 0 +ORDER BY Member +go + +CREATE PROCEDURE dbo.spGetMembers +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [IsNSAP], + [IsDC], + [IsLM], + [Disabled] +FROM MEMBERS +ORDER BY Member ASC +go + +CREATE PROCEDURE dbo.spGetNSAPsForUse + +AS + +SELECT + [MemberID], + [Prefix], + [Member], + [Disabled] +FROM MEMBERS +WHERE IsNSAP = 1 AND Disabled = 0 +ORDER BY Member +go + +CREATE PROCEDURE dbo.spGetTypes +AS + +SELECT + [TypeID], + [Type], + [Disabled] +FROM Types + +go + +CREATE PROCEDURE dbo.spGetTypesForUse + +AS + +SELECT + [TypeID], + [Type], + [Disabled] +FROM TYPES +WHERE Disabled = 0 +ORDER BY Type ASC +go + +CREATE PROCEDURE dbo.spGetUsers +AS + +SELECT + [UserID], + [FirstName], + [LastName], + [UserName], + [Password], + [Disabled] +FROM Users +go + +CREATE PROCEDURE dbo.spGetUsersDetails + @UserID int + +AS + +SELECT + [UserID], + [FirstName], + [LastName], + [UserName], + [Password], + [Disabled] +FROM Users +WHERE + [UserID] = @UserID +go + +CREATE PROCEDURE dbo.spInsert + + @DecisionID bigint OUTPUT, + @JurisdictionID int, + @Jurisdiction1ID int, + @Date datetime, + @AppealNumber varchar (100), + @Appellant varchar (250), + @Respondent varchar (500), + @AddParties varchar (500), + @SubjectDetail ntext, + @Appealed varchar (50), + @Disabled bit, + @File1_Name varchar (250), + @File2_Name varchar (250), + @File2_Title varchar (250), + @File3_Name varchar (250), + @File3_Title varchar (250), + @AppealURL varchar (1000), + @TextURL varchar (250) +AS + +INSERT INTO DECISIONS1 ( + [JurisdictionID], + [Jurisdiction1ID], + [Date], + [AppealNumber], + [Appellant], + [Respondent], + [AddParties], + [SubjectDetail], + [Appealed], + [Disabled], + [File1_Name], + [File2_Name], + [File2_Title], + [File3_Name], + [File3_Title], + [AppealURL], + [TextURL] +) VALUES ( + @JurisdictionID, + @Jurisdiction1ID, + @Date, + @AppealNumber, + @Appellant, + @Respondent, + @AddParties, + @SubjectDetail, + @Appealed, + @Disabled, + @File1_Name, + @File2_Name, + @File2_Title, + @File3_Name, + @File3_Title, + @AppealURL, + @TextURL +) + +SELECT @DecisionID = SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spInsertDecision + + @DecisionID bigint OUTPUT, + @TypeID int, + @Reference varchar (50), + @PromulgatedDate datetime, + @PresidentID int, + @QC1ID int, + @QC2ID int, + @DepChairID int, + @LayMember1ID int, + @LayMember2ID int, + @Appellant varchar (250), + @Respondent varchar (500), + @Parties varchar (750), + @Summary ntext, + @Disabled bit, + @HigherCourt bit +AS + +INSERT INTO Decisions ( + [TypeID], + [Reference], + [PromulgatedDate], + [PresidentID], + [QC1ID], + [QC2ID], + [DepChairID], + [LayMember1ID], + [LayMember2ID], + [Appellant], + [Respondent], + [Parties], + [Summary], + [Disabled], + [HigherCourt] +) VALUES ( + @TypeID, + @Reference, + @PromulgatedDate, + @PresidentID, + @QC1ID, + @QC2ID, + @DepChairID, + @LayMember1ID, + @LayMember2ID, + @Appellant, + @Respondent, + @Parties, + @Summary, + @Disabled, + @HigherCourt +) + +SELECT @DecisionID = SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spInsertMember + @MemberID int OUTPUT, + @Prefix varchar (50), + @Member varchar(250), + @IsNSAP bit, + @IsDC bit, + @IsLM bit, + @Disabled bit +AS + +INSERT INTO Members ( + [Prefix], + [Member], + [IsNSAP], + [IsDC], + [IsLM], + [Disabled] +) VALUES ( + @Prefix, + @Member, + @IsNSAP, + @IsDC, + @IsLM, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spInsertType + @TypeID int OUTPUT, + @Type varchar(250), + @Disabled bit +AS + +INSERT INTO Types ( + [Type], + [Disabled] +) VALUES ( + @Type, + @Disabled +) + +select SCOPE_IDENTITY() + + +go + +CREATE PROCEDURE dbo.spInsertUser + @UserID int OUTPUT, + @FirstName varchar(250), + @LastName varchar(250), + @UserName varchar(250), + @Password varchar (250), + @Disabled bit +AS + +INSERT INTO Users ( + [FirstName], + [LastName], + [UserName], + [Password], + [Disabled] +) VALUES ( + @FirstName, + @LastName, + @UserName, + @Password, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spPopulateDetails + + @SubjectID int + +AS + +SELECT + [DetailID], + d.[SubjectID], + [Detail], + d.[Disabled] + +FROM DETAIL AS d + +INNER JOIN SUBJECT AS s ON d.SubjectID = s.SubjectID + +WHERE d.SubjectID = @SubjectID + +ORDER BY Detail ASC +go + +CREATE PROCEDURE dbo.spPopulateDetailsDataGrid + + @SubjectID int + +AS + +SELECT + [DetailID], + d.[SubjectID], + [Detail], + d.[Disabled], + s.Subject + +FROM DETAIL AS d + +INNER JOIN SUBJECT AS s ON d.SubjectID = s.SubjectID + +WHERE d.SubjectID = @SubjectID + +ORDER BY Detail ASC +go + +CREATE PROCEDURE dbo.spPopulateDetailsList + +AS + +SELECT + [DetailID], + [SubjectID], + [Detail], + [Disabled] + +FROM DETAIL + +WHERE Disabled = 0 + +ORDER BY Detail ASC +go + +CREATE PROCEDURE [dbo].[spPopulateForm] + + @DecisionId int + +AS + +SELECT + [DecisionID], + D.[JurisdictionID], + D.[Jurisdiction1ID], + [Date], + [AppealNumber], + [Appellant], + [Respondent], + [AddParties], + [SubjectDetail], + [Appealed], + D.[Disabled], + [File1_Name], + [File2_Name], + [File2_Title], + [File3_Name], + [File3_Title], + [AppealURL], + [TextURL], + T.[tax1id], + T.[description] AS [Description], + T1.[description] AS [Description1] + +FROM DECISIONS1 AS D + +INNER JOIN TAXONOMY1 AS T ON (D.JurisdictionID = T.tax1id) +LEFT OUTER JOIN TAXONOMY1 AS T1 ON (D.Jurisdiction1ID = T1.tax1id) + +WHERE [DecisionId]=@DecisionID +go + +CREATE PROCEDURE dbo.spPopulateJurisdictions + +AS + +SELECT + [JurisdictionID], + [Jurisdiction], + [Disabled] +FROM JURISDICTIONS +WHERE Disabled = 0 +ORDER BY JurisdictionID ASC +go + +CREATE PROCEDURE dbo.spPopulateJurisdictionsDataGrid + +AS + +SELECT + [JurisdictionID], + [Jurisdiction], + [Disabled] +FROM JURISDICTIONS +ORDER BY Jurisdiction ASC +go + +CREATE PROCEDURE dbo.spPopulateJurisdictionsPublic + +AS + +SELECT + [JurisdictionID], + [Jurisdiction], + [Disabled] +FROM JURISDICTIONS +ORDER BY JurisdictionID ASC +go + +CREATE PROCEDURE [dbo].[spPopulateRepeaterTable] + +AS + +SELECT + [DecisionID], + D.[JurisdictionID], + D.[Jurisdiction1ID], + CONVERT(char(24),[Date],103) AS [Date], + [AppealNumber], + [Appellant], + [Respondent], + [AddParties], + [SubjectDetail], + [Appealed], + D.[Disabled], + T.[tax1id], + T.[description] AS [Description], + T1.[description] AS [Description1] + +FROM DECISIONS1 AS D + +INNER JOIN TAXONOMY1 AS T ON (D.JurisdictionID = T.tax1id) +LEFT OUTER JOIN TAXONOMY1 AS T1 ON (D.Jurisdiction1ID = T1.tax1id) + +ORDER BY [Date] DESC +go + +CREATE PROCEDURE [dbo].[spPopulateRepeaterTablePublic] + +AS + +SELECT + [DecisionID], + D.[JurisdictionID], + D.[Jurisdiction1ID], + CONVERT(char(24),[Date],103) AS [Date], + [AppealNumber], + [Appellant], + [Respondent], + [AddParties], + [SubjectDetail], + [Appealed], + D.[Disabled], + [File1_Name], + [File2_Name], + [File2_Title], + [File3_Name], + [File3_Title], + [AppealURL], + [TextURL], + T.[tax1id], + T.[description] AS [Description], + T1.[description] AS [Description1], + [Date] as [d] + +FROM DECISIONS1 AS D + +INNER JOIN TAXONOMY1 AS T ON (D.JurisdictionID = T.tax1id) +LEFT OUTER JOIN TAXONOMY1 AS T1 ON (D.Jurisdiction1ID = T1.tax1id) + +WHERE D.[Disabled] = 0 + +ORDER BY [d] DESC +go + +CREATE PROCEDURE dbo.spPopulateSubjects + +AS + +SELECT + [SubjectID], + [Subject], + [Disabled] +FROM SUBJECT +WHERE Disabled = 0 +ORDER BY Subject ASC +go + +CREATE PROCEDURE dbo.spPopulateSubjectsDataGrid + +AS + +SELECT + [SubjectID], + [Subject], + [Disabled] +FROM SUBJECT +ORDER BY Subject ASC +go + +CREATE PROCEDURE dbo.spPopulateTaxonomy1 + +AS + +SELECT + [tax1ID], + [description], + [disabled] +FROM Taxonomy1 +WHERE Disabled = 0 +ORDER BY [description] ASC +go + +CREATE PROCEDURE dbo.spPopulateTaxonomy1DataGrid + +AS + +SELECT + [tax1id], + [description], + [disabled] +FROM TAXONOMY1 +ORDER BY [description] ASC +go + +CREATE PROCEDURE dbo.spPopulateTaxonomy2 + +@tax1id int + +AS + +SELECT + t2.[tax2id], + t2.[description], + t2.[tax1id], + t1.[tax1id], + t2.[disabled] +FROM Taxonomy2 t2 +INNER JOIN Taxonomy1 t1 ON t2.tax1id = t1.tax1id +WHERE t2.tax1id = @tax1id AND t2.disabled = 0 +ORDER BY t2.[description] ASC +go + +CREATE PROCEDURE dbo.spPopulateTaxonomy2DataGrid + +@tax1id int + +AS + +SELECT + t2.[tax2id], + t2.[description], + t1.[description] AS Jurisdiction, + t2.[tax1id], + t1.[tax1id], + t2.[disabled] +FROM Taxonomy2 t2 +INNER JOIN Taxonomy1 t1 ON t2.tax1id = t1.tax1id +WHERE t2.tax1id = @tax1id AND t2.disabled = 0 +ORDER BY t2.[description] ASC +go + +CREATE PROCEDURE dbo.spPopulateTaxonomy3 + +@tax2id int, +@tax1id int + +AS + +SELECT + t3.[tax3id], + t3.[tax2id], + t2.[tax2id], + t3.[description], + t3.[tax1id], + t3.[disabled] +FROM Taxonomy3 t3 +INNER JOIN Taxonomy2 t2 ON t3.tax2id = t2.tax2id +WHERE t3.tax1id = @tax1id AND t3.tax2id = @tax2id AND t3.disabled = 0 +ORDER BY t3.[description] ASC +go + +CREATE PROCEDURE dbo.spPopulateTaxonomy3DataGrid + +@tax2id int + +AS + +SELECT + t3.[tax3id], + t3.[tax2id], + t2.[tax2id], + t2.[description] AS Subject, + t3.[description], + t3.[tax1id], + t3.[disabled], + t1.[description] AS Jurisdiction, + t2.[tax1id], + t1.[tax1id] +FROM Taxonomy3 t3 +INNER JOIN Taxonomy1 t1 ON t3.tax1id = t1.tax1id +INNER JOIN Taxonomy2 t2 ON t3.tax2id = t2.tax2id +WHERE t3.tax2id = @tax2id +ORDER BY t3.[description] ASC +go + +CREATE PROCEDURE dbo.spSaveDetail + + @SubjectID int, + @Detail varchar (300), + @Disabled bit +AS + +INSERT INTO DETAIL ( + [SubjectID], + [Detail], + [Disabled] +) VALUES ( + @SubjectID, + @Detail, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spSaveJurisdiction + + @JurisdictionID int OUTPUT, + @Jurisdiction varchar (300), + @Disabled bit +AS + +INSERT INTO Jurisdictions ( + [Jurisdiction], + [Disabled] +) VALUES ( + @Jurisdiction, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spSaveSubject + + @SubjectID int OUTPUT, + @Subject varchar (300), + @Disabled bit +AS + +INSERT INTO SUBJECT ( + [Subject], + [Disabled] +) VALUES ( + @Subject, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spSaveTaxonomy1 + + @tax1id int OUTPUT, + @description varchar (1000), + @Disabled bit +AS + +INSERT INTO Taxonomy1 ( + [description], + [Disabled] +) VALUES ( + @description, + @Disabled +) + +select SCOPE_IDENTITY() +go + +CREATE PROCEDURE dbo.spUpdate + + @DecisionID bigint OUTPUT, + @JurisdictionID int, + @Jurisdiction1ID int, + @Date datetime, + @AppealNumber varchar (100), + @Appellant varchar (250), + @Respondent varchar (500), + @AddParties varchar (500), + @SubjectDetail ntext, + @Appealed varchar (50), + @Disabled bit, + @File1_Name varchar (250), + @File2_Name varchar (250), + @File2_Title varchar (250), + @File3_Name varchar (250), + @File3_Title varchar (250), + @AppealURL varchar (1000), + @TextURL varchar (250) +AS + +UPDATE DECISIONS1 SET + [JurisdictionID] = @JurisdictionID, + [Jurisdiction1ID] = @Jurisdiction1ID, + [Date] = @Date, + [AppealNumber] = @AppealNumber, + [Appellant] = @Appellant, + [Respondent] = @Respondent, + [AddParties] = @AddParties, + [SubjectDetail] = @SubjectDetail, + [Appealed] = @Appealed, + [Disabled] = @Disabled, + [File1_Name] = @File1_Name, + [File2_Name] = @File2_Name, + [File2_Title] = @File2_Title, + [File3_Name] = @File3_Name, + [File3_Title] = @File3_Title, + [AppealURL] = @AppealURL, + [TextURL] = @TextURL + +WHERE [DecisionID] = @DecisionID +go + +CREATE PROCEDURE dbo.spUpdateDecision + + @DecisionID bigint, + @TypeID int, + @Reference varchar (50), + @PromulgatedDate datetime, + @PresidentID int, + @QC1ID int, + @QC2ID int, + @DepChairID int, + @LayMember1ID int, + @LayMember2ID int, + @Appellant varchar (250), + @Respondent varchar (500), + @Parties varchar (750), + @Summary ntext, + @Disabled bit, + @HigherCourt bit +AS + +UPDATE Decisions SET + [TypeID] = @TypeID, + [Reference] = @Reference, + [PromulgatedDate] = @PromulgatedDate, + [PresidentID] = @PresidentID, + [QC1ID] = @QC1ID, + [QC2ID] = @QC2ID, + [DepChairID] = @DepChairID, + [LayMember1ID] = @LayMember1ID, + [LayMember2ID] = @LayMember2ID, + [Appellant] = @Appellant, + [Respondent] = @Respondent, + [Parties] = @Parties, + [Summary] = @Summary, + [Disabled] = @Disabled, + [HigherCourt] = @HigherCourt +WHERE + [DecisionID] = @DecisionID +go + +CREATE PROCEDURE dbo.spUpdateDetail + + @DetailID int, + @Detail varchar (300), + @Disabled bit +AS + +UPDATE DETAIL SET + + [Detail] = @Detail, + [Disabled] = @Disabled + +WHERE [DetailID] = @DetailID +go + +CREATE PROCEDURE dbo.spUpdateJurisdiction + + @JurisdictionID int, + @Jurisdiction varchar (300), + @Disabled bit +AS + +UPDATE Jurisdictions SET + + [Jurisdiction] = @Jurisdiction, + [Disabled] = @Disabled + +WHERE [JurisdictionID] = @JurisdictionID +go + +CREATE PROCEDURE dbo.spUpdateMember + @MemberID int, + @Prefix varchar (50), + @Member varchar(250), + @IsNSAP bit, + @IsDC bit, + @IsLM bit, + @Disabled bit +AS + +UPDATE Members SET + [Prefix] = @Prefix, + [Member] = @Member, + [IsNSAP] = @IsNSAP, + [IsDC] = @IsDC, + [IsLM] = @IsLM, + [Disabled] = @Disabled +WHERE + [MemberID] = @MemberID +go + +CREATE PROCEDURE dbo.spUpdateSubject + + @SubjectID int, + @Subject varchar (300), + @Disabled bit +AS + +UPDATE SUBJECT SET + + [Subject] = @Subject, + [Disabled] = @Disabled + +WHERE [SubjectID] = @SubjectID +go + +CREATE PROCEDURE dbo.spUpdateTaxonomy1 + @tax1id int, + @description varchar (1000), + @Disabled bit +AS + +UPDATE Taxonomy1 SET + + [description] = @description, + [Disabled] = @Disabled + +WHERE [tax1id] = @tax1id +go + +CREATE PROCEDURE dbo.spUpdateType + @TypeID int, + @Type varchar(250), + @Disabled bit +AS + +UPDATE Types SET + [Type] = @Type, + [Disabled] = @Disabled +WHERE + [TypeID] = @TypeID + + +go + +CREATE PROCEDURE dbo.spUpdateUser + @UserID int, + @FirstName varchar(250), + @LastName varchar(250), + @UserName varchar(250), + @Password varchar(250), + @Disabled bit +AS + +UPDATE Users SET + [FirstName] = @FirstName, + [LastName] = @LastName, + [UserName] = @UserName, + [Password] = @Password, + [Disabled] = @Disabled +WHERE + [UserID] = @UserID +go + +CREATE PROCEDURE [dbo].[spUsersAuthenticate] + +@UserName varchar (250), +@Password varchar (250) + +AS SELECT * FROM USERS +WHERE UserName = @UserName AND [Password] = @Password +go + diff --git a/terraform/environments/tribunals/scripts/lands_chamber/sp_migration.sql b/terraform/environments/tribunals/scripts/lands_chamber/sp_migration.sql new file mode 100644 index 00000000000..2a9efe9be8c --- /dev/null +++ b/terraform/environments/tribunals/scripts/lands_chamber/sp_migration.sql @@ -0,0 +1,2510 @@ +use lands +go + + +CREATE FUNCTION [dbo].[awsdms_fn_LsnSegmentToHexa] (@InputData VARBINARY(32)) RETURNS VARCHAR(64) +AS + BEGIN + DECLARE @HexDigits CHAR(16), + @OutputData VARCHAR(64), + @i INT, + @InputDataLength INT + + DECLARE @ByteInfo INT, + @LeftNibble INT, + @RightNibble INT + + SET @OutputData = '' + + SET @i = 1 + + SET @InputDataLength = DATALENGTH(@InputData) + + SET @HexDigits = '0123456789abcdef' + + WHILE (@i <= @InputDataLength) + BEGIN + SET @ByteInfo = CONVERT(INT,SUBSTRING(@InputData,@i,1)) + SET @LeftNibble= FLOOR(@ByteInfo / 16) + SET @RightNibble = @ByteInfo - (@LeftNibble* 16) + SET @OutputData = @OutputData + SUBSTRING(@HexDigits,@LeftNibble+ 1,1) + SUBSTRING(@HexDigits,@RightNibble + 1,1) + SET @i = @i + 1 + END + + RETURN @OutputData + + END +go + + +CREATE FUNCTION [dbo].[awsdms_fn_NumericLsnToHexa](@numeric25Lsn numeric(25,0)) returns varchar(32) + AS + BEGIN +-- In order to avoid form sign overflow problems - declare the LSN segments +-- to be one 'type' larger than the intendent target type. +-- For example, convert(smallint, convert(numeric(25,0),65535)) will fail +-- but convert(binary(2), convert(int,convert(numeric(25,0),65535))) will give the +-- expected result of 0xffff. + +declare @high4bytelsnSegment bigint,@mid4bytelsnSegment bigint,@low2bytelsnSegment int +declare @highFactor bigint, @midFactor int + +declare @lsnLeftSeg binary(4) +declare @lsnMidSeg binary(4) +declare @lsnRightSeg binary(2) + +declare @hexaLsn varchar(32) + +select @highFactor = 1000000000000000 +select @midFactor = 100000 + +select @high4bytelsnSegment = convert(bigint, floor(@numeric25Lsn / @highFactor)) +select @numeric25Lsn = @numeric25Lsn - convert(numeric(25,0), @high4bytelsnSegment) * @highFactor +select @mid4bytelsnSegment = convert(bigint,floor(@numeric25Lsn / @midFactor )) +select @numeric25Lsn = @numeric25Lsn - convert(numeric(25,0), @mid4bytelsnSegment) * @midFactor +select @low2bytelsnSegment = convert(int, @numeric25Lsn) + +set @lsnLeftSeg = convert(binary(4), @high4bytelsnSegment) +set @lsnMidSeg = convert(binary(4), @mid4bytelsnSegment) +set @lsnRightSeg = convert(binary(2), @low2bytelsnSegment) + +return [dbo].[awsdms_fn_LsnSegmentToHexa](@lsnLeftSeg)+':'+[dbo].[awsdms_fn_LsnSegmentToHexa](@lsnMidSeg)+':'+[dbo].[awsdms_fn_LsnSegmentToHexa](@lsnRightSeg) +END +go + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(varchar(255), name) from sysobjects where type = 'P' and name not like 'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName varchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId nvarchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(nvarchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(nvarchar(255), name) from sysobjects where type = N'P' and name not like N'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName nvarchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +go + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +go + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText varchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText nvarchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode nvarchar(255)) + + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select N'VCS'=sourcecode from #commenttext order by id + select N'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput nvarchar(255) +declare @hr int +declare @vchSource nvarchar(255) +declare @vchDescription nvarchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +go + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +go + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +go + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +go + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +go + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select uvalue + from dbo.dtproperties + where @id=objectid and @property=property + ) + +go + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +go + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID nvarchar(100) + select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText nvarchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR(N'Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist nvarchar(255)) + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +go + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +go + +/* +** If the property already exists, reset the value; otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +go + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +go + +/* +** This procedure returns the version number of the stored +** procedures used by the Microsoft Visual Database Tools. +** Current version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +go + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +go + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue nvarchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + N'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +go + + + + + +CREATE PROCEDURE [dbo].[spAddCategory] + +@description varchar(100) + +AS + +INSERT + Category + ( + [description] + ) + +VALUES + ( + @description + ) + + +go + + + + + +CREATE PROCEDURE [dbo].[spAddCommissioner] + +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +INSERT + Commissioner + ( + prefix, + surname, + suffix + ) + +VALUES + ( + @Prefix, + @surname, + @suffix + ) + + +go + + + + +CREATE PROCEDURE [dbo].[spAddCommissionerJudgmentMap] + +@CommissionerID int, +@JudgmentID int + +AS + +INSERT + CommissionerJudgmentMap + ( + Commissioner_ID, + Judgment_ID + ) + + VALUES + ( + @CommissionerID, + @JudgmentID + ) + + +go + + + + + +CREATE PROCEDURE [dbo].[spAddJudgment] + +@Id int OUTPUT, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(4), +@Reported_no_1 varchar(3), +@Reported_no_2 varchar(3), +@Reported_no_3 varchar(2) + +AS + +DECLARE @Publication_datetime datetime + +IF @Is_published = 1 + SELECT @Publication_datetime = getdate() + + + INSERT + Judgment + ( + Is_published, + File_no_1, + File_no_2, + Decision_datetime, + Claimants, + Respondent, + Main_subcategory_id, + Sec_subcategory_id, + Headnote_summary, + Created_datetime, + Last_updatedtime, + Publication_datetime, + File_no_3, + Reported_no_1, + Reported_no_2, + Reported_no_3 + ) + + VALUES + ( + @Is_published, + @File_no_1, + @File_no_2, + @Decision_datetime, + @Claimants, + @Respondent, + @Main_subcategory_id, + @Sec_subcategory_id, + @Headnote_summary, + getdate(), + getdate(), + @Publication_datetime, + @File_no_3, + @Reported_no_1, + @Reported_no_2, + @Reported_no_3 + ) + + SELECT @Id = SCOPE_IDENTITY() + + +go + + + + + +CREATE PROCEDURE [dbo].[spAddSubCategory] + +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +INSERT + Subcategory + ( + parent_num, + [description], + num + ) + +VALUES + ( + @parent_num, + @description, + @num + ) + + +go + + + + +CREATE PROCEDURE [dbo].[spAddUser] + +@UserID integer OUTPUT, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +INSERT + Users + + ( + Username, + [Password], + Firstname, + Lastname + ) + +VALUES + + ( + @Username, + @Password, + @Firstname, + @Lastname + ) + +SELECT @UserID = SCOPE_IDENTITY() + + +go + + + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsByCommissioner] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + CommissionerJudgmentMap + +WHERE + commissioner_id = @Id + + +go + + + + + +CREATE PROCEDURE [dbo].[spCountJudgmentsBySubCategory] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + Judgment + +WHERE + main_subcategory_id = @Id + + OR + + sec_subcategory_id = @Id + + +go + + + + + +CREATE PROCEDURE [dbo].[spCountSubCategoryByCategory] + +@CategoryId int + +AS + +SELECT + COUNT(*) + +FROM + Subcategory + +WHERE + parent_num = @CategoryId + + +go + + + + + +CREATE PROCEDURE [dbo].[spDeleteCategory] + +@Id int + +AS + +DELETE + Category + +WHERE + Num = @Id + + +go + + + + + +CREATE PROCEDURE [dbo].[spDeleteCommissioner] + +@Id int + +AS + +DELETE + Commissioner + +WHERE + [Id] = @Id + + +go + + + + +CREATE PROCEDURE [dbo].[spDeleteCommissionerJudgmentMap] + +@JudgmentID int + +AS + +DELETE + CommissionerJudgmentMap + +WHERE + Judgment_Id = @JudgmentID + + +go + + + + + +CREATE PROCEDURE [dbo].[spDeleteSubCategory] + +@Id int + +AS + +DELETE + SubCategory + +WHERE + [Id] = @Id + + +go + + + + +CREATE PROCEDURE [dbo].[spDeleteUser] + +@UserID int + +AS + +DECLARE +@Count int + +SELECT @Count = COUNT(*) FROM Users + +IF @Count > 1 + BEGIN + DELETE + Users + + WHERE + UserID = @UserID + END + + +go + + + + + +CREATE PROCEDURE [dbo].[spGetCategoryList] + +AS + +SELECT num, [description] +FROM category +ORDER BY num + + +go + + + + +CREATE PROCEDURE [dbo].[spGetCommissionerList] + +AS + +select [id], prefix, surname, suffix +from commissioner +order by surname + + +go + + + + +CREATE PROCEDURE [dbo].[spGetCommissionerListSelected] + +@DecisionId int + +AS + +select [id], prefix, surname, suffix, isnull(m.commid, 0) as commid +from commissioner c +left join + ( + select commissioner_id as commid + from commissionerjudgmentmap cjm + where judgment_id = @DecisionId + ) as m +on c.[id] = m.commid +order by commid desc, c.surname + + +go + + + + + +CREATE PROCEDURE [dbo].[spGetDecision] + +@DecisionId int + +AS + +-- Main result set - ds.Tables(0) +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid, + j.file_no_1 + ' ' + j.file_no_2 + ' ' + j.file_no_3 as 'filenumber' +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId + +-- Selected commissioners result set - ds.Tables(1) +select cm.prefix, cm.surname, cm.suffix, cm.[id] as 'commissionerid', + cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as 'commissioner' +from judgment j +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +where cjm.judgment_id = @DecisionId + +go + + + + + +CREATE PROCEDURE [dbo].[spGetDecisionPublished] + +@DecisionId int, +@Is_Published bit + +AS + +-- Main result set - ds.Tables(0) +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid, + j.file_no_1 + '-' + j.file_no_3 + '-' + j.file_no_2 as 'filenumber' +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +and is_published = @Is_Published + +-- Selected commissioners result set - ds.Tables(1) +select cm.prefix, cm.surname, cm.suffix, cm.[id] as 'commissionerid', + cm.prefix + ' ' + cm.surname + ' ' + cm.suffix as 'commissioner' +from judgment j +left join commissionerjudgmentmap cjm on j.[id] = cjm.judgment_id +left join commissioner cm on cjm.commissioner_id = cm.[id] +where cjm.judgment_id = @DecisionId + +go + + + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryList] + +AS + +SELECT [id], parent_num, num, [description] +FROM subcategory +ORDER BY parent_num, num + + +go + + + + + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] + +@CategoryId int + +AS + +SELECT [id], parent_num, s.num, s.[description], c.[description] as categoryname +FROM subcategory s +inner join category c on s.parent_num = c.num +WHERE parent_num = @CategoryId +ORDER BY s. num + + +go + + + + +CREATE PROCEDURE [dbo].[spGetUser] + +@UserID int + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +WHERE + UserID = @UserID + + +go + + + + +CREATE PROCEDURE [dbo].[spGetUserList] + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + + +go + + + + + +CREATE PROCEDURE [dbo].[spLoginUser] + +@Username varchar(50), +@Password varchar(50) + +AS + +SELECT + * + +FROM + Users + +WHERE + Username = @Username + +AND + [Password] = @Password + + +go + + + + + +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@id int, +@description varchar(100) + +AS + +UPDATE + Category + +SET + [description] = @description + +WHERE + num = @id + + +go + + + + + +CREATE PROCEDURE [dbo].[spUpdateCommissioner] + +@id int, +@Prefix varchar(100), +@Surname varchar(300), +@Suffix varchar(100) + +AS + +UPDATE + Commissioner + +SET + prefix = @Prefix, + surname = @surname, + suffix = @suffix + +WHERE + [id] = @id + + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateCommissionerJudgmentMap] + +@CommissionerID int, +@JudgmentID int + +AS + +UPDATE + CommissionerJudgmentMap + +SET + Commissioner_ID = @CommissionerID + +WHERE + Judgment_ID = @JudgmentID + + +go + + + + + +CREATE PROCEDURE [dbo].[spUpdateJudgment] + +@Id int, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext, +@File_no_3 varchar(4), +@Reported_no_1 varchar(3), +@Reported_no_2 varchar(3), +@Reported_no_3 varchar(2) + +AS + +DECLARE +@Publication_datetime datetime, +@CurrentlyPublished bit + +SELECT @CurrentlyPublished = Is_Published FROM Judgment WHERE [id] = @Id + +IF (@CurrentlyPublished = 0 AND @Is_published = 1) + BEGIN + SELECT @Publication_datetime = getdate() + END + +IF @CurrentlyPublished = 1 + BEGIN + SELECT @Publication_datetime = Publication_datetime FROM Judgment WHERE [id] = @Id + END + +IF @Is_published = 0 + BEGIN + SELECT @Publication_datetime = NULL + END + + + UPDATE + Judgment + + SET + Is_Published = @Is_Published, + File_no_1 = @File_no_1, + File_no_2 = @File_no_2, + File_no_3 = @File_no_3, + Decision_datetime = @Decision_datetime, + Claimants = @Claimants, + Respondent = @Respondent, + Main_subcategory_id = @Main_subcategory_id, + Sec_subcategory_id = @Sec_subcategory_id, + Headnote_summary = @Headnote_summary, + Last_updatedtime = getdate(), + Publication_datetime = @Publication_datetime, + Reported_no_1 = @Reported_no_1, + Reported_no_2 = @Reported_no_2, + Reported_no_3 = @Reported_no_3 + + WHERE + [Id] = @Id + + +go + + + + + +CREATE PROCEDURE [dbo].[spUpdateSubCategory] + +@id int, +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +UPDATE + Subcategory + +SET + parent_num = @parent_num, + [description] = @description, + num = @num + +WHERE + [id] = @id + + +go + + + + +CREATE PROCEDURE [dbo].[spUpdateUser] + +@UserID int, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +UPDATE + Users + +SET + Username = @Username, + Firstname = @Firstname, + Lastname = @Lastname + +WHERE + UserID = @UserID + +-- Update the password ONLY if provided +IF @Password IS NOT NULL AND 0 < LEN(@Password) + UPDATE Users + SET [Password] = @Password + WHERE UserID = @UserID + + +go + diff --git a/terraform/environments/tribunals/scripts/transport/sp_migration.sql b/terraform/environments/tribunals/scripts/transport/sp_migration.sql new file mode 100644 index 00000000000..18dfecdd140 --- /dev/null +++ b/terraform/environments/tribunals/scripts/transport/sp_migration.sql @@ -0,0 +1,2232 @@ +use transport; +GO + + +CREATE FUNCTION [dbo].[awsdms_fn_LsnSegmentToHexa] (@InputData VARBINARY(32)) RETURNS VARCHAR(64) +AS + BEGIN + DECLARE @HexDigits CHAR(16), + @OutputData VARCHAR(64), + @i INT, + @InputDataLength INT + + DECLARE @ByteInfo INT, + @LeftNibble INT, + @RightNibble INT + + SET @OutputData = '' + + SET @i = 1 + + SET @InputDataLength = DATALENGTH(@InputData) + + SET @HexDigits = '0123456789abcdef' + + WHILE (@i <= @InputDataLength) + BEGIN + SET @ByteInfo = CONVERT(INT,SUBSTRING(@InputData,@i,1)) + SET @LeftNibble= FLOOR(@ByteInfo / 16) + SET @RightNibble = @ByteInfo - (@LeftNibble* 16) + SET @OutputData = @OutputData + SUBSTRING(@HexDigits,@LeftNibble+ 1,1) + SUBSTRING(@HexDigits,@RightNibble + 1,1) + SET @i = @i + 1 + END + + RETURN @OutputData + + END +; +GO + + +CREATE FUNCTION [dbo].[awsdms_fn_NumericLsnToHexa](@numeric25Lsn numeric(25,0)) returns varchar(32) + AS + BEGIN +-- In order to avoid form sign overflow problems - declare the LSN segments +-- to be one 'type' larger than the intendent target type. +-- For example, convert(smallint, convert(numeric(25,0),65535)) will fail +-- but convert(binary(2), convert(int,convert(numeric(25,0),65535))) will give the +-- expected result of 0xffff. + +declare @high4bytelsnSegment bigint,@mid4bytelsnSegment bigint,@low2bytelsnSegment int +declare @highFactor bigint, @midFactor int + +declare @lsnLeftSeg binary(4) +declare @lsnMidSeg binary(4) +declare @lsnRightSeg binary(2) + +declare @hexaLsn varchar(32) + +select @highFactor = 1000000000000000 +select @midFactor = 100000 + +select @high4bytelsnSegment = convert(bigint, floor(@numeric25Lsn / @highFactor)) +select @numeric25Lsn = @numeric25Lsn - convert(numeric(25,0), @high4bytelsnSegment) * @highFactor +select @mid4bytelsnSegment = convert(bigint,floor(@numeric25Lsn / @midFactor )) +select @numeric25Lsn = @numeric25Lsn - convert(numeric(25,0), @mid4bytelsnSegment) * @midFactor +select @low2bytelsnSegment = convert(int, @numeric25Lsn) + +set @lsnLeftSeg = convert(binary(4), @high4bytelsnSegment) +set @lsnMidSeg = convert(binary(4), @mid4bytelsnSegment) +set @lsnRightSeg = convert(binary(2), @low2bytelsnSegment) + +return [dbo].[awsdms_fn_LsnSegmentToHexa](@lsnLeftSeg)+':'+[dbo].[awsdms_fn_LsnSegmentToHexa](@lsnMidSeg)+':'+[dbo].[awsdms_fn_LsnSegmentToHexa](@lsnRightSeg) +END +; +GO + +create proc dbo.dt_addtosourcecontrol + @vchSourceSafeINI varchar(255) = '', + @vchProjectName varchar(255) ='', + @vchComment varchar(255) ='', + @vchLoginName varchar(255) ='', + @vchPassword varchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId varchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(varchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(varchar(255), name) from sysobjects where type = 'P' and name not like 'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName varchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +; +GO + +create proc dbo.dt_addtosourcecontrol_u + @vchSourceSafeINI nvarchar(255) = '', + @vchProjectName nvarchar(255) ='', + @vchComment nvarchar(255) ='', + @vchLoginName nvarchar(255) ='', + @vchPassword nvarchar(255) ='' + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @vchDatabaseName varchar(255) +select @vchDatabaseName = db_name() + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iPropertyObjectId int +declare @vchParentId nvarchar(255) + +declare @iObjectCount int +select @iObjectCount = 0 + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + + /* Create Project in SS */ + exec @iReturn = sp_OAMethod @iObjectId, + 'AddProjectToSourceSafe', + NULL, + @vchSourceSafeINI, + @vchProjectName output, + @@SERVERNAME, + @vchDatabaseName, + @vchLoginName, + @vchPassword, + @vchComment + + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + /* Set Database Properties */ + + begin tran SetProperties + + /* add high level object */ + + exec @iPropertyObjectId = dbo.dt_adduserobject_vcs 'VCSProjectID' + + select @vchParentId = CONVERT(nvarchar(255),@iPropertyObjectId) + + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProjectID', @vchParentId , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSProject' , @vchProjectName , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSourceSafeINI' , @vchSourceSafeINI , NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLServer', @@SERVERNAME, NULL + exec dbo.dt_setpropertybyid_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName, NULL + + if @@error <> 0 GOTO E_General_Error + + commit tran SetProperties + + declare cursorProcNames cursor for + select convert(nvarchar(255), name) from sysobjects where type = N'P' and name not like N'dt_%' + open cursorProcNames + + while 1 = 1 + begin + declare @vchProcName nvarchar(255) + fetch next from cursorProcNames into @vchProcName + if @@fetch_status <> 0 + break + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchProcName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @@SERVERNAME, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchProcName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = 0, + @iActionFlag = 0, + @sStream = '' + + if @iReturn = 0 select @iObjectCount = @iObjectCount + 1 + + end + +CleanUp: + close cursorProcNames + deallocate cursorProcNames + select @vchProjectName + select @iObjectCount + return + +E_General_Error: + /* this is an all or nothing. No specific error messages */ + goto CleanUp + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +; +GO + +/* +** Add an object to the dtproperties table +*/ +create procedure dbo.dt_adduserobject +as + set nocount on + /* + ** Create the user object if it does not exist already + */ + begin transaction + insert dbo.dtproperties (property) VALUES ('DtgSchemaOBJECT') + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property='DtgSchemaOBJECT' + commit + return @@identity +; +GO + +create procedure dbo.dt_adduserobject_vcs + @vchProperty varchar(64) + +as + +set nocount on + +declare @iReturn int + /* + ** Create the user object if it does not exist already + */ + begin transaction + select @iReturn = objectid from dbo.dtproperties where property = @vchProperty + if @iReturn IS NULL + begin + insert dbo.dtproperties (property) VALUES (@vchProperty) + update dbo.dtproperties set objectid=@@identity + where id=@@identity and property=@vchProperty + select @iReturn = @@identity + end + commit + return @iReturn + + +; +GO + +create proc dbo.dt_checkinobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255)='', + @vchLoginName varchar(255), + @vchPassword varchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece varchar(255) + select @vchProcLinePiece = convert(varchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, 'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +; +GO + +create proc dbo.dt_checkinobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255)='', + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255)='', + @iVCSFlags int = 0, + @iActionFlag int = 0, /* 0 => AddFile, 1 => CheckIn */ + @txStream1 Text = '', /* There is a bug that if items are NULL they do not pass to OLE servers */ + @txStream2 Text = '', + @txStream3 Text = '' + + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + +declare @iPropertyObjectId int +select @iPropertyObjectId = 0 + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + if @iActionFlag = 1 + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + begin tran compile_all + + /* try to compile the streams */ + exec (@txStream1) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream2) + if @@error <> 0 GOTO E_Compile_Fail + + exec (@txStream3) + if @@error <> 0 GOTO E_Compile_Fail + end + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = @txStream2 + end + else + begin + declare @iStreamObjectId int + declare @iReturnValue int + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + select colid, text into #ProcLines + from syscomments + where id = object_id(@vchObjectName) + order by colid + + declare @iCurProcLine int + declare @iProcLines int + select @iCurProcLine = 1 + select @iProcLines = (select count(*) from #ProcLines) + while @iCurProcLine <= @iProcLines + begin + declare @pos int + select @pos = 1 + declare @iCurLineSize int + select @iCurLineSize = len((select text from #ProcLines where colid = @iCurProcLine)) + while @pos <= @iCurLineSize + begin + declare @vchProcLinePiece nvarchar(255) + select @vchProcLinePiece = convert(nvarchar(255), + substring((select text from #ProcLines where colid = @iCurProcLine), + @pos, 255 )) + exec @iReturn = sp_OAMethod @iStreamObjectId, N'AddStream', @iReturnValue OUT, @vchProcLinePiece + if @iReturn <> 0 GOTO E_OAError + select @pos = @pos + 255 + end + select @iCurProcLine = @iCurProcLine + 1 + end + drop table #ProcLines + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckIn_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sObjectName = @vchObjectName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag, + @sStream = '' + end + + if @iReturn <> 0 GOTO E_OAError + + if @iActionFlag = 1 + begin + commit tran compile_all + if @@error <> 0 GOTO E_Compile_Fail + end + + end + +CleanUp: + return + +E_Compile_Fail: + declare @lerror int + select @lerror = @@error + rollback tran compile_all + RAISERROR (@lerror,16,-1) + goto CleanUp + +E_OAError: + if @iActionFlag = 1 rollback tran compile_all + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +; +GO + +create proc dbo.dt_checkoutobject + @chObjectType char(4), + @vchObjectName varchar(255), + @vchComment varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText varchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode varchar(255)) + + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select 'VCS'=sourcecode from #commenttext order by id + select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +; +GO + +create proc dbo.dt_checkoutobject_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchComment nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255), + @iVCSFlags int = 0, + @iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @vchTempText nvarchar(255) + +/* this is for our strings */ +declare @iStreamObjectId int +select @iStreamObjectId = 0 + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + /* Procedure Can have up to three streams + Drop Stream, Create Stream, GRANT stream */ + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'CheckOut_StoredProcedure', + NULL, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sComment = @vchComment, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword, + @iVCSFlags = @iVCSFlags, + @iActionFlag = @iActionFlag + + if @iReturn <> 0 GOTO E_OAError + + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #commenttext (id int identity, sourcecode nvarchar(255)) + + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText + end + + select N'VCS'=sourcecode from #commenttext order by id + select N'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +; +GO + +CREATE PROCEDURE dbo.dt_displayoaerror + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput varchar(255) +declare @hr int +declare @vchSource varchar(255) +declare @vchDescription varchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +; +GO + +CREATE PROCEDURE dbo.dt_displayoaerror_u + @iObject int, + @iresult int +as + +set nocount on + +declare @vchOutput nvarchar(255) +declare @hr int +declare @vchSource nvarchar(255) +declare @vchDescription nvarchar(255) + + exec @hr = sp_OAGetErrorInfo @iObject, @vchSource OUT, @vchDescription OUT + + select @vchOutput = @vchSource + ': ' + @vchDescription + raiserror (@vchOutput,16,-1) + + return + +; +GO + +/* +** Drop one or all the associated properties of an object or an attribute +** +** dt_dropproperties objid, null or '' -- drop all properties of the object itself +** dt_dropproperties objid, property -- drop the property +*/ +create procedure dbo.dt_droppropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + delete from dbo.dtproperties where objectid=@id + else + delete from dbo.dtproperties + where objectid=@id and property=@property + +; +GO + +/* +** Drop an object from the dbo.dtproperties table +*/ +create procedure dbo.dt_dropuserobjectbyid + @id int +as + set nocount on + delete from dbo.dtproperties where objectid=@id +; +GO + +/* +** Generate an ansi name that is unique in the dtproperties.value column +*/ +create procedure dbo.dt_generateansiname(@name varchar(255) output) +as + declare @prologue varchar(20) + declare @indexstring varchar(20) + declare @index integer + + set @prologue = 'MSDT-A-' + set @index = 1 + + while 1 = 1 + begin + set @indexstring = cast(@index as varchar(20)) + set @name = @prologue + @indexstring + if not exists (select value from dtproperties where value = @name) + break + + set @index = @index + 1 + + if (@index = 10000) + goto TooMany + end + +Leave: + + return + +TooMany: + + set @name = 'DIAGRAM' + goto Leave +; +GO + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop + @property varchar(30), + @value varchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@value is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and value=@value +; +GO + +/* +** Retrieve the owner object(s) of a given property +*/ +create procedure dbo.dt_getobjwithprop_u + @property varchar(30), + @uvalue nvarchar(255) +as + set nocount on + + if (@property is null) or (@property = '') + begin + raiserror('Must specify a property name.',-1,-1) + return (1) + end + + if (@uvalue is null) + select objectid id from dbo.dtproperties + where property=@property + + else + select objectid id from dbo.dtproperties + where property=@property and uvalue=@uvalue +; +GO + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, value, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +; +GO + +/* +** Retrieve properties by id's +** +** dt_getproperties objid, null or '' -- retrieve all properties of the object itself +** dt_getproperties objid, property -- retrieve the property specified +*/ +create procedure dbo.dt_getpropertiesbyid_u + @id int, + @property varchar(64) +as + set nocount on + + if (@property is null) or (@property = '') + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid + else + select property, version, uvalue, lvalue + from dbo.dtproperties + where @id=objectid and @property=property +; +GO + +create procedure dbo.dt_getpropertiesbyid_vcs + @id int, + @property varchar(64), + @value varchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select value + from dbo.dtproperties + where @id=objectid and @property=property + ) + +; +GO + +create procedure dbo.dt_getpropertiesbyid_vcs_u + @id int, + @property varchar(64), + @value nvarchar(255) = NULL OUT + +as + + set nocount on + + select @value = ( + select uvalue + from dbo.dtproperties + where @id=objectid and @property=property + ) + +; +GO + +create proc dbo.dt_isundersourcecontrol + @vchLoginName varchar(255) = '', + @vchPassword varchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; GO 1 => get list of objs */ + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + +declare @iReturnValue int +select @iReturnValue = 0 + +declare @iStreamObjectId int +select @iStreamObjectId = 0 + +declare @vchTempText varchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR('Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist varchar(255)) + + select @vchTempText = 'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + goto CleanUp + + +; +GO + +create proc dbo.dt_isundersourcecontrol_u + @vchLoginName nvarchar(255) = '', + @vchPassword nvarchar(255) = '', + @iWhoToo int = 0 /* 0 => Just check project; GO 1 => get list of objs */ + +as + + set nocount on + + declare @iReturn int + declare @iObjectId int + select @iObjectId = 0 + + declare @VSSGUID nvarchar(100) + select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iReturnValue int + select @iReturnValue = 0 + + declare @iStreamObjectId int + select @iStreamObjectId = 0 + + declare @vchTempText nvarchar(255) + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if (@vchProjectName IS NULL) or (@vchSourceSafeINI IS NULL) or (@vchServerName IS NULL) or (@vchDatabaseName IS NULL) + begin + RAISERROR(N'Not Under Source Control',16,-1) + return + end + + if @iWhoToo = 1 + begin + + /* Get List of Procs in the project */ + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'GetListOfObjects', + NULL, + @vchProjectName, + @vchSourceSafeINI, + @vchServerName, + @vchDatabaseName, + @vchLoginName, + @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAGetProperty @iObjectId, N'GetStreamObject', @iStreamObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + create table #ObjectList (id int identity, vchObjectlist nvarchar(255)) + + select @vchTempText = N'STUB' + while @vchTempText IS NOT NULL + begin + exec @iReturn = sp_OAMethod @iStreamObjectId, N'GetStream', @iReturnValue OUT, @vchTempText OUT + if @iReturn <> 0 GOTO E_OAError + + if (@vchTempText IS NOT NULL) insert into #ObjectList (vchObjectlist ) select @vchTempText + end + + select vchObjectlist from #ObjectList order by id + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + goto CleanUp + + +; +GO + +create procedure dbo.dt_removefromsourcecontrol + +as + + set nocount on + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + exec dbo.dt_droppropertiesbyid @iPropertyObjectId, null + + /* -1 is returned by dt_droppopertiesbyid */ + if @@error <> 0 and @@error <> -1 return 1 + + return 0 + + +; +GO + +/* +** If the property already exists, reset the value; GO otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** value -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid + @id int, + @property varchar(64), + @value varchar(255), + @lvalue image +as + set nocount on + declare @uvalue nvarchar(255) + set @uvalue = convert(nvarchar(255), @value) + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@value, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @value, @uvalue, @lvalue) + end + +; +GO + +/* +** If the property already exists, reset the value; GO otherwise add property +** id -- the id in sysobjects of the object +** property -- the name of the property +** uvalue -- the text value of the property +** lvalue -- the binary value of the property (image) +*/ +create procedure dbo.dt_setpropertybyid_u + @id int, + @property varchar(64), + @uvalue nvarchar(255), + @lvalue image +as + set nocount on + -- + -- If we are writing the name property, find the ansi equivalent. + -- If there is no lossless translation, generate an ansi name. + -- + declare @avalue varchar(255) + set @avalue = null + if (@uvalue is not null) + begin + if (convert(nvarchar(255), convert(varchar(255), @uvalue)) = @uvalue) + begin + set @avalue = convert(varchar(255), @uvalue) + end + else + begin + if 'DtgSchemaNAME' = @property + begin + exec dbo.dt_generateansiname @avalue output + end + end + end + if exists (select * from dbo.dtproperties + where objectid=@id and property=@property) + begin + -- + -- bump the version count for this row as we update it + -- + update dbo.dtproperties set value=@avalue, uvalue=@uvalue, lvalue=@lvalue, version=version+1 + where objectid=@id and property=@property + end + else + begin + -- + -- version count is auto-set to 0 on initial insert + -- + insert dbo.dtproperties (property, objectid, value, uvalue, lvalue) + values (@property, @id, @avalue, @uvalue, @lvalue) + end +; +GO + +create proc dbo.dt_validateloginparams + @vchLoginName varchar(255), + @vchPassword varchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + 'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +; +GO + +create proc dbo.dt_validateloginparams_u + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchSourceSafeINI nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 GOTO E_OAError + + exec @iReturn = sp_OAMethod @iObjectId, + N'ValidateLoginParams', + NULL, + @sSourceSafeINI = @vchSourceSafeINI, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + if @iReturn <> 0 GOTO E_OAError + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +; +GO + +create proc dbo.dt_vcsenabled + +as + +set nocount on + +declare @iObjectId int +select @iObjectId = 0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iReturn int + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + if @iReturn <> 0 raiserror('', 16, -1) /* Can't Load Helper DLLC */ + + +; +GO + +/* +** This procedure returns the version number of the stored +** procedures used by the Microsoft Visual Database Tools. +** Current version is 7.0.00. +*/ +create procedure dbo.dt_verstamp006 +as + select 7000 +; +GO + +create proc dbo.dt_whocheckedout + @chObjectType char(4), + @vchObjectName varchar(255), + @vchLoginName varchar(255), + @vchPassword varchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID varchar(100) +select @VSSGUID = 'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName varchar(255) + declare @vchSourceSafeINI varchar(255) + declare @vchServerName varchar(255) + declare @vchDatabaseName varchar(255) + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue varchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + 'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror @iObjectId, @iReturn + GOTO CleanUp + + +; +GO + +create proc dbo.dt_whocheckedout_u + @chObjectType char(4), + @vchObjectName nvarchar(255), + @vchLoginName nvarchar(255), + @vchPassword nvarchar(255) + +as + +set nocount on + +declare @iReturn int +declare @iObjectId int +select @iObjectId =0 + +declare @VSSGUID nvarchar(100) +select @VSSGUID = N'SQLVersionControl.VCS_SQL' + + declare @iPropertyObjectId int + + select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID') + + declare @vchProjectName nvarchar(255) + declare @vchSourceSafeINI nvarchar(255) + declare @vchServerName nvarchar(255) + declare @vchDatabaseName nvarchar(255) + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSProject', @vchProjectName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT + exec dbo.dt_getpropertiesbyid_vcs_u @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT + + if @chObjectType = 'PROC' + begin + exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT + + if @iReturn <> 0 GOTO E_OAError + + declare @vchReturnValue nvarchar(255) + select @vchReturnValue = '' + + exec @iReturn = sp_OAMethod @iObjectId, + N'WhoCheckedOut', + @vchReturnValue OUT, + @sProjectName = @vchProjectName, + @sSourceSafeINI = @vchSourceSafeINI, + @sObjectName = @vchObjectName, + @sServerName = @vchServerName, + @sDatabaseName = @vchDatabaseName, + @sLoginName = @vchLoginName, + @sPassword = @vchPassword + + if @iReturn <> 0 GOTO E_OAError + + select @vchReturnValue + + end + +CleanUp: + return + +E_OAError: + exec dbo.dt_displayoaerror_u @iObjectId, @iReturn + GOTO CleanUp + + +; +GO + + +CREATE PROCEDURE [dbo].[spAddCategory] + +@description varchar(100) + +AS + +INSERT + Category + ( + [description] + ) + +VALUES + ( + @description + ) +; +GO + + +CREATE PROCEDURE [dbo].[spAddJudgment] + +@Id int OUTPUT, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext + +AS + +DECLARE @Publication_datetime datetime + +IF @Is_published = 1 + SELECT @Publication_datetime = getdate() + +INSERT + Judgment + ( + Is_published, + File_no_1, + File_no_2, + Decision_datetime, + Claimants, + Respondent, + Main_subcategory_id, + Sec_subcategory_id, + Headnote_summary, + Created_datetime, + Last_updatedtime, + Publication_datetime + ) + +VALUES + ( + @Is_published, + @File_no_1, + @File_no_2, + @Decision_datetime, + @Claimants, + @Respondent, + @Main_subcategory_id, + @Sec_subcategory_id, + @Headnote_summary, + getdate(), + getdate(), + @Publication_datetime + ) + +SELECT @Id = SCOPE_IDENTITY() +; +GO + + +CREATE PROCEDURE [dbo].[spAddSubCategory] + +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +INSERT + Subcategory + ( + parent_num, + [description], + num + ) + +VALUES + ( + @parent_num, + @description, + @num + ) +; +GO + + + +CREATE PROCEDURE [dbo].[spAddUser] + +@UserID integer OUTPUT, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +INSERT + Users + + ( + Username, + [Password], + Firstname, + Lastname + ) + +VALUES + + ( + @Username, + @Password, + @Firstname, + @Lastname + ) + +SELECT @UserID = SCOPE_IDENTITY() + +; +GO + + +CREATE PROCEDURE [dbo].[spCountJudgmentsBySubCategory] + +@Id int + +AS + +SELECT + COUNT(*) as num + +FROM + Judgment + +WHERE + main_subcategory_id = @Id + + OR + + sec_subcategory_id = @Id +; +GO + + +CREATE PROCEDURE [dbo].[spCountSubCategoryByCategory] + +@CategoryId int + +AS + +SELECT + COUNT(*) + +FROM + Subcategory + +WHERE + parent_num = @CategoryId +; +GO + + +CREATE PROCEDURE [dbo].[spDeleteCategory] + +@Id int + +AS + +DELETE + Category + +WHERE + Num = @Id +; +GO + + +CREATE PROCEDURE [dbo].[spDeleteSubCategory] + +@Id int + +AS + +DELETE + SubCategory + +WHERE + [Id] = @Id +; +GO + + + +CREATE PROCEDURE [dbo].[spDeleteUser] + +@UserID int + +AS + +DECLARE +@Count int + +SELECT @Count = COUNT(*) FROM Users + +IF @Count > 1 + BEGIN + DELETE + Users + + WHERE + UserID = @UserID + END + +; +GO + + +CREATE PROCEDURE [dbo].[spGetCategoryList] + +AS + +SELECT num, [description] +FROM category +ORDER BY num +; +GO + + +CREATE PROCEDURE [dbo].[spGetDecision] + +@DecisionId int + +AS + +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +; +GO + + +CREATE PROCEDURE [dbo].[spGetDecisionPublished] + +@DecisionId int, +@Is_Published bit + +AS + +select j.*, s.[description] as subcategory, s.[id] as subcatid, c.[description] as category, c.num as catid, + s2.[description] as secsubcategory, s2.[id] as secsubcatid, c2.[description] as seccategory, c2.num as seccatid +from judgment j +inner join subcategory s on j.main_subcategory_id = s.id +inner join category c on s.parent_num = c.num +left join subcategory s2 on j.sec_subcategory_id = s2.id +left join category c2 on s2.parent_num = c2.num +where j.[id] = @DecisionId +and is_published = @Is_Published +; + GO + + +CREATE PROCEDURE [dbo].[spGetSubCategoryList] + +AS + +SELECT [id], parent_num, num, [description] +FROM subcategory +ORDER BY parent_num, num +; +GO + + +CREATE PROCEDURE [dbo].[spGetSubCategoryListByCategory] + +@CategoryId int + +AS + +SELECT [id], parent_num, s.num, s.[description], c.[description] as categoryname +FROM subcategory s +inner join category c on s.parent_num = c.num +WHERE parent_num = @CategoryId +ORDER BY s. num +; +GO + + + +CREATE PROCEDURE [dbo].[spGetUser] + +@UserID int + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +WHERE + UserID = @UserID + +; +GO + + + +CREATE PROCEDURE [dbo].[spGetUserList] + +AS + +SELECT + UserID, Username, [Password], Firstname, Lastname + +FROM + Users + +; +GO + + +CREATE PROCEDURE [dbo].[spLoginUser] + +@Username varchar(50), +@Password varchar(50) + +AS + +SELECT + * + +FROM + Users + +WHERE + Username = @Username + +AND + [Password] = @Password +; +GO + + +CREATE PROCEDURE [dbo].[spUpdateCategory] + +@id int, +@description varchar(100) + +AS + +UPDATE + Category + +SET + [description] = @description + +WHERE + num = @id +; +GO + + +CREATE PROCEDURE [dbo].[spUpdateJudgment] + +@Id int, +@Is_published bit, +@File_no_1 varchar(5), +@File_no_2 varchar(5), +@Decision_datetime datetime, +@Claimants ntext, +@Respondent ntext, +@Main_subcategory_id int, +@Sec_subcategory_id int, +@Headnote_summary ntext + +AS + +DECLARE +@Publication_datetime datetime, +@CurrentlyPublished bit + +SELECT @CurrentlyPublished = Is_Published FROM Judgment WHERE [id] = @Id + +IF (@CurrentlyPublished = 0 AND @Is_published = 1) + BEGIN + SELECT @Publication_datetime = getdate() + END + +IF @CurrentlyPublished = 1 + BEGIN + SELECT @Publication_datetime = Publication_datetime FROM Judgment WHERE [id] = @Id + END + +IF @Is_published = 0 + BEGIN + SELECT @Publication_datetime = NULL + END + +UPDATE + Judgment + +SET + Is_Published = @Is_Published, + File_no_1 = @File_no_1, + File_no_2 = @File_no_2, + Decision_datetime = @Decision_datetime, + Claimants = @Claimants, + Respondent = @Respondent, + Main_subcategory_id = @Main_subcategory_id, + Sec_subcategory_id = @Sec_subcategory_id, + Headnote_summary = @Headnote_summary, + Last_updatedtime = getdate(), + Publication_datetime = @Publication_datetime + +WHERE + [Id] = @Id +; +GO + + +CREATE PROCEDURE [dbo].[spUpdateSubCategory] + +@id int, +@parent_num tinyint, +@description varchar(100), +@num tinyint + +AS + +UPDATE + Subcategory + +SET + parent_num = @parent_num, + [description] = @description, + num = @num + +WHERE + [id] = @id +; +GO + + + +CREATE PROCEDURE [dbo].[spUpdateUser] + +@UserID int, +@Username varchar(50), +@Password varchar(50), +@Firstname varchar(50), +@Lastname varchar(50) + +AS + +UPDATE + Users + +SET + Username = @Username, + Firstname = @Firstname, + Lastname = @Lastname + +WHERE + UserID = @UserID + +-- Update the password ONLY if provided +IF @Password IS NOT NULL AND 0 < LEN(@Password) + UPDATE Users + SET [Password] = @Password + WHERE UserID = @UserID + +; +GO + diff --git a/terraform/environments/tribunals/secrets.tf b/terraform/environments/tribunals/secrets.tf index a6a94d9c098..8e6aea0fecf 100644 --- a/terraform/environments/tribunals/secrets.tf +++ b/terraform/environments/tribunals/secrets.tf @@ -1 +1,94 @@ -#### This file can be used to store secrets specific to the member account #### +# #### This file can be used to store secrets specific to the member account #### + +resource "random_password" "password" { + length = 16 + lower = true + upper = true + numeric = true + special = false +} + +resource "aws_secretsmanager_secret" "resource_rds_secret" { + name = "${local.application_data.accounts[local.environment].db_identifier}_credentials" + recovery_window_in_days = 0 +} + +resource "aws_secretsmanager_secret_version" "resource_rds_secret_current" { + secret_id = aws_secretsmanager_secret.resource_rds_secret.id + secret_string = <" +sqlcmd -S ${DB_URL} -U ${USER_NAME} -P ${PASSWORD} -Q "create database ${NEW_DB_NAME}" +sqlcmd -S ${DB_URL} -U ${USER_NAME} -P ${PASSWORD} -Q "CREATE LOGIN ${NEW_USER_NAME} WITH PASSWORD = '${NEW_PASSWORD}'" +sqlcmd -S ${DB_URL} -U ${USER_NAME} -P ${PASSWORD} -Q "CREATE USER ${NEW_USER_NAME} FOR LOGIN ${NEW_USER_NAME}" +sqlcmd -S ${DB_URL} -U ${USER_NAME} -P ${PASSWORD} -Q "USE ${NEW_DB_NAME} GO EXEC sp_addrolemember N'db_owner', N'${NEW_USER_NAME}'; GO" +sqlcmd -S ${DB_URL} -U ${USER_NAME} -P ${PASSWORD} -i "./modules/${APP_FOLDER}/sp_migration.sql" \ No newline at end of file diff --git a/terraform/environments/tribunals/setup-security-group.sh b/terraform/environments/tribunals/setup-security-group.sh new file mode 100644 index 00000000000..663290a24cb --- /dev/null +++ b/terraform/environments/tribunals/setup-security-group.sh @@ -0,0 +1,19 @@ +#! /bin/bash + +#source ./connect-to-aws.sh + +aws configure set aws_access_key_id "$DMS_SOURCE_ACCOUNT_ACCESS_KEY" --profile dts-legacy-apps-user +aws configure set aws_secret_access_key "$DMS_SOURCE_ACCOUNT_SECRET_KEY" --profile dts-legacy-apps-user +aws configure set region "$AWS_REGION" --profile dts-legacy-apps-user +aws configure set output "json" --profile dts-legacy-apps-user + +#retrieve existing security groups +aws ec2 describe-instances --region ${AWS_REGION} --instance-ids ${EC2_INSTANCE_ID} --profile dts-legacy-apps-user --query "Reservations[].Instances[].SecurityGroups[].GroupId" > security_groups + +existing_group_ids="$(sed 's/[^a-zA-Z0-9-]//g' security_groups)" +echo existing group ids : $existing_group_ids +echo DMS security group id : ${DMS_SECURITY_GROUP} +aws ec2 modify-instance-attribute --region ${AWS_REGION} --instance-id ${EC2_INSTANCE_ID} --profile dts-legacy-apps-user --groups $existing_group_ids ${DMS_SECURITY_GROUP} + +aws ec2 describe-instances --region ${AWS_REGION} --instance-ids ${EC2_INSTANCE_ID} --profile dts-legacy-apps-user --query "Reservations[].Instances[].SecurityGroups[].GroupId" > security_groups +cat security_groups \ No newline at end of file diff --git a/terraform/environments/tribunals/user_data.sh b/terraform/environments/tribunals/user_data.sh new file mode 100644 index 00000000000..996c5bddb61 --- /dev/null +++ b/terraform/environments/tribunals/user_data.sh @@ -0,0 +1,12 @@ + +Import-Module ECSTools +[Environment]::SetEnvironmentVariable("ECS_CONTAINER_START_TIMEOUT", "15m", [System.EnvironmentVariableTarget]::Machine) +[Environment]::SetEnvironmentVariable("ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE", "true", "Machine") +[Environment]::SetEnvironmentVariable("ECS_ENABLE_TASK_IAM_ROLE", "true", "Machine") + +Initialize-ECSAgent –Cluster ${cluster_name} -EnableTaskIAMRole -LoggingDrivers '["json-file","awslogs"]' + + +Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) + + \ No newline at end of file diff --git a/terraform/environments/tribunals/waf.tf b/terraform/environments/tribunals/waf.tf new file mode 100644 index 00000000000..c0998f34a83 --- /dev/null +++ b/terraform/environments/tribunals/waf.tf @@ -0,0 +1,42 @@ +resource "aws_wafv2_web_acl" "tribunals_web_acl" { + name = "tribunals-web-acl" + scope = "REGIONAL" + + default_action { + allow {} + } + + rule { + name = "common-rule-set" + priority = 1 + + override_action { + none {} + } + + statement { + managed_rule_group_statement { + name = "AWSManagedRulesCommonRuleSet" + vendor_name = "AWS" + rule_action_override { + action_to_use { + allow {} + } + name = "SizeRestrictions_BODY" + } + } + } + + visibility_config { + cloudwatch_metrics_enabled = true + metric_name = "AWSManagedRulesCommonRuleSetMetrics" + sampled_requests_enabled = true + } + } + + visibility_config { + cloudwatch_metrics_enabled = true + metric_name = "tribunals-web-acl" + sampled_requests_enabled = true + } +}