forked from lega911/sqlmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (48 loc) · 1.38 KB
/
utils.py
File metadata and controls
67 lines (48 loc) · 1.38 KB
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
import sys
import re
PY3 = sys.version_info.major == 3
NoValue = object()
if PY3:
def is_int(value):
return isinstance(value, int)
def is_str(value):
return isinstance(value, str)
def is_bytes(value):
return isinstance(value, bytes)
else:
def is_int(value):
return isinstance(value, (int, long))
def is_str(value):
return isinstance(value, unicode)
def is_bytes(value):
return isinstance(value, str)
def validate_name(name):
assert name
assert is_str(name) or is_bytes(name), 'Wrong type'
assert re.match(r'^[\w\d_]+$', name), 'Wrong name value: `{}`'.format(name)
def quote_key(name, q='`'):
if '.' in name:
name = name.split('.')
else:
name = [name]
result = []
for n in name:
validate_name(n)
result.append(q + n + q)
return '.'.join(result)
def format_func(name, q='`'):
if '(' not in name:
return quote_key(name, q)
r = re.match(r'^(\w+)\(([^\)]+)\)\s+as\s+(\w+)$', name)
if not r:
r = re.match(r'^(\w+)\(([^\)]+)\)$', name)
if not r:
raise ValueError('Error column name: "%"' % name)
rx = r.groups(0)
func = rx[0]
name = rx[1]
if len(rx) == 3:
key = rx[2]
else:
key = func + '_' + name
return '{}({}) as {}'.format(func, quote_key(name, q), quote_key(key, q).lower())