Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more datatypes & better identity_insert #75

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 14 additions & 42 deletions Migration.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -201,84 +201,56 @@
<cfargument name="table" type="string" required="true" hint="table name">
<cfscript>
var loc = {};
loc.columnNames = "";
loc.columnValues = "";
loc.values = {};
for (loc.key in arguments) {
if(loc.key neq "table") {
loc.columnNames = ListAppend(loc.columnNames,this.adapter.quoteColumnName(loc.key));
if(IsNumeric(arguments[loc.key])) {
loc.columnValues = ListAppend(loc.columnValues,arguments[loc.key]);
} else if(IsBoolean(arguments[loc.key])) {
loc.columnValues = ListAppend(loc.columnValues,IIf(arguments[loc.key],1,0));
} else if(IsDate(arguments[loc.key])) {
loc.columnValues = ListAppend(loc.columnValues,"#arguments[loc.key]#");
} else {
loc.columnValues = ListAppend(loc.columnValues,"'#ReplaceNoCase(arguments[loc.key],"'","''","all")#'");
}
loc.values[ loc.key ] = arguments[ loc.key ];
}
}
if(loc.columnNames != '') {
if(ListContainsNoCase(loc.columnnames, "[id]")) {
$execute(this.adapter.addRecordPrefix(arguments.table));
}
$execute("INSERT INTO #this.adapter.quoteTableName(LCase(arguments.table))# ( #loc.columnNames# ) VALUES ( #loc.columnValues# )");
if(ListContainsNoCase(loc.columnnames, "[id]")) {
$execute(this.adapter.addRecordSuffix(arguments.table));
}
if( NOT structIsEmpty( loc.values ) ) {
$execute( this.adapter.addRecord( arguments.table, loc.values ) );
announce("Added record to table #arguments.table#");
}
</cfscript>
</cffunction>


<cffunction name="updateRecord" returntype="void" access="public" hint="updates an existing record in a table">
<cfargument name="table" type="string" required="true" hint="table name">
<cfargument name="where" type="string" required="false" default="" hint="where condition">
<cfscript>
var loc = {};
loc.columnUpdates = "";
loc.values = {};
for (loc.key in arguments) {
if(loc.key neq "table" && loc.key neq "where") {
loc.update = "#this.adapter.quoteColumnName(loc.key)# = ";
if(IsNumeric(arguments[loc.key])) {
loc.update = loc.update & "#arguments[loc.key]#";
} else if(IsBoolean(arguments[loc.key])) {
loc.update = loc.update & "#IIf(arguments[loc.key],1,0)#";
} else if(IsDate(arguments[loc.key])) {
loc.update = loc.update & "#arguments[loc.key]#";
} else {
arguments[loc.key] = ReplaceNoCase(arguments[loc.key], "'", "''", "all");
loc.update = loc.update & "'#arguments[loc.key]#'";
}
loc.columnUpdates = ListAppend(loc.columnUpdates,loc.update);
if(loc.key neq "table" AND loc.key neq "where") {
loc.values[ loc.key ] = arguments[ loc.key ];
}
}
if(loc.columnUpdates != '') {
loc.sql = 'UPDATE #this.adapter.quoteTableName(LCase(arguments.table))# SET #loc.columnUpdates#';
if( NOT structIsEmpty( loc.values ) ) {
$execute( this.adapter.updateRecord( arguments.table, arguments.where, loc.values ) );
loc.message = 'Updated record(s) in table #arguments.table#';
if(arguments.where != '') {
loc.sql = loc.sql & ' WHERE #arguments.where#';
loc.message = loc.message & ' where #arguments.where#';
}
$execute(loc.sql);
announce(loc.message);
}
</cfscript>
</cffunction>


<cffunction name="removeRecord" returntype="void" access="public" hint="removes existing records from a table">
<cfargument name="table" type="string" required="true" hint="table name">
<cfargument name="where" type="string" required="false" default="" hint="where condition">
<cfscript>
var loc = {};
loc.sql = 'DELETE FROM #this.adapter.quoteTableName(LCase(arguments.table))#';
$execute( this.adapter.removeRecord( arguments.table, arguments.where ) );
loc.message = 'Removed record(s) from table #arguments.table#';
if(arguments.where != '') {
loc.sql = loc.sql & ' WHERE #arguments.where#';
loc.message = loc.message & ' where #arguments.where#';
}
$execute(loc.sql);
announce(loc.message);
</cfscript>
</cffunction>

</cfcomponent>

</cfcomponent>
101 changes: 80 additions & 21 deletions TableDefinition.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<cfargument name="name" type="string" required="yes" hint="primary key column name">
<cfargument name="type" type="string" required="false" default="integer" hint="type for the primary key column">
<cfargument name="autoIncrement" type="boolean" required="no" default="false">
<cfargument name="limit" type="numeric" required="no" hint="character or integer size">
<cfargument name="limit" type="string" required="no" hint="character or integer size">
<cfargument name="precision" type="numeric" required="no" hint="number of digits the column can hold">
<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)">
<cfargument name="references" type="string" required="no" hint="table this primary key should reference as a foreign key">
Expand Down Expand Up @@ -68,28 +68,28 @@
<cfargument name="columnType" type="string" required="yes" hint="column type">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="limit" type="numeric" required="no" hint="character or integer size">
<cfargument name="precision" type="numeric" required="no" hint="number of digits the column can hold">
<cfargument name="limit" type="string" required="no" hint="character or integer size">
<cfargument name="precision" type="string" required="no" hint="number of digits the column can hold">
<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)">
<cfargument name="encoding" type="string" required="no" hint="character encoding: unicode, ascii or others">
<cfscript>
var loc = {};
arguments.adapter = this.adapter;
arguments.name = arguments.columnName;
arguments.type = arguments.columnType;
loc.column = CreateObject("component","ColumnDefinition").init(argumentCollection=arguments);
loc.column = createObject("component","ColumnDefinition").init(argumentCollection=arguments);
ArrayAppend(this.columns,loc.column);
</cfscript>
<cfreturn this>
</cffunction>

<cffunction name="bigInteger" returntype="any" access="public" hint="adds integer columns to table definition">
<cffunction name="binary" returntype="any" access="public" hint="adds binary columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="numeric" required="no" hint="integer size">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfscript>
var loc = {};
arguments.columnType = "biginteger";
arguments.columnType = "binary";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
Expand All @@ -99,13 +99,13 @@
<cfreturn this>
</cffunction>

<cffunction name="binary" returntype="any" access="public" hint="adds binary columns to table definition">
<cffunction name="boolean" returntype="any" access="public" hint="adds boolean columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfscript>
var loc = {};
arguments.columnType = "binary";
arguments.columnType = "boolean";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
Expand All @@ -115,13 +115,13 @@
<cfreturn this>
</cffunction>

<cffunction name="boolean" returntype="any" access="public" hint="adds boolean columns to table definition">
<cffunction name="date" returntype="any" access="public" hint="adds date columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfscript>
var loc = {};
arguments.columnType = "boolean";
arguments.columnType = "date";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
Expand All @@ -131,13 +131,14 @@
<cfreturn this>
</cffunction>

<cffunction name="date" returntype="any" access="public" hint="adds date columns to table definition">
<cffunction name="datetime" returntype="any" access="public" hint="adds datetime columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="limit" type="string" required="no" hint="Use 'small' for SQL Server SMALLDATETIME or a numeric value for SQL Server DATETIME2">
<cfscript>
var loc = {};
arguments.columnType = "date";
arguments.columnType = "datetime";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
Expand All @@ -146,14 +147,16 @@
</cfscript>
<cfreturn this>
</cffunction>

<cffunction name="datetime" returntype="any" access="public" hint="adds datetime columns to table definition">
<cffunction name="decimal" returntype="any" access="public" hint="adds decimal columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="precision" type="numeric" required="no" hint="number of digits the column can hold">
<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)">
<cfscript>
var loc = {};
arguments.columnType = "datetime";
arguments.columnType = "decimal";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
Expand All @@ -163,15 +166,15 @@
<cfreturn this>
</cffunction>

<cffunction name="decimal" returntype="any" access="public" hint="adds decimal columns to table definition">
<cffunction name="money" returntype="any" access="public" hint="adds money columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="precision" type="numeric" required="no" hint="number of digits the column can hold">
<cfargument name="precision" type="string" required="no" hint="number of digits or 'small' for sql server">
<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)">
<cfscript>
var loc = {};
arguments.columnType = "decimal";
arguments.columnType = "money";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
Expand Down Expand Up @@ -199,7 +202,7 @@

<cffunction name="integer" returntype="any" access="public" hint="adds integer columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="numeric" required="no" hint="integer size">
<cfargument name="limit" type="string" required="no" hint="integer size or TINY, MEDIUM, SMALL, BIG, UNSIGNED">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfscript>
Expand All @@ -214,11 +217,48 @@
<cfreturn this>
</cffunction>

<cffunction name="tinyInteger" returntype="any" access="public" hint="adds integer columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="string" required="no" hint="Could still be UNSIGNED">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfset arguments.limit = listAppend( arguments.limit, "TINY", " " )>
<cfreturn integer( argumentCollection = arguments )>
</cffunction>

<cffunction name="smallInteger" returntype="any" access="public" hint="adds integer columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="string" required="no" hint="Could still be UNSIGNED">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfset arguments.limit = listAppend( arguments.limit, "SMALL", " " )>
<cfreturn integer( argumentCollection = arguments )>
</cffunction>

<cffunction name="mediumInteger" returntype="any" access="public" hint="adds integer columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="string" required="no" hint="Could still be UNSIGNED">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfset arguments.limit = listAppend( arguments.limit, "MEDIUM", " " )>
<cfreturn integer( argumentCollection = arguments )>
</cffunction>

<cffunction name="bigInteger" returntype="any" access="public" hint="adds integer columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="string" required="no" hint="Could still be UNSIGNED">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfset arguments.limit = listAppend( arguments.limit, "BIG", " " )>
<cfreturn integer( argumentCollection = arguments )>
</cffunction>

<cffunction name="string" returntype="any" access="public" hint="adds string columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="numeric" required="no" hint="character limit">
<cfargument name="limit" type="string" required="no" hint="character limit">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="encoding" type="string" required="no" hint="character encoding: unicode, ascii or others">
<cfscript>
var loc = {};
arguments.columnType = "string";
Expand All @@ -231,10 +271,29 @@
<cfreturn this>
</cffunction>

<cffunction name="char" returntype="any" access="public" hint="adds char columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="limit" type="string" required="no" hint="character limit">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="encoding" type="string" required="no" hint="character encoding: unicode, ascii or others">
<cfscript>
var loc = {};
arguments.columnType = "char";
loc.iEnd = ListLen(arguments.columnNames);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++) {
arguments.columnName = ListGetAt(arguments.columnNames,loc.i);
column(argumentCollection=arguments);
}
</cfscript>
<cfreturn this>
</cffunction>

<cffunction name="text" returntype="any" access="public" hint="adds text columns to table definition">
<cfargument name="columnNames" type="string" required="yes" hint="one or more column names, comma delimited">
<cfargument name="default" type="string" required="no" hint="default value">
<cfargument name="null" type="boolean" required="no" hint="whether nulls are allowed">
<cfargument name="encoding" type="string" required="no" hint="character encoding: unicode, ascii or others">
<cfscript>
var loc = {};
arguments.columnType = "text";
Expand Down
Loading