Util.hxx 3.77 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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
namespace Sqlite {

31 32 33 34 35 36 37 38 39 40 41 42
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;
}

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

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

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

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

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

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

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

	return false;
}

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

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

	return sqlite3_changes(sqlite3_db_handle(stmt));
}

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

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

		case SQLITE_DONE:
167
			return;
168 169

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

175 176
} // namespace Sqlite

177
#endif