File: Statement.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (456 lines) | stat: -rw-r--r-- 9,840 bytes parent folder | download | duplicates (2)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 * SPDX-FileName: Statement.cpp
 * SPDX-FileContributor: Dmitry Vedenko
 */

#include "Statement.h"

#include <cassert>
#include <cstring>
#include <utility>

#include "Connection.h"

#include "sqlite3.h"

namespace audacity::sqlite
{
struct StatementHandle final
{
   sqlite3_stmt* Handle {};

   explicit StatementHandle(sqlite3_stmt* Handle) noexcept
       : Handle { Handle }
   {
   }

   operator sqlite3_stmt* () noexcept
   {
      return Handle;
   }

   ~StatementHandle()
   {
      if (Handle != nullptr)
         sqlite3_finalize(Handle);
   }
};

Statement::Statement(sqlite3_stmt* stmt)
    : mStatement { std::make_shared<StatementHandle>(stmt) }
{
}

Statement::Statement(Statement&& rhs) noexcept
{
   *this = std::move(rhs);
}

Statement& Statement::operator=(Statement&& rhs) noexcept
{
   std::swap(mStatement, rhs.mStatement);
   return *this;
}

RunContext& Statement::Prepare() noexcept
{
   mRunContext = RunContext { mStatement };
   return *mRunContext;
}

RunContext::RunContext(StatementHandlePtr stmt) noexcept
    : mStatement { std::move(stmt) }
{
}

RunContext::RunContext(RunContext&& rhs) noexcept
{
   *this = std::move(rhs);
}

RunContext& RunContext::operator=(RunContext&& rhs) noexcept
{
   std::swap(mStatement, rhs.mStatement);
   std::swap(mErrors, rhs.mErrors);

   return *this;
}

template<typename Binder>
RunContext& RunContext::DoBind(Binder binder)
{
   if (mStatement == nullptr)
       mErrors.emplace_back(Error(SQLITE_MISUSE));
   else if (int result = binder(); result != SQLITE_OK)
      mErrors.emplace_back(Error(result));

   return *this;
}

RunContext& RunContext::Bind(
   int index, const void* data, int64_t size, bool makeCopy)
{
   if (data == nullptr)
      return BindZeroBlob(index, size);

   return DoBind(
      [&]
      {
         return sqlite3_bind_blob64(
            *mStatement, index, data, size,
            makeCopy ? SQLITE_TRANSIENT : SQLITE_STATIC);
      });
}


RunContext& RunContext::Bind(
   int index, const std::string& value, bool makeCopy)
{
   return Bind(index, std::string_view { value }, makeCopy);
}

RunContext& RunContext::Bind(
   int index, std::string_view value, bool makeCopy)
{
   return DoBind(
      [&]
      {
         if (mNeedsReset)
         {
            mNeedsReset = false;
            sqlite3_reset(*mStatement);
         }

         return sqlite3_bind_text(
            *mStatement, index, value.data(), value.size(),
            makeCopy ? SQLITE_TRANSIENT : SQLITE_STATIC);
      });
}

RunContext& RunContext::Bind(
   int index, const char* value, bool makeCopy)
{
   return Bind(index, std::string_view { value }, makeCopy);
}

RunContext& RunContext::Bind(int index, bool value)
{
   return Bind(index, static_cast<long long>(value));
}

RunContext&
RunContext::Bind(int index, int value)
{
   return Bind(index, static_cast<long long>(value));
}

RunContext&
RunContext::Bind(int index, long value)
{
   return Bind(index, static_cast<long long>(value));
}

RunContext&
RunContext::Bind(int index, long long value)
{
   return DoBind([&] { return sqlite3_bind_int64(*mStatement, index, value); });
}

RunContext& sqlite::RunContext::Bind(int index, std::size_t value)
{
   return DoBind([&] { return sqlite3_bind_int64(*mStatement, index, value); });
}

RunContext&
RunContext::Bind(int index, float value)
{
   return Bind(index, static_cast<double>(value));
}

RunContext&
RunContext::Bind(int index, double value)
{
   return DoBind([&] { return sqlite3_bind_double(*mStatement, index, value); });
}

RunContext&
RunContext::Bind(int index, std::nullptr_t)
{
   return DoBind([&] { return sqlite3_bind_null(*mStatement, index); });
}

RunContext&
RunContext::BindZeroBlob(int index, int64_t size)
{
   return DoBind([&]
                 { return sqlite3_bind_zeroblob64(*mStatement, index, size); });
}

int RunContext::GetParameterIndex(const std::string& name) const noexcept
{
   return mStatement != nullptr ?
             sqlite3_bind_parameter_index(*mStatement, name.data()) :
             -1;
}

RunResult RunContext::Run()
{
   mNeedsReset = true;
   return RunResult { mStatement, std::move(mErrors) };
}

RunResult::RunResult(StatementHandlePtr stmt, std::vector<Error> errors) noexcept
    : mStatement { std::move(stmt) }
    , mErrors { std::move(errors) }
{
   // mStatement can't be nullptr here, by construction
   assert(mStatement != nullptr);

   const auto rc = sqlite3_step(*mStatement);

   mHasRows = rc == SQLITE_ROW;

   if (rc == SQLITE_DONE)
      mModifiedRowsCount = sqlite3_changes(sqlite3_db_handle(*mStatement));

   if (rc != SQLITE_DONE && !mHasRows)
      mErrors.emplace_back(Error(rc));
}

RunResult::RunResult(RunResult&& rhs) noexcept
{
   *this = std::move(rhs);
}

RunResult& RunResult::operator=(RunResult&& rhs) noexcept
{
   std::swap(mStatement, rhs.mStatement);
   std::swap(mErrors, rhs.mErrors);
   std::swap(mHasRows, rhs.mHasRows);
   std::swap(mModifiedRowsCount, rhs.mModifiedRowsCount);

   return *this;
}

RunResult::~RunResult()
{
   if (mStatement != nullptr)
      sqlite3_reset(*mStatement);
}

bool RunResult::IsOk() const noexcept
{
   return mErrors.empty();
}

bool RunResult::HasRows() const noexcept
{
   return mHasRows;
}

int RunResult::GetModifiedRowsCount() const noexcept
{
   return mModifiedRowsCount;
}

const std::vector<Error>& RunResult::GetErrors() const noexcept
{
   return mErrors;
}

RowIterator RunResult::begin() noexcept
{
   return mHasRows ? RowIterator { mStatement, mErrors } : RowIterator {};
}

RowIterator RunResult::end() noexcept
{
   return RowIterator {};
}

RowIterator::RowIterator(StatementHandlePtr stmt, std::vector<Error>& errors) noexcept
    : mStatement { std::move(stmt) }
    , mErrors { &errors }
{
   // mStatement can't be nullptr here, by construction
   assert(mStatement != nullptr);
}

RowIterator::RowIterator () noexcept
    : mDone { true }
{
}

RowIterator::RowIterator(RowIterator&& rhs) noexcept
{
   *this = std::move(rhs);
}

RowIterator& RowIterator::operator=(RowIterator&& rhs) noexcept
{
   std::swap(mStatement, rhs.mStatement);
   std::swap(mErrors, rhs.mErrors);
   std::swap(mRowIndex, rhs.mRowIndex);
   std::swap(mDone, rhs.mDone);
   return *this;
}

RowIterator& RowIterator::operator++() noexcept
{
   if (mStatement == nullptr || mDone)
      return *this;

   const auto rc = sqlite3_step(*mStatement);

   if (rc == SQLITE_ROW)
      ++mRowIndex;
   else
   {
      mDone = true;

      if (rc != SQLITE_DONE)
         mErrors->emplace_back(Error(rc));
   }

   return *this;
}

bool RowIterator::operator==(const RowIterator& rhs) const noexcept
{
   if (mDone != rhs.mDone)
      return false;

   if (mDone)
      return true;

   return mStatement == rhs.mStatement && mRowIndex == rhs.mRowIndex;
}

bool RowIterator::operator!=(const RowIterator& rhs) const noexcept
{
   return !(*this == rhs);
}

Row RowIterator::operator*() const noexcept
{
   return Row { mStatement, *mErrors };
}

Row::Row(StatementHandlePtr statement, std::vector<Error>& errors) noexcept
    : mStatement { std::move(statement) }
    , mErrors { &errors }
{
   if (mStatement != nullptr)
      mColumnsCount = sqlite3_column_count(*mStatement);
}

template <typename Reader>
bool Row::DoGet(Reader reader, int columnIndex) const
{
   if (mStatement == nullptr)
   {
      if (mErrors != nullptr)
         mErrors->emplace_back(Error(SQLITE_MISUSE));
      return false;
   }

   if (columnIndex < 0 || columnIndex >= mColumnsCount)
   {
      if (mErrors != nullptr)
         mErrors->emplace_back(Error(SQLITE_RANGE));
      return false;
   }

   if constexpr (std::is_void_v<decltype(reader())>)
   {
      reader();
      return true;
   }
   else
      return reader();
}

bool Row::Get(int columnIndex, bool& value) const
{
   return DoGet([&] { value = sqlite3_column_int(*mStatement, columnIndex) != 0; }, columnIndex);
}

bool Row::Get(int columnIndex, int& value) const
{
   return DoGet([&] { value = sqlite3_column_int(*mStatement, columnIndex); }, columnIndex);
}

bool Row::Get(int columnIndex, long& value) const
{
   if (sizeof(long) == 4)
      return DoGet([&] { value = sqlite3_column_int(*mStatement, columnIndex); }, columnIndex);
   else
      return DoGet([&] { value = sqlite3_column_int64(*mStatement, columnIndex); }, columnIndex);
}

bool Row::Get(int columnIndex, long long& value) const
{
   return DoGet([&] { value = sqlite3_column_int64(*mStatement, columnIndex); }, columnIndex);
}

bool Row::Get(int columnIndex, float& value) const
{
   return DoGet(
      [&]
      {
         value =
            static_cast<float>(sqlite3_column_double(*mStatement, columnIndex));
      },
      columnIndex);
}

bool Row::Get(int columnIndex, double& value) const
{
   return DoGet([&] { value = sqlite3_column_double(*mStatement, columnIndex); },
      columnIndex);
}

bool Row::Get(int columnIndex, std::string& value) const
{
   return DoGet(
      [&]
      {
         const auto* text = reinterpret_cast<const char*>(
            sqlite3_column_text(*mStatement, columnIndex));

         if (text == nullptr)
            return false;

         // Per sqlite convention, text is never nullptr
         value = text;
         return true;
      },
      columnIndex);
}

int Row::GetColumnCount() const
{
   return sqlite3_column_count(*mStatement);
}

int64_t Row::GetColumnBytes(int columnIndex) const
{
   return sqlite3_column_bytes(*mStatement, columnIndex);
}

int64_t Row::ReadData(int columnIndex, void* buffer, int64_t maxSize) const
{
   const auto* data = sqlite3_column_blob(*mStatement, columnIndex);

   if (data == nullptr)
      return 0;

   const auto size = std::min(maxSize, GetColumnBytes(columnIndex));

   std::memcpy(buffer, data, size);

   return size;
}

} // namespace audacity::sqlite