Exemple #1
0
        private void Escape(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
        {
            string quoteEscape = null;
            string slashEscape = null;

            if (mappings != null)
            {
                foreach (var c in Value)
                {
                    if (c == '"')
                    {
                        quoteEscape = quoteEscape ?? PostgresTuple.BuildQuoteEscape(escaping);
                        foreach (var q in quoteEscape)
                        {
                            mappings(sw, q);
                        }
                    }
                    else if (c == '\\')
                    {
                        slashEscape = slashEscape ?? PostgresTuple.BuildSlashEscape(escaping.Length);
                        foreach (var q in slashEscape)
                        {
                            mappings(sw, q);
                        }
                    }
                    else
                    {
                        mappings(sw, c);
                    }
                }
            }
            else
            {
                for (int i = 0; i < Value.Length; i++)
                {
                    var c = Value[i];
                    if (c == '"')
                    {
                        quoteEscape = quoteEscape ?? PostgresTuple.BuildQuoteEscape(escaping);
                        sw.Write(quoteEscape);
                    }
                    else if (c == '\\')
                    {
                        slashEscape = slashEscape ?? PostgresTuple.BuildSlashEscape(escaping.Length);
                        sw.Write(slashEscape);
                    }
                    else
                    {
                        sw.Write(c);
                    }
                }
            }
        }
Exemple #2
0
            public void InsertRecord(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
            {
                string quoteEscape = null;
                string slashEscape = null;
                int    c;

                if (mappings != null)
                {
                    while ((c = Reader.Read()) != -1)
                    {
                        if (c == '"')
                        {
                            quoteEscape = quoteEscape ?? PostgresTuple.BuildQuoteEscape(escaping);
                            foreach (var q in quoteEscape)
                            {
                                mappings(sw, q);
                            }
                        }
                        else if (c == '\\')
                        {
                            slashEscape = slashEscape ?? PostgresTuple.BuildSlashEscape(escaping.Length);
                            foreach (var q in slashEscape)
                            {
                                mappings(sw, q);
                            }
                        }
                        else
                        {
                            mappings(sw, (char)c);
                        }
                    }
                }
                else
                {
                    while ((c = Reader.Read()) != -1)
                    {
                        if (c == '"')
                        {
                            quoteEscape = quoteEscape ?? PostgresTuple.BuildQuoteEscape(escaping);
                            sw.Write(quoteEscape);
                        }
                        else if (c == '\\')
                        {
                            slashEscape = slashEscape ?? PostgresTuple.BuildSlashEscape(escaping.Length);
                            sw.Write(slashEscape);
                        }
                        else
                        {
                            sw.Write((char)c);
                        }
                    }
                }
            }
Exemple #3
0
            public void InsertRecord(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
            {
                var pref = PostgresTuple.BuildSlashEscape(escaping.Length);

                if (mappings != null)
                {
                    foreach (var p in pref)
                    {
                        mappings(sw, p);
                    }
                }
                else
                {
                    sw.Write(pref);
                }
                sw.Write('x');
                BuildArray(sw);
            }
Exemple #4
0
 public RecordTuple(PostgresTuple[] properties)
 {
     this.Properties = properties;
 }
Exemple #5
0
        public void InsertRecord(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
        {
            sw.Write('(');
            var    newEscaping = escaping + '1';
            string quote       = null;
            var    p           = Properties[0];

            if (p != null)
            {
                if (p.MustEscapeRecord)
                {
                    quote = PostgresTuple.BuildQuoteEscape(escaping);
                    if (mappings != null)
                    {
                        foreach (var q in quote)
                        {
                            mappings(sw, q);
                        }
                    }
                    else
                    {
                        sw.Write(quote);
                    }
                    p.InsertRecord(sw, buf, newEscaping, mappings);
                    if (mappings != null)
                    {
                        foreach (var q in quote)
                        {
                            mappings(sw, q);
                        }
                    }
                    else
                    {
                        sw.Write(quote);
                    }
                }
                else
                {
                    p.InsertRecord(sw, buf, escaping, mappings);
                }
            }
            for (int i = 1; i < Properties.Length; i++)
            {
                sw.Write(',');
                p = Properties[i];
                if (p != null)
                {
                    if (p.MustEscapeRecord)
                    {
                        //TODO: build quote only once and reuse it, instead of looping all the time
                        quote = quote ?? PostgresTuple.BuildQuoteEscape(escaping);
                        if (mappings != null)
                        {
                            foreach (var q in quote)
                            {
                                mappings(sw, q);
                            }
                        }
                        else
                        {
                            sw.Write(quote);
                        }
                        p.InsertRecord(sw, buf, newEscaping, mappings);
                        if (mappings != null)
                        {
                            foreach (var q in quote)
                            {
                                mappings(sw, q);
                            }
                        }
                        else
                        {
                            sw.Write(quote);
                        }
                    }
                    else
                    {
                        p.InsertRecord(sw, buf, escaping, mappings);
                    }
                }
            }
            sw.Write(')');
        }
Exemple #6
0
            public void InsertRecord(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
            {
                var esc         = PostgresTuple.BuildQuoteEscape(escaping);
                var quoteEscape = new Lazy <string>(() => PostgresTuple.BuildQuoteEscape(escaping + "0"));
                var slashEscape = new Lazy <string>(() => PostgresTuple.BuildSlashEscape(escaping.Length + 1));
                var len         = Value.Count;

                if (mappings != null)
                {
                    foreach (var kv in Value)
                    {
                        len--;
                        foreach (var c in esc)
                        {
                            mappings(sw, c);
                        }
                        foreach (var c in kv.Key)
                        {
                            if (c == '"')
                            {
                                foreach (var e in quoteEscape.Value)
                                {
                                    mappings(sw, e);
                                }
                            }
                            else if (c == '\\')
                            {
                                foreach (var e in slashEscape.Value)
                                {
                                    mappings(sw, e);
                                }
                            }
                            else
                            {
                                mappings(sw, c);
                            }
                        }
                        foreach (var c in esc)
                        {
                            mappings(sw, c);
                        }
                        sw.Write("=>");
                        if (kv.Value == null)
                        {
                            sw.Write("NULL");
                        }
                        else
                        {
                            foreach (var c in esc)
                            {
                                mappings(sw, c);
                            }
                            foreach (var c in kv.Value)
                            {
                                if (c == '"')
                                {
                                    foreach (var e in quoteEscape.Value)
                                    {
                                        mappings(sw, e);
                                    }
                                }
                                else if (c == '\\')
                                {
                                    foreach (var e in slashEscape.Value)
                                    {
                                        mappings(sw, e);
                                    }
                                }
                                else
                                {
                                    mappings(sw, c);
                                }
                            }
                            foreach (var c in esc)
                            {
                                mappings(sw, c);
                            }
                        }
                        if (len > 0)
                        {
                            sw.Write(", ");
                        }
                    }
                }
                else
                {
                    foreach (var kv in Value)
                    {
                        len--;
                        sw.Write(esc);
                        foreach (var c in kv.Key)
                        {
                            if (c == '"')
                            {
                                sw.Write(quoteEscape.Value);
                            }
                            else if (c == '\\')
                            {
                                sw.Write(slashEscape.Value);
                            }
                            else
                            {
                                sw.Write(c);
                            }
                        }
                        sw.Write(esc);
                        sw.Write("=>");
                        if (kv.Value == null)
                        {
                            sw.Write("NULL");
                        }
                        else
                        {
                            sw.Write(esc);
                            foreach (var c in kv.Value)
                            {
                                if (c == '"')
                                {
                                    sw.Write(quoteEscape.Value);
                                }
                                else if (c == '\\')
                                {
                                    sw.Write(slashEscape.Value);
                                }
                                else
                                {
                                    sw.Write(c);
                                }
                            }
                            sw.Write(esc);
                        }
                        if (len > 0)
                        {
                            sw.Write(", ");
                        }
                    }
                }
            }
Exemple #7
0
 public string BuildTuple(bool quote)
 {
     return(PostgresTuple.BuildTuple(this, quote));
 }
Exemple #8
0
        public void InsertRecord(TextWriter sw, char[] buf, string escaping, Action <TextWriter, char> mappings)
        {
            sw.Write('{');
            var    newEscaping = escaping + "0";
            string quote       = null;
            var    e           = Elements[0];

            if (e != null)
            {
                if (e.MustEscapeArray)
                {
                    quote = quote ?? PostgresTuple.BuildQuoteEscape(escaping);
                    if (mappings != null)
                    {
                        foreach (var q in quote)
                        {
                            mappings(sw, q);
                        }
                    }
                    else
                    {
                        sw.Write(quote);
                    }
                    e.InsertArray(sw, buf, newEscaping, mappings);
                    if (mappings != null)
                    {
                        foreach (var q in quote)
                        {
                            mappings(sw, q);
                        }
                    }
                    else
                    {
                        sw.Write(quote);
                    }
                }
                else
                {
                    e.InsertArray(sw, buf, escaping, mappings);
                }
            }
            else
            {
                sw.Write("NULL");
            }
            for (int i = 1; i < Elements.Length; i++)
            {
                sw.Write(',');
                e = Elements[i];
                if (e != null)
                {
                    if (e.MustEscapeArray)
                    {
                        quote = quote ?? PostgresTuple.BuildQuoteEscape(escaping);
                        if (mappings != null)
                        {
                            foreach (var q in quote)
                            {
                                mappings(sw, q);
                            }
                        }
                        else
                        {
                            sw.Write(quote);
                        }
                        e.InsertArray(sw, buf, newEscaping, mappings);
                        if (mappings != null)
                        {
                            foreach (var q in quote)
                            {
                                mappings(sw, q);
                            }
                        }
                        else
                        {
                            sw.Write(quote);
                        }
                    }
                    else
                    {
                        e.InsertArray(sw, buf, escaping, mappings);
                    }
                }
                else
                {
                    sw.Write("NULL");
                }
            }
            sw.Write('}');
        }