The SQL Server version I am using is SQL Server 2008.
I've got an SQL query/script I've written which is to help simplify some things while our API is being rebuilt.
Essentially, this script allows the user to provide a short list of images (of varying dimensions), and a ProjectID
to which these images will be associated with.
The images need to be put into a table called ProjectPhotos
, which has headers of:
ProjectID
, ProjectFloorPlanID
, PhotoSizeID
, and PhotoURL
.
Rather than manually creating a query each time, and specifying the PhotoSizeID
for each URL individually, I thought I could semi-automate this by collecting a list of URLs from the user, and checking each URL for the resolution, and assigning the PhotoSizeID
to each automatically. (This is a task we'd need to preform internally on a daily basis...)
The script I have, accepts the URLs in a new "temporarily" created table called "TempPhotoURLs". This is in lieu of an Array, which to my knowledge isn't supported in SQL.
The table is then sorted and processed by reading the top of the table, where it checks the URL for a matching string (the resolution in the URL
) and if a certain resolution is found, the PhotoSizeID
is set, and the image is then appended to the ProjectPhotos
table. The row is then deleted from the table, the row counter is deducted by 1, and the process repeats with the next "top" item in the temporary table.
This is my "script/query" below:
/*
The following script will attempt to add images to the ProjectsPhotos database.
Set the list of URLs in the @TempPhotoURLs table, which is created below.
* Reminder !!!
└ Separate each line item with a comma (,)
└ The final line must end with a semi-colon (;)
PhotoSizeID's for refrence.
1 = 130 x 98
2 = 500 x 375
3 = 1024 x 768
*/
DECLARE @ProjectID INT SET @ProjectID =
7190 --◄◄◄ Specify the Project ID for which you're adding images.
DECLARE @TempPhotoURLs TABLE (url varchar(MAX)) INSERT INTO @TempPhotoURLs VALUES
-- ▼▼▼ Specify the list of images you're adding, below.
('https://cdn.domain.com/backend/projects/7190/130x98/image.jpg'),
('https://cdn.domain.com/backend/projects/7190/500x375/image.jpg'),
('https://cdn.domain.com/backend/projects/7190/1024x768/image.jpg');
DECLARE @RowNumber INT = (SELECT COUNT(*) FROM @TempPhotoURLs);
PRINT '[DEBUG] - Found ' + CAST(@RowNumber AS varchar(50)) + ' rows...'
PRINT '[DEBUG] - Processing...'
WHILE @RowNumber > 0 BEGIN;
declare @CurrentURL varchar(max) = (SELECT TOP (1) url FROM @TempPhotoURLs ORDER BY url asc);
PRINT '[DEBUG] - Current URL: ' + @CurrentURL;
IF @CurrentURL LIKE '%130x98%'
BEGIN
PRINT '[DEBUG] - Image found that is 130x98px!';
PRINT '[DEBUG] - Inserting image into the ProjectPhotos database as a Thumbnail (1)';
/*INSERT INTO ProjectPhotos (ProjectID, ProjectFloorPlanID, PhotoSizeID, PhotoURLs)VALUES
(@ProjectID, NULL, 1, @CurrentURL);
*/
END
ELSE IF @CurrentURL LIKE '%500x375%'
BEGIN
PRINT '[DEBUG] - Image found that is 500x375px!';
PRINT '[DEBUG] - Inserting image into the ProjectPhotos database as a Medium (2)';
/*
INSERT INTO ProjectPhotos (ProjectID, ProjectFloorPlanID, PhotoSizeID, PhotoURLs)VALUES
(@ProjectID, NULL, 2, @CurrentURL);
*/
END
ELSE IF @CurrentURL LIKE '%1024x768%'
BEGIN
PRINT '[DEBUG] - Image found that is 1024x768!';
PRINT '[DEBUG] - Inserting image into the ProjectPhotos database as a Large (3)';
/*INSERT INTO ProjectPhotos (ProjectID, ProjectFloorPlanID, PhotoSizeID, PhotoURLs)VALUES
(@ProjectID, NULL, 3, @CurrentURL);
*/
END
ELSE
PRINT '[DEBUG] - ERROR!';
PRINT '[DEBUG] - The image did not match any patters! Please check the inputs in @PhotoURLs';
WITH q AS (SELECT TOP (1) url FROM @TempPhotoURLs ORDER BY url asc) DELETE FROM q;
SET @RowNumber -= 1
IF @RowNumber = 0
PRINT '[DEBUG] - COMEPLETE!'
ELSE
PRINT '[DEBUG] - Rows remaining ' + CAST(@RowNumber AS varchar(50)) + '...'
END
When this script is executed, I get the following back in the console:
(3 row(s) affected)
[DEBUG] - Found 3 rows...
[DEBUG] - Processing...
[DEBUG] - Current URL: https://cdn.domain.com/backend/projects/7190/1024x768/image.jpg
[DEBUG] - Image found that is 1024x768!
[DEBUG] - Inserting image into the ProjectPhotos database as a Large (3)
[DEBUG] - The image did not match any patters! Please check the inputs in @TempPhotoURLs
(1 row(s) affected)
[DEBUG] - Rows remaining 2...
[DEBUG] - Current URL: https://cdn.domain.com/backend/projects/7190/130x98/image.jpg
[DEBUG] - Image found that is 130x98px!
[DEBUG] - Inserting image into the ProjectPhotos database as a Thumbnail (1)
[DEBUG] - The image did not match any patters! Please check the inputs in @TempPhotoURLs
(1 row(s) affected)
[DEBUG] - Rows remaining 1...
[DEBUG] - Current URL: https://cdn.domain.com/backend/projects/7190/500x375/image.jpg
[DEBUG] - Image found that is 500x375px!
[DEBUG] - Inserting image into the ProjectPhotos database as a Medium (2)
[DEBUG] - The image did not match any patters! Please check the inputs in @TempPhotoURLs
(1 row(s) affected)
[DEBUG] - COMEPLETE!
Currently, this does seem to work (although the actual inject into the ProjectPhotos
section, is temporarily commented out, while I make sure this script is not going to blow things up.
Essentially, I'm hoping for some advice here as to see if there is a better, more efficient way to handle the array of URLs entered at the beginning.
As well, the part I've written which parses the @TempPhotoURLs
table into individual URLs to be processes, was stitched together from code snippets and examples I've found online. I'm not entirely sure that this is the most "appropriate" way to create an array of items, and parse through them one by one. Hoping someone can ensue some confidence in my code here, or provide a more appropriate solution here.
At this point, I'm not 100% sure why it's firing the [DEBUG] - The image did not match any patters! Please check the inputs in @TempPhotoURLs
after "successfully" injecting the image into the ProjectPhotos
table, but I'm not here to debug this. Suggestions are welcome here, but not necessary.
--- [Additional details]
The 7190 refers to the ID of the project we're associate these images to. This is not a primary key value, but just a unique ID for the project so we can differentiate them internally. For some more context, this database ties into a front end website, for New Home Construction, and a "Project" in this sense is a New Development.
Per the resolutions, the images are fixed to these sizes specifically, and uploaded by us internally to our S3 storage box. There wouldn't be a case where they would have different/varying resolutions.
Essentially, our "backend" is not working, where we'd normally upload images to these projects. The API has degraded, and is in the works of being completely rewritten to restore this functionality. In the interim, I'm just trying to setup a simple short script to manually inject these images, and associate them to the correct project while we await our new API (which is likely a couple of months away still.)
Simply put, I need this script to accept 2 variables.
- A project ID to associate the images too.
- A list of image URLs to associate to the project.
The script should be able to check what the resolution is of the image, per the resolution being present in the URL. Then, determine whether PhotoSizeID
is 1, 2, or 3, based on these fixed resolutions.
After determining this, the photo is then inserted into the ProjectPhotos
table.