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

Fix for CleanUp to also work with column names in ActiveRecord #265

Open
Earlz opened this issue Jun 3, 2011 · 0 comments
Open

Fix for CleanUp to also work with column names in ActiveRecord #265

Earlz opened this issue Jun 3, 2011 · 0 comments

Comments

@Earlz
Copy link

Earlz commented Jun 3, 2011

Hello. I've modified SubSonic a while back in 3.0.0.3. When I had to upgrade to 3.0.0.4, merging in my changes by hand, I figured I should give back. Sorry that my patch files aren't quite the best

Anyway, this fix is for cleaning up column names when generating them. I've modified the T4 templates and SubSonic.Core to now use what I named CleanName, which is basically the property name generated by the templates. So IColumn.CleanName is the name of the property generated, and IColumn.Name is now only the name of the column in the database.

Example:

string CleanUp(string tableName){
        string result=tableName;

        //strip blanks
        result=result.Replace(" ","");

        //put your logic here...
+       if(result.EndsWith("_rid", StringComparison.OrdinalIgnoreCase)){
+           result=result.Substring(0,result.Length-4);
+           result+="RID";
+       }

        return result;
     }

I did because the primary keys of my database were in the form TableName_rid. This will change it so that I access it in C# by the name TableNameRID. I've made it work in links and such as well. The only thing is that I've only tested it in SQL Server

Patches follow:

+++ C:/Users/Jordan/AppData/Local/Temp/Database.cs-rev1050.svn000.tmp.cs    Fri Jun  3 09:45:05 2011
@@ -375,7 +375,7 @@
                 foreach(var dirty in ar.GetDirtyColumns())
                 {
                     if(!dirty.IsPrimaryKey && !dirty.IsReadOnly)
-                        query.Set(dirty.Name).EqualTo(settings[dirty.Name]);
+                        query.Set(dirty.Name).EqualTo(settings[dirty.CleanName]);
                 }
             }
             else
@@ -394,7 +394,7 @@
             //add the PK constraint
             Constraint c = new Constraint(ConstraintType.Where, tbl.PrimaryKey.Name)
                                {
-                                   ParameterValue = settings[tbl.PrimaryKey.Name],
+                                   ParameterValue = settings[tbl.PrimaryKey.CleanName],
                                    ParameterName = tbl.PrimaryKey.Name,
                                    ConstructionFragment = tbl.PrimaryKey.Name
                                };
@@ -446,7 +446,7 @@
                 {
                     var c = new Constraint(ConstraintType.Where, pk.Name)
                                 {
-                                    ParameterValue = settings[pk.Name],
+                                    ParameterValue = settings[pk.CleanName],
                                     ParameterName = pk.Name,
                                     ConstructionFragment = pk.Name
                                 };

--- C:/Users/Jordan/AppData/Local/Temp/External-rev1049.svn000.tmp  Fri Jun  3 09:49:32 2011
+++ C:/Users/Jordan/AppData/Local/Temp/DatabaseColumn.c-rev1050.svn001.tmp.cs   Fri Jun  3 09:49:32 2011
@@ -44,6 +44,11 @@
             Name = columnName;
         }

+       public string CleanName
+       {
+           get;
+           set;
+       }

         #region IColumn Members

--- C:/Users/Jordan/AppData/Local/Temp/SubSonic.Co-rev1049.svn002.tmp.Core  Fri Jun  3 09:46:30 2011
+++ C:/Users/Jordan/AppData/Local/Temp/DatabaseTable.cs-rev1050.svn000.tmp.cs   Fri Jun  3 09:46:30 2011
@@ -104,12 +104,24 @@

         public IColumn GetColumn(string ColumnName)
         {
-            return Columns.Where(c => c.Name.Matches(ColumnName)).SingleOrDefault();
+            var col=Columns.Where(c => c.CleanName.Matches(ColumnName)).SingleOrDefault();
+           if (col == null)
+           {
+               col = Columns.Where(c => c.Name.Matches(ColumnName)).SingleOrDefault();
+           }
+           return col;
         }

         public IColumn GetColumnByPropertyName(string PropertyName)
         {
-            return Columns.SingleOrDefault(x => x.Name.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase));
+           var c = Columns.SingleOrDefault(x => x.CleanName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase));
+           if (c == null)
+           {
+
+                   throw new NotSupportedException("Couldn't find column name");
+           }
+           return c;
         }

         public string CreateSql

--- C:/Users/Jordan/AppData/Local/Temp/Externa-rev1049.svn001.tmp   Fri Jun  3 09:49:47 2011
+++ C:/Users/Jordan/AppData/Local/Temp/IColumn.c-rev1050.svn001.tmp.cs  Fri Jun  3 09:49:47 2011
@@ -18,6 +18,7 @@
         object DefaultSetting { get; set; }
         string ParameterName { get; }
         string PropertyName { get; set; }
+       string CleanName { get; set; }

         ITable ForeignKeyTo { get; set; }

--- C:/Users/Jordan/AppData/Local/Temp/-rev823.svn00c.tmp   Fri Jun  3 09:56:54 2011
+++ C:/Users/Jordan/AppData/Local/Temp/Structs.-rev1010.svn002.tmp.tt   Fri Jun  3 09:56:54 2011
@@ -31,6 +32,7 @@

                 Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
                 {
+                   CleanName = "<#=col.CleanName#>",
                    IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
                    DataType = DbType.<#=col.DbType.ToString()#>,
                    IsNullable = <#=col.IsNullable.ToString().ToLower()#>,

--- C:/Users/Jordan/AppData/Local/Temp/SubSonic.Cor-rev1049.svn001.tmp.Core Fri Jun  3 09:46:01 2011
+++ C:/Users/Jordan/AppData/Local/Temp/SubSonicRepository.cs-rev1050.svn000.tmp.cs  Fri Jun  3 09:46:01 2011
@@ -205,7 +205,7 @@
                         try
                         {
                             var tbl = provider.FindOrCreateTable(typeof(T));
-                            var prop = item.GetType().GetProperty(tbl.PrimaryKey.Name);
+                            var prop = item.GetType().GetProperty(tbl.PrimaryKey.CleanName);
                             var settable = result.ChangeTypeTo(prop.PropertyType);
                             prop.SetValue(item, settable, null);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant