📄 sqliteconnection.cs
字号:
string baseDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
if (string.IsNullOrEmpty(baseDirectory))
{
baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
}
if ((sourceFile.Length > "|DataDirectory|".Length) && ((sourceFile["|DataDirectory|".Length] == Path.DirectorySeparatorChar) || (sourceFile["|DataDirectory|".Length] == Path.AltDirectorySeparatorChar)))
{
sourceFile = sourceFile.Remove("|DataDirectory|".Length, 1);
}
sourceFile = Path.Combine(baseDirectory, sourceFile.Substring("|DataDirectory|".Length));
}
return sourceFile;
}
internal static string FindKey(KeyValuePair<string, string>[] opts, string key, string defValue)
{
int length = opts.Length;
for (int i = 0; i < length; i++)
{
if (string.Compare(opts[i].get_Key(), key, true, CultureInfo.InvariantCulture) == 0)
{
return opts[i].get_Value();
}
}
return defValue;
}
internal void OnStateChange(ConnectionState newState)
{
ConnectionState originalState = this._connectionState;
this._connectionState = newState;
if ((this.StateChange != null) && (originalState != newState))
{
StateChangeEventArgs e = new StateChangeEventArgs(originalState, newState);
this.StateChange(this, e);
}
}
public void Open()
{
if (this._connectionState != ConnectionState.Closed)
{
throw new InvalidOperationException();
}
this.Close();
KeyValuePair<string, string>[] opts = this.ParseConnectionString();
if (Convert.ToInt32(FindKey(opts, "Version", "3"), CultureInfo.InvariantCulture) != 3)
{
throw new NotSupportedException("Only SQLite Version 3 is supported at this time");
}
string strA = FindKey(opts, "Data Source", "");
if (string.IsNullOrEmpty(strA))
{
throw new ArgumentException("Data Source cannot be empty. Use :memory: to open an in-memory database");
}
if (string.Compare(strA, ":MEMORY:", true, CultureInfo.InvariantCulture) == 0)
{
strA = ":memory:";
}
try
{
bool flag = Convert.ToBoolean(FindKey(opts, "UseUTF16Encoding", "False"), CultureInfo.InvariantCulture);
SQLiteDateFormats fmt = (string.Compare(FindKey(opts, "DateTimeFormat", "ISO8601"), "ticks", true, CultureInfo.InvariantCulture) == 0) ? SQLiteDateFormats.Ticks : SQLiteDateFormats.ISO8601;
if (flag)
{
this._sql = new SQLite3_UTF16(fmt);
}
else
{
this._sql = new SQLite3(fmt);
}
strA = this.ExpandFileName(strA);
try
{
if (!File.Exists(strA))
{
throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, "Unable to locate file \"{0}\", creating new database.", new object[] { strA }));
}
}
catch
{
}
this._sql.Open(strA);
this._binaryGuid = Convert.ToBoolean(FindKey(opts, "BinaryGUID", "True"), CultureInfo.InvariantCulture);
string s = FindKey(opts, "Password", null);
if (!string.IsNullOrEmpty(s))
{
this._sql.SetPassword(Encoding.UTF8.GetBytes(s));
}
else if (this._password != null)
{
this._sql.SetPassword(this._password);
}
this._password = null;
this._dataSource = Path.GetFileNameWithoutExtension(strA);
this.OnStateChange(ConnectionState.Open);
this._version++;
using (SQLiteCommand command = this.CreateCommand())
{
string text3 = FindKey(opts, "Synchronous", "Normal");
if (string.Compare(text3, "Normal", true, CultureInfo.InvariantCulture) != 0)
{
command.CommandText = string.Format(CultureInfo.InvariantCulture, "PRAGMA Synchronous={0}", new object[] { text3 });
command.ExecuteNonQuery();
}
text3 = FindKey(opts, "Cache Size", "2000");
if (Convert.ToInt32(text3) != 0x7d0)
{
command.CommandText = string.Format(CultureInfo.InvariantCulture, "PRAGMA Cache_Size={0}", new object[] { text3 });
command.ExecuteNonQuery();
}
if (strA != ":memory:")
{
text3 = FindKey(opts, "Page Size", "1024");
if (Convert.ToInt32(text3) != 0x400)
{
command.CommandText = string.Format(CultureInfo.InvariantCulture, "PRAGMA Page_Size={0}", new object[] { text3 });
command.ExecuteNonQuery();
}
}
}
}
catch (SQLiteException)
{
this.OnStateChange(ConnectionState.Broken);
throw;
}
}
internal KeyValuePair<string, string>[] ParseConnectionString()
{
string source = this._connectionString;
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
string[] textArray = SQLiteConvert.Split(source, ';');
int length = textArray.Length;
for (int i = 0; i < length; i++)
{
string[] textArray2 = SQLiteConvert.Split(textArray[i], '=');
if (textArray2.Length != 2)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Invalid ConnectionString format for parameter \"{0}\"", new object[] { (textArray2.Length > 0) ? textArray2[0] : "null" }));
}
list.Add(new KeyValuePair<string, string>(textArray2[0], textArray2[1]));
}
KeyValuePair<string, string>[] pairArray = new KeyValuePair<string, string>[list.get_Count()];
list.CopyTo(pairArray, 0);
return pairArray;
}
internal void RemoveCommand(SQLiteCommand cmd)
{
lock (this._commandList)
{
List<WeakReference>.Enumerator enumerator = this._commandList.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
WeakReference reference = enumerator.get_Current();
try
{
if ((reference.Target as SQLiteCommand) != cmd)
{
continue;
}
this._commandList.Remove(reference);
goto Label_0067;
}
catch
{
continue;
}
}
}
finally
{
enumerator.Dispose();
}
Label_0067:;
}
}
private void RollbackCallback()
{
this._rollbackHandler(this, EventArgs.Empty);
}
public void SetPassword(byte[] databasePassword)
{
if (this._connectionState != ConnectionState.Closed)
{
throw new InvalidOperationException("Password can only be set before the database is opened.");
}
if ((databasePassword != null) && (databasePassword.Length == 0))
{
databasePassword = null;
}
this._password = databasePassword;
}
public void SetPassword(string databasePassword)
{
this.SetPassword(string.IsNullOrEmpty(databasePassword) ? null : Encoding.UTF8.GetBytes(databasePassword));
}
private void UpdateCallback(int type, IntPtr database, int databaseLen, IntPtr table, int tableLen, long rowid)
{
this._updateHandler(this, new UpdateEventArgs(SQLiteConvert.UTF8ToString(database, databaseLen), SQLiteConvert.UTF8ToString(table, tableLen), (UpdateEventType) type, rowid));
}
public string ConnectionString
{
get
{
return this._connectionString;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
if (this._connectionState != ConnectionState.Closed)
{
throw new InvalidOperationException();
}
this._connectionString = value;
}
}
public string Database
{
get
{
return "main";
}
}
public string DataSource
{
get
{
return this._dataSource;
}
}
public string ServerVersion
{
get
{
if (this._connectionState != ConnectionState.Open)
{
throw new InvalidOperationException();
}
return this._sql.Version;
}
}
public ConnectionState State
{
get
{
return this._connectionState;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -