using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySqlConnector;
using skyscraper8.Skyscraper.Scraper.Storage;

namespace skyscraper5.Data.MySql
{
    public partial class MySqlDataStorage : DataStorage
    {
        public void SetNulls(DbCommand command)
        {
            foreach (DbParameter commandParameter in command.Parameters)
            {
                if (commandParameter.Value == null)
                    commandParameter.Value = DBNull.Value;
            }
        }
    }

    public static class MySqlDataReaderExtensions
    {
        public static byte[] GetByteArray(this MySqlDataReader reader, int column)
        {
            if (reader.IsDBNull(column))
                return null;

            Stream stream = reader.GetStream(column);
            int len32 = (int)stream.Length;
            byte[] buffer = new byte[len32];
            stream.Read(buffer, 0, len32);
            stream.Close();
            return buffer;
        }
    }

    public static class MySqlParameterCollectionExtensions
    {
        public static void AddOrSet<T>(this MySqlParameterCollection collection, string name, T value)
        {
            if (collection.Contains(name))
                collection[name].Value = value;
            else
                collection.AddWithValue(name, value);
        }
    }
}
