Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1753726

Browse files
thinkasanyyanglbme
andauthored
style: standardize the case format of sql function names (doocs#1814)
--------- Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 725ab64 commit 1753726

File tree

566 files changed

+1704
-1494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

566 files changed

+1704
-1494
lines changed

‎run_format.py‎

Lines changed: 248 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,246 @@
44
import re
55
import black
66

7-
suffixes = ['md', 'py', 'java', 'c', 'cpp', 'go', 'php', 'cs', 'rs', 'js', 'ts']
7+
suffixes = ["md", "py", "java", "c", "cpp", "go", "php", "cs", "rs", "js", "ts", "sql"]
88

9-
code_blocks = ['python', 'java', 'cpp', 'c', 'go', 'ts', 'js', 'php', 'cs', 'rs']
9+
code_blocks = ["python", "java", "cpp", "c", "go", "ts", "js", "php", "cs", "rs", "sql"]
10+
11+
functions_to_replace = [
12+
"ABS",
13+
"ACOS",
14+
"ADDDATE",
15+
"ADDTIME",
16+
"AES_DECRYPT",
17+
"AES_ENCRYPT",
18+
"ASCII",
19+
"ASIN",
20+
"ATAN",
21+
"AVG",
22+
"BIN",
23+
"BIT_COUNT",
24+
"CEIL",
25+
"CHAR",
26+
"CHAR_LENGTH",
27+
"CHARACTER_LENGTH",
28+
"CONCAT",
29+
"CONCAT_WS",
30+
"CONNECTION_ID",
31+
"CONV",
32+
"CONVERT",
33+
"COS",
34+
"COT",
35+
"COUNT",
36+
"CRC32",
37+
"CURDATE",
38+
"CURRENT_DATE",
39+
"CURRENT_TIME",
40+
"CURRENT_TIMESTAMP",
41+
"CURTIME",
42+
"DATABASE",
43+
"DATE",
44+
"DATEDIFF",
45+
"DATE_ADD",
46+
"DATE_FORMAT",
47+
"DATE_SUB",
48+
"DAY",
49+
"DAYNAME",
50+
"DAYOFMONTH",
51+
"DAYOFWEEK",
52+
"DAYOFYEAR",
53+
"DECODE",
54+
"DEFAULT",
55+
"DEGREES",
56+
"DES_DECRYPT",
57+
"DES_ENCRYPT",
58+
"ELT",
59+
"ENCODE",
60+
"ENCRYPT",
61+
"EXP",
62+
"EXPORT_SET",
63+
"EXTRACT",
64+
"FIELD",
65+
"FIND_IN_SET",
66+
"FLOOR",
67+
"FORMAT",
68+
"FOUND_ROWS",
69+
"FROM_DAYS",
70+
"FROM_UNIXTIME",
71+
"GET_FORMAT",
72+
"GET_LOCK",
73+
"GREATEST",
74+
"GROUP_CONCAT",
75+
"HEX",
76+
"HOUR",
77+
"IF",
78+
"IFNULL",
79+
"IN",
80+
"INET_ATON",
81+
"INET_NTOA",
82+
"INSERT",
83+
"INSTR",
84+
"INTERVAL",
85+
"ISNULL",
86+
"LAST_INSERT_ID",
87+
"LCASE",
88+
"LEAST",
89+
"LEFT",
90+
"LENGTH",
91+
"LN",
92+
"LOAD_FILE",
93+
"LOCALTIME",
94+
"LOCALTIMESTAMP",
95+
"LOCATE",
96+
"LOG",
97+
"LOG10",
98+
"LOG2",
99+
"LOWER",
100+
"LPAD",
101+
"LTRIM",
102+
"MAKE_SET",
103+
"MAKEDATE",
104+
"MAKETIME",
105+
"MATCH",
106+
"MAX",
107+
"MD5",
108+
"MICROSECOND",
109+
"MID",
110+
"MIN",
111+
"MINUTE",
112+
"MOD",
113+
"MONTH",
114+
"MONTHNAME",
115+
"NAME_CONST",
116+
"NOW",
117+
"NULLIF",
118+
"OCT",
119+
"OCTET_LENGTH",
120+
"ORD",
121+
"PASSWORD",
122+
"PERIOD_ADD",
123+
"PERIOD_DIFF",
124+
"PI",
125+
"POSITION",
126+
"POW",
127+
"POWER",
128+
"PROCEDURE ANALYSE",
129+
"QUARTER",
130+
"QUOTE",
131+
"RADIANS",
132+
"RAND",
133+
"RELEASE_LOCK",
134+
"REPEAT",
135+
"REPLACE",
136+
"REVERSE",
137+
"RIGHT",
138+
"ROUND",
139+
"ROW_COUNT",
140+
"RPAD",
141+
"RTRIM",
142+
"SCHEMA",
143+
"SEC_TO_TIME",
144+
"SECOND",
145+
"SESSION_USER",
146+
"SHA1",
147+
"SHA",
148+
"SIGN",
149+
"SIN",
150+
"SLEEP",
151+
"SOUNDEX",
152+
"SPACE",
153+
"SQRT",
154+
"STR_TO_DATE",
155+
"STRCMP",
156+
"SUBDATE",
157+
"SUBSTR",
158+
"SUBSTRING",
159+
"SUBSTRING_INDEX",
160+
"SUBTIME",
161+
"SUM",
162+
"SYSDATE",
163+
"SYSTEM_USER",
164+
"TAN",
165+
"TIME",
166+
"TIMEDIFF",
167+
"TIMESTAMP",
168+
"TIMESTAMPADD",
169+
"TIMESTAMPDIFF",
170+
"TIME_FORMAT",
171+
"TIME_TO_SEC",
172+
"TO_DAYS",
173+
"TRIM",
174+
"TRUNCATE",
175+
"UCASE",
176+
"UNCOMPRESS",
177+
"UNCOMPRESSED_LENGTH",
178+
"UNHEX",
179+
"UNIX_TIMESTAMP",
180+
"UPPER",
181+
"USER",
182+
"UTC_DATE",
183+
"UTC_TIME",
184+
"UTC_TIMESTAMP",
185+
"UUID",
186+
"VAR_POP",
187+
"VAR_SAMP",
188+
"VARIANCE",
189+
"VERSION",
190+
"WEEK",
191+
"WEEKDAY",
192+
"WEEKOFYEAR",
193+
"XOR",
194+
"YEAR",
195+
"YEARWEEK",
196+
"ROW_NUMBER",
197+
"RANK",
198+
"DENSE_RANK",
199+
"NTILE",
200+
"LAG",
201+
"LEAD",
202+
"FIRST_VALUE",
203+
"LAST_VALUE",
204+
"CUME_DIST",
205+
"PERCENT_RANK",
206+
"PERCENTILE_CONT",
207+
"PERCENTILE_DISC",
208+
]
10209

11210

12211
def add_header(path: str):
13212
"""Add header to php and go files"""
14-
print(f'[add header] path: {path}')
15-
with open(path, 'r', encoding='utf-8') as f:
213+
print(f"[add header] path: {path}")
214+
with open(path, "r", encoding="utf-8") as f:
16215
content = f.read()
17-
if path.endswith('.php'):
18-
content = '<?php\n' + content
19-
elif path.endswith('.go') and 'sorting' not in path:
20-
content = 'package main\n' + content
216+
if path.endswith(".php"):
217+
content = "<?php\n" + content
218+
elif path.endswith(".go") and "sorting" not in path:
219+
content = "package main\n" + content
220+
elif path.endswith(".sql"):
221+
for func in functions_to_replace:
222+
pattern = r"\b{}\s*\(".format(func)
223+
content = re.sub(pattern, f"{func.upper()}(", content, flags=re.IGNORECASE)
21224
else:
22225
return
23-
with open(path, 'w', encoding='utf-8') as f:
226+
with open(path, "w", encoding="utf-8") as f:
24227
f.write(content)
25228

26229

27230
def remove_header(path: str):
28231
"""Remove header from php and go files"""
29-
print(f'[remove header] path: {path}')
30-
with open(path, 'r', encoding='utf-8') as f:
232+
print(f"[remove header] path: {path}")
233+
with open(path, "r", encoding="utf-8") as f:
31234
content = f.read()
32-
if path.endswith('.php'):
235+
if path.endswith(".php"):
33236
content = content.rstrip()
34-
content = content.replace('<?php\n', '')
35-
elif path.endswith('.go'):
237+
content = content.replace("<?php\n", "")
238+
elif path.endswith(".go"):
36239
content = content.rstrip()
37-
if 'sorting' not in path:
38-
content = content.replace('package main\n\n', '').replace(
39-
'package main\n', ''
240+
if "sorting" not in path:
241+
content = content.replace("package main\n\n", "").replace(
242+
"package main\n", ""
40243
)
41244
else:
42245
return
43-
with open(path, 'w', encoding='utf-8') as f:
246+
with open(path, "w", encoding="utf-8") as f:
44247
f.write(content)
45248

46249

@@ -49,51 +252,58 @@ def find_all_paths() -> List[str]:
49252
paths = []
50253
for root, _, files in os.walk(os.getcwd()):
51254
for file in files:
52-
path = root + '/' + file
53-
if 'node_modules' in path or '__pycache__' in path or '.git' in path:
255+
path = root + "/" + file
256+
if "node_modules" in path or "__pycache__" in path or ".git" in path:
54257
continue
55-
if any(path.endswith(f'.{suf}') for suf in suffixes):
258+
if any(path.endswith(f".{suf}") for suf in suffixes):
56259
paths.append(path)
57260
return paths
58261

59262

60263
def format_inline_code(path: str):
61264
"""Format inline code in .md file"""
62-
if not path.endswith('.md'):
265+
if not path.endswith(".md"):
63266
return
64-
with open(path, 'r', encoding='utf-8') as f:
267+
with open(path, "r", encoding="utf-8") as f:
65268
content = f.read()
66-
root = path[: path.rfind('/')]
67-
print(f'[format inline code] path: {path}')
269+
root = path[: path.rfind("/")]
68270
for suf in code_blocks:
69-
res = re.findall(f'```{suf}\n(.*?)```', content, re.S)
271+
res = re.findall(f"```{suf}\n(.*?)```", content, re.S)
70272
for block in res or []:
71273
# skip empty code block
72274
if not block or not block.strip():
73275
continue
74-
if suf in ['c', 'cpp', 'java', 'go']:
75-
file = f'{root}/Solution2.{suf}'
76-
with open(file, 'w', encoding='utf-8') as f:
276+
if suf in ["c", "cpp", "java", "go"]:
277+
file = f"{root}/Solution2.{suf}"
278+
with open(file, "w", encoding="utf-8") as f:
77279
f.write(block)
78-
if suf == 'go':
280+
if suf == "go":
79281
add_header(file)
80282
os.system(f'gofmt -w "{file}"')
81283
remove_header(file)
82284
else:
83285
os.system(f'npx clang-format -i --style=file "{file}"')
84-
with open(file, 'r', encoding='utf-8') as f:
286+
with open(file, "r", encoding="utf-8") as f:
85287
new_block = f.read()
86-
if not new_block.endswith('\n'):
87-
new_block += '\n'
288+
if not new_block.endswith("\n"):
289+
new_block += "\n"
88290
content = content.replace(block, new_block)
89291
os.remove(file)
90-
elif suf == 'python':
292+
elif suf == "python":
91293
new_block = black.format_str(
92294
block, mode=black.FileMode(string_normalization=False)
93295
)
94296
content = content.replace(block, new_block)
297+
elif suf == "sql":
298+
for func in functions_to_replace:
299+
pattern = r"\b{}\s*\(".format(func)
300+
new_block = re.sub(
301+
pattern, f"{func.upper()}(", block, flags=re.IGNORECASE
302+
)
303+
content = content.replace(block, new_block)
304+
block = new_block
95305

96-
with open(path, 'w', encoding='utf-8') as f:
306+
with open(path, "w", encoding="utf-8") as f:
97307
f.write(content)
98308

99309

@@ -103,21 +313,21 @@ def run():
103313

104314
for path in paths:
105315
add_header(path)
106-
if any(path.endswith(suf) for suf in ['c', 'cpp', 'java']):
316+
if any(path.endswith(suf) for suf in ["c", "cpp", "java"]):
107317
# format with clang-format
108318
os.system(f'npx clang-format -i --style=file "{path}"')
109319

110320
# format with prettier
111321
os.system('npx prettier --write "**/*.{md,js,ts,php,sql}"')
112322

113323
# format with gofmt
114-
os.system('gofmt -w .')
324+
os.system("gofmt -w .")
115325

116326
for path in paths:
117327
remove_header(path)
118328
for path in paths:
119329
format_inline_code(path)
120330

121331

122-
if __name__ == '__main__':
332+
if __name__ == "__main__":
123333
run()

‎solution/0100-0199/0180.Consecutive Numbers/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ WITH
6868
SELECT
6969
*,
7070
CASE
71-
WHEN (lag(num) OVER (ORDER BY id)) = num THEN 0
71+
WHEN (LAG(num) OVER (ORDER BY id)) = num THEN 0
7272
ELSE 1
7373
END AS mark
7474
FROM Logs
7575
),
76-
p AS (SELECT num, sum(mark) OVER (ORDER BY id) AS gid FROM t)
76+
p AS (SELECT num, SUM(mark) OVER (ORDER BY id) AS gid FROM t)
7777
SELECT DISTINCT num AS ConsecutiveNums
7878
FROM p
7979
GROUP BY gid
80-
HAVING count(1) >= 3;
80+
HAVING COUNT(1) >= 3;
8181
```
8282

8383
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /