#include <array> #include <cmath> #include <iostream> // #define DATA_COUNT 1000000 #define DATA_COUNT 10 #define COUNT_AS_NAME 3 const char name_list[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}; constexpr int name_list_size = (sizeof(name_list)/sizeof(name_list[0])-1); constexpr int name_size = std::log(DATA_COUNT)/std::log(name_list_size)+2; class Data{ unsigned int data_count_; unsigned int count_as_name_; unsigned int name_count_; std::array<char,name_size> name; void set_name(){ auto it = name.begin(); unsigned int name_count=name_count_; bool is1st=true; do{ if(!is1st) --name_count; unsigned int index=name_count % name_list_size; *it=name_list[index]; ++it; name_count -= index; name_count /= name_list_size; is1st=false; }while(name_count); *it='\0'; } public: Data() : data_count_(DATA_COUNT),count_as_name_(COUNT_AS_NAME),name_count_(0) { set_name(); } const char* const get_name() const { return name.cbegin(); } const unsigned int get_count_as_name() const { return count_as_name_; } const bool is_end() const { return !data_count_; } void next() { --data_count_; --count_as_name_; if( !count_as_name_ ) { count_as_name_=COUNT_AS_NAME; ++name_count_; set_name(); } } }; int main(){ std::cout << "DROP TABLE IF EXISTS xxx;" << std::endl; std::cout << "CREATE TABLE xxx ( name text NOT NULL, datetime timestamp without time zone NOT NULL, comment text NOT NULL);" << std::endl; std::cout << "ALTER TABLE ONLY xxx ADD CONSTRAINT xxx_pkey PRIMARY KEY (name, datetime);" << std::endl; for(Data data;!data.is_end();data.next()){ std::cout << "INSERT INTO xxx(name,datetime,comment) VALUES ('" << data.get_name() << "','2019/01/01'::date+" << data.get_count_as_name() << ",'"<< data.get_name()<< data.get_count_as_name() << "');" << std::endl; } return 0; }
Standard input is empty
DROP TABLE IF EXISTS xxx; CREATE TABLE xxx ( name text NOT NULL, datetime timestamp without time zone NOT NULL, comment text NOT NULL); ALTER TABLE ONLY xxx ADD CONSTRAINT xxx_pkey PRIMARY KEY (name, datetime); INSERT INTO xxx(name,datetime,comment) VALUES ('a','2019/01/01'::date+3,'a3'); INSERT INTO xxx(name,datetime,comment) VALUES ('a','2019/01/01'::date+2,'a2'); INSERT INTO xxx(name,datetime,comment) VALUES ('a','2019/01/01'::date+1,'a1'); INSERT INTO xxx(name,datetime,comment) VALUES ('b','2019/01/01'::date+3,'b3'); INSERT INTO xxx(name,datetime,comment) VALUES ('b','2019/01/01'::date+2,'b2'); INSERT INTO xxx(name,datetime,comment) VALUES ('b','2019/01/01'::date+1,'b1'); INSERT INTO xxx(name,datetime,comment) VALUES ('c','2019/01/01'::date+3,'c3'); INSERT INTO xxx(name,datetime,comment) VALUES ('c','2019/01/01'::date+2,'c2'); INSERT INTO xxx(name,datetime,comment) VALUES ('c','2019/01/01'::date+1,'c1'); INSERT INTO xxx(name,datetime,comment) VALUES ('d','2019/01/01'::date+3,'d3');