コード例 #1
0
ファイル: Serializer.cs プロジェクト: EBassie/Potato
 /// <summary>
 /// Compile a parsed query
 /// </summary>
 /// <param name="parsed"></param>
 /// <returns></returns>
 public abstract ICompiledQuery Compile(IParsedQuery parsed);
コード例 #2
0
ファイル: Serializer.cs プロジェクト: EBassie/Potato
 /// <summary>
 /// Parse a single method, creating all the tokens to then compile the data
 /// </summary>
 /// <param name="method"></param>
 /// <param name="parsed"></param>
 /// <returns></returns>
 public abstract ISerializer Parse(IMethod method, IParsedQuery parsed);
コード例 #3
0
ファイル: Serializer.cs プロジェクト: EBassie/Potato
 protected Serializer() {
     this.Parsed = new ParsedQuery();
 }
コード例 #4
0
ファイル: SerializerSqLite.cs プロジェクト: EBassie/Potato
        public override ISerializer Parse(IMethod method, IParsedQuery parsed) {
            parsed.Root = method;

            parsed.Children = this.ParseChildren(method);

            parsed.Methods = this.ParseMethod(method);

            parsed.Skip = this.ParseSkip(method);

            parsed.Limit = this.ParseLimit(method);

            parsed.Databases = this.ParseDatabases(method);

            parsed.Indices = this.ParseIndices(method);

            parsed.Fields = this.ParseFields(method);
            parsed.Fields.AddRange(method.Where(statement => statement is Assignment).SelectMany(this.ParseFields));

            parsed.Values = method.Where(statement => statement is Assignment).SelectMany(this.ParseValues).ToList();

            parsed.Assignments = this.ParseAssignments(method);

            parsed.Conditions = this.ParseConditions(method);

            parsed.Collections = this.ParseCollections(method);

            parsed.Sortings = this.ParseSortings(method);

            return this;
        }
コード例 #5
0
ファイル: SerializerSqLite.cs プロジェクト: EBassie/Potato
        public override ICompiledQuery Compile(IParsedQuery parsed) {
            CompiledQuery serializedQuery = new CompiledQuery() {
                Children = parsed.Children.Select(this.Compile).Where(child => child != null).ToList(),
                Root = parsed.Root,
                Methods = parsed.Methods,
                Skip = parsed.Skip,
                Limit = parsed.Limit
            };

            List<String> compiled = new List<String>() {
                parsed.Methods.FirstOrDefault()
            };

            if (parsed.Root is Merge) {
                ICompiledQuery save = serializedQuery.Children.FirstOrDefault(child => child.Root is Save);
                ICompiledQuery modify = serializedQuery.Children.FirstOrDefault(child => child.Root is Modify);

                if (save != null && modify != null) {
                    compiled.Add("INTO");
                    compiled.Add(save.Collections.FirstOrDefault());
                    compiled.Add("SET");
                    compiled.Add(save.Assignments.FirstOrDefault());
                    //compiled.Add("ON DUPLICATE KEY UPDATE");
                    //compiled.Add(modify.Assignments.FirstOrDefault());
                }
            }
            else if (parsed.Root is Index) {
                Primary primary = parsed.Root.FirstOrDefault(attribute => attribute is Primary) as Primary;
                Unique unique = parsed.Root.FirstOrDefault(attribute => attribute is Unique) as Unique;

                if (primary == null) {
                    // UNIQUE INDEX `Score_UNIQUE` (`Score` ASC)
                    if (unique != null) {
                        compiled.Add("UNIQUE INDEX");

                        if (parsed.Root.Any(attribute => attribute is IfNotExists)) {
                            compiled.Add("IF NOT EXISTS");
                        }

                        // todo move the name element to a modifier?
                        compiled.Add(String.Format("`{0}`", ((Index)parsed.Root).Name));
                    }
                    // INDEX `Name_INDEX` (`Name` ASC)
                    else {
                        compiled.Add("INDEX");

                        if (parsed.Root.Any(attribute => attribute is IfNotExists)) {
                            compiled.Add("IF NOT EXISTS");
                        }

                        // todo move the name element to a modifier?
                        compiled.Add(String.Format("`{0}`", ((Index)parsed.Root).Name));
                    }

                    compiled.Add("ON");

                    if (parsed.Collections.Any() == true) {
                        serializedQuery.Collections.Add(String.Join(", ", parsed.Collections));
                        compiled.Add(serializedQuery.Collections.FirstOrDefault());
                    }

                    if (parsed.Sortings.Any() == true) {
                        serializedQuery.Sortings.Add(String.Join(", ", parsed.Sortings));
                        compiled.Add(String.Format("({0})", serializedQuery.Sortings.FirstOrDefault()));
                    }
                }
                else {
                    // SQLite does not support adding primary indexes after a table has been created.
                    serializedQuery = null;
                }
            }
            else if (parsed.Root is Alter) {
                if (parsed.Collections.Any() == true) {
                    compiled.Add("TABLE");

                    serializedQuery.Collections.Add(String.Join(", ", parsed.Collections));
                    compiled.Add(serializedQuery.Collections.FirstOrDefault());
                }

                compiled.Add(String.Join(", ", serializedQuery.Children.Where(child => child.Root is Create || child.Root is Drop).SelectMany(child => child.Compiled)));
            }
            else if (parsed.Root is Find) {
                serializedQuery.Fields = new List<String>(parsed.Fields);
                compiled.Add(parsed.Fields.Any() == true ? String.Join(", ", parsed.Fields) : "*");

                if (parsed.Collections.Any() == true) {
                    serializedQuery.Collections.Add(String.Join(", ", parsed.Collections));
                    compiled.Add("FROM");
                    compiled.Add(serializedQuery.Collections.FirstOrDefault());
                }

                if (parsed.Conditions.Any() == true) {
                    serializedQuery.Conditions.Add(String.Join(" AND ", parsed.Conditions));
                    compiled.Add("WHERE");
                    compiled.Add(serializedQuery.Conditions.FirstOrDefault());
                }

                if (parsed.Sortings.Any() == true) {
                    serializedQuery.Sortings.Add(String.Join(", ", parsed.Sortings));
                    compiled.Add("ORDER BY");
                    compiled.Add(serializedQuery.Sortings.FirstOrDefault());
                }

                if (parsed.Limit != null) {
                    compiled.Add("LIMIT");
                    compiled.Add(parsed.Limit.Value.ToString(CultureInfo.InvariantCulture));
                }

                if (parsed.Skip != null) {
                    compiled.Add("OFFSET");
                    compiled.Add(parsed.Skip.Value.ToString(CultureInfo.InvariantCulture));
                }
            }
            else if (parsed.Root is Create) {
                if (parsed.Databases.Any() == true) {
                    serializedQuery.Databases.Add(parsed.Databases.FirstOrDefault());
                    compiled = new List<String> {
                        "ATTACH",
                        "DATABASE",
                        serializedQuery.Databases.FirstOrDefault()
                    };
                }
                else if (parsed.Collections.Any() == true) {
                    compiled.Add("TABLE");

                    if (parsed.Root.Any(modifier => modifier is IfNotExists) == true) {
                        compiled.Add("IF NOT EXISTS");
                    }

                    compiled.Add(parsed.Collections.FirstOrDefault());

                    // parsed.Indices will only hae primary keys generated. Autoincrement primary keys mut be inline.
                    if (parsed.Indices.Any() == true && parsed.Root.DescendantsAndSelf<AutoIncrement>().Any() == false) {
                        List<String> fieldsIndicesCombination = new List<String>(parsed.Fields);
                        fieldsIndicesCombination.AddRange(parsed.Indices);

                        serializedQuery.Indices.Add(String.Join(", ", fieldsIndicesCombination.ToArray()));

                        compiled.Add(String.Format("({0})", serializedQuery.Indices.FirstOrDefault()));
                    }
                    else {
                        compiled.Add(String.Format("({0})", String.Join(", ", parsed.Fields.ToArray())));
                    }
                }
                else if (parsed.Fields.Any() == true) {
                    compiled.Add("COLUMN");

                    compiled.Add(String.Join(", ", parsed.Fields.ToArray()));
                }
            }
            else if (parsed.Root is Save) {
                if (parsed.Collections.Any() == true) {
                    compiled.Add("INTO");
                    serializedQuery.Collections.Add(parsed.Collections.FirstOrDefault());
                    compiled.Add(serializedQuery.Collections.FirstOrDefault());
                }

                if (parsed.Fields.Any() == true) {
                    serializedQuery.Fields.Add(String.Join(", ", parsed.Fields));
                    compiled.Add(String.Format("({0})", serializedQuery.Fields.FirstOrDefault()));
                }

                compiled.Add("VALUES");

                if (parsed.Values.Any() == true) {
                    serializedQuery.Values.Add(String.Join(", ", parsed.Values));
                    compiled.Add(String.Format("({0})", serializedQuery.Values.FirstOrDefault()));
                }

                if (parsed.Assignments.Any() == true) {
                    serializedQuery.Assignments.Add(String.Join(", ", parsed.Assignments));
                }
            }
            else if (parsed.Root is Modify) {
                if (parsed.Collections.Any() == true) {
                    serializedQuery.Collections.Add(String.Join(", ", parsed.Collections));
                    compiled.Add(serializedQuery.Collections.FirstOrDefault());
                }

                if (parsed.Assignments.Any() == true) {
                    serializedQuery.Assignments.Add(String.Join(", ", parsed.Assignments));
                    compiled.Add("SET");
                    compiled.Add(serializedQuery.Assignments.FirstOrDefault());
                }

                if (parsed.Conditions.Any() == true) {
                    serializedQuery.Conditions.Add(String.Join(" AND ", parsed.Conditions));
                    compiled.Add("WHERE");
                    compiled.Add(serializedQuery.Conditions.FirstOrDefault());
                }
            }
            else if (parsed.Root is Remove) {
                if (parsed.Collections.Any() == true) {
                    serializedQuery.Collections.Add(String.Join(", ", parsed.Collections));
                    compiled.Add("FROM");
                    compiled.Add(serializedQuery.Collections.FirstOrDefault());
                }

                if (parsed.Conditions.Any() == true) {
                    serializedQuery.Conditions.Add(String.Join(" AND ", parsed.Conditions));
                    compiled.Add("WHERE");
                    compiled.Add(serializedQuery.Conditions.FirstOrDefault());
                }
            }
            else if (parsed.Root is Drop) {
                if (parsed.Databases.Any() == true) {
                    serializedQuery.Databases.Add(parsed.Databases.FirstOrDefault());
                    compiled = new List<String> {
                        "DETACH",
                        "DATABASE",
                        serializedQuery.Databases.FirstOrDefault()
                    };
                }
                else if (parsed.Collections.Any() == true) {
                    compiled.Add("TABLE");
                    serializedQuery.Collections.Add(parsed.Collections.FirstOrDefault());
                    compiled.Add(serializedQuery.Collections.FirstOrDefault());
                }
                else if (parsed.Fields.Any() == true) {
                    compiled.Add("COLUMN");

                    compiled.Add(String.Join(", ", parsed.Fields.ToArray()));
                }
            }

            if (serializedQuery != null) serializedQuery.Compiled.Add(String.Join(" ", compiled));

            return serializedQuery;
        }
コード例 #6
0
ファイル: SerializerMySql.cs プロジェクト: EBassie/Potato
        public override ISerializer Parse(IMethod method, IParsedQuery parsed) {
            parsed.Root = method;

            parsed.Children = this.ParseChildren(method);

            parsed.Methods = this.ParseMethod(method);

            parsed.Skip = this.ParseSkip(method);

            parsed.Limit = this.ParseLimit(method);

            parsed.Databases = this.ParseDatabases(method);

            parsed.Indices = this.ParseIndices(method);

            parsed.Fields = this.ParseFields(method);

            parsed.Assignments = this.ParseAssignments(method);

            parsed.Conditions = this.ParseConditions(method);

            parsed.Collections = this.ParseCollections(method);

            parsed.Sortings = this.ParseSortings(method);

            return this;
        }
コード例 #7
0
ファイル: SerializerMongoDb.cs プロジェクト: EBassie/Potato
 public override ICompiledQuery Compile(IParsedQuery parsed) {
     return new CompiledQuery {
         Children = parsed.Children.Select(this.Compile).ToList(),
         Root = parsed.Root,
         Methods = parsed.Methods,
         Skip = parsed.Skip,
         Limit = parsed.Limit,
         Databases = parsed.Databases,
         Collections = parsed.Collections,
         Conditions = parsed.Conditions,
         Fields = parsed.Fields,
         Assignments = parsed.Assignments,
         Indices = parsed.Indices,
         Sortings = parsed.Sortings
     };
 }