Skip to content

Commit

Permalink
Extract sql query builder (#5123)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-prisma authored Jan 15, 2025
1 parent 361de20 commit 244e52c
Show file tree
Hide file tree
Showing 75 changed files with 524 additions and 437 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ members = [
"query-engine/query-engine-c-abi",
"query-engine/request-handlers",
"query-engine/schema",
"query-engine/query-builders/*",
"libs/*",
"prisma-fmt",
"prisma-schema-wasm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::{
MongoDbTransaction,
};
use async_trait::async_trait;
use connector_interface::{
Connection, ConnectionLike, ReadOperations, Transaction, UpdateType, WriteArgs, WriteOperations,
};
use connector_interface::{Connection, ConnectionLike, ReadOperations, Transaction, UpdateType, WriteOperations};
use mongodb::{ClientSession, Database};
use query_structure::{prelude::*, RelationLoadStrategy, SelectionResult};
use std::collections::HashMap;
Expand Down Expand Up @@ -55,7 +53,7 @@ impl WriteOperations for MongoDbConnection {
async fn create_record(
&mut self,
model: &Model,
args: WriteArgs,
args: query_structure::WriteArgs,
// The field selection on a create is never used on MongoDB as it cannot return more than the ID.
_selected_fields: FieldSelection,
_traceparent: Option<TraceParent>,
Expand All @@ -66,7 +64,7 @@ impl WriteOperations for MongoDbConnection {
async fn create_records(
&mut self,
model: &Model,
args: Vec<WriteArgs>,
args: Vec<query_structure::WriteArgs>,
skip_duplicates: bool,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<usize> {
Expand All @@ -83,7 +81,7 @@ impl WriteOperations for MongoDbConnection {
async fn create_records_returning(
&mut self,
_model: &Model,
_args: Vec<WriteArgs>,
_args: Vec<query_structure::WriteArgs>,
_skip_duplicates: bool,
_selected_fields: FieldSelection,
_traceparent: Option<TraceParent>,
Expand All @@ -94,8 +92,8 @@ impl WriteOperations for MongoDbConnection {
async fn update_records(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
args: WriteArgs,
record_filter: query_structure::RecordFilter,
args: query_structure::WriteArgs,
limit: Option<usize>,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<usize> {
Expand All @@ -118,8 +116,8 @@ impl WriteOperations for MongoDbConnection {
async fn update_records_returning(
&mut self,
_model: &Model,
_record_filter: connector_interface::RecordFilter,
_args: WriteArgs,
_record_filter: query_structure::RecordFilter,
_args: query_structure::WriteArgs,
_selected_fields: FieldSelection,
_limit: Option<usize>,
_traceparent: Option<TraceParent>,
Expand All @@ -130,8 +128,8 @@ impl WriteOperations for MongoDbConnection {
async fn update_record(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
args: WriteArgs,
record_filter: query_structure::RecordFilter,
args: query_structure::WriteArgs,
selected_fields: Option<FieldSelection>,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<Option<SingleRecord>> {
Expand Down Expand Up @@ -163,7 +161,7 @@ impl WriteOperations for MongoDbConnection {
async fn delete_records(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
record_filter: query_structure::RecordFilter,
limit: Option<usize>,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<usize> {
Expand All @@ -180,7 +178,7 @@ impl WriteOperations for MongoDbConnection {
async fn delete_record(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
record_filter: query_structure::RecordFilter,
selected_fields: FieldSelection,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<SingleRecord> {
Expand Down Expand Up @@ -314,7 +312,7 @@ impl ReadOperations for MongoDbConnection {
&mut self,
model: &Model,
query_arguments: query_structure::QueryArguments,
selections: Vec<connector_interface::AggregationSelection>,
selections: Vec<query_structure::AggregationSelection>,
group_by: Vec<ScalarFieldRef>,
having: Option<query_structure::Filter>,
_traceparent: Option<TraceParent>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn create_record(
&mut self,
model: &Model,
args: connector_interface::WriteArgs,
args: query_structure::WriteArgs,
// The field selection on a create is never used on MongoDB as it cannot return more than the ID.
_selected_fields: FieldSelection,
_traceparent: Option<TraceParent>,
Expand All @@ -97,7 +97,7 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn create_records(
&mut self,
model: &Model,
args: Vec<connector_interface::WriteArgs>,
args: Vec<query_structure::WriteArgs>,
skip_duplicates: bool,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<usize> {
Expand All @@ -114,7 +114,7 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn create_records_returning(
&mut self,
_model: &Model,
_args: Vec<connector_interface::WriteArgs>,
_args: Vec<query_structure::WriteArgs>,
_skip_duplicates: bool,
_selected_fields: FieldSelection,
_traceparent: Option<TraceParent>,
Expand All @@ -125,8 +125,8 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn update_records(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
args: connector_interface::WriteArgs,
record_filter: query_structure::RecordFilter,
args: query_structure::WriteArgs,
limit: Option<usize>,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<usize> {
Expand All @@ -148,8 +148,8 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn update_records_returning(
&mut self,
_model: &Model,
_record_filter: connector_interface::RecordFilter,
_args: connector_interface::WriteArgs,
_record_filter: query_structure::RecordFilter,
_args: query_structure::WriteArgs,
_selected_fields: FieldSelection,
_limit: Option<usize>,
_traceparent: Option<TraceParent>,
Expand All @@ -160,8 +160,8 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn update_record(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
args: connector_interface::WriteArgs,
record_filter: query_structure::RecordFilter,
args: query_structure::WriteArgs,
selected_fields: Option<FieldSelection>,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<Option<SingleRecord>> {
Expand Down Expand Up @@ -192,7 +192,7 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn delete_records(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
record_filter: query_structure::RecordFilter,
limit: Option<usize>,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<usize> {
Expand All @@ -209,7 +209,7 @@ impl WriteOperations for MongoDbTransaction<'_> {
async fn delete_record(
&mut self,
model: &Model,
record_filter: connector_interface::RecordFilter,
record_filter: query_structure::RecordFilter,
selected_fields: FieldSelection,
_traceparent: Option<TraceParent>,
) -> connector_interface::Result<SingleRecord> {
Expand Down Expand Up @@ -348,7 +348,7 @@ impl ReadOperations for MongoDbTransaction<'_> {
&mut self,
model: &Model,
query_arguments: query_structure::QueryArguments,
selections: Vec<connector_interface::AggregationSelection>,
selections: Vec<query_structure::AggregationSelection>,
group_by: Vec<ScalarFieldRef>,
having: Option<query_structure::Filter>,
_traceparent: Option<TraceParent>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use connector_interface::AggregationSelection;
use indexmap::IndexMap;
use query_structure::{
ast::FieldArity, DefaultKind, FieldSelection, PrismaValue, ScalarFieldRef, SelectedField, TypeIdentifier,
ast::FieldArity, AggregationSelection, DefaultKind, FieldSelection, PrismaValue, ScalarFieldRef, SelectedField,
TypeIdentifier,
};

/// Maps field db field names to their meta information.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::constants::*;

use bson::{doc, Bson, Document};
use connector_interface::AggregationSelection;
use query_structure::{AggregationFilter, Filter, ScalarFieldRef};
use query_structure::{AggregationFilter, AggregationSelection, Filter, ScalarFieldRef};
use std::collections::HashSet;

/// Represents a `$group` aggregation stage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use crate::{
vacuum_cursor, BsonTransform, IntoBson,
};
use bson::{doc, Document};
use connector_interface::AggregationSelection;
use itertools::Itertools;
use mongodb::{options::AggregateOptions, ClientSession, Collection};
use query_structure::{FieldSelection, Filter, Model, QueryArguments, ScalarFieldRef, VirtualSelection};
use query_structure::{
AggregationSelection, FieldSelection, Filter, Model, QueryArguments, ScalarFieldRef, VirtualSelection,
};
use std::convert::TryFrom;
use std::future::IntoFuture;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{constants::*, output_meta, query_builder::MongoReadQueryBuilder, val

use connector_interface::*;
use mongodb::{bson::Document, ClientSession, Database};
use query_structure::{prelude::*, Filter, QueryArguments};
use query_structure::{prelude::*, AggregationSelection, Filter, QueryArguments};

pub async fn aggregate<'conn>(
database: &Database,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{into_expression::IntoUpdateExpression, operation};

use bson::{doc, Bson, Document};
use connector_interface::FieldPath;
use indexmap::IndexMap;
use query_structure::FieldPath;

/// `UpdateExpression` is an intermediary AST that's used to represent MongoDB expressions.
/// It is meant to be transformed into `BSON`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use super::operation::*;
use crate::*;

use bson::doc;
use connector_interface::{CompositeWriteOperation, FieldPath, ScalarWriteOperation, WriteOperation};
use query_structure::{Field, PrismaValue};
use query_structure::{CompositeWriteOperation, Field, FieldPath, PrismaValue, ScalarWriteOperation, WriteOperation};

pub(crate) trait IntoUpdateOperation {
fn into_update_operations(self, field: &Field, path: FieldPath) -> crate::Result<Vec<UpdateOperation>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use super::*;
use crate::*;

use bson::Document;
use connector_interface::{FieldPath, WriteOperation};
use into_expression::IntoUpdateExpressions;
use into_operation::IntoUpdateOperation;
use query_structure::{FieldPath, WriteOperation};

pub(crate) trait IntoUpdateDocumentExtension {
fn into_update_docs(self, field: &Field, path: FieldPath) -> crate::Result<Vec<Document>>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{expression, into_expression::IntoUpdateExpression};
use bson::{doc, Document};
use connector_interface::FieldPath;
use query_structure::Filter;
use query_structure::{FieldPath, Filter};

/// `UpdateOperation` is an intermediary AST used to perform preliminary transformations from a `WriteOperation`.
/// It is meant to be transformed into an `UpdateExpression`.
Expand Down
Loading

0 comments on commit 244e52c

Please sign in to comment.