예제 #1
0
        /// <summary>
        /// Esegue update sul DB
        /// </summary>
        private ESaveResult performDbUpdate()
        {
            //Appoggio
            IDataBase          db        = this.Slot.DbGet(this.mClassSchema);
            List <DbParameter> oDbParams = new List <DbParameter>(this.mClassSchema.Properties.Count);

            int      iNumChangedProps = 0;
            Property oProp;

            StringBuilder sbSQL = new StringBuilder(@"UPDATE ", 500);

            sbSQL.Append(this.Slot.DbPrefixGetTableName(this.mClassSchema.TableDef));
            sbSQL.Append(@" SET ");

            try
            {
                //Inserisce campi
                for (int iPropIndex = 0; iPropIndex < this.mClassSchema.Properties.Count; iPropIndex++)
                {
                    //Imposta proprieta'
                    oProp = this.mClassSchema.Properties[iPropIndex];

                    //Se da escludere passa a successiva
                    if (oProp.ExcludeUpdate)
                    {
                        continue;
                    }

                    //Imposta controllo concorrenza
                    if (oProp.IsAutomatic)
                    {
                        //Scrive Campo
                        sbSQL.Append(oProp.Column.Name);
                        sbSQL.Append(@"=CURRENT_TIMESTAMP, ");
                        continue;
                    }

                    //PROPRIETA'
                    if (!this.mDataSchema.GetFlagsAll(oProp.PropertyIndex, DataFlags.Changed))
                    {
                        continue;
                    }

                    var oValue = oProp.GetValueForDb(this);

                    //Scrive Campo
                    sbSQL.Append(oProp.Column.Name);
                    sbSQL.Append(@"=");
                    sbSQL.Append(oProp.Column.ParamName);
                    sbSQL.Append(@", ");

                    //Imposta valore
                    oDbParams.Add(db.CreateParameter(oProp.Column.ParamName, oValue, oProp.Column.DbType));

                    //Aggiorna
                    iNumChangedProps++;
                    //TODO: Attenzione impostando qui il flag, in caso di errore rimane l'oggetto sporco!!!!
                    this.mDataSchema.SetFlags(oProp.PropertyIndex, DataFlags.Changed, false);
                }

                //NON C'È NULLA DA MODIFICARE ESCE
                if (iNumChangedProps == 0)
                {
                    return(ESaveResult.UnChanged);
                }

                //Rimuove ,
                sbSQL.Remove(sbSQL.Length - 2, 2);

                //Aggiunge parte PK + reload (se impostata)
                sbSQL.Append(this.mClassSchema.PrimaryKey.SQL_Where_Clause);
                sbSQL.Append(@"; ");
                if (!string.IsNullOrEmpty(this.mClassSchema.TableDef.SQL_Select_Reload))
                {
                    sbSQL.Append(this.mClassSchema.TableDef.SQL_Select_Reload);
                    sbSQL.Append(this.Slot.DbPrefixGetTableName(this.mClassSchema.TableDef));
                    sbSQL.Append(this.mClassSchema.PrimaryKey.SQL_Where_Clause);
                }

                //Genera la WHERE
                var oKeyValues = this.mClassSchema.PrimaryKey.FillKeyQueryWhereParams(db, this);

                //Esegue
                db.SQL = sbSQL.ToString();
                db.AddParameters(oDbParams);

                //A seconda
                //Se non presenti prop da aggiornare
                if (!this.mClassSchema.MustReload)
                {
                    //Esegue
                    int ret = db.ExecQuery();

                    //Controlla esito per verificare situazioni anomale
                    if (ret == 0)
                    {
                        //Nessuna Modifica
                        throw new ObjectException(Resources.ObjectMessages.Edit_NoRecord, this.GetType().Name, this.mClassSchema.PrimaryKey.Name, ObjectHelper.ObjectEnumerableToString(oKeyValues));
                    }
                    else if (ret > 1)
                    {
                        //Modifiche Multiple
                        throw new ObjectException(Resources.ObjectMessages.Edit_MultipleRecords, this.GetType().Name, this.mClassSchema.PrimaryKey.Name, ObjectHelper.ObjectEnumerableToString(oKeyValues));
                    }
                }
                else
                {//Presenti
                    using (DbDataReader dr = db.ExecReader())
                    {
                        //Controllo
                        if (!dr.Read())
                        {
                            //Nessuna Modifica
                            throw new ObjectException(Resources.ObjectMessages.Edit_NoRecord, this.GetType().Name, this.mClassSchema.PrimaryKey.Name, ObjectHelper.ObjectEnumerableToString(oKeyValues));
                        }

                        //Aggiorna proprieta'
                        for (int i = 0; i < this.mClassSchema.AutoProperties.Count; i++)
                        {
                            this.mClassSchema.AutoProperties[i].SetValueFromReader(this, dr);
                        }
                    }
                }
            }
            finally
            {
                //Resetta db per evitare eventuali parametri impostati
                //e mai utilizzati
                db.Reset();
            }

            return(ESaveResult.SaveDone);
        }
예제 #2
0
        /// <summary>
        /// Esegue la insert sul DB
        /// </summary>
        private ESaveResult performDbInsert()
        {
            //Appoggio
            IDataBase          db        = this.Slot.DbGet(this.mClassSchema);
            List <DbParameter> oDbParams = new List <DbParameter>(this.mClassSchema.Properties.Count);
            Property           oProp;
            var sTableFullName = this.Slot.DbPrefixGetTableName(this.mClassSchema.TableDef);

            try
            {
                //Inserisce campi
                for (int iPropIndex = 0; iPropIndex < this.mClassSchema.Properties.Count; iPropIndex++)
                {
                    oProp = this.mClassSchema.Properties[iPropIndex];

                    //In generale e' necessario impostare tutti i campi come non modificati
                    this.mDataSchema.SetFlags(oProp.PropertyIndex, DataFlags.Changed, false);

                    //Esclusione esplicita
                    if (oProp.ExcludeInsert || oProp.IsAutomatic)
                    {
                        continue;
                    }

                    //Imposta campi
                    object oValue = oProp.GetValueForDb(this);

                    //Imposta valore
                    oDbParams.Add(db.CreateParameter(oProp.Column.ParamName, oValue, oProp.Column.DbType));

                    //Aggiorna
                    this.mDataSchema.SetFlags(oProp.PropertyIndex, DataFlags.Loaded, true);
                }

                //Imposta SQL base
                var sbSql = new StringBuilder(@"INSERT INTO ", this.mClassSchema.TableDef.SQL_Insert.Length + 300);

                sbSql.Append(sTableFullName);
                sbSql.Append(@" ");
                sbSql.Append(string.Intern(this.mClassSchema.TableDef.SQL_Insert));

                //In base al tipo di esecuzione
                if (this.mClassSchema.MustReload)
                {
                    sbSql.Append(@";");
                    sbSql.Append(this.mClassSchema.TableDef.SQL_Select_Reload);
                    sbSql.Append(sTableFullName);
                    //Se autoinc imposta solo il nome della funzione
                    if (this.mClassSchema.AutoIncPk)
                    {
                        sbSql.Append(@" WHERE ");
                        sbSql.Append(this.mClassSchema.PrimaryKey.Properties[0].Column.Name);
                        sbSql.Append(@"=");
                        sbSql.Append(db.LastAutoIdFunction);
                    }
                    else
                    {
                        sbSql.Append(this.mClassSchema.PrimaryKey.SQL_Where_Clause);

                        for (int i = 0; i < this.mClassSchema.PrimaryKey.Properties.Count; i++)
                        {
                            oDbParams.Add(db.CreateParameter(this.mClassSchema.PrimaryKey.Properties[i].Column.GetKeyParamName(), this.mClassSchema.PrimaryKey.Properties[i].GetValueForDb(this)));
                        }
                    }

                    //Risolve
                    db.SQL = sbSql.ToString();
                    db.AddParameters(oDbParams);

                    using (DbDataReader dr = db.ExecReader())
                    {
                        //Controllo
                        if (!dr.Read())
                        {
                            throw new ObjectException(Resources.ObjectMessages.New_NoRecord, this.GetType().Name);
                        }

                        //Aggiorna proprieta'
                        for (int i = 0; i < this.mClassSchema.AutoProperties.Count; i++)
                        {
                            this.mClassSchema.AutoProperties[i].SetValueFromReader(this, dr);
                        }
                    }
                }
                else
                {
                    //Imposta db ed esegue
                    db.SQL = sbSql.ToString();
                    db.AddParameters(oDbParams);

                    //Controlla esito per verificare situazioni anomale
                    if (db.ExecQuery() == 0)
                    {
                        //Nessun Inserimento
                        throw new ObjectException(Resources.ObjectMessages.New_NoRecord, this.GetType().Name);
                    }
                }

                //Imposta stato caricato comunque
                this.mDataSchema.ObjectState = EObjectState.Loaded;
            }
            finally
            {
                //Resetta db per evitare eventuali parametri impostati
                //e mai utilizzati
                db.Reset();
            }

            return(ESaveResult.SaveDone);
        }