Пример #1
0
        public static IDictionary <string, long> CountRecords(this IDAC dac, IEnumerable <string> tableNames = null)
        {
            var result = new Dictionary <string, long>();

            if (tableNames == null)
            {
                tableNames = dac.GetSchemaCached().Tables.Select(table => table.Name);
            }

            if (!tableNames.Any())
            {
                return(result);
            }

            var sqlBuilder = dac.CreateSQLBuilder();

            foreach (var table in tableNames.WithDescriptions())
            {
                if (table.Index > 0)
                {
                    sqlBuilder.NewLine().Emit("UNION ALL").NewLine();
                }
                sqlBuilder.Emit("SELECT '{0}', COUNT(1) FROM {1}", table.Item, SQLBuilderCommand.TableName(table.Item));
            }

            dac
            .ExecuteQuery(((Object)sqlBuilder).ToString())
            .Rows
            .Cast <DataRow>()
            .ForEach(row => result.Add(row.Get <string>(0), row.Get <long>(1)));
            return(result);
        }
        public override ISQLBuilder AssignVariable(string variableName, object value)
        {
            if (!_variableDeclarations.ContainsKey(variableName))
            {
                throw new SoftwareException("A variable with name '{0}' has not been declared", variableName);
            }

            var variable             = _variableDeclarations[variableName];
            var variableID           = variable.Item1;
            var variableStorageClass = variable.Item2;


            Update(
                this.QuickString("{0}", SQLBuilderCommand.TableName(VariableTableName, TableType.Temporary)),
                new[] {
                new ColumnValue("IntValue", variableStorageClass == VariableStorageClass.Integer ? value : null),
                new ColumnValue("TextValue", variableStorageClass == VariableStorageClass.Text ? value : null),
                new ColumnValue("DateValue", variableStorageClass == VariableStorageClass.DateTime ? value : null),
                new ColumnValue("UuidValue", variableStorageClass == VariableStorageClass.Uuid ? value : null),
                new ColumnValue("BlobValue", variableStorageClass == VariableStorageClass.Blob ? value : null)
            },
                matchColumns:
                new[] {
                new ColumnValue("ID", variableID)
            }
                );

            return(this);
        }
Пример #3
0
 public static long GetMaxID(this IDAC dac, string tableName, string idColName)
 {
     return(dac.ExecuteScalar <long>(
                dac.QuickString(
                    "SELECT MAX(T.ID) FROM (SELECT {0} ID FROM {1} WHERE ID IS NOT NULL UNION SELECT 0 ID ) T",
                    SQLBuilderCommand.ColumnName(idColName),
                    SQLBuilderCommand.TableName(tableName)
                    )
                ));
 }
        public override ISQLBuilder VariableName(string variableName)
        {
            if (!_variableDeclarations.ContainsKey(variableName))
            {
                throw new SoftwareException("A variable with name '{0}' has not been declared", variableName);
            }

            var variable             = _variableDeclarations[variableName];
            var variableID           = variable.Item1;
            var variableStorageClass = variable.Item2;

            string column;

            switch (variableStorageClass)
            {
            case VariableStorageClass.Integer:
                column = "IntValue";
                break;

            case VariableStorageClass.DateTime:
                column = "DateValue";
                break;

            case VariableStorageClass.Text:
                column = "TextValue";
                break;

            case VariableStorageClass.Uuid:
                column = "UuidValue";
                break;

            case VariableStorageClass.Blob:
                column = "BlobValue";
                break;

            default:
                throw new NotImplementedException("Unknown VariableStorageClass " + variableStorageClass.ToString());
            }

            return
                (Emit(
                     "(SELECT {0} FROM {1} WHERE {2} = {3})",
                     SQLBuilderCommand.ColumnName(column),
                     SQLBuilderCommand.TableName(VariableTableName, TableType.Temporary),
                     SQLBuilderCommand.ColumnName("ID"),
                     SQLBuilderCommand.Literal(variableID)
                     ));
        }
        public override ISQLBuilder DeclareVariable(string variableName, Type type)
        {
            if (_variableDeclarations.ContainsKey(variableName))
            {
                throw new SoftwareException("A variable with name '{0}' has already been declared", variableName);
            }

            if (!VariableTableHasBeenCreated)
            {
                CreateVariableTempTable(VariableTableName);
            }

            if (VariableDeclarationCount == 0)
            {
                Emit("DELETE FROM ").TableName(VariableTableName, TableType.Temporary).EndOfStatement();
            }

            var variableID = VariableDeclarationCount++;

            Insert(this.QuickString("{0}", SQLBuilderCommand.TableName(VariableTableName, TableType.Temporary)), new[] { new ColumnValue("ID", variableID) });
            _variableDeclarations.Add(variableName, Tuple.Create(variableID, TypeToStorageClass(type)));
            return(this);
        }
Пример #6
0
 public virtual string FormatTableName(string tableName, TableType tableType)
 {
     return(this.QuickString("{0}", SQLBuilderCommand.TableName(tableName)));
 }