Exemple #1
0
        /// <summary>
        /// Esegue caricamento singola property da DB
        /// </summary>
        /// <param name="prop"></param>
        internal void LoadPropertyFromDB(PropertySimple prop)
        {
            //Imposta db
            IDataBase db = this.Slot.DbGet(this.mClassSchema);

            //Imposta SQL (preleva quello standard dalla PK e sostituisce il contenuto della select con il solo nome campo)
            db.SQL = string.Concat(@"SELECT ", prop.Column.Name, " FROM ", this.Slot.DbPrefixGetTableName(this.mClassSchema.TableDef), this.mClassSchema.PrimaryKey.SQL_Where_Clause);

            //Imposta PK
            var oKeyValues = this.mClassSchema.PrimaryKey.FillKeyQueryWhereParams(db, this);

            //Esegue query su datareader
            using (DbDataReader dr = db.ExecReader())
            {
                //Controlla presenza risultato e si dispone sul record
                if (!dr.Read())
                {
                    throw new ObjectException(Resources.ObjectMessages.Base_RecordKeyNotFound, this.GetType().Name, this.mClassSchema.PrimaryKey.Name, oKeyValues);
                }

                //OK Imposta oggetto
                prop.SetValueFromReader(this, dr);
            }
        }
Exemple #2
0
        /// <summary>
        /// Legge il tipo e ritorna lo schema pronto
        /// </summary>
        /// <param name="originalType"></param>
        /// <param name="InternalID"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        private static ClassSchema readClassSchemaWithSQL(Type originalType, long InternalID, PropertyInfo[] properties)
        {
            short            iPropertyIndex = 0;
            List <SearchKey> oListKeys      = new List <SearchKey>(2);
            StringBuilder    sbSqlSelect    = new StringBuilder(1000);
            StringBuilder    sbSqlReload    = new StringBuilder(100);
            StringBuilder    sbSqlInsertF   = new StringBuilder(400);
            StringBuilder    sbSqlInsertP   = new StringBuilder(400);

            sbSqlSelect.Append("SELECT ");
            sbSqlReload.Append("SELECT ");

            //Crea Schema
            ClassSchema oSchema = new ClassSchema(originalType);

            //Imposta ID Interno Schema
            oSchema.InternalID = InternalID;

            //Imposta info schema (tabella) se fornite
            foreach (Attribute att in originalType.GetCustomAttributes(false))
            {
                oSchema.FillFromAttribute(att);
            }

            //Nome tabella insert

            //Legge definizione campi
            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo propInfo = properties[i];

                if (propInfo == null)
                {
                    continue;
                }

                //Azzera variabili ciclo
                oListKeys.Clear();

                //Crea nuova Property
                Property oProp;

                //Verifica il tipo di proprietà
                if (propInfo.PropertyType.IsSubclassOf(typeof(DataObjectBase)))
                {
                    oProp = new PropertyObject(propInfo.Name, propInfo.PropertyType, oSchema.ObjCount);
                    oSchema.ObjCount++; //Incrementa contatore generale
                }
                else if (propInfo.PropertyType.IsSubclassOf(typeof(DataListBase)))
                {
                    oProp = new PropertyDataList(propInfo.Name, propInfo.PropertyType);
                }
                else
                {
                    oProp = new PropertySimple(propInfo.Name, propInfo.PropertyType);
                }

                oProp.Schema     = oSchema;
                oProp.IsReadonly = !propInfo.CanWrite;

                //Inizia valorizzazione
                foreach (Attribute attr in propInfo.GetCustomAttributes(false))
                {
                    if (!(attr is BaseAttribute))
                    {
                        continue;
                    }

                    //Primary Key, UniqueKey o SearchKey
                    if (attr is SearchKey)
                    {
                        oListKeys.Add((SearchKey)attr);
                    }
                    //ALTRI ATTRIBUTI
                    else
                    {
                        //Carica attributi
                        try
                        {
                            oProp.FillFromAttribute((BaseAttribute)attr);
                        }
                        catch (NotImplementedException)
                        {
                            throw new TypeFactoryException("{0}.{1} - Attrinuto '{2}' non previsto per il tipo di propieta'", oSchema.ClassName, oProp.Name, attr.GetType());
                        }
                        catch (Exception ex)
                        {
                            throw new TypeFactoryException("{0}.{1} - {2}", oSchema.ClassName, oProp.Name, ex.Message);
                        }
                    }

                    //FINE LOOP ATTRIBUTI
                }

                //Se non sono presenti campi definiti esplicitamente e non e' una mappatura interna ne crea uno di default
                if (oProp is PropertySimple)
                {
                    if (oProp.Column == null)
                    {
                        oProp.Column = new Column(oProp.Name);
                    }
                }


                //Gestisce una o più chiavi
                for (int j = 0; j < oListKeys.Count; j++)
                {
                    fillKeyAttribute(oSchema, oProp, oListKeys[j]);
                }

                //Esegue validazione proprietà
                oProp.ValidateDefinition();

                //Imposta indice e incrementa
                oProp.PropertyIndex = iPropertyIndex++;

                //Aggiunge a schema
                oSchema.Properties.Add(oProp);


                //Imposta flag di nuovo caricamento
                if (oProp.IsAutomatic)
                {
                    oSchema.MustReload = true;
                }

                //Select: esclude loadonaccess e mappature property-property
                if (!oProp.IsSqlSelectExcluded)
                {
                    sbSqlSelect.Append(oProp.Column.Name);
                    sbSqlSelect.Append(@", ");
                }

                //Se READONLY NON GENERA nulla
                if (!oSchema.IsReadOnly)
                {
                    //Reload:
                    if (oProp.IsAutomatic)
                    {
                        //Imposta sql reload
                        sbSqlReload.Append(oProp.Column.Name);
                        sbSqlReload.Append(@", ");
                    }

                    //Insert
                    if (!oProp.ExcludeInsert)
                    {
                        //Nomi campi insert
                        sbSqlInsertF.Append(oProp.Column.Name);
                        sbSqlInsertF.Append(@", ");

                        //Parametri o costanti
                        if (oProp.IsAutomatic)
                        {
                            sbSqlInsertP.Append(@"CURRENT_TIMESTAMP, ");
                        }
                        else
                        {
                            sbSqlInsertP.Append(oProp.Column.ParamName);
                            sbSqlInsertP.Append(@", ");
                        }
                    }
                }

                //FINE LOOP PROPRIETA'
            }


            //Esegue validazione schema
            oSchema.Validate();

            //SQLSELECT - Tabella
            sbSqlSelect.Remove(sbSqlSelect.Length - 2, 2);
            sbSqlSelect.Append(@" FROM ");

            oSchema.TableDef.SQL_Select_Item = sbSqlSelect.ToString();
            //SQLSELECT - Per ogni chiave prepara la where
            foreach (var key in oSchema.Keys.Values)
            {
                sbSqlSelect.Length = 0;//Resetta
                sbSqlSelect.Append(@" WHERE ");
                for (int i = 0; i < key.Properties.Count; i++)
                {
                    sbSqlSelect.Append(key.Properties[i].Column.Name);
                    sbSqlSelect.Append(@"=");
                    sbSqlSelect.Append(key.Properties[i].Column.GetKeyParamName());
                    sbSqlSelect.Append(@" AND ");
                }

                sbSqlSelect.Remove(sbSqlSelect.Length - 5, 5);

                //Imposta SQL completo
                key.SQL_Where_Clause = sbSqlSelect.ToString();

                //Se PK genera query base per lista
                if (key.Name.Equals(ClassSchema.PRIMARY_KEY))
                {
                    //Prepara la query base per la lista
                    sbSqlSelect.Length = 0;//Resetta
                    sbSqlSelect.Append(@"SELECT ");
                    for (int i = 0; i < key.Properties.Count; i++)
                    {
                        sbSqlSelect.Append(key.Properties[i].Column.Name);
                        sbSqlSelect.Append(@", ");
                    }
                    sbSqlSelect.Remove(sbSqlSelect.Length - 2, 2);
                    sbSqlSelect.Append(@" FROM ");

                    //Aggiunge
                    oSchema.TableDef.SQL_Select_List = sbSqlSelect.ToString();
                }
            }


            if (!oSchema.IsReadOnly)
            {
                //Rimuove caratteri
                sbSqlInsertF.Remove(sbSqlInsertF.Length - 2, 2);
                sbSqlInsertP.Remove(sbSqlInsertP.Length - 2, 2);

                //Sql reload
                if (sbSqlReload.Length > 7)
                {
                    //Rimuove caratteri
                    sbSqlReload.Remove(sbSqlReload.Length - 2, 2);
                    sbSqlReload.Append(@" FROM ");

                    oSchema.TableDef.SQL_Select_Reload = sbSqlReload.ToString();
                }

                //Insert
                oSchema.TableDef.SQL_Insert = string.Concat(@" (", sbSqlInsertF.ToString(), @") VALUES (", sbSqlInsertP.ToString(), @") ");
            }

            //Ritorna schema letto
            return(oSchema);
        }