Skip to content

Instantly share code, notes, and snippets.

@chenrui333
Created August 29, 2014 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenrui333/c2af3ded980b71078a14 to your computer and use it in GitHub Desktop.
Save chenrui333/c2af3ded980b71078a14 to your computer and use it in GitHub Desktop.
Generate Pojo with SQL server auto-gen table creation SQL code
import re
import fileinput
def camelCase(st):
"""
filter nonAlpha and nonDigit char, and camelCase the output string
input:
USER_DEF1
output:
userDef1
"""
output = ''.join(x for x in st.title() if x.isalpha() or x.isdigit())
return output[0].lower() + output[1:]
def generateLine(inputLine):
"""
String input like:
" [INTERFACE_RECORD_ID] [nvarchar](25) NOT NULL,"
output like:
"private String interfaceRecordId;"
"""
words = inputLine.split()
# dict for search types
types = {'nvarchar': 'String', 'datetime': 'Date', 'numeric': 'BigDecimal', 'nchar': 'String' }
words[0] = words[0].replace("[", "")
words[0] = words[0].replace("]", "")
for key, value in types.iteritems():
if key in words[1]:
print "%s %s %s%s" % ("private", value, camelCase(words[0]), ";")
# POJO generation process, 2nd version
def generatePojo(file):
"""
1. use auto-gen SQL server table creation SQL like (NOTE, not full file, only part of the file, looks like the testString above)
[INTERFACE_RECORD_ID] [nvarchar](25) NOT NULL,
[STATUS_FLOW_NAME] [nvarchar](25) NULL,
[warehouse] [nvarchar](25) NULL,
2. put into the xx.txt, specify the directory input, and then run for result
NOTE, types are limited for four types known for the current usage, check with MSDN for more data types mapping
"""
for line in fileinput.input(file, inplace = True):
# Known error when there is line space, cause "IndexError: string index out of range" error
generateLine(line)
# set the file
test_file = "./testfile.txt"
generatePojo(test_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment