Exemple #1
0
        protected virtual void WriteColumnDefinitionScriptFragment(StringBuilder sb, ColumnMold column)
        {
            // name
            var decoratedColumnName = this.Dialect.DecorateIdentifier(
                DbIdentifierType.Column,
                column.Name,
                this.CurrentOpeningIdentifierDelimiter);

            sb.Append(decoratedColumnName);

            sb.Append(" ");

            this.WriteTypeDefinitionScriptFragment(sb, column.Type);

            // nullability
            sb.Append(" ");

            var nullability = column.IsNullable ? "NULL" : "NOT NULL";

            sb.Append(nullability);

            if (column.Identity != null)
            {
                sb.Append(" ");
                this.WriteIdentityScriptFragment(sb, column.Identity);
            }
        }
Exemple #2
0
        protected override IDbValueConverter CreateDbValueConverter(ColumnMold column)
        {
            var typeName = column.Type.Name.ToLowerInvariant();
            switch (typeName)
            {
                case "uniqueidentifier":
                    return new GuidValueConverter();

                case "text":
                    return new StringValueConverter();

                case "datetime":
                    return new DateTimeValueConverter();

                case "integer":
                    return new SQLiteInt64ValueConverter();

                case "blob":
                    return new ByteArrayValueConverter();

                case "real":
                    return new DoubleValueConverter();

                case "numeric":
                    return new DecimalValueConverter();

                default:
                    throw new NotSupportedException($"Type name '{typeName}' not supported.");
            }
        }
        protected override IDbValueConverter CreateDbValueConverter(ColumnMold column)
        {
            var typeName = column.Type.Name.ToLowerInvariant();

            switch (typeName)
            {
            case "uniqueidentifier":
                return(new GuidValueConverter());

            case "char":
            case "varchar":
            case "nchar":
            case "nvarchar":
                return(new StringValueConverter());

            case "int":
            case "integer":
                return(new Int32ValueConverter());

            case "datetime":
            case "datetime2":
            case "date":
                return(new DateTimeValueConverter());

            case "bit":
                return(new BooleanValueConverter());

            case "binary":
            case "varbinary":
                return(new ByteArrayValueConverter());

            case "float":
                return(new DoubleValueConverter());

            case "real":
                return(new SingleValueConverter());

            case "money":
            case "decimal":
            case "numeric":
                return(new DecimalValueConverter());

            case "tinyint":
                return(new ByteValueConverter());

            case "smallint":
                return(new Int16ValueConverter());

            case "bigint":
                return(new Int64ValueConverter());

            default:
                throw new NotImplementedException();
            }
        }
        protected override ColumnMold ColumnInfoToColumnMold(ColumnInfo columnInfo)
        {
            var column = new ColumnMold
            {
                Name = columnInfo.Name,
                Type = this.Factory.GetDialect().ResolveType(
                    columnInfo.TypeName,
                    columnInfo.Size,
                    columnInfo.Precision,
                    columnInfo.Scale),
                IsNullable = columnInfo.IsNullable,
            };

            return(column);
        }
Exemple #5
0
 internal static void MarkAsExplicitPrimaryKey(this ColumnMold columnMold)
 {
     columnMold.SetBoolProperty("is-explicit-primary-key", true);
 }
Exemple #6
0
        private void ChargeRoot(INode root)
        {
            var allSqlNodes = root.FetchTree();

            var exactTextNodes = allSqlNodes
                                 .Where(x => x is ExactTextNode)
                                 .Cast <ExactTextNode>()
                                 .ToList();

            var reservedWords = exactTextNodes
                                .Select(x => x.ExactText)
                                .Distinct()
                                .Select(x => x.ToUpperInvariant())
                                .ToHashSet();

            var identifiersAsWords = allSqlNodes
                                     .Where(x =>
                                            x is TextNode textNode &&
                                            x.Name.EndsWith("-name", StringComparison.InvariantCultureIgnoreCase))
                                     .Cast <TextNode>()
                                     .ToList();

            #region assign job to nodes

            // table
            var createTable = (ActionNode)allSqlNodes.Single(x =>
                                                             string.Equals(x.Name, "do-create-table", StringComparison.InvariantCultureIgnoreCase));
            createTable.Action = (node, token, accumulator) =>
            {
                var tableMold = new TableMold();
                accumulator.AddResult(tableMold);
            };

            var tableName = (ActionNode)allSqlNodes.Single(x =>
                                                           string.Equals(x.Name, "table-name", StringComparison.InvariantCultureIgnoreCase));
            tableName.Action = (node, token, accumulator) =>
            {
                var tableMold = accumulator.GetLastResult <TableMold>();
                tableMold.Name = ((TextToken)token).Text;
            };

            var columnName = (ActionNode)allSqlNodes.Single(x =>
                                                            string.Equals(x.Name, "column-name", StringComparison.InvariantCultureIgnoreCase));
            columnName.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = new ColumnMold
                {
                    Name = ((TextToken)token).Text,
                };
                tableMold.Columns.Add(columnMold);
            };

            var typeName = (ActionNode)allSqlNodes.Single(x =>
                                                          string.Equals(x.Name, "type-name", StringComparison.InvariantCultureIgnoreCase));
            typeName.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Type.Name = ((TextToken)token).Text;
            };

            var precision = (ActionNode)allSqlNodes.Single(x =>
                                                           string.Equals(x.Name, "precision", StringComparison.InvariantCultureIgnoreCase));
            precision.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Type.Precision = ((IntegerToken)token).Value.ToInt32();
            };

            var scale = (ActionNode)allSqlNodes.Single(x =>
                                                       string.Equals(x.Name, "scale", StringComparison.InvariantCultureIgnoreCase));
            scale.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Type.Scale = ((IntegerToken)token).Value.ToInt32();
            };

            var nullToken = (ActionNode)allSqlNodes.Single(x =>
                                                           string.Equals(x.Name, "null", StringComparison.InvariantCultureIgnoreCase));
            nullToken.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.IsNullable = true;
            };

            var notNullToken = (ActionNode)allSqlNodes.Single(x =>
                                                              string.Equals(x.Name, "not-null", StringComparison.InvariantCultureIgnoreCase));
            notNullToken.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.IsNullable = false;
            };

            var inlinePrimaryKey = (ActionNode)allSqlNodes.Single(x =>
                                                                  string.Equals(x.Name, "inline-primary-key", StringComparison.InvariantCultureIgnoreCase));

            inlinePrimaryKey.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.MarkAsExplicitPrimaryKey();
            };

            var autoincrement = (ActionNode)allSqlNodes.Single(x =>
                                                               string.Equals(x.Name, "autoincrement", StringComparison.InvariantCultureIgnoreCase));
            autoincrement.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Identity = new ColumnIdentityMold();
            };

            var defaultNull = (ActionNode)allSqlNodes.Single(x =>
                                                             string.Equals(x.Name, "default-null", StringComparison.InvariantCultureIgnoreCase));
            defaultNull.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Default = "NULL";
            };

            var defaultInteger = (ActionNode)allSqlNodes.Single(x =>
                                                                string.Equals(x.Name, "default-integer", StringComparison.InvariantCultureIgnoreCase));
            defaultInteger.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Default = ((IntegerToken)token).Value;
            };

            var defaultString = (ActionNode)allSqlNodes.Single(x =>
                                                               string.Equals(x.Name, "default-string", StringComparison.InvariantCultureIgnoreCase));
            defaultString.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var columnMold = tableMold.Columns.Last();
                columnMold.Default = $"'{((TextToken)token).Text}'";
            };

            var constraintName = (ActionNode)allSqlNodes.Single(x =>
                                                                string.Equals(x.Name, "constraint-name", StringComparison.InvariantCultureIgnoreCase));
            constraintName.Action = (node, token, accumulator) =>
            {
                var tableMold = accumulator.GetLastResult <TableMold>();
                tableMold.SetLastConstraintName(((TextToken)token).Text);
            };

            var pk = (ActionNode)allSqlNodes.Single(x =>
                                                    string.Equals(x.Name, "do-primary-key", StringComparison.InvariantCultureIgnoreCase));
            pk.Action = (node, token, accumulator) =>
            {
                var tableMold = accumulator.GetLastResult <TableMold>();
                tableMold.PrimaryKey = new PrimaryKeyMold
                {
                    Name = tableMold.GetLastConstraintName(),
                };
            };

            var pkColumnName = (ActionNode)allSqlNodes.Single(x =>
                                                              string.Equals(x.Name, "pk-column-name", StringComparison.InvariantCultureIgnoreCase));
            pkColumnName.Action = (node, token, accumulator) =>
            {
                var tableMold   = accumulator.GetLastResult <TableMold>();
                var primaryKey  = tableMold.PrimaryKey;
                var indexColumn = new IndexColumnMold
                {
                    Name = ((TextToken)token).Text,
                };
                primaryKey.Columns.Add(indexColumn);
            };

            var pkColumnAscOrDesc = (ActionNode)allSqlNodes.Single(x =>
                                                                   string.Equals(x.Name, "pk-asc-or-desc", StringComparison.InvariantCultureIgnoreCase));
            pkColumnAscOrDesc.Action = (node, token, accumulator) =>
            {
                var tableInfo   = accumulator.GetLastResult <TableMold>();
                var primaryKey  = tableInfo.PrimaryKey;
                var indexColumn = primaryKey.Columns.Last();

                var ascOrDesc = ((TextToken)token).Text.ToLowerInvariant();
                indexColumn.SortDirection = SQLiteParsingHelper.SqlToSortDirection(ascOrDesc);
            };

            var fk = (ActionNode)allSqlNodes.Single(x =>
                                                    string.Equals(x.Name, "do-foreign-key", StringComparison.InvariantCultureIgnoreCase));
            fk.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var foreignKey = new ForeignKeyMold
                {
                    Name = tableMold.GetLastConstraintName(),
                };
                tableMold.ForeignKeys.Add(foreignKey);
            };

            var fkTableName = (ActionNode)allSqlNodes.Single(x =>
                                                             string.Equals(x.Name, "fk-referenced-table-name", StringComparison.InvariantCultureIgnoreCase));
            fkTableName.Action = (node, token, accumulator) =>
            {
                var tableMold           = accumulator.GetLastResult <TableMold>();
                var foreignKey          = tableMold.ForeignKeys.Last();
                var foreignKeyTableName = ((TextToken)token).Text;
                foreignKey.ReferencedTableName = foreignKeyTableName;
            };

            var fkColumnName = (ActionNode)allSqlNodes.Single(x =>
                                                              string.Equals(x.Name, "fk-column-name", StringComparison.InvariantCultureIgnoreCase));
            fkColumnName.Action = (node, token, accumulator) =>
            {
                var tableMold            = accumulator.GetLastResult <TableMold>();
                var foreignKey           = tableMold.ForeignKeys.Last();
                var foreignKeyColumnName = ((TextToken)token).Text;
                foreignKey.ColumnNames.Add(foreignKeyColumnName);
            };

            var fkReferencedColumnName = (ActionNode)allSqlNodes.Single(x =>
                                                                        string.Equals(x.Name, "fk-referenced-column-name", StringComparison.InvariantCultureIgnoreCase));
            fkReferencedColumnName.Action = (node, token, accumulator) =>
            {
                var tableMold  = accumulator.GetLastResult <TableMold>();
                var foreignKey = tableMold.ForeignKeys.Last();
                var foreignKeyReferencedColumnName = ((TextToken)token).Text;
                foreignKey.ReferencedColumnNames.Add(foreignKeyReferencedColumnName);
            };

            // index
            var createUniqueIndex = (ActionNode)allSqlNodes.Single(x =>
                                                                   string.Equals(x.Name, "do-create-unique-index", StringComparison.InvariantCultureIgnoreCase));
            createUniqueIndex.Action = (node, token, accumulator) =>
            {
                var index = new IndexMold
                {
                    IsUnique = true,
                };
                index.SetIsCreationFinalized(false);
                accumulator.AddResult(index);
            };

            var createIndex = (ActionNode)allSqlNodes.Single(x =>
                                                             string.Equals(x.Name, "do-create-index", StringComparison.InvariantCultureIgnoreCase));
            createIndex.Action = (node, token, accumulator) =>
            {
                bool brandNewIndex;

                if (accumulator.Count == 0)
                {
                    brandNewIndex = true;
                }
                else
                {
                    var result = accumulator.Last();
                    if (result is IndexMold indexMold)
                    {
                        brandNewIndex = indexMold.GetIsCreationFinalized();
                    }
                    else
                    {
                        brandNewIndex = true;
                    }
                }

                if (brandNewIndex)
                {
                    var newIndex = new IndexMold();
                    newIndex.SetIsCreationFinalized(true);
                    accumulator.AddResult(newIndex);
                }
                else
                {
                    var existingIndexMold = accumulator.GetLastResult <IndexMold>();
                    existingIndexMold.SetIsCreationFinalized(true);
                }
            };

            var indexName = (ActionNode)allSqlNodes.Single(x =>
                                                           string.Equals(x.Name, "index-name", StringComparison.InvariantCultureIgnoreCase));
            indexName.Action = (node, token, accumulator) =>
            {
                var index = accumulator.GetLastResult <IndexMold>();
                index.Name = ((TextToken)token).Text;
            };

            var indexTableName = (ActionNode)allSqlNodes.Single(x =>
                                                                string.Equals(x.Name, "index-table-name", StringComparison.InvariantCultureIgnoreCase));
            indexTableName.Action = (node, token, accumulator) =>
            {
                var index = accumulator.GetLastResult <IndexMold>();
                index.TableName = ((TextToken)token).Text;
            };

            var indexColumnName = (ActionNode)allSqlNodes.Single(x =>
                                                                 string.Equals(x.Name, "index-column-name", StringComparison.InvariantCultureIgnoreCase));
            indexColumnName.Action = (node, token, accumulator) =>
            {
                var index      = accumulator.GetLastResult <IndexMold>();
                var columnMold = new IndexColumnMold
                {
                    Name = ((TextToken)token).Text,
                };
                index.Columns.Add(columnMold);
            };

            var indexColumnAscOrDesc = (ActionNode)allSqlNodes.Single(x =>
                                                                      string.Equals(x.Name, "index-column-asc-or-desc", StringComparison.InvariantCultureIgnoreCase));
            indexColumnAscOrDesc.Action = (node, token, accumulator) =>
            {
                var index      = accumulator.GetLastResult <IndexMold>();
                var columnInfo = index.Columns.Last();

                var ascOrDesc = ((TextToken)token).Text.ToLowerInvariant();
                columnInfo.SortDirection = SQLiteParsingHelper.SqlToSortDirection(ascOrDesc);
            };

            #endregion

            foreach (var identifiersAsWord in identifiersAsWords)
            {
                identifiersAsWord.AdditionalChecker = (token, accumulator) =>
                {
                    var wordToken = ((TextToken)token).Text.ToUpperInvariant();
                    return(!reservedWords.Contains(wordToken));
                };
            }
        }