persistentstorage/sql/SRC/Server/SqlSrvGetFirstSqlStmt.cpp
branchRCL_3
changeset 6 5ffdb8f2067f
parent 2 6862383cf555
child 8 fa9941cf3867
equal deleted inserted replaced
2:6862383cf555 6:5ffdb8f2067f
     1 // Copyright (c) 2006-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 "sqlite3.h"
       
    18 
       
    19 //This function searches aString argument for ';' occurences.
       
    20 //Every time when it finds a ';' character, the function places a 0 right after the ';' and
       
    21 //tests the just created, zero-terminated substring if it is a comlpete SQL statement.
       
    22 //
       
    23 //If it is a SQL statement, the function replaces the found ';' character with 0 and returns the just created
       
    24 //zero-terminated substring.Also the function modifies aString argument to point right after the found
       
    25 //SQL string. If it is not SQL statement, the function will continue the searching.
       
    26 //
       
    27 //If there is no ';' inside aString argument, the function returns the same string as a return result and
       
    28 //modifies aString argument - sets it to TPtr(NULL, 0, 0).
       
    29 //
       
    30 //The function expects aString argument to be zero-terminated.
       
    31 TPtrC GetFirstSqlStmt(TPtr& aString)
       
    32 	{
       
    33 	const TChar KDelimitier(';');
       
    34 	TPtr str(const_cast <TUint16*> (aString.Ptr()), aString.Length(), aString.Length());
       
    35 	TInt afterDelimitierPos = 0;
       
    36 	TInt pos;
       
    37 	while((pos = str.Locate(KDelimitier) + 1) > 0 && pos < str.Length())
       
    38 		{
       
    39 		//There is a possibility that the string which terminates with the found ';' is SQL statement.
       
    40 		//Zero terminate the string placing a zero right after ';' character and test it using sqlite3_complete16()
       
    41 		//call. If it is not SQL string, restore the original character and continue searching.
       
    42 		afterDelimitierPos += pos;
       
    43 		TChar ch = aString[afterDelimitierPos];
       
    44 		aString[afterDelimitierPos] = 0;
       
    45 		TInt res = sqlite3_complete16(aString.Ptr());
       
    46 		aString[afterDelimitierPos] = ch;
       
    47 		if(res)
       
    48 			{
       
    49 			str.Set(const_cast <TUint16*> (aString.Ptr()), afterDelimitierPos, afterDelimitierPos);	
       
    50 			//Replace the found ';' character with 0.
       
    51 			str[afterDelimitierPos - 1] = 0;
       
    52 			aString.Set(const_cast <TUint16*> (aString.Ptr()) + afterDelimitierPos, aString.Length() - afterDelimitierPos, aString.Length() - afterDelimitierPos);
       
    53 			return str;
       
    54 			}
       
    55 		str.Set(const_cast <TUint16*> (str.Ptr()) + pos, str.Length() - pos, str.Length() - pos);	
       
    56 		}
       
    57 	//aString argument does not contain valid SQL statement or there is no ';' character inside aString.
       
    58 	//Set aString to TPtr(NULL, 0, 0) and return the original string.
       
    59 	aString.Set(NULL, 0, 0);
       
    60 	return str;
       
    61 	}