/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. <cffunction name="$generatedKey" returntype="string" access="public" output="false">
  3. <cfreturn "lastId">
  4. </cffunction>
  5. <cffunction name="$randomOrder" returntype="string" access="public" output="false">
  6. <cfreturn "random()">
  7. </cffunction>
  8. <cffunction name="$getType" returntype="string" access="public" output="false">
  9. <cfargument name="type" type="string" required="true">
  10. <cfscript>
  11. var loc = {};
  12. switch(arguments.type)
  13. {
  14. case "bigint": case "int8": case "bigserial": case "serial8": {loc.returnValue = "cf_sql_bigint"; break;}
  15. case "bit": case "varbit": {loc.returnValue = "cf_sql_bit"; break;}
  16. case "bool": case "boolean": {loc.returnValue = "cf_sql_varchar"; break;}
  17. case "bytea": {loc.returnValue = "cf_sql_binary"; break;}
  18. case "char": case "character": {loc.returnValue = "cf_sql_char"; break;}
  19. case "date": case "timestamp": case "timestamptz": {loc.returnValue = "cf_sql_timestamp"; break;}
  20. case "decimal": case "double": case "precision": case "float": case "float4": case "float8": {loc.returnValue = "cf_sql_decimal"; break;}
  21. 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
  22. 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
  23. case "real": {loc.returnValue = "cf_sql_real"; break;}
  24. case "smallint": case "int2": {loc.returnValue = "cf_sql_smallint"; break;}
  25. case "text": {loc.returnValue = "cf_sql_longvarchar"; break;}
  26. case "time": case "timetz": {loc.returnValue = "cf_sql_time"; break;}
  27. case "varchar": case "varying": case "bpchar": case "uuid": {loc.returnValue = "cf_sql_varchar"; break;}
  28. }
  29. </cfscript>
  30. <cfreturn loc.returnValue>
  31. </cffunction>
  32. <cffunction name="$query" returntype="struct" access="public" output="false">
  33. <cfargument name="sql" type="array" required="true">
  34. <cfargument name="limit" type="numeric" required="false" default=0>
  35. <cfargument name="offset" type="numeric" required="false" default=0>
  36. <cfargument name="parameterize" type="boolean" required="true">
  37. <cfargument name="$primaryKey" type="string" required="false" default="">
  38. <cfscript>
  39. var loc = {};
  40. arguments.sql = $removeColumnAliasesInOrderClause(arguments.sql);
  41. arguments.sql = $addColumnsToSelectAndGroupBy(arguments.sql);
  42. loc.returnValue = $performQuery(argumentCollection=arguments);
  43. </cfscript>
  44. <cfreturn loc.returnValue>
  45. </cffunction>
  46. <cffunction name="$identitySelect" returntype="any" access="public" output="false">
  47. <cfargument name="queryAttributes" type="struct" required="true">
  48. <cfargument name="result" type="struct" required="true">
  49. <cfargument name="primaryKey" type="string" required="true">
  50. <cfset var loc = {}>
  51. <cfset var query = {}>
  52. <cfset loc.sql = Trim(arguments.result.sql)>
  53. <cfif Left(loc.sql, 11) IS "INSERT INTO" AND NOT StructKeyExists(arguments.result, $generatedKey())>
  54. <cfset loc.startPar = Find("(", loc.sql) + 1>
  55. <cfset loc.endPar = Find(")", loc.sql)>
  56. <cfset loc.columnList = ReplaceList(Mid(loc.sql, loc.startPar, (loc.endPar-loc.startPar)), "#Chr(10)#,#Chr(13)#, ", ",,")>
  57. <cfif NOT ListFindNoCase(loc.columnList, ListFirst(arguments.primaryKey))>
  58. <!--- 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 --->
  59. <cfset loc.returnValue = {}>
  60. <cfset loc.tbl = SpanExcluding(Right(loc.sql, Len(loc.sql)-12), " ")>
  61. <cfquery attributeCollection="#arguments.queryAttributes#">SELECT currval(pg_get_serial_sequence('#loc.tbl#', '#arguments.primaryKey#')) AS lastId</cfquery>
  62. <cfset loc.returnValue[$generatedKey()] = query.name.lastId>
  63. <cfreturn loc.returnValue>
  64. </cfif>
  65. </cfif>
  66. </cffunction>
  67. <cfinclude template="../../plugins/injection.cfm">
  68. </cfcomponent>