<commit_before>package io.ddf.jdbc.etl

import java.io.StringReader
import java.util.Collections

import io.ddf.content.{Schema, SqlResult, SqlTypedResult}
import io.ddf.datasource.{DataFormat, DataSourceDescriptor, JDBCDataSourceDescriptor, SQLDataSourceDescriptor}
import io.ddf.exception.DDFException
import io.ddf.jdbc.JdbcDDFManager
import io.ddf.jdbc.content._
import io.ddf.{DDF, TableNameReplacer}
import net.sf.jsqlparser.JSQLParserException
import net.sf.jsqlparser.parser.CCJSqlParserManager
import net.sf.jsqlparser.statement.Statement
import net.sf.jsqlparser.statement.select.Select
import org.apache.commons.lang.StringUtils


class SqlHandler(ddf: DDF) extends io.ddf.etl.ASqlHandler(ddf) {

  val ddfManager: JdbcDDFManager = ddf.getManager.asInstanceOf[JdbcDDFManager]
  val baseSchema = ddfManager.baseSchema

  implicit val catalog = ddfManager.catalog
  val connection = ddfManager.connection

  override def sql2ddf(command: String, schema: Schema, dataSource: DataSourceDescriptor, dataFormat: DataFormat): DDF = {
    ddfManager.checkSinkAllowed()
    if (StringUtils.startsWithIgnoreCase(command.trim, "LOAD")) {
      load(command)
    } else if (StringUtils.startsWithIgnoreCase(command.trim, "CREATE")) {
      create2ddf(command, schema)
    } else {
      if (this.ddfManager.getCanCreateView()) {
        val viewName = genTableName(8)
        //View will allow select commands
        DdlCommand(connection, baseSchema, "CREATE VIEW " + viewName + " AS (" + command + ")")
        val viewSchema = if (schema == null) catalog.getViewSchema(connection, baseSchema, viewName) else schema
        val viewRep = TableNameRepresentation(viewName, viewSchema)
        // TODO(TJ): This function implementation is wrong.
        ddf.getManager.newDDF(this.getManager, viewRep, Array(Representations.VIEW), this.getManager.getEngineName, ddf.getNamespace, viewName, viewSchema)
      } else {
        val schema = new Schema(command,
          null.asInstanceOf[java.util.List[Schema.Column]])
        val newDDF = ddf.getManager.newDDF(this.getManager, // the ddfmanager
                                          "this is a view", // the content
                                          // content class
                                          Array(classOf[java.lang.String]),
                                          this.getManager.getEngineName,
                                          ddf.getNamespace,
                                          null,
                                          schema)
        // Indicate that this ddf is a view, this information will be handled
        // in TableNameReplacer<commit_msg>fix compile<commit_after>package io.ddf.jdbc.etl

import java.io.StringReader
import java.util.Collections

import io.ddf.content.{Schema, SqlResult, SqlTypedResult}
import io.ddf.datasource.{DataFormat, DataSourceDescriptor, JDBCDataSourceDescriptor, SQLDataSourceDescriptor}
import io.ddf.exception.DDFException
import io.ddf.jdbc.JdbcDDFManager
import io.ddf.jdbc.content._
import io.ddf.{DDF, TableNameReplacer}
import net.sf.jsqlparser.JSQLParserException
import net.sf.jsqlparser.parser.CCJSqlParserManager
import net.sf.jsqlparser.statement.Statement
import net.sf.jsqlparser.statement.select.Select
import org.apache.commons.lang.StringUtils


class SqlHandler(ddf: DDF) extends io.ddf.etl.ASqlHandler(ddf) {

  val ddfManager: JdbcDDFManager = ddf.getManager.asInstanceOf[JdbcDDFManager]
  val baseSchema = ddfManager.baseSchema

  implicit val catalog = ddfManager.catalog
  val connection = ddfManager.connection

  override def sql2ddf(command: String): DDF = {
    this.sql2ddf(command, null, null, null)
  }

  override def sql2ddf(command: String, schema: Schema): DDF = {
    this.sql2ddf(command, schema, null, null)
  }

  override def sql2ddf(command: String, dataFormat: DataFormat): DDF = {
    this.sql2ddf(command, null, null, null)
  }

  override def sql2ddf(command: String, schema: Schema, dataSource: DataSourceDescriptor): DDF = {
    this.sql2ddf(command, schema, dataSource, null)
  }

  override def sql2ddf(command: String, schema: Schema, dataFormat: DataFormat): DDF = {
    this.sql2ddf(command, schema, null, null)
  }

  override def sql2ddf(command: String, schema: Schema, dataSource: DataSourceDescriptor, dataFormat: DataFormat): DDF = {
    ddfManager.checkSinkAllowed()
    if (StringUtils.startsWithIgnoreCase(command.trim, "LOAD")) {
      load(command)
    } else if (StringUtils.startsWithIgnoreCase(command.trim, "CREATE")) {
      create2ddf(command, schema)
    } else {
      if (this.ddfManager.getCanCreateView()) {
        val viewName = genTableName(8)
        //View will allow select commands
        DdlCommand(connection, baseSchema, "CREATE VIEW " + viewName + " AS (" + command + ")")
        val viewSchema = if (schema == null) catalog.getViewSchema(connection, baseSchema, viewName) else schema
        val viewRep = TableNameRepresentation(viewName, viewSchema)
        // TODO(TJ): This function implementation is wrong.
        ddf.getManager.newDDF(this.getManager, viewRep, Array(Representations.VIEW), this.getManager.getEngineName, ddf.getNamespace, viewName, viewSchema)
      } else {
        val schema = new Schema(command,
          null.asInstanceOf[java.util.List[Schema.Column]])
        val newDDF = ddf.getManager.newDDF(this.getManager, // the ddfmanager
                                          "this is a view", // the content
                                          // content class
                                          Array(classOf[java.lang.String]),
                                          this.getManager.getEngineName,
                                          ddf.getNamespace,
                                          null,
                                          schema)
        // Indicate that this ddf is a view, this information will be handled
        // in TableNameReplacer
{'fraction_non_alphanumeric': 0.09266733938782375, 'fraction_numerical': 0.0026908846283215607, 'mean_word_length': 2.7261904761904763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '821706', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1656, 'n_words': 373}