Created
December 20, 2010 05:52
-
-
Save apathyboy/748073 to your computer and use it in GitHub Desktop.
binding parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto db_manager = make_shared<DatabaseManager>(); | |
auto connection = db_manager->connect("mysql:host=localhost;schema=global", "swganh", "something"); | |
auto statement = connection->prepareStatement("CALL sp_PersistSomeObject(:id, :name, :description)"); | |
// bind values | |
statement->bindParam(":id", object->id()); | |
statement->bindParam(":name", object->name()); | |
statement->bindParam(":description", object->description()); | |
// bind references | |
statement->bindParam(":id", std::bind(Object::id, object)); | |
statement->bindParam(":name", std::bind(Object::name, object)); | |
statement->bindParam(":description", std::bind(Object::description, object)); | |
// calls it sync | |
statement->execute(); | |
// executes an async query... if using the bound references, we could persist | |
// an object to the database by simply calling execute() whenever the object is dirty. | |
connection->execute(std::move(statement), [] (shared_ptr<PreparedStatement> stmt) { | |
unique_ptr<ResultRow> row = nullptr; | |
while(row.reset(stmt->fetch())) { | |
cout << row["name"].as<string>(); | |
} | |
}); | |
// executes a sync query | |
statement->execute(true); | |
unique_ptr<ResultRow> row = nullptr; | |
while(row.reset(statement->fetch())) { | |
cout << row["name"].as<string>(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment