Skip to content

Commit a51d715

Browse files
Add files via upload
1 parent 5e03c87 commit a51d715

10 files changed

+1045
-0
lines changed

Add SQL Commands to Files.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""
2+
SQL Code Block Prepend Tool
3+
4+
Description:
5+
This script automates the process of prepending a specific SQL code block to all .sql files within
6+
specified directories. It supports multiple predefined code block options, allowing the user to
7+
select which block to prepend based on their requirements. The script also handles file encoding
8+
intelligently, ensuring that the prepend action respects the original file's encoding. This feature
9+
is particularly useful for preparing SQL files with necessary configurations or statements before
10+
execution in different environments.
11+
12+
Usage:
13+
- Define the SQL code block options within the 'options' dictionary. Each option should be a complete,
14+
ready-to-prepend SQL statement or set of statements.
15+
- Set the 'option_to_use' variable to the key of the desired code block from the 'options' dictionary.
16+
- Specify the directories to be processed in the 'directories' list. All .sql files within these directories
17+
(and their subdirectories) will be processed.
18+
- Run the script. It will prepend the chosen SQL code block to each .sql file, handling various encodings
19+
and ensuring that the file's original encoding is preserved.
20+
21+
Features:
22+
- Allows specification of multiple SQL code block options for different use cases.
23+
- Prepends chosen SQL code block to the beginning of each .sql file in the specified directories.
24+
- Handles file encodings ('utf-8-sig', 'utf-8', 'latin-1') to accommodate files from different sources.
25+
- Checks if a file already starts with one of the options and, if so, replaces it with the chosen option to avoid duplication.
26+
- Provides feedback on the processing of each file, including the file path and encoding used.
27+
28+
Note:
29+
Before running this script, ensure that you have backups of your .sql files, as this process modifies the files in place.
30+
Modify the 'options' dictionary and the 'directories' list according to your specific
31+
32+
import os
33+
import re
34+
"""
35+
36+
# Define your options here
37+
options = {
38+
'option1': """
39+
USE bsav2_MyTest_DB; --Babelfish addition: Setting this for testing.
40+
GO
41+
SET NOCOUNT ON;
42+
GO
43+
""",
44+
'option2': """
45+
USE bsav2_MyTest_DB; --Babelfish addition: Setting this for testing.
46+
GO
47+
EXECUTE sp_babelfish_configure '%', 'ignore', 'server'--Babelfish addition: Setting this for testing.
48+
GO
49+
SET NOCOUNT ON;
50+
GO
51+
""",
52+
'option3': """
53+
EXECUTE sp_babelfish_configure '%', 'ignore', 'server'--Babelfish addition: Setting this for testing.
54+
GO
55+
"""
56+
}
57+
import os
58+
import re
59+
60+
def prepend_code_block_with_encoding_handling(directories, chosen_option, encodings, options):
61+
"""
62+
Prepends a given code block to all .sql files in the specified directories,
63+
taking into account the file's encoding to handle UTF properly.
64+
"""
65+
combined_options_pattern = re.compile('|'.join(re.escape(opt) for opt in options.values()), re.DOTALL)
66+
67+
for directory in directories:
68+
for root, _, files in os.walk(directory):
69+
for file in files:
70+
if file.endswith(".sql"):
71+
file_path = os.path.join(root, file)
72+
file_content, encoding_used = read_file_with_encoding_detection(file_path, encodings)
73+
74+
# Check if the file content starts with any of the options
75+
if combined_options_pattern.match(file_content):
76+
new_content = combined_options_pattern.sub('', file_content).lstrip()
77+
new_content = chosen_option.rstrip() + "\n\n" + new_content
78+
else:
79+
new_content = chosen_option.rstrip() + "\n\n" + file_content
80+
81+
with open(file_path, 'w', encoding=encoding_used) as f:
82+
f.write(new_content)
83+
print(f"Processed {file_path} with encoding {encoding_used}")
84+
85+
def read_file_with_encoding_detection(file_path, encodings):
86+
"""
87+
Attempts to read a file with multiple encodings until one succeeds.
88+
Returns the file content and the encoding used.
89+
"""
90+
for encoding in encodings:
91+
try:
92+
with open(file_path, 'r', encoding=encoding) as file:
93+
return file.read(), encoding
94+
except UnicodeDecodeError:
95+
continue
96+
raise ValueError(f"Failed to decode {file_path} with given encodings.")
97+
98+
if __name__ == "__main__":
99+
encodings = ['utf-8-sig', 'utf-8', 'latin-1']
100+
101+
#Set you option!!!
102+
#Set you option!!!
103+
#Set you option!!!
104+
option_to_use = 'option1' # for example
105+
code_block = options[option_to_use]
106+
107+
directories = [
108+
r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\new_install_before\\'
109+
#,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\bsav2_upgrade\\'
110+
#,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\before_every_upgrade\\'
111+
#,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\after_every_upgrade\\'
112+
]
113+
114+
prepend_code_block_with_encoding_handling(directories, code_block, encodings, options)
115+
print("Done processing .sql files.")

Create sqlcmd Script.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
SQLCMD Script Generator
3+
4+
Description:
5+
This script generates an SQLCMD script to execute .sql files found within specified directories.
6+
It automatically traverses through each directory, compiles commands to execute found .sql files,
7+
and copies the final SQLCMD script to the clipboard.
8+
9+
Usage:
10+
Modify the 'directories' list in the '__main__' section to include the paths to the directories
11+
containing your .sql files.
12+
13+
Features:
14+
- Traverses specified directories for .sql files.
15+
- Generates SQLCMD script commands for executing the found .sql files.
16+
- Supports outputting the script to the clipboard.
17+
18+
Note:
19+
This script requires the 'pyperclip' module for clipboard functionality. Ensure it is installed
20+
via 'pip install pyperclip' before running the script.
21+
"""
22+
23+
import os
24+
import pyperclip
25+
26+
def generate_sqlcmd_script(directories):
27+
"""
28+
Generates an SQLCMD script to execute .sql files in specified directories and
29+
copies the script to the clipboard.
30+
31+
Parameters:
32+
directories (list): A list of directory paths containing .sql files to execute.
33+
"""
34+
sqlcmd_script_content = "SET NOCOUNT ON;\nGO\nPRINT @@SERVERNAME;\nGO\n\n"
35+
36+
for directory in directories:
37+
path_var = f":setvar Path \"{directory}\"\n\n"
38+
sqlcmd_script_content += path_var
39+
40+
for root, _, files in os.walk(directory):
41+
for filename in files:
42+
if filename.endswith('.sql'):
43+
# Extract the last folder name from the directory path
44+
last_folder_name = os.path.basename(os.path.normpath(directory))
45+
sqlcmd_script_content += f"PRINT('Executing {last_folder_name}/{filename}')\n"
46+
sqlcmd_script_content += f":setvar SQLFile \"{filename}\"\n"
47+
sqlcmd_script_content += ":r $(Path)$(SQLFile)\nGO\n\n"
48+
49+
# Copy the generated SQLCMD script to the clipboard
50+
pyperclip.copy(sqlcmd_script_content)
51+
print("SQLCMD script has been copied to the clipboard.")
52+
53+
if __name__ == "__main__":
54+
# List of directories containing .sql files
55+
directories = [
56+
r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\new_install_before\\'
57+
#r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\before_every_upgrade\\'
58+
#r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\bsav2_upgrade\\'
59+
#r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\after_every_upgrade\\'
60+
]
61+
generate_sqlcmd_script(directories)

Delete n Lines From Scripts.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
3+
def confirm_action(num_lines, directories):
4+
"""
5+
Prompts the user for confirmation to proceed with the action.
6+
"""
7+
print(f"You are about to delete the first {num_lines} lines from all .sql files in the following directories:")
8+
for directory in directories:
9+
print(directory)
10+
return input("Do you want to proceed? (yes/no): ").strip().lower() == "yes"
11+
12+
"""
13+
SQL File Line Deletion Tool
14+
15+
Description:
16+
This script is designed to remove the first N lines from all `.sql` files within specified directories.
17+
It aims to automate the preparation of SQL files by deleting unnecessary header information or comments
18+
that may be present at the beginning of these files. The script supports multiple file encodings, attempting
19+
to read and write files with different encoding standards to ensure compatibility across various file formats.
20+
21+
Usage:
22+
- Update the 'directories' list in the '__main__' section with the paths to the directories containing your .sql files.
23+
- Set the 'num_lines_to_delete' variable to specify the number of lines you wish to remove from the beginning of each file.
24+
- Run the script. You will be prompted to confirm the deletion action before it proceeds.
25+
26+
Features:
27+
- Iterates over specified directories to find .sql files.
28+
- Prompts user for confirmation before performing deletions.
29+
- Attempts to handle files with multiple encodings, specifically 'utf-8' and 'latin-1', to accommodate various file origins.
30+
- Provides feedback on processing status and encoding issues encountered.
31+
32+
Note:
33+
It is recommended to backup your .sql files before running this script to prevent accidental data loss.
34+
Ensure you have write permissions for the directories and files you intend to process.
35+
"""
36+
37+
def delete_first_n_lines(directories, num_lines, encodings=('utf-8', 'latin-1')):
38+
"""
39+
Deletes the first n lines from all .sql files in the specified directories,
40+
trying multiple encodings in case of UnicodeDecodeErrors.
41+
"""
42+
if not confirm_action(num_lines, directories):
43+
print("Operation canceled.")
44+
return
45+
46+
for directory in directories:
47+
for root, _, files in os.walk(directory):
48+
for file in files:
49+
if file.endswith(".sql"):
50+
file_path = os.path.join(root, file)
51+
for encoding in encodings:
52+
try:
53+
with open(file_path, 'r', encoding=encoding) as f:
54+
lines = f.readlines()
55+
# Skip the first n lines
56+
new_content = lines[num_lines:]
57+
# Write the remaining content back to the file
58+
with open(file_path, 'w', encoding=encoding) as f:
59+
f.writelines(new_content)
60+
print(f"Processed {file_path} with encoding {encoding}")
61+
break # Stop trying encodings if successful
62+
except UnicodeDecodeError as e:
63+
print(f"Error decoding {file_path} with {encoding}: {e}")
64+
if encoding == encodings[-1]: # Last encoding option
65+
print(f"Failed to process {file_path} due to encoding issues.")
66+
67+
if __name__ == "__main__":
68+
# Directories to process
69+
directories = [
70+
#r'C:\a\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\new_install_before\\'
71+
#,r'C:\a\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\before_every_upgrade\\'
72+
#,r'C:\a\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\bsav2_upgrade\\'
73+
#, r'C:\a\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\after_every_upgrade\' ]
74+
]
75+
num_lines_to_delete = 6 # Adjust the number of lines you want to delete
76+
77+
delete_first_n_lines(directories, num_lines_to_delete)
78+
print("Done processing .sql files.")

Find and Replace.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
SQL File Pattern Replacement Tool
3+
4+
Description:
5+
This script facilitates the batch updating of SQL files across multiple directories,
6+
replacing specified search patterns with a new string. It's particularly useful for
7+
renaming database identifiers, schema names, or updating SQL commands en masse within
8+
a large set of SQL scripts. The script iterates over each file in the specified directories,
9+
searches for the given pattern using regular expressions, and replaces all occurrences
10+
with the provided replacement string.
11+
12+
Usage:
13+
- Define the 'search_pattern' with a regular expression that matches the text you wish to replace.
14+
- Set the 'replacement_string' to the text that will replace each occurrence of the search pattern.
15+
- List all target directories in the 'directories' list where the SQL files are located.
16+
- Specify the 'encodings' list with the encodings to try when opening and saving files, accommodating
17+
different file encoding standards for compatibility.
18+
- Run the script. It will recursively search through each directory, replacing the pattern in all files found.
19+
20+
Features:
21+
- Supports multiple encodings for reading and writing files, ensuring wide compatibility with different file formats.
22+
- Uses regular expressions for pattern matching, providing flexibility in defining the search pattern.
23+
- Performs in-place file updates, directly modifying the original files with the new content.
24+
- Prints a message for each file that is updated, providing a clear log of changes made.
25+
26+
Note:
27+
It is highly recommended to backup your files before running this script, as it modifies the files in place.
28+
Ensure that the search pattern correctly matches only the intended occurrences to prevent unintended replacements.
29+
The script does not modify filenames or directory names, only the contents of the files.
30+
31+
"""
32+
import os
33+
import re
34+
35+
def replace_in_file(file_path, search_pattern, replacement, encodings):
36+
for encoding in encodings:
37+
try:
38+
with open(file_path, 'r', encoding=encoding) as f:
39+
file_contents = f.read()
40+
41+
# Replace occurrences
42+
new_contents = re.sub(search_pattern, replacement, file_contents)
43+
44+
# If changes were made, write the file
45+
if new_contents != file_contents:
46+
with open(file_path, 'w', encoding=encoding) as f:
47+
f.write(new_contents)
48+
print(f"Updated {file_path}")
49+
# Stop after successfully reading and writing the file
50+
break
51+
except UnicodeDecodeError:
52+
continue
53+
except Exception as e:
54+
print(f"Error processing {file_path}: {e}")
55+
break
56+
57+
def replace_in_files(directories, search_pattern, replacement, encodings):
58+
for directory in directories:
59+
for root, dirs, files in os.walk(directory):
60+
for file in files:
61+
file_path = os.path.join(root, file)
62+
replace_in_file(file_path, search_pattern, replacement, encodings)
63+
64+
if __name__ == "__main__":
65+
66+
# Find this string
67+
search_pattern = r'bsav2_{BankId}'
68+
69+
# and replace it with this string
70+
replacement_string = 'bsav2_mytest_db'
71+
72+
encodings = ['utf-8-sig', 'utf-8', 'latin-1']
73+
74+
directories = [
75+
r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\new_install_before\\'
76+
,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\bsav2_upgrade\\'
77+
,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\before_every_upgrade\\'
78+
,r'C:\b\bamplus-postgres-research-main\bamplus-postgres-research-main\Bam+ Installer\module-packages\BAMPlus.BAM.Bridge\database\after_every_upgrade\\'
79+
]
80+
81+
replace_in_files(directories, search_pattern, replacement_string, encodings)
82+
print("Done processing all directories.")

0 commit comments

Comments
 (0)