Пример #1
0
        private static void VerifyMySqlType(MySqlVersion mySqlVersion, Column column, MySqlParameterInfo expectedParamInfo, string expectedSqlString)
        {
            var mySqlType = column.GetMySqlType();

            VerifyMySqlParamInfo(mySqlVersion, mySqlType, expectedParamInfo);
            Assert.AreEqual(expectedSqlString, mySqlType.GetDataTypeSql(mySqlVersion));
        }
Пример #2
0
        private static void VerifyMySqlParamInfo(MySqlVersion mySqlVersion, MySqlType mySqlType, MySqlParameterInfo expected)
        {
            var actual = mySqlType.GetSqlParameterInfo(mySqlVersion);

            Assert.AreEqual(expected.MySqlDbType, actual.MySqlDbType);
            Assert.AreEqual(expected.Size, actual.Size);
            Assert.AreEqual(expected.Precision, actual.Precision);
            Assert.AreEqual(expected.Scale, actual.Scale);
        }
Пример #3
0
 private SqlGenerator(MySqlVersion mySqlVersion)
 {
     MySqlVersion         = mySqlVersion;
     SqlBuilder           = new IndentedStringBuilder();
     _expressionGenerator = new ExpressionGenerator()
     {
         MySqlVersion = MySqlVersion,
         SqlBuilder   = SqlBuilder,
     };
 }
Пример #4
0
        internal static void GenerateConst(IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, Column column, object value)
        {
            if (value == null)
            {
                sqlBuilder.Append(NULL);
                return;
            }

            sqlBuilder.Append(column.GetMySqlType().GetLiteral(value, mySqlVersion));
        }
Пример #5
0
 private static void VerifyDbExpression(MySqlVersion mySqlVersion, DbExpression expr, string expectedSql, out ExpressionGenerator generator)
 {
     generator = new ExpressionGenerator()
     {
         MySqlVersion      = mySqlVersion,
         SqlBuilder        = new IndentedStringBuilder(),
         ModelAliasManager = new ModelAliasManagerMock()
     };
     expr.Accept(generator);
     Assert.AreEqual(expectedSql, generator.SqlBuilder.ToString());
 }
Пример #6
0
 private static void VerifyDefaultMySqlType <T>(MySqlVersion mySqlVersion, MySqlDbType mySqlDbType, string sqlString, byte precision, byte scale)
     where T : Column, new()
 {
     VerifyMySqlType(mySqlVersion, new T(), mySqlDbType, sqlString, precision, scale);
 }
Пример #7
0
 private static void VerifyDefaultMySqlType <T>(MySqlVersion mySqlVersion, MySqlDbType mySqlDbType, int size, string sqlString)
     where T : Column, new()
 {
     VerifyMySqlType(mySqlVersion, new T(), mySqlDbType, size, sqlString);
 }
Пример #8
0
 private static void VerifyDbExpression(MySqlVersion mySqlVersion, DbExpression expr, string expectedSql)
 {
     VerifyDbExpression(mySqlVersion, expr, expectedSql, out _);
 }
Пример #9
0
        private static void GenerateCheckConstraint(IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, DbCheckConstraint constraint)
        {
            Debug.Assert(constraint != null);

            sqlBuilder.Append("CHECK ");
            constraint.LogicalExpression.GenerateSql(sqlBuilder, mySqlVersion);
        }
Пример #10
0
        internal static void GenerateCreateTableSql(this Model model, string tableName, string description, IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, bool isTempTable)
        {
            tableName = tableName.ToQuotedIdentifier();

            if (isTempTable)                                   // Temp table may already exist when connection is reused with connection pooling, drop it if exists.
            {
                sqlBuilder.AppendLine("SET @@sql_notes = 0;"); // workaround: DROP TABLE IF EXISTS causes warning if table does not exist (https://bugs.mysql.com/bug.php?id=2839)
                sqlBuilder.Append("DROP TEMPORARY TABLE IF EXISTS ").Append(tableName).AppendLine(";");
                sqlBuilder.AppendLine("SET @@sql_notes = 1;").AppendLine();
            }

            sqlBuilder.Append("CREATE ");
            if (isTempTable)
            {
                sqlBuilder.Append("TEMPORARY ");
            }
            sqlBuilder.Append("TABLE ").Append(tableName).AppendLine(" (");
            sqlBuilder.IndentLevel++;

            var columns = model.GetColumns();

            for (int i = 0; i < columns.Count; i++)
            {
                var column = columns[i];
                column.GenerateColumnDefinitionSql(sqlBuilder, mySqlVersion, tableName, isTempTable, i < columns.Count - 1 || model.HasConstraintOrIndex(isTempTable));
            }

            int countConstraints = model.GenerateConstraints(sqlBuilder, mySqlVersion, tableName, isTempTable);
            int countIndexes     = model.GenerateIndexes(sqlBuilder, mySqlVersion, tableName, isTempTable, countConstraints > 0);

            if (countConstraints + countIndexes > 0)
            {
                sqlBuilder.AppendLine();
            }

            sqlBuilder.IndentLevel--;
            sqlBuilder.Append(")");

            if (!isTempTable)
            {
                sqlBuilder.GenerateComment(description);
            }

            sqlBuilder.AppendLine(";");
        }
Пример #11
0
        private static int GenerateConstraints(this Model model, IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, string tableName, bool isTempTable)
        {
            IReadOnlyList <DbTableConstraint> constraints = model.GetAddons <DbTableConstraint>();

            if (isTempTable)
            {
                constraints = constraints.Where(x => x.IsValidOnTempTable).ToList();
            }
            else
            {
                constraints = constraints.Where(x => x.IsValidOnTable).ToList();
            }
            if (constraints.Count == 0)
            {
                return(0);
            }

            sqlBuilder.AppendLine();
            for (int i = 0; i < constraints.Count; i++)
            {
                if (i > 0)
                {
                    sqlBuilder.AppendLine(",");
                }
                var constraint = constraints[i];
                sqlBuilder.GenerateConstraintName(constraint.Name, tableName, isTempTable);
                if (constraint is DbPrimaryKey)
                {
                    GeneratePrimaryKeyConstraint(sqlBuilder, (DbPrimaryKey)constraint);
                }
                else if (constraint is DbUniqueConstraint)
                {
                    GenerateUniqueConstraint(sqlBuilder, (DbUniqueConstraint)constraint);
                }
                else if (constraint is DbCheckConstraint)
                {
                    GenerateCheckConstraint(sqlBuilder, mySqlVersion, (DbCheckConstraint)constraint);
                }
                else if (constraint is DbForeignKeyConstraint)
                {
                    GenerateForeignKeyConstraint(sqlBuilder, (DbForeignKeyConstraint)constraint);
                }
                else
                {
                    throw new NotSupportedException(DiagnosticMessages.ConstraintTypeNotSupported(constraint.GetType()));
                }
            }
            return(constraints.Count);
        }
Пример #12
0
 internal static void GenerateCreateTableSql(this Model model, IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, bool isTempTable)
 {
     model.GenerateCreateTableSql(model.GetDbTableName(), model.GetDbTableDescription(), sqlBuilder, mySqlVersion, isTempTable);
 }
Пример #13
0
        private static void GenerateColumnDefinitionSql(this Column column, IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, string tableName, bool isTempTable, bool addComma)
        {
            var columnName = column.DbColumnName.ToQuotedIdentifier();

            sqlBuilder.Append(columnName).Append(' ');
            column.GetMySqlType().GenerateColumnDefinitionSql(sqlBuilder, tableName, isTempTable, mySqlVersion);
            if (addComma)
            {
                sqlBuilder.Append(",");
            }
            sqlBuilder.AppendLine();
        }
Пример #14
0
        private static void GetDebugInfo(this MySqlParameter mySqlParameter, out MySqlType mySqlType, out object value, out MySqlVersion mySqlVersion)
        {
            Tuple <MySqlType, object, MySqlVersion> result = null;

            s_debugInfo.TryGetValue(mySqlParameter, out result);
            mySqlType    = result.Item1;
            value        = result.Item2;
            mySqlVersion = result.Item3;
        }
Пример #15
0
 internal static void SetDebugInfo(this MySqlParameter mySqlParameter, MySqlType mySqlType, object value, MySqlVersion mySqlVersion)
 {
     Debug.Assert(mySqlParameter != null);
     Debug.Assert(mySqlType != null);
     s_debugInfo.Add(mySqlParameter, new Tuple <MySqlType, object, MySqlVersion>(mySqlType, value, mySqlVersion));
 }
Пример #16
0
        private static void VerifyMySqlType(MySqlVersion mySqlVersion, Column column, MySqlDbType mySqlDbType, int size, string sqlString)
        {
            var expectedParamInfo = new MySqlParameterInfo(mySqlDbType, size, default(byte?), default(byte?));

            VerifyMySqlType(mySqlVersion, column, expectedParamInfo, sqlString);
        }
Пример #17
0
        private static void VerifyMySqlType(MySqlVersion mySqlVersion, Column column, MySqlDbType mySqlDbType, string sqlString, byte precision, byte scale)
        {
            var expectedParamInfo = new MySqlParameterInfo(mySqlDbType, default(int?), precision, scale);

            VerifyMySqlType(mySqlVersion, column, expectedParamInfo, sqlString);
        }
Пример #18
0
        private static int GenerateIndexes(this Model model, IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion, string tableName, bool isTempTable, bool hasConstraint)
        {
            IReadOnlyList <DbIndex> indexes = model.GetAddons <DbIndex>();

            if (isTempTable)
            {
                indexes = indexes.Where(x => x.IsValidOnTempTable).ToList();
            }
            else
            {
                indexes = indexes.Where(x => x.IsValidOnTable).ToList();
            }
            if (indexes.Count == 0)
            {
                return(0);
            }

            for (int i = 0; i < indexes.Count; i++)
            {
                if (hasConstraint || i > 0)
                {
                    sqlBuilder.AppendLine(",");
                }
                else
                {
                    sqlBuilder.AppendLine();
                }
                var index = indexes[i];

                if (index.IsClustered)
                {
                    sqlBuilder.GenerateConstraintName(index.Name, tableName, isTempTable);
                    GeneratePrimaryKeyConstraint(sqlBuilder, index.Columns);
                }
                else if (index.IsUnique)
                {
                    sqlBuilder.GenerateConstraintName(index.Name, tableName, isTempTable);
                    GenerateUniqueConstraint(sqlBuilder, index.Columns);
                }
                else
                {
                    sqlBuilder.Append("INDEX ").Append(index.Name.FormatName(tableName)).Append(" (");
                    GenerateColumnSortList(sqlBuilder, index.Columns);
                    sqlBuilder.Append(')');
                }
            }
            return(indexes.Count);
        }
Пример #19
0
        public static void GenerateSql(this DbExpression expression, IndentedStringBuilder sqlBuilder, MySqlVersion mySqlVersion)
        {
            var generator = new ExpressionGenerator()
            {
                SqlBuilder   = sqlBuilder,
                MySqlVersion = mySqlVersion
            };

            expression.Accept(generator);
        }
Пример #20
0
 internal static MySqlDbType GetMySqlDbType(this Column column, MySqlVersion mySqlVersion)
 {
     return(column.GetMySqlType().GetSqlParameterInfo(mySqlVersion).MySqlDbType);
 }