fork download
  1. #include <array>
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. // #define DATA_COUNT 1000000
  6. #define DATA_COUNT 10
  7. #define COUNT_AS_NAME 3
  8.  
  9. const char name_list[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"};
  10. constexpr int name_list_size = (sizeof(name_list)/sizeof(name_list[0])-1);
  11. constexpr int name_size = std::log(DATA_COUNT)/std::log(name_list_size)+2;
  12.  
  13. class Data{
  14. unsigned int data_count_;
  15. unsigned int count_as_name_;
  16. unsigned int name_count_;
  17.  
  18. std::array<char,name_size> name;
  19. void set_name(){
  20. auto it = name.begin();
  21. unsigned int name_count=name_count_;
  22. bool is1st=true;
  23. do{
  24. if(!is1st) --name_count;
  25. unsigned int index=name_count % name_list_size;
  26. *it=name_list[index]; ++it;
  27. name_count -= index;
  28. name_count /= name_list_size;
  29. is1st=false;
  30. }while(name_count);
  31. *it='\0';
  32. }
  33. public:
  34. Data() : data_count_(DATA_COUNT),count_as_name_(COUNT_AS_NAME),name_count_(0) {
  35. set_name();
  36. }
  37. const char* const get_name() const {
  38. return name.cbegin();
  39. }
  40. const unsigned int get_count_as_name() const {
  41. return count_as_name_;
  42. }
  43. const bool is_end() const {
  44. return !data_count_;
  45. }
  46. void next() {
  47. --data_count_;
  48. --count_as_name_;
  49. if( !count_as_name_ ) {
  50. count_as_name_=COUNT_AS_NAME;
  51. ++name_count_;
  52. set_name();
  53. }
  54. }
  55. };
  56.  
  57. int main(){
  58. std::cout << "DROP TABLE IF EXISTS xxx;" << std::endl;
  59. std::cout << "CREATE TABLE xxx ( name text NOT NULL, datetime timestamp without time zone NOT NULL, comment text NOT NULL);" << std::endl;
  60. std::cout << "ALTER TABLE ONLY xxx ADD CONSTRAINT xxx_pkey PRIMARY KEY (name, datetime);" << std::endl;
  61.  
  62. for(Data data;!data.is_end();data.next()){
  63. 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;
  64. }
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
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');