11import { useFormFieldContext } from "../hooks/form-field-context" ;
2+ import { useFormContext } from "../hooks/form-context" ;
23import { useCustomize } from "../hooks/customization-context" ;
34import type { CSSProperties } from "react" ;
45
56export function ControlSql ( ) {
67 const formFieldContext = useFormFieldContext ( ) ;
8+ const formContext = useFormContext ( ) ;
79 const {
810 id, onChange, prop, value,
911 } = formFieldContext ;
1012 const {
1113 getProps, theme,
1214 } = useCustomize ( ) ;
1315
16+ // Find the first app prop to determine which database app to use
17+ const appProp = formContext . configurableProps . find ( ( p : any ) => p . type === "app" ) ;
18+ const appName = appProp ?. app || "postgresql" ; // Default to postgresql
19+
20+ // Extract the query string from the structured value or use empty string
21+ const queryValue = typeof value === "object" && value && "query" in value
22+ ? ( value as any ) . query
23+ : typeof value === "string"
24+ ? value
25+ : "" ;
26+
27+ const handleChange = ( queryText : string ) => {
28+ // Transform the simple query string into the structured SQL object
29+ const sqlObject = {
30+ app : appName ,
31+ query : queryText ,
32+ params : [ ] , // For now, always empty array
33+ } ;
34+ onChange ( sqlObject ) ;
35+ } ;
36+
1437 const baseStyles : CSSProperties = {
1538 color : theme . colors . neutral60 ,
1639 display : "block" ,
@@ -31,8 +54,8 @@ export function ControlSql() {
3154 < textarea
3255 id = { id }
3356 name = { prop . name }
34- value = { value ?? "" }
35- onChange = { ( e ) => onChange ( e . target . value ) }
57+ value = { queryValue }
58+ onChange = { ( e ) => handleChange ( e . target . value ) }
3659 placeholder = "SELECT * FROM table_name"
3760 required = { ! prop . optional }
3861 { ...getProps ( "controlSql" , baseStyles , formFieldContext ) }
0 commit comments