Util.hxx 3.47 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_SQLITE_UTIL_HXX
#define MPD_SQLITE_UTIL_HXX

23
#include "Error.hxx"
24 25 26 27 28

#include <sqlite3.h>

#include <assert.h>

29 30 31
/**
 * Throws #SqliteError on error.
 */
32
static void
33
Bind(sqlite3_stmt *stmt, unsigned i, const char *value)
34 35
{
	int result = sqlite3_bind_text(stmt, i, value, -1, nullptr);
36 37
	if (result != SQLITE_OK)
		throw SqliteError(stmt, result, "sqlite3_bind_text() failed");
38 39 40
}

template<typename... Args>
41 42
static void
BindAll2(gcc_unused sqlite3_stmt *stmt, gcc_unused unsigned i)
43 44 45 46 47
{
	assert(int(i - 1) == sqlite3_bind_parameter_count(stmt));
}

template<typename... Args>
48 49
static void
BindAll2(sqlite3_stmt *stmt, unsigned i,
50
	 const char *value, Args&&... args)
51
{
52 53
	Bind(stmt, i, value);
	BindAll2(stmt, i + 1, std::forward<Args>(args)...);
54 55
}

56
/**
57
 * Throws #SqliteError on error.
58 59
 */
template<typename... Args>
60 61
static void
BindAll(sqlite3_stmt *stmt, Args&&... args)
62
{
63 64 65
	assert(int(sizeof...(args)) == sqlite3_bind_parameter_count(stmt));

	BindAll2(stmt, 1, std::forward<Args>(args)...);
66 67
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/**
 * Call sqlite3_stmt() repepatedly until something other than
 * SQLITE_BUSY is returned.
 */
static int
ExecuteBusy(sqlite3_stmt *stmt)
{
	int result;
	do {
		result = sqlite3_step(stmt);
	} while (result == SQLITE_BUSY);

	return result;
}

/**
 * Wrapper for ExecuteBusy() that returns true on SQLITE_ROW.
85 86
 *
 * Throws #SqliteError on error.
87 88
 */
static bool
89
ExecuteRow(sqlite3_stmt *stmt)
90 91 92 93 94 95
{
	int result = ExecuteBusy(stmt);
	if (result == SQLITE_ROW)
		return true;

	if (result != SQLITE_DONE)
96
		throw SqliteError(stmt, result, "sqlite3_step() failed");
97 98 99 100 101 102 103

	return false;
}

/**
 * Wrapper for ExecuteBusy() that interprets everything other than
 * SQLITE_DONE as error.
104 105
 *
 * Throws #SqliteError on error.
106
 */
107 108
static void
ExecuteCommand(sqlite3_stmt *stmt)
109 110
{
	int result = ExecuteBusy(stmt);
111 112
	if (result != SQLITE_DONE)
		throw SqliteError(stmt, result, "sqlite3_step() failed");
113 114 115 116
}

/**
 * Wrapper for ExecuteCommand() that returns the number of rows
117 118 119
 * modified via sqlite3_changes().
 *
 * Throws #SqliteError on error.
120
 */
121 122
static inline unsigned
ExecuteChanges(sqlite3_stmt *stmt)
123
{
124
	ExecuteCommand(stmt);
125 126 127 128

	return sqlite3_changes(sqlite3_db_handle(stmt));
}

129 130
/**
 * Wrapper for ExecuteChanges() that returns true if at least one row
131 132 133
 * was modified.  Returns false if nothing was modified.
 *
 * Throws #SqliteError on error.
134 135
 */
static inline bool
136
ExecuteModified(sqlite3_stmt *stmt)
137
{
138
	return ExecuteChanges(stmt) > 0;
139 140
}

141
template<typename F>
142 143
static inline void
ExecuteForEach(sqlite3_stmt *stmt, F &&f)
144 145
{
	while (true) {
146 147
		int result = ExecuteBusy(stmt);
		switch (result) {
148 149 150 151 152
		case SQLITE_ROW:
			f();
			break;

		case SQLITE_DONE:
153
			return;
154 155

		default:
156
			throw SqliteError(stmt, result, "sqlite3_step() failed");
157 158 159 160
		}
	}
}

161
#endif