-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_generator.py
242 lines (164 loc) · 7.06 KB
/
string_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import itertools
import oracledb
import time
from Full_Traversal_Cursed import regex_s,like_s,like_s2,i1,i2,s,flag_end,flag_start
# Replace these with your actual values
user = "SYS" # Your Oracle username
password = "mypassword1" # Your Oracle password
dsn = "localhost:1521/ORCLCDB" # DSN (e.g., "localhost:1521/orclpdb1")
# Establish the connection
connection = oracledb.connect(user=user, password=password, dsn=dsn, mode=oracledb.SYSDBA)
# Function to generate all possible combinations from a regex pattern with character classes
def generate_combinations_from_regex(regex):
# Strip the starting (^) and ending ($) anchors
regex = regex.strip('^').strip('$')
# Step 1: Find all the character classes in the regex
char_classes = []
index = 0
while index < len(regex):
if regex[index] == '[':
# Find the end of the character class
end_index = regex.find(']', index)
if end_index != -1:
char_classes.append(regex[index + 1:end_index]) # Capture the characters inside []
index = end_index
index += 1
# Step 2: Generate all combinations of characters inside each class
combinations = []
for char_class in char_classes:
combinations.append(list(char_class)) # Each class is just a list of possible chars
# Step 3: Use itertools.product to generate all possible combinations from the lists
all_combinations = list(itertools.product(*combinations))
# Step 4: Rebuild the strings by replacing the character classes with each combination
generated_strings = []
for combo in all_combinations:
result_string = regex
for i, char_class in enumerate(char_classes):
# Replace each character class with the combination value
result_string = result_string.replace(f"[{char_class}]", combo[i], 1)
generated_strings.append(result_string)
return generated_strings
# Test with the regex '^ab[de]e[fk]$'
# regex = '^ab[de]*e[fk]*$' # Matches strings like abdef, abeef, abdek, abeek
start_time = time.time()
query = "SELECT count(*) FROM Q1 WHERE "
# Generate combinations and print the result
if len(like_s)>0:
generated_strings = generate_combinations_from_regex(like_s)
print("Generated strings:")
for string in generated_strings:
print(string)
if i1!=-1:
if flag_start == True:
like_conditions = [f"NAME LIKE '{string}%'" for string in generated_strings]
else:
like_conditions = [f"NAME LIKE '%{string}%'" for string in generated_strings]
else:
if flag_start == True:
if flag_end == True:
like_conditions = [f"NAME LIKE '{string}'" for string in generated_strings]
else:
like_conditions = [f"NAME LIKE '{string}%'" for string in generated_strings]
else:
if flag_end == True:
like_conditions = [f"NAME LIKE '%{string}'" for string in generated_strings]
else:
like_conditions = [f"NAME LIKE '%{string}%'" for string in generated_strings]
# query = "SELECT * FROM SAMPLE_TABLE WHERE (" + " OR ".join(like_conditions)
query = query + '( '
query2 = query+ " OR ".join(like_conditions)
query2=query2 + ')'
query = query2
if len(like_s2)>0:
generated_strings2= generate_combinations_from_regex(like_s2)
print("Generated strings2 :")
for string in generated_strings2:
print(string)
# if i1!=-1:
# if flag_end == True:
# like2_conditions = [f"NAME LIKE '{string}'" for string in generated_strings2]
# else:
# like2_conditions = [f"NAME LIKE '%{string}%'" for string in generated_strings2]
# else:
if flag_end == True:
like2_conditions = [f"NAME LIKE '%{string}'" for string in generated_strings2]
else:
like2_conditions = [f"NAME LIKE '%{string}%'" for string in generated_strings2]
if(len(like_s)>0):
query = query + ' AND ('
else:
query = query + ' ('
query2= query + " OR ".join(like2_conditions)
query2 = query2 + ')'
query = query2
if len(regex_s)>0:
if len(like_s)!=0 or len(like_s2)!=0 :
query=query + " AND REGEXP_LIKE(NAME,'" + regex_s + "')"
else :
query = query + " REGEXP_LIKE(NAME,'" + regex_s + "') "
# Print the query to check if it's correct
print("Generated Query:")
print(query)
print('\n')
# def star_checking(regex):
# # Find the index of the first occurrence of '*'
# first_star_index = regex.find('*')
# # Find the index of the last occurrence of '*'
# last_star_index = regex.rfind('*')
# # Print the results
# print(f"First occurrence of '*' is at index: {first_star_index}")
# print(f"Last occurrence of '*' is at index: {last_star_index}")
# # Check if '*' is found
# if first_star_index != -1:
# # Check if the character before '*' is ']'
# if first_star_index > 0 and regex[first_star_index - 1] == ']':
# # Find the nearest '[' before the '*' (search backward)
# open_bracket_index = regex.rfind('[', 0, first_star_index)
# # If '[' is found before the '*', return its index
# if open_bracket_index != -1:
# print(f"Nearest '[' before '*' is at index: {open_bracket_index}")
# else:
# print("No '[' found before '*'")
# else:
# # If '*' is not preceded by ']', return index - 1
# print(f"Character before '*' is not ']', returning index-1: {first_star_index - 1}")
# else:
# print("The character '*' is not present in the string.")
# Execute the query
cursor = connection.cursor()
cursor.execute(query)
result_like = cursor.fetchall()
end_time = time.time()
Time_Like = end_time - start_time
print("Query for Like :" + query)
print("Time taken by Like : " + str(Time_Like))
print('\n')
# print(result_like)
#Time for regex direct Regex
start_time = time.time()
regex_query = f"select count(*) from Q1 where REGEXP_LIKE(NAME, '{s}')"
cursor.execute(regex_query)
result_regex=cursor.fetchall()
end_time = time.time()
Time_Regex = end_time - start_time
print("Query for Regex : " + str(regex_query))
print("Time taken by regex: " + str(Time_Regex))
print('\n')
print("No. of rows in Result : "+str(result_regex[0][0]))
print('\n')
Total_rows= f"select count(*) from Q1"
cursor.execute(Total_rows)
result_total=cursor.fetchall()
print("No. of rows in Table : "+str(result_total[0][0]))
print('\n')
print(flag_start)
print(flag_end)
# Q1 - SCHEMA(NAME-varchar(10000)) Selectivity- 0.25
# s = "a[bc]de(ab)*fgr*hq"
# Follows- abdeababababababababababababababababababababababfgrrrhq
# Not follows- abdeababababababababababababababababababababababfgrrrhp
# Not follows- abdeabababababababababababababababababababababafgrrrhp
# Not follows- abdababababababababababababababababababababababfgrrrhp
# Q2 - SCHEMA(NAME,varchar(10000)) Selectivity- 1.0
# s = "a[bc]de(ab)*fgr*hq"
# Follows- abdeababababababababababababababababababababababfgrrrhq