PageRenderTime 51ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 18ms app.codeStats 0ms

/wheels/model/adapters/PostgreSQL.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 75 lines | 68 code | 7 blank | 0 comment | 0 complexity | 0debedd5573b892e218ad7b3942f879d MD5 | raw file
 1<cfcomponent extends="Base" output="false">
 2
 3	<cffunction name="$generatedKey" returntype="string" access="public" output="false">
 4		<cfreturn "lastId">
 5	</cffunction>
 6	
 7	<cffunction name="$randomOrder" returntype="string" access="public" output="false">
 8		<cfreturn "random()">
 9	</cffunction>
10	
11	<cffunction name="$getType" returntype="string" access="public" output="false">
12		<cfargument name="type" type="string" required="true">
13		<cfscript>
14			var loc = {};
15			switch(arguments.type)
16			{
17				case "bigint": case "int8": case "bigserial": case "serial8": {loc.returnValue = "cf_sql_bigint"; break;}
18				case "bit": case "varbit": {loc.returnValue = "cf_sql_bit"; break;}
19				case "bool": case "boolean": {loc.returnValue = "cf_sql_varchar"; break;}
20				case "bytea": {loc.returnValue = "cf_sql_binary"; break;}
21				case "char": case "character": {loc.returnValue = "cf_sql_char"; break;}
22				case "date": case "timestamp": case "timestamptz": {loc.returnValue = "cf_sql_timestamp"; break;}
23				case "decimal": case "double": case "precision": case "float": case "float4": case "float8": {loc.returnValue = "cf_sql_decimal"; break;}
24				case "integer": case "int": case "int4": case "serial": case "oid": {loc.returnValue = "cf_sql_integer"; break;}  // oid cols should probably be avoided - placed here for completeness
25				case "numeric": case "smallmoney": case "money": {loc.returnValue = "cf_sql_numeric"; break;}  // postgres has deprecated the money type: http://www.postgresql.org/docs/8.1/static/datatype-money.html
26				case "real": {loc.returnValue = "cf_sql_real"; break;}
27				case "smallint": case "int2": {loc.returnValue = "cf_sql_smallint"; break;}
28				case "text": {loc.returnValue = "cf_sql_longvarchar"; break;}
29				case "time": case "timetz": {loc.returnValue = "cf_sql_time"; break;}
30				case "varchar": case "varying": case "bpchar": case "uuid": {loc.returnValue = "cf_sql_varchar"; break;}
31			}
32		</cfscript>
33		<cfreturn loc.returnValue>
34	</cffunction>
35	
36	<cffunction name="$query" returntype="struct" access="public" output="false">
37		<cfargument name="sql" type="array" required="true">
38		<cfargument name="limit" type="numeric" required="false" default=0>
39		<cfargument name="offset" type="numeric" required="false" default=0>
40		<cfargument name="parameterize" type="boolean" required="true">
41		<cfargument name="$primaryKey" type="string" required="false" default="">
42		<cfscript>
43			var loc = {};
44			arguments.sql = $removeColumnAliasesInOrderClause(arguments.sql);
45			arguments.sql = $addColumnsToSelectAndGroupBy(arguments.sql);
46			loc.returnValue = $performQuery(argumentCollection=arguments);
47		</cfscript>
48		<cfreturn loc.returnValue>
49	</cffunction>
50	
51	<cffunction name="$identitySelect" returntype="any" access="public" output="false">
52		<cfargument name="queryAttributes" type="struct" required="true">
53		<cfargument name="result" type="struct" required="true">
54		<cfargument name="primaryKey" type="string" required="true">
55		<cfset var loc = {}>
56		<cfset var query = {}>
57		<cfset loc.sql = Trim(arguments.result.sql)>
58		<cfif Left(loc.sql, 11) IS "INSERT INTO" AND NOT StructKeyExists(arguments.result, $generatedKey())>
59			<cfset loc.startPar = Find("(", loc.sql) + 1>
60			<cfset loc.endPar = Find(")", loc.sql)>
61			<cfset loc.columnList = ReplaceList(Mid(loc.sql, loc.startPar, (loc.endPar-loc.startPar)), "#Chr(10)#,#Chr(13)#, ", ",,")>
62			<cfif NOT ListFindNoCase(loc.columnList, ListFirst(arguments.primaryKey))>
63				<!--- Railo/ACF doesn't support PostgreSQL natively when it comes to returning the primary key value of the last inserted record so we have to do it manually by using the sequence --->
64				<cfset loc.returnValue = {}>
65				<cfset loc.tbl = SpanExcluding(Right(loc.sql, Len(loc.sql)-12), " ")>
66				<cfquery attributeCollection="#arguments.queryAttributes#">SELECT currval(pg_get_serial_sequence('#loc.tbl#', '#arguments.primaryKey#')) AS lastId</cfquery>
67				<cfset loc.returnValue[$generatedKey()] = query.name.lastId>
68				<cfreturn loc.returnValue>
69			</cfif>
70		</cfif>
71	</cffunction>
72
73	<cfinclude template="../../plugins/injection.cfm">
74
75</cfcomponent>