db.h
changeset 14 51eb85ae4de4
parent 13 23a242d7b7fa
child 15 2706fc514dea
equal deleted inserted replaced
13:23a242d7b7fa 14:51eb85ae4de4
    29 */
    29 */
    30 
    30 
    31 #ifndef _DB_H
    31 #ifndef _DB_H
    32 #define _DB_H
    32 #define _DB_H
    33 
    33 
    34 #include "sqlite3.h"
       
    35 
       
    36 #define DBSQL_MATCH_REQUEST "                                       \
       
    37 	SELECT rewrite_rule, (                                          \
       
    38 		CASE WHEN scheme       IS null THEN 1 ELSE 0 END +          \
       
    39 		CASE WHEN host         IS null THEN 1 ELSE 0 END +          \
       
    40 		CASE WHEN tld          IS null THEN 1 ELSE 0 END +          \
       
    41 		CASE WHEN path         IS null THEN 1 ELSE 0 END +          \
       
    42 		CASE WHEN port         IS null THEN 1 ELSE 0 END +          \
       
    43 		CASE WHEN ip           IS null THEN 1 ELSE 0 END +          \
       
    44 		CASE WHEN user         IS null THEN 1 ELSE 0 END +          \
       
    45 		CASE WHEN method       IS null THEN 1 ELSE 0 END +          \
       
    46 		CASE WHEN rewrite_rule IS null THEN 1 ELSE 0 END ) as nullc \
       
    47 	FROM requests                                                   \
       
    48 	WHERE                                                           \
       
    49 		( scheme IS NULL OR scheme          = lower(?1) ) AND       \
       
    50 		( host   IS NULL OR lower( host )   = lower(?2) ) AND       \
       
    51 		( tld    IS NULL OR lower( tld )    = lower(?3) ) AND       \
       
    52 		( path   IS NULL OR lower( path ) LIKE '?4%'    ) AND       \
       
    53 		( port   IS NULL OR port            = ?5        ) AND       \
       
    54 		( ip     IS NULL OR ip              = ?6        ) AND       \
       
    55 		( user   IS NULL OR lower( user )   = lower(?7) ) AND       \
       
    56 		( method IS NULL OR lower( method ) = lower(?8) ) AND       \
       
    57 		rewrite_rule IS NOT null                                    \
       
    58 	ORDER BY                                                        \
       
    59 		length(path) DESC,                                          \
       
    60 		nullc ASC                                                   \
       
    61 	LIMIT 1"
       
    62 
       
    63 /* Pull the entire rewrite rule row. */
       
    64 #define DBSQL_GET_REWRITE_RULE "\
       
    65 	SELECT * \
       
    66 	FROM rewrite_rules \
       
    67 	WHERE id = ?1"
       
    68 
       
    69 extern const unsigned short int DB_VERSION;
       
    70 
       
    71 /*
    34 /*
    72  * Function prototypes
    35  * Function prototypes
    73  *
    36  *
    74  */
    37  */
    75 int db_attach( void );
    38 short int db_attach( void );
    76 int db_upgrade( unsigned short int );
    39 unsigned short int db_create_new( char * );
    77 unsigned short int prepare_statements( void );
    40 struct db_input *parse_dbinput( char * );
    78 short int db_version( void );
    41 char *find_record( char * );
    79 rewrite *init_rewrite( void );
    42 unsigned int find_records( char *, parsed ** );
    80 rewrite *prepare_rewrite( request * );
       
    81 void finish_rewrite( rewrite * );
       
    82 
    43 
    83 #endif
    44 #endif
    84 
    45