Skip to content

Conversation

@akhodakivskiy
Copy link
Contributor

My limited understanding of the Activate under the hood and some free time over the weekend resulted in this pull request. The idea is to allow users define context specific query functions and operators.

@fwbrasil could you please review and let me know what you think? Is this a worthwhile approach? Should I extend it to allow custom operators?

@fwbrasil
Copy link
Owner

Hi Anton. Thanks for digging into this! :)

I'm working on a project using Activate and I also need to use custom functions, but in my case it should be used to store/recover an entity property. For instance:

class MyEntity(var updatedAt: Long) extends Entity

The "updatedAt" is a date value in the mysql database. To store it I need to use the from_unixtime function and to recover, unix_timestamp. I'm using a really hacky solution for this overriding the escape method for the mysql dialect.

I think we could work on something that works for queries and entity properties.

My idea is to have this class in Activate:

abstract class MappedValue[T](implicit tval: (=> T) => StatementSelectValue) {
   val value: T
}

And use it:

case class UpdatedAt(value: Long) extends MappedValue[Long] {
     def toStorage(column: String) = s"from_unixtime($column)"
     def fromStorage(column: String) = s"unix_timestamp($column)"
}

class MyEntity(var updatedAt: UpdatedAt) extends Entity

object Main extends App {
     transactional {
          new MyEntity(UpdatedAt(112221))
     } 
     transactional {
          val entity = select[MyEntity].where(_.updatedAt :== UpdatedAt(112221)).head
     } 
}

wdyt?

@akhodakivskiy
Copy link
Contributor Author

So it's definitely useful to be able to run storage specific functions on either parts of the select/update/insert statements, e.g. SELECT unix_timestamp(date_time_column) FROM a_table WHERE abs(int_column) > 10 or INSERT INTO a_table VALUES (from_timestamp(21344323432), pow(3, 4))

It's fairly straightforward to plug them into SQL based backends, and probably not very challenging for Mongo as well. But the memory context (LiveCache) seems to be more challenging. Unless we make these features available only for contexts with some sort of backend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fwbrasil what is the purpose of entityValue in this context? I was trying to trace it, but couldn't find any use of it for FunctionApply.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akhodakivskiy It is used to run the in-memory queries.

@fwbrasil
Copy link
Owner

fwbrasil commented May 2, 2014

Hey, sorry for taking so long to answer.

Good catch about the in-memory queries. We could have an abstract method in the MappedValue class to return the value for in-memory queries.

I will work to cut the 1.5 version and should be able to work with you on this following.

Cheers

@akhodakivskiy
Copy link
Contributor Author

@fwbrasil will you have time to tackle this issue? Which approach do you think makes the most sense to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants