Add support for BETWEEN conditions.
authorscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 31 May 2010 19:45:26 +0000 (19:45 +0000)
committerscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 31 May 2010 19:45:26 +0000 (19:45 +0000)
M    Open-ILS/src/c-apps/oils_storedq.c
M    Open-ILS/src/c-apps/buildSQL.c

git-svn-id: svn://svn.open-ils.org/ILS/trunk@16538 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/c-apps/buildSQL.c
Open-ILS/src/c-apps/oils_storedq.c

index bf83fa6..7749cda 100644 (file)
@@ -577,8 +577,31 @@ static void buildExpression( BuildSQLState* state, const Expression* expr ) {
                        if( expr->negate )
                                buffer_add( state->sql, "NOT " );
 
                        if( expr->negate )
                                buffer_add( state->sql, "NOT " );
 
-                       sqlAddMsg( state, "BETWEEN expressions not yet supported" );
-                       state->error = 1;
+                       buildExpression( state, expr->left_operand );
+                       if( state->error ) {
+                               sqlAddMsg( state, "Unable to emit left operand in BETWEEN expression # %d",
+                                       expr->id );
+                               break;
+                       }
+
+                       buffer_add( state->sql, " BETWEEN " );
+
+                       buildExpression( state, expr->subexp_list );
+                       if( state->error ) {
+                               sqlAddMsg( state, "Unable to emit lower limit in BETWEEN expression # %d",
+                                       expr->id );
+                               break;
+                       }
+
+                       buffer_add( state->sql, " AND " );
+
+                       buildExpression( state, expr->subexp_list->next );
+                       if( state->error ) {
+                               sqlAddMsg( state, "Unable to emit upper limit in BETWEEN expression # %d",
+                                       expr->id );
+                               break;
+                       }
+
                        break;
                case EXP_BIND :
                        if( !expr->bind ) {     // Sanity check
                        break;
                case EXP_BIND :
                        if( !expr->bind ) {     // Sanity check
index 3c90dc1..914a1e1 100644 (file)
@@ -1195,7 +1195,53 @@ static Expression* constructExpression( BuildSQLState* state, dbi_result result
        BindVar* bind = NULL;
        Expression* subexp_list = NULL;
 
        BindVar* bind = NULL;
        Expression* subexp_list = NULL;
 
-       if( EXP_BIND == type ) {
+       if( EXP_BETWEEN == type ) {
+               // Get the left operand
+               if( -1 == left_operand_id ) {
+                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                               "No left operand defined for BETWEEN expression # %d", id ));
+                       state->error = 1;
+                       return NULL;
+               } else {
+                       left_operand = getExpression( state, left_operand_id );
+                       if( !left_operand ) {
+                               osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                                       "Unable to get left operand in BETWEEN expression # %d", id ));
+                               state->error = 1;
+                               return NULL;
+                       }
+               }
+
+               // Get the end points of the BETWEEN range
+               subexp_list = getExpressionList( state, id );
+               if( state->error ) {
+                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                               "Unable to get subexpressions for BETWEEN expression # %d", id ));
+                       expressionFree( left_operand );
+                       return NULL;
+               } else if( !subexp_list ) {
+                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                               "BETWEEN range is empty in expression # %d", id ));
+                       state->error = 1;
+                       expressionFree( left_operand );
+                       return NULL;
+               } else if( !subexp_list->next ) {
+                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                               "BETWEEN range has only one end point in expression # %d", id ));
+                       state->error = 1;
+                       expressionListFree( subexp_list );
+                       expressionFree( left_operand );
+                       return NULL;
+               } else if( subexp_list->next->next ) {
+                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                               "BETWEEN range has more than two subexpressions in expression # %d", id ));
+                       state->error = 1;
+                       expressionListFree( subexp_list );
+                       expressionFree( left_operand );
+                       return NULL;
+               }
+
+       } else if( EXP_BIND == type ) {
                if( bind_variable ) {
                        // To do: Build a BindVar
                        bind = getBindVar( state, bind_variable );
                if( bind_variable ) {
                        // To do: Build a BindVar
                        bind = getBindVar( state, bind_variable );
@@ -1225,6 +1271,22 @@ static Expression* constructExpression( BuildSQLState* state, dbi_result result
                        }
                }
 
                        }
                }
 
+       } else if( EXP_EXIST == type ) {
+               if( -1 == subquery_id ) {
+                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                               "Internal error: No subquery found for EXIST expression # %d", id ));
+                       state->error = 1;
+                       return NULL;
+               } else {
+                       subquery = getStoredQuery( state, subquery_id );
+                       if( !subquery ) {
+                               osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
+                                       "Unable to load subquery for EXIST expression # %d", id ));
+                               state->error = 1;
+                               return NULL;
+                       }
+               }
+
        } else if( EXP_FUNCTION == type ) {
                if( !function_name ) {
                        osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
        } else if( EXP_FUNCTION == type ) {
                if( !function_name ) {
                        osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
@@ -1297,21 +1359,6 @@ static Expression* constructExpression( BuildSQLState* state, dbi_result result
                                return NULL;
                        }
                }
                                return NULL;
                        }
                }
-       } else if( EXP_EXIST == type ) {
-               if( -1 == subquery_id ) {
-                       osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
-                               "Internal error: No subquery found for EXIST expression # %d", id ));
-                       state->error = 1;
-                       return NULL;
-               } else {
-                       subquery = getStoredQuery( state, subquery_id );
-                       if( !subquery ) {
-                               osrfLogWarning( OSRF_LOG_MARK, sqlAddMsg( state,
-                                       "Unable to load subquery for EXIST expression # %d", id ));
-                               state->error = 1;
-                               return NULL;
-                       }
-               }
 
        } else if( EXP_NUMBER == type ) {
                if( !literal ) {
 
        } else if( EXP_NUMBER == type ) {
                if( !literal ) {