Guest User

Untitled

a guest
Aug 6th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.42 KB | None | 0 0
  1. diff --git a/wheels/model/initialization.cfm b/wheels/model/initialization.cfm
  2. index 3755c2c..24a6c94 100644
  3. --- a/wheels/model/initialization.cfm
  4. +++ b/wheels/model/initialization.cfm
  5. @@ -45,140 +45,144 @@
  6. // run developer's init method if it exists
  7. if (StructKeyExists(variables, "init"))
  8. init();
  9. -
  10. - // make sure that the tablename has the respected prefix
  11. - table(getTableNamePrefix() & tableName());
  12. -
  13. - // load the database adapter
  14. - variables.wheels.class.adapter = $createObjectFromRoot(path="#application.wheels.wheelsComponentPath#", fileName="Connection", method="init", datasource="#variables.wheels.class.connection.datasource#", username="#variables.wheels.class.connection.username#", password="#variables.wheels.class.connection.password#");
  15. -
  16. - // get columns for the table
  17. - loc.columns = variables.wheels.class.adapter.$getColumns(tableName());
  18. -
  19. +
  20. variables.wheels.class.propertyList = "";
  21. - variables.wheels.class.columnList = "";
  22. - loc.processedColumns = "";
  23. - loc.iEnd = loc.columns.recordCount;
  24. - for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  25. + // add calculated properties
  26. + variables.wheels.class.calculatedPropertyList = "";
  27. +
  28. + if(len(tableName()))
  29. {
  30. - // set up properties and column mapping
  31. - if (!ListFind(loc.processedColumns, loc.columns["column_name"][loc.i]))
  32. + // make sure that the tablename has the respected prefix
  33. + table(getTableNamePrefix() & tableName());
  34. +
  35. + // load the database adapter
  36. + variables.wheels.class.adapter = $createObjectFromRoot(path="#application.wheels.wheelsComponentPath#", fileName="Connection", method="init", datasource="#variables.wheels.class.connection.datasource#", username="#variables.wheels.class.connection.username#", password="#variables.wheels.class.connection.password#");
  37. +
  38. + // get columns for the table
  39. + loc.columns = variables.wheels.class.adapter.$getColumns(tableName());
  40. +
  41. + variables.wheels.class.columnList = "";
  42. + loc.processedColumns = "";
  43. + loc.iEnd = loc.columns.recordCount;
  44. + for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  45. {
  46. - loc.property = loc.columns["column_name"][loc.i]; // default the column to map to a property with the same name
  47. - for (loc.key in variables.wheels.class.mapping)
  48. + // set up properties and column mapping
  49. + if (!ListFind(loc.processedColumns, loc.columns["column_name"][loc.i]))
  50. {
  51. - if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type == "column" && variables.wheels.class.mapping[loc.key].value == loc.property)
  52. + loc.property = loc.columns["column_name"][loc.i]; // default the column to map to a property with the same name
  53. + for (loc.key in variables.wheels.class.mapping)
  54. {
  55. - // developer has chosen to map this column to a property with a different name so set that here
  56. - loc.property = loc.key;
  57. - break;
  58. + if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type == "column" && variables.wheels.class.mapping[loc.key].value == loc.property)
  59. + {
  60. + // developer has chosen to map this column to a property with a different name so set that here
  61. + loc.property = loc.key;
  62. + break;
  63. + }
  64. }
  65. - }
  66. - loc.type = SpanExcluding(loc.columns["type_name"][loc.i], "( ");
  67. -
  68. - // set the info we need for each property
  69. - variables.wheels.class.properties[loc.property] = {};
  70. - variables.wheels.class.properties[loc.property].dataType = loc.type;
  71. - variables.wheels.class.properties[loc.property].type = variables.wheels.class.adapter.$getType(loc.type, loc.columns["decimal_digits"][loc.i]);
  72. - variables.wheels.class.properties[loc.property].column = loc.columns["column_name"][loc.i];
  73. - variables.wheels.class.properties[loc.property].scale = loc.columns["decimal_digits"][loc.i];
  74. -
  75. - // get a boolean value for whether this column can be set to null or not
  76. - // if we don't get a boolean back we try to translate y/n to proper boolean values in cfml (yes/no)
  77. - variables.wheels.class.properties[loc.property].nullable = Trim(loc.columns["is_nullable"][loc.i]);
  78. - if (!IsBoolean(variables.wheels.class.properties[loc.property].nullable))
  79. - variables.wheels.class.properties[loc.property].nullable = ReplaceList(variables.wheels.class.properties[loc.property].nullable, "N,Y", "No,Yes");
  80. -
  81. - variables.wheels.class.properties[loc.property].size = loc.columns["column_size"][loc.i];
  82. - variables.wheels.class.properties[loc.property].label = Humanize(loc.property);
  83. - variables.wheels.class.properties[loc.property].validationtype = variables.wheels.class.adapter.$getValidationType(variables.wheels.class.properties[loc.property].type);
  84. -
  85. - if (StructKeyExists(variables.wheels.class.mapping, loc.property)) {
  86. - if (StructKeyExists(variables.wheels.class.mapping[loc.property], "label"))
  87. - variables.wheels.class.properties[loc.property].label = variables.wheels.class.mapping[loc.property].label;
  88. - if (StructKeyExists(variables.wheels.class.mapping[loc.property], "defaultValue"))
  89. - variables.wheels.class.properties[loc.property].defaultValue = variables.wheels.class.mapping[loc.property].defaultValue;
  90. - }
  91. -
  92. - if (loc.columns["is_primarykey"][loc.i])
  93. - {
  94. - setPrimaryKey(loc.property);
  95. - }
  96. - else if (variables.wheels.class.automaticValidations and not ListFindNoCase("#application.wheels.timeStampOnCreateProperty#,#application.wheels.timeStampOnUpdateProperty#,#application.wheels.softDeleteProperty#", loc.property))
  97. - {
  98. - // set nullable validations if the developer has not
  99. - loc.defaultValidationsAllowBlank = variables.wheels.class.properties[loc.property].nullable;
  100. - if (!variables.wheels.class.properties[loc.property].nullable and !Len(loc.columns["column_default_value"][loc.i]) and !$validationExists(property=loc.property, validation="validatesPresenceOf"))
  101. + loc.type = SpanExcluding(loc.columns["type_name"][loc.i], "( ");
  102. +
  103. + // set the info we need for each property
  104. + variables.wheels.class.properties[loc.property] = {};
  105. + variables.wheels.class.properties[loc.property].dataType = loc.type;
  106. + variables.wheels.class.properties[loc.property].type = variables.wheels.class.adapter.$getType(loc.type, loc.columns["decimal_digits"][loc.i]);
  107. + variables.wheels.class.properties[loc.property].column = loc.columns["column_name"][loc.i];
  108. + variables.wheels.class.properties[loc.property].scale = loc.columns["decimal_digits"][loc.i];
  109. +
  110. + // get a boolean value for whether this column can be set to null or not
  111. + // if we don't get a boolean back we try to translate y/n to proper boolean values in cfml (yes/no)
  112. + variables.wheels.class.properties[loc.property].nullable = Trim(loc.columns["is_nullable"][loc.i]);
  113. + if (!IsBoolean(variables.wheels.class.properties[loc.property].nullable))
  114. + variables.wheels.class.properties[loc.property].nullable = ReplaceList(variables.wheels.class.properties[loc.property].nullable, "N,Y", "No,Yes");
  115. +
  116. + variables.wheels.class.properties[loc.property].size = loc.columns["column_size"][loc.i];
  117. + variables.wheels.class.properties[loc.property].label = Humanize(loc.property);
  118. + variables.wheels.class.properties[loc.property].validationtype = variables.wheels.class.adapter.$getValidationType(variables.wheels.class.properties[loc.property].type);
  119. +
  120. + if (StructKeyExists(variables.wheels.class.mapping, loc.property)) {
  121. + if (StructKeyExists(variables.wheels.class.mapping[loc.property], "label"))
  122. + variables.wheels.class.properties[loc.property].label = variables.wheels.class.mapping[loc.property].label;
  123. + if (StructKeyExists(variables.wheels.class.mapping[loc.property], "defaultValue"))
  124. + variables.wheels.class.properties[loc.property].defaultValue = variables.wheels.class.mapping[loc.property].defaultValue;
  125. + }
  126. +
  127. + if (loc.columns["is_primarykey"][loc.i])
  128. + {
  129. + setPrimaryKey(loc.property);
  130. + }
  131. + else if (variables.wheels.class.automaticValidations and not ListFindNoCase("#application.wheels.timeStampOnCreateProperty#,#application.wheels.timeStampOnUpdateProperty#,#application.wheels.softDeleteProperty#", loc.property))
  132. {
  133. - validatesPresenceOf(properties=loc.property);
  134. + // set nullable validations if the developer has not
  135. + loc.defaultValidationsAllowBlank = variables.wheels.class.properties[loc.property].nullable;
  136. + if (!variables.wheels.class.properties[loc.property].nullable and !Len(loc.columns["column_default_value"][loc.i]) and !$validationExists(property=loc.property, validation="validatesPresenceOf"))
  137. + {
  138. + validatesPresenceOf(properties=loc.property);
  139. + }
  140. + // always allowblank if a database default or validatesPresenceOf() has been set
  141. + if (Len(loc.columns["column_default_value"][loc.i]) or $validationExists(property=loc.property, validation="validatesPresenceOf"))
  142. + loc.defaultValidationsAllowBlank = true;
  143. + // set length validations if the developer has not
  144. + if (variables.wheels.class.properties[loc.property].validationtype eq "string" and !$validationExists(property=loc.property, validation="validatesLengthOf"))
  145. + validatesLengthOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, maximum=variables.wheels.class.properties[loc.property].size);
  146. + // set numericality validations if the developer has not
  147. + if (ListFindNoCase("integer,float", variables.wheels.class.properties[loc.property].validationtype) and !$validationExists(property=loc.property, validation="validatesNumericalityOf"))
  148. + validatesNumericalityOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, onlyInteger=(variables.wheels.class.properties[loc.property].validationtype eq "integer"));
  149. + // set date validations if the developer has not (checks both dates or times as per the IsDate() function)
  150. + if (variables.wheels.class.properties[loc.property].validationtype eq "datetime" and !$validationExists(property=loc.property, validation="validatesFormatOf"))
  151. + validatesFormatOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, type="date");
  152. }
  153. - // always allowblank if a database default or validatesPresenceOf() has been set
  154. - if (Len(loc.columns["column_default_value"][loc.i]) or $validationExists(property=loc.property, validation="validatesPresenceOf"))
  155. - loc.defaultValidationsAllowBlank = true;
  156. - // set length validations if the developer has not
  157. - if (variables.wheels.class.properties[loc.property].validationtype eq "string" and !$validationExists(property=loc.property, validation="validatesLengthOf"))
  158. - validatesLengthOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, maximum=variables.wheels.class.properties[loc.property].size);
  159. - // set numericality validations if the developer has not
  160. - if (ListFindNoCase("integer,float", variables.wheels.class.properties[loc.property].validationtype) and !$validationExists(property=loc.property, validation="validatesNumericalityOf"))
  161. - validatesNumericalityOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, onlyInteger=(variables.wheels.class.properties[loc.property].validationtype eq "integer"));
  162. - // set date validations if the developer has not (checks both dates or times as per the IsDate() function)
  163. - if (variables.wheels.class.properties[loc.property].validationtype eq "datetime" and !$validationExists(property=loc.property, validation="validatesFormatOf"))
  164. - validatesFormatOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, type="date");
  165. +
  166. + variables.wheels.class.propertyList = ListAppend(variables.wheels.class.propertyList, loc.property);
  167. + variables.wheels.class.columnList = ListAppend(variables.wheels.class.columnList, variables.wheels.class.properties[loc.property].column);
  168. + loc.processedColumns = ListAppend(loc.processedColumns, loc.columns["column_name"][loc.i]);
  169. }
  170. -
  171. - variables.wheels.class.propertyList = ListAppend(variables.wheels.class.propertyList, loc.property);
  172. - variables.wheels.class.columnList = ListAppend(variables.wheels.class.columnList, variables.wheels.class.properties[loc.property].column);
  173. - loc.processedColumns = ListAppend(loc.processedColumns, loc.columns["column_name"][loc.i]);
  174. }
  175. - }
  176. -
  177. - // raise error when no primary key has been defined for the table
  178. - if (!Len(primaryKeys()))
  179. - {
  180. - $throw(type="Wheels.NoPrimaryKey", message="No primary key exists on the `#tableName()#` table.", extendedInfo="Set an appropriate primary key on the `#tableName()#` table.");
  181. - }
  182. -
  183. - // add calculated properties
  184. - variables.wheels.class.calculatedPropertyList = "";
  185. - for (loc.key in variables.wheels.class.mapping)
  186. - {
  187. - if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type != "column")
  188. +
  189. + // raise error when no primary key has been defined for the table
  190. + if (!Len(primaryKeys()))
  191. {
  192. - variables.wheels.class.calculatedPropertyList = ListAppend(variables.wheels.class.calculatedPropertyList, loc.key);
  193. - variables.wheels.class.calculatedProperties[loc.key] = {};
  194. - variables.wheels.class.calculatedProperties[loc.key][variables.wheels.class.mapping[loc.key].type] = variables.wheels.class.mapping[loc.key].value;
  195. + $throw(type="Wheels.NoPrimaryKey", message="No primary key exists on the `#tableName()#` table.", extendedInfo="Set an appropriate primary key on the `#tableName()#` table.");
  196. }
  197. - }
  198.  
  199. - // set up soft deletion and time stamping if the necessary columns in the table exist
  200. - if (Len(application.wheels.softDeleteProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.softDeleteProperty))
  201. - {
  202. - variables.wheels.class.softDeletion = true;
  203. - variables.wheels.class.softDeleteColumn = variables.wheels.class.properties[application.wheels.softDeleteProperty].column;
  204. - }
  205. - else
  206. - {
  207. - variables.wheels.class.softDeletion = false;
  208. - }
  209. -
  210. - if (Len(application.wheels.timeStampOnCreateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnCreateProperty))
  211. - {
  212. - variables.wheels.class.timeStampingOnCreate = true;
  213. - variables.wheels.class.timeStampOnCreateProperty = application.wheels.timeStampOnCreateProperty;
  214. - }
  215. - else
  216. - {
  217. - variables.wheels.class.timeStampingOnCreate = false;
  218. - }
  219. -
  220. - if (Len(application.wheels.timeStampOnUpdateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnUpdateProperty))
  221. - {
  222. - variables.wheels.class.timeStampingOnUpdate = true;
  223. - variables.wheels.class.timeStampOnUpdateProperty = application.wheels.timeStampOnUpdateProperty;
  224. - }
  225. - else
  226. - {
  227. - variables.wheels.class.timeStampingOnUpdate = false;
  228. + for (loc.key in variables.wheels.class.mapping)
  229. + {
  230. + if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type != "column")
  231. + {
  232. + variables.wheels.class.calculatedPropertyList = ListAppend(variables.wheels.class.calculatedPropertyList, loc.key);
  233. + variables.wheels.class.calculatedProperties[loc.key] = {};
  234. + variables.wheels.class.calculatedProperties[loc.key][variables.wheels.class.mapping[loc.key].type] = variables.wheels.class.mapping[loc.key].value;
  235. + }
  236. + }
  237. +
  238. + // set up soft deletion and time stamping if the necessary columns in the table exist
  239. + if (Len(application.wheels.softDeleteProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.softDeleteProperty))
  240. + {
  241. + variables.wheels.class.softDeletion = true;
  242. + variables.wheels.class.softDeleteColumn = variables.wheels.class.properties[application.wheels.softDeleteProperty].column;
  243. + }
  244. + else
  245. + {
  246. + variables.wheels.class.softDeletion = false;
  247. + }
  248. +
  249. + if (Len(application.wheels.timeStampOnCreateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnCreateProperty))
  250. + {
  251. + variables.wheels.class.timeStampingOnCreate = true;
  252. + variables.wheels.class.timeStampOnCreateProperty = application.wheels.timeStampOnCreateProperty;
  253. + }
  254. + else
  255. + {
  256. + variables.wheels.class.timeStampingOnCreate = false;
  257. + }
  258. +
  259. + if (Len(application.wheels.timeStampOnUpdateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnUpdateProperty))
  260. + {
  261. + variables.wheels.class.timeStampingOnUpdate = true;
  262. + variables.wheels.class.timeStampOnUpdateProperty = application.wheels.timeStampOnUpdateProperty;
  263. + }
  264. + else
  265. + {
  266. + variables.wheels.class.timeStampingOnUpdate = false;
  267. + }
  268. }
  269. </cfscript>
  270. <cfreturn this>
  271. diff --git a/wheels/model/properties.cfm b/wheels/model/properties.cfm
  272. index 727b53f..fb96b34 100644
  273. --- a/wheels/model/properties.cfm
  274. +++ b/wheels/model/properties.cfm
  275. @@ -58,7 +58,7 @@
  276. <cfargument name="column" type="string" required="false" default="" hint="The name of the column in the database table to map the property to.">
  277. <cfargument name="sql" type="string" required="false" default="" hint="A SQL expression to use to calculate the property value.">
  278. <cfargument name="label" type="string" required="false" default="" hint="A custom label for this property to be referenced in the interface and error messages.">
  279. - <cfargument name="defaultValue" type="string" required="false" hint="A default value for this property.">
  280. + <cfargument name="defaultValue" type="any" required="false" hint="A default value for this property.">
  281. <cfscript>
  282. // validate setup
  283. if (Len(arguments.column) and Len(arguments.sql))
Add Comment
Please, Sign In to add comment