class_docstr: This handler handles connection and execution of the Xata statements.
class_docstr_tok: ["This","handler","handles","connection","and","execution","of","the","Xata","statements","."]
docstr: Insert data into the Xata database. 
docstr_tok: ["Insert","data","into","the","Xata","database","."]
code: def insert(
      self,
      table_name: str,
      data: pd.DataFrame,
      columns: List[str] = None,
  ) -> HandlerResponse:
      """Insert data into the Xata database."""
      if columns:
          data = data[columns]
      # Convert to records
      data = data.to_dict("records")
      # Convert metadata to json
      for row in data:
          if "metadata" in row:
              row["metadata"] = json.dumps(row["metadata"])
      if len(data) > 1:
          # Bulk processing
          try:
              bp = BulkProcessor(self._client, throw_exception=True)
              bp.put_records(table_name, data)
              bp.flush_queue()
          except Exception as e:
              return Response(
                  resp_type=RESPONSE_TYPE.ERROR,
                  error_message=f"Error inserting data into '{table_name}': {e}",
              )
      elif len(data) == 0:
          # Skip
          return Response(resp_type=RESPONSE_TYPE.OK)
      elif "id" in data[0] and TableField.ID.value in columns:
          # If id present
          id = data[0]["id"]
          rest_of_data = data[0].copy()
          del rest_of_data["id"]
          try:
              resp = self._client.records().insert_with_id(
                  table_name=table_name,
                  record_id=id,
                  payload=rest_of_data,
                  create_only=True,
                  columns=columns,
              )
              if not resp.is_success():
                  raise Exception(resp["message"])
          except Exception as e:
              return Response(
                  resp_type=RESPONSE_TYPE.ERROR,
                  error_message=f"Error inserting data into '{table_name}': {e}",
              )
      else:
          # If id not present
          try:
              resp = self._client.records().insert(
                  table_name=table_name,
                  payload=data[0],
                  columns=columns,
              )
              if not resp.is_success():
                  raise Exception(resp["message"])
          except Exception as e:
              return Response(
                  resp_type=RESPONSE_TYPE.ERROR,
                  error_message=f"Error inserting data into '{table_name}': {e}",
              )
      return Response(resp_type=RESPONSE_TYPE.OK)
  
imports: from collections import OrderedDict
  from typing import List, Optional
  import pandas
  import json
  import xata
  from xata.helpers import BulkProcessor
  from mindsdb.integrations.libs.const import HANDLER_CONNECTION_ARG_TYPE
  from mindsdb.integrations.libs.response import RESPONSE_TYPE
  from mindsdb.integrations.libs.response import HandlerResponse
  from mindsdb.integrations.libs.response import HandlerResponse
  from mindsdb.integrations.libs.response import HandlerStatusResponse
  from mindsdb.integrations.libs.vectordatabase_handler import FilterCondition, FilterOperator, TableField, VectorStoreHandler
  from mindsdb.utilities import log
label_desc: Use image node_id 10 for calling the XataHandler obj's underlying member method code with example usage: obj.insert(table_name, data, columns) and returns: Response, Response, Response, Response, Response