Flutter Impeller
impeller::ArchiveStatement Class Reference

Represents a read/write query to an archive database. Statements are expensive to create and must be cached for as long as possible. More...

#include <archive_statement.h>

Classes

struct  Handle
 

Public Types

enum  Result {
  Result::kDone,
  Result::kRow,
  Result::kFailure
}
 

Public Member Functions

 ~ArchiveStatement ()
 
bool IsValid () const
 
Result Execute ()
 Execute the given statement with the provided data. More...
 
bool Reset ()
 All writes after the last successfull Run call are reset. Since statements are expensive to create, reset them for new writes instead of creating new statements. More...
 
bool WriteValue (size_t index, const std::string &item)
 
template<class T , class = std::enable_if<std::is_integral<T>::value>>
bool WriteValue (size_t index, T item)
 
bool WriteValue (size_t index, double item)
 
bool WriteValue (size_t index, const Allocation &item)
 
template<class T , class = std::enable_if<std::is_integral<T>::value>>
bool ReadValue (size_t index, T &item)
 
bool ReadValue (size_t index, double &item)
 
bool ReadValue (size_t index, std::string &item)
 
bool ReadValue (size_t index, Allocation &item)
 
size_t GetColumnCount ()
 

Friends

class ArchiveDatabase
 

Detailed Description

Represents a read/write query to an archive database. Statements are expensive to create and must be cached for as long as possible.

Definition at line 20 of file archive_statement.h.

Member Enumeration Documentation

◆ Result

Enumerator
kDone 

The statement is done executing.

kRow 

The statement found a row of information ready for reading.

kFailure 

Statement execution was a failure.

Definition at line 26 of file archive_statement.h.

26  {
27  //--------------------------------------------------------------------------
28  /// The statement is done executing.
29  ///
30  kDone,
31  //--------------------------------------------------------------------------
32  /// The statement found a row of information ready for reading.
33  ///
34  kRow,
35  //--------------------------------------------------------------------------
36  /// Statement execution was a failure.
37  ///
38  kFailure,
39  };

Constructor & Destructor Documentation

◆ ~ArchiveStatement()

impeller::ArchiveStatement::~ArchiveStatement ( )
default

Member Function Documentation

◆ Execute()

ArchiveStatement::Result impeller::ArchiveStatement::Execute ( )

Execute the given statement with the provided data.

Returns
Is the execution was succeessful.

Definition at line 217 of file archive_statement.cc.

217  {
218  if (!IsValid()) {
219  return Result::kFailure;
220  }
221  switch (::sqlite3_step(statement_handle_->Get())) {
222  case SQLITE_DONE:
223  return Result::kDone;
224  case SQLITE_ROW:
225  return Result::kRow;
226  default:
227  return Result::kFailure;
228  }
229 }

References IsValid(), kDone, kFailure, and kRow.

Referenced by impeller::ArchiveTransaction::~ArchiveTransaction().

◆ GetColumnCount()

size_t impeller::ArchiveStatement::GetColumnCount ( )

Definition at line 91 of file archive_statement.cc.

91  {
92  if (!IsValid()) {
93  return 0u;
94  }
95  return ::sqlite3_column_count(statement_handle_->Get());
96 }

References IsValid().

◆ IsValid()

bool impeller::ArchiveStatement::IsValid ( ) const

Definition at line 58 of file archive_statement.cc.

58  {
59  return statement_handle_ != nullptr;
60 }

Referenced by Execute(), GetColumnCount(), ReadValue(), Reset(), and WriteValue().

◆ ReadValue() [1/4]

bool impeller::ArchiveStatement::ReadValue ( size_t  index,
Allocation item 
)

Definition at line 190 of file archive_statement.cc.

190  {
191  if (!IsValid()) {
192  return false;
193  }
194  /*
195  * Get a blob pointer
196  */
197  auto blob = reinterpret_cast<const uint8_t*>(
198  ::sqlite3_column_blob(statement_handle_->Get(), ToColumn(index)));
199 
200  /*
201  * Decode the number of bytes in the blob
202  */
203  size_t byteSize =
204  ::sqlite3_column_bytes(statement_handle_->Get(), ToColumn(index));
205 
206  /*
207  * Reszie the host allocation and move the blob contents into it
208  */
209  if (!item.Truncate(byteSize, false /* npot */)) {
210  return false;
211  }
212 
213  memmove(item.GetBuffer(), blob, byteSize);
214  return true;
215 }

References impeller::Allocation::GetBuffer(), IsValid(), impeller::ToColumn(), and impeller::Allocation::Truncate().

◆ ReadValue() [2/4]

bool impeller::ArchiveStatement::ReadValue ( size_t  index,
double &  item 
)

Definition at line 152 of file archive_statement.cc.

152  {
153  if (!IsValid()) {
154  return false;
155  }
156  item = ::sqlite3_column_double(statement_handle_->Get(), ToColumn(index));
157  return true;
158 }

References IsValid(), and impeller::ToColumn().

◆ ReadValue() [3/4]

bool impeller::ArchiveStatement::ReadValue ( size_t  index,
std::string &  item 
)

Definition at line 168 of file archive_statement.cc.

168  {
169  if (!IsValid()) {
170  return false;
171  }
172  /*
173  * Get the character data
174  */
175  auto chars = reinterpret_cast<const char*>(
176  ::sqlite3_column_text(statement_handle_->Get(), ToColumn(index)));
177 
178  /*
179  * Get the length of the string (in bytes)
180  */
181  size_t textByteSize =
182  ::sqlite3_column_bytes(statement_handle_->Get(), ToColumn(index));
183 
184  std::string text(chars, textByteSize);
185  item.swap(text);
186 
187  return true;
188 }

References IsValid(), and impeller::ToColumn().

◆ ReadValue() [4/4]

template<class T , class = std::enable_if<std::is_integral<T>::value>>
bool impeller::ArchiveStatement::ReadValue ( size_t  index,
T &  item 
)
inline

Definition at line 69 of file archive_statement.h.

69  {
70  return ColumnIntegral(index, item);
71  }

Referenced by impeller::ArchiveLocation::Read().

◆ Reset()

bool impeller::ArchiveStatement::Reset ( )

All writes after the last successfull Run call are reset. Since statements are expensive to create, reset them for new writes instead of creating new statements.

Returns
If the statement writes were reset.

Definition at line 62 of file archive_statement.cc.

62  {
63  if (!IsValid()) {
64  return false;
65  }
66  if (::sqlite3_reset(statement_handle_->Get()) != SQLITE_OK) {
67  return false;
68  }
69 
70  if (::sqlite3_clear_bindings(statement_handle_->Get()) != SQLITE_OK) {
71  return false;
72  }
73 
74  return true;
75 }

References IsValid().

◆ WriteValue() [1/4]

bool impeller::ArchiveStatement::WriteValue ( size_t  index,
const Allocation item 
)

Definition at line 130 of file archive_statement.cc.

130  {
131  if (!IsValid()) {
132  return false;
133  }
134  return ::sqlite3_bind_blob(statement_handle_->Get(), //
135  ToParam(index), //
136  item.GetBuffer(), //
137  static_cast<int>(item.GetLength()), //
138  SQLITE_TRANSIENT) == SQLITE_OK;
139 }

References impeller::Allocation::GetBuffer(), impeller::Allocation::GetLength(), IsValid(), and impeller::ToParam().

◆ WriteValue() [2/4]

bool impeller::ArchiveStatement::WriteValue ( size_t  index,
const std::string &  item 
)

Definition at line 101 of file archive_statement.cc.

101  {
102  if (!IsValid()) {
103  return false;
104  }
105  return ::sqlite3_bind_text(statement_handle_->Get(), //
106  ToParam(index), //
107  item.data(), //
108  static_cast<int>(item.size()), //
109  SQLITE_TRANSIENT) == SQLITE_OK;
110 }

References IsValid(), and impeller::ToParam().

Referenced by impeller::ArchiveLocation::Write().

◆ WriteValue() [3/4]

bool impeller::ArchiveStatement::WriteValue ( size_t  index,
double  item 
)

Definition at line 121 of file archive_statement.cc.

121  {
122  if (!IsValid()) {
123  return false;
124  }
125  return ::sqlite3_bind_double(statement_handle_->Get(), //
126  ToParam(index), //
127  item) == SQLITE_OK;
128 }

References IsValid(), and impeller::ToParam().

◆ WriteValue() [4/4]

template<class T , class = std::enable_if<std::is_integral<T>::value>>
bool impeller::ArchiveStatement::WriteValue ( size_t  index,
item 
)
inline

Definition at line 60 of file archive_statement.h.

60  {
61  return BindIntegral(index, static_cast<int64_t>(item));
62  }

Friends And Related Function Documentation

◆ ArchiveDatabase

friend class ArchiveDatabase
friend

Definition at line 85 of file archive_statement.h.


The documentation for this class was generated from the following files:
impeller::ArchiveStatement::Result::kDone
@ kDone
impeller::ToColumn
static constexpr int ToColumn(size_t index)
Definition: archive_statement.cc:84
impeller::ToParam
static constexpr int ToParam(size_t index)
Definition: archive_statement.cc:77
impeller::ArchiveStatement::Result::kFailure
@ kFailure
impeller::ArchiveStatement::Result::kRow
@ kRow
impeller::ArchiveStatement::IsValid
bool IsValid() const
Definition: archive_statement.cc:58