db.h
changeset 13 23a242d7b7fa
parent 11 9aa5114326e8
child 14 51eb85ae4de4
equal deleted inserted replaced
12:191b3c25974a 13:23a242d7b7fa
    31 #ifndef _DB_H
    31 #ifndef _DB_H
    32 #define _DB_H
    32 #define _DB_H
    33 
    33 
    34 #include "sqlite3.h"
    34 #include "sqlite3.h"
    35 
    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 
    36 extern const unsigned short int DB_VERSION;
    69 extern const unsigned short int DB_VERSION;
    37 
    70 
    38 /*
    71 /*
    39  * Function prototypes
    72  * Function prototypes
    40  *
    73  *
    41  */
    74  */
    42 int db_attach( void );
    75 int db_attach( void );
    43 int db_upgrade( unsigned short int current_version );
    76 int db_upgrade( unsigned short int );
       
    77 unsigned short int prepare_statements( void );
    44 short int db_version( void );
    78 short int db_version( void );
       
    79 rewrite *init_rewrite( void );
       
    80 rewrite *prepare_rewrite( request * );
       
    81 void finish_rewrite( rewrite * );
    45 
    82 
    46 #endif
    83 #endif
    47 
    84