Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Statement.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
10
11#include "Statement.h"
12
13using namespace Stroika::Foundation;
14
15using namespace Characters;
16using namespace Debug;
17using namespace Database;
18using namespace Database::SQL;
19using namespace Execution;
20using namespace Time;
21
22// Comment this in to turn on aggressive noisy DbgTrace in this module
23//#define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
24
25/*
26 ********************************************************************************
27 ********************* SQL::Statement::ColumnDescription ************************
28 ********************************************************************************
29 */
31{
33 sb << "{"sv;
34 sb << "name: "sv << fName;
35 sb << ", type: "sv << fType;
36 sb << "}"sv;
37 return sb;
38}
39
40/*
41 ********************************************************************************
42 ****************** SQL::Statement::ParameterDescription ************************
43 ********************************************************************************
44 */
46{
48 sb << "{"sv;
49 sb << "name: "sv << fName;
50 sb << ", value: "sv << fValue;
51 sb << "}"sv;
52 return sb;
53}
54
55/*
56 ********************************************************************************
57 ****************************** SQL::Statement **********************************
58 ********************************************************************************
59 */
61{
62#if USE_NOISY_TRACE_IN_THIS_MODULE_
63 TraceContextBumper ctx{"SQL::Statement::GetAllRemainingRows"};
64#endif
65 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
66 Sequence<Row> result;
67 while (auto o = GetNextRow ()) {
68 result += *o;
69 }
70 return result;
71}
72
74{
75#if USE_NOISY_TRACE_IN_THIS_MODULE_
76 TraceContextBumper ctx{"SQL::Statement::GetAllRemainingRows"};
77#endif
78 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
80 ColumnDescription col0 = GetColumns ()[restrictToColumn];
81 while (auto o = GetNextRow ()) {
82 result += *o->Lookup (col0.fName);
83 }
84 return result;
85}
86
87Sequence<tuple<VariantValue, VariantValue>> Statement::GetAllRemainingRows (size_t restrictToColumn1, size_t restrictToColumn2)
88{
89#if USE_NOISY_TRACE_IN_THIS_MODULE_
90 TraceContextBumper ctx{"SQL::Statement::GetAllRemainingRows"};
91#endif
92 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
94 ColumnDescription col0 = GetColumns ()[restrictToColumn1];
95 ColumnDescription col1 = GetColumns ()[restrictToColumn2];
96 while (auto o = GetNextRow ()) {
97 result += make_tuple (*o->Lookup (col0.fName), *o->Lookup (col1.fName));
98 }
99 return result;
100}
101
103 size_t restrictToColumn2, size_t restrictToColumn3)
104{
105#if USE_NOISY_TRACE_IN_THIS_MODULE_
106 TraceContextBumper ctx{"SQL::Statement::GetAllRemainingRows"};
107#endif
108 AssertExternallySynchronizedMutex::WriteContext critSec{_fAssertExternallySynchronizedMutex};
110 ColumnDescription col0 = GetColumns ()[restrictToColumn1];
111 ColumnDescription col1 = GetColumns ()[restrictToColumn2];
112 ColumnDescription col2 = GetColumns ()[restrictToColumn3];
113 while (auto o = GetNextRow ()) {
114 result += make_tuple (*o->Lookup (col0.fName), *o->Lookup (col1.fName), *o->Lookup (col2.fName));
115 }
116 return result;
117}
118
120{
121 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
122 int idx = 0;
123 Bind ();
124 for (const auto& i : parameters) {
125 if (i.fName) {
126 Bind (*i.fName, i.fValue);
127 }
128 else {
129 Bind (idx, i.fValue);
130 }
131 ++idx;
132 }
133}
134
136{
137 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
138 Bind ();
139 for (const auto& i : parameters) {
140 Bind (i.fKey, i.fValue);
141 }
142}
143
145{
146#if USE_NOISY_TRACE_IN_THIS_MODULE_
147 TraceContextBumper ctx{"SQL::Statement::Execute"};
148#endif
149 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
150 Reset ();
151 (void)_fRep->GetNextRow ();
152}
153
155{
156#if USE_NOISY_TRACE_IN_THIS_MODULE_
157 TraceContextBumper ctx{"SQL::Statement::Execute"};
158#endif
159 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
160 Reset ();
161 Bind (parameters);
162 (void)_fRep->GetNextRow ();
163}
164
166{
167#if USE_NOISY_TRACE_IN_THIS_MODULE_
168 TraceContextBumper ctx{"SQL::Statement::Execute", "parameters={}"_f, parameters};
169#endif
170 AssertExternallySynchronizedMutex::WriteContext declareContext{_fAssertExternallySynchronizedMutex};
171 Reset ();
172 Bind (parameters);
173 (void)_fRep->GetNextRow ();
174}
175
177{
178 AssertExternallySynchronizedMutex::ReadContext declareContext{_fAssertExternallySynchronizedMutex};
179 StringBuilder sb;
180 sb << "{"sv;
181 sb << "Parameter-Bindings: "sv << GetParameters ();
182 sb << ", Column-Descriptions: "sv << GetColumns ();
183 sb << ", Original-SQL: "sv << GetSQL ();
184 sb << "}"sv;
185 return sb;
186}
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
nonvirtual void Reset()
resets the prepared statement back to the beginning of its program (this does NOT clear bindings)
Definition Statement.inl:52
nonvirtual optional< Row > GetNextRow()
Definition Statement.inl:58
nonvirtual String GetSQL(WhichSQLFlag whichSQL=WhichSQLFlag::eOriginal) const
Definition Statement.inl:46
nonvirtual Sequence< ColumnDescription > GetColumns() const
Definition Statement.inl:16
nonvirtual Sequence< ParameterDescription > GetParameters() const
Definition Statement.inl:22
nonvirtual Sequence< Row > GetAllRemainingRows()
Call GetNextRow () repeatedly, and accumulate Rows into a Sequence (
Definition Statement.cpp:60
shared_lock< const AssertExternallySynchronizedMutex > ReadContext
Instantiate AssertExternallySynchronizedMutex::ReadContext to designate an area of code where protect...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237