Created
May 29, 2018 13:28
-
-
Save fairjm/8ed8ef8896af70906f7d332f0c5eb421 to your computer and use it in GitHub Desktop.
create sql to java fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
code = """ | |
`name` varchar(10) DEFAULT NULL', | |
`age` int NOT NULL DEFAULT '' | |
""" | |
def parse_field_name(s): | |
return re.sub(r"_(.)", lambda x: x.group(1).upper(), s.replace("`","")) | |
def parse_field_type(t): | |
tl = t.lower() | |
if t.startswith("int"): | |
return "Long" | |
elif t.startswith("varchar"): | |
return "String" | |
elif t.startswith("timestamp"): | |
return "Timestamp" | |
return t | |
lines = [(parse_field_name(a.split()[0]), parse_field_type(a.split()[1])) for a in code.splitlines() if len(a.strip()) > 0] | |
for name,type in lines: | |
print("private {0} {1};".format(type, name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment