Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

478 lines
17 KiB

  1. #####
  2. # T E S T . P L
  3. # -------------
  4. # A test script for exercising the Win32::ODBC extension. Install
  5. # the ODBC.PLL extension and the ODBC.PM wrapper, set up an ODBC
  6. # DSN (Data Source Name) by the ODBC administrator, then give this a try!
  7. #
  8. # READ THE DOCUMENTATION -- I AM NOT RESPOSIBLE FOR ANY PROBLEMS THAT
  9. # THIS MAY CAUSE WHATSOEVER. BY USING THIS OR ANY ---
  10. # OF THE WIN32::ODBC PARTS FOUND IN THE DISTRIBUTION YOU ARE AGREEING
  11. # WITH THE TERMS OF THIS DISTRIBUTION!!!!!
  12. #
  13. #
  14. # You have been warned.
  15. # --- ---- ---- ------
  16. #
  17. # Updated to test newest version v961007. Dave Roth <[email protected]>
  18. # This version contains a small sample database (in MS Access 7.0
  19. # format) called ODBCTest.mdb. Place this database in the same
  20. # directory as this test.pl file.
  21. #
  22. # --------------------------------------------------------------------
  23. #
  24. # SYNTAX:
  25. # perl test.pl ["DSN Name" [Max Rows]]
  26. #
  27. # DSN Name....Name of DSN that will be used. If this is
  28. # omitted then we will use the obnoxious default DSN.
  29. # Max Rows....Maximum number of rows wanted to be retrieved from
  30. # the DSN.
  31. # - If this is 0 then the request is to retrieve as
  32. # many as possible.
  33. # - If this is a value greater than that which the DSN
  34. # driver can handle the value will be the greatest
  35. # the DSN driver allows.
  36. # - If omitted then we use the default value.
  37. #####
  38. use Win32::ODBC;
  39. $TempDSN = "Win32 ODBC Test --123xxYYzz987--";
  40. $iTempDSN = 1;
  41. if (!($DSN = $ARGV[0])){
  42. $DSN = $TempDSN;
  43. }
  44. $MaxRows = 8 unless defined ($MaxRows = $ARGV[1]);
  45. $DriverType = "Microsoft Access Driver (*.mdb)";
  46. $Desc = "Description=The Win32::ODBC Test DSN for Perl";
  47. $Dir = `cd`;
  48. chop $Dir;
  49. $DBase = "ODBCTest.mdb";
  50. $iWidth=60;
  51. %SQLStmtTypes = (SQL_CLOSE, "SQL_CLOSE", SQL_DROP, "SQL_DROP", SQL_UNBIND, "SQL_UNBIND", SQL_RESET_PARAMS, "SQL_RESET_PARAMS");
  52. # Initialize();
  53. ($Name, $Version, $Date, $Author, $CompileDate, $CompileTime, $Credits) = Win32::ODBC::Info();
  54. print "\n";
  55. print "\t+", "=" x ($iWidth), "+\n";
  56. print "\t|", Center("", $iWidth), "|\n";
  57. print "\t|", Center("", $iWidth), "|\n";
  58. print "\t|", Center("$Name", $iWidth), "|\n";
  59. print "\t|", Center("-" x length("$Name"), $iWidth), "|\n";
  60. print "\t|", Center("", $iWidth), "|\n";
  61. print "\t|", Center("Version $Version ($Date)", $iWidth), "|\n";
  62. print "\t|", Center("by $Author", $iWidth), "|\n";
  63. print "\t|", Center("Compiled on $CompileDate at $CompileTime.", $iWidth), "|\n";
  64. print "\t|", Center("", $iWidth), "|\n";
  65. print "\t|", Center("Credits:", $iWidth), "|\n";
  66. print "\t|", Center(("-" x length("Credits:")), $iWidth), "|\n";
  67. foreach $Temp (split("\n", $Credits)){
  68. print "\t|", Center("$Temp", $iWidth), "|\n";
  69. }
  70. print "\t|", Center("", $iWidth), "|\n";
  71. print "\t+", "=" x ($iWidth), "+\n";
  72. ####
  73. # T E S T 1
  74. ####
  75. PrintTest(1, "Dump available ODBC Drivers");
  76. print "\nAvailable ODBC Drivers:\n";
  77. if (!(%Drivers = Win32::ODBC::Drivers())){
  78. $Failed{'Test 1'} = "Drivers(): " . Win32::ODBC::Error();
  79. }
  80. foreach $Driver (keys(%Drivers)){
  81. print " Driver=\"$Driver\"\n Attributes: ", join("\n" . " "x14, sort(split(';', $Drivers{$Driver}))), "\n\n";
  82. }
  83. ####
  84. # T E S T 2
  85. ####
  86. PrintTest(2,"Dump available datasources");
  87. ####
  88. # Notice you don't need an instantiated object to use this...
  89. ####
  90. print "\nHere are the available datasources...\n";
  91. if (!(%DSNs = Win32::ODBC::DataSources())){
  92. $Failed{'Test 2'} = "DataSources(): " . Win32::ODBC::Error();
  93. }
  94. foreach $Temp (keys(%DSNs)){
  95. if (($Temp eq $TempDSN) && ($DSNs{$Temp} eq $DriverType)){
  96. $iTempDSNExists++;
  97. }
  98. if ($DSN =~ /$Temp/i){
  99. $iTempDSN = 0;
  100. $DriverType = $DSNs{$Temp};
  101. }
  102. print "\tDSN=\"$Temp\" (\"$DSNs{$Temp}\")\n";
  103. }
  104. ####
  105. # T E S T 2.5
  106. ####
  107. # IF WE DO NOT find the DSN the user specified...
  108. if ($iTempDSN){
  109. PrintTest("2.5", "Create a Temporary DSN");
  110. print "\n\tCould not find the DSN (\"$DSN\") so we will\n\tuse a temporary one (\"$TempDSN\")...\n\n";
  111. $DSN = $TempDSN;
  112. if (! $iTempDSNExists){
  113. print "\tAdding DSN \"$DSN\"...";
  114. if (Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType, ("DSN=$DSN", "Description=The Win32 ODBC Test DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD="))){
  115. print "Successful!\n";
  116. }else{
  117. print "Failure\n";
  118. $Failed{'Test 2.5'} = "ConfigDSN(): Could not add \"$DSN\": " . Win32::ODBC::Error();
  119. # If we failed here then use the last DSN we listed in Test 2
  120. $DriverType = $DSNs{$Temp};
  121. $DSN = $Temp;
  122. print "\n\tCould not add a temporary DSN so using the last one listed:\n";
  123. print "\t\t$DSN ($DriverType)\n";
  124. }
  125. }
  126. }
  127. ####
  128. # Report What Driver/DSN we are using
  129. ####
  130. print "\n\nWe are using the DSN:\n\tDSN = \"$DSN\"\n";
  131. print "\tDriver = \"$DriverType\"\n\n";
  132. ####
  133. # T E S T 3
  134. ####
  135. PrintTest(3, "Open several ODBC connections");
  136. print "\n\tOpening ODBC connection for \"$DSN\"...\n\t\t";
  137. if (!($O = new Win32::ODBC($DSN))){
  138. print "Failure. \n\n";
  139. $Failed{'Test 3a'} = "new(): " . Win32::ODBC::Error();
  140. PresentErrors();
  141. exit();
  142. }else{
  143. print "Success (connection #", $O->Connection(), ")\n\n";
  144. }
  145. print "\tOpening ODBC connection for \"$DSN\"...\n\t\t";
  146. if (!($O2 = new Win32::ODBC($DSN))){
  147. $Failed{'Test 3b'} = "new(): " . Win32::ODBC::Error();
  148. print "Failure. \n\n";
  149. }else{
  150. print "Success (connection #", $O2->Connection(), ")\n\n";
  151. }
  152. print "\tOpening ODBC connection for \"$DSN\"\n\t\t";
  153. if (!($O3 = new Win32::ODBC($DSN))){
  154. $Failed{'Test 3c'} = "new(): " . Win32::ODBC::Error();
  155. print "Failure. \n\n";
  156. }else{
  157. print "Success (connection #", $O3->Connection(), ")\n\n";
  158. }
  159. ####
  160. # T E S T 4
  161. ####
  162. PrintTest(4, "Close all but one connection");
  163. print "\n\tCurrently open ODBC connections are: \"", join(", ", sort($O2->GetConnections())), "\"\n";
  164. print "\tClosing ODBC connection #", $O2->Connection(), "...\n";
  165. print "\t\t...", (($O2->Close())? "Successful.":"Failure."), "\n";
  166. print "\n\tCurrently open ODBC connections are: \"", join(", ", sort($O3->GetConnections())), "\"\n";
  167. print "\tClosing ODBC connection #", $O3->Connection(), "...\n";
  168. print "\t\t...", (($O3->Close())? "Successful.":"Failure."), "\n";
  169. print "\n\tCurrently open ODBC connections are: \"", join(", ", sort($O2->GetConnections())), "\"\n";
  170. ####
  171. # T E S T 5
  172. ####
  173. PrintTest(5, "Set/query Max Buffer size for a connection");
  174. srand(time);
  175. $Temp = int(rand(10240)) + 10240;
  176. print "\nMaximum Buffer Size for connection #", $O->Connection(), ":\n";
  177. print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  178. print "\tSetting Maximum Buffer Size to $Temp... it has been set to ", $O->SetMaxBufSize($Temp), "\n";
  179. print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  180. $Temp += int(rand(10240)) + 102400;
  181. print "\tSetting Maximum Buffer Size to $Temp... (can not be more than 102400)\n\t\t...it has been set to ", $O->SetMaxBufSize($Temp), "\n";
  182. print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  183. $Temp = int(rand(1024)) + 2048;
  184. print "\tSetting Maximum Buffer Size to $Temp... it has been set to ", $O->SetMaxBufSize($Temp), "\n";
  185. print "\tValue set at ", $O->GetMaxBufSize(), "\n";
  186. ####
  187. # T E S T 6
  188. ####
  189. PrintTest(6, "Set/query Stmt Close Type");
  190. print "\n\tStatement Close Type is currently set as ", $O->GetStmtCloseType(), " " . $O->Error . "\n";
  191. print "\tSetting Statement Close Type to SQL_CLOSE: (returned code of ", $O->SetStmtCloseType(SQL_CLOSE), ")" . $O->Error . "\n";
  192. print "\tStatement Close Type is currently set as ", $O->GetStmtCloseType(), " " . $O->Error ."\n";
  193. ####
  194. # T E S T 7
  195. ####
  196. PrintTest(7, "Dump DSN for current connection");
  197. if (! (%DSNAttributes = $O->GetDSN())){
  198. $Failed{'Test 7'} = "GetDSN(): " . $O->Error();
  199. }else{
  200. print"\nThe DSN for connection #", $O->Connection(), ":\n";
  201. print "\tDSN...\n";
  202. foreach (sort(keys(%DSNAttributes))){
  203. print "\t$_ = \"$DSNAttributes{$_}\"\n";
  204. }
  205. }
  206. ####
  207. # T E S T 8
  208. ####
  209. PrintTest(8, "Dump list of ALL tables in datasource");
  210. print "\nList of tables for \"$DSN\"\n\n";
  211. $Num = 0;
  212. if ($O->Catalog("", "", "%", "'TABLE','VIEW','SYSTEM TABLE', 'GLOBAL TEMPORARY','LOCAL TEMPORARY','ALIAS','SYNONYM'")){
  213. print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n";
  214. print "\tRenaming cursor to \"TestCursor\"...", (($O->SetCursorName("TestCursor"))? "Success":"Failure"), ".\n";
  215. print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n\n";
  216. @FieldNames = $O->FieldNames();
  217. $~ = "Test_8_Header";
  218. write;
  219. $~ = "Test_8_Body";
  220. while($O->FetchRow()){
  221. undef %Data;
  222. %Data = $O->DataHash();
  223. write;
  224. }
  225. }
  226. print "\n\tTotal number of tables displayed: $Num\n";
  227. ####
  228. # T E S T 9
  229. ####
  230. PrintTest(9, "Dump list of non-system tables and views in datasource");
  231. print "\n";
  232. $Num = 0;
  233. foreach $Temp ($O->TableList("", "", "%", "TABLE, VIEW, SYSTEM_TABLE")){
  234. $Table = $Temp;
  235. print "\t", ++$Num, ".) \"$Temp\"\n";
  236. }
  237. print "\n\tTotal number of tables displayed: $Num\n";
  238. ####
  239. # T E S T 10
  240. ####
  241. PrintTest(10, "Dump contents of the table: \"$Table\"");
  242. print "\n";
  243. print "\tResetting (dropping) cursor...", (($O->DropCursor())? "Successful":"Failure"), ".\n\n";
  244. print "\tCurrently the cursor type is: ", $O->GetStmtOption($O->SQL_CURSOR_TYPE), "\n";
  245. print "\tSetting Cursor to Dynamic (", ($O->SQL_CURSOR_DYNAMIC), ")...", (($O->SetStmtOption($O->SQL_CURSOR_TYPE, $O->SQL_CURSOR_DYNAMIC))? "Success":"Failure"), ".\n";
  246. print "\t\tThis may have failed depending on your ODBC Driver.\n";
  247. print "\t\tThis is not really a problem, it will default to another value.\n";
  248. print "\tUsing the cursor type of: ", $O->GetStmtOption($O->SQL_CURSOR_TYPE), "\n\n";
  249. print "\tSetting the connection to only grab $MaxRows row", ($MaxRows == 1)? "":"s", " maximum...";
  250. if ($O->SetStmtOption($O->SQL_MAX_ROWS, $MaxRows)){
  251. print "Success!\n";
  252. }else{
  253. $Failed{'Test 10a'} = "SetStmtOption(): " . Win32::ODBC::Error();
  254. print "Failure.\n";
  255. }
  256. $iTemp = $O->GetStmtOption($O->SQL_MAX_ROWS);
  257. print "\tUsing the maximum rows: ", (($iTemp)? $iTemp:"No maximum limit"), "\n\n";
  258. print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n";
  259. print "\tRenaming cursor to \"TestCursor\"...", (($O->SetCursorName("TestCursor"))? "Success":"Failure"), ".\n";
  260. print "\tCursor is currently named \"", $O->GetCursorName(), "\".\n\n";
  261. if (! $O->Sql("SELECT * FROM [$Table]")){
  262. @FieldNames = $O->FieldNames();
  263. $Cols = $#FieldNames + 1;
  264. $Cols = 8 if ($Cols > 8);
  265. $FmH = "format Test_10_Header =\n";
  266. $FmH2 = "";
  267. $FmH3 = "";
  268. $FmB = "format Test_10_Body = \n";
  269. $FmB2 = "";
  270. for ($iTemp = 0; $iTemp < $Cols; $iTemp++){
  271. $FmH .= "@" . "<" x (80/$Cols - 2) . " ";
  272. $FmH2 .= "\$FieldNames[$iTemp],";
  273. $FmH3 .= "-" x (80/$Cols - 1) . " ";
  274. $FmB .= "@" . "<" x (80/$Cols - 2) . " ";
  275. $FmB2 .= "\$Data{\$FieldNames[$iTemp]},";
  276. }
  277. chop $FmH2;
  278. chop $FmB2;
  279. eval"$FmH\n$FmH2\n$FmH3\n.\n";
  280. eval "$FmB\n$FmB2\n.\n";
  281. $~ = "Test_10_Header";
  282. write();
  283. $~ = "Test_10_Body";
  284. # Fetch the next rowset
  285. while($O->FetchRow()){
  286. undef %Data;
  287. %Data = $O->DataHash();
  288. write();
  289. }
  290. ####
  291. # THE preceeding block could have been written like this:
  292. # ------------------------------------------------------
  293. #
  294. # print "\tCurrently the cursor type is: ", $O->GetStmtOption($O->SQL_CURSOR_TYPE), "\n";
  295. # print "\tSetting Cursor to Dynamic (", ($O->SQL_CURSOR_DYNAMIC), ")...", (($O->SetStmtOption($O->SQL_CURSOR_TYPE, $O->SQL_CURSOR_DYNAMIC))? "Success":"Failure"), ".\n";
  296. # print "\t\tThis may have failed depending on your ODBC Driver. No real problem.\n";
  297. # print "\tUsing the cursor type of: ", $O->GetStmtOption($O->SQL_CURSOR_TYPE), "\n\n";
  298. #
  299. # print "\tSetting rowset size = 15 ...", (($O->SetStmtOption($O->SQL_ROWSET_SIZE, 15))? "Success":"Failure"), ".\n";
  300. # print "\tGetting rowset size: ", $O->GetStmtOption($O->SQL_ROWSET_SIZE), "\n\n";
  301. #
  302. # while($O->FetchRow()){
  303. # $iNum = 1;
  304. # # Position yourself in the rowset
  305. # while($O->SetPos($iNum++ ,$O->SQL_POSITION, $O->SQL_LOCK_NO_CHANGE)){
  306. # undef %Data;
  307. # %Data = $O->DataHash();
  308. # write();
  309. # }
  310. # print "\t\tNext rowset...\n";
  311. # }
  312. #
  313. # The reason I didn't write it that way (which is easier) is to
  314. # show that we can now SetPos(). Also Fetch() now uses
  315. # SQLExtendedFetch() so it can position itself and retrieve
  316. # rowsets. Notice earlier in this Test 10 we set the
  317. # SQL_ROWSET_SIZE. If this was not set it would default to
  318. # no limit (depending upon your ODBC Driver).
  319. ####
  320. print "\n\tNo more records available.\n";
  321. }else{
  322. $Failed{'Test 10'} = "Sql(): " . $O->Error();
  323. }
  324. $O->Close();
  325. ####
  326. # T E S T 11
  327. ####
  328. if ($iTempDSN){
  329. PrintTest(11, "Remove the temporary DSN");
  330. print "\n\tRemoving the temporary DSN:\n";
  331. print "\t\tDSN = \"$DSN\"\n\t\tDriver = \"$DriverType\"\n";
  332. if (Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN")){
  333. print "\tSuccessful!\n";
  334. }else{
  335. print "\tFailed.\n";
  336. $Failed{'Test 11'} = "ConfigDSN(): Could not remove \"$DSN\":" . Win32::ODBC::Error();
  337. }
  338. }
  339. PrintTest("E N D O F T E S T");
  340. PresentErrors();
  341. #----------------------- F U N C T I O N S ---------------------------
  342. sub Error{
  343. my($Data) = @_;
  344. $Data->DumpError() if ref($Data);
  345. Win32::ODBC::DumpError() if ! ref($Data);
  346. }
  347. sub Center{
  348. local($Temp, $Width) = @_;
  349. local($Len) = ($Width - length($Temp)) / 2;
  350. return " " x int($Len), $Temp, " " x (int($Len) + (($Len != int($Len))? 1:0));
  351. }
  352. sub PrintTest{
  353. my($Num, $String) = @_;
  354. my($Temp);
  355. if (length($String)){
  356. $Temp = " T E S T $Num $String ";
  357. }else{
  358. $Temp = " $Num ";
  359. }
  360. $Len = length($Temp);
  361. print "\n", "-" x ((79 - $Len)/2), $Temp, "-" x ((79 - $Len)/2 - 1), "\n";
  362. print "\t$String\n";
  363. }
  364. sub PresentErrors{
  365. PrintTest("", "Error Report:");
  366. if (keys(%Failed)){
  367. print "The following were errors:\n";
  368. foreach (sort(keys(%Failed))){
  369. print "$_ = $Failed{$_}\n";
  370. }
  371. }else{
  372. print "\n\nThere were no errors reported during this test.\n\n";
  373. }
  374. }
  375. sub Initialize{
  376. format Test_8_Header =
  377. @<<<<<<<<<<<<<<<<<<<<<<<<<<< @|||||||||||| @|||||||||||| @|||||||||||
  378. $FieldNames[0], $FieldNames[1], $FieldNames[2], $FieldNames[3]
  379. ---------------------------- ------------- ------------- ------------
  380. .
  381. format Test_8_Body =
  382. @>. @<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<
  383. ++$Num, $Data{$FieldNames[0]}, $Data{$FieldNames[1]}, $Data{$FieldNames[2]}, $Data{$FieldNames[3]}
  384. .
  385. format Test_9_Header =
  386. @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
  387. $FieldNames[0], $FieldNames[1], $FieldNames[2], $FieldNames[3]
  388. .
  389. format Test_9_Body =
  390. @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
  391. $Data{$FieldNames[0]}, $Data{$FieldNames[1]}, $Data{$FieldNames[2]}, $Data{$FieldNames[3]}
  392. .
  393. }