|
1 // Copyright (c) 2005-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 <s32mem.h> |
|
17 #include "SqlStmtSession.h" //RSqlStatementSession |
|
18 |
|
19 /** |
|
20 Sends a request to the server to close the statement handle. |
|
21 Closes the session object. |
|
22 */ |
|
23 void RSqlStatementSession::Close() |
|
24 { |
|
25 if(iDbSession && iHandle > 0) |
|
26 { |
|
27 (void)iDbSession->SendReceive(::MakeMsgCode(ESqlSrvStmtClose, ESqlSrvStatementHandle, iHandle)); |
|
28 } |
|
29 iDbSession = NULL; |
|
30 iHandle = -1; |
|
31 } |
|
32 |
|
33 /** |
|
34 Binds the statement parameters and sends a request to the SQL server to move to the next record which satisfies the |
|
35 condition of the prepared SQL statement. |
|
36 If there is a valid next record, the method transfers the column values from the server. |
|
37 |
|
38 @param aParamBuf It references RSqlBufFlat object where the parameter values are stored. |
|
39 @param aColumnBuf It references RSqlBufFlat object where the column values will be stored. |
|
40 |
|
41 @return KSqlAtRow, the record data is ready for processing by the caller; |
|
42 KSqlAtEnd, there is no more record data; |
|
43 KSqlErrBusy, the database file is locked; |
|
44 KErrNoMemory, an out of memory condition has occurred - the statement |
|
45 will be reset; |
|
46 KSqlErrGeneral, a run-time error has occured - this function must not |
|
47 be called again; |
|
48 KSqlErrMisuse, this function has been called after a previous call |
|
49 returned KSqlAtEnd or KSqlErrGeneral. |
|
50 KSqlErrStmtExpired, the SQL statement has expired (if new functions or |
|
51 collating sequences have been registered or if an |
|
52 authorizer function has been added or changed); |
|
53 */ |
|
54 TInt RSqlStatementSession::BindNext(const RSqlBufFlat& aParamBuf, RSqlBufFlat& aColumnBuf) |
|
55 { |
|
56 TPtrC8 prmData(aParamBuf.BufDes()); |
|
57 TIpcArgs ipcArgs; |
|
58 ipcArgs.Set(0, prmData.Length()); |
|
59 ipcArgs.Set(1, &prmData); |
|
60 return DoBindNext(ESqlSrvStmtBindNext, ipcArgs, aColumnBuf); |
|
61 } |
|
62 |
|
63 /** |
|
64 Implements RSqlStatementSession::Next() and RSqlStatementSession::BindNext(). |
|
65 Sends a "Next" command to the server combined with optional "Bind" command. |
|
66 In a single IPC call the statement parameters will be bound and the current row columns - returned. |
|
67 If the client side flat buffer is not big enough, a second IPC call will be made after reallocating the buffer. |
|
68 |
|
69 Usage of the IPC call arguments: |
|
70 Arg 0: [out] parameter buffer length in bytes |
|
71 Arg 1: [out] parameter buffer |
|
72 Arg 2: [out] column buffer length in bytes |
|
73 Arg 3: [in/out] column buffer |
|
74 |
|
75 @see RSqlStatementSession::Next() |
|
76 @see RSqlStatementSession::BindNext() |
|
77 */ |
|
78 TInt RSqlStatementSession::DoBindNext(TSqlSrvFunction aFunction, TIpcArgs& aIpcArgs, RSqlBufFlat& aColumnBuf) |
|
79 { |
|
80 aColumnBuf.Reset(); |
|
81 aIpcArgs.Set(2, aColumnBuf.MaxSize()); |
|
82 aIpcArgs.Set(3, &aColumnBuf.BufPtr()); |
|
83 TInt err = DbSession().SendReceive(::MakeMsgCode(aFunction, ESqlSrvStatementHandle, iHandle), aIpcArgs); |
|
84 if(err > KSqlClientBufOverflowCode) |
|
85 { |
|
86 err = Retry(aColumnBuf, err - KSqlClientBufOverflowCode, ESqlColumnValuesBuf); |
|
87 if(err == KErrNone) |
|
88 { |
|
89 err = KSqlAtRow; |
|
90 } |
|
91 } |
|
92 return err; |
|
93 } |
|
94 |
|
95 /** |
|
96 Sends a command to the server for retrieving parameter names or column names. |
|
97 |
|
98 Usage of the IPC call arguments: |
|
99 Arg 0: [out] buffer length in bytes |
|
100 Arg 1: [in/out] buffer |
|
101 */ |
|
102 TInt RSqlStatementSession::GetNames(TSqlSrvFunction aFunction, RSqlBufFlat& aNameBuf) |
|
103 { |
|
104 aNameBuf.Reset(); |
|
105 TPtr8& ptr = aNameBuf.BufPtr(); |
|
106 TInt err = DbSession().SendReceive(::MakeMsgCode(aFunction, ESqlSrvStatementHandle, iHandle), TIpcArgs(ptr.MaxLength(), &ptr)); |
|
107 if(err > KSqlClientBufOverflowCode) |
|
108 { |
|
109 err = Retry(aNameBuf, err - KSqlClientBufOverflowCode, aFunction == ESqlSrvStmtColumnNames ? ESqlColumnNamesBuf : ESqlParamNamesBuf); |
|
110 } |
|
111 return err; |
|
112 } |
|
113 |
|
114 /** |
|
115 Sends a command to the server for retrieving specified data (aWhat parameter). |
|
116 |
|
117 Usage of the IPC call arguments: |
|
118 Arg 0: [out] The type of the data to be retrieved |
|
119 Arg 1: [in/out] Data buffer |
|
120 */ |
|
121 TInt RSqlStatementSession::Retry(RSqlBufFlat& aBufFlat, TInt aSize, TSqlBufFlatType aWhat) |
|
122 { |
|
123 aBufFlat.Reset(); |
|
124 TInt err = aBufFlat.ReAlloc(aSize); |
|
125 if(err == KErrNone) |
|
126 { |
|
127 TPtr8& ptr = aBufFlat.BufPtr(); |
|
128 err = DbSession().SendReceive(::MakeMsgCode(ESqlSrvStmtBufFlat, ESqlSrvStatementHandle, iHandle), TIpcArgs(aWhat, &ptr)); |
|
129 } |
|
130 return err; |
|
131 } |
|
132 |
|
133 /** |
|
134 Sends a command to the server for retrieving a string with ";" separated declared types of columns |
|
135 The caller is responsible for deleting the result buffer. |
|
136 |
|
137 @param aColumnCount The number of the columns which the statement has. |
|
138 @return A pointer to a heap based HBufC object, containing the column names, separated with ";". |
|
139 |
|
140 Usage of the IPC call arguments: |
|
141 Arg 0: [out] Input buffer max length |
|
142 Arg 1: [in/out] Data buffer, will be filled with the declared types of columns, separated with ";" |
|
143 */ |
|
144 HBufC* RSqlStatementSession::GetDeclColumnTypesL(TInt aColumnCount) |
|
145 { |
|
146 //The longest DBMS data type, represented as text, is "LONG VARBINARY" - 14 characters. |
|
147 //So we can safely assume that 20 characters buffer space per DBMS column type is enough. |
|
148 const TInt KLongestDbmsTypeLength = 20; |
|
149 HBufC* colTypeBuf = HBufC::NewLC(aColumnCount * KLongestDbmsTypeLength); |
|
150 TPtr ptr = colTypeBuf->Des(); |
|
151 TInt err = DbSession().SendReceive(::MakeMsgCode(ESqlSrvStmtDeclColumnTypes, ESqlSrvStatementHandle, iHandle), TIpcArgs(ptr.MaxLength(), &ptr)); |
|
152 __SQLLEAVE_IF_ERROR(err); |
|
153 CleanupStack::Pop(colTypeBuf); |
|
154 return colTypeBuf; |
|
155 } |