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 215 of file archive_statement.cc.

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

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

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

◆ GetColumnCount()

size_t impeller::ArchiveStatement::GetColumnCount ( )

Definition at line 89 of file archive_statement.cc.

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

References IsValid().

◆ IsValid()

bool impeller::ArchiveStatement::IsValid ( ) const

Definition at line 56 of file archive_statement.cc.

56  {
57  return statement_handle_ != nullptr;
58 }

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

◆ ReadValue() [1/4]

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

Definition at line 188 of file archive_statement.cc.

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

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 150 of file archive_statement.cc.

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

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

◆ ReadValue() [3/4]

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

Definition at line 166 of file archive_statement.cc.

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

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 60 of file archive_statement.cc.

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

References IsValid().

◆ WriteValue() [1/4]

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

Definition at line 128 of file archive_statement.cc.

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

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 99 of file archive_statement.cc.

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

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 119 of file archive_statement.cc.

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

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:82
impeller::ToParam
static constexpr int ToParam(size_t index)
Definition: archive_statement.cc:75
impeller::ArchiveStatement::Result::kFailure
@ kFailure
impeller::ArchiveStatement::Result::kRow
@ kRow
impeller::ArchiveStatement::IsValid
bool IsValid() const
Definition: archive_statement.cc:56