Пример #1
0
        /// <summary>
        /// Extract PrimaryKey Definition from context
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static MySqlKeyDefinition ExtractPrimaryKey(PrimaryKeyTableConstraintContext context)
        {
            if (context == null)
            {
                throw new ArgumentOutOfRangeException($"{nameof(context)} is null");
            }

            var keys   = context.GetIndexNames();
            var pkName = context.GetChildlen <IndexColumnNamesContext>()
                         .Single()
                         .GetText().RemoveBackQuote().RemoveParenthesis();
            var pkIndexes  = keys.Select(x => x.RemoveBackQuote().RemoveParenthesis()).ToArray();
            var definition = new MySqlKeyDefinition
            {
                KeyName = pkName,
                Indexes = pkIndexes.Select((x, i) => new MySqlKeyDetailDefinition
                {
                    Order    = i,
                    IndexKey = x,
                })
                          .ToArray(),
            };

            return(definition);
        }
        /// <summary>
        /// Get PrimaryKey names from context.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string[] GetIndexNames(this PrimaryKeyTableConstraintContext context)
        {
            if (context == null)
            {
                throw new ArgumentOutOfRangeException($"{nameof(context)} is null");
            }
            var result = context.GetChildlen <IndexColumnNamesContext>()
                         .SelectMany(x => x.GetChildlen <IndexColumnNameContext>())
                         .Select(x => x.GetText())
                         .ToArray();

            return(result);
        }