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.

1493 lines
40 KiB

  1. package Win32::ODBC;
  2. $VERSION = '0.032';
  3. # Win32::ODBC.pm
  4. # +==========================================================+
  5. # | |
  6. # | ODBC.PM package |
  7. # | --------------- |
  8. # | |
  9. # | Copyright (c) 1996, 1997 Dave Roth. All rights reserved. |
  10. # | This program is free software; you can redistribute |
  11. # | it and/or modify it under the same terms as Perl itself. |
  12. # | |
  13. # +==========================================================+
  14. #
  15. #
  16. # based on original code by Dan DeMaggio ([email protected])
  17. #
  18. # Use under GNU General Public License or Larry Wall's "Artistic License"
  19. #
  20. # Check the README.TXT file that comes with this package for details about
  21. # it's history.
  22. #
  23. require Exporter;
  24. require DynaLoader;
  25. $ODBCPackage = "Win32::ODBC";
  26. $ODBCPackage::Version = 970208;
  27. $::ODBC = $ODBCPackage;
  28. $CacheConnection = 0;
  29. # Reserve ODBC in the main namespace for US!
  30. *ODBC::=\%Win32::ODBC::;
  31. @ISA= qw( Exporter DynaLoader );
  32. # Items to export into callers namespace by default. Note: do not export
  33. # names by default without a very good reason. Use EXPORT_OK instead.
  34. # Do not simply export all your public functions/methods/constants.
  35. @EXPORT = qw(
  36. ODBC_ADD_DSN
  37. ODBC_REMOVE_DSN
  38. ODBC_CONFIG_DSN
  39. ODBC_ADD_SYS_DSN
  40. ODBC_REMOVE_SYS_DSN
  41. ODBC_CONFIG_SYS_DSN
  42. SQL_DONT_CLOSE
  43. SQL_DROP
  44. SQL_CLOSE
  45. SQL_UNBIND
  46. SQL_RESET_PARAMS
  47. SQL_FETCH_NEXT
  48. SQL_FETCH_FIRST
  49. SQL_FETCH_LAST
  50. SQL_FETCH_PRIOR
  51. SQL_FETCH_ABSOLUTE
  52. SQL_FETCH_RELATIVE
  53. SQL_FETCH_BOOKMARK
  54. SQL_COLUMN_COUNT
  55. SQL_COLUMN_NAME
  56. SQL_COLUMN_TYPE
  57. SQL_COLUMN_LENGTH
  58. SQL_COLUMN_PRECISION
  59. SQL_COLUMN_SCALE
  60. SQL_COLUMN_DISPLAY_SIZE
  61. SQL_COLUMN_NULLABLE
  62. SQL_COLUMN_UNSIGNED
  63. SQL_COLUMN_MONEY
  64. SQL_COLUMN_UPDATABLE
  65. SQL_COLUMN_AUTO_INCREMENT
  66. SQL_COLUMN_CASE_SENSITIVE
  67. SQL_COLUMN_SEARCHABLE
  68. SQL_COLUMN_TYPE_NAME
  69. SQL_COLUMN_TABLE_NAME
  70. SQL_COLUMN_OWNER_NAME
  71. SQL_COLUMN_QUALIFIER_NAME
  72. SQL_COLUMN_LABEL
  73. SQL_COLATT_OPT_MAX
  74. SQL_COLUMN_DRIVER_START
  75. SQL_COLATT_OPT_MIN
  76. SQL_ATTR_READONLY
  77. SQL_ATTR_WRITE
  78. SQL_ATTR_READWRITE_UNKNOWN
  79. SQL_UNSEARCHABLE
  80. SQL_LIKE_ONLY
  81. SQL_ALL_EXCEPT_LIKE
  82. SQL_SEARCHABLE
  83. );
  84. #The above are included for backward compatibility
  85. sub new
  86. {
  87. my ($n, $self);
  88. my ($type) = shift;
  89. my ($DSN) = shift;
  90. my (@Results) = @_;
  91. if (ref $DSN){
  92. @Results = ODBCClone($DSN->{'connection'});
  93. }else{
  94. @Results = ODBCConnect($DSN, @Results);
  95. }
  96. @Results = processError(-1, @Results);
  97. if (! scalar(@Results)){
  98. return undef;
  99. }
  100. $self = bless {};
  101. $self->{'connection'} = $Results[0];
  102. $ErrConn = $Results[0];
  103. $ErrText = $Results[1];
  104. $ErrNum = 0;
  105. $self->{'DSN'} = $DSN;
  106. $self;
  107. }
  108. ####
  109. # Close this ODBC session (or all sessions)
  110. ####
  111. sub Close
  112. {
  113. my ($self, $Result) = shift;
  114. $Result = DESTROY($self);
  115. $self->{'connection'} = -1;
  116. return $Result;
  117. }
  118. ####
  119. # Auto-Kill an instance of this module
  120. ####
  121. sub DESTROY
  122. {
  123. my ($self) = shift;
  124. my (@Results) = (0);
  125. if($self->{'connection'} > -1){
  126. @Results = ODBCDisconnect($self->{'connection'});
  127. @Results = processError($self, @Results);
  128. if ($Results[0]){
  129. undef $self->{'DSN'};
  130. undef @{$self->{'fnames'}};
  131. undef %{$self->{'field'}};
  132. undef %{$self->{'connection'}};
  133. }
  134. }
  135. return $Results[0];
  136. }
  137. sub sql{
  138. return (Sql(@_));
  139. }
  140. ####
  141. # Submit an SQL Execute statement for processing
  142. ####
  143. sub Sql{
  144. my ($self, $Sql, @Results) = @_;
  145. @Results = ODBCExecute($self->{'connection'}, $Sql);
  146. return updateResults($self, @Results);
  147. }
  148. ####
  149. # Retrieve data from a particular field
  150. ####
  151. sub Data{
  152. # Change by JOC 06-APR-96
  153. # Altered by Dave Roth <[email protected]> 96.05.07
  154. my($self) = shift;
  155. my(@Fields) = @_;
  156. my(@Results, $Results, $Field);
  157. if ($self->{'Dirty'}){
  158. GetData($self);
  159. $self->{'Dirty'} = 0;
  160. }
  161. @Fields = @{$self->{'fnames'}} if (! scalar(@Fields));
  162. foreach $Field (@Fields) {
  163. if (wantarray) {
  164. push(@Results, data($self, $Field));
  165. } else {
  166. $Results .= data($self, $Field);
  167. }
  168. }
  169. return wantarray ? @Results : $Results;
  170. }
  171. sub DataHash{
  172. my($self, @Results) = @_;
  173. my(%Results, $Element);
  174. if ($self->{'Dirty'}){
  175. GetData($self);
  176. $self->{'Dirty'} = 0;
  177. }
  178. @Results = @{$self->{'fnames'}} if (! scalar(@Results));
  179. foreach $Element (@Results) {
  180. $Results{$Element} = data($self, $Element);
  181. }
  182. return %Results;
  183. }
  184. ####
  185. # Retrieve data from the data buffer
  186. ####
  187. sub data
  188. { $_[0]->{'data'}->{$_[1]}; }
  189. sub fetchrow{
  190. return (FetchRow(@_));
  191. }
  192. ####
  193. # Put a row from an ODBC data set into data buffer
  194. ####
  195. sub FetchRow{
  196. my ($self, @Results) = @_;
  197. my ($item, $num, $sqlcode);
  198. # Added by JOC 06-APR-96
  199. # $num = 0;
  200. $num = 0;
  201. undef $self->{'data'};
  202. @Results = ODBCFetch($self->{'connection'}, @Results);
  203. if (! (@Results = processError($self, @Results))){
  204. ####
  205. # There should be an innocuous error "No records remain"
  206. # This indicates no more records in the dataset
  207. ####
  208. return undef;
  209. }
  210. # Set the Dirty bit so we will go and extract data via the
  211. # ODBCGetData function. Otherwise use the cache.
  212. $self->{'Dirty'} = 1;
  213. # Return the array of field Results.
  214. return @Results;
  215. }
  216. sub GetData{
  217. my($self) = @_;
  218. my @Results;
  219. my $num = 0;
  220. @Results = ODBCGetData($self->{'connection'});
  221. if (!(@Results = processError($self, @Results))){
  222. return undef;
  223. }
  224. ####
  225. # This is a special case. Do not call processResults
  226. ####
  227. ClearError();
  228. foreach (@Results){
  229. s/ +$// if defined $_; # HACK
  230. $self->{'data'}->{ ${$self->{'fnames'}}[$num] } = $_;
  231. $num++;
  232. }
  233. # return is a hack to interface with a assoc array.
  234. return wantarray? (1, 1): 1;
  235. }
  236. ####
  237. # See if any more ODBC Results Sets
  238. # Added by Brian Dunfordshore <[email protected]>
  239. # 96.07.10
  240. ####
  241. sub MoreResults{
  242. my ($self) = @_;
  243. my(@Results) = ODBCMoreResults($self->{'connection'});
  244. return (processError($self, @Results))[0];
  245. }
  246. ####
  247. # Retrieve the catalog from the current DSN
  248. # NOTE: All Field names are uppercase!!!
  249. ####
  250. sub Catalog{
  251. my ($self) = shift;
  252. my ($Qualifier, $Owner, $Name, $Type) = @_;
  253. my (@Results) = ODBCTableList($self->{'connection'}, $Qualifier, $Owner, $Name, $Type);
  254. # If there was an error return 0 else 1
  255. return (updateResults($self, @Results) != 1);
  256. }
  257. ####
  258. # Return an array of names from the catalog for the current DSN
  259. # TableList($Qualifier, $Owner, $Name, $Type)
  260. # Return: (array of names of tables)
  261. # NOTE: All Field names are uppercase!!!
  262. ####
  263. sub TableList{
  264. my ($self) = shift;
  265. my (@Results) = @_;
  266. if (! scalar(@Results)){
  267. @Results = ("", "", "%", "TABLE");
  268. }
  269. if (! Catalog($self, @Results)){
  270. return undef;
  271. }
  272. undef @Results;
  273. while (FetchRow($self)){
  274. push(@Results, Data($self, "TABLE_NAME"));
  275. }
  276. return sort(@Results);
  277. }
  278. sub fieldnames{
  279. return (FieldNames(@_));
  280. }
  281. ####
  282. # Return an array of fieldnames extracted from the current dataset
  283. ####
  284. sub FieldNames { $self = shift; return @{$self->{'fnames'}}; }
  285. ####
  286. # Closes this connection. This is used mostly for testing. You should
  287. # probably use Close().
  288. ####
  289. sub ShutDown{
  290. my($self) = @_;
  291. print "\nClosing connection $self->{'connection'}...";
  292. $self->Close();
  293. print "\nDone\n";
  294. }
  295. ####
  296. # Return this connection number
  297. ####
  298. sub Connection{
  299. my($self) = @_;
  300. return $self->{'connection'};
  301. }
  302. ####
  303. # Returns the current connections that are in use.
  304. ####
  305. sub GetConnections{
  306. return ODBCGetConnections();
  307. }
  308. ####
  309. # Set the Max Buffer Size for this connection. This determines just how much
  310. # ram can be allocated when a fetch() is performed that requires a HUGE amount
  311. # of memory. The default max is 10k and the absolute max is 100k.
  312. # This will probably never be used but I put it in because I noticed a fetch()
  313. # of a MEMO field in an Access table was something like 4Gig. Maybe I did
  314. # something wrong, but after checking several times I decided to impliment
  315. # this limit thingie.
  316. ####
  317. sub SetMaxBufSize{
  318. my($self, $Size) = @_;
  319. my(@Results) = ODBCSetMaxBufSize($self->{'connection'}, $Size);
  320. return (processError($self, @Results))[0];
  321. }
  322. ####
  323. # Returns the Max Buffer Size for this connection. See SetMaxBufSize().
  324. ####
  325. sub GetMaxBufSize{
  326. my($self) = @_;
  327. my(@Results) = ODBCGetMaxBufSize($self->{'connection'});
  328. return (processError($self, @Results))[0];
  329. }
  330. ####
  331. # Returns the DSN for this connection as an associative array.
  332. ####
  333. sub GetDSN{
  334. my($self, $DSN) = @_;
  335. if(! ref($self)){
  336. $DSN = $self;
  337. $self = 0;
  338. }
  339. if (! $DSN){
  340. $self = $self->{'connection'};
  341. }
  342. my(@Results) = ODBCGetDSN($self, $DSN);
  343. return (processError($self, @Results));
  344. }
  345. ####
  346. # Returns an associative array of $XXX{'DSN'}=Description
  347. ####
  348. sub DataSources{
  349. my($self, $DSN) = @_;
  350. if(! ref $self){
  351. $DSN = $self;
  352. $self = 0;
  353. }
  354. my(@Results) = ODBCDataSources($DSN);
  355. return (processError($self, @Results));
  356. }
  357. ####
  358. # Returns an associative array of $XXX{'Driver Name'}=Driver Attributes
  359. ####
  360. sub Drivers{
  361. my($self) = @_;
  362. if(! ref $self){
  363. $self = 0;
  364. }
  365. my(@Results) = ODBCDrivers();
  366. return (processError($self, @Results));
  367. }
  368. ####
  369. # Returns the number of Rows that were affected by the previous SQL command.
  370. ####
  371. sub RowCount{
  372. my($self, $Connection) = @_;
  373. if (! ref($self)){
  374. $Connection = $self;
  375. $self = 0;
  376. }
  377. if (! $Connection){$Connection = $self->{'connection'};}
  378. my(@Results) = ODBCRowCount($Connection);
  379. return (processError($self, @Results))[0];
  380. }
  381. ####
  382. # Returns the Statement Close Type -- how does ODBC Close a statment.
  383. # Types:
  384. # SQL_DROP
  385. # SQL_CLOSE
  386. # SQL_UNBIND
  387. # SQL_RESET_PARAMS
  388. ####
  389. sub GetStmtCloseType{
  390. my($self, $Connection) = @_;
  391. if (! ref($self)){
  392. $Connection = $self;
  393. $self = 0;
  394. }
  395. if (! $Connection){$Connection = $self->{'connection'};}
  396. my(@Results) = ODBCGetStmtCloseType($Connection);
  397. return (processError($self, @Results));
  398. }
  399. ####
  400. # Sets the Statement Close Type -- how does ODBC Close a statment.
  401. # Types:
  402. # SQL_DROP
  403. # SQL_CLOSE
  404. # SQL_UNBIND
  405. # SQL_RESET_PARAMS
  406. # Returns the newly set value.
  407. ####
  408. sub SetStmtCloseType{
  409. my($self, $Type, $Connection) = @_;
  410. if (! ref($self)){
  411. $Connection = $Type;
  412. $Type = $self;
  413. $self = 0;
  414. }
  415. if (! $Connection){$Connection = $self->{'connection'};}
  416. my(@Results) = ODBCSetStmtCloseType($Connection, $Type);
  417. return (processError($self, @Results))[0];
  418. }
  419. sub ColAttributes{
  420. my($self, $Type, @Field) = @_;
  421. my(%Results, @Results, $Results, $Attrib, $Connection, $Temp);
  422. if (! ref($self)){
  423. $Type = $Field;
  424. $Field = $self;
  425. $self = 0;
  426. }
  427. $Connection = $self->{'connection'};
  428. if (! scalar(@Field)){ @Field = $self->fieldnames;}
  429. foreach $Temp (@Field){
  430. @Results = ODBCColAttributes($Connection, $Temp, $Type);
  431. ($Attrib) = processError($self, @Results);
  432. if (wantarray){
  433. $Results{$Temp} = $Attrib;
  434. }else{
  435. $Results .= "$Temp";
  436. }
  437. }
  438. return wantarray? %Results:$Results;
  439. }
  440. sub GetInfo{
  441. my($self, $Type) = @_;
  442. my($Connection, @Results);
  443. if(! ref $self){
  444. $Type = $self;
  445. $self = 0;
  446. $Connection = 0;
  447. }else{
  448. $Connection = $self->{'connection'};
  449. }
  450. @Results = ODBCGetInfo($Connection, $Type);
  451. return (processError($self, @Results))[0];
  452. }
  453. sub GetConnectOption{
  454. my($self, $Type) = @_;
  455. my(@Results);
  456. if(! ref $self){
  457. $Type = $self;
  458. $self = 0;
  459. }
  460. @Results = ODBCGetConnectOption($self->{'connection'}, $Type);
  461. return (processError($self, @Results))[0];
  462. }
  463. sub SetConnectOption{
  464. my($self, $Type, $Value) = @_;
  465. if(! ref $self){
  466. $Value = $Type;
  467. $Type = $self;
  468. $self = 0;
  469. }
  470. my(@Results) = ODBCSetConnectOption($self->{'connection'}, $Type, $Value);
  471. return (processError($self, @Results))[0];
  472. }
  473. sub Transact{
  474. my($self, $Type) = @_;
  475. my(@Results);
  476. if(! ref $self){
  477. $Type = $self;
  478. $self = 0;
  479. }
  480. @Results = ODBCTransact($self->{'connection'}, $Type);
  481. return (processError($self, @Results))[0];
  482. }
  483. sub SetPos{
  484. my($self, @Results) = @_;
  485. @Results = ODBCSetPos($self->{'connection'}, @Results);
  486. $self->{'Dirty'} = 1;
  487. return (processError($self, @Results))[0];
  488. }
  489. sub ConfigDSN{
  490. my($self) = shift @_;
  491. my($Type, $Connection);
  492. if(! ref $self){
  493. $Type = $self;
  494. $Connection = 0;
  495. $self = 0;
  496. }else{
  497. $Type = shift @_;
  498. $Connection = $self->{'connection'};
  499. }
  500. my($Driver, @Attributes) = @_;
  501. @Results = ODBCConfigDSN($Connection, $Type, $Driver, @Attributes);
  502. return (processError($self, @Results))[0];
  503. }
  504. sub Version{
  505. my($self, @Packages) = @_;
  506. my($Temp, @Results);
  507. if (! ref($self)){
  508. push(@Packages, $self);
  509. }
  510. my($ExtName, $ExtVersion) = Info();
  511. if (! scalar(@Packages)){
  512. @Packages = ("ODBC.PM", "ODBC.PLL");
  513. }
  514. foreach $Temp (@Packages){
  515. if ($Temp =~ /pll/i){
  516. push(@Results, "ODBC.PM:$Win32::ODBC::Version");
  517. }elsif ($Temp =~ /pm/i){
  518. push(@Results, "ODBC.PLL:$ExtVersion");
  519. }
  520. }
  521. return @Results;
  522. }
  523. sub SetStmtOption{
  524. my($self, $Option, $Value) = @_;
  525. if(! ref $self){
  526. $Value = $Option;
  527. $Option = $self;
  528. $self = 0;
  529. }
  530. my(@Results) = ODBCSetStmtOption($self->{'connection'}, $Option, $Value);
  531. return (processError($self, @Results))[0];
  532. }
  533. sub GetStmtOption{
  534. my($self, $Type) = @_;
  535. if(! ref $self){
  536. $Type = $self;
  537. $self = 0;
  538. }
  539. my(@Results) = ODBCGetStmtOption($self->{'connection'}, $Type);
  540. return (processError($self, @Results))[0];
  541. }
  542. sub GetFunctions{
  543. my($self, @Results)=@_;
  544. @Results = ODBCGetFunctions($self->{'connection'}, @Results);
  545. return (processError($self, @Results));
  546. }
  547. sub DropCursor{
  548. my($self) = @_;
  549. my(@Results) = ODBCDropCursor($self->{'connection'});
  550. return (processError($self, @Results))[0];
  551. }
  552. sub SetCursorName{
  553. my($self, $Name) = @_;
  554. my(@Results) = ODBCSetCursorName($self->{'connection'}, $Name);
  555. return (processError($self, @Results))[0];
  556. }
  557. sub GetCursorName{
  558. my($self) = @_;
  559. my(@Results) = ODBCGetCursorName($self->{'connection'});
  560. return (processError($self, @Results))[0];
  561. }
  562. sub GetSQLState{
  563. my($self) = @_;
  564. my(@Results) = ODBCGetSQLState($self->{'connection'});
  565. return (processError($self, @Results))[0];
  566. }
  567. # ----------- R e s u l t P r o c e s s i n g F u n c t i o n s ----------
  568. ####
  569. # Generic processing of data into associative arrays
  570. ####
  571. sub updateResults{
  572. my ($self, $Error, @Results) = @_;
  573. undef %{$self->{'field'}};
  574. ClearError($self);
  575. if ($Error){
  576. SetError($self, $Results[0], $Results[1]);
  577. return ($Error);
  578. }
  579. @{$self->{'fnames'}} = @Results;
  580. foreach (0..$#{$self->{'fnames'}}){
  581. s/ +$//;
  582. $self->{'field'}->{${$self->{'fnames'}}[$_]} = $_;
  583. }
  584. return undef;
  585. }
  586. # ----------------------------------------------------------------------------
  587. # ----------------- D e b u g g i n g F u n c t i o n s --------------------
  588. sub Debug{
  589. my($self, $iDebug, $File) = @_;
  590. my(@Results);
  591. if (! ref($self)){
  592. if (defined $self){
  593. $File = $iDebug;
  594. $iDebug = $self;
  595. }
  596. $Connection = 0;
  597. $self = 0;
  598. }else{
  599. $Connection = $self->{'connection'};
  600. }
  601. push(@Results, ($Connection, $iDebug));
  602. push(@Results, $File) if ($File ne "");
  603. @Results = ODBCDebug(@Results);
  604. return (processError($self, @Results))[0];
  605. }
  606. ####
  607. # Prints out the current dataset (used mostly for testing)
  608. ####
  609. sub DumpData {
  610. my($self) = @_; my($f, $goo);
  611. # Changed by JOC 06-Apr-96
  612. # print "\nDumping Data for connection: $conn->{'connection'}\n";
  613. print "\nDumping Data for connection: $self->{'connection'}\n";
  614. print "Error: \"";
  615. print $self->Error();
  616. print "\"\n";
  617. if (! $self->Error()){
  618. foreach $f ($self->FieldNames){
  619. print $f . " ";
  620. $goo .= "-" x length($f);
  621. $goo .= " ";
  622. }
  623. print "\n$goo\n";
  624. while ($self->FetchRow()){
  625. foreach $f ($self->FieldNames){
  626. print $self->Data($f) . " ";
  627. }
  628. print "\n";
  629. }
  630. }
  631. }
  632. sub DumpError{
  633. my($self) = @_;
  634. my($ErrNum, $ErrText, $ErrConn);
  635. my($Temp);
  636. print "\n---------- Error Report: ----------\n";
  637. if (ref $self){
  638. ($ErrNum, $ErrText, $ErrConn) = $self->Error();
  639. ($Temp = $self->GetDSN()) =~ s/.*DSN=(.*?);.*/$1/i;
  640. print "Errors for \"$Temp\" on connection " . $self->{'connection'} . ":\n";
  641. }else{
  642. ($ErrNum, $ErrText, $ErrConn) = Error();
  643. print "Errors for the package:\n";
  644. }
  645. print "Connection Number: $ErrConn\nError number: $ErrNum\nError message: \"$ErrText\"\n";
  646. print "-----------------------------------\n";
  647. }
  648. ####
  649. # Submit an SQL statement and print data about it (used mostly for testing)
  650. ####
  651. sub Run{
  652. my($self, $Sql) = @_;
  653. print "\nExcecuting connection $self->{'connection'}\nsql statement: \"$Sql\"\n";
  654. $self->Sql($Sql);
  655. print "Error: \"";
  656. print $self->error;
  657. print "\"\n";
  658. print "--------------------\n\n";
  659. }
  660. # ----------------------------------------------------------------------------
  661. # ----------- E r r o r P r o c e s s i n g F u n c t i o n s ------------
  662. ####
  663. # Process Errors returned from a call to ODBCxxxx().
  664. # It is assumed that the Win32::ODBC function returned the following structure:
  665. # ($ErrorNumber, $ResultsText, ...)
  666. # $ErrorNumber....0 = No Error
  667. # >0 = Error Number
  668. # $ResultsText.....if no error then this is the first Results element.
  669. # if error then this is the error text.
  670. ####
  671. sub processError{
  672. my($self, $Error, @Results) = @_;
  673. if ($Error){
  674. SetError($self, $Results[0], $Results[1]);
  675. undef @Results;
  676. }
  677. return @Results;
  678. }
  679. ####
  680. # Return the last recorded error message
  681. ####
  682. sub error{
  683. return (Error(@_));
  684. }
  685. sub Error{
  686. my($self) = @_;
  687. if(ref($self)){
  688. if($self->{'ErrNum'}){
  689. my($State) = ODBCGetSQLState($self->{'connection'});
  690. return (wantarray)? ($self->{'ErrNum'}, $self->{'ErrText'}, $self->{'connection'}, $State) :"[$self->{'ErrNum'}] [$self->{'connection'}] [$State] \"$self->{'ErrText'}\"";
  691. }
  692. }elsif ($ErrNum){
  693. return (wantarray)? ($ErrNum, $ErrText, $ErrConn):"[$ErrNum] [$ErrConn] \"$ErrText\"";
  694. }
  695. return undef
  696. }
  697. ####
  698. # SetError:
  699. # Assume that if $self is not a reference then it is just a placeholder
  700. # and should be ignored.
  701. ####
  702. sub SetError{
  703. my($self, $Num, $Text, $Conn) = @_;
  704. if (ref $self){
  705. $self->{'ErrNum'} = $Num;
  706. $self->{'ErrText'} = $Text;
  707. $Conn = $self->{'connection'} if ! $Conn;
  708. }
  709. $ErrNum = $Num;
  710. $ErrText = $Text;
  711. ####
  712. # Test Section Begin
  713. ####
  714. # $! = ($Num, $Text);
  715. ####
  716. # Test Section End
  717. ####
  718. $ErrConn = $Conn;
  719. }
  720. sub ClearError{
  721. my($self, $Num, $Text) = @_;
  722. if (ref $self){
  723. undef $self->{'ErrNum'};
  724. undef $self->{'ErrText'};
  725. }else{
  726. undef $ErrConn;
  727. undef $ErrNum;
  728. undef $ErrText;
  729. }
  730. ODBCCleanError();
  731. return 1;
  732. }
  733. sub GetError{
  734. my($self, $Connection) = @_;
  735. my(@Results);
  736. if (! ref($self)){
  737. $Connection = $self;
  738. $self = 0;
  739. }else{
  740. if (! defined($Connection)){
  741. $Connection = $self->{'connection'};
  742. }
  743. }
  744. @Results = ODBCGetError($Connection);
  745. return @Results;
  746. }
  747. # ----------------------------------------------------------------------------
  748. # ------------------ A U T O L O A D F U N C T I O N -----------------------
  749. sub AUTOLOAD {
  750. # This AUTOLOAD is used to 'autoload' constants from the constant()
  751. # XS function. If a constant is not found then control is passed
  752. # to the AUTOLOAD in AutoLoader.
  753. my($constname);
  754. ($constname = $AUTOLOAD) =~ s/.*:://;
  755. #reset $! to zero to reset any current errors.
  756. $!=0;
  757. $val = constant($constname);
  758. if ($! != 0) {
  759. if ($! =~ /Invalid/) {
  760. $AutoLoader::AUTOLOAD = $AUTOLOAD;
  761. goto &AutoLoader::AUTOLOAD;
  762. }
  763. else {
  764. # Added by JOC 06-APR-96
  765. # $pack = 0;
  766. $pack = 0;
  767. ($pack,$file,$line) = caller;
  768. print "Your vendor has not defined Win32::ODBC macro $constname, used in $file at line $line.";
  769. }
  770. }
  771. eval "sub $AUTOLOAD { $val }";
  772. goto &$AUTOLOAD;
  773. }
  774. # --------------------------------------------------------------
  775. #
  776. #
  777. # Make sure that we shutdown ODBC and free memory even if we are
  778. # using perlis.dll on Win32 platform!
  779. END{
  780. # ODBCShutDown() unless $CacheConnection;
  781. }
  782. bootstrap Win32::ODBC;
  783. # Preloaded methods go here.
  784. # Autoload methods go after __END__, and are processed by the autosplit program.
  785. 1;
  786. __END__
  787. =head1 NAME
  788. Win32::ODBC - ODBC Extension for Win32
  789. =head1 SYNOPSIS
  790. To use this module, include the following statement at the top of your
  791. script:
  792. use Win32::ODBC;
  793. Next, create a data connection to your DSN:
  794. $Data = new Win32::ODBC("MyDSN");
  795. B<NOTE>: I<MyDSN> can be either the I<DSN> as defined in the ODBC
  796. Administrator, I<or> it can be an honest-to-God I<DSN Connect String>.
  797. Example: "DSN=My Database;UID=Brown Cow;PWD=Moo;"
  798. You should check to see if C<$Data> is indeed defined, otherwise there
  799. has been an error.
  800. You can now send SQL queries and retrieve info to your heart's
  801. content! See the description of the methods provided by this module
  802. below and also the file F<TEST.PL> as referred to in L<INSTALLATION
  803. NOTES> to see how it all works.
  804. Finally, B<MAKE SURE> that you close your connection when you are
  805. finished:
  806. $Data->Close();
  807. =head1 DESCRIPTION
  808. =head2 Background
  809. This is a hack of Dan DeMaggio's <[email protected]> F<NTXS.C> ODBC
  810. implementation. I have recoded and restructured most of it including
  811. most of the F<ODBC.PM> package, but its very core is still based on
  812. Dan's code (thanks Dan!).
  813. The history of this extension is found in the file F<HISTORY.TXT> that
  814. comes with the original archive (see L<INSTALLATION NOTES> below).
  815. =head2 Benefits
  816. And what are the benefits of this module?
  817. =over
  818. =item *
  819. The number of ODBC connections is limited by memory and ODBC itself
  820. (have as many as you want!).
  821. =item *
  822. The working limit for the size of a field is 10,240 bytes, but you can
  823. increase that limit (if needed) to a max of 2,147,483,647 bytes. (You
  824. can always recompile to increase the max limit.)
  825. =item *
  826. You can open a connection by either specifing a DSN or a connection
  827. string!
  828. =item *
  829. You can open and close the connections in any order!
  830. =item *
  831. Other things that I can not think of right now... :)
  832. =back
  833. =head1 CONSTANTS
  834. This package defines a number of constants. You may refer to each of
  835. these constants using the notation C<ODBC::xxxxx>, where C<xxxxx> is
  836. the constant.
  837. Example:
  838. print ODBC::SQL_SQL_COLUMN_NAME, "\n";
  839. =head1 SPECIAL NOTATION
  840. For the method documentation that follows, an B<*> following the
  841. method parameters indicates that that method is new or has been
  842. modified for this version.
  843. =head1 CONSTRUCTOR
  844. =over
  845. =item new ( ODBC_OBJECT | DSN [, (OPTION1, VALUE1), (OPTION2, VALUE2) ...] )
  846. *
  847. Creates a new ODBC connection based on C<DSN>, or, if you specify an
  848. already existing ODBC object, then a new ODBC object will be created
  849. but using the ODBC Connection specified by C<ODBC_OBJECT>. (The new
  850. object will be a new I<hstmt> using the I<hdbc> connection in
  851. C<ODBC_OBJECT>.)
  852. C<DSN> is I<Data Source Name> or a proper C<ODBCDriverConnect> string.
  853. You can specify SQL Connect Options that are implemented before the
  854. actual connection to the DSN takes place. These option/values are the
  855. same as specified in C<GetConnectOption>/C<SetConnectOption> (see
  856. below) and are defined in the ODBC API specs.
  857. Returns a handle to the database on success, or I<undef> on failure.
  858. =back
  859. =head1 METHODS
  860. =over
  861. =item Catalog ( QUALIFIER, OWNER, NAME, TYPE )
  862. Tells ODBC to create a data set that contains table information about
  863. the DSN. Use C<Fetch> and C<Data> or C<DataHash> to retrieve the data.
  864. The returned format is:
  865. [Qualifier] [Owner] [Name] [Type]
  866. Returns I<true> on error.
  867. =item ColAttributes ( ATTRIBUTE [, FIELD_NAMES ] )
  868. Returns the attribute C<ATTRIBUTE> on each of the fields in the list
  869. C<FIELD_NAMES> in the current record set. If C<FIELD_NAMES> is empty,
  870. then all fields are assumed. The attributes are returned as an
  871. associative array.
  872. =item ConfigDSN ( OPTION, DRIVER, ATTRIBUTE1 [, ATTRIBUTE2, ATTRIBUTE3, ...
  873. ] )
  874. Configures a DSN. C<OPTION> takes on one of the following values:
  875. ODBC_ADD_DSN.......Adds a new DSN.
  876. ODBC_MODIFY_DSN....Modifies an existing DSN.
  877. ODBC_REMOVE_DSN....Removes an existing DSN.
  878. ODBC_ADD_SYS_DSN.......Adds a new System DSN.
  879. ODBC_MODIFY_SYS_DSN....Modifies an existing System DSN.
  880. ODBC_REMOVE_SYS_DSN....Removes an existing System DSN.
  881. You must specify the driver C<DRIVER> (which can be retrieved by using
  882. C<DataSources> or C<Drivers>).
  883. C<ATTRIBUTE1> B<should> be I<"DSN=xxx"> where I<xxx> is the name of
  884. the DSN. Other attributes can be any DSN attribute such as:
  885. "UID=Cow"
  886. "PWD=Moo"
  887. "Description=My little bitty Data Source Name"
  888. Returns I<true> on success, I<false> on failure.
  889. B<NOTE 1>: If you use C<ODBC_ADD_DSN>, then you must include at least
  890. I<"DSN=xxx"> and the location of the database.
  891. Example: For MS Access databases, you must specify the
  892. I<DatabaseQualifier>:
  893. "DBQ=c:\\...\\MyDatabase.mdb"
  894. B<NOTE 2>: If you use C<ODBC_MODIFY_DSN>, then you need only specify
  895. the I<"DNS=xxx"> attribute. Any other attribute you include will be
  896. changed to what you specify.
  897. B<NOTE 3>: If you use C<ODBC_REMOVE_DSN>, then you need only specify
  898. the I<"DSN=xxx"> attribute.
  899. =item Connection ()
  900. Returns the connection number associated with the ODBC connection.
  901. =item Close ()
  902. Closes the ODBC connection. No return value.
  903. =item Data ( [ FIELD_NAME ] )
  904. Returns the contents of column name C<FIELD_NAME> or the current row
  905. (if nothing is specified).
  906. =item DataHash ( [ FIELD1, FIELD2, ... ] )
  907. Returns the contents for C<FIELD1, FIELD2, ...> or the entire row (if
  908. nothing is specified) as an associative array consisting of:
  909. {Field Name} => Field Data
  910. =item DataSources ()
  911. Returns an associative array of Data Sources and ODBC remarks about them.
  912. They are returned in the form of:
  913. $ArrayName{'DSN'}=Driver
  914. where I<DSN> is the Data Source Name and ODBC Driver used.
  915. =item Debug ( [ 1 | 0 ] )
  916. Sets the debug option to on or off. If nothing is specified, then
  917. nothing is changed.
  918. Returns the debugging value (I<1> or I<0>).
  919. =item Drivers ()
  920. Returns an associative array of ODBC Drivers and their attributes.
  921. They are returned in the form of:
  922. $ArrayName{'DRIVER'}=Attrib1;Attrib2;Attrib3;...
  923. where I<DRIVER> is the ODBC Driver Name and I<AttribX> are the
  924. driver-defined attributes.
  925. =item DropCursor ( [ CLOSE_TYPE ] )
  926. Drops the cursor associated with the ODBC object. This forces the
  927. cursor to be deallocated. This overrides C<SetStmtCloseType>, but the
  928. ODBC object does not lose the C<StmtCloseType> setting. C<CLOSE_TYPE>
  929. can be any valid C<SmtpCloseType> and will perform a close on the stmt
  930. using the specified close type.
  931. Returns I<true> on success, I<false> on failure.
  932. =item DumpData ()
  933. Dumps to the screen the fieldnames and all records of the current data
  934. set. Used primarily for debugging. No return value.
  935. =item Error ()
  936. Returns the last encountered error. The returned value is context
  937. dependent:
  938. If called in a I<scalar> context, then a I<3-element array> is
  939. returned:
  940. ( ERROR_NUMBER, ERROR_TEXT, CONNECTION_NUMBER )
  941. If called in a I<string> context, then a I<string> is returned:
  942. "[ERROR_NUMBER] [CONNECTION_NUMBER] [ERROR_TEXT]"
  943. If debugging is on then two more variables are returned:
  944. ( ..., FUNCTION, LEVEL )
  945. where C<FUNCTION> is the name of the function in which the error
  946. occurred, and C<LEVEL> represents extra information about the error
  947. (usually the location of the error).
  948. =item FetchRow ( [ ROW [, TYPE ] ] )
  949. Retrieves the next record from the keyset. When C<ROW> and/or C<TYPE>
  950. are specified, the call is made using C<SQLExtendedFetch> instead of
  951. C<SQLFetch>.
  952. B<NOTE 1>: If you are unaware of C<SQLExtendedFetch> and its
  953. implications, stay with just regular C<FetchRow> with no parameters.
  954. B<NOTE 2>: The ODBC API explicitly warns against mixing calls to
  955. C<SQLFetch> and C<SQLExtendedFetch>; use one or the other but not
  956. both.
  957. If I<ROW> is specified, it moves the keyset B<RELATIVE> C<ROW> number
  958. of rows.
  959. If I<ROW> is specified and C<TYPE> is B<not>, then the type used is
  960. B<RELATIVE>.
  961. Returns I<true> when another record is available to read, and I<false>
  962. when there are no more records.
  963. =item FieldNames ()
  964. Returns an array of fieldnames found in the current data set. There is
  965. no guarantee on order.
  966. =item GetConnections ()
  967. Returns an array of connection numbers showing what connections are
  968. currently open.
  969. =item GetConnectOption ( OPTION )
  970. Returns the value of the specified connect option C<OPTION>. Refer to
  971. ODBC documentation for more information on the options and values.
  972. Returns a string or scalar depending upon the option specified.
  973. =item GetCursorName ()
  974. Returns the name of the current cursor as a string or I<undef>.
  975. =item GetData ()
  976. Retrieves the current row from the dataset. This is not generally
  977. used by users; it is used internally.
  978. Returns an array of field data where the first element is either
  979. I<false> (if successful) and I<true> (if B<not> successful).
  980. =item getDSN ( [ DSN ] )
  981. Returns an associative array indicating the configuration for the
  982. specified DSN.
  983. If no DSN is specified then the current connection is used.
  984. The returned associative array consists of:
  985. keys=DSN keyword; values=Keyword value. $Data{$Keyword}=Value
  986. =item GetFunctions ( [ FUNCTION1, FUNCTION2, ... ] )
  987. Returns an associative array indicating the ability of the ODBC Driver
  988. to support the specified functions. If no functions are specified,
  989. then a 100 element associative array is returned containing all
  990. possible functions and their values.
  991. C<FUNCTION> must be in the form of an ODBC API constant like
  992. C<SQL_API_SQLTRANSACT>.
  993. The returned array will contain the results like:
  994. $Results{SQL_API_SQLTRANSACT}=Value
  995. Example:
  996. $Results = $O->GetFunctions(
  997. $O->SQL_API_SQLTRANSACT,
  998. SQL_API_SQLSETCONNECTOPTION
  999. );
  1000. $ConnectOption = $Results{SQL_API_SQLSETCONNECTOPTION};
  1001. $Transact = $Results{SQL_API_SQLTRANSACT};
  1002. =item GetInfo ( OPTION )
  1003. Returns a string indicating the value of the particular
  1004. option specified.
  1005. =item GetMaxBufSize ()
  1006. Returns the current allocated limit for I<MaxBufSize>. For more info,
  1007. see C<SetMaxBufSize>.
  1008. =item GetSQLState () *
  1009. Returns a string indicating the SQL state as reported by ODBC. The SQL
  1010. state is a code that the ODBC Manager or ODBC Driver returns after the
  1011. execution of a SQL function. This is helpful for debugging purposes.
  1012. =item GetStmtCloseType ( [ CONNECTION ] )
  1013. Returns a string indicating the type of closure that will be used
  1014. everytime the I<hstmt> is freed. See C<SetStmtCloseType> for details.
  1015. By default, the connection of the current object will be used. If
  1016. C<CONNECTION> is a valid connection number, then it will be used.
  1017. =item GetStmtOption ( OPTION )
  1018. Returns the value of the specified statement option C<OPTION>. Refer
  1019. to ODBC documentation for more information on the options and values.
  1020. Returns a string or scalar depending upon the option specified.
  1021. =item MoreResults ()
  1022. This will report whether there is data yet to be retrieved from the
  1023. query. This can happen if the query was a multiple select.
  1024. Example:
  1025. "SELECT * FROM [foo] SELECT * FROM [bar]"
  1026. B<NOTE>: Not all drivers support this.
  1027. Returns I<1> if there is more data, I<undef> otherwise.
  1028. =item RowCount ( CONNECTION )
  1029. For I<UPDATE>, I<INSERT> and I<DELETE> statements, the returned value
  1030. is the number of rows affected by the request or I<-1> if the number
  1031. of affected rows is not available.
  1032. B<NOTE 1>: This function is not supported by all ODBC drivers! Some
  1033. drivers do support this but not for all statements (e.g., it is
  1034. supported for I<UPDATE>, I<INSERT> and I<DELETE> commands but not for
  1035. the I<SELECT> command).
  1036. B<NOTE 2>: Many data sources cannot return the number of rows in a
  1037. result set before fetching them; for maximum interoperability,
  1038. applications should not rely on this behavior.
  1039. Returns the number of affected rows, or I<-1> if not supported by the
  1040. driver in the current context.
  1041. =item Run ( SQL )
  1042. Executes the SQL command B<SQL> and dumps to the screen info about
  1043. it. Used primarily for debugging.
  1044. No return value.
  1045. =item SetConnectOption ( OPTION ) *
  1046. Sets the value of the specified connect option B<OPTION>. Refer to
  1047. ODBC documentation for more information on the options and values.
  1048. Returns I<true> on success, I<false> otherwise.
  1049. =item SetCursorName ( NAME ) *
  1050. Sets the name of the current cursor.
  1051. Returns I<true> on success, I<false> otherwise.
  1052. =item SetPos ( ROW [, OPTION, LOCK ] ) *
  1053. Moves the cursor to the row C<ROW> within the current keyset (B<not>
  1054. the current data/result set).
  1055. Returns I<true> on success, I<false> otherwise.
  1056. =item SetMaxBufSize ( SIZE )
  1057. This sets the I<MaxBufSize> for a particular connection. This will
  1058. most likely never be needed but...
  1059. The amount of memory that is allocated to retrieve the field data of a
  1060. record is dynamic and changes when it need to be larger. I found that
  1061. a memo field in an MS Access database ended up requesting 4 Gig of
  1062. space. This was a bit much so there is an imposed limit (2,147,483,647
  1063. bytes) that can be allocated for data retrieval.
  1064. Since it is possible that someone has a database with field data
  1065. greater than 10,240, you can use this function to increase the limit
  1066. up to a ceiling of 2,147,483,647 (recompile if you need more).
  1067. Returns the max number of bytes.
  1068. =item SetStmtCloseType ( TYPE [, CONNECTION ] )
  1069. Sets a particular I<hstmt> close type for the connection. This is the
  1070. same as C<ODBCFreeStmt(hstmt, TYPE)>. By default, the connection of
  1071. the current object will be used. If C<CONNECTION> is a valid
  1072. connection number, then it will be used.
  1073. C<TYPE> may be one of:
  1074. SQL_CLOSE
  1075. SQL_DROP
  1076. SQL_UNBIND
  1077. SQL_RESET_PARAMS
  1078. Returns a string indicating the newly set type.
  1079. =item SetStmtOption ( OPTION ) *
  1080. Sets the value of the specified statement option C<OPTION>. Refer to
  1081. ODBC documentation for more information on the options and values.
  1082. Returns I<true> on success, I<false> otherwise.
  1083. =item ShutDown ()
  1084. Closes the ODBC connection and dumps to the screen info about
  1085. it. Used primarily for debugging.
  1086. No return value.
  1087. =item Sql ( SQL_STRING )
  1088. Executes the SQL command C<SQL_STRING> on the current connection.
  1089. Returns I<?> on success, or an error number on failure.
  1090. =item TableList ( QUALIFIER, OWNER, NAME, TYPE )
  1091. Returns the catalog of tables that are available in the DSN. For an
  1092. unknown parameter, just specify the empty string I<"">.
  1093. Returns an array of table names.
  1094. =item Transact ( TYPE ) *
  1095. Forces the ODBC connection to perform a I<rollback> or I<commit>
  1096. transaction.
  1097. C<TYPE> may be one of:
  1098. SQL_COMMIT
  1099. SQL_ROLLBACK
  1100. B<NOTE>: This only works with ODBC drivers that support transactions.
  1101. Your driver supports it if I<true> is returned from:
  1102. $O->GetFunctions($O->SQL_API_SQLTRANSACT)[1]
  1103. (See C<GetFunctions> for more details.)
  1104. Returns I<true> on success, I<false> otherwise.
  1105. =item Version ( PACKAGES )
  1106. Returns an array of version numbers for the requested packages
  1107. (F<ODBC.pm> or F<ODBC.PLL>). If the list C<PACKAGES> is empty, then
  1108. all version numbers are returned.
  1109. =back
  1110. =head1 LIMITATIONS
  1111. What known problems does this thing have?
  1112. =over
  1113. =item *
  1114. If the account under which the process runs does not have write
  1115. permission on the default directory (for the process, not the ODBC
  1116. DSN), you will probably get a runtime error during a
  1117. C<SQLConnection>. I don't think that this is a problem with the code,
  1118. but more like a problem with ODBC. This happens because some ODBC
  1119. drivers need to write a temporary file. I noticed this using the MS
  1120. Jet Engine (Access Driver).
  1121. =item *
  1122. This module has been neither optimized for speed nor optimized for
  1123. memory consumption.
  1124. =back
  1125. =head1 INSTALLATION NOTES
  1126. If you wish to use this module with a build of Perl other than
  1127. ActivePerl, you may wish to fetch the original source distribution for
  1128. this module at:
  1129. ftp://ftp.roth.net:/pub/ntperl/ODBC/970208/Bin/Win32_ODBC_Build_CORE.zip
  1130. or one of the other archives at that same location. See the included
  1131. README for hints on installing this module manually, what to do if you
  1132. get a I<parse exception>, and a pointer to a test script for this
  1133. module.
  1134. =head1 OTHER DOCUMENTATION
  1135. Find a FAQ for Win32::ODBC at:
  1136. http://www.roth.net/odbc/odbcfaq.htm
  1137. =head1 AUTHOR
  1138. Dave Roth <[email protected]>
  1139. =head1 CREDITS
  1140. Based on original code by Dan DeMaggio <[email protected]>
  1141. =head1 DISCLAIMER
  1142. I do not guarantee B<ANYTHING> with this package. If you use it you
  1143. are doing so B<AT YOUR OWN RISK>! I may or may not support this
  1144. depending on my time schedule.
  1145. =head1 HISTORY
  1146. Last Modified 1999.09.25.
  1147. =head1 COPYRIGHT
  1148. Copyright (c) 1996-1998 Dave Roth. All rights reserved.
  1149. Courtesy of Roth Consulting: http://www.roth.net/consult/
  1150. Use under GNU General Public License. Details can be found at:
  1151. http://www.gnu.org/copyleft/gpl.html
  1152. =cut