001package gudusoft.gsqlparser.nodes.flink; 002 003import gudusoft.gsqlparser.nodes.TParseTreeNodeList; 004import gudusoft.gsqlparser.nodes.TPTNodeList; 005import gudusoft.gsqlparser.nodes.TParseTreeNode; 006import gudusoft.gsqlparser.nodes.TParseTreeVisitor; 007 008/** 009 * Represents Flink's WITH clause for specifying connector properties. 010 * 011 * <p>Syntax:</p> 012 * <pre> 013 * WITH ( 014 * 'property1' = 'value1', 015 * 'property2' = 'value2', 016 * ... 017 * ) 018 * </pre> 019 * 020 * <p>Example:</p> 021 * <pre> 022 * WITH ( 023 * 'connector' = 'kafka', 024 * 'topic' = 'my-topic', 025 * 'properties.bootstrap.servers' = 'localhost:9092', 026 * 'format' = 'json' 027 * ) 028 * </pre> 029 */ 030public class TFlinkWithClause extends TParseTreeNode { 031 private TPTNodeList<TFlinkTableProperty> properties; 032 033 /** 034 * Initialize with properties list. 035 * @param arg1 the list of properties (TParseTreeNodeList from grammar) 036 */ 037 public void init(Object arg1) { 038 if (arg1 instanceof TParseTreeNodeList) { 039 TParseTreeNodeList nodeList = (TParseTreeNodeList) arg1; 040 properties = new TPTNodeList<TFlinkTableProperty>(); 041 for (int i = 0; i < nodeList.size(); i++) { 042 TParseTreeNode node = nodeList.getElement(i); 043 if (node instanceof TFlinkTableProperty) { 044 properties.addElement((TFlinkTableProperty) node); 045 } 046 } 047 } else if (arg1 instanceof TPTNodeList) { 048 @SuppressWarnings("unchecked") 049 TPTNodeList<TFlinkTableProperty> list = (TPTNodeList<TFlinkTableProperty>) arg1; 050 properties = list; 051 } 052 } 053 054 /** 055 * Get the list of properties defined in the WITH clause. 056 * @return the list of properties 057 */ 058 public TPTNodeList<TFlinkTableProperty> getProperties() { 059 return properties; 060 } 061 062 /** 063 * Set the properties list. 064 * @param properties the list of properties 065 */ 066 public void setProperties(TPTNodeList<TFlinkTableProperty> properties) { 067 this.properties = properties; 068 } 069 070 /** 071 * Get the number of properties in the WITH clause. 072 * @return the number of properties 073 */ 074 public int getPropertyCount() { 075 return properties != null ? properties.size() : 0; 076 } 077 078 /** 079 * Get a property by index. 080 * @param index the index of the property 081 * @return the property at the given index 082 */ 083 public TFlinkTableProperty getProperty(int index) { 084 if (properties != null && index >= 0 && index < properties.size()) { 085 return properties.getElement(index); 086 } 087 return null; 088 } 089 090 /** 091 * Find a property by key name. 092 * @param keyName the key name to search for (without quotes) 093 * @return the property with the given key, or null if not found 094 */ 095 public TFlinkTableProperty findProperty(String keyName) { 096 if (properties == null || keyName == null) return null; 097 for (int i = 0; i < properties.size(); i++) { 098 TFlinkTableProperty prop = properties.getElement(i); 099 if (keyName.equals(prop.getKeyString())) { 100 return prop; 101 } 102 } 103 return null; 104 } 105 106 /** 107 * Get a property value by key name. 108 * @param keyName the key name to search for (without quotes) 109 * @return the property value (without quotes), or null if not found 110 */ 111 public String getPropertyValue(String keyName) { 112 TFlinkTableProperty prop = findProperty(keyName); 113 return prop != null ? prop.getValueString() : null; 114 } 115 116 @Override 117 public void accept(TParseTreeVisitor v) { 118 v.preVisit(this); 119 v.postVisit(this); 120 } 121 122 @Override 123 public void acceptChildren(TParseTreeVisitor v) { 124 v.preVisit(this); 125 if (properties != null) { 126 properties.acceptChildren(v); 127 } 128 v.postVisit(this); 129 } 130}