You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.3 KiB
C#

using System;
using System.Data;
using System.IO;
using Common.Library.SQL.Base;
using MySql.Data.MySqlClient;
namespace Common.Library.SQL.MySQL
{
public class MySQL : SQLBase
{
protected MySqlConnection DBConnection { get; set; }
protected MySqlCommand DBCommand { get; set; }
protected MySqlDataReader DBReader { get; set; }
public MySQL(string server, string database, string username, string password)
{
DBConnectionInfo = new DBConnectionInformation()
{
DBHost = server,
DBUserName = username,
DBName = database,
DBPassword = password,
DBType = DBType.MYSQL
};
}
public MySQL(DBConnectionInformation connectionInformation)
{
DBConnectionInfo = connectionInformation;
}
protected override void InitConnection()
{
DBConnection = new MySqlConnection(DBConnectionInfo.GetConnectionString());
DBConnection.Open();
DBCommand = DBConnection.CreateCommand();
DBCommand.CommandType = CommandType.Text;
DBCommand.CommandText = "SET character_set_results=utf8mb4";
DBCommand.ExecuteNonQuery();
}
protected override void DisposeConnection()
{
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
}
protected override int GetOrdinal(string columnName)
{
return DBReader.GetOrdinal(columnName);
}
protected override int? GetInt(int columnID)
{
int? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetInt32(columnID);
}
return rv;
}
protected override int? GetInt(string columnName)
{
return GetInt(GetOrdinal(columnName));
}
protected override byte? GetByte(int columnID)
{
byte? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetByte(columnID);
}
return rv;
}
protected override byte? GetByte(string columnName)
{
return GetByte(GetOrdinal(columnName));
}
protected override byte[] GetBytes(int columnID)
{
byte[] rv = null;
if (!DBReader.IsDBNull(columnID))
{
// const int CHUNK_SIZE = 2 * 1024;
// byte[] buffer = new byte[CHUNK_SIZE];
// long bytesRead;
// long fieldOffset = 0;
// using (var stream = new MemoryStream())
// {
// while ((bytesRead = DBReader.GetBytes(columnID, fieldOffset, buffer, 0, buffer.Length)) == buffer.Length)
// {
// stream.Write(buffer, 0, (int)bytesRead);
// fieldOffset += bytesRead;
// }
// rv = stream.ToArray();
// }
rv = (byte[])DBReader[columnID];
}
return rv;
}
protected override byte[] GetBytes(string columnName)
{
return GetBytes(GetOrdinal(columnName));
}
protected override decimal? GetDecimal(int columnID)
{
decimal? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetDecimal(columnID);
}
return rv;
}
protected override decimal? GetDecimal(string columnName)
{
return GetDecimal(GetOrdinal(columnName));
}
protected override long? GetLong(int columnID)
{
long? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetInt64(columnID);
}
return rv;
}
protected override long? GetLong(string columnName)
{
return GetLong(GetOrdinal(columnName));
}
protected override short? GetShort(int columnID)
{
short? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetInt16(columnID);
}
return rv;
}
protected override short? GetShort(string columnName)
{
return GetShort(GetOrdinal(columnName));
}
protected override string GetString(int columnID)
{
string rv = string.Empty;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetString(columnID);
}
return rv;
}
protected override string GetString(string columnName)
{
return GetString(GetOrdinal(columnName));
}
protected override bool? GetBool(int columnID)
{
bool? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = DBReader.GetBoolean(columnID);
}
return rv;
}
protected override bool? GetBool(string columnName)
{
return GetBool(GetOrdinal(columnName));
}
protected override DateTime? GetDateTime(int columnID)
{
DateTime? rv = null;
if (!DBReader.IsDBNull(columnID))
{
rv = GetDateTime(columnID);
}
return rv;
}
protected override DateTime? GetDateTime(string columnName)
{
return GetDateTime(GetOrdinal(columnName));
}
//new MySqlParameter("GUID", MySqlDbType.VarChar, 255, ParameterDirection.Input, false, 0, 0, string.Empty, DataRowVersion.Proposed, guid)
protected MySqlParameter GetParameter(string columnName, MySqlDbType dataType, ParameterDirection direction, object data)
{
return null;
}
protected MySqlParameter GetParameter(string columnName, MySqlDbType dataType, int size, ParameterDirection direction, object data)
{
return new MySqlParameter(columnName, dataType, size, direction, false, 0, 0, string.Empty, DataRowVersion.Proposed, data);
}
}
}