Exemple #1
0
        /// <summary>
        /// Détermine le nom du type T-SQL avec la précision.
        /// </summary>
        /// <param name="property">Propriété de classe.</param>
        /// <returns>Nom du type T-SQL.</returns>
        public static string DeterminerSqlDataType(this ModelProperty property)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            IPersistenceData persistenceData =
                property.IsDatabaseOnly ?
                property.DataMember :                              // Propriété non applicative : les données de persistence sont portées par le champ directement.
                (IPersistenceData)property.DataDescription.Domain; // Propriété applicative : les données de persistences sont portées par le domaine.

            string persistentType = CodeUtils.PowerDesignerPersistentDataTypeToSqlDatType(persistenceData.PersistentDataType);

            bool hasLength    = persistenceData.PersistentLength.HasValue;
            bool hasPrecision = persistenceData.PersistentPrecision.HasValue;

            if (hasLength)
            {
                persistentType += "(" + persistenceData.PersistentLength;
                if (hasPrecision)
                {
                    persistentType += "," + persistenceData.PersistentPrecision;
                }

                persistentType += ")";
            }
            else if (property.DataDescription.IsPrimaryKey && property.DataDescription.Domain.Code == "DO_ID")
            {
                persistentType += " identity";
            }

            return(persistentType);
        }
Exemple #2
0
        /// <summary>
        /// Déclaration de la table.
        /// </summary>
        /// <param name="classe">La table à ecrire.</param>
        /// <param name="writerCrebas">Flux d'écriture crebas.</param>
        /// <param name="writerUk">Flux d'écriture Unique Key.</param>
        /// <param name="writerType">Flux d'écritures des types.</param>
        /// <returns>Liste des propriétés étrangères persistentes.</returns>
        private IEnumerable <ModelProperty> WriteTableDeclaration(ModelClass classe, StreamWriter writerCrebas, StreamWriter writerUk, StreamWriter writerType)
        {
            ICollection <ModelProperty> fkPropertiesList = new List <ModelProperty>();

            string tableName = CheckIdentifierLength(classe.DataContract.Name.ToUpperInvariant());

            writerCrebas.WriteLine("/**");
            writerCrebas.WriteLine("  * Création de la table " + tableName);
            writerCrebas.WriteLine(" **/");
            writerCrebas.WriteLine("create table " + tableName + " (");

            bool isContainsInsertKey = classe.PersistentPropertyList.Where(p => p.Name == InsertKeyName).Count() > 0;

            if (isContainsInsertKey)
            {
                WriteType(classe, writerType);
            }

            int  nbPropertyCount = classe.PersistentPropertyList.Count;
            int  t = 0;
            bool hasUniqueMultipleProperties = false;

            foreach (ModelProperty property in classe.PersistentPropertyList)
            {
                string persistentType = CodeUtils.PowerDesignerPersistentDataTypeToSqlDatType(property.DataDescription.Domain.PersistentDataType);
                persistentType = GetLengthInformation(property, persistentType);
                writerCrebas.Write("\t" + CheckIdentifierLength(property.DataMember.Name) + " " + persistentType);
                if (property.DataDescription.IsPrimaryKey && property.DataDescription.Domain.Code == "DO_ID")
                {
                    WriteIdentityColumn(writerCrebas);
                }

                if (isContainsInsertKey && !property.DataDescription.IsPrimaryKey && property.Name != InsertKeyName)
                {
                    if (t > 0)
                    {
                        writerType.Write(",");
                        writerType.WriteLine();
                    }

                    writerType.Write("\t" + property.DataMember.Name + " " + persistentType);
                    t++;
                }

                if (property.DataMember.IsRequired)
                {
                    writerCrebas.Write(" not null");
                }

                writerCrebas.Write(",");
                writerCrebas.WriteLine();

                if (!string.IsNullOrEmpty(property.DataDescription.ReferenceType))
                {
                    fkPropertiesList.Add(property);
                }

                if (property.IsUniqueMany)
                {
                    hasUniqueMultipleProperties = true;
                }

                if (property.IsUnique)
                {
                    writerUk.WriteLine("alter table " + classe.DataContract.Name.ToUpperInvariant() + " add constraint " + CheckIdentifierLength("UK_" + classe.DataContract.Name.ToUpperInvariant() + '_' + property.Name.ToUpperInvariant()) + " unique (" + property.DataMember.Name + ")");
                    writerUk.WriteLine(BatchSeparator);
                    writerUk.WriteLine();
                }
            }

            WriteBooleanConstraints(writerCrebas, classe);
            WritePrimaryKeyConstraint(writerCrebas, classe);
            WriteTableSpaceTable(writerCrebas, classe);
            writerCrebas.WriteLine(BatchSeparator);

            CreateSequenceIfNeeded(writerCrebas, classe);

            if (hasUniqueMultipleProperties)
            {
                WriteUniqueMultipleProperties(classe, writerUk);
            }

            writerCrebas.WriteLine();

            if (isContainsInsertKey)
            {
                if (t > 0)
                {
                    writerType.Write(',');
                    writerType.WriteLine();
                }

                writerType.WriteLine('\t' + classe.Trigram + '_' + "INSERT_KEY int");
                writerType.WriteLine();
                writerType.WriteLine(")");
                writerType.WriteLine(BatchSeparator);
                writerType.WriteLine();
            }

            // Histo table
            if (classe.IsHistorized)
            {
                string histTableName        = "HIST_" + tableName;
                string triggerCommandInsert = "INSERT INTO " + histTableName + "(";
                string triggerCommandValue  = "VALUES (";

                // Table histo.
                writerCrebas.WriteLine("/**");
                writerCrebas.WriteLine("  * Création de la table " + histTableName);
                writerCrebas.WriteLine(" **/");
                writerCrebas.WriteLine("create table " + histTableName + " (");
                foreach (ModelProperty property in classe.PersistentPropertyList)
                {
                    triggerCommandInsert += property.DataMember.Name + ", ";
                    triggerCommandValue  += ":new." + property.DataMember.Name + ", ";
                    string persistentType = CodeUtils.PowerDesignerPersistentDataTypeToSqlDatType(property.DataDescription.Domain.PersistentDataType);
                    persistentType = GetLengthInformation(property, persistentType);
                    writerCrebas.Write("\t" + CheckIdentifierLength(property.DataMember.Name) + " " + persistentType);

                    writerCrebas.Write(",");
                    writerCrebas.WriteLine();
                }

                writerCrebas.WriteLine("\tDATE_EDIT  TIMESTAMP(6) NOT NULL,");
                writerCrebas.WriteLine("\tIS_CREATION NUMBER(1,0) NOT NULL");

                writerCrebas.WriteLine(")");
                WriteTableSpaceTable(writerCrebas, classe);
                writerCrebas.WriteLine(BatchSeparator);
                writerCrebas.WriteLine();

                WriteTrigger(writerCrebas, tableName, triggerCommandInsert, triggerCommandValue, true);
                WriteTrigger(writerCrebas, tableName, triggerCommandInsert, triggerCommandValue, false);
            }

            return(fkPropertiesList);
        }