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

support schema operations #805

Closed
wants to merge 7 commits into from
Closed

Conversation

hfazai
Copy link
Contributor

@hfazai hfazai commented Feb 22, 2020

Fixes #145 , #254 and #803
This PR is to support shemas creation,cross-joins from different schemas and make selects from different schemas. also make all operations(selects, drops and updates) with specific schema . Some examples of use

/** create two schemas  */
SchemaUtils.createSchema(Schema("schema1"))
SchemaUtils.createSchema(Schema("schema2"))

/** create tables in schemas */
val author = Author.withSchema(schema1)
val book = Book.withSchema(schema2, author)
SchemaUtils.create(author)
SchemaUtils.create(book)

/** insert in Author table from schema1  */
Author.insertInSchema(schema1) {
    it[name] = "author-name"
}

/** you can also use author.insert {} directly or Author.withSchema(schema1).insert{} */
author.insert {
    it[Author.name] = "author2-name"
}

/** test inner-joins from different schemas  */
(Author.withSchema(schema1) innerJoin Book.withSchema(schema2)).slice(Book.id, Author.id).selectAll().forEach {
    println("${it[Author.id]} wrote ${it[Book.id]}")
}

/** test cross-joins from different schemas  */
(Author.withSchema(schema1) crossJoin Book.withSchema(schema2)).slice(Book.id, Author.id).selectAll().forEach {
    println("${it[Author.id]} wrote ${it[Book.id]}")
}

/** same thing for right and left joins */

/** you can use author and book objects directly  */
(author innerJoin book).slice(Book.id, Author.id).selectAll().forEach {
    println("${it[Author.id]} wrote ${it[Book.id]}")
}

val authorSchema = Author.withSchema(schema1)

/** update table with schema. */
authorSchema.update({ Author.name eq "author1" }) {
    it[Author.name] = "author"
}

/** delete from table with schema. */
authorSchema.deleteWhere {
    Author.name eq "hichem"
 }
    
object Author : IntIdTable("authork") {
    val name = varchar("name", 20)
}

object Book : Table("bookk") {
    val id = integer("id")
    val authorId = reference("authorId", Author).nullable()

    override val primaryKey = PrimaryKey(id)
}

This PR could be a first step to resolve - #795

@hfazai hfazai requested a review from Tapac February 22, 2020 15:00
@hfazai
Copy link
Contributor Author

hfazai commented Feb 22, 2020

Also, you can optionally specify the "authorization" parameter when creating schema :

val schema = Schema (name = "schema1",
                     authorization = "root")

SchemaUtils.createSchema(schema)

//To drop
SchemaUtils.dropSchema(schema )

@Tapac
Copy link
Contributor

Tapac commented Feb 23, 2020

Hi, thank you for a PR. I have some ideas and suggestions.
Do you registered on kotlin slack? It will be a bit faster to discuss short questions there.
You could find me (tapac) at kotlinlang.slack.com.

If you could not/won't use slack I will share my thoughts there

@hfazai
Copy link
Contributor Author

hfazai commented Feb 23, 2020

Hi, thank you for a PR. I have some ideas and suggestions.

Great 🎉 I already inspired it from your last suggestions. And tell me if you have any change requests or improvements 😉

Do you registered on kotlin slack? It will be a bit faster to discuss short questions there.
You could find me (tapac) at kotlinlang.slack.com.

I'm not registred but it would be great to do that. Do I need to have an email (@jetbrains.com) to register to kotlin slack? (I'm not very familiar with slack 😄) . I tried to join kotlinlang workspace but it tells me to use @jetbrains.com mail to register

@Tapac
Copy link
Contributor

Tapac commented Feb 23, 2020

I sent you an invitation.

@hfazai
Copy link
Contributor Author

hfazai commented Apr 11, 2020

Closed, should be done with another design.

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

Successfully merging this pull request may close these issues.

PostgreSQL: Referencing data on another schema
2 participants