mirror of
https://git.oceanpay.cc/danial/kami_itunes_june.git
synced 2025-12-18 22:31:24 +00:00
249 lines
6.7 KiB
C#
249 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SQLite;
|
|
using System.Data.Common;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace AppleBatch_June
|
|
{
|
|
internal class SqliteHelper
|
|
{
|
|
private static string str;
|
|
|
|
public static readonly object insertLock;
|
|
|
|
public static DbProviderFactory factory { get; set; }
|
|
|
|
private static void InstanceFactory()
|
|
{
|
|
try
|
|
{
|
|
factory = SQLiteFactory.Instance;
|
|
using DbConnection dbConnection = factory.CreateConnection();
|
|
dbConnection.ConnectionString = str;
|
|
using DbCommand dbCommand = dbConnection.CreateCommand();
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = "SELECT name FROM sqlite_master WHERE type='index' AND name='va'";
|
|
if (dbCommand.ExecuteScalar() == null)
|
|
{
|
|
dbCommand.CommandText = "CREATE INDEX 'va' ON 'itunes_db' ('type' ASC, 'value16' ASC)";
|
|
dbCommand.ExecuteNonQuery();
|
|
}
|
|
dbConnection.Close();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
factory = null;
|
|
}
|
|
}
|
|
|
|
static SqliteHelper()
|
|
{
|
|
str = "Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\AppleBatch\\AppleDB.db";
|
|
insertLock = new object();
|
|
InstanceFactory();
|
|
}
|
|
|
|
public static int ExecuteNonQuery(string sql, params DbParameter[] param)
|
|
{
|
|
if (factory != null)
|
|
{
|
|
using (DbConnection dbConnection = factory.CreateConnection())
|
|
{
|
|
dbConnection.ConnectionString = str;
|
|
using DbCommand dbCommand = dbConnection.CreateCommand();
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = sql;
|
|
if (param != null)
|
|
{
|
|
dbCommand.Parameters.AddRange(param);
|
|
}
|
|
return dbCommand.ExecuteNonQuery();
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public static object ExecuteScalar(string sql, params DbParameter[] param)
|
|
{
|
|
if (factory == null)
|
|
{
|
|
throw new Exception("无法加载 System.Data.SQLite.dll,请重新解压安装包");
|
|
}
|
|
if (factory != null)
|
|
{
|
|
using (DbConnection dbConnection = factory.CreateConnection())
|
|
{
|
|
dbConnection.ConnectionString = str;
|
|
using DbCommand dbCommand = dbConnection.CreateCommand();
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = sql;
|
|
if (param != null)
|
|
{
|
|
dbCommand.Parameters.AddRange(param);
|
|
}
|
|
return dbCommand.ExecuteScalar();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static DataTable ExecuteTable(string sql, params DbParameter[] param)
|
|
{
|
|
if (factory == null)
|
|
{
|
|
throw new Exception("无法加载 System.Data.SQLite.dll,请重新解压安装包");
|
|
}
|
|
DataTable dataTable = new DataTable();
|
|
if (factory != null)
|
|
{
|
|
using (DbConnection dbConnection = factory.CreateConnection())
|
|
{
|
|
dbConnection.ConnectionString = str;
|
|
using DbCommand dbCommand = dbConnection.CreateCommand();
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = sql;
|
|
if (param != null)
|
|
{
|
|
dbCommand.Parameters.AddRange(param);
|
|
}
|
|
DbDataAdapter dbDataAdapter = factory.CreateDataAdapter();
|
|
dbDataAdapter.SelectCommand = dbCommand;
|
|
dbDataAdapter.Fill(dataTable);
|
|
return dataTable;
|
|
}
|
|
}
|
|
return dataTable;
|
|
}
|
|
|
|
public static DataTable QueryTable(string tbName, ref string total, string fields = "*", string where = "1", string orderBy = "", string limit = "", params DbParameter[] param)
|
|
{
|
|
if (factory == null)
|
|
{
|
|
throw new Exception("无法加载 System.Data.SQLite.dll,请重新解压安装包");
|
|
}
|
|
if (orderBy != "")
|
|
{
|
|
orderBy = "ORDER BY " + orderBy;
|
|
}
|
|
string sql = string.Format("SELECT count(1) FROM `{1}` WHERE {2} {3}", fields, tbName, where, orderBy);
|
|
total = ExecuteTable(sql, param).Rows[0][0].ToString();
|
|
if (limit != "")
|
|
{
|
|
limit = "LIMIT " + limit;
|
|
}
|
|
return ExecuteTable($"SELECT {fields} FROM `{tbName}` WHERE {where} {orderBy} {limit}", param);
|
|
}
|
|
|
|
public static void ExecuteInsert(string tbName, Dictionary<string, string> insertData)
|
|
{
|
|
if (factory == null)
|
|
{
|
|
throw new Exception("无法加载 System.Data.SQLite.dll,请重新解压安装包");
|
|
}
|
|
Task.Run(delegate
|
|
{
|
|
string arg = "";
|
|
string text = "";
|
|
string text2 = "";
|
|
insertData.Add("value16", Tools.GetMD5_32(HttpUtility.UrlEncode(AppSysConfig.userName) + "aaa").Substring(10));
|
|
string text3 = "";
|
|
int num = 0;
|
|
string guid = AppSysConfig.guid;
|
|
foreach (char c in guid)
|
|
{
|
|
if (num % 2 != 0)
|
|
{
|
|
text3 += c;
|
|
}
|
|
num++;
|
|
}
|
|
text3 = text3.PadRight(16, 'x');
|
|
List<DbParameter> list = new List<DbParameter>();
|
|
bool flag = true;
|
|
foreach (string key in insertData.Keys)
|
|
{
|
|
text += $"{arg} `{key}`";
|
|
text2 += $"{arg} @{key}";
|
|
if (key != "type" && key != "addTime" && key != "value16")
|
|
{
|
|
if (flag)
|
|
{
|
|
list.Add(CreateParameter("@" + key, "key4_" + DESEncrypt.Encrypt(insertData[key], Tools.GetMD5_32(key + HttpUtility.UrlEncode(text3)).Substring(16), "WBiASAWaPcAaLsLa")));
|
|
}
|
|
else
|
|
{
|
|
list.Add(CreateParameter("@" + key, insertData[key]));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (key == "type" && (insertData[key] == "RealNameId" || insertData[key] == "guidManage"))
|
|
{
|
|
flag = false;
|
|
}
|
|
list.Add(CreateParameter("@" + key, insertData[key]));
|
|
}
|
|
arg = ",";
|
|
}
|
|
string sql = $"INSERT INTO `{tbName}`({text}) VALUES({text2})";
|
|
lock (insertLock)
|
|
{
|
|
ExecuteNonQuery(sql, list.ToArray());
|
|
}
|
|
}).ContinueWith(delegate(Task t)
|
|
{
|
|
foreach (Exception innerException in t.Exception.Flatten().InnerExceptions)
|
|
{
|
|
APIUtlis.ApiSeriveError(innerException, "ExecuteInsert");
|
|
}
|
|
}, TaskContinuationOptions.OnlyOnFaulted);
|
|
}
|
|
|
|
public static int ExecuteUpdate(string tbName, Dictionary<string, string> whereData, Dictionary<string, string> insertData)
|
|
{
|
|
string arg = "";
|
|
string text = "";
|
|
List<DbParameter> list = new List<DbParameter>();
|
|
string text2 = "";
|
|
int num = 0;
|
|
string guid = AppSysConfig.guid;
|
|
foreach (char c in guid)
|
|
{
|
|
if (num % 2 != 0)
|
|
{
|
|
text2 += c;
|
|
}
|
|
num++;
|
|
}
|
|
text2 = text2.PadRight(16, 'x');
|
|
foreach (string key in insertData.Keys)
|
|
{
|
|
text += $"{arg} {key}=@{key}";
|
|
list.Add(CreateParameter("@" + key, insertData[key]));
|
|
arg = ",";
|
|
}
|
|
string text3 = "";
|
|
arg = "";
|
|
foreach (string key2 in whereData.Keys)
|
|
{
|
|
text3 += $"{arg} {key2}=@{key2}";
|
|
list.Add(CreateParameter("@" + key2, whereData[key2]));
|
|
arg = "and";
|
|
}
|
|
return ExecuteNonQuery($"UPDATE `{tbName}` SET {text} WHERE {text3}", list.ToArray());
|
|
}
|
|
|
|
private static DbParameter CreateParameter(string ParameterName, string Value)
|
|
{
|
|
DbParameter dbParameter = factory.CreateParameter();
|
|
dbParameter.ParameterName = ParameterName;
|
|
dbParameter.Value = Value;
|
|
return dbParameter;
|
|
}
|
|
}
|
|
}
|