/*********************************************************
* CREATE statements *
*********************************************************/
public void createMethod(MethodFactoryInput method, Connection connection) throws SQLException {
CodeStyleCaseSettings styleCaseSettings = PSQLCodeStyleSettings.getInstance(method.getSchema().getProject()).getCaseSettings();
CodeStyleCaseOption keywordCaseOption = styleCaseSettings.getKeywordCaseOption();
CodeStyleCaseOption objectCaseOption = styleCaseSettings.getObjectCaseOption();
CodeStyleCaseOption dataTypeCaseOption = styleCaseSettings.getDatatypeCaseOption();
StringBuilder buffer = new StringBuilder();
String methodType = method.isFunction() ? "function " : "procedure ";
buffer.append(keywordCaseOption.changeCase(methodType));
buffer.append(objectCaseOption.changeCase(method.getObjectName()));
buffer.append("(");
int maxArgNameLength = 0;
int maxArgDirectionLength = 0;
for (ArgumentFactoryInput argument : method.getArguments()) {
maxArgNameLength = Math.max(maxArgNameLength, argument.getObjectName().length());
maxArgDirectionLength = Math.max(maxArgDirectionLength,
argument.isInput() && argument.isOutput() ? 5 :
argument.isInput() ? 2 :
argument.isOutput() ? 3 : 0);
}
for (ArgumentFactoryInput argument : method.getArguments()) {
buffer.append("\n ");
if (!method.isFunction()) {
String direction =
argument.isInput() && argument.isOutput() ? keywordCaseOption.changeCase("inout") :
argument.isInput() ? keywordCaseOption.changeCase("in") :
argument.isOutput() ? keywordCaseOption.changeCase("out") : "";
buffer.append(direction);
buffer.append(StringUtil.repeatSymbol(' ', maxArgDirectionLength - direction.length() + 1));
}
buffer.append(objectCaseOption.changeCase(argument.getObjectName()));
buffer.append(StringUtil.repeatSymbol(' ', maxArgNameLength - argument.getObjectName().length() + 1));
buffer.append(dataTypeCaseOption.changeCase(argument.getDataType()));
if (argument != method.getArguments().get(method.getArguments().size() -1)) {
buffer.append(",");
}
}
buffer.append(")\n");
if (method.isFunction()) {
buffer.append(keywordCaseOption.changeCase("returns "));
buffer.append(dataTypeCaseOption.changeCase(method.getReturnArgument().getDataType()));
buffer.append("\n");
}
buffer.append(keywordCaseOption.changeCase("begin\n\n"));
if (method.isFunction()) buffer.append(keywordCaseOption.changeCase(" return null;\n\n"));
buffer.append("end");