99import com .clickhouse .client .api .internal .ValidationUtils ;
1010import com .clickhouse .data .ClickHouseColumn ;
1111
12+ import java .io .IOException ;
1213import java .io .InputStream ;
14+ import java .lang .reflect .InvocationTargetException ;
1315import java .lang .reflect .Method ;
1416import java .math .BigDecimal ;
1517import java .math .BigInteger ;
@@ -41,7 +43,8 @@ public class Client {
4143 private Set <String > endpoints ;
4244 private Map <String , String > configuration ;
4345 private List <ClickHouseNode > serverNodes = new ArrayList <>();
44- private Map <Class <?>, List <POJOSerializer >> serializers = new HashMap <>();
46+ private Map <Class <?>, Map <ClickHouseColumn , POJOSerializer >> serializers ;
47+ private Map <Class <?>, Map <String , Method >> getters ;
4548 private static final Logger LOG = LoggerFactory .getLogger (Client .class );
4649
4750 private Client (Set <String > endpoints , Map <String ,String > configuration ) {
@@ -50,6 +53,8 @@ private Client(Set<String> endpoints, Map<String,String> configuration) {
5053 this .endpoints .forEach (endpoint -> {
5154 this .serverNodes .add (ClickHouseNode .of (endpoint , this .configuration ));
5255 });
56+ this .serializers = new HashMap <>();
57+ this .getters = new HashMap <>();
5358 }
5459
5560 public static class Builder {
@@ -173,7 +178,7 @@ public boolean ping(int timeout) {
173178 public void register (Class <?> clazz , TableSchema schema ) {
174179 //Create a new POJOSerializer with static .serialize(object, columns) methods
175180 Map <String , Method > getters = new HashMap <>();
176- List < POJOSerializer > serializers = new ArrayList <>();
181+ Map < ClickHouseColumn , POJOSerializer > serializers = new HashMap <>();
177182
178183 //Retrieve all methods
179184 for (Method method : clazz .getMethods ()) {
@@ -185,42 +190,50 @@ public void register(Class<?> clazz, TableSchema schema) {
185190 ClickHouseColumn column = schema .getColumnByName (fieldName );
186191 if (column != null ) {//Check if the field is in the schema
187192 getters .put (fieldName , method );
188- serializers .add ((obj , stream , columns ) -> {//Create the field serializer
189- if (columns == null || columns .contains (fieldName )) {
190- Method getter = getters .get (fieldName );
191- Object value = getter .invoke (obj );
192-
193- //Handle null values
194- if (value == null ) {
195- BinaryStreamUtils .writeNull (stream );
196- return ;
197- } else {//If nullable, we have to specify that the value is not null
198- if (column .isNullable ()) {
199- BinaryStreamUtils .writeNonNull (stream );
200- }
193+ serializers .put (column , (obj , stream ) -> {//Create the field serializer
194+ Method getter = this .getters .get (clazz ).get (fieldName );
195+ Object value = getter .invoke (obj );
196+
197+ //Handle null values
198+ if (value == null ) {
199+ BinaryStreamUtils .writeNull (stream );
200+ return ;
201+ } else {//If nullable, we have to specify that the value is not null
202+ if (column .isNullable ()) {
203+ BinaryStreamUtils .writeNonNull (stream );
201204 }
202-
203- //Handle the different types
204- SerializerUtils .serializeData (stream , value , column );
205205 }
206+
207+ //Handle the different types
208+ SerializerUtils .serializeData (stream , value , column );
206209 });
207210 }
208211 }
209212 }
210213
211214 this .serializers .put (clazz , serializers );
215+ this .getters .put (clazz , getters );
212216 }
213217
214218 /**
215219 * Insert data into ClickHouse using a POJO
216220 */
217221 public Future <InsertResponse > insert (String tableName ,
218222 List <Object > data ,
219- InsertSettings settings ,
220- List <ClickHouseColumn > columns ) throws ClickHouseException , SocketException {
223+ InsertSettings settings ) throws ClickHouseException , IOException , InvocationTargetException , IllegalAccessException {
221224 //Lookup the Serializer for the POJO
222225 //Call the static .serialize method on the POJOSerializer for each object in the list
223- return null ;//This is just a placeholder
226+ List <POJOSerializer > serializers = this .serializers .get (data .get (0 ).getClass ());
227+ if (serializers == null || serializers .isEmpty ()) {
228+ throw new IllegalArgumentException ("No serializer found for the given class. Please register() before calling this method." );
229+ }
230+
231+ for (Object obj : data ) {
232+ for (POJOSerializer serializer : serializers ) {
233+ serializer .serialize (data , null );
234+ }
235+ }
236+
224237 }
225238
226239 /**
0 commit comments