home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _ed7369b4731ecc9237887534e1c5e08d < prev    next >
Text File  |  2000-03-23  |  12KB  |  296 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Sybase::BCP - Simple front end to the Sybase BCP API</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> Sybase::BCP - Simple front end to the Sybase BCP API</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <UL>
  26.  
  27.         <LI><A HREF="#features">Features</A></LI>
  28.         <LI><A HREF="#the following methods are available:">The following methods are available:</A></LI>
  29.         <LI><A HREF="#configuration parameters">Configuration Parameters</A></LI>
  30.         <LI><A HREF="#paramaters for config()">Paramaters for <CODE>config()</CODE></A></LI>
  31.         <LI><A HREF="#parameters for describe()">Parameters for <CODE>describe()</CODE></A></LI>
  32.     </UL>
  33.  
  34.     <LI><A HREF="#examples">EXAMPLES</A></LI>
  35.     <LI><A HREF="#bugs">BUGS</A></LI>
  36.     <LI><A HREF="#author">AUTHOR</A></LI>
  37. </UL>
  38. <!-- INDEX END -->
  39.  
  40. <HR>
  41. <P>
  42. <H1><A NAME="name">NAME</A></H1>
  43. <P>Sybase::BCP - Simple front end to the Sybase BCP API</P>
  44. <P>
  45. <HR>
  46. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  47. <UL>
  48. <LI>Windows</LI>
  49. </UL>
  50. <HR>
  51. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  52. <PRE>
  53.     use Sybase::BCP;</PRE>
  54. <PRE>
  55.     $bcp = new Sybase::BCP ...;
  56.     $bcp->config(...);
  57.     $bcp->run;</PRE>
  58. <P>
  59. <HR>
  60. <H1><A NAME="description">DESCRIPTION</A></H1>
  61. <P>The Sybase::BCP module serves as a simplified front end for Sybase's Bulk
  62. Copy library. It is sub-classed from the <A HREF="../../../site/lib/Sybase/DBlib.html">the Sybase::DBlib manpage</A> module, so all the
  63. features of the Sybase::DBlib module are available in addition to the
  64. specific Sybase::BCP methods.</P>
  65. <P>So how does it work?</P>
  66. <P>Let's say we want to copy the contents of a file name 'foo.bcp' into the
  67. table 'mydb.dbo.bar'. The fields in the file are separated by a '|'.</P>
  68. <PRE>
  69.     #!/usr/local/bin/perl</PRE>
  70. <PRE>
  71.     use Sybase::BCP;</PRE>
  72. <PRE>
  73.     $bcp = new Sybase::BCP $user, $passwd;
  74.     $bcp->config(INPUT => 'foo.bcp',
  75.                  OUTPUT => 'mydb.dbo.bar',
  76.                  SEPARATOR => '|');
  77.     $bcp->run;</PRE>
  78. <P>That's it!</P>
  79. <P>Of course, there are several things you can do to cater for non-standard
  80. input files (see <STRONG>Configuration Parameters</STRONG>, below).</P>
  81. <P>
  82. <H2><A NAME="features">Features</A></H2>
  83. <UL>
  84. <LI><STRONG><A NAME="item_Automatic_conversions_from_non%2Dstandard_date_for">Automatic conversions from non-standard date formats.</A></STRONG><BR>
  85.  
  86. <LI><STRONG><A NAME="item_Automatic_retries_of_failed_batches%2E">Automatic retries of failed batches.</A></STRONG><BR>
  87.  
  88. If there are errors in the input file, or if there are duplicat rows that are
  89. rejected, the invalid rows are stored in an error log file, and the batch is
  90. retried, so that only the failed rows are not uploaded.
  91. <P></P>
  92. <LI><STRONG><A NAME="item_Handles_column_reordering_and%2For_skipping_of_unn">Handles column reordering and/or skipping of unneeded data.</A></STRONG><BR>
  93.  
  94. <LI><STRONG><A NAME="item_Row_or_column_based_callbacks%2E">Row or column based callbacks.</A></STRONG><BR>
  95.  
  96. Allows vetoing of rows, or arbitrary processing of data on input.
  97. <P></P></UL>
  98. <P>
  99. <H2><A NAME="the following methods are available:">The following methods are available:</A></H2>
  100. <DL>
  101. <DT><STRONG><A NAME="item_%24bcp_%3D_new_Sybase%3A%3ABCP_%5B%24user_%5B%2C_%">$bcp = new Sybase::BCP [$user [, $password [, $server [, $appname]]]]</A></STRONG><BR>
  102. <DD>
  103. Allocate a new <STRONG>BCP</STRONG> handle. Opens a new connection to Sybase via the
  104. <STRONG>Sybase::DBlib</STRONG> module, and enables BCP IN on this handle.
  105. <P></P>
  106. <DT><STRONG><A NAME="item_config">$bcp-><CODE>config([parameters])</CODE></A></STRONG><BR>
  107. <DD>
  108. Sets up the Bulk Copy operation. See <STRONG>Configuration Parameters</STRONG> below for
  109. details.
  110. <P></P>
  111. <DT><STRONG><A NAME="item_describe">$bcp->describe($colid, {parameters})</A></STRONG><BR>
  112. <DD>
  113. Adds a specific configuration element for column $colid. Columns are numbered
  114. starting at 1, as is standard in the Sybase APIs.
  115. <P></P>
  116. <DT><STRONG><A NAME="item_run">$bcp->run</A></STRONG><BR>
  117. <DD>
  118. Perform the <STRONG>BCP</STRONG> operation, returns the actual number of rows sent to the
  119. server.
  120. <P></P></DL>
  121. <P>
  122. <H2><A NAME="configuration parameters">Configuration Parameters</A></H2>
  123. <P>The general form for configuration is to pass (parameter => value) pairs
  124. via the <A HREF="#item_config"><CODE>config()</CODE></A> or <A HREF="#item_describe"><CODE>describe()</CODE></A> methods. Some parameters take slightly more
  125. complex arguments (see <STRONG>REORDER</STRONG>).</P>
  126. <P>
  127. <H2><A NAME="paramaters for config()">Paramaters for <A HREF="#item_config"><CODE>config()</CODE></A></A></H2>
  128. <DL>
  129. <DT><STRONG><A NAME="item_DIRECTION">DIRECTION</A></STRONG><BR>
  130. <DD>
  131. The direction in which the bulkcopy operation is done. Can be 'IN' or 'OUT'.
  132. Default: 'IN' (<EM>Note:</EM> 'OUT' is not implemented yet.)
  133. <P></P>
  134. <DT><STRONG><A NAME="item_INPUT">INPUT</A></STRONG><BR>
  135. <DD>
  136. Where <STRONG>BCP</STRONG> should take it's input from. It's a filename for <STRONG>bcp IN</STRONG>, it's
  137. a table name for <STRONG>bcp OUT</STRONG>.
  138. <P>For <STRONG>bcp IN</STRONG> <STRONG>INPUT</STRONG> can also be a reference to a perl subroutine that
  139. returns the array to be inserted via bcp_sendrow().</P>
  140. <P></P>
  141. <DT><STRONG><A NAME="item_OUTPUT">OUTPUT</A></STRONG><BR>
  142. <DD>
  143. Where <STRONG>BCP</STRONG> should place it's output. It's a table name for <STRONG>bcp IN</STRONG>, a
  144. filename for <STRONG>bcp OUT</STRONG>.
  145. <P></P>
  146. <DT><STRONG><A NAME="item_ERRORS">ERRORS</A></STRONG><BR>
  147. <DD>
  148. The file where invalid rows should be recorded. Default: bcp.err.
  149. <P></P>
  150. <DT><STRONG><A NAME="item_SEPARATOR">SEPARATOR</A></STRONG><BR>
  151. <DD>
  152. The pattern that separates fields in the input file, or that should be used
  153. to separate fields in the output file. Default: TAB.
  154. <P></P>
  155. <DT><STRONG><A NAME="item_RECORD_SEPARATOR">RECORD_SEPARATOR</A></STRONG><BR>
  156. <DD>
  157. The pattern that separates records (rows) in the input file. Sybase:BCP will
  158. set a local copy of $\ to this value before reading the file. Default: NEWLINE.
  159. <P></P>
  160. <DT><STRONG><A NAME="item_FIELDS">FIELDS</A></STRONG><BR>
  161. <DD>
  162. Number of fields in the input file for <STRONG>bcp IN</STRONG> operations. Default: Number
  163. of fields found in the first line. This parameter is ignored for <STRONG>bcp OUT</STRONG>.
  164. <P></P>
  165. <DT><STRONG><A NAME="item_BATCH_SIZE">BATCH_SIZE</A></STRONG><BR>
  166. <DD>
  167. Number of rows to be batched together before committing to the server for
  168. <STRONG>bcp IN</STRONG> operations. Defaults to 100. If there is a risk that retries could
  169. be requiered due to failed batches (e.g. duplicat rows/keys errors) then
  170. you should not use a large batch size: one failed row in a batch requires
  171. the entire batch to be resent.
  172. <P></P>
  173. <DT><STRONG><A NAME="item_NULL">NULL</A></STRONG><BR>
  174. <DD>
  175. A pattern to be used to detect NULL values in the input file. Defaults to
  176. a zero length string.
  177. <P></P>
  178. <DT><STRONG><A NAME="item_DATE">DATE</A></STRONG><BR>
  179. <DD>
  180. The default format for DATE fields in the input file. The parameter should
  181. be a symbolic value representing the format. Currently, the following values
  182. are recognized: CTIME (the Unix <CODE>ctime(3)</CODE> format), or the numbers 0-12,
  183. 100-112, corresponding to the conversion formats defined in table 2-4 of
  184. the <EM>SQL Server Reference Manual</EM>.
  185. <P><STRONG>BCP</STRONG> detects <EM>datetime</EM> targets by looking up the target table
  186. structure in the Sybase system tables.</P>
  187. <P></P>
  188. <DT><STRONG><A NAME="item_REORDER">REORDER</A></STRONG><BR>
  189. <DD>
  190. The ordering of the fields in the input file does not correspond to the
  191. order of columns in the table, or there are columns that you wish to
  192. skip. The REORDER parameter takes a hash that describes the reordering
  193. operation:
  194. <PRE>
  195.     $bcp->config(...
  196.                  REORDER => { 1 => 2,
  197.                               3 => 1,
  198.                               2 => 'foobar',
  199.                               12 => 4},
  200.                  ...);</PRE>
  201. <P>In this example, field 1 of the input file goes in column 2 of the table,
  202. field 3 goes in column 1, field 2 goes in the column named <EM>foobar</EM>, and
  203. field 12 goes in column 4. Fields 4-11, and anything beyond 12 is skipped.
  204. As you can see you can use the column <EM>name</EM> instead of its position.
  205. The default is to not do any reordering.</P>
  206. <P></P>
  207. <DT><STRONG><A NAME="item_CALLBACK">CALLBACK</A></STRONG><BR>
  208. <DD>
  209. The callback subroutine is called for each row (after any reordering), and
  210. allows the user to do global processing on the row, or vetoing it's
  211. processing. Example:
  212. <PRE>
  213.     $bcp->config(...
  214.                  CALLBACK => \&row_cb,
  215.                  ...);</PRE>
  216. <PRE>
  217.     sub row_cb {
  218.         my $row_ref = shift;</PRE>
  219. <PRE>
  220.         # Skip rows where the first field starts with FOO:
  221.         return undef if $$row_ref[0] =~ /^FOO/;</PRE>
  222. <PRE>
  223.         1;
  224.     }</PRE>
  225. <P></P>
  226. <DT><STRONG><A NAME="item_CONDITION">CONDITION</A></STRONG><BR>
  227. <DD>
  228. A <EM>where</EM> clause to be used in <STRONG>bcp OUT</STRONG> operations. Not implemented.
  229. <P></P></DL>
  230. <P>
  231. <H2><A NAME="parameters for describe()">Parameters for <A HREF="#item_describe"><CODE>describe()</CODE></A></A></H2>
  232. <DL>
  233. <DT><STRONG>CALLBACK</STRONG><BR>
  234. <DD>
  235. Specify a callback for this column. The field value is passed as the first
  236. parameter, and the callback should return the value that it wants <STRONG>BCP</STRONG>
  237. to use. Example:
  238. <PRE>
  239.     $dbh->describe(2, {CALLBACK, \&col_cb});</PRE>
  240. <PRE>
  241.     sub col_cb {
  242.         my $data = shift;</PRE>
  243. <PRE>
  244.         # Convert to lower case...
  245.         $data =~ tr/A-Z/a-z/;
  246.     }</PRE>
  247. <P></P>
  248. <DT><STRONG><A NAME="item_SKIP">SKIP</A></STRONG><BR>
  249. <DD>
  250. If this is defined then this field is skipped. This is useful if only one or
  251. two fields need to be skipped and you don't want to define a big REORDER hash
  252. to handle the skipping.
  253. <P></P></DL>
  254. <P>
  255. <HR>
  256. <H1><A NAME="examples">EXAMPLES</A></H1>
  257. <PRE>
  258.     #!/usr/local/bin/perl
  259. </PRE>
  260. <PRE>
  261.  
  262.     use Sybase::BCP;
  263.     require 'sybutil.pl';</PRE>
  264. <PRE>
  265.     $bcp = new Sybase::BCP sa, undef, TROLL;</PRE>
  266. <PRE>
  267.     $bcp->config(INPUT => '../../Sybperl/xab',
  268.                  OUTPUT => 'excalibur.dbo.t3',
  269.                  BATCH_SIZE => 200,
  270.                  FIELDS => 4,
  271.                  REORDER => {1 => 'account',
  272.                              3 => 'date',
  273.                              2 => 'seq_no',
  274.                              11 => 'broker'},
  275.                  SEPARATOR => '|');
  276.     $bcp->run;</PRE>
  277. <P>
  278. <HR>
  279. <H1><A NAME="bugs">BUGS</A></H1>
  280. <P>The current implementation seems to run about 2.5 to 3 times slower than
  281. plain bcp.</P>
  282. <P>
  283. <HR>
  284. <H1><A NAME="author">AUTHOR</A></H1>
  285. <P>Michael Peppler <EM><<A HREF="mailto:mpeppler@mbay.net">mpeppler@mbay.net</A>></EM>. Contact the sybperl mailing
  286. list <CODE>mailto:sybperl-l@trln.lib.unc.edu</CODE> if you have any questions.</P>
  287. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  288. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  289. <STRONG><P CLASS=block> Sybase::BCP - Simple front end to the Sybase BCP API</P></STRONG>
  290. </TD></TR>
  291. </TABLE>
  292.  
  293. </BODY>
  294.  
  295. </HTML>
  296.