Sqlpp11gen

sqlpp11gen is a c++ table class generator for sqlpp11.

NOTE: This gem is not published yet, but you can install it manually. Someday I will fix it. If you're familiar with ruby gems, you can help fix it.

Installation

Manual install

(1) downlod the source code and copy app folder to the app folder of your Rails project. Make sure the following files exist in your project:

app/controllers/cxxes_controller.rb
app/helpers/cxxes_helper.rb
app/views/cxxes/index.h.erb

(2) add a route in config/routes.rb

# config/routes.rb
resources :cxxes

(3) register a new MIME type in config/initializers/mime_types.rb

# config/initializers/mime_types.rb
Mime::Type.register "text/plain", :h

That's it! Now ignite your rails app!

Usage

Assume that a 'users' table already exists, browse 'http://localhost:3000/cxxes.h'.

If not, browse 'http://localhost:3000/cxxes.h?model=YourModelName'. e.g. for Product model, browse 'http://localhost:3000/cxxes.h?model=Product'.

Step by step

create your table

Let's create a table named 'Product'.

$ rails g scaffold Product name:string price:decimal
$ rake db:migrate
$ rails s 

browse

Browse 'http://localhost:3000/cxxes.h?model=Product'. The output will be:

// WARNING; don't change this file manually. This file is generated by sqlpp11gen!
#ifndef SQLPP_TAB_PRODUCT_H
#define SQLPP_TAB_PRODUCT_H

#include <sqlpp11/table.h>
#include <sqlpp11/char_sequence.h>
#include <sqlpp11/column_types.h>

//
// table: Product
// fields: 5
//   id                             integer    false      integer
//   name                           string     true       varchar
//   price                          decimal    true       decimal
//   created_at                     datetime   false      datetime
//   updated_at                     datetime   false      datetime
//

namespace TabProduct_ {
  struct Id {
    struct _alias_t {
      static constexpr const char _literal[] = "id";
      using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
      template <typename T>
      struct _member_t {
        T id;
        T& operator()() { return id; }
        const T& operator()() const { return id; }
      };
    };
    using _traits = sqlpp::make_traits<sqlpp::integer>;
  };
  struct Name {
    struct _alias_t {
      static constexpr const char _literal[] = "name";
      using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
      template <typename T>
      struct _member_t {
        T name;
        T& operator()() { return name; }
        const T& operator()() const { return name; }
      };
    };
    using _traits = sqlpp::make_traits<sqlpp::varchar, sqlpp::tag::can_be_null>;
  };
  struct Price {
    struct _alias_t {
      static constexpr const char _literal[] = "price";
      using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
      template <typename T>
      struct _member_t {
        T price;
        T& operator()() { return price; }
        const T& operator()() const { return price; }
      };
    };
    using _traits = sqlpp::make_traits<sqlpp::floating_point, sqlpp::tag::can_be_null>;
  };
  struct CreatedAt {
    struct _alias_t {
      static constexpr const char _literal[] = "created_at";
      using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
      template <typename T>
      struct _member_t {
        T created_at;
        T& operator()() { return created_at; }
        const T& operator()() const { return created_at; }
      };
    };
    using _traits = sqlpp::make_traits<sqlpp::time_point>;
  };
  struct UpdatedAt {
    struct _alias_t {
      static constexpr const char _literal[] = "updated_at";
      using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
      template <typename T>
      struct _member_t {
        T updated_at;
        T& operator()() { return updated_at; }
        const T& operator()() const { return updated_at; }
      };
    };
    using _traits = sqlpp::make_traits<sqlpp::time_point>;
  };
} // namespace TabProduct_

struct TabProduct : sqlpp::table_t<TabProduct
       , TabProduct_::Id 
       , TabProduct_::Name 
       , TabProduct_::Price 
       , TabProduct_::CreatedAt 
       , TabProduct_::UpdatedAt 
     >
{
  using _value_type = sqlpp::no_value_t;
  struct _alias_t {
    static constexpr const char _literal[] = "products";
    using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
    template <typename T>
    struct _member_t {
      T tabProduct;
      T& operator()() { return tabProduct; }
      const T& operator()() const { return tabProduct; }
    };
  };
};

struct ProductData {
  ProductData() : id(0), price(0.0f) {}
  std::int64_t                     id                       ; // false      integer
  std::string                      name                     ; // true       varchar
  double                           price                    ; // true       decimal
  sqlpp::chrono::microsecond_point created_at               ; // false      datetime
  sqlpp::chrono::microsecond_point updated_at               ; // false      datetime
};

#endif // SQLPP_TAB_PRODUCT_H

copy and paste the code!

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sqlpp11gen. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.