Ejemplo n.º 1
0
        public static string FormatCreateTableCommand(IDatabaseDetails details, string tableName, string autoIncrementName, ICollection <KeyValuePair <string, int> > columns)
        {
            var sql = new StringBuilder()
                      .Append("CREATE TABLE ")
                      .Append(details.QuoteName(tableName))
                      .Append(" (");

            var colSpecs = new List <string>(1 + columns.Count);

            if (!string.IsNullOrEmpty(autoIncrementName))
            {
                colSpecs
                .Add($"{details.QuoteName(autoIncrementName)} {details.AutoIncrementSqlType}");
            }

            colSpecs
            .AddRange(columns.Select(pair => details.QuoteName(pair.Key) + " "
                                     + details.GetSqlTypeFromRank(pair.Value)));

            sql
            .Append(string.Join(", ", colSpecs))
            .Append(")");

            var postfix = details.GetCreateTableStatementPostfix();

            if (!string.IsNullOrEmpty(postfix))
            {
                sql.Append(" ").Append(postfix);
            }

            return(sql.ToString());
        }
Ejemplo n.º 2
0
        public static void FixLongToDoubleUpgrade(IDatabaseDetails details, IDatabaseAccess db, string tableName, IDictionary <string, int> oldColumns, IDictionary <string, int> changedColumns, int longRank, int doubleRank, int safeRank)
        {
            var names           = new List <string>(changedColumns.Keys);
            var quotedTableName = details.QuoteName(tableName);

            foreach (var name in names)
            {
                var oldRank = oldColumns[name];
                var newRank = changedColumns[name];

                if (oldRank != longRank || newRank != doubleRank)
                {
                    continue;
                }

                var quotedName = details.QuoteName(name);
                var min        = db.Cell <long>(false, "SELECT MIN(" + quotedName + ") FROM " + quotedTableName);
                var max        = db.Cell <long>(false, "SELECT MAX(" + quotedName + ") FROM " + quotedTableName);

                if (!min.IsInt53Range() || !max.IsInt53Range())
                {
                    changedColumns[name] = safeRank;
                }
            }
        }
Ejemplo n.º 3
0
        string FormatSelectQuery(string kind, string expr, bool countOnly = false)
        {
            var sql = "select " + (countOnly ? "count(*)" : "*") + " from " + _details.QuoteName(kind);

            if (!String.IsNullOrEmpty(expr))
            {
                sql += " " + expr;
            }
            return(sql);
        }
Ejemplo n.º 4
0
        private string FormatSelectQuery(string kind, string expr, bool countOnly = false)
        {
            var sql = "SELECT " + (countOnly ? "COUNT(*)" : "*") + " FROM " + _details.QuoteName(kind);

            if (!string.IsNullOrEmpty(expr))
            {
                sql += " " + expr;
            }

            return(sql);
        }
Ejemplo n.º 5
0
        public static string FormatInsertCommand(IDatabaseDetails details, string tableName, ICollection <string> fieldNames, string valuesPrefix = null, string defaultsExpr = "default values", string postfix = null)
        {
            var builder = new StringBuilder("INSERT INTO ")
                          .Append(details.QuoteName(tableName))
                          .Append(" ");

            if (fieldNames.Count > 0)
            {
                builder
                .Append("(")
                .Append(string.Join(", ", fieldNames.Select(details.QuoteName)))
                .Append(") ");
            }

            if (!string.IsNullOrEmpty(valuesPrefix))
            {
                builder.Append(valuesPrefix).Append(" ");
            }

            if (fieldNames.Count > 0)
            {
                builder.Append("values (");

                for (var i = 0; i < fieldNames.Count; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }

                    builder.Append("{").Append(i).Append("}");
                }

                builder.Append(")");
            }
            else
            {
                builder.Append(defaultsExpr);
            }

            if (!string.IsNullOrEmpty(postfix))
            {
                builder.Append(" ").Append(postfix);
            }

            return(builder.ToString());
        }
Ejemplo n.º 6
0
 string QuoteName(string name)
 {
     return(_details.QuoteName(name));
 }