DataMgr 1.2 Documentation: Replace SQL Inserts and Updates

Replace SQL Inserts and Updates

DataMgr can act as a replace for SQL inserts and updates with several advantages.

Example with SQL (leaving out cfqueryparam and data-type checking for brevity):

<cfif isDefined("Form.employee_id") AND isNumeric(Form.employee_id)>
	<cfquery datasource="#request.dsn#">
	UPDATE	employees
	SET		FirstName = '#Form.FirstName#',
			LastName = '#Form.LastName#',
			department_id = #Val(Form.department_id)#,
			HireDate = #CreateODBCDate(Form.HireDate)#,
			LastPromotionDate = #CreateODBCDate(Form.LastPromotionDate)#,
			isManagement = #Form.isManagement#
	WHERE	employee_id = #Val(form.employee_id)#
	</cfquery>
<cfelse>
	<cfquery datasource="#request.dsn#">
	INSERT INTO employees (
			FirstName,
			LastName,
			department_id,
			HireDate,
			LastPromotionDate,
			isManagement
	)
	VALUES (
			'#Form.FirstName#',
			'#Form.LastName#',
			#Val(Form.department_id)#,
			#CreateODBCDate(Form.HireDate)#,
			#CreateODBCDate(Form.LastPromotionDate)#,
			#Form.isManagement#
	)
	</cfquery>
</cfif>

Example with DataMgr (which does include cfqueryparam and data-type checking):

<cfif isDefined("Form.employee_id") AND isNumeric(Form.employee_id)>
	<cfset Application.DataMgr.insertRecord("employees",Form)>
<cfelse>
	<cfset Application.DataMgr.updateRecord("employees",Form)>
</cfif>

Example where DataMgr determines if it should be an insert or an update:

<cfset Application.DataMgr.saveRecord("employees",Form)>

Example with DataMgr returning the primary key (if it is a simple primary key):

<cfset employee_id = Application.DataMgr.saveRecord("employees",Form)>

(insertRecord and updateRecord will both also return the primary key)

If your form field names don't match your table field names, you can also build your own structure to pass into DataMgr.

Note: OnExists

If you are using insertRecord, it has another optional argument; "OnExists" with the following possible values which determine what DataMgr will do if a record already exists with matching data: