/plugins/dbmigrate/basefunctions.cfm

http://raihan.googlecode.com/ · ColdFusion · 111 lines · 95 code · 6 blank · 10 comment · 27 complexity · a4deadac3ffc36088a61147a47529ebd MD5 · raw file

  1. <!--- we include wheels global functions for:-
  2. pluralize
  3. model
  4. $file
  5. $dbinfo
  6. --->
  7. <cfinclude template="../../wheels/global/functions.cfm">
  8. <cffunction name="announce" access="public">
  9. <cfargument name="message" type="string" required="yes">
  10. <cfparam name="Request.migrationOutput" default="">
  11. <cfset Request.migrationOutput = Request.migrationOutput & arguments.message & chr(13)>
  12. </cffunction>
  13. <cffunction name="$execute" access="private">
  14. <cfargument name="sql" type="string" required="yes">
  15. <cfscript>
  16. // trim and remove trailing semicolon (appears to cause problems for Oracle thin client JDBC driver)
  17. arguments.sql = REReplace(trim(arguments.sql),";$","","ONE");
  18. if(IsDefined("Request.migrationSQLFile")) {
  19. $file(action="append",file=Request.migrationSQLFile,output="#arguments.sql#;",addNewLine="yes",fixNewLine="yes");
  20. }
  21. </cfscript>
  22. <cfquery datasource="#application.wheels.dataSourceName#" username="#application.wheels.dataSourceUserName#" password="#application.wheels.dataSourcePassword#">
  23. #PreserveSingleQuotes(arguments.sql)#
  24. </cfquery>
  25. </cffunction>
  26. <cffunction name="$getDBType" returntype="string" access="private" output="false">
  27. <cfscript>
  28. var loc = {};
  29. loc.info = $dbinfo(type="version",datasource=application.wheels.dataSourceName,username=application.wheels.dataSourceUserName,password=application.wheels.dataSourcePassword);
  30. if (loc.info.driver_name Contains "SQLServer" || loc.info.driver_name Contains "Microsoft SQL Server")
  31. loc.adapterName = "MicrosoftSQLServer";
  32. else if (loc.info.driver_name Contains "MySQL")
  33. loc.adapterName = "MySQL";
  34. else if (loc.info.driver_name Contains "Oracle")
  35. loc.adapterName = "Oracle";
  36. else if (loc.info.driver_name Contains "PostgreSQL")
  37. loc.adapterName = "PostgreSQL";
  38. else if (loc.info.driver_name Contains "SQLite")
  39. loc.adapterName = "SQLite";
  40. else {
  41. loc.adapterName = "";
  42. }
  43. </cfscript>
  44. <cfreturn loc.adapterName>
  45. </cffunction>
  46. <cffunction name="$getForeignKeys" returntype="string" access="private" output="false">
  47. <cfargument name="table" type="string" required="yes">
  48. <cfscript>
  49. var loc = {};
  50. loc.foreignKeys = $dbinfo(type="foreignkeys",table=arguments.table,datasource=application.wheels.dataSourceName,username=application.wheels.dataSourceUserName,password=application.wheels.dataSourcePassword);
  51. loc.foreignKeyList = ValueList(loc.foreignKeys.FKCOLUMN_NAME);
  52. </cfscript>
  53. <cfreturn loc.foreignKeyList>
  54. </cffunction>
  55. <cffunction name="$getColumns" returntype="string" access="public" output="false">
  56. <cfargument name="tableName" type="string" required="yes" hint="table name">
  57. <cfset var loc = {}>
  58. <cfif $getDBType() eq "Oracle">
  59. <!--- oracle thin client jdbc throws error when usgin cfdbinfo to access column data --->
  60. <!--- because of this error wheels can't load models anyway so maybe we don't need to support this driver --->
  61. <cfquery name="loc.columns" datasource="#application.wheels.dataSourceName#" username="#application.wheels.dataSourceUserName#" password="#application.wheels.dataSourcePassword#">
  62. SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = '#this.name#'
  63. </cfquery>
  64. <cfelse>
  65. <!--- use cfdbinfo --->
  66. <cfset loc.columns = $dbinfo(type="columns",table=arguments.tableName,datasource=application.wheels.dataSourceName,username=application.wheels.dataSourceUserName,password=application.wheels.dataSourcePassword)>
  67. </cfif>
  68. <cfreturn ValueList(loc.columns.COLUMN_NAME)>
  69. </cffunction>
  70. <cffunction name="$getColumnDefinition" returntype="string" access="private">
  71. <cfargument name="tableName" type="string" required="yes" hint="table name">
  72. <cfargument name="columnName" type="string" required="yes" hint="column name">
  73. <cfset var loc = {}>
  74. <cfdbinfo name="loc.columns" type="columns" table="#arguments.tableName#" datasource="#application.wheels.dataSourceName#" username="#application.wheels.dataSourceUserName#" password="#application.wheels.dataSourcePassword#">
  75. <!--- <cfdump var="#loc#" abort> --->
  76. <cfscript>
  77. loc.columnDefinition = "";
  78. loc.iEnd = loc.columns.RecordCount;
  79. for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
  80. if(loc.columns["COLUMN_NAME"][loc.i] == arguments.columnName) {
  81. loc.columnType = loc.columns["TYPE_NAME"][loc.i];
  82. loc.columnDefinition = loc.columnType;
  83. if(ListFindNoCase("char,varchar,int,bigint,smallint,tinyint,binary,varbinary",loc.columnType)) {
  84. loc.columnDefinition = loc.columnDefinition & "(#loc.columns["COLUMN_SIZE"][loc.i]#)";
  85. } else if(ListFindNoCase("decimal,float,double",loc.columnType)) {
  86. loc.columnDefinition = loc.columnDefinition & "(#loc.columns["COLUMN_SIZE"][loc.i]#,#loc.columns["DECIMAL_DIGITS"][loc.i]#)";
  87. }
  88. if(loc.columns["IS_NULLABLE"][loc.i]) {
  89. loc.columnDefinition = loc.columnDefinition & " NULL";
  90. } else {
  91. loc.columnDefinition = loc.columnDefinition & " NOT NULL";
  92. }
  93. if(Len(loc.columns["COLUMN_DEFAULT_VALUE"][loc.i]) == 0) {
  94. loc.columnDefinition = loc.columnDefinition & " DEFAULT NULL";
  95. } else if(ListFindNoCase("char,varchar,binary,varbinary",loc.columnType)) {
  96. loc.columnDefinition = loc.columnDefinition & " DEFAULT '#loc.columns["COLUMN_DEFAULT_VALUE"][loc.i]#'";
  97. } else if(ListFindNoCase("int,bigint,smallint,tinyint,decimal,float,double",loc.columnType)) {
  98. loc.columnDefinition = loc.columnDefinition & " DEFAULT #loc.columns["COLUMN_DEFAULT_VALUE"][loc.i]#";
  99. }
  100. break;
  101. }
  102. }
  103. </cfscript>
  104. <cfreturn loc.columnDefinition>
  105. </cffunction>