| 1 | <cfcomponent hint="I read Object data from a MSSQL database." extends="reactor.data.abstractObjectDao"> |
|---|
| 2 | |
|---|
| 3 | <cffunction name="read" access="public" hint="I populate an Object object based on it's name" output="false" returntype="void"> |
|---|
| 4 | <cfargument name="Object" hint="I am the object to populate." required="yes" type="any" _type="reactor.core.object" /> |
|---|
| 5 | |
|---|
| 6 | <!--- get all field data ---> |
|---|
| 7 | <cfset readObject(arguments.Object) /> |
|---|
| 8 | <cfset readFields(arguments.Object) /> |
|---|
| 9 | </cffunction> |
|---|
| 10 | |
|---|
| 11 | <cffunction name="readObject" access="private" hint="I confirm that this object exists at all. If not, I throw an error." output="false" returntype="void"> |
|---|
| 12 | <cfargument name="Object" hint="I am the object to check on." required="yes" type="any" _type="reactor.core.object" /> |
|---|
| 13 | <cfset var qObject = 0 /> |
|---|
| 14 | |
|---|
| 15 | <cfquery name="qObject" datasource="#getDsn()#" username="#getUsername()#" password="#getPassword()#"> |
|---|
| 16 | SELECT database() as DATABASE_NAME, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE |
|---|
| 17 | FROM information_schema.TABLES |
|---|
| 18 | WHERE TABLE_SCHEMA = database() AND TABLE_NAME = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="64" value="#arguments.Object.getName()#" /> |
|---|
| 19 | </cfquery> |
|---|
| 20 | |
|---|
| 21 | <cfif qObject.recordCount> |
|---|
| 22 | <!--- set the owner ---> |
|---|
| 23 | <cfset arguments.Object.setDatabase(qObject.DATABASE_NAME) /> |
|---|
| 24 | <cfset arguments.Object.setType(Iif(qObject.TABLE_TYPE IS "BASE TABLE", DE('table'), DE('view'))) /> |
|---|
| 25 | <cfelse> |
|---|
| 26 | <cfthrow type="Reactor.NoSuchObject" /> |
|---|
| 27 | </cfif> |
|---|
| 28 | </cffunction> |
|---|
| 29 | |
|---|
| 30 | <cffunction name="readFields" access="private" hint="I populate the table with fields." output="false" returntype="void"> |
|---|
| 31 | <cfargument name="Object" hint="I am the object to read fields into." required="yes" type="any" _type="reactor.core.object" /> |
|---|
| 32 | <cfset var qFields = 0 /> |
|---|
| 33 | <cfset var Field = 0 /> |
|---|
| 34 | |
|---|
| 35 | <cfquery name="qFields" datasource="#getDsn()#" username="#getUsername()#" password="#getPassword()#"> |
|---|
| 36 | SELECT COLUMN_NAME as name, |
|---|
| 37 | CASE |
|---|
| 38 | WHEN COLUMN_KEY = 'PRI' THEN 'true' |
|---|
| 39 | ELSE 'false' |
|---|
| 40 | END as primaryKey, |
|---|
| 41 | CASE |
|---|
| 42 | WHEN EXTRA = 'auto_increment' THEN 'true' |
|---|
| 43 | ELSE 'false' |
|---|
| 44 | END as identity, |
|---|
| 45 | CASE |
|---|
| 46 | WHEN IS_NULLABLE = 'Yes' THEN 'true' |
|---|
| 47 | ELSE 'false' |
|---|
| 48 | END as nullable, |
|---|
| 49 | DATA_TYPE as dbDataType, |
|---|
| 50 | CASE |
|---|
| 51 | WHEN CHARACTER_OCTET_LENGTH IS NULL THEN 0 |
|---|
| 52 | ELSE CHARACTER_OCTET_LENGTH |
|---|
| 53 | END as length, |
|---|
| 54 | COLUMN_DEFAULT as 'default' |
|---|
| 55 | FROM information_schema.COLUMNS |
|---|
| 56 | WHERE TABLE_SCHEMA = Database() AND TABLE_NAME = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="128" value="#arguments.Object.getName()#" /> |
|---|
| 57 | ORDER BY ORDINAL_POSITION |
|---|
| 58 | </cfquery> |
|---|
| 59 | |
|---|
| 60 | <cfloop query="qFields"> |
|---|
| 61 | <!--- create the field ---> |
|---|
| 62 | <cfset Field = StructNew() /> |
|---|
| 63 | <cfset Field.name = qFields.name /> |
|---|
| 64 | <cfset Field.primaryKey = qFields.primaryKey /> |
|---|
| 65 | <cfset Field.identity = qFields.identity /> |
|---|
| 66 | <cfset Field.nullable = qFields.nullable /> |
|---|
| 67 | <cfset Field.dbDataType = qFields.dbDataType /> |
|---|
| 68 | <cfset Field.cfDataType = getCfDataType(qFields.dbDataType) /> |
|---|
| 69 | <cfset Field.cfSqlType = getCfSqlType(qFields.dbDataType) /> |
|---|
| 70 | <cfset Field.length = qFields.length /> |
|---|
| 71 | <cfset Field.default = getDefault(qFields.default, Field.cfDataType, Field.nullable) /> |
|---|
| 72 | <cfset Field.sequenceName = "" /> |
|---|
| 73 | <cfset Field.readOnly = "false" /> |
|---|
| 74 | <cfset Field.scale = "0" /> |
|---|
| 75 | |
|---|
| 76 | <!--- add the field to the table ---> |
|---|
| 77 | <cfset arguments.Object.addField(Field) /> |
|---|
| 78 | </cfloop> |
|---|
| 79 | |
|---|
| 80 | </cffunction> |
|---|
| 81 | |
|---|
| 82 | <cffunction name="getDefault" access="public" hint="I get a default value for a cf datatype." output="false" returntype="any" _returntype="string"> |
|---|
| 83 | <cfargument name="sqlDefaultValue" hint="I am the default value defined by SQL." required="yes" type="any" _type="string" /> |
|---|
| 84 | <cfargument name="typeName" hint="I am the cf type name to get a default value for." required="yes" type="any" _type="string" /> |
|---|
| 85 | <cfargument name="nullable" hint="I indicate if the column is nullable." required="yes" type="any" _type="boolean" /> |
|---|
| 86 | |
|---|
| 87 | <cfswitch expression="#arguments.typeName#"> |
|---|
| 88 | <cfcase value="numeric"> |
|---|
| 89 | <cfif IsNumeric(arguments.sqlDefaultValue)> |
|---|
| 90 | <cfreturn arguments.sqlDefaultValue /> |
|---|
| 91 | <cfelseif arguments.nullable> |
|---|
| 92 | <cfreturn ""/> |
|---|
| 93 | <cfelse> |
|---|
| 94 | <cfreturn 0 /> |
|---|
| 95 | </cfif> |
|---|
| 96 | </cfcase> |
|---|
| 97 | <cfcase value="binary"> |
|---|
| 98 | <cfreturn "" /> |
|---|
| 99 | </cfcase> |
|---|
| 100 | <cfcase value="boolean"> |
|---|
| 101 | <cfif IsBoolean(arguments.sqlDefaultValue)> |
|---|
| 102 | <cfreturn Iif(arguments.sqlDefaultValue, DE(true), DE(false)) /> |
|---|
| 103 | <cfelse> |
|---|
| 104 | <cfreturn false /> |
|---|
| 105 | </cfif> |
|---|
| 106 | </cfcase> |
|---|
| 107 | <cfcase value="string"> |
|---|
| 108 | <!--- insure that the first and last characters are "'" ---> |
|---|
| 109 | <cfif Left(arguments.sqlDefaultValue, 1) IS "'" AND Right(arguments.sqlDefaultValue, 1) IS "'"> |
|---|
| 110 | <!--- mssql functions must be constants. for this reason I can convert anything quoted in single quotes safely to a string ---> |
|---|
| 111 | <cfset arguments.sqlDefaultValue = Mid(arguments.sqlDefaultValue, 2, Len(arguments.sqlDefaultValue)-2) /> |
|---|
| 112 | <cfset arguments.sqlDefaultValue = Replace(arguments.sqlDefaultValue, "''", "'", "All") /> |
|---|
| 113 | <cfset arguments.sqlDefaultValue = Replace(arguments.sqlDefaultValue, """", """""", "All") /> |
|---|
| 114 | <cfreturn arguments.sqlDefaultValue /> |
|---|
| 115 | <cfelse> |
|---|
| 116 | <cfreturn "" /> |
|---|
| 117 | </cfif> |
|---|
| 118 | </cfcase> |
|---|
| 119 | <cfcase value="date"> |
|---|
| 120 | <cfif Left(arguments.sqlDefaultValue, 1) IS "'" AND Right(arguments.sqlDefaultValue, 1) IS "'"> |
|---|
| 121 | <cfreturn Mid(arguments.sqlDefaultValue, 2, Len(arguments.sqlDefaultValue)-2) /> |
|---|
| 122 | <cfelseif arguments.sqlDefaultValue IS "getDate()"> |
|---|
| 123 | <cfreturn "##Now()##" /> |
|---|
| 124 | <cfelse> |
|---|
| 125 | <cfreturn "" /> |
|---|
| 126 | </cfif> |
|---|
| 127 | </cfcase> |
|---|
| 128 | <cfdefaultcase> |
|---|
| 129 | <cfreturn "" /> |
|---|
| 130 | </cfdefaultcase> |
|---|
| 131 | </cfswitch> |
|---|
| 132 | </cffunction> |
|---|
| 133 | |
|---|
| 134 | <cffunction name="getCfSqlType" access="private" hint="I translate the MSSQL data type names into ColdFusion cf_sql_xyz names" output="false" returntype="any" _returntype="string"> |
|---|
| 135 | <cfargument name="typeName" hint="I am the type name to translate" required="yes" type="any" _type="string" /> |
|---|
| 136 | |
|---|
| 137 | <cfswitch expression="#arguments.typeName#"> |
|---|
| 138 | <cfcase value="bit,bool,boolean"> |
|---|
| 139 | <cfreturn "cf_sql_bit" /> |
|---|
| 140 | </cfcase> |
|---|
| 141 | <cfcase value="tinyint"> |
|---|
| 142 | <cfreturn "cf_sql_tinyint" /> |
|---|
| 143 | </cfcase> |
|---|
| 144 | <cfcase value="smallint,year"> |
|---|
| 145 | <cfreturn "cf_sql_smallint" /> |
|---|
| 146 | </cfcase> |
|---|
| 147 | <cfcase value="mediumint,int,integer"> |
|---|
| 148 | <cfreturn "cf_sql_integer" /> |
|---|
| 149 | </cfcase> |
|---|
| 150 | <cfcase value="bigint"> |
|---|
| 151 | <cfreturn "cf_sql_bigint" /> |
|---|
| 152 | </cfcase> |
|---|
| 153 | <cfcase value="float"> |
|---|
| 154 | <cfreturn "cf_sql_float" /> |
|---|
| 155 | </cfcase> |
|---|
| 156 | <cfcase value="double,double percision"> |
|---|
| 157 | <cfreturn "cf_sql_double" /> |
|---|
| 158 | </cfcase> |
|---|
| 159 | <cfcase value="decimal,dec"> |
|---|
| 160 | <cfreturn "cf_sql_float" /> |
|---|
| 161 | </cfcase> |
|---|
| 162 | <cfcase value="date"> |
|---|
| 163 | <cfreturn "cf_sql_date" /> |
|---|
| 164 | </cfcase> |
|---|
| 165 | <cfcase value="datetime"> |
|---|
| 166 | <cfreturn "cf_sql_timestamp" /> |
|---|
| 167 | </cfcase> |
|---|
| 168 | <cfcase value="timestamp"> |
|---|
| 169 | <cfreturn "cf_sql_timestamp" /> |
|---|
| 170 | </cfcase> |
|---|
| 171 | <cfcase value="char"> |
|---|
| 172 | <cfreturn "cf_sql_char" /> |
|---|
| 173 | </cfcase> |
|---|
| 174 | <cfcase value="varchar,enum,set"> |
|---|
| 175 | <cfreturn "cf_sql_varchar" /> |
|---|
| 176 | </cfcase> |
|---|
| 177 | <cfcase value="tinytext,text,mediumtext,longtext"> |
|---|
| 178 | <cfreturn "cf_sql_longvarchar" /> |
|---|
| 179 | </cfcase> |
|---|
| 180 | <cfcase value="varbinary"> |
|---|
| 181 | <cfreturn "cf_sql_varbinary" /> |
|---|
| 182 | </cfcase> |
|---|
| 183 | <cfcase value="tinyblob,blob,mediumblob,longblob"> |
|---|
| 184 | <cfreturn "cf_sql_blob" /> |
|---|
| 185 | </cfcase> |
|---|
| 186 | <cfcase value="binary"> |
|---|
| 187 | <cfreturn "cf_sql_binary" /> |
|---|
| 188 | </cfcase> |
|---|
| 189 | <cfcase value="time"> |
|---|
| 190 | <cfreturn "cf_sql_time" /> |
|---|
| 191 | </cfcase> |
|---|
| 192 | </cfswitch> |
|---|
| 193 | |
|---|
| 194 | <cfthrow message="Unsupported (or incorrectly supported) database datatype: #arguments.typeName#." /> |
|---|
| 195 | </cffunction> |
|---|
| 196 | |
|---|
| 197 | <cffunction name="getCfDataType" access="private" hint="I translate the MSSQL data type names into ColdFusion data type names" output="false" returntype="any" _returntype="string"> |
|---|
| 198 | <cfargument name="typeName" hint="I am the type name to translate" required="yes" type="any" _type="string" /> |
|---|
| 199 | |
|---|
| 200 | <cfswitch expression="#arguments.typeName#"> |
|---|
| 201 | <cfcase value="bit,bool,boolean"> |
|---|
| 202 | <cfreturn "boolean" /> |
|---|
| 203 | </cfcase> |
|---|
| 204 | <cfcase value="tinyint,smallint,mediumint,int,integer,bigint,float,double,double percision,decimal,dec,year"> |
|---|
| 205 | <cfreturn "numeric" /> |
|---|
| 206 | </cfcase> |
|---|
| 207 | <cfcase value="date,datetime,timestamp"> |
|---|
| 208 | <cfreturn "date" /> |
|---|
| 209 | </cfcase> |
|---|
| 210 | <cfcase value="time,enum,set"> |
|---|
| 211 | <cfreturn "string" /> |
|---|
| 212 | </cfcase> |
|---|
| 213 | <cfcase value="char,varchar,tinytext,text,mediumtext,longtext"> |
|---|
| 214 | <cfreturn "string" /> |
|---|
| 215 | </cfcase> |
|---|
| 216 | <cfcase value="binary,varbinary,tinyblob,blob,mediumblob,longblob"> |
|---|
| 217 | <cfreturn "binary" /> |
|---|
| 218 | </cfcase> |
|---|
| 219 | </cfswitch> |
|---|
| 220 | |
|---|
| 221 | <cfthrow message="Unsupported (or incorrectly supported) database datatype: #arguments.typeName#." /> |
|---|
| 222 | |
|---|
| 223 | </cffunction> |
|---|
| 224 | |
|---|
| 225 | </cfcomponent> |
|---|