mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			87 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using FreeSql.Internal;
 | 
						|
using FreeSql.Internal.Model;
 | 
						|
using FreeSql.Internal.ObjectPool;
 | 
						|
using System;
 | 
						|
using System.Collections;
 | 
						|
using System.Data.Common;
 | 
						|
using System.Text;
 | 
						|
using System.Threading;
 | 
						|
#if MySqlConnector
 | 
						|
using MySqlConnector;
 | 
						|
#else
 | 
						|
using MySql.Data.MySqlClient;
 | 
						|
#endif
 | 
						|
 | 
						|
namespace FreeSql.MySql
 | 
						|
{
 | 
						|
    class MySqlAdo : FreeSql.Internal.CommonProvider.AdoProvider
 | 
						|
    {
 | 
						|
 | 
						|
        public MySqlAdo() : base(DataType.MySql, null, null) { }
 | 
						|
        public MySqlAdo(CommonUtils util, string masterConnectionString, string[] slaveConnectionStrings, Func<DbConnection> connectionFactory) : base(DataType.MySql, masterConnectionString, slaveConnectionStrings)
 | 
						|
        {
 | 
						|
            base._util = util;
 | 
						|
            if (connectionFactory != null)
 | 
						|
            {
 | 
						|
                var pool = new FreeSql.Internal.CommonProvider.DbConnectionPool(DataType.MySql, connectionFactory);
 | 
						|
                ConnectionString = pool.TestConnection?.ConnectionString;
 | 
						|
                MasterPool = pool;
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            if (!string.IsNullOrEmpty(masterConnectionString))
 | 
						|
                MasterPool = new MySqlConnectionPool(CoreStrings.S_MasterDatabase, masterConnectionString, null, null);
 | 
						|
            if (slaveConnectionStrings != null)
 | 
						|
            {
 | 
						|
                foreach (var slaveConnectionString in slaveConnectionStrings)
 | 
						|
                {
 | 
						|
                    var slavePool = new MySqlConnectionPool($"{CoreStrings.S_SlaveDatabase}{SlavePools.Count + 1}", slaveConnectionString, () => Interlocked.Decrement(ref slaveUnavailables), () => Interlocked.Increment(ref slaveUnavailables));
 | 
						|
                    SlavePools.Add(slavePool);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        public override object AddslashesProcessParam(object param, Type mapType, ColumnInfo mapColumn)
 | 
						|
        {
 | 
						|
            if (param == null) return "NULL";
 | 
						|
            if (mapType != null && mapType != param.GetType() && (param is IEnumerable == false))
 | 
						|
                param = Utils.GetDataReaderValue(mapType, param);
 | 
						|
 | 
						|
            if (param is bool || param is bool?)
 | 
						|
                return (bool)param ? 1 : 0;
 | 
						|
            else if (param is string)
 | 
						|
                return string.Concat("'", param.ToString().Replace("'", "''").Replace("\\", "\\\\"), "'"); //只有 mysql 需要处理反斜杠
 | 
						|
            else if (param is char)
 | 
						|
                return string.Concat("'", param.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace('\0', ' '), "'");
 | 
						|
            else if (param is Enum)
 | 
						|
                return string.Concat("'", param.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace(", ", ","), "'"); //((Enum)val).ToInt64();
 | 
						|
            else if (decimal.TryParse(string.Concat(param), out var trydec))
 | 
						|
                return param;
 | 
						|
            else if (param is DateTime || param is DateTime?)
 | 
						|
                return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss.fff"), "'");
 | 
						|
            else if (param is TimeSpan || param is TimeSpan?)
 | 
						|
                return ((TimeSpan)param).Ticks / 10;
 | 
						|
            else if (param is byte[])
 | 
						|
                return $"0x{CommonUtils.BytesSqlRaw(param as byte[])}";
 | 
						|
            else if (param is MygisGeometry)
 | 
						|
                return string.Concat("ST_GeomFromText('", (param as MygisGeometry).AsText().Replace("'", "''").Replace("\\", "\\\\"), "')");
 | 
						|
            else if (param is IEnumerable)
 | 
						|
                return AddslashesIEnumerable(param, mapType, mapColumn);
 | 
						|
 | 
						|
            return string.Concat("'", param.ToString().Replace("'", "''").Replace("\\", "\\\\"), "'");
 | 
						|
        }
 | 
						|
 | 
						|
        public override DbCommand CreateCommand()
 | 
						|
        {
 | 
						|
            return new MySqlCommand();
 | 
						|
        }
 | 
						|
 | 
						|
        public override void ReturnConnection(IObjectPool<DbConnection> pool, Object<DbConnection> conn, Exception ex)
 | 
						|
        {
 | 
						|
            var rawPool = pool as MySqlConnectionPool;
 | 
						|
            if (rawPool != null) rawPool.Return(conn, ex);
 | 
						|
            else pool.Return(conn);
 | 
						|
        }
 | 
						|
 | 
						|
        public override DbParameter[] GetDbParamtersByObject(string sql, object obj) => _util.GetDbParamtersByObject(sql, obj);
 | 
						|
    }
 | 
						|
}
 |