AnyEvent-DBI-3.04/0000755000000000000000000000000013267405123012266 5ustar rootrootAnyEvent-DBI-3.04/t/0000755000000000000000000000000013267405123012531 5ustar rootrootAnyEvent-DBI-3.04/t/fake-mysql0000644000000000000000000005301111211237020014507 0ustar rootroot#!/usr/bin/perl =pod =head1 SYNOPSIS perl examples/myserver.pl --config=examples/myserver.conf --port=1234 --dsn="dbi:mysql:" mysql -h127.0.0.1 -P1234 -umyuser -e 'info' =head1 DESCRIPTION This is a simple server that listens for incoming connections from MySQL clients or connectors. Each query received is processed according to a set of configuration files, which can rewrite the query, forward it to a DBI handle or construct a response or a result set on the fly from any data. =head1 COMMAND LINE OPTIONS C<--port=XXXX> - port to listen on. Default is C<23306>, which is the default MySQL port with a 2 in front. C<--interface=AAA.BBB.CCC.DDD> - interface to listen to. Default is C<127.0.0.1> which means that only connections from the localhost will be accepted. To enable connections from the outside use C<--interface=0.0.0.0>. In this case, please make sure you have some other form of access protection, e.g. like the first rule in the C example configuration file. C<--config=config.file> - a configuration file containing rules to be executed. The option can be specified multiple times and the rules will be checked in the order specified. C<--dsn> - specifies a L DSN. All queries that did not match a rule or where the rule rewrote the query or did not return any response or a result set on its own will be forwarded to that database. Individual rules can forward specific queries to specific DSNs. If you do not want non-matching queries to be forwarded, either create a match-all rule at the bottom of your last configuration file or omit the C<--dsn> option. If you omit the option, an error message will be sent to the client. C<--dsn_user> and C<--dsn_password> can be used to specify username and password for DBI drivers where those can not be specified in the DSN string. =head1 RULES Rules to be executed are contained in configuration files. The configuration files are actually standard perl scripts and are executed as perl subroutines. Therefore, they can contain any perl code -- the only requirement is that the last statement in the file (that is, the return value of the file) is an array containing the rules to be executed. The actions from a rule will be executed for all queries that match a specific pattern. Rules are processed in order and processing is terminated at the first rule that returns some data to the client. This allows you to rewrite a query numerous times and have a final default rule that forwards the query to another server. If C is defined, further rules are not processed. Each rule can have the following attributes: C The rule will match if the MySQL command issued by the client matches C. C can either be an integer from the list found at C or a reference to a C that returns such an integer. This is mainly useful for processing incoming C, C and C. C The rule will match if the test of the query matches a regular expression or is identical to a string. C can also be a reference to a C in which case the sub is executed and can either return a string or a regular expression. If both C and C are specified, both must match for the rule to be executed. C if specified, any matching query will be forwarded to this database handle (possibly after a C), rather than the default handle specifeid on the command line. C behaves identically, however a database handle is contructed from the C provided and an attempt is made to connect to the database. If C is a reference to an array, the first item from the array is used as a DSN, the second one is used as username and the third one is used as password. C this can be a reference to a subroutine that will be called after a matching query has been encountered but before any further processing has taken place. The subroutine will be called with the text of the query as the first argument, followed by extra arguments containing the strings matched from any parenthesis found in the C regular expression. You can use C to execute any extra queries before the main query, such as C. The return value from the C subroutine is discarded and is not used. C is a string that will replace the original query that matches the rule, or a reference to a subroutine that will produce such a string. If C is not defined, and C was a string, the query is passed along unchanged. If C was a regular expression, the string matched by the first set of parenthesis is used. This way, if the rule does not specify any C, C, C or C clauses, but a valid DBI handle is defined, the query will be forwarded to that handle automatically. C can be either an array reference containing three arguments for C or a reference to a subroutine returning such an array (or array reference). If this is the case, the error message will be sent to the client. If C is not defined or the subroutine returns C, no error message will be sent. In this case, you need to send at some point either an C or a result set, otherwise the client will hang forever waiting for a response. C behaves identically to C -- if it is defined or points to a subroutine which, when called, returns a true value, an OK response will be sent to the client. C can also be a reference to an array, or the subroutine can return such an array -- in this case the first item is the message to be sent to the client, the second one is the number of affected rows, the third is the insert_id and the last one is the warning count. C must contain either an array reference or a reference to a subroutine which returns and array or array reference. The column names from the array will be sent to the client. By default, all columns are defined as C. C must contain either a reference to the data to be returned to the client or a reference to subroutine that will produce the data. "Data" can be a reference to a C, in which case the hash will be sent with the key names in the first column and the key values in the second. It can be a flat array, in which case the array items will be sent as a single column, or it can be a reference to a nested array, with each sub-array being a single row from the response. C is called after all other parts of the rule have been processed. C if defined, the query will be immediately forwarded to the server and no further rules will be processed. All subroutine references that are called will have the text of the query passed as the first argument and the subsequent arguments will be any strings matched by parenthesis in the C regular expression. =head1 VARIABLES Your code in the configuration file can save and retrieve state by using C and C. State is retained as long as the connection is open. Each new connection starts with a clean state. The following variables are maintained by the system: C contains a reference to the L object being used to service the connection. You can use this to inject data and packets directly into the network stream. C contains the username provided by the client at connection establishment. C contains the database requested by the client at connection establishment. By default, C will not automatically handle any database changes requested by the client. You are responsible for handling those either by responding with a simple OK or by updating the variables. C contains the IP of the client. C and C will contain a reference to the default DBI handle and the DSN string it was produced from, as taken from the command line. Even if a specific rule has its own C, the value of those variables will always refer to the default C and C. If you change the variable, the system will attempt to connect to the new dsn string and will produce a new C handle from it. If you set C to an array reference, the first item will be used as a DSN, the second one as a username and the third one as a password. C and C can be used for the same purpose. C contains a reference to the C<@ARGV> array, that is, the command line options that evoked myserver.pl C, C and C are convenience variables that can also be specified on the command line. It is not used by C however you can use it in your rules, the way C does. =head1 SECURITY By default the script will only accept incoming connections from the local host. If you relax that via the C<--interface> command-line option, all connections will be accepted. However, once the connection has been established, you can implement access control as demonstrated in the first rule of the C file -- it returns "Access denied" for every query unless the username is "myuser". Future versions of the script will allow connections to be rejected during handshake. =head1 SAMPLE RULES The following rule sets are provided in the C directory. =head2 Simple examples - myserver.conf This configuration provides some simple query rewriting examples as suggested by Giuseppe Maxia and Jan Kneschke, e.g. commands like C, C as well as fixing spelling mistakes. In addition, some very simple access control is demonstrated at the top of the file. =head2 Remote queries - remotequery.conf This rule set implements a C