99package schemacrawler .tools .ai .tools ;
1010
1111import static java .util .Objects .requireNonNull ;
12+ import static schemacrawler .tools .ai .utility .JsonUtility .mapper ;
1213import static schemacrawler .tools .ai .utility .JsonUtility .wrapException ;
1314import static us .fatehi .utility .Utility .isBlank ;
1415
1516import com .fasterxml .jackson .databind .JsonNode ;
16- import com .fasterxml .jackson .databind .ObjectMapper ;
1717import com .fasterxml .jackson .databind .node .ObjectNode ;
1818import java .sql .Connection ;
1919import java .util .Optional ;
@@ -32,8 +32,6 @@ public final class FunctionCallback {
3232
3333 private static final Logger LOGGER = Logger .getLogger (FunctionCallback .class .getCanonicalName ());
3434
35- private static ObjectMapper objectMapper = new ObjectMapper ();
36-
3735 private FunctionDefinition <FunctionParameters > functionDefinition ;
3836 private final Catalog catalog ;
3937
@@ -71,9 +69,9 @@ public String execute(final String argumentsString, final Connection connection)
7169
7270 requireNonNull (connection , "No database connection provided" );
7371
74- final PropertyName functionName = getFunctionName ();
7572 LOGGER .log (
76- Level .INFO , new StringFormat ("Executing%n%s" , toObject (argumentsString ).toPrettyString ()));
73+ Level .FINER ,
74+ new StringFormat ("Executing%n%s" , toCallObject (argumentsString ).toPrettyString ()));
7775
7876 if (functionDefinition == null ) {
7977 return "" ;
@@ -89,8 +87,7 @@ public String execute(final String argumentsString, final Connection connection)
8987 Level .INFO ,
9088 e ,
9189 new StringFormat (
92- "Could not call function with arguments: %s(%s)%n%s" ,
93- functionName , argumentsString , e .getMessage ()));
90+ "Exception executing: %s%n%s" , toCallObject (argumentsString ), e .getMessage ()));
9491 return wrapException (e );
9592 }
9693 }
@@ -110,9 +107,31 @@ public PropertyName getFunctionName() {
110107 return functionName ;
111108 }
112109
110+ public JsonNode toCallObject (final String argumentsString ) {
111+ final ObjectNode objectNode = mapper .createObjectNode ();
112+
113+ final PropertyName functionName = getFunctionName ();
114+ objectNode .put ("name" , functionName .getName ());
115+
116+ try {
117+ final String functionArguments ;
118+ if (isBlank (argumentsString )) {
119+ functionArguments = "{}" ;
120+ } else {
121+ functionArguments = argumentsString ;
122+ }
123+ final JsonNode arguments = mapper .readTree (functionArguments );
124+ objectNode .set ("arguments" , arguments );
125+ } catch (final Exception e ) {
126+ objectNode .set ("arguments" , mapper .createObjectNode ());
127+ }
128+
129+ return objectNode ;
130+ }
131+
113132 @ Override
114133 public String toString () {
115- return toObject (null ).toPrettyString ();
134+ return toCallObject (null ).toPrettyString ();
116135 }
117136
118137 private String executeFunction (final FunctionParameters arguments , final Connection connection )
@@ -141,9 +160,8 @@ private <P extends FunctionParameters> P instantiateArguments(final String argum
141160 } else {
142161 functionArguments = argumentsString ;
143162 }
144- final ObjectMapper objectMapper = new ObjectMapper ();
145163 try {
146- final P parameters = objectMapper .readValue (functionArguments , parametersClass );
164+ final P parameters = mapper .readValue (functionArguments , parametersClass );
147165 LOGGER .log (Level .FINE , String .valueOf (parameters ));
148166 return parameters ;
149167 } catch (final Exception e ) {
@@ -156,26 +174,4 @@ private <P extends FunctionParameters> P instantiateArguments(final String argum
156174 return parametersClass .getDeclaredConstructor ().newInstance ();
157175 }
158176 }
159-
160- private ObjectNode toObject (final String argumentsString ) {
161- final ObjectNode objectNode = objectMapper .createObjectNode ();
162-
163- final PropertyName functionName = getFunctionName ();
164- objectNode .put ("name" , functionName .getName ());
165-
166- try {
167- final String functionArguments ;
168- if (isBlank (argumentsString )) {
169- functionArguments = "{}" ;
170- } else {
171- functionArguments = argumentsString ;
172- }
173- final JsonNode arguments = objectMapper .readTree (functionArguments );
174- objectNode .set ("arguments" , arguments );
175- } catch (final Exception e ) {
176- objectNode .set ("arguments" , objectMapper .createObjectNode ());
177- }
178-
179- return objectNode ;
180- }
181177}
0 commit comments