Q1. What is prepared statement?
for eg below:
var userStatmt: PreparedStatement = _
for {
insert <- cassandraSession.prepare(
"INSERT INTO usertable(id, name) VALUES(?, ?)")
} yield {
userStatmt = insert
Done
}
Does prepared statement means that the words are stored because below if not stored how will name
in the below will be known
"SELECT * FROM usertable WHERE name = '$name'"
Q2. If so , why bind is used for?
for example :
val userBindStatement: BoundStatement = userStatmt.bind()
userBindStatement.setString("id", user.id)
userBindStatement.setString("name", user.name)
Could you please explain, I have just started lagom and found this in Lagom Readside and I am also new to microservices and also there are limited resources on this topics.
Prepared statements are an optimisation that means the statement doesn't need to be sent to the server and parsed each time it's executed. In order for this to work, any parts of the statement that are dynamic (ie parameters) are replaced with placeholders (ie ?), which then need to be bound to actual values when the statement is executed.
This by the way is nothing specific to Lagom, it's specific to Cassandra (and even then, the same concept is used in many databases, especially relational databases). You can read more about it as it applies to Cassandra here:
https://docs.datastax.com/en/developer/java-driver/3.0/manual/statements/prepared/
And the general concept of prepared statements is described here: