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

automic expressions supporting #455

Closed
erhuabushuo opened this issue May 12, 2021 · 6 comments · Fixed by #481
Closed

automic expressions supporting #455

erhuabushuo opened this issue May 12, 2021 · 6 comments · Fixed by #481
Labels
enhancement A feature that exists, works as intended but needs to be improved

Comments

@erhuabushuo
Copy link
Contributor

Masonite ORM currently does not have automic expressions

@erhuabushuo erhuabushuo added the enhancement A feature that exists, works as intended but needs to be improved label May 12, 2021
@Marlysson
Copy link
Contributor

@erhuabushuo Can you provide an example about this expressions?
If you are talking about atomic/operations inside a transaction we have a section about it:

https://orm.masoniteproject.com/installation#transactions

Let me know wheter it' what you're finding?

@erhuabushuo
Copy link
Contributor Author

erhuabushuo commented May 12, 2021

like sqlachemy:

class SomeClass(Base):
    __tablename__ = "some_table"

    # ...

    value = Column(Integer)

someobject = session.query(SomeClass).get(5)

# set 'value' attribute to a SQL expression adding one
someobject.value = SomeClass.value + 1

# issues "UPDATE some_table SET value=value+1"
session.commit()

@erhuabushuo
Copy link
Contributor Author

user = User.find(2)
user.balance = field('balance') + 1
user.save()

UPDATE "users" SET "balance" = "balance" + 1 WHERE ID = 2

@Marlysson
Copy link
Contributor

@erhuabushuo

For the first case you can do this by using .increment/.decrement methods, so you can pass some value to increment the value

builder.table("table").increment("column", value)

https://orm.masoniteproject.com/query-builder#increment

For the second one, we have this way to do this, by using raw expression to perform operation:
#264 (comment)

Let'me know if these way is useful for your problem.

@josephmancuso
Copy link
Member

josephmancuso commented May 12, 2021

Should just be as simple as adding FOR UPDATE and LOCK IN SHARE MODE at the end of queries using the builder and grammar classes.

Orator had this:

https://orator-orm.com/docs/0.9/query_builder.html#pessimistic-locking

MySQL reference:

https://medium.com/@ibraheemabukaff/locking-rows-in-mysql-e84fd3bbb8cd

Postgres reference:

https://www.postgresql.org/docs/9.1/explicit-locking.html#:~:text=To%20acquire%20a%20shared%20row,transaction%20holds%20a%20shared%20lock.

@erhuabushuo
Copy link
Contributor Author

@erhuabushuo

For the first case you can do this by using .increment/.decrement methods, so you can pass some value to increment the value

builder.table("table").increment("column", value)

https://orm.masoniteproject.com/query-builder#increment

For the second one, we have this way to do this, by using raw expression to perform operation:
#264 (comment)

Let'me know if these way is useful for your problem.

raw expression is useful for me, does then model instance fetch the latest value when updated by using raw expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants