PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/dbmigrate/TableDefinition.cfc

http://raihan.googlecode.com/
ColdFusion CFScript | 283 lines | 265 code | 18 blank | 0 comment | 26 complexity | c9154e0d090587c156e818eec3c25b58 MD5 | raw file
  1<cfcomponent extends="Base">
  2	
  3	<cffunction name="init" returntype="TableDefinition" access="public">
  4		<cfargument name="adapter" type="any" required="yes" hint="database adapter">
  5		<cfargument name="name" type="string" required="yes" hint="table name in pluralized form">
  6		<cfargument name="force" type="boolean" required="no" default="true" hint="whether or not to drop table of same name before creating new one">
  7		<cfargument name="id" type="boolean" required="no" default="false" hint="if false, defines a table with no primary key">
  8		<cfargument name="primaryKey" type="string" required="no" default="id" hint="overrides default primary key">
  9		<cfscript>
 10		var loc = {};
 11		loc.args = "adapter,name,force";
 12		loc.iEnd = ListLen(loc.args);
 13		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
 14			loc.argumentName = ListGetAt(loc.args,loc.i);
 15			if(StructKeyExists(arguments,loc.argumentName)) {
 16				this[loc.argumentName] = arguments[loc.argumentName];
 17			}
 18		}
 19		this.columns = ArrayNew(1);
 20		if(arguments.id && Len(arguments.primaryKey)) {
 21			column(columnName=arguments.primaryKey,columnType="primaryKey");
 22		}
 23		this.foreignKeys = ArrayNew(1);
 24		</cfscript>
 25		<cfreturn this>
 26	</cffunction>
 27	
 28	<cffunction name="column" returntype="void" access="public" hint="adds a column to table definition">
 29		<cfargument name="columnName" type="string" required="yes" hint="column name">
 30		<cfargument name="columnType" type="string" required="yes" hint="column type">
 31		<cfargument name="default" type="string" required="no" hint="default value">
 32		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
 33		<cfargument name="limit" type="numeric" required="no" hint="character or integer size">
 34		<cfargument name="precision" type="numeric" required="no" hint="number of digits the column can hold">
 35		<cfargument name="scale" type="numeric" required="no" hint="number of digits that can be placed to the right of the decimal point (must be less than or equal to precision)">
 36		<cfscript>
 37		var loc = {};
 38		arguments.adapter = this.adapter;
 39		arguments.name = arguments.columnName;
 40		arguments.type = arguments.columnType;
 41		loc.column = CreateObject("component","ColumnDefinition").init(argumentCollection=arguments);
 42		ArrayAppend(this.columns,loc.column);
 43		</cfscript>
 44	</cffunction>
 45	
 46	<cffunction name="binary" returntype="void" access="public" hint="adds binary columns to table definition">
 47		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
 48		<cfargument name="default" type="string" required="no" hint="default value">
 49		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
 50		<cfscript>
 51		var loc = {};
 52		arguments.columnType = "binary";
 53		loc.iEnd = ListLen(arguments.columnNames);
 54		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
 55			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
 56			column(argumentCollection=arguments);
 57		}
 58		</cfscript>
 59	</cffunction>
 60
 61	<cffunction name="boolean" returntype="void" access="public" hint="adds boolean columns to table definition">
 62		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
 63		<cfargument name="default" type="string" required="no" hint="default value">
 64		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
 65		<cfscript>
 66		var loc = {};
 67		arguments.columnType = "boolean";
 68		loc.iEnd = ListLen(arguments.columnNames);
 69		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
 70			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
 71			column(argumentCollection=arguments);
 72		}
 73		</cfscript>
 74	</cffunction>
 75
 76	<cffunction name="date" returntype="void" access="public" hint="adds date columns to table definition">
 77		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
 78		<cfargument name="default" type="string" required="no" hint="default value">
 79		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
 80		<cfscript>
 81		var loc = {};
 82		arguments.columnType = "date";
 83		loc.iEnd = ListLen(arguments.columnNames);
 84		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
 85			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
 86			column(argumentCollection=arguments);
 87		}
 88		</cfscript>
 89	</cffunction>
 90
 91	<cffunction name="datetime" returntype="void" access="public" hint="adds datetime columns to table definition">
 92		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
 93		<cfargument name="default" type="string" required="no" hint="default value">
 94		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
 95		<cfscript>
 96		var loc = {};
 97		arguments.columnType = "datetime";
 98		loc.iEnd = ListLen(arguments.columnNames);
 99		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
100			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
101			column(argumentCollection=arguments);
102		}
103		</cfscript>
104	</cffunction>
105	
106	<cffunction name="decimal" returntype="void" access="public" hint="adds decimal columns to table definition">
107		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
108		<cfargument name="default" type="string" required="no" hint="default value">
109		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
110		<cfargument name="precision" type="numeric" required="no" hint="number of digits the column can hold">
111		<cfargument name="scale" type="numeric" required="no" hint="number of digits that can be placed to the right of the decimal point (must be less than or equal to precision)">
112		<cfscript>
113		var loc = {};
114		arguments.columnType = "decimal";
115		loc.iEnd = ListLen(arguments.columnNames);
116		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
117			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
118			column(argumentCollection=arguments);
119		}
120		</cfscript>
121	</cffunction>
122	
123	<cffunction name="float" returntype="void" access="public" hint="adds float columns to table definition">
124		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
125		<cfargument name="default" type="string" required="no" default="" hint="default value">
126		<cfargument name="null" type="boolean" required="no" default="true" hint="whether nulls are allowed">
127		<cfscript>
128		var loc = {};
129		arguments.columnType = "float";
130		loc.iEnd = ListLen(arguments.columnNames);
131		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
132			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
133			column(argumentCollection=arguments);
134		}
135		</cfscript>
136	</cffunction>
137
138	<cffunction name="integer" returntype="void" access="public" hint="adds integer columns to table definition">
139		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
140		<cfargument name="limit" type="numeric" required="no" hint="integer size">
141		<cfargument name="default" type="string" required="no" hint="default value">
142		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
143		<cfscript>
144		var loc = {};
145		arguments.columnType = "integer";
146		loc.iEnd = ListLen(arguments.columnNames);
147		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
148			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
149			column(argumentCollection=arguments);
150		}
151		</cfscript>
152	</cffunction>
153	
154	<cffunction name="string" returntype="void" access="public" hint="adds string columns to table definition">
155		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
156		<cfargument name="limit" type="numeric" required="no" hint="character limit">
157		<cfargument name="default" type="string" required="no" hint="default value">
158		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
159		<cfscript>
160		var loc = {};
161		arguments.columnType = "string";
162		loc.iEnd = ListLen(arguments.columnNames);
163		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
164			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
165			column(argumentCollection=arguments);
166		}
167		</cfscript>
168	</cffunction>
169	
170	<cffunction name="text" returntype="void" access="public" hint="adds text columns to table definition">
171		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
172		<cfargument name="default" type="string" required="no" hint="default value">
173		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
174		<cfscript>
175		var loc = {};
176		arguments.columnType = "text";
177		loc.iEnd = ListLen(arguments.columnNames);
178		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
179			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
180			column(argumentCollection=arguments);
181		}
182		</cfscript>
183	</cffunction>
184	
185	<cffunction name="time" returntype="void" access="public" hint="adds time columns to table definition">
186		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
187		<cfargument name="default" type="string" required="no" hint="default value">
188		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
189		<cfscript>
190		var loc = {};
191		arguments.columnType = "time";
192		loc.iEnd = ListLen(arguments.columnNames);
193		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
194			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
195			column(argumentCollection=arguments);
196		}
197		</cfscript>
198	</cffunction>	
199	
200	<cffunction name="timestamp" returntype="void" access="public" hint="adds timestamp columns to table definition">
201		<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
202		<cfargument name="default" type="string" required="no" hint="default value">
203		<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
204		<cfscript>
205		var loc = {};
206		arguments.columnType = "timestamp";
207		loc.iEnd = ListLen(arguments.columnNames);
208		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
209			arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
210			column(argumentCollection=arguments);
211		}
212		</cfscript>
213	</cffunction>
214	
215	<cffunction name="timestamps" returntype="void" access="public" hint="adds CFWheels convention automatic timestamp and soft delete columns to table definition">
216		<cfscript>
217		timestamp(columnNames="createdat,updatedat,deletedat",null=true);
218		</cfscript>
219	</cffunction>
220    
221	<cffunction name="references" returntype="void" access="public" hint="adds integer reference columns to table definition and creates foreign key constraints">
222		<cfargument name="referenceNames" type="string" required="yes" hint="one or more reference names (singular of referenced tables), comma delimited. eg. referenceNames=page will create a column pageId that references table:pages, column:id">
223		<cfargument name="default" type="string" required="no" default="NULL" hint="default value">
224		<cfargument name="null" type="boolean" required="no" default="true" hint="whether nulls are allowed">
225		<cfargument name="polymorphic" type="boolean" required="no" default="false" hint="whether or not to create an Id/Type pair of columns for a polymorphic relationship">
226		<cfargument name="foreignKey" type="boolean" required="no" default="true" hint="whether or not to create a foreign key constraint">
227		<cfscript>
228		var loc = {};
229		loc.iEnd = ListLen(arguments.referenceNames);
230		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
231			loc.referenceName = ListGetAt(arguments.referenceNames,loc.i);
232			column(columnName = loc.referenceName & "id",columnType="integer",default=arguments.default,null=arguments.null);
233			if(arguments.polymorphic) {
234				column(columnName=loc.referenceName & "type",columnType="string");
235			}
236			if(arguments.foreignKey && !arguments.polymorphic) {
237				loc.referenceTable = pluralize(loc.referenceName);
238				loc.foreignKey = CreateObject("component","ForeignKeyDefinition").init(adapter=this.adapter,table=this.name,referenceTable=loc.referenceTable,column="#loc.referenceName#id",referenceColumn="id");
239				ArrayAppend(this.foreignKeys,loc.foreignKey);
240			}
241		}
242		</cfscript>
243	</cffunction>
244
245	<cffunction name="create" returntype="void" access="public" hint="creates the table in the database">
246		<cfscript>
247		if(this.force) {
248			$execute(this.adapter.dropTable(this.name));
249			announce("Dropped table #LCase(this.name)#");
250		}
251		$execute(this.adapter.createTable(name=this.name,columns=this.columns,foreignKeys=this.foreignKeys));
252		announce("Created table #LCase(this.name)#");
253		loc.iEnd = ArrayLen(this.foreignKeys);
254		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
255			announce("--> added foreign key #this.foreignKeys[loc.i].name#");
256		}
257		</cfscript>
258	</cffunction>
259
260	<cffunction name="change" returntype="void" access="public" hint="alters existing table in the database">
261		<cfargument name="addColumns" type="boolean" required="false" default="false" hint="if true, attempt to add new columns, else check whether column exists to determine whether to add or update">
262		<cfscript>
263		var loc = {};
264		loc.existingColumns = $getColumns(this.name);
265		loc.iEnd = ArrayLen(this.columns);
266		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
267			if(arguments.addColumns || !ListFindNoCase(loc.existingColumns,this.columns[loc.i].name)) {
268				$execute(this.adapter.addColumnToTable(name=this.name,column=this.columns[loc.i]));
269				announce("Added column #this.columns[loc.i].name# to table #this.name#");
270			} else {
271				$execute(this.adapter.changeColumnInTable(name=this.name,column=this.columns[loc.i]));
272				announce("Changed column #this.columns[loc.i].name# in table #this.name#");
273			}
274		}
275		loc.iEnd = ArrayLen(this.foreignKeys);
276		for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
277			$execute(this.adapter.addForeignKeyToTable(name=this.name,foreignKey=this.foreignKeys[loc.i]));
278			announce("Added foreign key #this.foreignKeys[loc.i].name# to table #this.name#");			
279		}
280		</cfscript>
281	</cffunction>
282
283</cfcomponent>