1. object DatabaseConnection {
  2.     private val Connection = ...
  3.    
  4.     private object PasswordDevice {
  5.         def check(pw1: String, pw2: String) = ...
  6.         def encrypt(password: String) = ...
  7.     }
  8.    
  9.     object LoginDevice {
  10.         private val GetUserStatement = Connection.prepareStatement("SELECT Name, Password, ... FROM Users WHERE UPPER(Name) = UPPER(?)")
  11.  
  12.         def login(username: String, password: String) = {
  13.             GetUserStatement.setString(1, username)
  14.             val userResult = GetUserStatement.executeQuery
  15.             if ( !userResult.next )
  16.                 throw User.NotFoundException
  17.             if ( !PasswordDevice.check(password, userResult.getString("Password")) )
  18.                 throw User.WrongPasswordException
  19.             new User(userResult.getString("Name"), ...)
  20.         }
  21.     }
  22.  
  23.  
  24.     object RegisterDevice {
  25.         private val RegisterStatement = Connection.prepareStatement("SELECT Name, Password, ... FROM Users WHERE UPPER(Name) = UPPER(?)")
  26.         private val GetKeyStatement = Connection.prepareStatement("SELECT Key FROM Keys WHERE Key = ?")
  27.         private val InsertStatement = Connection.prepareStatement(s"INSERT INTO Users VALUES(?, ?, ...)")
  28.         private val DeleteKeyStatement = Connection.prepareStatement("DELETE FROM Keys WHERE Key = ?")
  29.  
  30.         def register(name: String, password: String, key: String) = {
  31.             RegisterStatement.setString(1, name)
  32.             GetKeyStatement.setString(1, key)
  33.             val userExistsResult = RegisterStatement.executeQuery
  34.             val keyValidResult = GetKeyStatement.executeQuery
  35.             if ( userExistsResult.next ) throw User.AlreadyExistsException
  36.             if ( !keyValidResult.next ) throw User.InvalidKeyException
  37.             InsertStatement.setString(1, name)
  38.             InsertStatement.setString(2, PasswordDevice.encrypt(password))
  39.             InsertStatement.executeUpdate()
  40.             DeleteKeyStatement.setString(1, key)
  41.             DeleteKeyStatement.executeUpdate()
  42.             new User(name, ...)
  43.         }
  44.     }
  45.  
  46.     object RequestDevice {
  47.         private val RequestStatement = Connection.prepareStatement("SELECT Request.* FROM Request WHERE ID = ?")
  48.  
  49.         def request(id: String) = {
  50.             RequestStatement.setString(1, id)
  51.             val result = RequestStatement.executeQuery
  52.             if(!result.next)
  53.                 throw Request.InvalidIDException
  54.             new DataElement(result.getString("ID"), ...)
  55.         }
  56.     }
  57.    
  58.     ...
  59. }