Coverage Report

Created: 2025-09-04 07:09

/src/tarantool/src/box/bind.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef TARANTOOL_SQL_BIND_H_INCLUDED
2
#define TARANTOOL_SQL_BIND_H_INCLUDED
3
/*
4
 * Copyright 2010-2019, Tarantool AUTHORS, please see AUTHORS file.
5
 *
6
 * Redistribution and use in source and binary forms, with or
7
 * without modification, are permitted provided that the following
8
 * conditions are met:
9
 *
10
 * 1. Redistributions of source code must retain the above
11
 *    copyright notice, this list of conditions and the
12
 *    following disclaimer.
13
 *
14
 * 2. Redistributions in binary form must reproduce the above
15
 *    copyright notice, this list of conditions and the following
16
 *    disclaimer in the documentation and/or other materials
17
 *    provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 */
33
34
#if defined(__cplusplus)
35
extern "C" {
36
#endif
37
38
#include <assert.h>
39
#include <stdint.h>
40
#include <stdlib.h>
41
42
#include "msgpuck.h"
43
#include "core/decimal.h"
44
#include "mp_extension_types.h"
45
#include "tt_uuid.h"
46
#include "datetime.h"
47
48
struct Vdbe;
49
50
/**
51
 * Name and value of an SQL prepared statement parameter.
52
 * @todo: merge with sql_value.
53
 */
54
struct sql_bind {
55
  /** Bind name. NULL for ordinal binds. */
56
  const char *name;
57
  /** Length of the @name. */
58
  uint32_t name_len;
59
  /** Ordinal position of the bind, for ordinal binds. */
60
  uint32_t pos;
61
62
  /** Byte length of the value. */
63
  uint32_t bytes;
64
  /** MessagePack type of the value. */
65
  enum mp_type type;
66
  /** Subtype of MP_EXT type. */
67
  enum mp_extension_type ext_type;
68
  /** Bind value. */
69
  union {
70
    bool b;
71
    double d;
72
    int64_t i64;
73
    uint64_t u64;
74
    /** For string or blob. */
75
    const char *s;
76
    struct tt_uuid uuid;
77
    decimal_t dec;
78
    /** DATETIME value. */
79
    struct datetime dt;
80
    /** INTERVAL value. */
81
    struct interval itv;
82
  };
83
};
84
85
/**
86
 * Return a string name of a parameter marker.
87
 * @param Bind to get name.
88
 * @retval Zero terminated name.
89
 */
90
const char *
91
sql_bind_name(const struct sql_bind *bind);
92
93
/**
94
 * Parse MessagePack array of SQL parameters.
95
 * @param data MessagePack array of parameters. Each parameter
96
 *        either must have scalar type, or must be a map with the
97
 *        following format: {name: value}. Name - string name of
98
 *        the named parameter, value - scalar value of the
99
 *        parameter. Named and positioned parameters can be mixed.
100
 * @param[out] out_bind Pointer to save decoded parameters.
101
 *
102
 * @retval  >= 0 Number of decoded parameters.
103
 * @retval -1 Client or memory error.
104
 */
105
int
106
sql_bind_list_decode(const char *data, struct sql_bind **out_bind);
107
108
/**
109
 * Decode a single bind column from the binary protocol packet.
110
 * @param[out] bind Bind to decode to.
111
 * @param i Ordinal bind number.
112
 * @param packet MessagePack encoded parameter value. Either
113
 *        scalar or map: {string_name: scalar_value}.
114
 *
115
 * @retval  0 Success.
116
 * @retval -1 Memory or client error.
117
 */
118
int
119
sql_bind_decode(struct sql_bind *bind, int i, const char **packet);
120
121
/**
122
 * Bind SQL parameter value to its position.
123
 * @param stmt Prepared statement.
124
 * @param p Parameter value.
125
 * @param pos Ordinal bind position.
126
 *
127
 * @retval  0 Success.
128
 * @retval -1 SQL error.
129
 */
130
int
131
sql_bind_column(struct Vdbe *stmt, const struct sql_bind *p, uint32_t pos);
132
133
/**
134
 * Bind parameter values to the prepared statement.
135
 * @param stmt Prepared statement.
136
 * @param bind Parameters to bind.
137
 * @param bind_count Length of @a bind.
138
 *
139
 * @retval  0 Success.
140
 * @retval -1 Client or memory error.
141
 */
142
static inline int
143
sql_bind(struct Vdbe *stmt, const struct sql_bind *bind, uint32_t bind_count)
144
0
{
145
0
  assert(stmt != NULL);
146
0
  uint32_t pos = 1;
147
0
  for (uint32_t i = 0; i < bind_count; pos = ++i + 1) {
148
0
    if (sql_bind_column(stmt, &bind[i], pos) != 0)
149
0
      return -1;
150
0
  }
151
0
  return 0;
152
0
}
Unexecuted instantiation: execute.c:sql_bind
Unexecuted instantiation: iproto.cc:sql_bind(Vdbe*, sql_bind const*, unsigned int)
Unexecuted instantiation: bind.c:sql_bind
153
154
#if defined(__cplusplus)
155
} /* extern "C" { */
156
#endif
157
158
#endif /* TARANTOOL_SQL_BIND_H_INCLUDED */