Вот как я это делаю,
INSERT INTO AUDITEVENT (CODE, DESCRIPTION, AUDITEVENTPK)
SELECT 'value1', 'value2', 'value3'
WHERE NOT EXISTS (SELECT 1 FROM AUDITEVENT WHERE CODE = 'value1')
В моем IDConneciton у меня есть метод ProcessCommandText для обновления запроса с синтаксиса SQL Server только на SQLite.
Это реализация этого метода
public string ProcessSqlText(string text)
{
var result = text.Replace("SET ANSI_NULLS ON", "").
Replace("SET ANSI_NULLS ON", "").
Replace(" ASC)", ")").
Replace("TOP 100", "TOP(100)").
Replace("TOP 1", "TOP(1)").
Replace(" VARCHAR", " NVARCHAR").
Replace(" CHAR", " NCHAR").
Replace(" TEXT", " NTEXT").
Replace("WITH ( IGNORE_DUP_KEY = OFF)", "").
Replace("SET QUOTED_IDENTIFIER ON", "").
Replace("SET ANSI_PADDING ON", "").
Replace("SET ANSI_PADDING OFF", "").
Replace("SET ANSI_WARNINGS ON", "").Trim(' ').
Replace("WITH NOCHECK", "").
Replace("(nolock)", "").
Replace("CREATE CLUSTERED INDEX", "CREATE NONCLUSTERED INDEX").
Replace("CREATE UNIQUE CLUSTERED INDEX", "CREATE UNIQUE NONCLUSTERED INDEX").
Replace("[dbo].", "").
ToUpper().
Replace("NEWID()", "'" + Guid.NewGuid().ToString() + "'"). // NEWID() is not supported
Replace("GO", ""); // GO is not supported
if (result.Contains("TOP(100)"))
{
result = result.Replace("TOP(100)", "");
result += " LIMIT 100";
}
if (result.Contains("TOP(1)"))
{
result = result.Replace("TOP(1)", "");
result += " LIMIT 1";
}
if (result.Contains("DATEPART"))
{
result = result.Replace("DATEPART", "strftime");
result = result.Replace("minute", "'%M'");
result = result.Replace("day", "'%d'");
result = result.Replace("month", "'%m'");
result = result.Replace("year", "'%Y'");
}
result = TransformIfNotExistsQueriesToSqlLite(result);
return result;
}
private string TransformIfNotExistsQueriesToSqlLite(string query)
{
var ifNotExistsRegEx = @"(IF NOT EXISTS\s+)\(+.+\)+\r+\n+INSERT+";
var m = Regex.Match(query, @"(IF NOT EXISTS\s+)\(+.+\)+\r+\n+INSERT+", RegexOptions.IgnoreCase);
if (m.Groups.Count > 0 && m.Groups[0].Value.Contains("IF NOT EXISTS"))
{
var conditionalStatement = m.Groups[0].Value;
var newQuery = query.Replace(conditionalStatement, " INSERT ");
conditionalStatement = conditionalStatement.ToUpper().Replace("IF", "WHERE").Replace("INSERT", "");
newQuery = Regex.Replace(newQuery.ToUpper(), @"VALUES\s+\(+", " SELECT ");
var lastIndexOfClosingBracket = newQuery.LastIndexOf(')');
newQuery = newQuery.Substring(0, lastIndexOfClosingBracket);
return newQuery + " " + conditionalStatement;
}
return query;
}