persistentstorage/sql/SRC/Server/SqlSrvCheckStatement.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include "SqlSrvStrings.h"
       
    18 
       
    19 _LIT(KCreateSql, "CREATE");
       
    20 _LIT(KIndexSql,  "INDEX");
       
    21 _LIT(KIfSql,     "IF");
       
    22 _LIT(KNotSql,    "NOT");
       
    23 _LIT(KExistsSql, "EXISTS");
       
    24 _LIT(KDotSql,    ".");
       
    25 
       
    26 
       
    27 /**
       
    28 Determines whether the specified string is an SQL statement that is supported as a config operation.
       
    29 Currently only 'CREATE INDEX' SQL statements are supported. If necessary adds qualifyed DB name  to index name.
       
    30 
       
    31 @param aStatementIn The string to be checked to determine if it is a supported SQL statement
       
    32 @param aDbName Logical database name: "main" for the main database or attached database name
       
    33 @param aStatementOut Output parameter. Buffer for the constructed statement (if the statement is supported).
       
    34 
       
    35 @return True if the aStatementIn statement is supported, aStatementOut will contain the zero-terminated
       
    36 		aStatementIn statement with the database name in it, if aDbName is a name of an attached database
       
    37 */	
       
    38 TBool IsStatementSupported(const TDesC& aStatementIn, const TDesC& aDbName, TDes& aStatementOut)
       
    39 	{	
       
    40 	//'CREATE INDEX' is the only statement currently supported.
       
    41 	//We must allow for whitespace between the 'CREATE' and the 'INDEX'
       
    42 	TLex stmtParser(aStatementIn);
       
    43 	TPtrC firstToken = stmtParser.NextToken(); // extract the first token of the statement
       
    44 	if(firstToken.CompareF(KCreateSql()) != 0)
       
    45 		{
       
    46 		return EFalse;	
       
    47 		}
       
    48 	//The first token is 'CREATE', now skip any whitespace between 
       
    49 	//it and the next token and ensure that the next token is 'INDEX'
       
    50 	TPtrC secondToken = stmtParser.NextToken(); // skip any whitespace and extract the next token
       
    51 	if(secondToken.CompareF(KIndexSql()) != 0)
       
    52 		{
       
    53 		return EFalse;	
       
    54 		}
       
    55 	//The second token is 'INDEX'
       
    56 	TPtrC curToken = stmtParser.NextToken();
       
    57 	// skip optional [IF NOT EXISTS]
       
    58 	if(curToken.CompareF(KIfSql()) == 0)
       
    59 		{
       
    60 		curToken.Set(stmtParser.NextToken());
       
    61 		if(curToken.CompareF(KNotSql()) != 0)
       
    62 			{
       
    63 			return EFalse;	
       
    64 			}
       
    65 		curToken.Set(stmtParser.NextToken());
       
    66 		if(curToken.CompareF(KExistsSql()) != 0)
       
    67 			{
       
    68 			return EFalse;	
       
    69 			}
       
    70 		curToken.Set(stmtParser.NextToken());
       
    71 		}
       
    72 	// we got '[database-name.]index-name'
       
    73 	if(curToken.Find(KDotSql)==KErrNotFound && aDbName.CompareF(KMainDb16) != 0) // just index-name
       
    74 		{
       
    75 		// Add DB name prefix before table name in the case of CREATE INDEX
       
    76 		aStatementOut.Copy(aStatementIn.Ptr(), curToken.Ptr()-aStatementIn.Ptr());
       
    77 		aStatementOut.Append(aDbName);
       
    78 		aStatementOut.Append(KDotSql);
       
    79 		aStatementOut.Append(curToken);
       
    80 		aStatementOut.Append(stmtParser.Remainder());
       
    81 		}
       
    82 	else
       
    83 		{
       
    84 		aStatementOut.Copy(aStatementIn);
       
    85 		}
       
    86 	aStatementOut.Append(TChar(0)); // SQLite requires statements to be zero-terminated
       
    87 	return ETrue;
       
    88 	}