Exemple #1
0
const char *
sqlite_strerror(int err)
{
	if (err > 0xFF) {
		gchar *end = g_stpcpy(buf, sqlite_strerror(err & 0xFF));
		g_snprintf(end, _RC_ERRBUF_LEN - (end - buf),
				" (+ ext code %d) (errno %d: %s)",
				err >> 8, errno, strerror(errno));
		return buf;
	}
Exemple #2
0
GError*
_db_count_bean(const struct bean_descriptor_s *descr,
		sqlite3 *db, const gchar *clause, GVariant **params,
		gint64 *pcount)
{
	GError *err = NULL;
	sqlite3_stmt *stmt = NULL;
	gint rc;

	EXTRA_ASSERT(descr != NULL);
	EXTRA_ASSERT(db != NULL);
	EXTRA_ASSERT(pcount != NULL);

	if (!clause || !*clause)
		err = _db_prepare_statement(db, descr->sql_count, &stmt);
	else {
		gchar *sql = g_strconcat(descr->sql_count, " WHERE ", clause, NULL);
		err = _db_prepare_statement(db, sql, &stmt);
		g_free(sql);
	}

	if (NULL != err) {
		g_prefix_error(&err, "Prepare error: ");
		return err;
	}

	if (!(err = _stmt_apply_GV_parameters(stmt, params))) {
		while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
			*pcount = sqlite3_column_int64(stmt, 0);
		}
		if (unlikely(!(rc == SQLITE_OK || rc == SQLITE_DONE))) {
			err = NEWERROR(CODE_INTERNAL_ERROR,
					"Got an error from sqlite: (%d) %s",
					rc, sqlite_strerror(rc));
		}
	}

	sqlite3_finalize(stmt);
	stmt = NULL;
	return err;
}
Exemple #3
0
GError *
_db_get_bean(const struct bean_descriptor_s *descr,
		sqlite3 *db, const gchar *clause, GVariant **params,
		on_bean_f cb, gpointer u)
{
	GError *err = NULL;
	sqlite3_stmt *stmt = NULL;
	gint rc;

	g_assert(descr != NULL);
	g_assert(db != NULL);
	g_assert(params != NULL);
	g_assert(cb != NULL);

	if (!clause || !*clause)
		err = _db_prepare_statement(db, descr->sql_select, &stmt);
	else {
		gchar *sql = g_strconcat(descr->sql_select, " WHERE ", clause, NULL);
		err = _db_prepare_statement(db, sql, &stmt);
		g_free(sql);
	}

	if (NULL != err) {
		g_prefix_error(&err, "Prepare error: ");
		return err;
	}

	if (!(err = _stmt_apply_GV_parameters(stmt, params))) {
		while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
			cb(u, _row_to_bean(descr, stmt));
		if (unlikely(!(rc == SQLITE_OK || rc == SQLITE_DONE))) {
			err = NEWERROR(CODE_INTERNAL_ERROR,
					"Got an error from sqlite: (%d) %s",
					rc, sqlite_strerror(rc));
		}
	}

	sqlite3_finalize(stmt);
	stmt = NULL;
	return err;
}