PageRenderTime 74ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/tpccbench/TPCCDatabaseGenerator/TPCCGenData.cs

#
C# | 648 lines | 420 code | 83 blank | 145 comment | 31 complexity | 0712e2c0fca66787b6bc9b093293aaea MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using SharpNeatLib.Maths;
  5. /// <summary>
  6. /// TPCCGenData is the container class for the routines that generate and load the actual data into the tables.
  7. /// </summary>
  8. public class TPCCGenData
  9. {
  10. private static readonly FastRandom Frnd = new FastRandom();
  11. #pragma warning disable 649
  12. /*
  13. public static readonly object Locker;
  14. */
  15. #pragma warning restore 649
  16. private static readonly String RandHold = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  17. private static readonly String ZipRandHold = "1234567890";
  18. public int MaxNumWh;
  19. public int NumWh;
  20. public string Sqlconn;
  21. /// <summary>
  22. /// /************************************************************************************************
  23. /// Populate CUSTOMER table
  24. /// By Wesley D. Brown
  25. /// Date 11/24/2007
  26. /// Mod
  27. /// **Description**
  28. /// Functions:
  29. /// **End Discription**
  30. /// **Change Log**
  31. /// **End Change Log**
  32. /// ************************************************************************************************/
  33. /// </summary>
  34. /// <returns></returns>
  35. public void BuildCustomerTable()
  36. {
  37. var currdate = new DateTime();
  38. var dt = new DataTable();
  39. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  40. sqlConnect.Open();
  41. var dc = new SqlCommand("select top 0 * from dbo.CUSTOMER", sqlConnect) {CommandType = CommandType.Text};
  42. var da = new SqlDataAdapter(dc);
  43. da.FillSchema(dt, SchemaType.Mapped);
  44. sqlConnect.Close();
  45. sqlConnect.Dispose();
  46. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.CUSTOMER", BatchSize = 10000};
  47. int i = NumWh;
  48. int tid = (NumWh*10*3000) + 1;
  49. while (i < MaxNumWh + 1)
  50. {
  51. for (int id = 1; id < 11; id++)
  52. {
  53. for (int cid = 1; cid < 3001; cid++)
  54. {
  55. DataRow dr = dt.NewRow();
  56. dr["C_ID"] = "C_W" + i + "_D" + id + "_" + cid;
  57. dr["C_D_ID"] = "D_W" + i + "_" + id;
  58. dr["C_W_ID"] = "W_" + i;
  59. dr["C_FIRST"] = RandomString(5, 16);
  60. dr["C_MIDDLE"] = "oe";
  61. dr["C_LAST"] = RandomString(8, 16);
  62. dr["C_STREET_1"] = RandomString(10, 20);
  63. dr["C_STREET_2"] = RandomString(10, 20);
  64. dr["C_CITY"] = RandomString(10, 20);
  65. dr["C_STATE"] = RandomString(2, 2);
  66. dr["C_ZIP"] = RandZip();
  67. dr["C_PHONE"] = RandomString(12, 12);
  68. dr["C_SINCE"] = currdate.ToLongTimeString();
  69. dr["C_CREDIT"] = "GC";
  70. dr["C_CREDIT_LIM"] = 5000;
  71. dr["C_DISCOUNT"] = .5;
  72. dr["C_BALANCE"] = -10.00;
  73. dr["C_YTD_PAYMENT"] = 10.00;
  74. dr["C_PAYMENT_CNT"] = 1;
  75. dr["C_DELIVERY_CNT"] = 0;
  76. dr["C_DATA"] = RandomString(300, 500);
  77. dr["SEQ_ID"] = tid;
  78. dt.Rows.Add(dr);
  79. }
  80. }
  81. bulkCopy.WriteToServer(dt);
  82. dt.Clear();
  83. i++;
  84. }
  85. return;
  86. }
  87. /// <summary>
  88. /// /************************************************************************************************
  89. /// Populate DISTRICT table
  90. /// By Wesley D. Brown
  91. /// Date 11/24/2007
  92. /// Mod
  93. /// **Description**
  94. /// Functions:
  95. /// **End Discription**
  96. /// **Change Log**
  97. /// **End Change Log**
  98. /// ************************************************************************************************/
  99. /// </summary>
  100. /// <returns></returns>
  101. public void BuildDistrictTable()
  102. {
  103. var dt = new DataTable();
  104. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  105. sqlConnect.Open();
  106. var dc = new SqlCommand("select top 0 * from dbo.DISTRICT", sqlConnect) {CommandType = CommandType.Text};
  107. var da = new SqlDataAdapter(dc);
  108. da.FillSchema(dt, SchemaType.Mapped);
  109. sqlConnect.Close();
  110. sqlConnect.Dispose();
  111. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.DISTRICT", BatchSize = 10000};
  112. //,SqlBulkCopyOptions.TableLock);
  113. int i = 1;
  114. int tid = 1;
  115. while (i < MaxNumWh + 1)
  116. {
  117. for (int id = 1; id < 11; id++)
  118. {
  119. DataRow dr = dt.NewRow();
  120. dr["D_ID"] = "D_W" + i + "_" + id;
  121. dr["D_W_ID"] = "W_" + i;
  122. dr["D_NAME"] = RandomString(6, 10);
  123. dr["D_STREET_1"] = RandomString(10, 20);
  124. dr["D_STREET_2"] = RandomString(10, 20);
  125. dr["D_CITY"] = RandomString(10, 20);
  126. dr["D_STATE"] = RandomString(2, 2);
  127. dr["D_ZIP"] = RandZip();
  128. dr["D_TAX"] = 0.1000;
  129. dr["D_YTD"] = 30000;
  130. dr["D_NEXT_O_ID"] = "3001";
  131. dr["SEQ_ID"] = tid;
  132. dt.Rows.Add(dr);
  133. tid++;
  134. }
  135. bulkCopy.WriteToServer(dt);
  136. dt.Clear();
  137. i++;
  138. }
  139. return;
  140. }
  141. /// <summary>
  142. /// /************************************************************************************************
  143. /// Populate HISTORY table
  144. /// By Wesley D. Brown
  145. /// Date 11/24/2007
  146. /// Mod
  147. /// **Description**
  148. /// Functions:
  149. /// **End Discription**
  150. /// **Change Log**
  151. /// **End Change Log**
  152. /// ************************************************************************************************/
  153. /// </summary>
  154. /// <returns></returns>
  155. public void BuildHistoryTable()
  156. {
  157. var currdate = new DateTime();
  158. var dt = new DataTable();
  159. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  160. sqlConnect.Open();
  161. var dc = new SqlCommand("select top 0 * from dbo.HISTORY", sqlConnect) {CommandType = CommandType.Text};
  162. var da = new SqlDataAdapter(dc);
  163. da.FillSchema(dt, SchemaType.Mapped);
  164. sqlConnect.Close();
  165. sqlConnect.Dispose();
  166. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.HISTORY", BatchSize = 10000};
  167. int i = NumWh;
  168. int tid = (NumWh*10*3000) + 1;
  169. while (i < MaxNumWh + 1)
  170. {
  171. for (int id = 1; id < 11; id++) //check to see if ID should be 11 and not 10
  172. {
  173. for (int cid = 1; cid < 3001; cid++)
  174. {
  175. DataRow dr = dt.NewRow();
  176. dr["H_C_ID"] = "C_W" + i + "_D" + id + "_" + cid;
  177. dr["H_C_D_ID"] = "D_W" + i + "_" + id;
  178. dr["H_C_W_ID"] = "W_" + i;
  179. dr["H_D_ID"] = "D_W" + i + "_" + id;
  180. dr["H_W_ID"] = "W_" + i;
  181. dr["H_DATE"] = currdate.ToLongTimeString();
  182. dr["H_AMOUNT"] = 10.00;
  183. dr["H_DATA"] = RandomString(12, 24);
  184. dr["SEQ_ID"] = tid;
  185. dt.Rows.Add(dr);
  186. tid++;
  187. }
  188. }
  189. bulkCopy.WriteToServer(dt);
  190. dt.Clear();
  191. i++;
  192. }
  193. return;
  194. }
  195. /// <summary>
  196. /// /************************************************************************************************
  197. /// Populate ITEM table
  198. /// By Wesley D. Brown
  199. /// Date 11/24/2007
  200. /// Mod
  201. /// **Description**
  202. /// Functions:
  203. /// **End Discription**
  204. /// **Change Log**
  205. /// **End Change Log**
  206. /// ************************************************************************************************/
  207. /// </summary>
  208. /// <returns></returns>
  209. public void BuildItemsTable()
  210. {
  211. var dt = new DataTable();
  212. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  213. sqlConnect.Open();
  214. var dc = new SqlCommand("select top 0 * from dbo.ITEM", sqlConnect) {CommandType = CommandType.Text};
  215. var da = new SqlDataAdapter(dc);
  216. da.FillSchema(dt, SchemaType.Mapped);
  217. sqlConnect.Close();
  218. sqlConnect.Dispose();
  219. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.ITEM", BatchSize = 10000}; //,SqlBulkCopyOptions.TableLock);
  220. int i = 1;
  221. while (i < 100001)
  222. {
  223. DataRow dr = dt.NewRow();
  224. dr["I_ID"] = i;
  225. dr["I_IM_ID"] = RandomString(10, 20);
  226. dr["I_NAME"] = RandomString(14, 24);
  227. dr["I_PRICE"] = Frnd.Next(1, 100);
  228. dr["I_DATA"] = RandomString(26, 50);
  229. i++;
  230. dt.Rows.Add(dr);
  231. }
  232. bulkCopy.WriteToServer(dt);
  233. return;
  234. }
  235. /// <summary>
  236. /// /************************************************************************************************
  237. /// Populate NEW_ORDER table
  238. /// By Wesley D. Brown
  239. /// Date 11/24/2007
  240. /// Mod
  241. /// **Description**
  242. /// Functions:
  243. /// **End Discription**
  244. /// **Change Log**
  245. /// **End Change Log**
  246. /// ************************************************************************************************/
  247. /// </summary>
  248. /// <returns></returns>
  249. public void BuildNewOrderTable()
  250. {
  251. var dt = new DataTable();
  252. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  253. sqlConnect.Open();
  254. var dc = new SqlCommand("select top 0 * from dbo.NEW_ORDER", sqlConnect) {CommandType = CommandType.Text};
  255. var da = new SqlDataAdapter(dc);
  256. da.FillSchema(dt, SchemaType.Mapped);
  257. sqlConnect.Close();
  258. sqlConnect.Dispose();
  259. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.NEW_ORDER", BatchSize = 10000};
  260. int i = NumWh;
  261. int tid;
  262. int tolid;
  263. if (NumWh == 1)
  264. {
  265. tid = 1;
  266. tolid = 1;
  267. }
  268. else
  269. {
  270. tid = (NumWh*10*3000) + 1;
  271. tolid = (NumWh*10*3000*10) + 1;
  272. }
  273. while (i < MaxNumWh + 1)
  274. {
  275. for (int id = 1; id < 11; id++)
  276. {
  277. for (int cid = 1; cid < 3001; cid++)
  278. {
  279. if (cid < 901)
  280. {
  281. DataRow dr = dt.NewRow();
  282. dr["NO_O_ID"] = tid;
  283. dr["NO_D_ID"] = "D_W" + i + "_" + id;
  284. dr["NO_W_ID"] = "W_" + i;
  285. dr["SEQ_ID"] = tolid;
  286. tolid++;
  287. dt.Rows.Add(dr);
  288. }
  289. tid++;
  290. }
  291. }
  292. bulkCopy.WriteToServer(dt);
  293. dt.Clear();
  294. i++;
  295. }
  296. return;
  297. }
  298. /// <summary>
  299. /// /************************************************************************************************
  300. /// Populate O_ORDER table
  301. /// By Wesley D. Brown
  302. /// Date 11/24/2007
  303. /// Mod
  304. /// **Description**
  305. /// Functions:
  306. /// **End Discription**
  307. /// **Change Log**
  308. /// **End Change Log**
  309. /// ************************************************************************************************/
  310. /// </summary>
  311. /// <returns></returns>
  312. public void BuildOrderTable()
  313. {
  314. var currdate = new DateTime();
  315. var dt = new DataTable();
  316. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  317. sqlConnect.Open();
  318. var dc = new SqlCommand("select top 0 * from dbo.O_ORDER", sqlConnect) {CommandType = CommandType.Text};
  319. var da = new SqlDataAdapter(dc);
  320. da.FillSchema(dt, SchemaType.Mapped);
  321. sqlConnect.Close();
  322. sqlConnect.Dispose();
  323. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.O_ORDER", BatchSize = 10000};
  324. int i = NumWh;
  325. int tid;
  326. if (NumWh == 1)
  327. {
  328. tid = 1;
  329. }
  330. else
  331. {
  332. tid = (NumWh*10*3000) + 1;
  333. }
  334. while (i < MaxNumWh + 1)
  335. {
  336. for (int id = 1; id < 11; id++)
  337. {
  338. for (int cid = 1; cid < 3001; cid++)
  339. {
  340. DataRow dr = dt.NewRow();
  341. dr["O_ID"] = tid;
  342. dr["O_D_ID"] = "D_W" + i + "_" + id;
  343. dr["O_W_ID"] = "W_" + i;
  344. dr["O_C_ID"] = "C_W" + i + "_D" + id + "_" + cid;
  345. dr["O_ENTRY_D"] = currdate.ToLongTimeString();
  346. dr["O_CARRIER_ID"] = "";
  347. dr["O_OL_CNT"] = Frnd.Next(5, 15);
  348. dr["O_ALL_LOCAL"] = "1";
  349. dr["SEQ_ID"] = tid;
  350. dr["SOURCE_TIME"] = currdate.ToLongTimeString();
  351. tid++;
  352. dt.Rows.Add(dr);
  353. }
  354. }
  355. bulkCopy.WriteToServer(dt);
  356. dt.Clear();
  357. i++;
  358. }
  359. return;
  360. }
  361. /// <summary>
  362. /// /************************************************************************************************
  363. /// Populate ORDER_LINE table
  364. /// By Wesley D. Brown
  365. /// Date 11/24/2007
  366. /// Mod
  367. /// **Description**
  368. /// Functions:
  369. /// **End Discription**
  370. /// **Change Log**
  371. /// **End Change Log**
  372. /// ************************************************************************************************/
  373. /// </summary>
  374. /// <returns></returns>
  375. public void BuildOrderLineTable()
  376. {
  377. var dt = new DataTable();
  378. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  379. sqlConnect.Open();
  380. var dc = new SqlCommand("select top 0 * from dbo.ORDER_LINE", sqlConnect) {CommandType = CommandType.Text};
  381. var da = new SqlDataAdapter(dc);
  382. da.FillSchema(dt, SchemaType.Mapped);
  383. sqlConnect.Close();
  384. sqlConnect.Dispose();
  385. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.ORDER_LINE", BatchSize = 10000};
  386. int i = NumWh;
  387. int tid;
  388. int tolid;
  389. if (NumWh == 1)
  390. {
  391. tid = 1;
  392. tolid = 1;
  393. }
  394. else
  395. {
  396. tid = (NumWh*10*3000) + 1;
  397. tolid = (NumWh*10*3000*10) + 1;
  398. }
  399. while (i < MaxNumWh + 1)
  400. {
  401. for (int id = 1; id < 11; id++)
  402. {
  403. for (int cid = 1; cid < 3001; cid++)
  404. {
  405. for (int olid = 1; olid < 11; olid++)
  406. {
  407. DataRow dr = dt.NewRow();
  408. dr["OL_O_ID"] = tid;
  409. dr["OL_D_ID"] = "D_W" + i + "_" + id;
  410. dr["OL_W_ID"] = "W_" + i;
  411. dr["OL_NUMBER"] = "OL_NUM_" + tolid;
  412. dr["OL_I_ID"] = Frnd.Next(1, 100000);
  413. dr["OL_SUPPLY_W_ID"] = "W_" + i;
  414. dr["OL_DELIVERY_D"] = "01/01/1900 00:00:00.000"; // SqlDateTime.Null;// sqldatenull;
  415. dr["OL_QUANTITY"] = 5;
  416. dr["OL_AMOUNT"] = Frnd.Next(1, 9999);
  417. dr["OL_DIST_INFO"] = RandomString(24, 24);
  418. dr["SEQ_ID"] = tolid;
  419. tolid++;
  420. dt.Rows.Add(dr);
  421. }
  422. tid++;
  423. }
  424. }
  425. bulkCopy.WriteToServer(dt);
  426. dt.Clear();
  427. i++;
  428. }
  429. return;
  430. }
  431. /// <summary>
  432. /// /************************************************************************************************
  433. /// Populate STOCK table
  434. /// By Wesley D. Brown
  435. /// Date 11/24/2007
  436. /// Mod
  437. /// **Description**
  438. /// Functions:
  439. /// **End Discription**
  440. /// **Change Log**
  441. /// **End Change Log**
  442. /// ************************************************************************************************/
  443. /// </summary>
  444. /// <returns></returns>
  445. public void BuildStockTable()
  446. {
  447. var dt = new DataTable();
  448. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  449. sqlConnect.Open();
  450. var dc = new SqlCommand("select top 0 * from dbo.STOCK", sqlConnect) {CommandType = CommandType.Text};
  451. var da = new SqlDataAdapter(dc);
  452. da.FillSchema(dt, SchemaType.Mapped);
  453. sqlConnect.Close();
  454. sqlConnect.Dispose();
  455. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.STOCK", BatchSize = 10000};
  456. int i = NumWh;
  457. while (i < MaxNumWh + 1)
  458. {
  459. for (int id = 1; id < 100001; id++)
  460. {
  461. DataRow dr = dt.NewRow();
  462. dr["S_I_ID"] = id;
  463. dr["S_W_ID"] = "W_" + i;
  464. dr["S_QUANTITY"] = Frnd.Next(10, 100);
  465. dr["S_DIST_01"] = RandomString(24, 24);
  466. dr["S_DIST_02"] = RandomString(24, 24);
  467. dr["S_DIST_03"] = RandomString(24, 24);
  468. dr["S_DIST_04"] = RandomString(24, 24);
  469. dr["S_DIST_05"] = RandomString(24, 24);
  470. dr["S_DIST_06"] = RandomString(24, 24);
  471. dr["S_DIST_07"] = RandomString(24, 24);
  472. dr["S_DIST_08"] = RandomString(24, 24);
  473. dr["S_DIST_09"] = RandomString(24, 24);
  474. dr["S_DIST_10"] = RandomString(24, 24);
  475. dr["S_YTD"] = 0;
  476. dr["S_ORDER_CNT"] = 0;
  477. dr["S_REMOTE_CNT"] = 0;
  478. dr["S_DATA"] = RandomString(26, 50);
  479. dr["SEQ_ID"] = id;
  480. dt.Rows.Add(dr);
  481. }
  482. bulkCopy.WriteToServer(dt);
  483. dt.Clear();
  484. i++;
  485. }
  486. return;
  487. }
  488. /// <summary>
  489. /// /************************************************************************************************
  490. /// Populate WAREHOUSE table
  491. /// By Wesley D. Brown
  492. /// Date 11/24/2007
  493. /// Mod
  494. /// **Description**
  495. /// Functions:
  496. /// **End Discription**
  497. /// **Change Log**
  498. /// **End Change Log**
  499. /// ************************************************************************************************/
  500. /// </summary>
  501. /// <returns></returns>
  502. public void BuildWarehouseTable()
  503. {
  504. var dt = new DataTable();
  505. var sqlConnect = new SqlConnection {ConnectionString = Sqlconn};
  506. sqlConnect.Open();
  507. var dc = new SqlCommand("select top 0 * from dbo.WAREHOUSE", sqlConnect) {CommandType = CommandType.Text};
  508. var da = new SqlDataAdapter(dc);
  509. da.FillSchema(dt, SchemaType.Mapped);
  510. sqlConnect.Close();
  511. sqlConnect.Dispose();
  512. var bulkCopy = new SqlBulkCopy(Sqlconn,SqlBulkCopyOptions.TableLock) {DestinationTableName = "dbo.WAREHOUSE", BatchSize = 10000};
  513. int i = NumWh;
  514. while (i < MaxNumWh + 1)
  515. {
  516. DataRow dr = dt.NewRow();
  517. dr["W_ID"] = "W_" + i;
  518. dr["W_NAME"] = RandomString(6, 10);
  519. dr["W_STREET_1"] = RandomString(10, 20);
  520. dr["W_STREET_2"] = RandomString(10, 20);
  521. dr["W_CITY"] = RandomString(10, 20);
  522. dr["W_STATE"] = RandomString(2, 2);
  523. dr["W_ZIP"] = RandZip();
  524. dr["W_TAX"] = 0.1000;
  525. dr["W_YTD"] = 3000000.00;
  526. dr["SEQ_ID"] = i;
  527. dt.Rows.Add(dr);
  528. i++;
  529. }
  530. bulkCopy.WriteToServer(dt);
  531. dt.Clear();
  532. return;
  533. }
  534. /// <summary>
  535. /// Generates a random string with the given length
  536. /// </summary>
  537. /// <param name="strMin">minimum size of the string</param>
  538. /// <param name="strMin">minimum size of the string</param>
  539. /// <param name="strMax"></param>
  540. /// <returns>Random string</returns>
  541. private static String RandomString(int strMin, int strMax)
  542. {
  543. String randomString = "";
  544. for (int x = 0; x < Frnd.Next(strMin, strMax); ++x)
  545. {
  546. randomString += RandHold.Substring(Frnd.Next(0, 62), 1);
  547. }
  548. return randomString;
  549. }
  550. /// <summary>
  551. /// Generates a random zip code string with the given length
  552. /// </summary>
  553. /// <returns>Random string</returns>
  554. private static String RandZip()
  555. {
  556. string holdZip = "";
  557. for (int x = 0; x < 4; ++x)
  558. {
  559. holdZip += ZipRandHold.Substring(Frnd.Next(0, 9), 1);
  560. }
  561. holdZip += "11111";
  562. return holdZip;
  563. }
  564. //private static Random Frnd = new Random();
  565. }