Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Column<> more open #148

Closed
chmuche opened this issue Aug 31, 2017 · 2 comments
Closed

Make Column<> more open #148

chmuche opened this issue Aug 31, 2017 · 2 comments

Comments

@chmuche
Copy link

chmuche commented Aug 31, 2017

I want to use Exposed inside my framework but the definition of the column don't satisfy me.
I want to add more information about a column.

A pattern info to satisfy before the insert/update for example:

object Users {
val login = varchar("login", pattern="my regex validator", otherInfo =...)
}

I can't do that

so now I make a class that hold a table instance.

But I can't do the same trick with column
because if I create for example a custom column

class MyStringColumn(val col: Column<String, val pattern=""){
 //some code here
}

when I use the sql query api I need to write something like

val userId = Users.insert {
            it[Users.login.col] = "My login"
        } get Users.id

but it's ugly.

So my question is
Can you make Column and why not Table open
and any other answer are welcome.

Thanks

@Tapac
Copy link
Contributor

Tapac commented Aug 31, 2017

You don't need to extend Column, ColumnType is a right way to do.
Try this:

class RegexpString(private val pattern: String) : StringColumnType() {
    private val regexp = pattern.toRegex()
    override fun valueToDB(value: Any?): Any? {
        assert(value is String && regexp.matches(value)) { "$value doesn't match $pattern" }
        return super.valueToDB(value)
    }
}

fun Table.regexpString(name:String, pattern: String) = registerColumn<String>(name, RegexpString(pattern))

object Users : Table() {
   val login = regexpString("login", "pattern")
}

@chmuche
Copy link
Author

chmuche commented Aug 31, 2017

that so nice, thanks.
I close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants