• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRombauts / SQLiteCpp / #613698365

22 Nov 2023 10:24AM UTC coverage: 98.238%. First build
#613698365

travis-ci

669 of 681 relevant lines covered (98.24%)

29.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/src/Column.cpp
1
/**
2
 * @file    Column.cpp
3
 * @ingroup SQLiteCpp
4
 * @brief   Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement.
5
 *
6
 * Copyright (c) 2012-2025 Sebastien Rombauts (sebastien.rombauts@gmail.com)
7
 *
8
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9
 * or copy at http://opensource.org/licenses/MIT)
10
 */
11
#include <SQLiteCpp/Column.h>
12

13
#include <sqlite3.h>
14

15
#include <iostream>
16

17
namespace SQLite
18
{
19

20
const int INTEGER   = SQLITE_INTEGER;
21
const int FLOAT     = SQLITE_FLOAT;
22
const int TEXT      = SQLITE_TEXT;
23
const int BLOB      = SQLITE_BLOB;
24
const int Null      = SQLITE_NULL;
25

26

27
// Encapsulation of a Column in a row of the result pointed by the prepared Statement.
28
Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) :
418✔
29
    mStmtPtr(aStmtPtr),
30
    mIndex(aIndex)
418✔
31
{
32
    if (!aStmtPtr)
418✔
33
    {
34
        throw SQLite::Exception("Statement was destroyed");
×
35
    }
36
}
418✔
37

38
// Return the named assigned to this result column (potentially aliased)
39
const char* Column::getName() const noexcept
3✔
40
{
41
    return sqlite3_column_name(mStmtPtr.get(), mIndex);
3✔
42
}
43

44
#ifdef SQLITE_ENABLE_COLUMN_METADATA
45
// Return the name of the table column that is the origin of this result column
46
const char* Column::getOriginName() const noexcept
2✔
47
{
48
    return sqlite3_column_origin_name(mStmtPtr.get(), mIndex);
2✔
49
}
50
#endif
51

52
// Return the integer value of the column specified by its index starting at 0
53
int32_t Column::getInt() const noexcept
110✔
54
{
55
    return sqlite3_column_int(mStmtPtr.get(), mIndex);
110✔
56
}
57

58
// Return the unsigned integer value of the column specified by its index starting at 0
59
uint32_t Column::getUInt() const noexcept
11✔
60
{
61
    return static_cast<unsigned>(getInt64());
11✔
62
}
63

64
// Return the 64bits integer value of the column specified by its index starting at 0
65
int64_t Column::getInt64() const noexcept
45✔
66
{
67
    return sqlite3_column_int64(mStmtPtr.get(), mIndex);
45✔
68
}
69

70
// Return the double value of the column specified by its index starting at 0
71
double Column::getDouble() const noexcept
29✔
72
{
73
    return sqlite3_column_double(mStmtPtr.get(), mIndex);
29✔
74
}
75

76
// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0
77
const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept
91✔
78
{
79
    auto pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr.get(), mIndex));
91✔
80
    return (pText ? pText : apDefaultValue);
91✔
81
}
82

83
// Return a pointer to the blob value (*not* NULL terminated) of the column specified by its index starting at 0
84
const void* Column::getBlob() const noexcept
7✔
85
{
86
    return sqlite3_column_blob(mStmtPtr.get(), mIndex);
7✔
87
}
88

89
// Return a std::string to a TEXT or BLOB column
90
std::string Column::getString() const
42✔
91
{
92
    // Note: using sqlite3_column_blob and not sqlite3_column_text
93
    // - no need for sqlite3_column_text to add a \0 on the end, as we're getting the bytes length directly
94
    //   however, we need to call sqlite3_column_bytes() to ensure correct format. It's a noop on a BLOB
95
    //   or a TEXT value with the correct encoding (UTF-8). Otherwise it'll do a conversion to TEXT (UTF-8).
96
    (void)sqlite3_column_bytes(mStmtPtr.get(), mIndex);
42✔
97
    auto data = static_cast<const char *>(sqlite3_column_blob(mStmtPtr.get(), mIndex));
42✔
98

99
    // SQLite docs: "The safest policy is to invoke… sqlite3_column_blob() followed by sqlite3_column_bytes()"
100
    // Note: std::string is ok to pass nullptr as first arg, if length is 0
101
    return std::string(data, sqlite3_column_bytes(mStmtPtr.get(), mIndex));
42✔
102
}
103

104
// Return the type of the value of the column
105
int Column::getType() const noexcept
73✔
106
{
107
    return sqlite3_column_type(mStmtPtr.get(), mIndex);
73✔
108
}
109

110
// Return the number of bytes used by the text value of the column
111
int Column::getBytes() const noexcept
64✔
112
{
113
    return sqlite3_column_bytes(mStmtPtr.get(), mIndex);
64✔
114
}
115

116
// Standard std::ostream inserter
117
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
47✔
118
{
119
    aStream.write(aColumn.getText(), aColumn.getBytes());
47✔
120
    return aStream;
47✔
121
}
122

123
}  // namespace SQLite
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc