alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Relation.scala)

This example Scala source code file (Relation.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

boolean, fieldmetadata, int, iterable, iterator, iterator, list, option, option, relation, string, string, tuple, tuple

The Scala Relation.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.dbc
package result


/** An ISO-9075:2003 (SQL) table. This is equivalent to a relation in the
 * relational model. */
@deprecated(DbcIsDeprecated, "2.9.0") abstract class Relation extends AnyRef with Iterable[Tuple] {

  /** The statement that generated this relation. */
  def statement: scala.dbc.statement.Relation
  
  /** A JDBC result containing this relation. */
  protected def sqlResult: java.sql.ResultSet
  
  /** A JDBC metadata object attached to the relation. */
  protected def sqlMetadata: java.sql.ResultSetMetaData = sqlResult.getMetaData()
  
  /** Metadata about all fields in a tuple of the relation. */
  def metadata: List[FieldMetadata] =
    for (count <- List.range(1, sqlMetadata.getColumnCount()+1)) yield
      new FieldMetadata {
        val name: String = sqlMetadata.getColumnName(count)
        val index: Int = count
        val datatype: DataType = dbc.datatype.Factory.create(sqlMetadata,count)
        val catalog: String = sqlMetadata.getCatalogName(count)
        val schema: String = sqlMetadata.getSchemaName(count)
        val table: String = sqlMetadata.getTableName(count)
      }
  
  /** Metadata about the field at the given index. If there is no such
   * field <code>None is returned instead. */
  def metadataFor (index:Int): Option[FieldMetadata] = {
    val meta = metadata
    if (meta.length > index)
      Some(meta(index))
    else
      None
  }
  
  /** Metadata about the field with the given column name. If there is no
   * such field, <code>None is returned instead. */
  def metadataFor (name:String): Option[FieldMetadata] = 
    metadata.find(f=>(f.name==name));
  
  /** An iterator on the tuples of the relation.
   * <h3>Caution A Relation only has one single iterator, due to limitations
   * in DBMS. This means that if this method is called multiple times, all returned
   * iterators will share the same state. */
  def iterator: Iterator[Tuple] = new Iterator[Tuple] {
    protected val result: java.sql.ResultSet = Relation.this.sqlResult
    def hasNext: Boolean = resultNext
    private var resultNext = result.next()
    def next: Tuple = {
      if (resultNext) {
        val newTuple = new Tuple {
          val me = this
          val originatingRelation = Relation.this
          val fields: List[Field] = for (fieldMetadata <- metadata) yield
            new Field {
              val metadata = fieldMetadata
              val content = dbc.value.Factory.create(result,metadata.index,metadata.datatype)
              val originatingTuple = me
            }
        }
        resultNext = result.next()
        newTuple
      }
      else sys.error("next on empty iterator")
    }
  }	

}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Relation.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.