Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/0.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Aug 21, 2018
2 parents f3ecfee + a39477f commit eb66651
Show file tree
Hide file tree
Showing 26 changed files with 310 additions and 124 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
### New in 0.7.0 (Released 2018/08/21)

- [__#4__](https://github.com/spectresystems/spectre.query/issues/4) Add support for LIKE operator
- [__#16__](https://github.com/spectresystems/spectre.query/issues/16) Add EBNF grammar to repo

### New in 0.6.0 (Released 2018/08/19)

- [__#13__](https://github.com/spectresystems/spectre.query/issues/13) Add support for navigational properties

### New in 0.5.0 (Released 2018/08/19)

- [__#11__](https://github.com/spectresystems/spectre.query/issues/11) Add way of getting the parsed expression from a query
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Spectre.Query is a library for doing simplified (safe) querying in Entity Framework Core. Perfect when you want to let end users or APIs search with a SQL-esque language without actually letting them execute any SQL directly (which you never should).

```
ID > 0 AND Year < 2007 AND Comment != null AND !Seen
ID > 0 AND Year < 2007 AND Comment != null AND (!Seen OR Comment LIKE '%Awesome%')
```

This project is currently under active development and might not be ready for production.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Spectre.Query.AspNetCore.Example.Data;
using Spectre.Query.AspNetCore.Example.Models;

Expand All @@ -20,6 +21,7 @@ public HomeController(MovieContext context, IQueryProvider<MovieContext> provide
public IActionResult Index(string query = null)
{
var movies = _provider.Query<Movie>(_context, query)
.Include(m => m.Genre)
.OrderByDescending(movie => movie.Rating)
.ToList();

Expand Down
5 changes: 4 additions & 1 deletion examples/Spectre.Query.Example.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,26 @@ public void ConfigureServices(IServiceCollection services)
// Configure the query provider.
services.AddQueryProvider<MovieContext>(options =>
{
// Configure an entity.
options.Configure<Movie>(movie =>
{
movie.Map("Id", e => e.MovieId);
movie.Map("Genre", e => e.Genre.Name);
movie.Map("Title", e => e.Name);
movie.Map("Year", e => e.ReleasedAt);
movie.Map("Score", e => e.Rating);
movie.Map("Seen", e => e.Seen);
});

// Configure a query type projection.
options.Configure<MovieProjection>(movie =>
{
movie.Map("Id", e => e.MovieId);
movie.Map("Genre", e => e.Genre);
movie.Map("Title", e => e.Name);
movie.Map("Year", e => e.ReleasedAt);
movie.Map("Score", e => e.Rating);
movie.Map("Seen", e => e.Seen);
movie.Map("Genre", e => e.Genre);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<th>Title</th>
<th>Year</th>
<th>Score (0-100)</th>
<th>Genre</th>
<th>Seen</th>
</tr>
</thead>
Expand All @@ -34,6 +35,7 @@
<td>@movie.Name</td>
<td>@movie.ReleasedAt</td>
<td>@movie.Rating</td>
<td>@movie.Genre.Name</td>
<td>@(movie.Seen ? "Yes" : "No")</td>
</tr>
}
Expand Down
22 changes: 15 additions & 7 deletions examples/Spectre.Query.Example/Application.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Spectre.Query.Example.Data;

namespace Spectre.Query.Example
Expand Down Expand Up @@ -37,10 +38,13 @@ public void Run()
}

// Query the EF context for movies.
var movies = _provider.Query<Movie>(_context, query).ToList();
var movies = _provider
.Query<Movie>(_context, query)
.Include(m => m.Genre)
.ToList();

// Output the result.
OutputMovies(movies);
ListResults(movies);
}
catch (Exception ex)
{
Expand All @@ -60,17 +64,21 @@ private string GetInput()
return Console.ReadLine();
}

private void OutputMovies(List<Movie> movies)
private void ListResults(List<Movie> movies)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("{0,-40} {1,4} {2,5} {3, 5}", "Title", "Year", "Score", "Seen?");
Console.WriteLine(new string('=', 60));
Console.WriteLine("{0,-40} {1,4} {2,5} {3, -10} {4, 5}", "Title", "Year", "Score", "Genre", "Seen?");
Console.WriteLine(new string('=', 72));
Console.ResetColor();
foreach (var movie in movies)
{
Console.WriteLine("{0,-40} {1,4} {2,5} {3, 5}",
movie.Name, movie.ReleasedAt, movie.Rating, movie.Seen ? "Yes" : "No");
Console.WriteLine("{0,-40} {1,4} {2,5} {3, -10} {4, 5}",
movie.Name,
movie.ReleasedAt,
movie.Rating,
movie.Genre.Name,
movie.Seen ? "Yes" : "No");
}
Console.WriteLine();
}
Expand Down
8 changes: 8 additions & 0 deletions examples/Spectre.Query.Example/Data/Genre.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Spectre.Query.Example.Data
{
public sealed class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
}
}
1 change: 1 addition & 0 deletions examples/Spectre.Query.Example/Data/Movie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public sealed class Movie
public int ReleasedAt { get; set; }
public int Rating { get; set; }
public bool Seen { get; set; }
public Genre Genre { get; set; }
}
}
60 changes: 35 additions & 25 deletions examples/Spectre.Query.Example/Data/MovieContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace Spectre.Query.Example.Data
public sealed class MovieContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
public DbSet<Genre> Genres { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MovieDatabaseConsole;Trusted_Connection=True;");
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MovieTerminalDatabase;Trusted_Connection=True;");
}

public static MovieContext Initialize()
Expand All @@ -19,30 +20,39 @@ public static MovieContext Initialize()

if (context.Movies.Count() == 0)
{
context.Movies.Add(new Movie { Name = "The Shawshank Redemption", Rating = 93, ReleasedAt = 1994, Seen = true });
context.Movies.Add(new Movie { Name = "The Godfather", Rating = 92, ReleasedAt = 1972, Seen = true });
context.Movies.Add(new Movie { Name = "It's a Wonderful Life", Rating = 86, ReleasedAt = 1946, Seen = false });
context.Movies.Add(new Movie { Name = "Back to the Future", Rating = 85, ReleasedAt = 1985, Seen = true });
context.Movies.Add(new Movie { Name = "Psycho", Rating = 85, ReleasedAt = 1960, Seen = true });
context.Movies.Add(new Movie { Name = "The Fast and the Furious: Tokyo Drift", Rating = 60, ReleasedAt = 2006, Seen = false });
context.Movies.Add(new Movie { Name = "Spider-Man 3", Rating = 62, ReleasedAt = 2007, Seen = false });
context.Movies.Add(new Movie { Name = "Interstellar", Rating = 86, ReleasedAt = 2014, Seen = true });
context.Movies.Add(new Movie { Name = "The Disaster Artist", Rating = 75, ReleasedAt = 2017, Seen = true });
context.Movies.Add(new Movie { Name = "The Room", Rating = 36, ReleasedAt = 2003, Seen = true });
context.Movies.Add(new Movie { Name = "Zoolander", Rating = 66, ReleasedAt = 2001, Seen = true });
context.Movies.Add(new Movie { Name = "The Big Sick", Rating = 76, ReleasedAt = 2017, Seen = false });
context.Movies.Add(new Movie { Name = "Moonlight", Rating = 74, ReleasedAt = 2016, Seen = true });
context.Movies.Add(new Movie { Name = "Get Out", Rating = 77, ReleasedAt = 2017, Seen = false });
context.Movies.Add(new Movie { Name = "Perfetti Sconosciuti", Rating = 78, ReleasedAt = 2016, Seen = true });
context.Movies.Add(new Movie { Name = "Jurassic Park", Rating = 81, ReleasedAt = 1993, Seen = true });
context.Movies.Add(new Movie { Name = "Little Miss Sunshine", Rating = 78, ReleasedAt = 2006, Seen = true });
context.Movies.Add(new Movie { Name = "The Usual Suspects", Rating = 86, ReleasedAt = 1995, Seen = true });
context.Movies.Add(new Movie { Name = "Gremlings", Rating = 72, ReleasedAt = 1984, Seen = true });
context.Movies.Add(new Movie { Name = "Stuart Little", Rating = 59, ReleasedAt = 1999, Seen = false });
context.Movies.Add(new Movie { Name = "Cars 2", Rating = 62, ReleasedAt = 2011, Seen = false });
context.Movies.Add(new Movie { Name = "Zombieland", Rating = 77, ReleasedAt = 2009, Seen = true });
context.Movies.Add(new Movie { Name = "The Pursuit of Happyness", Rating = 80, ReleasedAt = 2006, Seen = false });
context.Movies.Add(new Movie { Name = "Fargo", Rating = 81, ReleasedAt = 1996, Seen = true });
var drama = context.Genres.Add(new Genre { Name = "Drama" });
var crime = context.Genres.Add(new Genre { Name = "Crime" });
var scifi = context.Genres.Add(new Genre { Name = "Sci-Fi" });
var action = context.Genres.Add(new Genre { Name = "Action" });
var comedy = context.Genres.Add(new Genre { Name = "Comedy" });
var thriller = context.Genres.Add(new Genre { Name = "Thriller" });
var family = context.Genres.Add(new Genre { Name = "Family" });
var adventure = context.Genres.Add(new Genre { Name = "Adventure" });

context.Movies.Add(new Movie { Name = "The Shawshank Redemption", Rating = 93, ReleasedAt = 1994, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "The Godfather", Rating = 92, ReleasedAt = 1972, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "It's a Wonderful Life", Rating = 86, ReleasedAt = 1946, Seen = false, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "Back to the Future", Rating = 85, ReleasedAt = 1985, Seen = true, Genre = scifi.Entity });
context.Movies.Add(new Movie { Name = "Psycho", Rating = 85, ReleasedAt = 1960, Seen = true, Genre = thriller.Entity });
context.Movies.Add(new Movie { Name = "The Fast and the Furious: Tokyo Drift", Rating = 60, ReleasedAt = 2006, Seen = false, Genre = action.Entity });
context.Movies.Add(new Movie { Name = "Spider-Man 3", Rating = 62, ReleasedAt = 2007, Seen = false, Genre = action.Entity });
context.Movies.Add(new Movie { Name = "Interstellar", Rating = 86, ReleasedAt = 2014, Seen = true, Genre = scifi.Entity });
context.Movies.Add(new Movie { Name = "The Disaster Artist", Rating = 75, ReleasedAt = 2017, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "The Room", Rating = 36, ReleasedAt = 2003, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "Zoolander", Rating = 66, ReleasedAt = 2001, Seen = true, Genre = comedy.Entity });
context.Movies.Add(new Movie { Name = "The Big Sick", Rating = 76, ReleasedAt = 2017, Seen = false, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "Moonlight", Rating = 74, ReleasedAt = 2016, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "Get Out", Rating = 77, ReleasedAt = 2017, Seen = false, Genre = thriller.Entity });
context.Movies.Add(new Movie { Name = "Perfetti Sconosciuti", Rating = 78, ReleasedAt = 2016, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "Jurassic Park", Rating = 81, ReleasedAt = 1993, Seen = true, Genre = adventure.Entity });
context.Movies.Add(new Movie { Name = "Little Miss Sunshine", Rating = 78, ReleasedAt = 2006, Seen = true, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "The Usual Suspects", Rating = 86, ReleasedAt = 1995, Seen = true, Genre = crime.Entity });
context.Movies.Add(new Movie { Name = "Gremlings", Rating = 72, ReleasedAt = 1984, Seen = true, Genre = comedy.Entity });
context.Movies.Add(new Movie { Name = "Stuart Little", Rating = 59, ReleasedAt = 1999, Seen = false, Genre = family.Entity });
context.Movies.Add(new Movie { Name = "Cars 2", Rating = 62, ReleasedAt = 2011, Seen = false, Genre = family.Entity });
context.Movies.Add(new Movie { Name = "Zombieland", Rating = 77, ReleasedAt = 2009, Seen = true, Genre = thriller.Entity });
context.Movies.Add(new Movie { Name = "The Pursuit of Happyness", Rating = 80, ReleasedAt = 2006, Seen = false, Genre = drama.Entity });
context.Movies.Add(new Movie { Name = "Fargo", Rating = 81, ReleasedAt = 1996, Seen = true, Genre = drama.Entity });

context.SaveChanges();
}
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Spectre.Query.Example.Migrations
{
public partial class InitialMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Genres",
columns: table => new
{
GenreId = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Genres", x => x.GenreId);
});

migrationBuilder.CreateTable(
name: "Movies",
columns: table => new
{
MovieId = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true),
ReleasedAt = table.Column<int>(nullable: false),
Rating = table.Column<int>(nullable: false),
Seen = table.Column<bool>(nullable: false),
GenreId = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Movies", x => x.MovieId);
table.ForeignKey(
name: "FK_Movies_Genres_GenreId",
column: x => x.GenreId,
principalTable: "Genres",
principalColumn: "GenreId",
onDelete: ReferentialAction.Restrict);
});

migrationBuilder.CreateIndex(
name: "IX_Movies_GenreId",
table: "Movies",
column: "GenreId");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Movies");

migrationBuilder.DropTable(
name: "Genres");
}
}
}
Loading

0 comments on commit eb66651

Please sign in to comment.