Пример #1
0
        /// <summary>
        ///     Return a sequence default string.
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="def"></param>
        /// <returns></returns>
        private static DefaultValue Sequence(string schema, string def)
        {
            var b = def.IndexOf("'", StringComparison.Ordinal);
            var e = def.IndexOf("'", b + 1, StringComparison.Ordinal);
            var seq = def.Substring(b + 1, e - b - 1);

            var defType = new DefaultValue
            {
                Type = DefaultValue.SequenceType,
                Value = "NEXT VALUE FOR [" + schema + "].[" + seq + "]",
                Sequence = seq

            };

            return defType;
        }
Пример #2
0
        /// <summary>
        ///     Convert a Postgres default value to SQL Server.
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="def"></param>
        /// <returns></returns>
        internal static DefaultValue SsDefaultValue(string schema, string def)
        {
            string typ;
            string val;

            // Check for sequence.
            var p = def.IndexOf("(", StringComparison.Ordinal);
            if (p > 0 && def.Substring(0, p).Equals("nextval")) {
                return Sequence(schema, def.Substring(p));
            }

            p = def.IndexOf("::", StringComparison.Ordinal);
            if (p > 0)
            {
                var p2 = def.IndexOf("(", p + 2, StringComparison.Ordinal);
                typ = p2 > 0 ? def.Substring(p + 2, p2 - p - 2) : def.Substring(p + 2);
                val = def.Substring(0, p);
            }
            else
            {
                typ = "";
                val = def;
            }

            var defType = new DefaultValue {Type = DefaultValue.ValueType};

            switch (typ)
            {
                case "":
                    switch (val)
                    {
                        case "now()":
                            defType.Value = "GETDATE()";
                            break;
                        case "false":
                            defType.Value = "0";
                            break;
                        case "true":
                            defType.Value = "1";
                            break;
                        default:
                            defType.Value = val;
                            break;
                    }
                    break;
                case "bpchar":
                case "date":
                case "timestamp without time zone":
                case "character":
                case "character varying":
                case "interval":
                case "text":
                    defType.Value = val;
                    break;
                default:
                    defType.Value = "";
                    break;
            }

            return defType;
        }