PageRenderTime 112ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/model/serialize.cfm

http://cfwheels.googlecode.com/
ColdFusion | 176 lines | 160 code | 16 blank | 0 comment | 34 complexity | c54d3a718509f8cf77365f40531fe202 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cffunction name="$serializeQueryToObjects" access="public" output="false" returntype="any">
  2. <cfargument name="query" type="query" required="true" />
  3. <cfargument name="include" type="string" required="false" default="" />
  4. <cfargument name="callbacks" type="string" required="false" default="true" />
  5. <cfargument name="returnIncluded" type="string" required="false" default="true" />
  6. <cfscript>
  7. var loc = {};
  8. // grab our objects as structs first so we don't waste cpu creating objects we don't need
  9. loc.returnValue = $serializeQueryToStructs(argumentCollection=arguments);
  10. </cfscript>
  11. <cfreturn $serializeStructsToObjects(structs=loc.returnValue, argumentCollection=arguments) />
  12. </cffunction>
  13. <cffunction name="$serializeStructsToObjects" access="public" output="false" returntype="any">
  14. <cfargument name="structs" type="any" required="true" />
  15. <cfargument name="include" type="string" required="true" />
  16. <cfargument name="callbacks" type="string" required="true" />
  17. <cfargument name="returnIncluded" type="string" required="true" />
  18. <cfscript>
  19. var loc = {};
  20. if (IsStruct(arguments.structs))
  21. loc.returnValue = [ arguments.structs ];
  22. else if (IsArray(arguments.structs))
  23. loc.returnValue = arguments.structs;
  24. loc.iEnd = ArrayLen(loc.returnValue);
  25. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  26. {
  27. if (Len(arguments.include) && arguments.returnIncluded)
  28. {
  29. // create each object from the assocations before creating our root object
  30. loc.xEnd = ListLen(arguments.include);
  31. for (loc.x = 1; loc.x lte loc.xEnd; loc.x++)
  32. {
  33. loc.include = ListGetAt(arguments.include, loc.x);
  34. loc.model = model(variables.wheels.class.associations[loc.include].modelName);
  35. if (variables.wheels.class.associations[loc.include].type == "hasMany")
  36. {
  37. loc.jEnd = ArrayLen(loc.returnValue[loc.i][loc.include]);
  38. for (loc.j=1; loc.j <= loc.jEnd; loc.j++)
  39. loc.returnValue[loc.i][loc.include][loc.j] = loc.model.$createInstance(properties=loc.returnValue[loc.i][loc.include][loc.j], persisted=true, base=false, callbacks=arguments.callbacks);
  40. }
  41. else
  42. {
  43. // we have a hasOne or belongsTo assocation, so just add the object to the root object
  44. loc.returnValue[loc.i][loc.include] = loc.model.$createInstance(properties=loc.returnValue[loc.i][loc.include], persisted=true, base=false, callbacks=arguments.callbacks);
  45. }
  46. }
  47. }
  48. // create an instance
  49. loc.returnValue[loc.i] = $createInstance(properties=loc.returnValue[loc.i], persisted=true, callbacks=arguments.callbacks);
  50. }
  51. </cfscript>
  52. <cfreturn loc.returnValue />
  53. </cffunction>
  54. <cffunction name="$serializeQueryToStructs" access="public" output="false" returntype="any">
  55. <cfargument name="query" type="query" required="true" />
  56. <cfargument name="include" type="string" required="true" />
  57. <cfargument name="callbacks" type="string" required="true" />
  58. <cfargument name="returnIncluded" type="string" required="true" />
  59. <cfscript>
  60. var loc = {};
  61. loc.returnValue = [];
  62. loc.doneStructs = "";
  63. // loop through all of our records and create an object for each row in the query
  64. for (loc.i=1; loc.i <= arguments.query.recordCount; loc.i++)
  65. {
  66. // create a new struct
  67. loc.struct = $queryRowToStruct(properties=arguments.query, row=loc.i);
  68. loc.structHash = $hashedKey(loc.struct);
  69. if (!ListFind(loc.doneStructs, loc.structHash, Chr(7)))
  70. {
  71. if (Len(arguments.include) && arguments.returnIncluded)
  72. {
  73. // loop through our assocations to build nested objects attached to the main object
  74. loc.xEnd = ListLen(arguments.include);
  75. for (loc.x = 1; loc.x lte loc.xEnd; loc.x++)
  76. {
  77. loc.include = ListGetAt(arguments.include, loc.x);
  78. if (variables.wheels.class.associations[loc.include].type == "hasMany")
  79. {
  80. // we have a hasMany assocation, so loop through all of the records again to find the ones that belong to our root object
  81. loc.struct[loc.include] = [];
  82. loc.hasManyDoneStructs = "";
  83. // only get a reference to our model once per assocation
  84. loc.model = model(variables.wheels.class.associations[loc.include].modelName);
  85. for (loc.j=1; loc.j <= arguments.query.recordCount; loc.j++)
  86. {
  87. // is there anything we can do here to not instantiate an object if it is not going to be use or is already created
  88. // this extra instantiation is really slowing things down
  89. loc.hasManyStruct = loc.model.$queryRowToStruct(properties=arguments.query, row=loc.j, base=false);
  90. loc.hasManyStructHash = $hashedKey(loc.hasManyStruct);
  91. if (!ListFind(loc.hasManyDoneStructs, loc.hasManyStructHash, Chr(7)))
  92. {
  93. // create object instance from values in current query row if it belongs to the current object
  94. loc.primaryKeyColumnValues = "";
  95. loc.kEnd = ListLen(primaryKeys());
  96. for (loc.k=1; loc.k <= loc.kEnd; loc.k++)
  97. loc.primaryKeyColumnValues = ListAppend(loc.primaryKeyColumnValues, arguments.query[primaryKeys(loc.k)][loc.j]);
  98. if (Len(loc.model.$keyFromStruct(loc.hasManyStruct)) && this.$keyFromStruct(loc.struct) == loc.primaryKeyColumnValues)
  99. ArrayAppend(loc.struct[loc.include], loc.hasManyStruct);
  100. loc.hasManyDoneStructs = ListAppend(loc.hasManyDoneStructs, loc.hasManyStructHash, Chr(7));
  101. }
  102. }
  103. }
  104. else
  105. {
  106. // we have a hasOne or belongsTo assocation, so just add the object to the root object
  107. loc.struct[loc.include] = model(variables.wheels.class.associations[loc.include].modelName).$queryRowToStruct(properties=arguments.query, row=loc.i, base=false);
  108. }
  109. }
  110. }
  111. ArrayAppend(loc.returnValue, loc.struct);
  112. loc.doneStructs = ListAppend(loc.doneStructs, loc.structHash, Chr(7));
  113. }
  114. }
  115. </cfscript>
  116. <cfreturn loc.returnValue />
  117. </cffunction>
  118. <cffunction name="$queryRowToStruct" access="public" output="false" returntype="struct">
  119. <cfargument name="properties" type="any" required="true">
  120. <cfargument name="name" type="string" required="false" default="#variables.wheels.class.modelName#">
  121. <cfargument name="row" type="numeric" required="false" default="1">
  122. <cfargument name="base" type="boolean" required="false" default="true">
  123. <cfscript>
  124. var loc = {};
  125. loc.returnValue = {};
  126. loc.allProperties = ListAppend(variables.wheels.class.propertyList, variables.wheels.class.calculatedPropertyList);
  127. loc.iEnd = ListLen(loc.allProperties);
  128. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  129. {
  130. try // coldfusion has a problem with empty strings in queries for bit types
  131. {
  132. loc.iItem = ListGetAt(loc.allProperties, loc.i);
  133. if (!arguments.base && ListFindNoCase(arguments.properties.columnList, arguments.name & loc.iItem))
  134. loc.returnValue[loc.iItem] = arguments.properties[arguments.name & loc.iItem][arguments.row];
  135. else if (ListFindNoCase(arguments.properties.columnList, loc.iItem))
  136. loc.returnValue[loc.iItem] = arguments.properties[loc.iItem][arguments.row];
  137. }
  138. catch (Any e)
  139. {
  140. loc.returnValue[loc.iItem] = "";
  141. }
  142. }
  143. </cfscript>
  144. <cfreturn loc.returnValue />
  145. </cffunction>
  146. <cffunction name="$keyFromStruct" access="public" output="false" returntype="string">
  147. <cfargument name="struct" type="struct" required="true">
  148. <cfscript>
  149. var loc = {};
  150. loc.returnValue = "";
  151. loc.iEnd = ListLen(primaryKeys());
  152. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  153. {
  154. loc.property = primaryKeys(loc.i);
  155. if (StructKeyExists(arguments.struct, loc.property))
  156. loc.returnValue = ListAppend(loc.returnValue, arguments.struct[loc.property]);
  157. }
  158. </cfscript>
  159. <cfreturn loc.returnValue>
  160. </cffunction>