Advertisement
Guest User

Untitled

a guest
Mar 1st, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.23 KB | None | 0 0
  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, Rights 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, Rights FROM Users WHERE UPPER(Name) = UPPER(?)")
  26.             private val GetKeyStatement = Connection.prepareStatement("SELECT Key FROM Keys WHERE UPPER(Key) = UPPER(?)")
  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.  
  47.         object RequestDevice {
  48.             private val RequestStatement = Connection.prepareStatement("SELECT Request.* FROM Request WHERE ID = ?")
  49.  
  50.             def request(id: String) = {
  51.                 RequestStatement.setString(1, id)
  52.                 val result = RequestStatement.executeQuery
  53.                 if(!result.next)
  54.                     throw Request.InvalidIDException
  55.                 new DataElement(result.getString("id"), ...)
  56.             }
  57.         }
  58.        
  59.         ...
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement