Commits

malweka committed 84def7c

fixes and small changes

Comments (0)

Files changed (8)

src/Goliath.Data/Mapping/MapBuilderConfig.Read.cs

                                 break;
                             case "type":
                                 property.ComplexTypeName = reader.Value;
+                                property.IsComplexType = true;
                                 break;
                             case "lazy":
                                 property.LazyLoad = ReadBool(reader.Value);

src/Goliath.Data/Mapping/Models/Property.cs

         /// </returns>
         public virtual string ToString(Providers.SqlMapper sqlMapper)
         {
-            StringBuilder sqlSb = new StringBuilder(ColumnName);
-            var sType = sqlMapper.TranslateToSqlStringType(this) ?? SqlType;
-            sqlSb.AppendFormat(" {0}", sType);
-
-            //if (this.IsPrimaryKey)
-            //{
-            //    sqlSb.AppendFormat(" {0}", sqlMapper.PrimarykeySql().ToUpper());
-            //}
-            if (!string.IsNullOrWhiteSpace(DefaultValue))
-            {
-                string dVal = DefaultValue;
-                var sfunc = sqlMapper.GetFunction(DefaultValue);
-                if (sfunc != null)
-                    dVal = sfunc.ToString();
-                sqlSb.AppendFormat(" DEFAULT({0})", dVal);
-            }
-            if (!IsNullable)
-            {
-                sqlSb.Append(" NOT NULL");
-            }
-            
-            return sqlSb.ToString();
+            var txt = sqlMapper.TranslateToSqlStringType(this);
+            return txt;
         }
 
         /// <summary>

src/Goliath.Data/Mapping/PostGenProcessors/NamePostProcessor.cs

             {
                 table.Name = tableNamer.Transform(table, table.Name);
                 table.TableAlias = tableAbbreviator.Abbreviate(table.Name);
+                var propertyListClone = table.ToArray();
 
-                for(int i=0; i<table.Count; i++)//foreach (var prop in table)
+                foreach (var prop in propertyListClone)
                 {
-                    var prop = table[i];
+                    //var prop = table[i];
 
                     if (prop is Relation)
                     {

src/Goliath.Data/Providers/SqlMapper.cs

 
         Dictionary<string, DbTypeInfo> typeMap = new Dictionary<string, DbTypeInfo>();
         //Dictionary<SqlStatement, string> statements = new Dictionary<SqlStatement, string>(Utils.EnumComparer<SqlStatement>.Instance);
-        Dictionary<string, ISqlFunction> functionMap = new Dictionary<string, ISqlFunction>();
-        Dictionary<string, string> translationTypeMap;// = new Dictionary<string, string>();
+        protected Dictionary<string, ISqlFunction> functionMap = new Dictionary<string, ISqlFunction>();
+        protected Dictionary<string, string> translationTypeMap;// = new Dictionary<string, string>();
         List<string> reservedWords = new List<string>();
 
         /// <summary>
                 canTranslate = true;
 
                 RegisterTranslateType("integer", "integer");
+                RegisterTranslateType("int", "integer");
                 RegisterTranslateType("char", "char");
                 RegisterTranslateType("nvarchar", "nvarchar");
                 RegisterTranslateType("nchar", "nchar");
             if (!string.IsNullOrWhiteSpace(fType))
             {
                 translationTypeMap.TryGetValue(fType, out to);
+
+                if (to == null)
+                    to = fType;
+
                 if ((fromType.Length > 0) && !fType.Equals("text") && !fType.Equals("ntext") && !fType.Equals("image"))
                 {
                     if (!string.IsNullOrWhiteSpace(to) && to.ToUpper().Equals("BLOB"))
                     translationTypeMap.Remove(fromType);
                 }
 
-                translationTypeMap.Add(fromType, toType.ToUpper());
+                translationTypeMap.Add(fromType, toType);
             }
         }
 

src/Goliath.Data/Providers/SqlServer/MssqlSchemaDescriptor.cs

                     while (reader.Read())
                     {
                         string name = reader.GetValueAsString("TABLE_NAME");
+                        if (!string.IsNullOrWhiteSpace(name) && name.Equals("sysdiagrams", StringComparison.OrdinalIgnoreCase))
+                            continue;
+
                         string schemaName = reader.GetValueAsString("TABLE_SCHEMA");
                         logger.Log(LogType.Info, string.Format("reading table {0}", name));
                         EntityMap table = new EntityMap(name, name);
             return tables;
         }
 
-        Dictionary<string, Property> ProcessColumns(EntityMap table)
+        protected virtual Dictionary<string, Property> ProcessColumns(EntityMap table)
         {
             Dictionary<string, Property> columnList = new Dictionary<string, Property>();
             using (DbDataReader reader = db.ExecuteReader(Connection, SELECT_COLUMNS, dbConnector.CreateParameter("tableName", table.TableName)))
                     if (isIdentity)
                         col.IsAutoGenerated = true;
 
-                    col.IsNullable = isNullable;                   
+                    col.IsNullable = isNullable;
                     col.DefaultValue = ProcessDefaultValue(reader.GetValueAsString("COLUMN_DEFAULT"));
                     col.ClrType = mapper.GetClrType(col.DbType, isNullable);
-
+                    OnTableAddProperty(table, col);
                     columnList.Add(colName, col);
 
                 }
             return columnList;
         }
 
-        void ProcessConstraints(EntityMap table, Dictionary<string, Property> columnList)
+        protected virtual void OnTableAddProperty(EntityMap table, Property property)
+        {
+            //should be overriden by sub class
+        }
+
+        protected virtual void ProcessConstraints(EntityMap table, Dictionary<string, Property> columnList)
         {
             List<string> constraints = new List<string>();
             using (var reader = db.ExecuteReader(Connection, SELECT_CONSTRAINTS, dbConnector.CreateParameter("tableName", table.TableName)))
             }
         }
 
-        void ProcessReferences(EntityMap table, Dictionary<string, Property> columns)
+        protected virtual void ProcessReferences(EntityMap table, Dictionary<string, Property> columns)
         {
             using (var reader = db.ExecuteReader(Connection, SELECT_REFERENCES, dbConnector.CreateParameter("tableName", table.TableName)))
             {
             }
         }
 
-        string ProcessDefaultValue(string defaultValue)
+        protected virtual string ProcessDefaultValue(string defaultValue)
         {
             if (!string.IsNullOrWhiteSpace(defaultValue))
             {

src/Goliath.Data/Providers/SqlServer/MssqlSqlMapper.cs

 
 namespace Goliath.Data.Providers.SqlServer
 {
+    using Mapping;
+
     [Serializable]
     public class Mssq2008SqlMapper : SqlMapper
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="Mssq2008SqlMapper"/> class.
         /// </summary>
-        public Mssq2008SqlMapper() : base(Constants.ProviderName)
-        {            
-           
+        public Mssq2008SqlMapper()
+            : base(Constants.ProviderName)
+        {
+
         }
 
         protected override void OnRegisterTypes()
         {
-           base.OnRegisterTypes();
+            base.OnRegisterTypes();
 
-           RegisterType(DbType.Byte, "tinyint");
-           RegisterType(DbType.DateTimeOffset, "datetimeoffset");
-           RegisterType(DbType.String, "ntext");
-           RegisterType(DbType.Int16, "smallint");
-           RegisterType(DbType.Binary, 8000, "image");
-           RegisterType(DbType.Xml, 8000, "xml");
-           RegisterType(DbType.DateTime, "smalldatetime");
-           RegisterType(DbType.Int64, "bigint");
-           RegisterType(DbType.Double, "double precision");
-           RegisterType(DbType.Single, "float"); 
-           RegisterType(DbType.DateTime2, "datetime2");
-           RegisterType(DbType.Currency, "currency");
-           RegisterType(DbType.Xml, "xml");
-            
+            RegisterType(DbType.Byte, "tinyint");
+            RegisterType(DbType.DateTimeOffset, "datetimeoffset");
+            RegisterType(DbType.String, "ntext");
+            RegisterType(DbType.Int16, "smallint");
+            RegisterType(DbType.Binary, 8000, "image");
+            RegisterType(DbType.Xml, 8000, "xml");
+            RegisterType(DbType.DateTime, "smalldatetime");
+            RegisterType(DbType.Int64, "bigint");
+            RegisterType(DbType.Double, "double precision");
+            RegisterType(DbType.Single, "float");
+            RegisterType(DbType.DateTime2, "datetime2");
+            RegisterType(DbType.Currency, "currency");
+            RegisterType(DbType.Xml, "xml");
+            RegisterType(DbType.Currency, "money");
         }
 
         protected override void OnRegisterFunctions()
 
             return string.Format("identity({0},{1})", increment, seed);
         }
+
+        //protected override string OnTranslateToSqlTypeString(Property fromType)
+        //{
+
+        //    StringBuilder sqlSb = new StringBuilder();
+        //    sqlSb.AppendFormat("[{0}]", fromType.ColumnName);
+        //    string to = null;
+        //    string fType = fromType.SqlType.ToLower();
+        //    if (!string.IsNullOrWhiteSpace(fType))
+        //    {
+        //        translationTypeMap.TryGetValue(fType, out to);
+        //        if ((fromType.Length > 0) && !fType.Equals("text") && !fType.Equals("ntext") && !fType.Equals("image"))
+        //        {
+        //            if (!string.IsNullOrWhiteSpace(to) && !to.ToUpper().Equals("NTEXT"))
+        //            {
+        //                to = string.Format("{0}({1})", to, fromType.Length);
+        //            }
+        //        }
+        //    }
+
+        //    var sType = to ?? fromType.SqlType;
+        //    sqlSb.AppendFormat(" {0}", sType);
+
+        //    if (fromType.IsPrimaryKey)
+        //    {
+        //        sqlSb.AppendFormat(" {0}", PrimarykeySql().ToUpper());
+        //    }
+        //    if (!string.IsNullOrWhiteSpace(fromType.DefaultValue))
+        //    {
+        //        string dVal = fromType.DefaultValue;
+        //        var sfunc = GetFunction(fromType.DefaultValue);
+        //        if (sfunc != null)
+        //            dVal = sfunc.ToString();
+        //        sqlSb.AppendFormat(" DEFAULT({0})", dVal);
+        //    }
+        //    if (!fromType.IsNullable)
+        //    {
+        //        sqlSb.Append(" NOT NULL");
+        //    }
+
+        //    return sqlSb.ToString();
+
+
+        //}
     }
 }

src/Goliath.Data/Transformers/OneToManyRelationNameTransformer.cs

                     return original.Pascalize();
                 }
 
-                if (original.EndsWith(prefix, StringComparison.InvariantCultureIgnoreCase))
+                if (original.EndsWith(prefix, StringComparison.InvariantCultureIgnoreCase) && !mapModel.IsPrimaryKey)
                 {
                     string name = original.Substring(0, original.IndexOf("Id", StringComparison.OrdinalIgnoreCase));
                     //mapModel.PropertyName = name;
                     //mapModel.KeyFieldName = mapModel.PropertyName + "_Key";
                     return original.Pascalize();
                 }
+
+                //return original.Pascalize();
             }
             catch (Exception ex)
             {

src/Goliath.Data/Utilities/DbExtensionMethods.cs

             }
         }
 
+        public static long? GetValueAsLong(this DbDataReader reader, string columnName)
+        {
+            if (string.IsNullOrEmpty(columnName))
+                throw new ArgumentNullException("columnName");
+
+            if (reader == null)
+                throw new ArgumentNullException("reader");
+
+            var data = reader[columnName];
+            if (data == DBNull.Value)
+                return null;
+            else
+            {
+                if (data is long)
+                    return (long)data;
+                else
+                {
+                    var val = Convert.ToInt64(data.ToString());
+                    return val;
+                }
+            }
+        }
+
         public static object GetValueAsObject(this DbDataReader reader, string columnName)
         {
             if (string.IsNullOrEmpty(columnName))