Пример #1
0
        public OleDbObjectAccessCommand(OleDbDataStore dataStore, Type dynamicType, string cmdText, bool polymorphic, CommandType commandType)
        {
            this.dataStore = dataStore;
            this.cmdText = cmdText;
            this.polymorphic = polymorphic;
            this.dynamicType = dynamicType;
            this.commandType = commandType;

            if(commandType == CommandType.Text){
                command = new OleDbDataAccessCommand(dataStore, dataStore.DataStorageManager.CreateQuery(dynamicType, cmdText, polymorphic), commandType);
            } else {
                command = new OleDbDataAccessCommand(dataStore, cmdText, commandType);
            }
        }
Пример #2
0
 public object Clone()
 {
     OleDbObjectAccessCommand cmd = (OleDbObjectAccessCommand)this.MemberwiseClone();
     this.command = (OleDbDataAccessCommand)command.Clone();
     return cmd;
 }
Пример #3
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();
            }
        }
Пример #4
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);
            }

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

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

                OleDbMemberDbType dbType = (OleDbMemberDbType)mapping.MemberDbType;

                if(!typeMapping.UseStoredProcedures && mapping.PrimaryKey){
                    primaryKeyMapping = mapping;
                    continue;
                }

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

            if(!typeMapping.UseStoredProcedures && primaryKeyMapping != null){
                OleDbMemberDbType dbType = (OleDbMemberDbType)primaryKeyMapping.MemberDbType;
                if(dbType.Length == 0){
                    updateCommand.CreateInputParameter("@" + primaryKeyMapping.ColumnName, dbType.OleDbType, primaryKeyMapping.GetValue(o));
                } else {
                    updateCommand.CreateInputParameter("@" + primaryKeyMapping.ColumnName, dbType.OleDbType, dbType.Length, primaryKeyMapping.GetValue(o));
                }
            }

            updateCommand.ExecuteNonQuery();
        }
Пример #5
0
        public void Delete(TypeMapping typeMapping, object primaryKey, bool cascade)
        {
            foreach(Type subClass in typeMapping.SubClasses){
                Delete(this.DataStorageManager.GetTypeMapping(subClass), primaryKey, false);
            }

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

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

            if(dbType.Length == 0){
                deleteCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.OleDbType, primaryKey);
            } else {
                deleteCommand.CreateInputParameter("@" + mapping.ColumnName, dbType.OleDbType, 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);
                }

                //TODO: delete subclass table entry
            }
        }