protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) { if (ReaderMethodCache.TryGetValue(CommandText, out var cached) == false) { var lastDotIndex = CommandText.LastIndexOf('.'); var typeString = CommandText.Substring(0, lastDotIndex); var type = Assembly.GetExecutingAssembly().GetType(typeString); var methodString = CommandText.Substring(lastDotIndex + 1); var methodInfo = type.GetMethod(methodString); var returnType = methodInfo.ReturnType; var enumeratorMethodInfo = returnType.GetMethod("GetEnumerator"); var rowType = returnType.GetGenericArguments()[0]; if (IsClrType(Nullable.GetUnderlyingType(rowType) ?? rowType)) { cached = Tuple.Create(methodInfo, enumeratorMethodInfo, rowType, (PropertyInfo[])null); } else { cached = Tuple.Create(methodInfo, enumeratorMethodInfo, (Type)null, rowType.GetProperties()); } ReaderMethodCache.Add(CommandText, cached); } var enumerable = cached.Item1.Invoke(null, null); var enumerator = (IEnumerator)cached.Item2.Invoke(enumerable, null); return(new FakeDataReader(cached.Item3, cached.Item4, enumerator)); }
/// <summary> /// EXAMPLE QUERY /// CREATE INDEX idx_lastname /// ON Persons(LastName); /// </summary> protected override void ParseQuery() { // find index of open bracket in create query, where are descriptions of columns var openBacket = CommandText.IndexOf('('); // find index of close bracket in create query, where are descriptions of columns var closeBacket = CommandText.LastIndexOf(')'); if (openBacket == -1 || closeBacket == -1 || openBacket == closeBacket) { throw new CreateIndexParse($"Command has incorrect signature. Check brackets."); } // get columns name var commandWords = CommandText? .Substring(0, openBacket) .Trim() .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (commandWords[0].ToLower() != "create") { throw new CreateIndexParse($"Command has incorrect signature."); } if (commandWords.Length == 5) { if (commandWords[2].ToLower() != "index") { throw new CreateIndexParse($"Command has incorrect signature."); } // find 'ON' into query if (commandWords[3].ToLower() != "on") { throw new CreateIndexParse($"Command has incorrect signature."); } TableName = commandWords[4]; IndexName = commandWords[2]; IndexType = IndexType.DenseIndex; } else if (commandWords.Length == 6) { if (commandWords[3].ToLower() != "index") { throw new CreateIndexParse($"Command has incorrect signature."); } throw new CreateIndexParse($"Command has not released."); } else { throw new CreateIndexParse($"Command has incorrect signature."); } var columnName = CommandText? .Substring(openBacket + 1, closeBacket - 1 - openBacket); if (columnName.Length == 0) { throw new CreateIndexParse($"Command has incorrect signature. Can not parse columns name."); } TableName = commandWords[2].Trim(); }
protected override void ParseQuery() { // find index of open bracket in create query, where are descriptions of columns var openBacket = CommandText.IndexOf('('); // find index of close bracket in create query, where are descriptions of columns var closeBacket = CommandText.LastIndexOf(')'); if (openBacket == -1 || closeBacket == -1 || openBacket == closeBacket) { throw new CreateTableParse($"Command has incorrect signature. Check brackets."); } // get command words, words befor descriptions of columns var commandWords = CommandText? .Substring(0, openBacket) .Trim() .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (commandWords.Length != 3) { throw new CreateTableParse($"Command has incorrect signature. Check table name."); } // set table name and database name if format [dbname].[tableName] var tableAndDatabaseNames = commandWords[2].Split('.'); if (tableAndDatabaseNames.Length == 2) { DatabaseName = tableAndDatabaseNames[0]; TableName = tableAndDatabaseNames[1]; } else { TableName = tableAndDatabaseNames[0]; } // Columns = CommandText? .Substring(openBacket + 1, closeBacket - openBacket - 1) .Trim() .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (Columns.Length == 0) { throw new CreateTableParse($"Command does not have new columns description."); } }
public override object ExecuteScalar() { if (ScalarMethodCache.TryGetValue(CommandText, out var cached) == false) { var lastDotIndex = CommandText.LastIndexOf('.'); var typeString = CommandText.Substring(0, lastDotIndex); var type = Assembly.GetExecutingAssembly().GetType(typeString); var methodString = CommandText.Substring(lastDotIndex + 1); cached = type.GetMethod(methodString); ScalarMethodCache.Add(CommandText, cached); } var value = cached.Invoke(null, null); return(value ?? DBNull.Value); }