Skip to content

Commit

Permalink
Clean up duplicate code in Table/FunctionIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Or committed Mar 16, 2016
1 parent 90ccdbb commit caa4013
Showing 1 changed file with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,48 @@

package org.apache.spark.sql.catalyst


/**
* Identifies a table in a database.
* If `database` is not defined, the current database is used.
* An identifier that optionally specifies a database.
*
* Format (unquoted): "name" or "db.name"
* Format (quoted): "`name`" or "`db`.`name`"
*/
private[sql] case class TableIdentifier(table: String, database: Option[String]) {
def this(table: String) = this(table, None)

private[sql] abstract class IdentifierWithDatabase(name: String) {
def database: Option[String]
def quotedString: String = database.map(db => s"`$db`.`$name`").getOrElse(s"`$name`")
def unquotedString: String = database.map(db => s"$db.$name").getOrElse(name)
override def toString: String = quotedString
}


def quotedString: String = database.map(db => s"`$db`.`$table`").getOrElse(s"`$table`")
/**
* Identifies a table in a database.
* If `database` is not defined, the current database is used.
*/
private[sql] case class TableIdentifier(
table: String,
database: Option[String])
extends IdentifierWithDatabase(table) {

def unquotedString: String = database.map(db => s"$db.$table").getOrElse(table)
def this(name: String) = this(name, None)
}

private[sql] object TableIdentifier {
def apply(tableName: String): TableIdentifier = new TableIdentifier(tableName)
def apply(tableName: String): TableIdentifier = TableIdentifier(tableName)
}


/**
* Identifies a function in a database.
* If `database` is not defined, the current database is used.
*/
// TODO: reuse some code with TableIdentifier.
private[sql] case class FunctionIdentifier(funcName: String, database: Option[String]) {
def this(name: String) = this(name, None)
private[sql] case class FunctionIdentifier(
funcName: String,
database: Option[String])
extends IdentifierWithDatabase(funcName) {

override def toString: String = quotedString

def quotedString: String = database.map(db => s"`$db`.`$funcName`").getOrElse(s"`$funcName`")

def unquotedString: String = database.map(db => s"$db.$funcName").getOrElse(funcName)
def this(name: String) = this(name, None)
}

private[sql] object FunctionIdentifier {
Expand Down

0 comments on commit caa4013

Please sign in to comment.