---
title: llarp/peerstats/orm.hpp

---

# llarp/peerstats/orm.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[sqlite_orm](Namespaces/namespacesqlite__orm.md)** <br>"custom" types for [sqlite_orm]() reference: [https://github.com/fnc12/sqlite_orm/blob/master/examples/enum_binding.cpp](https://github.com/fnc12/sqlite_orm/blob/master/examples/enum_binding.cpp) |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[sqlite_orm::type_printer< llarp_time_t >](Classes/structsqlite__orm_1_1type__printer_3_01llarp__time__t_01_4.md)** <br>llarp_time_t serialization  |
| struct | **[sqlite_orm::statement_binder< llarp_time_t >](Classes/structsqlite__orm_1_1statement__binder_3_01llarp__time__t_01_4.md)**  |
| struct | **[sqlite_orm::field_printer< llarp_time_t >](Classes/structsqlite__orm_1_1field__printer_3_01llarp__time__t_01_4.md)**  |
| struct | **[sqlite_orm::row_extractor< llarp_time_t >](Classes/structsqlite__orm_1_1row__extractor_3_01llarp__time__t_01_4.md)**  |
| struct | **[sqlite_orm::type_printer< llarp::RouterID >](Classes/structsqlite__orm_1_1type__printer_3_01llarp_1_1RouterID_01_4.md)** <br>RouterID serialization.  |
| struct | **[sqlite_orm::statement_binder< llarp::RouterID >](Classes/structsqlite__orm_1_1statement__binder_3_01llarp_1_1RouterID_01_4.md)**  |
| struct | **[sqlite_orm::field_printer< llarp::RouterID >](Classes/structsqlite__orm_1_1field__printer_3_01llarp_1_1RouterID_01_4.md)**  |
| struct | **[sqlite_orm::row_extractor< llarp::RouterID >](Classes/structsqlite__orm_1_1row__extractor_3_01llarp_1_1RouterID_01_4.md)**  |




## Source code

```cpp
#pragma once

#include <sqlite_orm/sqlite_orm.h>

#include "types.hpp"


namespace llarp
{
  inline auto
  initStorage(const std::string& file)
  {
    using namespace sqlite_orm;
    return make_storage(
        file,
        make_table(
            "peerstats",
            make_column("routerId", &PeerStats::routerId, primary_key(), unique()),
            make_column("numConnectionAttempts", &PeerStats::numConnectionAttempts),
            make_column("numConnectionSuccesses", &PeerStats::numConnectionSuccesses),
            make_column("numConnectionRejections", &PeerStats::numConnectionRejections),
            make_column("numConnectionTimeouts", &PeerStats::numConnectionTimeouts),
            make_column("numPathBuilds", &PeerStats::numPathBuilds),
            make_column("numPacketsAttempted", &PeerStats::numPacketsAttempted),
            make_column("numPacketsSent", &PeerStats::numPacketsSent),
            make_column("numPacketsDropped", &PeerStats::numPacketsDropped),
            make_column("numPacketsResent", &PeerStats::numPacketsResent),
            make_column("numDistinctRCsReceived", &PeerStats::numDistinctRCsReceived),
            make_column("numLateRCs", &PeerStats::numLateRCs),
            make_column("peakBandwidthBytesPerSec", &PeerStats::peakBandwidthBytesPerSec),
            make_column("longestRCReceiveInterval", &PeerStats::longestRCReceiveInterval),
            make_column("leastRCRemainingLifetime", &PeerStats::leastRCRemainingLifetime)));
  }

  using PeerDbStorage = decltype(initStorage(""));

}  // namespace llarp

namespace sqlite_orm
{
  template <>
  struct type_printer<llarp_time_t> : public integer_printer
  {};

  template <>
  struct statement_binder<llarp_time_t>
  {
    int
    bind(sqlite3_stmt* stmt, int index, const llarp_time_t& value)
    {
      return statement_binder<int64_t>().bind(stmt, index, value.count());
    }
  };

  template <>
  struct field_printer<llarp_time_t>
  {
    std::string
    operator()(const llarp_time_t& value) const
    {
      return fmt::format("{}", value.count());
    }
  };

  template <>
  struct row_extractor<llarp_time_t>
  {
    llarp_time_t
    extract(const char* row_value)
    {
      int64_t raw = static_cast<int64_t>(atoi(row_value));
      return llarp_time_t(raw);
    }

    llarp_time_t
    extract(sqlite3_stmt* stmt, int columnIndex)
    {
      auto str = sqlite3_column_text(stmt, columnIndex);
      return this->extract((const char*)str);
    }
  };

  template <>
  struct type_printer<llarp::RouterID> : public text_printer
  {};

  template <>
  struct statement_binder<llarp::RouterID>
  {
    int
    bind(sqlite3_stmt* stmt, int index, const llarp::RouterID& value)
    {
      return statement_binder<std::string>().bind(stmt, index, value.ToString());
    }
  };

  template <>
  struct field_printer<llarp::RouterID>
  {
    std::string
    operator()(const llarp::RouterID& value) const
    {
      return value.ToString();
    }
  };

  template <>
  struct row_extractor<llarp::RouterID>
  {
    llarp::RouterID
    extract(const char* row_value)
    {
      llarp::RouterID id;
      if (not id.FromString(row_value))
        throw std::runtime_error("Invalid RouterID in sqlite3 database");

      return id;
    }

    llarp::RouterID
    extract(sqlite3_stmt* stmt, int columnIndex)
    {
      auto str = sqlite3_column_text(stmt, columnIndex);
      return this->extract((const char*)str);
    }
  };

}  // namespace sqlite_orm
```


-------------------------------

Updated on 2023-02-01 at 20:39:32 +0000
