Shadowrun: Awakened 29 September 2011 - Build 871
CppSetRoutine.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 
00006 namespace RoutineAnalyzer
00007 {
00034     class CppSetRoutine : RoutineBase
00035     {
00036         public CppSetRoutine(string sql) : base(sql)
00037         {
00038         }
00039 
00044         public override string GetDeclaration()
00045         {
00046             StringBuilder builder = new StringBuilder();
00047             builder.Append("static __declspec(dllexport) void ");
00048             builder.Append(ParseHelper.ConvertUnderscoreToCamelCase(_name));
00049             builder.Append("(");
00050             foreach (RoutineParamBase param in _params)
00051             {
00052                 builder.Append(param.GetDeclaration());
00053                 if (param != _params.Last<RoutineParamBase>())
00054                     builder.Append(", ");
00055             }
00056 
00057             builder.Append(")");
00058             return builder.ToString();
00059         }
00060 
00061         public override string GetCode()
00062         {
00063             //start off with the declaration, then put in
00064             StringBuilder builder = new StringBuilder();
00065             builder.AppendLine(GetDeclaration().Replace("static __declspec(dllexport) ",""));
00066             builder.AppendLine("{");
00067 
00068             //create the prepared statement that we will call
00069             //this prepared statement include the routine name and a number of question marks
00070             //equal to the number of arguments
00071             builder.AppendLine("sql::Connection* connection = SraData::DbConnectionFactory::GetSingleton().getConnection();");
00072             //use auto_ptr so we don't lose any memory in here
00073             builder.AppendLine("std::auto_ptr<sql::PreparedStatement> pstmt;");
00074             builder.Append("pstmt.reset(connection->prepareStatement(\"CALL ");
00075             builder.Append(_name);
00076             builder.Append("(");
00077             for (int i = 0; i < _params.Count; ++i)
00078             {
00079                 builder.Append(_params[i].GetParameter());
00080                 if (_params[i] != _params.Last<RoutineParamBase>())
00081                     builder.Append(",");
00082             }
00083             builder.AppendLine(")\"));");
00084 
00085             //add the pre-execute code for each parameter
00086             for (int i = 0; i < _params.Count; ++i)
00087             {
00088                 string code = _params[i].GetPreExecuteCode();
00089                 if (string.IsNullOrEmpty(code))
00090                     continue;
00091                 builder.AppendLine(code);
00092             }
00093 
00094             //add the execute statement
00095             builder.AppendLine();
00096             builder.AppendLine("pstmt->execute();");
00097             builder.AppendLine();
00098 
00099             //add the post-execute code for each parameter
00100             for (int i = 0; i < _params.Count; ++i)
00101             {
00102                 string code = _params[i].GetPostExecuteCode();
00103                 if (string.IsNullOrEmpty(code))
00104                     continue;
00105                 builder.AppendLine(code);
00106             }
00107 
00108             builder.AppendLine("SraData::DbConnectionFactory::GetSingleton().recycleConnection(connection);");
00109 
00110             //close up the statement and return it
00111             builder.Append("}");
00112             return builder.ToString();
00113         }
00114 
00115         protected override RoutineParamBase CreateRoutineParam(string sql, int index)
00116         {
00117             return new CppSetRoutineParam(sql, index);
00118         }
00119     }
00120 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net