sunkaixuan 7 years ago
parent
commit
f1d0cfedea
  1. 8
      Src/Asp.Net/SqlSugar/Infrastructure/DependencyManagement.cs
  2. 67
      Src/Asp.Net/SqlSugar/Realization/PostgreSQL/CodeFirst/PostgreSQLCodeFirst.cs
  3. 92
      Src/Asp.Net/SqlSugar/Realization/PostgreSQL/DbBind/PostgreSQLDbBind.cs
  4. 11
      Src/Asp.Net/SqlSugar/Realization/PostgreSQL/DbFirst/PostgreSQLDbFirst.cs
  5. 116
      Src/Asp.Net/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs
  6. 60
      Src/Asp.Net/SqlSugar/Realization/PostgreSQL/Queryable/PostgreSQLQueryable.cs
  7. 10
      Src/Asp.Net/SqlSugar/SqlSugar.csproj
  8. 3
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs

8
Src/Asp.Net/SqlSugar/Infrastructure/DependencyManagement.cs

@ -50,6 +50,14 @@ namespace SqlSugar
}
}
internal static void TryPostgreSQL()
{
var message = ErrorMessage.GetThrowMessage(
"SqlSugar PostGreSQL only support.NET CORE",
"SqlSugar使用 PostGreSQL只支持.NET CORE");
throw new Exception(message);
}
internal static void TryOracle()
{
if (!IsTryOracle)

67
Src/Asp.Net/SqlSugar/Realization/PostgreSQL/CodeFirst/PostgreSQLCodeFirst.cs

@ -1,67 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class PostgreSQLCodeFirst : CodeFirstProvider
{
public override void NoExistLogic(EntityInfo entityInfo)
{
var tableName = GetTableName(entityInfo);
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
List<DbColumnInfo> columns = new List<DbColumnInfo>();
if (entityInfo.Columns.HasValue())
{
foreach (var item in entityInfo.Columns)
{
DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
columns.Add(dbColumnInfo);
}
}
this.Context.DbMaintenance.CreateTable(tableName, columns);
}
protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
{
var result = new DbColumnInfo()
{
DataType = this.Context.Ado.DbBind.GetDbTypeName(UtilMethods.GetUnderType(item.PropertyInfo).Name),
TableId = entityInfo.Columns.IndexOf(item),
DbColumnName = item.DbColumnName.HasValue() ? item.DbColumnName : item.PropertyName,
IsPrimarykey = item.IsPrimarykey,
IsIdentity = item.IsIdentity,
TableName = tableName,
IsNullable = item.IsNullable,
DefaultValue = item.DefaultValue,
ColumnDescription = item.ColumnDescription,
Length = item.Length
};
if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
{
result.Length = 1;
}
return result;
}
protected override void ConvertColumns(List<DbColumnInfo> dbColumns)
{
foreach (var item in dbColumns)
{
if (item.DataType == "DateTime")
{
item.Length = 0;
}
}
}
protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
{
this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
if (!item.IsPrimarykey)
this.Context.DbMaintenance.DropConstraint(tableName,null);
if (item.IsPrimarykey)
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
}
}
}

92
Src/Asp.Net/SqlSugar/Realization/PostgreSQL/DbBind/PostgreSQLDbBind.cs

@ -1,92 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace SqlSugar
{
public class PostgreSQLDbBind : DbBindProvider
{
public override string GetDbTypeName(string csharpTypeName)
{
if (csharpTypeName == UtilConstants.ByteArrayType.Name)
{
return "blob";
}
if (csharpTypeName == "Int32")
csharpTypeName = "int";
if (csharpTypeName == "Int16")
csharpTypeName = "short";
if (csharpTypeName == "Int64")
csharpTypeName = "long";
if (csharpTypeName == "Boolean")
csharpTypeName = "bool";
var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName, StringComparison.CurrentCultureIgnoreCase));
return mappings.HasValue() ? mappings.First().Key : "varchar";
}
public override List<KeyValuePair<string, CSharpDataType>> MappingTypes
{
get
{
var extService = this.Context.CurrentConnectionConfig.ConfigureExternalServices;
if (extService != null && extService.AppendDataReaderTypeMappings.HasValue())
{
return extService.AppendDataReaderTypeMappings.Union(MappingTypesConst).ToList();
}
else
{
return MappingTypesConst;
}
}
}
public static List<KeyValuePair<string, CSharpDataType>> MappingTypesConst = new List<KeyValuePair<string, CSharpDataType>>(){
new KeyValuePair<string, CSharpDataType>("int",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("mediumint",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("integer",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("enum",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("mediumtext",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("tinytext",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("longtext",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("tinyint",CSharpDataType.@byte),
new KeyValuePair<string, CSharpDataType>("smallint",CSharpDataType.@short),
new KeyValuePair<string, CSharpDataType>("bigint",CSharpDataType.@long),
new KeyValuePair<string, CSharpDataType>("bit",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("real",CSharpDataType.@double),
new KeyValuePair<string, CSharpDataType>("double",CSharpDataType.@double),
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.@float),
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.Single),
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("numeric",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("year",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("datetime",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("timestamp",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("date",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("blob",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("tinyblob",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("varbinary",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("binary",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("multipoint",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("geometry",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("multilinestring",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("polygon",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("mediumblob",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.Guid),
};
public override List<string> StringThrow
{
get
{
return new List<string>() { "int32", "datetime", "decimal", "double", "byte" };
}
}
}
}

11
Src/Asp.Net/SqlSugar/Realization/PostgreSQL/DbFirst/PostgreSQLDbFirst.cs

@ -1,11 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class PostgreSQLDbFirst : DbFirstProvider
{
}
}

116
Src/Asp.Net/SqlSugar/Realization/PostgreSQL/PostgreSQLProvider.cs

@ -1,116 +0,0 @@
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class PostgreSQLProvider : AdoProvider
{
public PostgreSQLProvider() { }
public override IDbConnection Connection
{
get
{
if (base._DbConnection == null)
{
try
{
var mySqlConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
if (!mySqlConnectionString.ToLower().Contains("charset"))
{
mySqlConnectionString = mySqlConnectionString.Trim().TrimEnd(';') + ";charset=utf8;";
}
base._DbConnection = new MySqlConnection(mySqlConnectionString);
}
catch (Exception ex)
{
Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
}
}
return base._DbConnection;
}
set
{
base._DbConnection = value;
}
}
public override void BeginTran(string transactionName)
{
base.BeginTran();
}
/// <summary>
/// Only SqlServer
/// </summary>
/// <param name="iso"></param>
/// <param name="transactionName"></param>
public override void BeginTran(IsolationLevel iso, string transactionName)
{
base.BeginTran(iso);
}
public override IDataAdapter GetAdapter()
{
return new MySqlDataAdapter();
}
public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
{
MySqlCommand sqlCommand = new MySqlCommand(sql, (MySqlConnection)this.Connection);
sqlCommand.CommandType = this.CommandType;
sqlCommand.CommandTimeout = this.CommandTimeOut;
if (this.Transaction != null)
{
sqlCommand.Transaction = (MySqlTransaction)this.Transaction;
}
if (parameters.HasValue())
{
IDataParameter[] ipars = ToIDbDataParameter(parameters);
sqlCommand.Parameters.AddRange((MySqlParameter[])ipars);
}
CheckConnection();
return sqlCommand;
}
public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
{
((MySqlDataAdapter)dataAdapter).SelectCommand = (MySqlCommand)command;
}
/// <summary>
/// if mysql return MySqlParameter[] pars
/// if sqlerver return SqlParameter[] pars ...
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
{
if (parameters == null || parameters.Length == 0) return null;
MySqlParameter[] result = new MySqlParameter[parameters.Length];
int index = 0;
foreach (var parameter in parameters)
{
if (parameter.Value == null) parameter.Value = DBNull.Value;
var sqlParameter = new MySqlParameter();
sqlParameter.ParameterName = parameter.ParameterName;
sqlParameter.Size = parameter.Size;
sqlParameter.Value = parameter.Value;
sqlParameter.DbType = parameter.DbType;
sqlParameter.Direction = parameter.Direction;
if (sqlParameter.Direction == 0)
{
sqlParameter.Direction = ParameterDirection.Input;
}
result[index] = sqlParameter;
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput))
{
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
this.OutputParameters.Add(sqlParameter);
}
++index;
}
return result;
}
}
}

60
Src/Asp.Net/SqlSugar/Realization/PostgreSQL/Queryable/PostgreSQLQueryable.cs

@ -1,60 +0,0 @@
namespace SqlSugar
{
public class PostgreSQLProviderQueryable<T> : QueryableProvider<T>
{
public override ISugarQueryable<T> With(string withString)
{
return this;
}
public override ISugarQueryable<T> PartitionBy(string groupFileds)
{
this.GroupBy(groupFileds);
return this;
}
}
public class PostgreSQLQueryable<T, T2> : QueryableProvider<T, T2>
{
}
public class PostgreSQLQueryable<T, T2, T3> : QueryableProvider<T, T2, T3>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4> : QueryableProvider<T, T2, T3, T4>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5> : QueryableProvider<T, T2, T3, T4, T5>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6> : QueryableProvider<T, T2, T3, T4, T5, T6>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6, T7> : QueryableProvider<T, T2, T3, T4, T5, T6, T7>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6, T7, T8> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6, T7, T8,T9> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8,T9>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6, T7, T8,T9,T10> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8 ,T9, T10, T11>
{
}
public class PostgreSQLQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>
{
}
}

10
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -110,11 +110,6 @@
<Compile Include="Infrastructure\DependencyManagement.cs" />
<Compile Include="ExternalServiceInterface\ISerializeService.cs" />
<Compile Include="Entities\DefaultServices.cs" />
<Compile Include="Realization\PostgreSQL\CodeFirst\PostgreSQLCodeFirst.cs" />
<Compile Include="Realization\PostgreSQL\DbBind\PostgreSQLDbBind.cs" />
<Compile Include="Realization\PostgreSQL\DbFirst\PostgreSQLDbFirst.cs" />
<Compile Include="Realization\PostgreSQL\PostgreSQLProvider.cs" />
<Compile Include="Realization\PostgreSQL\Queryable\PostgreSQLQueryable.cs" />
<Compile Include="Realization\Oracle\Deleteable\OracleDeleteable.cs" />
<Compile Include="Realization\Oracle\Insertable\OracleInsertable.cs" />
<Compile Include="Realization\Oracle\Updateable\OracleUpdateable.cs" />
@ -274,10 +269,7 @@
<Content Include="References\Oracle.ManagedDataAccess.dll" />
<Content Include="References\System.Data.SQLite.dll" />
</ItemGroup>
<ItemGroup>
<Folder Include="Realization\PostgreSQL\DbMaintenance\" />
<Folder Include="Realization\PostgreSQL\SqlBuilder\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

3
Src/Asp.Net/SqlSugar/SqlSugarClient.cs

@ -38,7 +38,8 @@ namespace SqlSugar
DependencyManagement.TryOracle();
break;
case DbType.PostgreSQL:
throw new Exception("Development 50%");
DependencyManagement.TryPostgreSQL();
break;
default:
throw new Exception("ConnectionConfig.DbType is null");
}

Loading…
Cancel
Save