Public paste
Undefined
By: Guest | Date: Oct 28 2011 16:45 | Format: UnrealScript | Expires: never | Size: 6.83 KB | Hits: 687

  1. /**
  2.  * Database - Data provider class.
  3.  * Base class for all data providers gathering data from loaded databases.
  4.  */
  5. class DB_DataProvider extends Actor
  6.         dependson(DB_DLLAPI);
  7.  
  8.  
  9. //=============================================================================
  10. // Structs & Enumerations
  11. //=============================================================================
  12. struct SBindInfo
  13. {
  14.   var int    BindType;
  15.  
  16.   var string BindParam;
  17.   var int    BindIdx;
  18.  
  19.   var string BindValue;
  20. };
  21.  
  22. struct SDataItem
  23. {
  24.   var array<string> Data;
  25. };
  26.  
  27. struct SDataSet
  28. {
  29.   var array<string> ColumnNames;
  30.   var array<int>    ColumnTypes;
  31.   var array<int>    ColumnDetails;
  32.  
  33.   var int              DataCount;
  34.   var array<SDataItem> Data;
  35. };
  36.  
  37. enum ECommandType
  38. {
  39.   ECT_SELECT,
  40.   ECT_UPDATE,
  41.   ECT_INSERT,
  42.   ECT_DELETE,
  43.  
  44.   ECT_COMMAND_COUNT
  45. };
  46.  
  47. //=============================================================================
  48. // Variables
  49. //=============================================================================
  50. var DB_DLLAPI mDLLAPI;  // backreference to DB_Manager.mDLLAPI
  51.  
  52. var int mDBId;          // referenced database ID
  53. var string mStatement;  // data provider statement
  54. var string mCommands[4];
  55.  
  56. var private SDataSet mDataSet;  // current dataset of this provider
  57.  
  58.  
  59. //=============================================================================
  60. // Functions
  61. //=============================================================================
  62. function InitCommands()
  63. {
  64.   local int il;
  65.  
  66.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  67.     return;
  68.  
  69.   for(il=0; il<ECT_COMMAND_COUNT; il++)
  70.     mDLLAPI.SQL_prepareStatement(mCommands[il]);  
  71. }
  72.  
  73. function SetCommand(ECommandType aType, string aCommand)
  74. {
  75.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  76.     return;
  77.  
  78.   mCommands[aType] = aCommand;
  79.   mDLLAPI.SQL_prepareStatement(aCommand);
  80. }
  81.  
  82.  
  83. //=============================================================================
  84. // Database handling
  85. //=============================================================================
  86. function fill(int aResultIdx, out SDataSet aDataSet)
  87. {
  88.   local int il, lColumnCount;
  89.  
  90.   local string lColumnName;
  91.   local int lColumnType;
  92.   local int lColumnDetail;
  93.  
  94.   local SDataItem lNewDataItem;
  95.   local int lNewIntVal, lNewDataCount;
  96.   local float lNewFloatVal;
  97.   local string lNewStrVal;
  98.  
  99.   aDataSet.ColumnNames.Length = 0;
  100.   aDataSet.ColumnTypes.Length = 0;
  101.   aDataSet.ColumnDetails.Length = 0;
  102.   aDataSet.DataCount = 0;
  103.   aDataSet.Data.Length = 0;
  104.  
  105.   if(aResultIdx >= 0)
  106.   {
  107.     lColumnCount = mDLLAPI.SQL_getColumnCount(aResultIdx);
  108.     for(il=0; il<lColumnCount; ++il)
  109.     {
  110.       `DB_InitString(lColumnName,255);
  111.  
  112.       mDLLAPI.SQL_getColumnInfo(aResultIdx, il, lColumnName, lColumnType, lColumnDetail);
  113.       aDataSet.ColumnNames[il] = lColumnName;
  114.       aDataSet.ColumnTypes[il] = lColumnType;
  115.       aDataSet.ColumnDetails[il] = lColumnDetail;
  116.     }
  117.     lNewDataCount = 0;
  118.     while(mDLLAPI.SQL_nextResult(aResultIdx))
  119.     {
  120.       lNewDataCount++;
  121.       lNewDataItem.Data.Length = 0;
  122.       for(il=0; il<lColumnCount; ++il)
  123.       {
  124.         switch(aDataSet.ColumnTypes[il])
  125.         {
  126.           case 0:
  127.             lNewDataItem.Data[il] = "";
  128.             break;
  129.           case 1:
  130.             mDLLAPI.SQL_getIntVal(aResultIdx, aDataSet.ColumnNames[il], lNewIntVal);
  131.             lNewDataItem.Data[il] = string(lNewIntVal);
  132.             break;
  133.           case 2:
  134.             mDLLAPI.SQL_getFloatVal(aResultIdx, aDataSet.ColumnNames[il], lNewFloatVal);
  135.             lNewDataItem.Data[il] = string(lNewFloatVal);
  136.             break;
  137.           case 3:
  138.             `DB_InitString(lNewStrVal, aDataSet.ColumnDetails[il]);
  139.             mDLLAPI.SQL_getStringVal(aResultIdx, aDataSet.ColumnNames[il], lNewStrVal);
  140.             lNewDataItem.Data[il] = lNewStrVal;
  141.             break;
  142.         }
  143.       }
  144.       aDataSet.Data[aDataSet.Data.Length] = lNewDataItem;
  145.     }
  146.     aDataSet.DataCount = lNewDataCount;
  147.   }
  148. }
  149.  
  150. //=============================================================================
  151. // DataProvider control functions
  152. //=============================================================================
  153. function Select()
  154. {
  155.   local int lResIdx;
  156.  
  157.   if (mCommands[ECT_SELECT] == "")
  158.     return;
  159.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  160.     return;
  161.  
  162.   mDLLAPI.SQL_prepareStatement(mCommands[ECT_SELECT]);
  163.   lResIdx = mDLLAPI.SQL_executeStatement();
  164.   fill(lResIdx, mDataSet);
  165. }
  166.  
  167. function Update()
  168. {
  169.   if (mCommands[ECT_UPDATE] == "")
  170.     return;
  171.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  172.     return;
  173.  
  174.   mDLLAPI.SQL_prepareStatement(mCommands[ECT_UPDATE]);
  175.   mDLLAPI.SQL_executeStatement();
  176.   Select();
  177. }
  178.  
  179. function Insert()
  180. {
  181.   if (mCommands[ECT_INSERT] == "")
  182.     return;
  183.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  184.     return;
  185.  
  186.   mDLLAPI.SQL_prepareStatement(mCommands[ECT_INSERT]);
  187.   mDLLAPI.SQL_executeStatement();
  188.   Select();
  189. }
  190.  
  191. function Delete()
  192. {
  193.   if (mCommands[ECT_DELETE] == "")
  194.     return;
  195.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  196.     return;
  197.  
  198.   mDLLAPI.SQL_prepareStatement(mCommands[ECT_DELETE]);
  199.   mDLLAPI.SQL_executeStatement();
  200.   Select();
  201. }
  202.  
  203. function BindValues(ECommandType aCommand, array<SBindInfo> aBindingInfo)
  204. {
  205.   local int il, lBindInfoCount;
  206.  
  207.   if (mCommands[aCommand] == "")
  208.     return;
  209.   if (!mDLLAPI.SQL_selectDatabase(mDBId))
  210.     return;
  211.  
  212.   mDLLAPI.SQL_prepareStatement(mCommands[ECT_DELETE]);
  213.  
  214.   lBindInfoCount = aBindingInfo.Length;
  215.  
  216.   for(il=0; il<lBindInfoCount; il++)
  217.   {
  218.     switch(aBindingInfo[il].BindType)
  219.     {
  220.       case 0:
  221.         break;
  222.       case 1:
  223.         if (aBindingInfo[il].BindParam != "" && aBindingInfo[il].BindIdx == -1)
  224.           mDLLAPI.SQL_bindNamedValueInt(aBindingInfo[il].BindParam, int(aBindingInfo[il].BindValue));
  225.         else if (aBindingInfo[il].BindIdx >= 0)
  226.           mDLLAPI.SQL_bindValueInt(aBindingInfo[il].BindIdx, int(aBindingInfo[il].BindValue));
  227.         break;
  228.       case 2:
  229.         if (aBindingInfo[il].BindParam != "" && aBindingInfo[il].BindIdx == -1)
  230.           mDLLAPI.SQL_bindNamedValueFloat(aBindingInfo[il].BindParam, float(aBindingInfo[il].BindValue));
  231.         else if (aBindingInfo[il].BindIdx >= 0)
  232.           mDLLAPI.SQL_bindValueFloat(aBindingInfo[il].BindIdx, float(aBindingInfo[il].BindValue));
  233.         break;
  234.       case 3:
  235.         if (aBindingInfo[il].BindParam != "" && aBindingInfo[il].BindIdx == -1)
  236.           mDLLAPI.SQL_bindNamedValueString(aBindingInfo[il].BindParam, aBindingInfo[il].BindValue);
  237.         else if (aBindingInfo[il].BindIdx >= 0)
  238.           mDLLAPI.SQL_bindValueString(aBindingInfo[il].BindIdx, aBindingInfo[il].BindValue);
  239.         break;
  240.     }
  241.   }
  242. }
  243.  
  244.  
  245. DefaultProperties
  246. {
  247.   TickGroup=TG_DuringAsyncWork
  248.  
  249.         bHidden=TRUE
  250.         Physics=PHYS_None
  251.         bReplicateMovement=FALSE
  252.         bStatic=FALSE
  253.         bNoDelete=FALSE
  254.  
  255.         Name="Default__DB_DataProvider"
  256. }
  257.