home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Eval.pm < prev    next >
Encoding:
Text File  |  2004-04-20  |  7.7 KB  |  335 lines

  1. # -*- perl -*-
  2.  
  3. require 5.004;
  4. use strict;
  5.  
  6. require SQL::Statement;
  7.  
  8. package SQL::Eval;
  9.  
  10. sub new ($$) {
  11.     my($proto, $attr) = @_;
  12.     my($self) = { %$attr };
  13.     bless($self, (ref($proto) || $proto));
  14.     $self;
  15. }
  16.  
  17. sub param ($$;$) {
  18.     my($self, $paramNum, $param) = @_;
  19.     if (@_ == 3) {
  20.     $self->{'params'}->[$paramNum] = $param;
  21.     } else {
  22.     if ($paramNum < 0) {
  23.         die "Illegal parameter number: $paramNum";
  24.     }
  25.     $self->{'params'}->[$paramNum];
  26.     }
  27. }
  28.  
  29. sub params ($;$) {
  30.     my($self, $array) = @_;
  31.     if (@_ == 2) {
  32.     $self->{'params'} = $array;
  33.     } else {
  34.     $self->{'params'};
  35.     }
  36. }
  37.  
  38.  
  39. sub table ($$) {
  40.     my($self, $table) = @_;
  41.     $self->{'tables'}->{$table};
  42. }
  43.  
  44. sub column ($$$;$) {
  45.     my($self, $table, $column, $val) = @_;
  46.     if (@_ == 4) {
  47.     $self->table($table)->column($column, $val);
  48.     } else {
  49.     $self->table($table)->column($column);
  50.     }
  51. }
  52.  
  53.  
  54. package SQL::Eval::Table;
  55.  
  56. sub new ($$) {
  57.     my($proto, $attr) = @_;
  58.     my($self) = { %$attr };
  59.     bless($self, (ref($proto) || $proto));
  60.     $self;
  61. }
  62.  
  63. sub row ($;$) {
  64.     my($self, $row) = @_;
  65.     if (@_ == 2) {
  66.     $self->{'row'} = $row;
  67.     } else {
  68.     $self->{'row'};
  69.     }
  70. }
  71.  
  72. sub column ($$;$) {
  73.     my($self, $column, $val) = @_;
  74.     if (@_ == 3) {
  75.     $self->{'row'}->[$self->{'col_nums'}->{$column}] = $val;
  76.     } else {
  77.     $self->{'row'}->[$self->{'col_nums'}->{$column}];
  78.     }
  79. }
  80.  
  81. sub column_num ($$) {
  82.     my($self, $col) = @_;
  83.     $self->{'col_nums'}->{$col};
  84. }
  85.  
  86. sub col_names ($) {
  87.     shift->{'col_names'};
  88. }
  89.  
  90. 1;
  91.  
  92.  
  93. __END__
  94.  
  95. =head1 NAME
  96.  
  97. SQL::Eval - Base for deriving evalution objects for SQL::Statement
  98.  
  99.  
  100. =head1 SYNOPSIS
  101.  
  102.     require SQL::Statement;
  103.     require SQL::Eval;
  104.  
  105.     # Create an SQL statement; use a concrete subclass of
  106.     # SQL::Statement
  107.     my $stmt = MyStatement->new("SELECT * FROM foo, bar",
  108.                     SQL::Parser->new('Ansi'));
  109.  
  110.     # Get an eval object by calling open_tables; this
  111.     # will call MyStatement::open_table
  112.     my $eval = $stmt->open_tables($data);
  113.  
  114.     # Set parameter 0 to 'Van Gogh'
  115.     $eval->param(0, 'Van Gogh');
  116.     # Get parameter 2
  117.     my $param = $eval->param(2);
  118.  
  119.     # Get the SQL::Eval::Table object referring the 'foo' table
  120.     my $fooTable = $eval->table('foo');
  121.  
  122.  
  123. =head1 DESCRIPTION
  124.  
  125. This module implements two classes that can be used for deriving
  126. concrete subclasses to evaluate SQL::Statement objects. The
  127. SQL::Eval object can be thought as an abstract state engine for
  128. executing SQL queries, the SQL::Eval::Table object can be considered
  129. a *very* table abstraction. It implements method for fetching or
  130. storing rows, retrieving column names and numbers and so on.
  131. See the C<test.pl> script as an example for implementing a concrete
  132. subclass.
  133.  
  134. While reading on, keep in mind that these are abstract classes,
  135. you *must* implement at least some of the methods describe below.
  136. Even more, you need not derive from SQL::Eval or SQL::Eval::Table,
  137. you just need to implement the method interface.
  138.  
  139. All methods just throw a Perl exception in case of errors.
  140.  
  141.  
  142. =head2 Method interface of SQL::Eval
  143.  
  144. =over 8
  145.  
  146. =item new
  147.  
  148. Constructor; use it like this:
  149.  
  150.     $eval = SQL::Eval->new(\%attr);
  151.  
  152. Blesses the hash ref \%attr into the SQL::Eval class (or a subclass).
  153.  
  154. =item param
  155.  
  156. Used for getting or setting input parameters, as in the SQL query
  157.  
  158.     INSERT INTO foo VALUES (?, ?);
  159.  
  160. Example:
  161.  
  162.     $eval->param(0, $val);        # Set parameter 0
  163.     $eval->param(0);              # Get parameter 0
  164.  
  165. =item params
  166.  
  167. Likewise used for getting or setting the complete array of input
  168. parameters. Example:
  169.  
  170.     $eval->params($params);       # Set the array
  171.     $eval->params();              # Get the array
  172.  
  173. =item table
  174.  
  175. Returns or sets a table object. Example:
  176.  
  177.     $eval->table('foo', $fooTable);  # Set the 'foo' table object
  178.     $eval->table('foo');             # Return the 'foo' table object
  179.  
  180. =item column
  181.  
  182. Return the value of a column with a given name; example:
  183.  
  184.     $col = $eval->column('foo', 'id');  # Return the 'id' column of
  185.                                         # the current row in the
  186.                                         # 'foo' table
  187.  
  188. This is equivalent and just a shorthand for
  189.  
  190.     $col = $eval->table('foo')->column('id');
  191.  
  192. =back
  193.  
  194.  
  195. =head2 Method interface of SQL::Eval::Table
  196.  
  197. =over 8
  198.  
  199. =item new
  200.  
  201. Constructor; use it like this:
  202.  
  203.     $eval = SQL::Eval::Table->new(\%attr);
  204.  
  205. Blesses the hash ref \%attr into the SQL::Eval::Table class (or a
  206. subclass).
  207.  
  208. =item row
  209.  
  210. Used to get the current row as an array ref. Do not mismatch
  211. getting the current row with the fetch_row method! In fact this
  212. method is valid only after a successfull C<$table-E<gt>fetchrow()>.
  213. Example:
  214.  
  215.     $row = $table->row();
  216.  
  217. =item column
  218.  
  219. Get the column with a given name in the current row. Valid only after
  220. a successfull C<$table-E<gt>fetchrow()>. Example:
  221.  
  222.     $col = $table->column($colName);
  223.  
  224. =item column_num
  225.  
  226. Return the number of the given column name. Column numbers start with
  227. 0. Returns undef, if a column name is not defined, so that you can
  228. well use this for verifying valid column names. Example:
  229.  
  230.     $colNum = $table->column_num($colNum);
  231.  
  232. =item column_names
  233.  
  234. Returns an array ref of column names.
  235.  
  236. =back
  237.  
  238. The above methods are implemented by SQL::Eval::Table. The following
  239. methods aren't, so that they *must* be implemented by concrete
  240. subclassed. See the C<test.pl> script for example.
  241.  
  242. =over 8
  243.  
  244. =item fetch_row
  245.  
  246. Fetches the next row from the table. Returns C<undef>, if the last
  247. row was already fetched. The argument $data is for private use of
  248. the concrete subclass. Example:
  249.  
  250.     $row = $table->fetch_row($data);
  251.  
  252. Note, that you may use
  253.  
  254.     $row = $table->row();
  255.  
  256. for retrieving the same row again, until the next call of C<fetch_row>.
  257.  
  258. =item push_row
  259.  
  260. Likewise for storing rows. Example:
  261.  
  262.     $table->push_row($data, $row);
  263.  
  264. =item push_names
  265.  
  266. Used by the I<CREATE TABLE> statement to set the column names of the
  267. new table. Receives an array ref of names. Example:
  268.  
  269.     $table->push_names($data, $names);
  270.  
  271. =item seek
  272.  
  273. Similar to the seek method of a filehandle; used for setting the number
  274. of the next row being written. Example:
  275.  
  276.     $table->seek($data, $whence, $rowNum);
  277.  
  278. Actually the current implementation is using only C<seek($data, 0,0)>
  279. (first row) and C<seek($data, 2,0)> (last row, end of file).
  280.  
  281. =item truncate
  282.  
  283. Truncates a table after the current row. Example:
  284.  
  285.     $table->truncate($data);
  286.  
  287. =back
  288.  
  289.  
  290. =head1 INTERNALS
  291.  
  292. The current implementation is quite simple: An SQL::Eval object is an
  293. hash ref with only two attributes. The C<params> attribute is an array
  294. ref of parameters. The C<tables> attribute is an hash ref of table
  295. names (keys) and table objects (values).
  296.  
  297. SQL::Eval::Table instances are implemented as hash refs. Used attributes
  298. are C<row> (the array ref of the current row), C<col_nums> (an hash ref
  299. of column names as keys and column numbers as values) and C<col_names>,
  300. an array ref of column names with the column numbers as indexes.
  301.  
  302.  
  303. =head1 MULTITHREADING
  304.  
  305. All methods are working with instance-local data only, thus the module
  306. is reentrant and thread safe, if you either don't share handles between
  307. threads or grant serialized use.
  308.  
  309.  
  310. =head1 AUTHOR AND COPYRIGHT
  311.  
  312. This module is Copyright (C) 1998 by
  313.  
  314.     Jochen Wiedmann
  315.     Am Eisteich 9
  316.     72555 Metzingen
  317.     Germany
  318.  
  319.     Email: joe@ispsoft.de
  320.     Phone: +49 7123 14887
  321.  
  322. All rights reserved.
  323.  
  324. You may distribute this module under the terms of either the GNU
  325. General Public License or the Artistic License, as specified in
  326. the Perl README file.
  327.  
  328.  
  329. =head1 SEE ALSO
  330.  
  331. L<SQL::Statement(3)>
  332.  
  333.  
  334. =cut
  335.