1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
/*
* Copyright (C) Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL.
*/
#ifndef ZDB_INCLUDED
#define ZDB_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
/**
* Include this interface in your C code to import the libzdb API.
*
* @file
*/
/* --------------------------------------------------------------- Version */
#define LIBZDB_MAJOR 3
#define LIBZDB_MINOR 4
#define LIBZDB_REVISION 0
#define LIBZDB_VERSION "3.4.0"
#define LIBZDB_VERSION_NUMBER ((LIBZDB_MAJOR * 1000000) + (LIBZDB_MINOR * 1000) + LIBZDB_REVISION)
/* ------------------------------------------------- libzdb API interfaces */
#include <SQLException.h>
#include <URL.h>
#include <ResultSet.h>
#include <PreparedStatement.h>
#include <Connection.h>
#include <ConnectionPool.h>
#ifdef __cplusplus
}
#endif
#ifndef __cplusplus
/* --------------------------------------------------------- Utility Macro */
/**
* @brief Provides a default value if the expression is NULL, 0, or negative.
*
* The valueOr macro is a convenient way to handle potentially unset or error
* values returned by libzdb functions. It works with pointers, integers, and
* floating-point types.
*
* This macro evaluates the expression only once, making it safe to use with
* function calls or expressions that may have side effects.
*
* For pointers:
* - Returns the default value if the expression evaluates to NULL.
* - Otherwise, returns the original pointer value.
*
* For integers and floating-point types:
* - Returns the default value if the expression evaluates to 0 or any negative value.
* - Otherwise, returns the original numeric value.
*
* @param expr The expression to evaluate (typically a libzdb function call)
* @param default_value The value to return if expr is NULL, 0, or negative
*
* @return If expr is not NULL, 0, or negative, returns expr.
* Otherwise, returns default_value.
*
* @note This macro uses a GNU C extension and is compatible with GCC and Clang.
* It may not work with other C compilers.
*
* @example
* // Usage with string (pointer) return type
* const char* host = valueOr(URL_getHost(url), "localhost");
* printf("Host: %s\n", host);
*
* // Usage with integer return type
* int port = valueOr(ResultSet_getInt(r, 1), 5432);
* printf("Port: %d\n", port);
*
* // Usage with floating-point return type
* double percent = valueOr(ResultSet_getDouble(r, 1), 1.0);
* printf("Percent: %.1f\n", percent);
*/
#define valueOr(expr, default_value) \
({ \
__typeof__(expr) _t = (expr); \
(_t < 0 || _t == 0) ? (default_value) : _t; \
})
#endif /* not __cplusplus */
#endif
|