Comments (25)
貌似可以通过使用动态表达式的写法,还有其他更好的方法吗?
Expression<Func<MediaMst, OOSKey, MediaKey>> selectExp;
if (_oSSUseCrypt)
{
selectExp = (m, o) => new MediaKey()
{
AccessKey = DecryptAccessKey(o.AccessKey),
SecretKey = DecryptSecretKey(o.SecretKey)
};
}
else
{
selectExp = (m, o) => new MediaKey()
{
AccessKey = o.AccessKey,
SecretKey = o.SecretKey
};
}
var mediaKeys = _dbClient.Queryable<MediaMst>().InnerJoin<OOSKey>((m, o) => m.MediaUID == o.MediaUID)
.Where((m, o) => o.IsPrimary && o.Status == 1)
.Select(selectExp, true)
.ToList();
from sqlsugar.
SELECT中不支持自定义方法吧
DecryptAccessKey(o.AccessKey)
from sqlsugar.
正确应该这样写
from sqlsugar.
var mediaKeys = _dbClient.Queryable<MediaMst>().InnerJoin<OOSKey>((m, o) => m.MediaUID == o.MediaUID)
.Where((m, o) => o.IsPrimary && o.Status == 1)
.Select((m, o) => new MediaKey
{
AccessKey = o.AccessKey,
SecretKey = o.SecretKey,
}, true)
.Mappper(it=>{
it.AccessKey = _oSSUseCrypt ? it.AccessKey :DecryptAccessKey( it.AccessKey),
it.SecretKey = _oSSUseCrypt ? it.AccessKey: DecryptSecretKey( it.SecretKey)
})
.ToList();
from sqlsugar.
SELECT中不支持自定义方法吧 DecryptAccessKey(o.AccessKey)
啊不是支持的吗,可以使用
from sqlsugar.
var mediaKeys = _dbClient.Queryable<MediaMst>().InnerJoin<OOSKey>((m, o) => m.MediaUID == o.MediaUID) .Where((m, o) => o.IsPrimary && o.Status == 1) .Select((m, o) => new MediaKey { AccessKey = o.AccessKey, SecretKey = o.SecretKey, }, true) .Mappper(it=>{ it.AccessKey = _oSSUseCrypt ? it.AccessKey :DecryptAccessKey( it.AccessKey), it.SecretKey = _oSSUseCrypt ? it.AccessKey: DecryptSecretKey( it.SecretKey) }) .ToList();
这个是调用C#的方法吧?我需要调用原生SQL方法
var expMethods = new List<SqlFuncExternal>
{
new SqlFuncExternal()
{
UniqueMethodName = "DecryptAccessKey",
MethodValue = (expInfo, dbType, expContext) =>
{
if (dbType == DbType.MySql)
{
return string.Format("CAST(AES_DECRYPT({0},'123') AS CHAR(100))", expInfo.Args[0].MemberName);
}
else if (dbType == DbType.SqlServer)
{
return string.Format("Convert(nvarchar(255), DecryptByPassPhrase('0574', {0}, 32423, 'xxx'))", expInfo.Args[0].MemberName);
}
else
throw new Exception("未实现");
}
},
new SqlFuncExternal()
{
UniqueMethodName = "DecryptSecretKey",
MethodValue = (expInfo, dbType, expContext) =>
{
if (dbType == DbType.MySql)
{
return string.Format("CAST(AES_DECRYPT({0},'123') AS CHAR(100))", expInfo.Args[0].MemberName);
}
else if (dbType == DbType.SqlServer)
{
return string.Format("Convert(nvarchar(255), DecryptByPassPhrase('365', {0}, 2342, 'xxx'))", expInfo.Args[0].MemberName);
}
else
throw new Exception("未实现");
}
}
};
_dbClient = new SqlSugarScope(new ConnectionConfig()
{
DbType = config.DbType,
ConnectionString = config.ConnectionString,
IsAutoCloseConnection = true,
ConfigureExternalServices = new ConfigureExternalServices()
{
SqlFuncServices = expMethods//set ext method
}
}
from sqlsugar.
.Select((m, o) => new MediaKey
{
AccessKey = _oSSUseCrypt==true ? DecryptAccessKey(o.AccessKey) : o.AccessKey,
SecretKey = _oSSUseCrypt==true ? DecryptSecretKey(o.SecretKey) : o.SecretKey,
}, true)
如果扩展方法那就直接这样用_oSSUseCrypt加上==true
from sqlsugar.
.Select((m, o) => new MediaKey { AccessKey = _oSSUseCrypt==true ? DecryptAccessKey(o.AccessKey) : o.AccessKey, SecretKey = _oSSUseCrypt==true ? DecryptSecretKey(o.SecretKey) : o.SecretKey, }, true)如果扩展方法那就直接这样用_oSSUseCrypt加上==true
这样会生成CASE When的语句,执行有BUG,即使判断条件是true但是也没有走那个判定结果,很奇怪
from sqlsugar.
AOP看生成的SQL
from sqlsugar.
能有case when就对了
from sqlsugar.
能有case when就对了
不想在数据库里做判断,想判断完了再去查询数据库
from sqlsugar.
select中又不是where 在数据库里加case when问题不大
from sqlsugar.
你不想加就if else表达式吧,暂时没有好办法
如果你想方便就扔select里面
还有更好的方案 你发新的issue这个先关闭。
from sqlsugar.
select中又不是where 在数据库里加case when问题不大
主要是生成的语句执行有BUG
from sqlsugar.
生成的问题你发出来,你不发出来我怎么知道哪有BUG
from sqlsugar.
我这边测试都正常的
from sqlsugar.
稍等
from sqlsugar.
代码+AOP打印的SQL
from sqlsugar.
代码:
var mediaKeys = _dbClient.Queryable<MediaMst>().InnerJoin<OOSKey>((m, o) => m.MediaUID == o.MediaUID)
.Where((m, o) => o.IsPrimary && o.Status == 1)
.Select((m, o) => new MediaKey()
{
AccessKey = _oSSUseCrypt == true ? DecryptAccessKey(o.AccessKey) : o.AccessKey,
SecretKey = _oSSUseCrypt == true ? DecryptSecretKey(o.SecretKey) : o.SecretKey,
}, true)
.ToList();
生成的SQL:
SELECT ( CASE WHEN ( 1 = 1 ) THEN CAST(AES_DECRYPT(`o`.`AccessKey`,'T0mtaw@0574') AS CHAR(100)) ELSE `o`.`AccessKey` END ) AS `AccessKey` , ( CASE WHEN ( 1 = 1 ) THEN CAST(AES_DECRYPT(`o`.`SecretKey`,'Tomt@w365') AS CHAR(100)) ELSE `o`.`SecretKey` END ) AS `SecretKey` ,`m`.`MediaUID` AS `MediaUID` ,`m`.`MediaName` AS `MediaName` ,`m`.`MediaHost` AS `MediaHost` ,`m`.`Path` AS `Path` ,`m`.`PathType` AS `MediaType` ,`o`.`VisitIP` AS `VisitIP` FROM `MediaMst` `m` Inner JOIN `OOSKey` `o` ON ( `m`.`MediaUID` = `o`.`MediaUID` ) WHERE ( ( `o`.`IsPrimary`=1 ) AND( `o`.`Status` = 1 ))
from sqlsugar.
(
CASE WHEN ( 1 = 1 )
THEN CAST(AES_DECRYPT(o
.AccessKey
,'T0mtaw@0574') AS CHAR(100))
ELSE o
.AccessKey
END ) AS AccessKey
这个SQL没错吧1=1是true走了加密逻辑
from sqlsugar.
_oSSUseCrypt == true ? DecryptAccessKey(o.AccessKey) : o.AccessKey.ToString()
我知道了可能是casewhen类型不同,第二个参数加个.ToString()
from sqlsugar.
把前casewhen2边都是字符串类型
from sqlsugar.
SQL我看了是没错的,可能你需要小调整一下就行了比加TOSTRING等
from sqlsugar.
SQL我看了是没错的,可能你需要小调整一下就行了比加TOSTRING等
好的原来是这样,我试试
from sqlsugar.
SQL我看了是没错的,可能你需要小调整一下就行了比加TOSTRING等
可以了,谢谢,这个还真的不容易知道原因
from sqlsugar.
Related Issues (20)
- 描误信息: intemal Error 堆钱信息: at 0raclelntemal.TTC.TTCExecuteSal.ReceiveExecuteResponse HOT 4
- 大佬 MAUI Release第一次 访问数据库会抛出这个错误。(Debug没有这个错误)
- Updateable一起使用 WhereColumns和UpdateColumns好像有错误 HOT 3
- IsAutoRemoveDataCache相關問題 HOT 4
- How to retrieve data from a SplitTable and join it with an SQL function [dbo].fn_ListDayOfMonth HOT 3
- Exception in {"Index was outside the bounds of the array."} When use Join three table HOT 5
- Is there any way to optimize this command? HOT 10
- Illegal mix of collations HOT 3
- 如何在 SqlSugar 中集成 CAP HOT 3
- Oracle中INTERVAL DAY TO SECOND数据类型映射TimeSpan, OracleDbBind中MappingTypesConst中应为timespan HOT 3
- If we use IList<T> for Storageable/Insertable/Updatable, sometime may face Newtonsoft.Json.JsonSerializationException HOT 2
- 多个DB的问题 HOT 3
- sqlserver数据查询的问题 HOT 2
- WhereClassByPrimaryKey參數前後綴過長 HOT 2
- InsertOrUpdate()函数插入更新遇到的问题 HOT 2
- Operand type clash: time is incompatible with float HOT 1
- Insert Other Flied DateTime.Now Bug Insert => '2024-01-1613:00:00.000' HOT 9
- The most industrious open-source project globally, resolving issues at the fastest pace.
- 查询和增删改查方面的改进 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlsugar.