jqGridPHP /examples/misc/test_data_generator.php

Language PHP Lines 222
MD5 Hash 6c7c5a4a09db34ffaa68d4fd7902dd5f Estimated Cost $4,074 (why?)
Repository git://github.com/wildraid/jqGridPHP.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

class test_data_generator
{
	protected $DB;
	
	public function __construct(jqGridLoader $loader)
	{
		$this->DB = $loader->loadDB();
	}

	public function run()
	{
		echo 'generating test data<br>';
		
		//-----------
		// Get essential data
		//-----------
		
		$first_names = array_map('trim', array_map('ucfirst', array_map('strtolower', file('misc/first_names.txt'))));
		$last_names  = array_map('trim', array_map('ucfirst', array_map('strtolower', file('misc/last_names.txt'))));

		$mail_suffix = array('@mail.ru', '@list.ru', '@gmail.com', '@yahoo.com', '@yandex.ru', '@inbox.ru');

		$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');

		//-----------
		// Create tables
		//-----------

		set_time_limit(60 * 10);

		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `lst_delivery_types` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `name` text NOT NULL,
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB;
		");

		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `tbl_books` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `name` varchar(100) NOT NULL,
			  `price` int(11) unsigned NOT NULL,
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB;
		");

		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `tbl_customer` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `first_name` text NOT NULL,
			  `last_name` text,
			  `email` text NOT NULL,
			  `phone` text,
			  `discount` decimal(4,2) NOT NULL DEFAULT '0.00',
			  `date_register` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
			  `date_birth` date DEFAULT NULL,
			  `contract_type` tinyint(4) unsigned NOT NULL,
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB;
		");

		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `tbl_order` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `customer_id` int(11) unsigned NOT NULL,
			  `date_create` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
			  `delivery_type` tinyint(4) unsigned NOT NULL,
			  `delivery_cost` int(11) unsigned NOT NULL DEFAULT '0',
			  `comment` text,
			  PRIMARY KEY (`id`),
			  KEY `customer_id` (`customer_id`)
			) ENGINE=InnoDB;
		");

		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `tbl_order_item` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `order_id` int(11) unsigned NOT NULL,
			  `book_id` int(11) unsigned NOT NULL,
			  `price` int(11) unsigned NOT NULL,
			  `quantity` tinyint(4) unsigned NOT NULL,
			  PRIMARY KEY (`id`),
			  KEY `order_id` (`order_id`),
			  KEY `book_id` (`book_id`)
			) ENGINE=InnoDB;
		");

		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `tbl_files` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `filename` text NOT NULL,
			  `size` int(11) unsigned NOT NULL,
			  `comment` text,
			  `version` int(11) NOT NULL DEFAULT '0',
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB;
		");
		
		$this->DB->query("
			CREATE TABLE IF NOT EXISTS `tbl_tree` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `parent_id` int(11) unsigned NOT NULL,
			  `node_name` text NOT NULL,
			  `price` int(11) unsigned NOT NULL,
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB;
		");

		//-----------
		// Truncate
		//-----------
		
		$this->DB->query('TRUNCATE TABLE lst_delivery_types');
		$this->DB->query('TRUNCATE TABLE tbl_books');
		$this->DB->query('TRUNCATE TABLE tbl_customer');
		$this->DB->query('TRUNCATE TABLE tbl_order');
		$this->DB->query('TRUNCATE TABLE tbl_order_item');
		//$this->DB->query('TRUNCATE TABLE tbl_files');
		$this->DB->query('TRUNCATE TABLE tbl_tree');


		//-----------
		// Generate data
		//-----------

		$this->DB->insert('lst_delivery_types', array('name' => 'Courier'));
		$this->DB->insert('lst_delivery_types', array('name' => 'Cash and carry'));
		$this->DB->insert('lst_delivery_types', array('name' => 'DHL'));

		#Generate books
		for($i=0;$i<=10000;$i++)
		{
			$book = array(
				'name' => $book_prefix[array_rand($book_prefix)] . ' ' . $first_names[array_rand($first_names)] . ' ' . $last_names[array_rand($last_names)],
				'price' => mt_rand(10, 3000),
			);

			$this->DB->insert('tbl_books', $book);
		}

		#Customers and order
		for($i=0;$i<1500;$i++)
		{
			#1. Generate customer
			$customer = array(
				'first_name' => $first_names[array_rand($first_names)],
				'last_name'  => $last_names[array_rand($last_names)],
				'phone'		 => mt_rand(1,7) . mt_rand(900, 925) . mt_rand(1000000, 9999999),
				'discount'	 => mt_rand(0, 30) / 100,
				'date_register' => date('Y-m-d H:i:s', mt_rand(strtotime('01.01.2011'), strtotime('10.06.2011'))),
				'date_birth' => date('Y-m-d', mt_rand(strtotime('01.01.1950'), strtotime('01.01.2000'))),
				'contract_type' => mt_rand(1,3),
			);

			$customer['email'] = strtolower($customer['first_name']) . mt_rand(10, 99) . $mail_suffix[array_rand($mail_suffix)];

			$customer_id = $this->DB->insert('tbl_customer', $customer, true);

			#2. Generate orders
			$order_cnt = mt_rand(-5, 7);

			if($order_cnt)
			{
				for($j=0;$j<$order_cnt;$j++)
				{
					$order = array(
						'customer_id' => $customer_id,
						'date_create' => date('Y-m-d H:i:s', mt_rand(strtotime('01.01.2011'), strtotime('10.06.2011'))),
						'delivery_type' => mt_rand(1,3),
						'delivery_cost' => mt_rand(0, 50),
					);

					$order_id = $this->DB->insert('tbl_order', $order, true);

					$item_cnt = mt_rand(1,5);

					for($u=0;$u<$item_cnt;$u++)
					{
						$item = array(
							'order_id' => $order_id,
							'book_id'  => mt_rand(1,10000),
							'price'    => mt_rand(10, 3000),
							'quantity' => mt_rand(1,10),
						);

						$this->DB->insert('tbl_order_item', $item);
					}
				}
			}
		}
		
		#tree
		$main_nodes = array(
			1 => 'Books',
			2 => 'Games',
			3 => 'Toys',
			4 => 'Cards',
			5 => 'Animals',
		);
		
		foreach($main_nodes as $k => $v)
		{
			$this->DB->insert('tbl_tree', array('node_name' => $v, 'parent_id' => 0, 'price' => mt_rand(30, 1000)));
		}
		
		for($i=6;$i<=500;$i++)
		{
			$ins = array(
				'parent_id' => mt_rand(1, $i),
				'node_name' => $book_prefix[array_rand($book_prefix)] . ' ' . $first_names[array_rand($first_names)],
				'price' => mt_rand(30, 1000),
			);
			
			$this->DB->insert('tbl_tree', $ins);
		}

		echo 'process complete!';
	}
}
Back to Top