PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/misc/test_data_generator.php

http://github.com/wildraid/jqGridPHP
PHP | 244 lines | 184 code | 41 blank | 19 comment | 5 complexity | 2ec567113ed1da28e91a9c76341e9bd8 MD5 | raw file
  1. <?php
  2. class test_data_generator
  3. {
  4. protected $DB;
  5. public function __construct(jqGridLoader $loader)
  6. {
  7. $this->DB = $loader->loadDB();
  8. }
  9. public function run()
  10. {
  11. echo 'generating test data<br>';
  12. //-----------
  13. // Get essential data
  14. //-----------
  15. $first_names = array_map('trim', array_map('ucfirst', array_map('strtolower', file('misc/first_names.txt'))));
  16. $last_names = array_map('trim', array_map('ucfirst', array_map('strtolower', file('misc/last_names.txt'))));
  17. $country_list = array_map('trim', file('misc/countries.txt'));
  18. $mail_suffix = array('@mail.ru', '@list.ru', '@gmail.com', '@yahoo.com', '@yandex.ru', '@inbox.ru');
  19. $book_prefix = array('Life of', 'Death of', 'Boots of', 'Smile at', 'Laugh at', 'Lurk behind', 'Attack of', 'Toy for', 'Database on', 'Postgresql for', 'Mysql on', 'PC under', 'Macs for', 'Unlimited', 'Pirates kills', 'Parrot behind', 'Virtual', 'Default', 'Rendering of', 'Cartoons about', 'PHP for', 'jQuery rulez on', 'Mail for', 'Dinosaur bites', 'Random name for', 'Scary spider', 'Blue dragon', 'Final fantasy of', 'Black magic for', 'Frogs dream about');
  20. //-----------
  21. // Create tables
  22. //-----------
  23. set_time_limit(60 * 10);
  24. $this->DB->query("
  25. CREATE TABLE IF NOT EXISTS `lst_delivery_types` (
  26. `id` int(11) NOT NULL AUTO_INCREMENT,
  27. `name` text NOT NULL,
  28. PRIMARY KEY (`id`)
  29. ) ENGINE=InnoDB;
  30. ");
  31. $this->DB->query("
  32. CREATE TABLE IF NOT EXISTS `tbl_books` (
  33. `id` int(11) NOT NULL AUTO_INCREMENT,
  34. `name` varchar(100) NOT NULL,
  35. `price` int(11) unsigned NOT NULL,
  36. PRIMARY KEY (`id`)
  37. ) ENGINE=InnoDB;
  38. ");
  39. $this->DB->query("
  40. CREATE TABLE IF NOT EXISTS `tbl_customer` (
  41. `id` int(11) NOT NULL AUTO_INCREMENT,
  42. `first_name` text NOT NULL,
  43. `last_name` text,
  44. `email` text NOT NULL,
  45. `phone` text,
  46. `discount` decimal(4,2) NOT NULL DEFAULT '0.00',
  47. `date_register` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  48. `date_birth` date DEFAULT NULL,
  49. `contract_type` tinyint(4) unsigned NOT NULL,
  50. PRIMARY KEY (`id`)
  51. ) ENGINE=InnoDB;
  52. ");
  53. $this->DB->query("
  54. CREATE TABLE IF NOT EXISTS `tbl_order` (
  55. `id` int(11) NOT NULL AUTO_INCREMENT,
  56. `customer_id` int(11) unsigned NOT NULL,
  57. `date_create` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  58. `delivery_type` tinyint(4) unsigned NOT NULL,
  59. `delivery_cost` int(11) unsigned NOT NULL DEFAULT '0',
  60. `comment` text,
  61. PRIMARY KEY (`id`),
  62. KEY `customer_id` (`customer_id`)
  63. ) ENGINE=InnoDB;
  64. ");
  65. $this->DB->query("
  66. CREATE TABLE IF NOT EXISTS `tbl_order_item` (
  67. `id` int(11) NOT NULL AUTO_INCREMENT,
  68. `order_id` int(11) unsigned NOT NULL,
  69. `book_id` int(11) unsigned NOT NULL,
  70. `price` int(11) unsigned NOT NULL,
  71. `quantity` tinyint(4) unsigned NOT NULL,
  72. PRIMARY KEY (`id`),
  73. KEY `order_id` (`order_id`),
  74. KEY `book_id` (`book_id`)
  75. ) ENGINE=InnoDB;
  76. ");
  77. $this->DB->query("
  78. CREATE TABLE IF NOT EXISTS `tbl_files` (
  79. `id` int(11) NOT NULL AUTO_INCREMENT,
  80. `filename` text NOT NULL,
  81. `size` int(11) unsigned NOT NULL,
  82. `comment` text,
  83. `version` int(11) NOT NULL DEFAULT '0',
  84. PRIMARY KEY (`id`)
  85. ) ENGINE=InnoDB;
  86. ");
  87. $this->DB->query("
  88. CREATE TABLE IF NOT EXISTS `tbl_tree` (
  89. `id` int(11) NOT NULL AUTO_INCREMENT,
  90. `parent_id` int(11) unsigned NOT NULL,
  91. `node_name` text NOT NULL,
  92. `price` int(11) unsigned NOT NULL,
  93. PRIMARY KEY (`id`)
  94. ) ENGINE=InnoDB;
  95. ");
  96. $this->DB->query("
  97. CREATE TABLE IF NOT EXISTS `bnd_customer_country` (
  98. `customer_id` int(11) NOT NULL,
  99. `country_name` varchar(255) NOT NULL,
  100. `value` int(11) NOT NULL,
  101. PRIMARY KEY (`customer_id`, `country_name`)
  102. ) ENGINE=InnoDB;
  103. ");
  104. //-----------
  105. // Truncate
  106. //-----------
  107. $this->DB->query('TRUNCATE TABLE lst_delivery_types');
  108. $this->DB->query('TRUNCATE TABLE tbl_books');
  109. $this->DB->query('TRUNCATE TABLE tbl_customer');
  110. $this->DB->query('TRUNCATE TABLE tbl_order');
  111. $this->DB->query('TRUNCATE TABLE tbl_order_item');
  112. //$this->DB->query('TRUNCATE TABLE tbl_files');
  113. $this->DB->query('TRUNCATE TABLE tbl_tree');
  114. $this->DB->query('TRUNCATE TABLE bnd_customer_country');
  115. //-----------
  116. // Generate data
  117. //-----------
  118. $this->DB->insert('lst_delivery_types', array('name' => 'Courier'));
  119. $this->DB->insert('lst_delivery_types', array('name' => 'Cash and carry'));
  120. $this->DB->insert('lst_delivery_types', array('name' => 'DHL'));
  121. #Generate books
  122. for($i = 0; $i <= 10000; $i++)
  123. {
  124. $book = array(
  125. 'name' => $book_prefix[array_rand($book_prefix)] . ' ' . $first_names[array_rand($first_names)] . ' ' . $last_names[array_rand($last_names)],
  126. 'price' => mt_rand(10, 3000),
  127. );
  128. $this->DB->insert('tbl_books', $book);
  129. }
  130. #Customers and order
  131. for($i = 0; $i < 1500; $i++)
  132. {
  133. #1. Generate customer
  134. $customer = array(
  135. 'first_name' => $first_names[array_rand($first_names)],
  136. 'last_name' => $last_names[array_rand($last_names)],
  137. 'phone' => mt_rand(1, 7) . mt_rand(900, 925) . mt_rand(1000000, 9999999),
  138. 'discount' => mt_rand(0, 30) / 100,
  139. 'date_register' => date('Y-m-d H:i:s', mt_rand(strtotime('01.01.2011'), strtotime('10.06.2011'))),
  140. 'date_birth' => date('Y-m-d', mt_rand(strtotime('01.01.1950'), strtotime('01.01.2000'))),
  141. 'contract_type' => mt_rand(1, 3),
  142. );
  143. $customer['email'] = strtolower($customer['first_name']) . mt_rand(10, 99) . $mail_suffix[array_rand($mail_suffix)];
  144. $customer_id = $this->DB->insert('tbl_customer', $customer, true);
  145. #2. Generate orders
  146. $order_cnt = mt_rand(2, 5);
  147. for($j = 0; $j < $order_cnt; $j++)
  148. {
  149. $order = array(
  150. 'customer_id' => $customer_id,
  151. 'date_create' => date('Y-m-d H:i:s', mt_rand(strtotime('01.01.2011'), strtotime('10.06.2011'))),
  152. 'delivery_type' => mt_rand(1, 3),
  153. 'delivery_cost' => mt_rand(0, 50),
  154. );
  155. $order_id = $this->DB->insert('tbl_order', $order, true);
  156. $item_cnt = mt_rand(1, 5);
  157. for($u = 0; $u < $item_cnt; $u++)
  158. {
  159. $item = array(
  160. 'order_id' => $order_id,
  161. 'book_id' => mt_rand(1, 10000),
  162. 'price' => mt_rand(10, 3000),
  163. 'quantity' => mt_rand(1, 10),
  164. );
  165. $this->DB->insert('tbl_order_item', $item);
  166. }
  167. }
  168. #3. Generate multi-col primary key
  169. $country_cnt = mt_rand(2, 5);
  170. foreach(array_rand($country_list, $country_cnt) as $k)
  171. {
  172. $ins = array(
  173. 'customer_id' => $customer_id,
  174. 'country_name' => $country_list[$k],
  175. 'value' => mt_rand(1, 100),
  176. );
  177. $this->DB->insert('bnd_customer_country', $ins);
  178. }
  179. }
  180. #tree
  181. $main_nodes = array(
  182. 1 => 'Books',
  183. 2 => 'Games',
  184. 3 => 'Toys',
  185. 4 => 'Cards',
  186. 5 => 'Animals',
  187. );
  188. foreach($main_nodes as $k => $v)
  189. {
  190. $this->DB->insert('tbl_tree', array('node_name' => $v, 'parent_id' => 0, 'price' => mt_rand(30, 1000)));
  191. }
  192. for($i = 6; $i <= 500; $i++)
  193. {
  194. $ins = array(
  195. 'parent_id' => mt_rand(1, $i),
  196. 'node_name' => $book_prefix[array_rand($book_prefix)] . ' ' . $first_names[array_rand($first_names)],
  197. 'price' => mt_rand(30, 1000),
  198. );
  199. $this->DB->insert('tbl_tree', $ins);
  200. }
  201. echo 'process complete!';
  202. }
  203. }