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

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>SQL::Statement - SQL parsing and processing engine</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> SQL::Statement - SQL parsing and processing engine</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="#creating a parser object">Creating a parser object</A></LI>
  28.         <LI><A HREF="#parsing a query">Parsing a query</A></LI>
  29.         <LI><A HREF="#retrieving query information">Retrieving query information</A></LI>
  30.         <LI><A HREF="#evaluating a where clause">Evaluating a WHERE clause</A></LI>
  31.         <LI><A HREF="#evaluating queries">Evaluating queries</A></LI>
  32.     </UL>
  33.  
  34.     <LI><A HREF="#sql syntax">SQL syntax</A></LI>
  35.     <UL>
  36.  
  37.         <LI><A HREF="#create">CREATE</A></LI>
  38.         <LI><A HREF="#drop">DROP</A></LI>
  39.         <LI><A HREF="#insert">INSERT</A></LI>
  40.         <LI><A HREF="#delete">DELETE</A></LI>
  41.         <LI><A HREF="#update">UPDATE</A></LI>
  42.         <LI><A HREF="#select">SELECT</A></LI>
  43.     </UL>
  44.  
  45.     <LI><A HREF="#installation">INSTALLATION</A></LI>
  46.     <LI><A HREF="#internals">INTERNALS</A></LI>
  47.     <UL>
  48.  
  49.         <LI><A HREF="#perlindependent c part">Perl-independent C part</A></LI>
  50.         <LI><A HREF="#perldependent c part">Perl-dependent C part</A></LI>
  51.         <LI><A HREF="#perl part">Perl part</A></LI>
  52.         <LI><A HREF="#the sql_stmt_t structure">The sql_stmt_t structure</A></LI>
  53.         <LI><A HREF="#the where clause evaluation">The WHERE clause evaluation</A></LI>
  54.         <LI><A HREF="#features">Features</A></LI>
  55.     </UL>
  56.  
  57.     <LI><A HREF="#multithreading">MULTITHREADING</A></LI>
  58.     <LI><A HREF="#author and copyright">AUTHOR AND COPYRIGHT</A></LI>
  59.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  60. </UL>
  61. <!-- INDEX END -->
  62.  
  63. <HR>
  64. <P>
  65. <H1><A NAME="name">NAME</A></H1>
  66. <P>SQL::Statement - SQL parsing and processing engine</P>
  67. <P>
  68. <HR>
  69. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  70. <UL>
  71. <LI>Linux</LI>
  72. <LI>Solaris</LI>
  73. <LI>Windows</LI>
  74. </UL>
  75. <HR>
  76. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  77. <PRE>
  78.     require SQL::Statement;</PRE>
  79. <PRE>
  80.     # Create a parser
  81.     my($parser) = SQL::Statement->new('Ansi');</PRE>
  82. <PRE>
  83.     # Parse an SQL statement
  84.     $@ = '';
  85.     my ($stmt) = eval {
  86.         SQL::Statement->new("SELECT id, name FROM foo WHERE id > 1",
  87.                             $parser);
  88.     };
  89.     if ($@) {
  90.         die "Cannot parse statement: $@";
  91.     }</PRE>
  92. <PRE>
  93.     # Query the list of result columns;
  94.     my $numColums = $stmt->columns();  # Scalar context
  95.     my @columns = $stmt->columns();    # Array context
  96.     # @columns now contains SQL::Statement::Column instances</PRE>
  97. <PRE>
  98.     # Likewise, query the tables being used in the statement:
  99.     my $numTables = $stmt->tables();   # Scalar context
  100.     my @tables = $stmt->tables();      # Array context
  101.     # @tables now contains SQL::Statement::Table instances</PRE>
  102. <PRE>
  103.     # Query the WHERE clause; this will retrieve an
  104.     # SQL::Statement::Op instance
  105.     my $where = $stmt->where();</PRE>
  106. <PRE>
  107.     # Evaluate the WHERE clause with concrete data, represented
  108.     # by an SQL::Eval object
  109.     my $result = $stmt->eval_where($eval);</PRE>
  110. <PRE>
  111.     # Execute a statement:
  112.     $stmt->execute($data, $params);</PRE>
  113. <P>
  114. <HR>
  115. <H1><A NAME="description">DESCRIPTION</A></H1>
  116. <P>For installing the module, see <A HREF="#installation">INSTALLATION</A> below.</P>
  117. <P>The SQL::Statement module implements a small, abstract SQL engine. This
  118. module is not usefull itself, but as a base class for deriving concrete
  119. SQL engines. The implementation is designed to work fine with the
  120. DBI driver DBD::CSV, thus probably not so well suited for a larger
  121. environment, but I'd hope it is extendable without too much problems.</P>
  122. <P>By parsing an SQL query you create an SQL::Statement instance. This
  123. instance offers methods for retrieving syntax, for WHERE clause and
  124. statement evaluation.</P>
  125. <P>
  126. <H2><A NAME="creating a parser object">Creating a parser object</A></H2>
  127. <P>What's accepted as valid SQL, depends on the parser object. There is
  128. a set of so-called features that the parsers may have or not. Usually
  129. you start with a builtin parser:</P>
  130. <PRE>
  131.     my $parser = SQL::Parser->new($name, [ \%attr ]);</PRE>
  132. <P>Currently two parsers are builtin: The <EM>Ansi</EM> parser implements a proper
  133. subset of ANSI SQL. (At least I hope so. :-) The <EM>SQL::Statement</EM> parser
  134. is used by the DBD:CSV driver.</P>
  135. <P>You can query or set individual features. Currently available are:</P>
  136. <DL>
  137. <DT><STRONG><A NAME="item_create%2Etype_blob">create.type_blob</A></STRONG><BR>
  138. <DD>
  139. <DT><STRONG><A NAME="item_create%2Etype_real">create.type_real</A></STRONG><BR>
  140. <DD>
  141. <DT><STRONG><A NAME="item_create%2Etype_text">create.type_text</A></STRONG><BR>
  142. <DD>
  143. These enable the respective column types in a <EM>CREATE TABLE</EM> clause.
  144. They are all disabled in the <EM>Ansi</EM> parser, but enabled in the
  145. <EM>SQL::Statement</EM> parser. Example:
  146. <P></P>
  147. <DT><STRONG><A NAME="item_select%2Ejoin">select.join</A></STRONG><BR>
  148. <DD>
  149. This enables the use of multiple tables in a SELECT statement, for
  150. example
  151. <PRE>
  152.   SELECT a.id, b.name FROM a, b WHERE a.id = b.id AND a.id = 2</PRE>
  153. <P></P></DL>
  154. <P>To enable or disable a feature, for example <EM>select.join</EM>, use the
  155. following:</P>
  156. <PRE>
  157.   # Enable feature
  158.   $parser->feature("select", "join", 1);
  159.   # Disable feature
  160.   $parser->feature("select", "join", 0);</PRE>
  161. <P>Of course you can query features:</P>
  162. <PRE>
  163.   # Query feature
  164.   my $haveSelectJoin = $parser->feature("select", "join");</PRE>
  165. <P>The <CODE>new</CODE> method allows a shorthand for setting features. For example,
  166. the following is equivalent to the <EM>SQL::Statement</EM> parser:</P>
  167. <PRE>
  168.   $parser = SQL::Statement->new('Ansi',
  169.                                 { 'create' => { 'type_text' => 1,
  170.                                                 'type_real' => 1,
  171.                                                 'type_blob' => 1 },
  172.                                   'select' => { 'join' => 0 }});</PRE>
  173. <P>
  174. <H2><A NAME="parsing a query">Parsing a query</A></H2>
  175. <P>A statement can be parsed with</P>
  176. <PRE>
  177.     my $stmt = SQL::Statement->new($query, $parser);</PRE>
  178. <P>In case of syntax errors or other problems, the method throws a Perl
  179. exception. Thus, if you want to catch exceptions, the above becomes</P>
  180. <PRE>
  181.     $@ = '';
  182.     my $stmt = eval { SQL::Statement->new($query, $parser) };
  183.     if ($@) { print "An error occurred: $@"; }</PRE>
  184. <P>The accepted SQL syntax is restricted, though easily extendable. See
  185. <A HREF="#sql syntax">SQL syntax</A> below. See <A HREF="#creating a parser object">Creating a parser object</A> above.</P>
  186. <P>
  187. <H2><A NAME="retrieving query information">Retrieving query information</A></H2>
  188. <P>The following methods can be used to obtain information about a
  189. query:</P>
  190. <DL>
  191. <DT><STRONG><A NAME="item_command">command</A></STRONG><BR>
  192. <DD>
  193. Returns the SQL command, currently one of <EM>SELECT</EM>, <EM>INSERT</EM>, <EM>UPDATE</EM>,
  194. <EM>DELETE</EM>, <EM>CREATE</EM> or <EM>DROP</EM>, the last two referring to
  195. <EM>CREATE TABLE</EM> and <EM>DROP TABLE</EM>. See <A HREF="#sql syntax">SQL syntax</A> below. Example:
  196. <PRE>
  197.     my $command = $stmt->command();</PRE>
  198. <P></P>
  199. <DT><STRONG><A NAME="item_columns">columns</A></STRONG><BR>
  200. <DD>
  201. <PRE>
  202.     my $numColumns = $stmt->columns();  # Scalar context
  203.     my @columnList = $stmt->columns();  # Array context
  204.     my($col1, $col2) = ($stmt->columns(0), $stmt->columns(1));</PRE>
  205. <P>This method is used to retrieve column lists. The meaning depends on
  206. the query command:</P>
  207. <PRE>
  208.     SELECT $col1, $col2, ... $colN FROM $table WHERE ...
  209.     UPDATE $table SET $col1 = $val1, $col2 = $val2, ...
  210.         $colN = $valN WHERE ...
  211.     INSERT INTO $table ($col1, $col2, ..., $colN) VALUES (...)</PRE>
  212. <P>When used without arguments, the method returns a list of the
  213. columns $col1, $col2, ..., $colN, you may alternatively use a
  214. column number as argument. Note that the column list may be
  215. empty, like in</P>
  216. <PRE>
  217.     INSERT INTO $table VALUES (...)</PRE>
  218. <P>and in <EM>CREATE</EM> or <EM>DROP</EM> statements.</P>
  219. <P>But what does ``returning a column'' mean? It is returning an
  220. SQL::Statement::Column instance, a class that implements the
  221. methods <CODE>table</CODE> and <CODE>name</CODE>, both returning the respective
  222. scalar. For example, consider the following statements:</P>
  223. <PRE>
  224.     INSERT INTO foo (bar) VALUES (1)
  225.     SELECT bar FROM foo WHERE ...
  226.     SELECT foo.bar FROM foo WHERE ...</PRE>
  227. <P>In all these cases exactly one column instance would be returned
  228. with</P>
  229. <PRE>
  230.     $col->name() eq 'bar'
  231.     $col->table() eq 'foo'</PRE>
  232. <DT><STRONG><A NAME="item_tables">tables</A></STRONG><BR>
  233. <DD>
  234. <PRE>
  235.     my $tableNum = $stmt->tables();  # Scalar context
  236.     my @tables = $stmt->tables();    # Array context
  237.     my($table1, $table2) = ($stmt->tables(0), $stmt->tables(1));</PRE>
  238. <P>Similar to <A HREF="#item_columns"><CODE>columns</CODE></A>, this method returns instances of
  239. <CODE>SQL::Statement::Table</CODE>.  For <EM>UPDATE</EM>, <EM>DELETE</EM>, <EM>INSERT</EM>,
  240. <EM>CREATE</EM> and <EM>DROP</EM>, a single table will always be returned.
  241. <EM>SELECT</EM> statements can return more than one table, in case
  242. of joins. Table objects offer a single method, <CODE>name</CODE> which
  243. returns the table name.</P>
  244. <DT><STRONG><A NAME="item_params">params</A></STRONG><BR>
  245. <DD>
  246. <PRE>
  247.     my $paramNum = $stmt->params();  # Scalar context
  248.     my @params = $stmt->params();    # Array context
  249.     my($p1, $p2) = ($stmt->params(0), $stmt->params(1));</PRE>
  250. <P>The <A HREF="#item_params"><CODE>params</CODE></A> method returns information about the input parameters
  251. used in a statement. For example, consider the following:</P>
  252. <PRE>
  253.     INSERT INTO foo VALUES (?, ?)</PRE>
  254. <P>This would return two instances of SQL::Statement::Param. Param objects
  255. implement a single method, <CODE>$param-</CODE>num()>, which retrieves the
  256. parameter number. (0 and 1, in the above example). As of now, not very
  257. usefull ... :-)</P>
  258. <DT><STRONG><A NAME="item_row_values">row_values</A></STRONG><BR>
  259. <DD>
  260. <PRE>
  261.     my $rowValueNum = $stmt->row_values(); # Scalar context
  262.     my @rowValues = $stmt->row_values();   # Array context
  263.     my($rval1, $rval2) = ($stmt->row_values(0),
  264.                           $stmt->row_values(1));</PRE>
  265. <P>This method is used for statements like</P>
  266. <PRE>
  267.     UPDATE $table SET $col1 = $val1, $col2 = $val2, ...
  268.         $colN = $valN WHERE ...
  269.     INSERT INTO $table (...) VALUES ($val1, $val2, ..., $valN)</PRE>
  270. <P>to read the values $val1, $val2, ... $valN. It returns scalar values
  271. or SQL::Statement::Param instances.</P>
  272. <DT><STRONG><A NAME="item_Order">Order</A></STRONG><BR>
  273. <DD>
  274. <PRE>
  275.     my $orderNum = $stmt->order();   # Scalar context
  276.     my @order = $stmt->order();      # Array context
  277.     my($o1, $o2) = ($stmt->order(0), $stmt->order(1));</PRE>
  278. <P>In <EM>SELECT</EM> statements you can use this for looking at the ORDER
  279. clause. Example:</P>
  280. <PRE>
  281.     SELECT * FROM FOO ORDER BY id DESC, name</PRE>
  282. <P>In this case, <CODE>order</CODE> could return 2 instances of SQL::Statement::Order.
  283. You can use the methods <CODE>$o->table()</CODE>, <CODE>$o->column()</CODE> and
  284. <CODE>$o->desc()</CODE> to examine the order object.</P>
  285. <DT><STRONG><A NAME="item_where">where</A></STRONG><BR>
  286. <DD>
  287. <PRE>
  288.     my $where = $stmt->where();</PRE>
  289. <P>This method is used to examine the syntax tree of the <CODE>WHERE</CODE> clause.
  290. It returns undef (if no WHERE clause was used) or an instance of
  291. SQL::Statement::Op. The Op instance offers 4 methods:</P>
  292. <DL>
  293. <DT><STRONG><A NAME="item_op">op</A></STRONG><BR>
  294. <DD>
  295. returns the operator, one of <CODE>AND</CODE>, <CODE>OR</CODE>, <CODE>=</CODE>, <CODE><></CODE>, <CODE>>=</CODE>,
  296. <CODE>></CODE>, <CODE><=</CODE>, <CODE><</CODE>, <CODE>LIKE</CODE>, <CODE>CLIKE</CODE> or <CODE>IS</CODE>.
  297. <P></P>
  298. <DT><STRONG><A NAME="item_arg1">arg1</A></STRONG><BR>
  299. <DD>
  300. <DT><STRONG><A NAME="item_arg2">arg2</A></STRONG><BR>
  301. <DD>
  302. returns the left-hand and right-hand sides of the operator. This can be a
  303. scalar value, an SQL::Statement::Param object or yet another
  304. SQL::Statement::Op instance.
  305. <P></P>
  306. <DT><STRONG><A NAME="item_neg">neg</A></STRONG><BR>
  307. <DD>
  308. returns a TRUE value, if the operation result must be negated after
  309. evalution.
  310. <P></P></DL>
  311. <P>To evaluate the <EM>WHERE</EM> clause, fetch the topmost Op instance with
  312. the <A HREF="#item_where"><CODE>where</CODE></A> method. Then evaluate the left-hand and right-hand side
  313. of the operation, perhaps recursively. Once that is done, apply the
  314. operator and finally negate the result, if required.</P>
  315. </DL>
  316. <P>To illustrate the above, consider the following WHERE clause:</P>
  317. <PRE>
  318.     WHERE NOT (id > 2 AND name = 'joe') OR name IS NULL</PRE>
  319. <P>We can represent this clause by the following tree:</P>
  320. <PRE>
  321.               (id > 2)   (name = 'joe')
  322.                      \   /
  323.           NOT         AND
  324.                          \      (name IS NULL)
  325.                           \    /
  326.                             OR</PRE>
  327. <P>Thus the WHERE clause would return an SQL::Statement::Op instance with
  328. the <A HREF="#item_op"><CODE>op()</CODE></A> field set to 'OR'. The <A HREF="#item_arg2"><CODE>arg2()</CODE></A> field would return another
  329. SQL::Statement::Op instance with <A HREF="#item_arg1"><CODE>arg1()</CODE></A> being the SQL::Statement::Column
  330. instance representing id, the <A HREF="#item_arg2"><CODE>arg2()</CODE></A> field containing the value undef
  331. (NULL) and the <A HREF="#item_op"><CODE>op()</CODE></A> field being 'IS'.</P>
  332. <P>The <A HREF="#item_arg1"><CODE>arg1()</CODE></A> field of the topmost Op instance would return an Op instance
  333. with <A HREF="#item_op"><CODE>op()</CODE></A> eq 'AND' and <A HREF="#item_neg"><CODE>neg()</CODE></A> returning TRUE. The <A HREF="#item_arg1"><CODE>arg1()</CODE></A> and <A HREF="#item_arg2"><CODE>arg2()</CODE></A>
  334. fields would be Op's representing ``id > 2'' and ``name = 'joe'''.</P>
  335. <P>Of course there's a ready-for-use method for WHERE clause evaluation:</P>
  336. <P>
  337. <H2><A NAME="evaluating a where clause">Evaluating a WHERE clause</A></H2>
  338. <P>The WHERE clause evaluation depends on an object being used for
  339. fetching parameter and column values. Usually this can be an
  340. SQL::Eval object, but in fact it can be any object that supplies
  341. the methods</P>
  342. <PRE>
  343.     $val = $eval->param($paramNum);
  344.     $val = $eval->column($table, $column);</PRE>
  345. <P>See <A HREF="../../../site/lib/SQL/Eval.html">the SQL::Eval manpage</A> for a detailed description of these methods.
  346. Once you have such an object, you can call a</P>
  347. <PRE>
  348.     $match = $stmt->eval_where($eval);</PRE>
  349. <P>
  350. <H2><A NAME="evaluating queries">Evaluating queries</A></H2>
  351. <P>So far all methods have been concrete. However, the interface for
  352. executing and evaluating queries is abstract. That means, for using
  353. them you have to derive a subclass from SQL::Statement that implements
  354. at least certain missing methods and/or overwrites others. See the
  355. <CODE>test.pl</CODE> script for an example subclass.</P>
  356. <P>Something that all methods have in common is that they simply throw
  357. a Perl exception in case of errors.</P>
  358. <DL>
  359. <DT><STRONG><A NAME="item_execute">execute</A></STRONG><BR>
  360. <DD>
  361. After creating a statement, you must execute it by calling the <A HREF="#item_execute"><CODE>execute</CODE></A>
  362. method. Usually you put an eval statement around this call:
  363. <PRE>
  364.     $@ = '';
  365.     my $rows = eval { $self->execute($data); };
  366.     if ($@) { die "An error occurred!"; }</PRE>
  367. <P>In case of success the method returns the number of affected rows or -1,
  368. if unknown. Additionally it sets the attributes</P>
  369. <PRE>
  370.     $self->{'NUM_OF_FIELDS'}
  371.     $self->{'NUM_OF_ROWS'}
  372.     $self->{'data'}</PRE>
  373. <P>the latter being an array ref of result rows. The argument $data is for
  374. private use by concrete subclasses and will be passed through to all
  375. methods. (It is intentionally not implemented as attribute: Otherwise
  376. we might well become self referencing data structures which could
  377. prevent garbage collection.)</P>
  378. <P></P>
  379. <DT><STRONG><A NAME="item_CREATE">CREATE</A></STRONG><BR>
  380. <DD>
  381. <DT><STRONG><A NAME="item_DROP">DROP</A></STRONG><BR>
  382. <DD>
  383. <DT><STRONG><A NAME="item_INSERT">INSERT</A></STRONG><BR>
  384. <DD>
  385. <DT><STRONG><A NAME="item_UPDATE">UPDATE</A></STRONG><BR>
  386. <DD>
  387. <DT><STRONG><A NAME="item_DELETE">DELETE</A></STRONG><BR>
  388. <DD>
  389. <DT><STRONG><A NAME="item_SELECT">SELECT</A></STRONG><BR>
  390. <DD>
  391. Called by <A HREF="#item_execute"><CODE>execute</CODE></A> for doing the real work. Usually they create an
  392. SQL::Eval object by calling <A HREF="#item_open_tables"><CODE>$self->open_tables()</CODE></A>, call
  393. <A HREF="#item_verify_columns"><CODE>$self->verify_columns()</CODE></A> and then do their job. Finally they return
  394. the triple
  395. <PRE>
  396.     ($self->{'NUM_OF_ROWS'}, $self->{'NUM_OF_FIELDS'},
  397.      $self->{'data'})</PRE>
  398. <P>so that execute can setup these attributes. Example:</P>
  399. <PRE>
  400.     ($self->{'NUM_OF_ROWS'}, $self->{'NUM_OF_FIELDS'},
  401.      $self->{'data'}) = $self->SELECT($data);</PRE>
  402. <P></P>
  403. <DT><STRONG><A NAME="item_verify_columns">verify_columns</A></STRONG><BR>
  404. <DD>
  405. Called for verifying the row names that are used in the statement.
  406. Example:
  407. <PRE>
  408.     $self->verify_columns($eval, $data);</PRE>
  409. <P></P>
  410. <DT><STRONG><A NAME="item_open_tables">open_tables</A></STRONG><BR>
  411. <DD>
  412. Called for creating an SQL::Eval object. In fact what it returns
  413. doesn't need to be derived from SQL::Eval, it's completely sufficient
  414. to implement the same interface of methods. See <A HREF="../../../site/lib/SQL/Eval.html">the SQL::Eval manpage</A> for
  415. details. The arguments <CODE>$data</CODE>, <CODE>$createMode</CODE> and <CODE>$lockMode</CODE>
  416. are corresponding to those of SQL::Eval::Table::open_table and
  417. usually passed through. Example:
  418. <PRE>
  419.     my $eval = $self->open_tables($data, $createMode, $lockMode);</PRE>
  420. <P>The eval object can be used for calling <CODE>$self-</CODE>verify_columns> or
  421. <CODE>$self-</CODE>eval_where>.</P>
  422. <P></P>
  423. <DT><STRONG><A NAME="item_open_table">open_table</A></STRONG><BR>
  424. <DD>
  425. This method is completely abstract and *must* be implemented by subclasses.
  426. The default implementation of <CODE>$self-</CODE>open_tables> calls this method for
  427. any table used by the statement. See the <CODE>test.pl</CODE> script for an example
  428. of imlplementing a subclass.
  429. <P></P></DL>
  430. <P>
  431. <HR>
  432. <H1><A NAME="sql syntax">SQL syntax</A></H1>
  433. <P>The SQL::Statement module is far away from ANSI SQL or something similar,
  434. it is designed for implementing the DBD::CSV module. See <A HREF="../../../DBD/CSV(3).html">the DBD::CSV(3) manpage</A>.</P>
  435. <P>I do not want to give a formal grammar here, more an informal
  436. description: Read the statement definition in sql_yacc.y, if you need
  437. something precise.</P>
  438. <P>The main lexical elements of the grammar are:</P>
  439. <DL>
  440. <DT><STRONG><A NAME="item_Integers">Integers</A></STRONG><BR>
  441. <DD>
  442. <DT><STRONG><A NAME="item_Reals">Reals</A></STRONG><BR>
  443. <DD>
  444. Syntax obvious
  445. <P></P>
  446. <DT><STRONG><A NAME="item_Strings">Strings</A></STRONG><BR>
  447. <DD>
  448. Surrounded by either single or double quotes; some characters need to
  449. be escaped with a backslash, in particular the backslash itself (\\),
  450. the NUL byte (\0), Line feeds (\n), Carriage return (\r), and the
  451. quotes (\' or \``).
  452. <P></P>
  453. <DT><STRONG><A NAME="item_Parameters">Parameters</A></STRONG><BR>
  454. <DD>
  455. Parameters represent scalar values, like Integers, Reals and Strings
  456. do. However, their values are read inside <CODE>Execute()</CODE> and not inside
  457. Prepare(). Parameters are represented by question marks (?).
  458. <P></P>
  459. <DT><STRONG><A NAME="item_Identifiers">Identifiers</A></STRONG><BR>
  460. <DD>
  461. Identifiers are table or column names. Syntactically they consist of
  462. alphabetic characters, followed by an arbitrary number of alphanumeric
  463. characters. Identifiers like SELECT, INSERT, INTO, ORDER, BY, WHERE,
  464. ... are forbidden and reserved for other tokens.
  465. <P></P></DL>
  466. <P>What it offers is the following:</P>
  467. <P>
  468. <H2><A NAME="create">CREATE</A></H2>
  469. <P>This is the CREATE TABLE command:</P>
  470. <PRE>
  471.     CREATE TABLE $table ( $col1 $type1, ..., $colN $typeN,
  472.                           [ PRIMARY KEY ($col1, ... $colM) ] )</PRE>
  473. <P>The column names are $col1, ... $colN. The column types can be
  474. <CODE>INTEGER</CODE>, <CODE>CHAR(n)</CODE>, <CODE>VARCHAR(n)</CODE>, <CODE>REAL</CODE> or <CODE>BLOB</CODE>. These
  475. types are currently completely ignored. So is the (optional)
  476. <CODE>PRIMARY KEY</CODE> clause.</P>
  477. <P>
  478. <H2><A NAME="drop">DROP</A></H2>
  479. <P>Very simple:</P>
  480. <PRE>
  481.     DROP TABLE $table</PRE>
  482. <P>
  483. <H2><A NAME="insert">INSERT</A></H2>
  484. <P>This can be</P>
  485. <PRE>
  486.     INSERT INTO $table [ ( $col1, ..., $colN ) ]
  487.         VALUES ( $val1, ... $valN )</PRE>
  488. <P>
  489. <H2><A NAME="delete">DELETE</A></H2>
  490. <PRE>
  491.     DELETE FROM $table [ WHERE $where_clause ]</PRE>
  492. <P>See <A HREF="#item_SELECT">SELECT</A> below for a decsription of $where_clause</P>
  493. <P>
  494. <H2><A NAME="update">UPDATE</A></H2>
  495. <PRE>
  496.     UPDATE $table SET $col1 = $val1, ... $colN = $valN
  497.         [ WHERE $where_clause ]</PRE>
  498. <P>See <A HREF="#item_SELECT">SELECT</A> below for a decsription of $where_clause</P>
  499. <P>
  500. <H2><A NAME="select">SELECT</A></H2>
  501. <PRE>
  502.     SELECT [DISTINCT] $col1, ... $colN FROM $table
  503.         [ WHERE $where_clause ] [ ORDER BY $ocol1, ... $ocolM ]</PRE>
  504. <P>The $where_clause is based on boolean expressions of the form
  505. $val1 $op $val2, with $op being one of '=', '<>', '>', '<', '>=',
  506. '<=', 'LIKE', 'CLIKE' or IS. You may use OR, AND and brackets to combine
  507. such boolean expressions or NOT to negate them.</P>
  508. <P>
  509. <HR>
  510. <H1><A NAME="installation">INSTALLATION</A></H1>
  511. <P>Like most other Perl modules, you simply do a</P>
  512. <PRE>
  513.     perl Makefile.PL
  514.     make                (nmake or dmake, if you are using Win32)
  515.     make test           (Let me know, if any tests fail)
  516.     make install</PRE>
  517. <P>Known problems are:</P>
  518. <UL>
  519. <LI>
  520. Some flavours of SCO Unix don't seem to have <CODE>alloca()</CODE> or something similar.
  521. I recommend using gcc or egcs for compiling Perl and the SQL::Statement
  522. module: Both compilers have a builtin alloca().
  523. <P>Another option could be to use external alloca.c, for example</P>
  524. <PRE>
  525.   <A HREF="http://www.pu.informatik.th-darmstadt.de/FTP/pub/pu/alloca.c">http://www.pu.informatik.th-darmstadt.de/FTP/pub/pu/alloca.c</A>
  526.   <A HREF="http://www.cs.purdue.edu/homes/young/src2www-example/alloca.c.html">http://www.cs.purdue.edu/homes/young/src2www-example/alloca.c.html</A></PRE>
  527. <P>I did test neither of them and cannot give detailed instructions for
  528. including them into the SQL::Statement module. However, it should
  529. be sufficient to compile alloca.c with the same instructions than,
  530. for example, sql_yacc.c and finally repeat the linker command by
  531. inserting alloca.o after sql_yacc.o.</P>
  532. <P>Note that I cannot modify the sources to work without alloca(), as it is
  533. the bison parser that's using <CODE>alloca()</CODE> and I don't have the bison generated
  534. code in my hands.</P>
  535. <P>My thanks to Theo Petersen, <<A HREF="mailto:theo@acsp.com">theo@acsp.com</A>>, for pointing out this problem
  536. and the possible workarounds.</P>
  537. <P></P></UL>
  538. <P>
  539. <HR>
  540. <H1><A NAME="internals">INTERNALS</A></H1>
  541. <P>Internally the module is splitted into three parts:</P>
  542. <P>
  543. <H2><A NAME="perlindependent c part">Perl-independent C part</A></H2>
  544. <P>This part, contained in the files <CODE>sql_yacc.y</CODE>, <CODE>sql_data.h</CODE>,
  545. <CODE>sql_data.c</CODE> and <CODE>sql_op.c</CODE>, is completely independent from Perl.
  546. It might well be used from within another script language, Tcl say,
  547. or from a true C application.</P>
  548. <P>You probably ask, why Perl independence? Well, first of all, I
  549. think this is a valuable target in itself. But the main reason was
  550. the impossibility to use the Perl headers inside bison generated
  551. code. The Perl headers export almost the complete Yacc interface
  552. to XS, for whatever reason, thus redefining constants and structures
  553. created by your own bison code. :-(</P>
  554. <P>
  555. <H2><A NAME="perldependent c part">Perl-dependent C part</A></H2>
  556. <P>This is contained in <CODE>Statement.xs</CODE>. The both C parts communicate via
  557. a C structure sql_stmt_t. In fact, an SQL::Statement object is nothing
  558. else than a pointer to such a structure. The XS calls columns(), Table(),
  559. where(), ... do nothing more than fetching data from this structure
  560. and converting it to Perl objects. See <A HREF="#the sql_stmt_t structure">The sql_stmt_t structure</A>
  561. below for details on the structure.</P>
  562. <P>
  563. <H2><A NAME="perl part">Perl part</A></H2>
  564. <P>Besides some stub functions for retrieving statement data, this is
  565. mainly the query processing with the exception of WHERE clause
  566. evaluation.</P>
  567. <P>
  568. <H2><A NAME="the sql_stmt_t structure">The sql_stmt_t structure</A></H2>
  569. <P>This structure is designed for optimal performance. A typical query
  570. will be parsed with only 4 or 5 <CODE>malloc()</CODE> calls; in particular no
  571. memory will be aquired for storing strings; only pointers into the
  572. query string are used.</P>
  573. <P>The statement stores its tokens in the values array. The array elements
  574. are of type sql_val_t, a union, that can represent the most interesting
  575. tokens; for example integers and reals are stored in the data.i and
  576. data.d parts of the union, strings are stored in the data.str part,
  577. columns in the data.col part and so on. Arrays are allocated in chunks
  578. of 64 elements, thus a single <CODE>malloc()</CODE> will be usually sufficient for
  579. allocating the complete array. Some types use pointers into the values
  580. array: For example, operations are stored in an sql_op_t structure that
  581. containes elements arg1 and arg2 which are pointers into the value
  582. table, pointing to other operations or scalars. These pointers are
  583. stored as indices, so that the array can be extended using realloc().</P>
  584. <P>The sql_stmt_t structure contains other arrays: columns, tables,
  585. rowvals, order, ... representing the data returned by the columns(),
  586. tables(), <A HREF="#item_row_values"><CODE>row_values()</CODE></A> and <CODE>order()</CODE> methods. All of these contain
  587. pointers into the values array, again stored as integers.</P>
  588. <P>Arrays are initialized with the _InitArray call in SQL_Statement_Prepare
  589. and deallocated with _DestroyArray in SQL_Statement_Destroy. Array
  590. elements are obtained by calling _AllocData, which returns an index.
  591. The number -1 is used for errors or as a NULL value.</P>
  592. <P>
  593. <H2><A NAME="the where clause evaluation">The WHERE clause evaluation</A></H2>
  594. <P>A WHERE clause is evaluated by calling SQL_Statement_EvalWhere(). This
  595. function is in the Perl independent part, but it needs the possibility
  596. to retrieve data from the Perl part, for example column or parameter
  597. values. These values are retrieved via callbacks, stored in the
  598. sql_eval_t structure. The field stmt->evalData points to such a
  599. structure. Of course the calling method can extend the sql_eval_t
  600. structure (like eval_where in Statement.xs does) to include private data
  601. not used by SQL_Statement_EvalWhere.</P>
  602. <P>
  603. <H2><A NAME="features">Features</A></H2>
  604. <P>Different parsers are implemented via the sql_parser_t structure. This
  605. is mainly a set of yes/no flags. If you'd like to add features, do
  606. the following:</P>
  607. <P>First of all, extend the sql_parser_t structure. If your feature is
  608. part of a certain statement, place it into the statements section,
  609. for example ``select.join''. Otherwise choose a section like ``misc''
  610. or ``general''. (There's no particular for the section design, but
  611. structure never hurts.)</P>
  612. <P>Second, add your feature to sql_yacc.y. If your feature needs to
  613. extend the lexer, do it like this:</P>
  614. <PRE>
  615.     if (FEATURE(misc, myfeature) {
  616.         /*  Scan your new symbols  */
  617.         ...
  618.     }</PRE>
  619. <P>See the <EM>BOOL</EM> symbol as an example.</P>
  620. <P>If you need to extend the parser, do it like this:</P>
  621. <PRE>
  622.     my_new_rule:
  623.         /*  NULL, old behaviour, doesn't use my feature  */
  624.         | my_feature
  625.             { YFEATURE(misc, myfeature); }
  626.     ;</PRE>
  627. <P>Thus all parsers not having FEATURE(misc, myfeature) set will produce
  628. a parse error here. Again, see the BOOL symbol for an example.</P>
  629. <P>Third thing is to extend the builtin parsers. If they support your
  630. feature, add a 1, otherwise a 0. Currently there are two builtin
  631. parsers: The <EM>ansiParser</EM> in sql_yacc.y and the sqlEvalParser in
  632. Statement.xs.</P>
  633. <P>Finally add support for your feature to the <CODE>feature</CODE> method in
  634. Statement.xs. That's it!</P>
  635. <P>
  636. <HR>
  637. <H1><A NAME="multithreading">MULTITHREADING</A></H1>
  638. <P>The complete module code is reentrant. In particular the parser is
  639. created with <CODE>%pure_parser</CODE>. See <EM>bison(1)</EM> for details on
  640. reentrant parsers. That means, the module is ready for multithreading,
  641. as long as you don't share handles between threads. Read-only handles,
  642. for example parsers, can even be shared.</P>
  643. <P>Statement handles cannot be shared among threads, at least not, if
  644. you don't grant serialized access. Per-thread handles are always safe.</P>
  645. <P>
  646. <HR>
  647. <H1><A NAME="author and copyright">AUTHOR AND COPYRIGHT</A></H1>
  648. <P>This module is Copyright (C) 1998 by</P>
  649. <PRE>
  650.     Jochen Wiedmann
  651.     Am Eisteich 9
  652.     72555 Metzingen
  653.     Germany</PRE>
  654. <PRE>
  655.     Email: joe@ispsoft.de
  656.     Phone: +49 7123 14887</PRE>
  657. <P>All rights reserved.</P>
  658. <P>You may distribute this module under the terms of either the GNU
  659. General Public License or the Artistic License, as specified in
  660. the Perl README file.</P>
  661. <P>
  662. <HR>
  663. <H1><A NAME="see also">SEE ALSO</A></H1>
  664. <P><EM>DBI(3)</EM>, <A HREF="../../../DBD/CSV(3).html">the DBD::CSV(3) manpage</A></P>
  665. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  666. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  667. <STRONG><P CLASS=block> SQL::Statement - SQL parsing and processing engine</P></STRONG>
  668. </TD></TR>
  669. </TABLE>
  670.  
  671. </BODY>
  672.  
  673. </HTML>
  674.