Commits

Amr Hassan committed 2aba77e

Statement.execute() can now execute a set of semi-colon delimited SQL statements

Comments (0)

Files changed (3)

src/Connection.hpp

 
 	/**
 	 * A shortcut for creating a non-query statement and executing it.
+	 * It can execute a set of SQL statements delimited by semi-colons.
 	 *
 	 * @param sql A UTF-8 SQL statement
 	 */

src/Statement.cpp

 {
 	boost::mutex::scoped_lock(mutex);
 
-	if (prepared) {
-		return;
-	}
+	std::cout << "PREPARING: " << unusedSql << '\n';
 
 	sqlite3_stmt* temp;
-	int resultCode = sqlite3_prepare_v2(internalConnection.get(), _sql.c_str(), -1, &temp, 0);
+	const char* unused;
+	int resultCode = sqlite3_prepare_v2(internalConnection.get(), unusedSql.c_str(), -1, &temp, &unused);
+	unusedSql = unused;
 
 	checkResultCode(resultCode, internalConnection.get());
 
 	internalStatement = boost::shared_ptr<sqlite3_stmt>(temp, StatementDeleter());
-
-	prepared = true;
 }
 
 sqlite3pp::Statement::Statement(const Connection& connection, const std::string& sql) :
-		internalConnection(connection.internalObject), prepared(false), _sql(sql)
+		internalConnection(connection.internalObject), unusedSql(sql)
 {
 	bool opened = false;
-	for (unsigned int i = 0; i < _sql.size(); i++) {
-		if (_sql[i] == '\'') {
+	for (unsigned int i = 0; i < unusedSql.size(); i++) {
+		if (unusedSql[i] == '\'') {
 			opened = !opened;
-		} else if (_sql[i] == '?' && !opened) {
+		} else if (unusedSql[i] == '?' && !opened) {
 			parameterLocations.push_back(i);
 		}
 	}
 {
 	boost::mutex::scoped_lock(mutex);
 
-	prepare();
-	int resultCode = sqlite3_step(internalStatement.get());
-	checkResultCode(resultCode, internalConnection.get());
+	while (!unusedSql.empty()) {
+		prepare();
+		int resultCode = sqlite3_step(internalStatement.get());
+		checkResultCode(resultCode, internalConnection.get());
+	}
 }
 
 ResultSet Statement::executeQuery() throw (Exception)
 	std::string replacer = '\'' + escape(value) + '\'';
 
 	unsigned int location = parameterLocations[arg];
-	_sql.erase(location, 1);
-	_sql.insert(location, replacer);
+	unusedSql.erase(location, 1);
+	unusedSql.insert(location, replacer);
 
 	for (unsigned int i = arg+1; i < parameterLocations.size(); i++) {
 		parameterLocations[i] += replacer.size() - 1;
 	std::string replacer = ss.str();
 
 	unsigned int location = parameterLocations[arg];
-	_sql.erase(location, 1);
-	_sql.insert(location, replacer);
+	unusedSql.erase(location, 1);
+	unusedSql.insert(location, replacer);
 
 	for (unsigned int i = arg+1; i < parameterLocations.size(); i++) {
 		parameterLocations[i] += replacer.size() - 1;
 	std::string replacer = ss.str();
 
 	unsigned int location = parameterLocations[arg];
-	_sql.erase(location, 1);
-	_sql.insert(location, replacer);
+	unusedSql.erase(location, 1);
+	unusedSql.insert(location, replacer);
 
 	for (unsigned int i = arg+1; i < parameterLocations.size(); i++) {
 		parameterLocations[i] += replacer.size() - 1;

src/Statement.hpp

 	Statement(const Connection& connection, const std::string& sql);
 
 	/**
-	 * Execute this non-query statement
+	 * Execute this non-query statement. It can execute a set of semi-colon statements.
 	 */
 	void execute() throw (Exception);
 
 
 	boost::shared_ptr<sqlite3_stmt> internalStatement;
 	boost::shared_ptr<sqlite3> internalConnection;
-	bool prepared;
-	std::string _sql;
+	std::string unusedSql;
 	std::vector<int> parameterLocations;
 	static boost::recursive_mutex mutex;			// TODO: Make this non-static
 };