public override Task <bool> Read(DataSet.Key key, DataSet.Column column) { TaskCompletionSource <bool> source = new TaskCompletionSource <bool>(); source.SetResult(false); return(source.Task); }
public override async Task <bool> Select(DataSet.Define define, DataSet.Key key, DataSet.Column column) { string sql = string.Format("SELECT {0} FROM '{1}'{2};", define.ColumnsName(), define.Name, define.Where()); sql = this.BindKey(define, key, sql); SQLiteCommand command = Pool <SQLiteCommand> .Default.Acquire(); command.Connection = connect; command.CommandText = sql; for (int i = 0; i < parameters.Count; ++i) { command.Parameters.Add(parameters[i]); } parameters.Clear(); using (var reader = await command.ExecuteReaderAsync()) { if (!await reader.ReadAsync()) { Pool <SQLiteCommand> .Default.Release(command); return(false); } reader.ReadColumn(define, column); } Pool <SQLiteCommand> .Default.Release(command); return(true); }
public override async Task <bool> Read(DataSet.Key key, DataSet.Column column) { if (reader == null) { reader = await command.ExecuteReaderAsync() as SQLiteDataReader; } if (!await reader.ReadAsync()) { return(false); } reader.ReadColumn(define, column, reader.ReadKey(define, key, 0)); return(true); }
public override void Select(DataSet.Define define, DataSet.Key key) { string sql = string.Format("SELECT {0}, {1} FROM '{2}'{3};", define.KeysName(), define.ColumnsName(), define.Name, define.Where()); sql = this.BindKey(define, key, sql); SQLiteCommand command = Pool <SQLiteCommand> .Default.Acquire(); command.Connection = connect; command.CommandText = sql; for (int i = 0; i < parameters.Count; ++i) { command.Parameters.Add(parameters[i]); } parameters.Clear(); commands.Add(new KeyValuePair <SQLiteCommand, DataSet.Define>(command, define)); }
public override void Remove(DataSet.Define define, DataSet.Key key) { string sql = string.Format("DELETE FROM '{0}'{1};", define.Name, define.Where()); sql = this.BindKey(define, key, sql); SQLiteCommand command = Pool <SQLiteCommand> .Default.Acquire(); command.Connection = connect; command.CommandText = sql; for (int i = 0; i < parameters.Count; ++i) { command.Parameters.Add(parameters[i]); } parameters.Clear(); commands.Add(command); }
public override void Write(DataSet.Define define, DataSet.Key key, DataSet.Column column) { Remove(define, key); string sql = string.Format("INSERT INTO '{0}' ({1}, {2}) VALUES ({3}, {4});", define.Name, define.KeysName(), define.ColumnsName(), define.KeysValue(), define.ColumnsValue()); sql = this.BindKeyColumn(define, key, column, sql); SQLiteCommand command = Pool <SQLiteCommand> .Default.Acquire(); command.Connection = connect; command.CommandText = sql; for (int i = 0; i < parameters.Count; ++i) { command.Parameters.Add(parameters[i]); } parameters.Clear(); commands.Add(command); }
public static string BindKey(this Command command, DataSet.Define define, DataSet.Key key, string sql) { KeyReplacement replacement = Pool <KeyReplacement> .Default.Acquire(); replacement.define = define; replacement.key = key; string result = KeyPattern.Replace(sql, replacement.Execute); replacement.define = null; replacement.key = null; Pool <KeyReplacement> .Default.Release(replacement); var keys = define.Keys; for (int i = 0; i < keys.Count; ++i) { var type = keys[i]; if (type == DataSet.Type.String) { string str = null; if (!key.Read(i, ref str)) { throw new DataSet.TypeMismatchException(); } str = str ?? ""; command.AddParam(str); } else if (type == DataSet.Type.Blob) { byte[] bytes = null; if (!key.Read(i, ref bytes)) { throw new DataSet.TypeMismatchException(); } command.AddParam(bytes); } } return(result); }
public override async Task <bool> Read(DataSet.Key key, DataSet.Column column) { if (reader == null) { reader = await commands[index].Key.ExecuteReaderAsync() as SQLiteDataReader; } while (true) { if (await reader.ReadAsync()) { break; } ++index; if (index >= commands.Count) { return(false); } reader.Close(); reader = await commands[index].Key.ExecuteReaderAsync() as SQLiteDataReader; } DataSet.Define define = commands[index].Value; reader.ReadColumn(define, column, reader.ReadKey(define, key, 0)); return(true); }
public static int ReadKey(this DbDataReader reader, DataSet.Define define, DataSet.Key key, int start = 0) { int index = start; for (int i = 0; i < define.Keys.Count; ++i) { switch (define.Keys[i]) { case DataSet.Type.Bool: { int result = reader.GetInt32(index); if (!key.Write(i, result != 0)) { throw new DataSet.TypeMismatchException(); } } break; case DataSet.Type.Integer: { int result = reader.GetInt32(index); if (!key.Write(i, result)) { throw new DataSet.TypeMismatchException(); } } break; case DataSet.Type.Long: { long result = reader.GetInt64(index); if (!key.Write(i, result)) { throw new DataSet.TypeMismatchException(); } } break; case DataSet.Type.Double: { double result = reader.GetDouble(index); if (!key.Write(i, result)) { throw new DataSet.TypeMismatchException(); } } break; case DataSet.Type.String: { ++index; string result = reader.GetString(index); if (!key.Write(i, result ?? "")) { throw new DataSet.TypeMismatchException(); } } break; case DataSet.Type.Blob: { ++index; using (Stream stream = reader.GetStream(index)) { int length = (int)stream.Length; byte[] result = new byte[length]; stream.Read(result, 0, length); if (!key.Write(i, result)) { throw new DataSet.TypeMismatchException(); } } } break; } ++index; } return(index); }