mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
pgsql DbFirst 完成未测试;oracle 适配进行中
This commit is contained in:
54
FreeSql/Extensions/FreeSqlGlobalExtensions.cs
Normal file
54
FreeSql/Extensions/FreeSqlGlobalExtensions.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using NpgsqlTypes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Reflection;
|
||||
|
||||
public static class FreeSqlGlobalExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// 测量两个经纬度的距离,返回单位:米
|
||||
/// </summary>
|
||||
/// <param name="that">经纬坐标1</param>
|
||||
/// <param name="point">经纬坐标2</param>
|
||||
/// <returns>返回距离(单位:米)</returns>
|
||||
public static double Distance(this Point that, Point point) {
|
||||
double radLat1 = (double)(that.Y) * Math.PI / 180d;
|
||||
double radLng1 = (double)(that.X) * Math.PI / 180d;
|
||||
double radLat2 = (double)(point.Y) * Math.PI / 180d;
|
||||
double radLng2 = (double)(point.X) * Math.PI / 180d;
|
||||
return 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin((radLat1 - radLat2) / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin((radLng1 - radLng2) / 2), 2))) * 6378137;
|
||||
}
|
||||
|
||||
public static object GetEnum<T>(this IDataReader dr, int index) {
|
||||
string value = dr.GetString(index);
|
||||
Type t = typeof(T);
|
||||
foreach (var f in t.GetFields())
|
||||
if (f.GetCustomAttribute<DescriptionAttribute>()?.Description == value || f.Name == value) return Enum.Parse(t, f.Name, true);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string ToDescriptionOrString(this Enum item) {
|
||||
string name = item.ToString();
|
||||
var desc = item.GetType().GetField(name)?.GetCustomAttribute<DescriptionAttribute>();
|
||||
return desc?.Description ?? name;
|
||||
}
|
||||
public static long ToInt64(this Enum item) {
|
||||
return Convert.ToInt64(item);
|
||||
}
|
||||
public static IEnumerable<T> ToSet<T>(this long value) {
|
||||
List<T> ret = new List<T>();
|
||||
if (value == 0) return ret;
|
||||
Type t = typeof(T);
|
||||
foreach (FieldInfo f in t.GetFields()) {
|
||||
if (f.FieldType != t) continue;
|
||||
object o = Enum.Parse(t, f.Name, true);
|
||||
long v = (long)o;
|
||||
if ((value & v) == v) ret.Add((T)o);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
public static class StringExtensions {
|
||||
public static class FreeSqlStringExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// 特殊处理类似 string.Format 的使用方法,防止注入,以及 IS NULL 转换
|
||||
@ -24,6 +24,14 @@
|
||||
/// <returns></returns>
|
||||
public static string FormatPostgreSQL(this string that, params object[] args) => _postgresqlAdo.Addslashes(that, args);
|
||||
static FreeSql.PostgreSQL.PostgreSQLAdo _postgresqlAdo = new FreeSql.PostgreSQL.PostgreSQLAdo();
|
||||
/// <summary>
|
||||
/// 特殊处理类似 string.Format 的使用方法,防止注入,以及 IS NULL 转换
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static string FormatOracleSQL(this string that, params object[] args) => _oracleAdo.Addslashes(that, args);
|
||||
static FreeSql.Oracle.OracleAdo _oracleAdo = new FreeSql.Oracle.OracleAdo();
|
||||
}
|
||||
|
||||
namespace System.Runtime.CompilerServices {
|
@ -1,39 +0,0 @@
|
||||
using NpgsqlTypes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace NpgsqlTypes {
|
||||
public static class FreeSqlExtensions {
|
||||
|
||||
public static string To1010(this BitArray ba) {
|
||||
char[] ret = new char[ba.Length];
|
||||
for (int a = 0; a < ba.Length; a++) ret[a] = ba[a] ? '1' : '0';
|
||||
return new string(ret);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 1010101010 这样的二进制字符串转换成 BitArray
|
||||
/// </summary>
|
||||
/// <param name="_1010">1010101010</param>
|
||||
/// <returns></returns>
|
||||
public static BitArray ToBitArray(this string _1010Str) {
|
||||
if (_1010Str == null) return null;
|
||||
BitArray ret = new BitArray(_1010Str.Length);
|
||||
for (int a = 0; a < _1010Str.Length; a++) ret[a] = _1010Str[a] == '1';
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static NpgsqlRange<T> ToNpgsqlRange<T>(this string that) {
|
||||
var s = that;
|
||||
if (string.IsNullOrEmpty(s) || s == "empty") return NpgsqlRange<T>.Empty;
|
||||
string s1 = s.Trim('(', ')', '[', ']');
|
||||
string[] ss = s1.Split(new char[] { ',' }, 2);
|
||||
if (ss.Length != 2) return NpgsqlRange<T>.Empty;
|
||||
T t1 = default(T);
|
||||
T t2 = default(T);
|
||||
if (!string.IsNullOrEmpty(ss[0])) t1 = (T)Convert.ChangeType(ss[0], typeof(T));
|
||||
if (!string.IsNullOrEmpty(ss[1])) t2 = (T)Convert.ChangeType(ss[1], typeof(T));
|
||||
return new NpgsqlRange<T>(t1, s[0] == '[', s[0] == '(', t2, s[s.Length - 1] == ']', s[s.Length - 1] == ')');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user