Util.hxx 3.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "util/Compiler.h"
24
#include "Error.hxx"
25 26 27 28 29

#include <sqlite3.h>

#include <assert.h>

30 31
namespace Sqlite {

32 33 34 35 36 37 38 39 40 41 42 43
static inline sqlite3_stmt *
Prepare(sqlite3 *db, const char *sql)
{
	sqlite3_stmt *stmt;
	int ret = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
	if (ret != SQLITE_OK)
		throw SqliteError(db, ret,
				  "sqlite3_prepare_v2() failed");

	return stmt;
}

44 45 46
/**
 * Throws #SqliteError on error.
 */
47
static void
48
Bind(sqlite3_stmt *stmt, unsigned i, const char *value)
49 50
{
	int result = sqlite3_bind_text(stmt, i, value, -1, nullptr);
51 52
	if (result != SQLITE_OK)
		throw SqliteError(stmt, result, "sqlite3_bind_text() failed");
53 54 55
}

template<typename... Args>
56 57
static void
BindAll2(gcc_unused sqlite3_stmt *stmt, gcc_unused unsigned i)
58 59 60 61 62
{
	assert(int(i - 1) == sqlite3_bind_parameter_count(stmt));
}

template<typename... Args>
63 64
static void
BindAll2(sqlite3_stmt *stmt, unsigned i,
65
	 const char *value, Args&&... args)
66
{
67 68
	Bind(stmt, i, value);
	BindAll2(stmt, i + 1, std::forward<Args>(args)...);
69 70
}

71
/**
72
 * Throws #SqliteError on error.
73 74
 */
template<typename... Args>
75 76
static void
BindAll(sqlite3_stmt *stmt, Args&&... args)
77
{
78 79 80
	assert(int(sizeof...(args)) == sqlite3_bind_parameter_count(stmt));

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

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * 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.
100 101
 *
 * Throws #SqliteError on error.
102 103
 */
static bool
104
ExecuteRow(sqlite3_stmt *stmt)
105 106 107 108 109 110
{
	int result = ExecuteBusy(stmt);
	if (result == SQLITE_ROW)
		return true;

	if (result != SQLITE_DONE)
111
		throw SqliteError(stmt, result, "sqlite3_step() failed");
112 113 114 115 116 117 118

	return false;
}

/**
 * Wrapper for ExecuteBusy() that interprets everything other than
 * SQLITE_DONE as error.
119 120
 *
 * Throws #SqliteError on error.
121
 */
122 123
static void
ExecuteCommand(sqlite3_stmt *stmt)
124 125
{
	int result = ExecuteBusy(stmt);
126 127
	if (result != SQLITE_DONE)
		throw SqliteError(stmt, result, "sqlite3_step() failed");
128 129 130 131
}

/**
 * Wrapper for ExecuteCommand() that returns the number of rows
132 133 134
 * modified via sqlite3_changes().
 *
 * Throws #SqliteError on error.
135
 */
136 137
static inline unsigned
ExecuteChanges(sqlite3_stmt *stmt)
138
{
139
	ExecuteCommand(stmt);
140 141 142 143

	return sqlite3_changes(sqlite3_db_handle(stmt));
}

144 145
/**
 * Wrapper for ExecuteChanges() that returns true if at least one row
146 147 148
 * was modified.  Returns false if nothing was modified.
 *
 * Throws #SqliteError on error.
149 150
 */
static inline bool
151
ExecuteModified(sqlite3_stmt *stmt)
152
{
153
	return ExecuteChanges(stmt) > 0;
154 155
}

156
template<typename F>
157 158
static inline void
ExecuteForEach(sqlite3_stmt *stmt, F &&f)
159 160
{
	while (true) {
161 162
		int result = ExecuteBusy(stmt);
		switch (result) {
163 164 165 166 167
		case SQLITE_ROW:
			f();
			break;

		case SQLITE_DONE:
168
			return;
169 170

		default:
171
			throw SqliteError(stmt, result, "sqlite3_step() failed");
172 173 174 175
		}
	}
}

176 177
} // namespace Sqlite

178
#endif