Exemplo n.º 1
0
        public override TypeMappingState CheckMapping(TypeMapping typeMapping)
        {
            if(!TableExists(typeMapping.TableName)){
                return new TypeMappingState(TypeMappingState.TypeMappingStateCondition.TableMissing);
            }

            IDataAccessCommand command = ManagedDataStore.CreateDataAccessCommand("SELECT * FROM " + typeMapping.TableName, CommandType.Text);

            //Get Schema
            IDataReader r = null;
            DataTable schema;
            try {
                r = command.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.CloseConnection);
                schema = r.GetSchemaTable();
            } finally {
                if(r != null){
                    r.Close();
                }
            }

            //Check Schema
            ArrayList errors = new ArrayList();
            foreach(IMemberMapping mapping in typeMapping.MemberMappings){
                DataRow[] rows = schema.Select("ColumnName = '" + mapping.ColumnName + "'");
                if(rows.Length != 1){
                    errors.Add(new MemberMappingError(MemberMappingError.MemberMappingErrorCode.ColumnMissing,mapping));
                }
            }

            if(errors.Count > 0){
                return new TypeMappingState(errors);
            } else {
                return new TypeMappingState(TypeMappingState.TypeMappingStateCondition.None);
            }
        }
Exemplo n.º 2
0
        protected Type GetType(IDataReader reader, TypeMapping typeMapping)
        {
            foreach(Type subClass in typeMapping.SubClasses){
                TypeMapping subClassTypeMapping = this.dataStore.DataStorageManager.GetTypeMapping(subClass);
                if(!DBNull.Value.Equals( reader[this.dataStore.DataStorageManager.PolymorphicPrimaryKey(subClassTypeMapping)])){
                    return GetType(reader, subClassTypeMapping);
                }
            }

            return typeMapping.MappedType;
        }
Exemplo n.º 3
0
        private void AddDefaultCacheMappings(MemberInfo memberInfo, TypeMapping typeMapping)
        {
            RelationshipMappingAttribute rma = (RelationshipMappingAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(RelationshipMappingAttribute),false);
            if(rma != null){

                RelationshipMapping rm = new RelationshipMapping();
                rm.Name = rma.Name;
                if(rma.ParentType != null){
                    rm.ParentType = rma.ParentType;
                    rm.ChildType = typeMapping.MappedType;
                } else {
                    rm.ParentType = typeMapping.MappedType;
                    rm.ChildType = rma.ChildType;
                }
                rm.ParentMember = rma.ParentMember;
                rm.ChildMember = rma.ChildMember;
                rm.Type = rma.Type;
                relationshipMappings.Add(rm);
            }

            CacheMappingAttribute ca = (CacheMappingAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(CacheMappingAttribute), false);
            if(ca != null){
                CacheMapping cm = new CacheMapping();
                cm.Relationship = ca.Relationship == null ? rma.Name : ca.Relationship;
                cm.MemberInfo = memberInfo;
                cm.DataStore = this.dataStore;
                cm.OrderBy = ca.OrderBy;
                typeMapping.CacheMappings.Add(cm);
            } else if(rma != null){
                CacheMapping cm = new CacheMapping();
                cm.Relationship = rma.Name;
                cm.MemberInfo = memberInfo;
                cm.DataStore = this.dataStore;
                typeMapping.CacheMappings.Add(cm);
            }
        }
Exemplo n.º 4
0
        public override string GenerateInsertSql(TypeMapping typeMapping)
        {
            StringBuilder sqlCreateInsert = new StringBuilder();
            sqlCreateInsert.AppendFormat("INSERT INTO {0} (\r\n",typeMapping.TableName);

            //Add the column names
            StringConcatenator concatenator = new StringConcatenator(sqlCreateInsert,",\r\n");
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){
                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    continue;
                }
                concatenator.AppendFormat("\t[{0}]", propertyMapping.ColumnName);
            }

            sqlCreateInsert.Append("\r\n) VALUES (\r\n");

            //Add the values
            concatenator.Reset();
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){
                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    continue;
                }
                concatenator.AppendFormat("\t@{0}", propertyMapping.ColumnName);
            }

            sqlCreateInsert.Append("\r\n");
            sqlCreateInsert.Append(")\r\n");

            if(typeMapping.PrimaryKey != null && typeMapping.PrimaryKey.Identity){
                sqlCreateInsert.AppendFormat("SELECT @{0} = @@IDENTITY\r\n",typeMapping.PrimaryKey.ColumnName);
            }

            return sqlCreateInsert.ToString();
        }
Exemplo n.º 5
0
 public override string GenerateDeleteTableSql(TypeMapping typeMapping)
 {
     return "DROP TABLE [dbo].[" + typeMapping.TableName + "]";
 }
Exemplo n.º 6
0
        public override string GenerateCreateTableSql(TypeMapping typeMapping)
        {
            StringBuilder sqlCreateTable = new StringBuilder();

            //Create statement
            sqlCreateTable.Append("CREATE TABLE [dbo].[" + typeMapping.TableName + "] (\r\n");

            //Add the columns
            bool addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                //add comas after column definitions
                if(addComa){
                    sqlCreateTable.Append(",\r\n");
                }

                sqlCreateTable.Append("\t[" + propertyMapping.ColumnName + "] "+ propertyMapping.MemberDbType.ToSql());

                if (propertyMapping.PrimaryKey){
                    if(propertyMapping.Identity){
                        sqlCreateTable.Append(" IDENTITY (1, 1) PRIMARY KEY NOT NULL");
                    } else {
                        sqlCreateTable.Append(" PRIMARY KEY");
                    }
                } else {
                    if(propertyMapping.MemberDbType.IsNullable) {
                        sqlCreateTable.Append(" NULL");
                    } else {
                        sqlCreateTable.Append(" NOT NULL");
                    }
                }

                addComa = true;
            }
            //return from last column defintion
            sqlCreateTable.Append("\r\n");

            //close create statement
            sqlCreateTable.Append(")\r\n");

            return sqlCreateTable.ToString();
        }
Exemplo n.º 7
0
        public override string GenerateCreateDeleteProcedureSql(TypeMapping typeMapping)
        {
            if(typeMapping.PrimaryKey == null){
                throw new DataStoreException("Can not create delete procedure without defined primary key");
            }

            StringBuilder sqlCreateSQL = new StringBuilder();

            sqlCreateSQL.AppendFormat("CREATE PROCEDURE dbo.{0}\r\n", GetDeleteProcedureName(typeMapping));
            sqlCreateSQL.Append("(\r\n");

            IMemberMapping primarykeyMapping = typeMapping.PrimaryKey;
            sqlCreateSQL.AppendFormat("\t@{0} {1}\r\n", primarykeyMapping.ColumnName, primarykeyMapping.MemberDbType.ToSql());

            sqlCreateSQL.Append(")\r\n");
            sqlCreateSQL.Append("AS\r\n");

            sqlCreateSQL.AppendFormat("DELETE FROM {0}\r\n",typeMapping.TableName);
            sqlCreateSQL.AppendFormat("WHERE {0} = @{0}\r\n", typeMapping.PrimaryKey.ColumnName);

            return sqlCreateSQL.ToString();
        }
Exemplo n.º 8
0
        private void SetContext(object o, TypeMapping typeMapping)
        {
            foreach( CacheMapping mapping in typeMapping.CacheMappings){
                mapping.SetContext(o);
            }

            if(typeMapping.BaseType != null){
                TypeMapping baseTypeMapping = DataStorageManager.GetTypeMapping(typeMapping.BaseType, true);
                SetContext(o,baseTypeMapping);
            }
        }
Exemplo n.º 9
0
        internal override void Insert(object o, TypeMapping typeMapping)
        {
            if(typeMapping.BaseType != null){
                TypeMapping baseTypeMapping = null;
                if((baseTypeMapping = DataStorageManager.GetTypeMapping(typeMapping.BaseType)) == null){
                    throw new DataStoreException("DataStore does not contain storage for " + typeMapping.BaseType.FullName);
                }
                Insert(o, baseTypeMapping);
            }

            OleDbDataAccessCommand insertCommand;
            if(typeMapping.UseStoredProcedures){
                insertCommand = new OleDbDataAccessCommand(this, ((OleDbDataStorageManager)this.DataStorageManager).GetInsertProcedureName(typeMapping), CommandType.StoredProcedure);
            } else {
                insertCommand =  new OleDbDataAccessCommand(this, ((OleDbDataStorageManager)this.DataStorageManager).GenerateInsertSql(typeMapping), CommandType.Text);
            }

            IMemberMapping outputmapping = null;
            foreach(IMemberMapping mapping in typeMapping.MemberMappings){

                OleDbMemberDbType dbType = (OleDbMemberDbType)mapping.MemberDbType;

                if(mapping.PrimaryKey && mapping.Identity){
                    //No batch statemenst allowed in access
                    //output = insertCommand.CreateOutputParameter("@" + mapping.ColumnName, dbType.DbType);
                    outputmapping = mapping;
                } else if(dbType.Length == 0){
                    insertCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.OleDbType, mapping.GetValue(o));
                } else {
                    insertCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.OleDbType, dbType.Length, mapping.GetValue(o));
                }
            }

            if(outputmapping != null){
                insertCommand.ExecuteNonQuery(CommandBehavior.Default);
                insertCommand.CommandType = CommandType.Text;
                insertCommand.CommandText = "SELECT @@IDENTITY;";
                Object output = insertCommand.ExecuteScalar();
                if(output != null){
                    outputmapping.SetValue(o,output);
                }
            } else {
                insertCommand.ExecuteNonQuery();
            }
        }
Exemplo n.º 10
0
        public override string GenerateInsertSql(TypeMapping typeMapping)
        {
            StringBuilder sqlCreateInsert = new StringBuilder();

            //Add the columns
            bool addComa = false;

            sqlCreateInsert.AppendFormat("INSERT INTO {0} (\r\n",typeMapping.TableName);

            //Add the column names
            addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    continue;
                }

                //add comas after column definitions
                if(addComa){
                    sqlCreateInsert.Append(",\r\n");
                }

                sqlCreateInsert.AppendFormat("\t[{0}]", propertyMapping.ColumnName);

                addComa = true;
            }

            sqlCreateInsert.Append("\r\n) VALUES (\r\n");

            //Add the values
            addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    continue;
                }

                //add comas after column definitions
                if(addComa){
                    sqlCreateInsert.Append(",\r\n");
                }

                sqlCreateInsert.AppendFormat("\t@{0}", propertyMapping.ColumnName);

                addComa = true;
            }
            //return from last column defintion
            sqlCreateInsert.Append("\r\n");
            sqlCreateInsert.Append(")\r\n");

            return sqlCreateInsert.ToString();
        }
Exemplo n.º 11
0
 private string JoinSubClasses(string from, TypeMapping typeMapping)
 {
     foreach(Type subClass in typeMapping.SubClasses){
         TypeMapping subClassMapping = GetTypeMapping(subClass);
         from = string.Format("({3} LEFT JOIN {0}\r\n ON {0}.{1} = {2}.{1}) ",subClassMapping.TableName,typeMapping.PrimaryKey.ColumnName,typeMapping.TableName, from);
     }
     return from;
 }
Exemplo n.º 12
0
        public override string GenerateCreateUpdateProcedureSql(TypeMapping typeMapping)
        {
            StringBuilder sqlCreateSQL = new StringBuilder();

            sqlCreateSQL.AppendFormat("CREATE PROCEDURE {0}\r\n", GetUpdateProcedureName(typeMapping));
            sqlCreateSQL.Append("(\r\n");

            //Add the columns
            bool addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                //add comas after column definitions
                if(addComa){
                    sqlCreateSQL.Append(",\r\n");
                }

                sqlCreateSQL.AppendFormat("\t{0} {1}", propertyMapping.ColumnName, propertyMapping.MemberDbType.ToSql());

                addComa = true;
            }
            //return from last column defintion
            sqlCreateSQL.Append("\r\n");

            sqlCreateSQL.Append(")\r\n");
            sqlCreateSQL.Append("AS\r\n");

            sqlCreateSQL.AppendFormat("UPDATE {0}\r\n",typeMapping.TableName);
            sqlCreateSQL.Append("SET \r\n");
            //Add the columns
            addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                if(propertyMapping.PrimaryKey){
                    continue;
                }

                //add comas after column definitions
                if(addComa){
                    sqlCreateSQL.Append(",\r\n");
                }

                sqlCreateSQL.AppendFormat("\t{1}.{0} = {0}", propertyMapping.ColumnName,typeMapping.TableName);

                addComa = true;
            }
            //return from last column defintion
            sqlCreateSQL.Append("\r\n");
            sqlCreateSQL.AppendFormat("WHERE {1}.{0} = {0}\r\n", typeMapping.PrimaryKey.ColumnName,typeMapping.TableName);

            return sqlCreateSQL.ToString();
        }
Exemplo n.º 13
0
        public string BuildBaseClassColumnList(TypeMapping typeMapping)
        {
            if(typeMapping.BaseType != null){
                typeMapping = GetTypeMapping(typeMapping.BaseType);

                StringBuilder columns = new StringBuilder();
                bool addComma = true;
                foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){
                    if(addComma){
                        columns.Append(",");
                    } else {
                        addComma = true;
                    }
                    columns.AppendFormat(" {0}.{1} AS [{0}_{1}]", typeMapping.TableName, propertyMapping.ColumnName);
                }

                columns.Append(BuildBaseClassColumnList(typeMapping));
                return columns.ToString();
            } else {
                return string.Empty;
            }
        }
Exemplo n.º 14
0
 public void AddTypeMapping(TypeMapping typeMapping)
 {
     this.typeMappings.Add(typeMapping);
     if(typeMapping.BaseType != null){
         this.GetTypeMapping(typeMapping.BaseType).SubClasses.Add(typeMapping.MappedType);
     }
 }
Exemplo n.º 15
0
 /// <summary>
 /// Inserts an object into the data source.
 /// </summary>
 /// <param name="o">The object to insert.</param>
 /// <param name="typeMapping">The TypeMapping that defines the mapping to the data source.</param>
 internal abstract void Insert(object o, TypeMapping typeMapping);
Exemplo n.º 16
0
        protected object MappedPopulation(IDataReader reader, object dynamicObject, TypeMapping typeMapping)
        {
            if(dynamicObject == null){
                if(!polymorphic){
                    dynamicObject = mappedType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[]{}, null).Invoke(null); //,
                } else {
                    dynamicObject = GetType(reader, typeMapping).GetConstructor(new Type[]{}).Invoke(null);
                    return MappedPopulation(reader, dynamicObject, this.dataStore.DataStorageManager.GetTypeMapping(dynamicObject.GetType()));
                }
            }

            if(typeMapping.BaseType != null){
                TypeMapping baseTypeMapping = null;
                if((baseTypeMapping = dataStore.DataStorageManager.GetTypeMapping(typeMapping.BaseType)) == null){
                    throw new ApplicationException("DataStore does not contain storage for " + typeMapping.BaseType.FullName);
                }
                MappedPopulation(reader, dynamicObject, baseTypeMapping);
            }

            //follow type mapping
            foreach( IMemberMapping mapping in typeMapping.MemberMappings) {
                try {
                    if(polymorphic){
                        mapping.SetValue(dynamicObject,reader[typeMapping.TableName + "_" + mapping.ColumnName]);
                    } else {
                        mapping.SetValue(dynamicObject,reader[mapping.ColumnName]);
                    }
                } catch (IndexOutOfRangeException iore) {
                    throw new DataStoreException("Error populating field: " + mapping.ColumnName, iore);
                } catch (ArgumentException e) {
                    throw new DataStoreException("Error populating field: " + mapping.ColumnName ,e);
                }
            }

            foreach( CacheMapping mapping in typeMapping.CacheMappings){
                mapping.SetContext(dynamicObject);
            }

            return dynamicObject;
        }
Exemplo n.º 17
0
 /// <summary>
 /// Updates an object in the data source.
 /// </summary>
 /// <param name="o">The object to update.</param>
 /// <param name="typeMapping">The TypeMapping that defines the mapping to the data source.</param>
 internal abstract void Update(object o, TypeMapping typeMapping);
Exemplo n.º 18
0
        protected Type GetType(IDataReader reader, TypeMapping typeMapping)
        {
            foreach(Type subClass in typeMapping.SubClasses){
                TypeMapping subClassTypeMapping = this.dataStore.DataStorageManager.GetTypeMapping(subClass);
                if(!DBNull.Value.Equals( reader[this.dataStore.DataStorageManager.PolymorphicPrimaryKey(subClassTypeMapping)])){
                    return GetType(reader, subClassTypeMapping);
                }
            }

            return typeMapping.MappedType;

            //			string fullName = (string)reader["FullName"];
            //			//TODO: This does not need to be dynamic, but could be a look up
            //			//http://www.gotdotnet.com/team/fxcop/docs/rules/SecurityRules/LateBoundMembers.html
            //			return Type.GetType(fullName);
        }
Exemplo n.º 19
0
 public override bool ExistsStorage(TypeMapping typeMapping)
 {
     return CheckMapping(typeMapping).Condition != TypeMappingState.TypeMappingStateCondition.TableMissing;
 }
Exemplo n.º 20
0
 public void Add(TypeMapping value)
 {
     base.Add(value.MappedType.FullName,value);
 }
Exemplo n.º 21
0
        public override string GenerateCreateInsertProcedureSql(TypeMapping typeMapping)
        {
            StringBuilder sqlCreateInsert = new StringBuilder();

            sqlCreateInsert.AppendFormat("CREATE PROCEDURE dbo.{0}\r\n", GetInsertProcedureName(typeMapping));
            sqlCreateInsert.Append("(\r\n");

            //Add the columns
            bool addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                //add comas after column definitions
                if(addComa){
                    sqlCreateInsert.Append(",\r\n");
                }

                sqlCreateInsert.AppendFormat("\t@{0} {1}", propertyMapping.ColumnName, propertyMapping.MemberDbType.ToSql());

                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    sqlCreateInsert.Append(" OUTPUT");
                }

                addComa = true;
            }
            //return from last column defintion
            sqlCreateInsert.Append("\r\n");

            sqlCreateInsert.Append(")\r\n");
            sqlCreateInsert.Append("AS\r\n");

            sqlCreateInsert.AppendFormat("INSERT INTO {0} (\r\n",typeMapping.TableName);

            //Add the column names
            addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    continue;
                }

                //add comas after column definitions
                if(addComa){
                    sqlCreateInsert.Append(",\r\n");
                }

                sqlCreateInsert.AppendFormat("\t[{0}]", propertyMapping.ColumnName);

                addComa = true;
            }

            sqlCreateInsert.Append("\r\n) VALUES (\r\n");

            //Add the values
            addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                if(propertyMapping.PrimaryKey && propertyMapping.Identity){
                    continue;
                }

                //add comas after column definitions
                if(addComa){
                    sqlCreateInsert.Append(",\r\n");
                }

                sqlCreateInsert.AppendFormat("\t@{0}", propertyMapping.ColumnName);

                addComa = true;
            }
            //return from last column defintion
            sqlCreateInsert.Append("\r\n");
            sqlCreateInsert.Append(")\r\n");

            if(typeMapping.PrimaryKey.Identity){
                sqlCreateInsert.AppendFormat("SELECT @{0} = @@IDENTITY\r\n",typeMapping.PrimaryKey.ColumnName);
            }

            return sqlCreateInsert.ToString();
        }
Exemplo n.º 22
0
        public void Delete(TypeMapping typeMapping, object primaryKey, bool cascade)
        {
            foreach(Type subClass in typeMapping.SubClasses){
                Delete(this.DataStorageManager.GetTypeMapping(subClass), primaryKey, false);
            }

            SqlDataAccessCommand deleteCommand;
            if(typeMapping.UseStoredProcedures){
                deleteCommand = new SqlDataAccessCommand(this, ((SqlDataStorageManager)this.DataStorageManager).GetDeleteProcedureName(typeMapping), CommandType.StoredProcedure);
            } else {
                deleteCommand = new SqlDataAccessCommand(this, ((SqlDataStorageManager)this.DataStorageManager).GenerateDeleteSql(typeMapping), CommandType.Text);
            }

            IMemberMapping mapping = typeMapping.PrimaryKey;
            SqlMemberDbType dbType = (SqlMemberDbType)mapping.MemberDbType;

            if(dbType.Length == 0){
                deleteCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.SqlDbType, primaryKey);
            } else {
                deleteCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.SqlDbType, dbType.Length, primaryKey);
            }

            deleteCommand.ExecuteNonQuery();

            if(cascade){
                if(typeMapping.BaseType != null){
                    TypeMapping baseTypeMapping = null;
                    if((baseTypeMapping = DataStorageManager.GetTypeMapping(typeMapping.BaseType)) == null){
                        throw new DataStoreException("DataStore does not contain storage for " + typeMapping.BaseType.FullName);
                    }
                    Delete(baseTypeMapping, primaryKey, true);
                }
            }
        }
Exemplo n.º 23
0
        public override string GenerateDeleteSql(TypeMapping typeMapping)
        {
            if(typeMapping.PrimaryKey == null){
                throw new DataStoreException("Can not create delete procedure without defined primary key");
            }

            StringBuilder sqlCreateSQL = new StringBuilder();

            sqlCreateSQL.AppendFormat("DELETE FROM {0}\r\n",typeMapping.TableName);
            sqlCreateSQL.AppendFormat("WHERE {0} = @{0}\r\n", typeMapping.PrimaryKey.ColumnName);

            return sqlCreateSQL.ToString();
        }
Exemplo n.º 24
0
        internal override void Insert(object o, TypeMapping typeMapping)
        {
            if(typeMapping.BaseType != null){
                TypeMapping baseTypeMapping = null;
                if((baseTypeMapping = DataStorageManager.GetTypeMapping(typeMapping.BaseType)) == null){
                    throw new DataStoreException("DataStore does not contain storage for " + typeMapping.BaseType.FullName);
                }
                Insert(o,baseTypeMapping);
            }

            SqlDataAccessCommand insertCommand;
            if(typeMapping.UseStoredProcedures){
                insertCommand = new SqlDataAccessCommand(this, ((SqlDataStorageManager)this.DataStorageManager).GetInsertProcedureName(typeMapping), CommandType.StoredProcedure);
            } else {
                insertCommand = new SqlDataAccessCommand(this, ((SqlDataStorageManager)this.DataStorageManager).GenerateInsertSql(typeMapping), CommandType.Text);
            }

            IDbDataParameter output = null;
            IMemberMapping outputmapping = null;

            foreach(IMemberMapping mapping in typeMapping.MemberMappings){

                SqlMemberDbType dbType = (SqlMemberDbType)mapping.MemberDbType;

                if(mapping.PrimaryKey && mapping.Identity){
                    output = insertCommand.CreateOutputParameter("@" + mapping.ColumnName, dbType.SqlDbType);
                    outputmapping = mapping;
                } else if(dbType.Length == 0){
                    insertCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.SqlDbType, mapping.GetValue(o));
                } else {
                    insertCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.SqlDbType, dbType.Length, mapping.GetValue(o));
                }
            }

            insertCommand.ExecuteNonQuery();

            if(output!=null){
                outputmapping.SetValue(o,output.Value);
            }
        }
Exemplo n.º 25
0
        public override string GenerateFindByPrimaryKeySql(TypeMapping typeMapping)
        {
            if(typeMapping.PrimaryKey == null) {
                throw new DataStoreException("TypeMapping does not have defined PrimaryKey for " + typeMapping.MappedType.FullName);
            }

            StringBuilder sqlCreateSQL = new StringBuilder();

            sqlCreateSQL.AppendFormat("WHERE [{0}].[{1}] = @{1}", typeMapping.TableName, typeMapping.PrimaryKey.ColumnName);

            return sqlCreateSQL.ToString();
        }
Exemplo n.º 26
0
        internal override void Update(object o, TypeMapping typeMapping)
        {
            if(typeMapping.BaseType != null){
                TypeMapping baseTypeMapping = null;
                if((baseTypeMapping = DataStorageManager.GetTypeMapping(typeMapping.BaseType)) == null){
                    throw new DataStoreException("DataStore does not contain storage for " + typeMapping.BaseType.FullName);
                }
                Update(o,baseTypeMapping);
            }

            if ((typeMapping.PrimaryKey != null && typeMapping.MemberMappings.Count > 1) || (typeMapping.PrimaryKey == null && typeMapping.MemberMappings.Count > 0)) {

                SqlDataAccessCommand updateCommand;
                if (typeMapping.UseStoredProcedures) {
                    updateCommand = new SqlDataAccessCommand(this, ((SqlDataStorageManager)this.DataStorageManager).GetUpdateProcedureName(typeMapping), CommandType.StoredProcedure);
                } else {
                    updateCommand = new SqlDataAccessCommand(this, ((SqlDataStorageManager)this.DataStorageManager).GenerateUpdateSql(typeMapping), CommandType.Text);
                }

                foreach (IMemberMapping mapping in typeMapping.MemberMappings) {

                    SqlMemberDbType dbType = (SqlMemberDbType)mapping.MemberDbType;

                    if (dbType.Length == 0) {
                        updateCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.SqlDbType, mapping.GetValue(o));
                    } else {
                        updateCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.SqlDbType, dbType.Length, mapping.GetValue(o));
                    }
                }

                updateCommand.ExecuteNonQuery();
            }
        }
Exemplo n.º 27
0
        public override string GenerateUpdateSql(TypeMapping typeMapping)
        {
            StringBuilder sqlCreateSQL = new StringBuilder();
            sqlCreateSQL.AppendFormat("UPDATE {0}\r\n",typeMapping.TableName);
            sqlCreateSQL.Append("SET \r\n");
            //Add the columns
            bool addComa = false;
            foreach(IMemberMapping propertyMapping in typeMapping.MemberMappings){

                if(propertyMapping.PrimaryKey){
                    continue;
                }

                //add comas after column definitions
                if(addComa){
                    sqlCreateSQL.Append(",\r\n");
                }

                sqlCreateSQL.AppendFormat("\t[{0}] = @{0}", propertyMapping.ColumnName);

                addComa = true;
            }
            //return from last column defintion
            sqlCreateSQL.Append("\r\n");
            sqlCreateSQL.AppendFormat("WHERE {0} = @{0}\r\n", typeMapping.PrimaryKey.ColumnName);

            return sqlCreateSQL.ToString();
        }
Exemplo n.º 28
0
        /// <summary>
        /// Creates the necessary tables and,  if defined in the TypeMapping, stored procedures in the datasource for the type using a specific TypeMapping.
        /// </summary>
        public void CreateStorage(TypeMapping typeMapping)
        {
            //TODO: Review - add if not already added
            if(DataStorageManager.GetTypeMapping(typeMapping.MappedType) == null){
                DataStorageManager.AddTypeMapping(typeMapping);
            }

            this.DataStorageManager.CreateStorage(typeMapping);
        }
Exemplo n.º 29
0
 private void AppendSubClassJoins(TypeMapping typeMapping, StringBuilder sqlQuerySQL)
 {
     foreach (Type subClass in typeMapping.SubClasses) {
         TypeMapping subClassMapping = GetTypeMapping(subClass);
         sqlQuerySQL.AppendFormat("LEFT OUTER JOIN {0}\r\n ON {0}.{1} = {2}.{1}\r\n", subClassMapping.TableName, typeMapping.PrimaryKey.ColumnName, typeMapping.TableName);
         AppendSubClassJoins(subClassMapping, sqlQuerySQL);
     }
 }
Exemplo n.º 30
0
 public string PolymorphicPrimaryKey(TypeMapping typeMapping)
 {
     return string.Format("{0}_{1}", typeMapping.TableName, typeMapping.PrimaryKey.ColumnName);
 }