sqlpp11gen is a c++ table class generator for sqlpp11.
There are two ways to use this gem. One way is to install it as a gem which needs to run in command line, the other one is to use the sourcce code in your rails app directly, which allows you to navigate in a client-side web browser.
Include the following line in the Gemfile of your Rails app.
gem "sqlpp11gen"
You can put it under the :development group.
And then run "bundle install" to install it.
(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!
Let's create a table named 'Product'. You can skip this step if there are some tables already available in your Rails project.
$ rails g scaffold Product name:string price:decimal
$ rake db:migrate
Start the server if you installed the source code of this gem.
$ rails s # optional, start the web server
Make sure the current directory is in the home of your Rails app.
- Run without any parameters to show usage message.
$ rails g sqlpp11gen:tab
Usage:
rails generate sqlpp11gen:tab NAME [OUTPUT_PATH] [FILENAME_PREFIX] [FILENAME_EXT] [options]
- The
NAME
is necessary, it's the table name you want to generate code from. OUTPUT_PATH
allows you to specify the output path for the generated code. The default path is 'app/cxxes'.FILENAME_PREFIX
allows you to append a filename prefix. The default one is 'tab_'.FILENAME_EXT
allows you to choose the filename ext. The default one is 'hpp'.
- Run with one parameter
$ rails g sqlpp11gen:tab product
created app/cxxes/tab_product.hpp
- Run with all parameters
$ rails g sqlpp11gen:tab product public/cxxes dom_ h
created public/cxxes/dom_product.h
Now, you can use the generated files.
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'.
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
Now, copy and paste the code!
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.
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.
The gem is available as open source under the terms of the MIT License.