Beispiel #1
0
        public void Update()
        {
            if (_master == null)
            {
                _output.Error("The master isn't set, which indicates your arrangement has errors.");
                return;
            }

            var status = _output.GetEntityStatus();

            if (!status.NeedsUpdate())
            {
                return;
            }

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var sql = _queryWriter.Write(status);
                try {
                    var rowCount = cn.Execute(sql, new {
                        TflBatchId       = _output.Entity.BatchId,
                        MasterTflBatchId = _master.BatchId
                    }, null, 0, System.Data.CommandType.Text);
                    _output.Info(rowCount + " updates to master");
                } catch (DbException ex) {
                    _output.Error("error executing: {0}", sql);
                    _output.Error(ex, ex.Message);
                }
            }
        }
Beispiel #2
0
        public void Update()
        {
            var status = _c.GetEntityStatus();

            if (!status.NeedsUpdate())
            {
                return;
            }

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var select = SelectStatement(status);
                var update = UpdateStatement(status);
                var trans  = cn.BeginTransaction();
                try {
                    foreach (var batch in Read(cn, select).Partition(_master.UpdateSize))
                    {
                        var expanded = batch.ToArray();
                        cn.Execute(update, expanded, trans);
                    }
                    trans.Commit();
                } catch (Exception ex) {
                    _c.Error("error executing: {0} {1}", select, ex.Message);
                    _c.Error(ex, ex.Message);
                    _c.Warn("rolling back");
                    trans.Rollback();
                }
            }
        }
Beispiel #3
0
        public object GetMaxVersion()
        {
            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            var    version = _context.Entity.GetVersionField();
            string sql;
            string objectName;

            switch (_cf.AdoProvider)
            {
            case AdoProvider.PostgreSql:
                objectName = _cf.Enclose(_context.Entity.OutputViewName(_context.Process.Name));
                break;

            case AdoProvider.SqlCe:
                objectName = _cf.Enclose(_context.Entity.OutputTableName(_context.Process.Name));
                break;

            default:
                objectName = _cf.Enclose(_context.Entity.OutputViewName(_context.Process.Name));
                break;
            }

            switch (_cf.AdoProvider)
            {
            case AdoProvider.PostgreSql:
                sql = _context.Entity.Delete ? $"SELECT {_cf.Enclose(version.Alias)} FROM {objectName} WHERE {_cf.Enclose(Constants.TflDeleted)} = false ORDER BY {_cf.Enclose(version.Alias)} DESC LIMIT 1;" : $"SELECT {_cf.Enclose(version.Alias)} FROM {objectName} ORDER BY {_cf.Enclose(version.Alias)} DESC LIMIT 1;";
                break;

            case AdoProvider.SqlCe:
                sql = _context.Entity.Delete ? $"SELECT MAX({_cf.Enclose(version.FieldName())}) FROM {objectName} WHERE {_cf.Enclose(_context.Entity.TflDeleted().FieldName())} = 0;" : $"SELECT MAX({_cf.Enclose(version.FieldName())}) FROM {objectName};";
                break;

            default:
                sql = _context.Entity.Delete ? $"SELECT MAX({_cf.Enclose(version.Alias)}) FROM {objectName} WHERE {_cf.Enclose(Constants.TflDeleted)} = 0;" : $"SELECT MAX({_cf.Enclose(version.Alias)}) FROM {objectName};";
                break;
            }

            _context.Debug(() => $"Loading Output Version: {sql}");

            try {
                if (_cn.State != ConnectionState.Open)
                {
                    _cn.Open();
                }
                var result = _cn.ExecuteScalar(sql, commandTimeout: _context.Connection.RequestTimeout);
                return(result == DBNull.Value ? null : result);
            } catch (System.Data.Common.DbException ex) {
                _context.Error($"Error retrieving maximum version from {_context.Connection.Name}, {objectName}.");
                _context.Error(ex.Message);
                _context.Debug(() => ex.StackTrace);
                _context.Debug(() => sql);
                return(null);
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            if (!_output.Entity.NeedsUpdate())
            {
                return;
            }

            var ids = new SSASIdentifiers(_input, _output);

            using (var server = new Server()) {
                server.Connect($"Data Source={_output.Connection.Server};Catalog=;");

                if (server.Databases.Contains(ids.DatabaseId))
                {
                    var database = server.Databases.Find(ids.DatabaseId);
                    if (_output.Process.Mode == "init")
                    {
                        _output.Info($"Processing OLAP database {ids.DatabaseId}");
                        SSAS.Process(database, ProcessType.ProcessFull, _output);
                    }
                    else
                    {
                        if (database.Dimensions.Contains(_output.Entity.Alias))
                        {
                            _output.Info($"Updating dimension {_output.Entity.Alias}");
                            var dimension = database.Dimensions.Find(_output.Entity.Alias);
                            SSAS.Process(dimension, ProcessType.ProcessUpdate, _output);
                        }
                        else
                        {
                            _output.Error($"{_output.Entity.Alias} dimension does not exist!");
                        }
                        if (database.Cubes.Contains(ids.CubeId))
                        {
                            var cube = database.Cubes.Find(ids.CubeId);
                            ProcessPartition(cube, ids.NormalMeasureGroupId);
                            ProcessPartition(cube, ids.DistinctMeasureGroupId);
                        }
                        else
                        {
                            _output.Error($"{ids.CubeId} cube does not exist!");
                        }
                    }
                }
                else
                {
                    _output.Error($"{ids.DatabaseId} OLAP database does not exist!");
                }
                server.Disconnect();
            }
        }
Beispiel #5
0
        private void Create(IDbConnection cn)
        {
            var createSql = _context.SqlCreateOutput(_cf);

            try {
                cn.Execute(createSql);
            } catch (DbException ex) {
                _context.Error($"Could not create output {_context.Entity.OutputTableName(_context.Process.Name)}");
                _context.Error(ex, ex.Message);
            }

            try {
                var createIndex = _context.SqlCreateOutputUniqueIndex(_cf);
                cn.Execute(createIndex);
            } catch (DbException ex) {
                _context.Error($"Could not create unique index on output {_context.Entity.OutputTableName(_context.Process.Name)}");
                _context.Error(ex, ex.Message);
            }

            if (_cf.AdoProvider == AdoProvider.SqlCe)
            {
                return;
            }

            try {
                var createView = _context.SqlCreateOutputView(_cf);
                cn.Execute(createView);
            } catch (DbException ex) {
                _context.Error($"Could not create output view {_context.Entity.OutputViewName(_context.Process.Name)}");
                _context.Error(ex, ex.Message);
            }
        }
Beispiel #6
0
        public void Write(IEnumerable <IRow> rows)
        {
            _originalConnectionLimit = ServicePointManager.DefaultConnectionLimit;
            ServicePointManager.DefaultConnectionLimit = _context.Connection.MaxDegreeOfParallelism * 2;

            try {
                Parallel.ForEach(rows.Partition(_context.Entity.InsertSize), _options, part => {
                    var docs = new List <Dictionary <string, object> >();
                    foreach (var row in part)
                    {
                        Interlocked.Increment(ref _count);
                        docs.Add(_fields.ToDictionary(field => field.Alias.ToLower(), field => row[field]));
                    }

                    var response = _solr.AddRange(docs);
                    if (response.Status != 0)
                    {
                        _context.Error($"Couldn't add range of {docs.Count} document{docs.Count.Plural()} to SOLR.");
                    }
                });
            } catch (AggregateException ex) {
                foreach (var exception in ex.InnerExceptions)
                {
                    _context.Error(exception.Message);
                    _context.Error(exception.StackTrace);
                }
                return;
            }

            ServicePointManager.DefaultConnectionLimit = _originalConnectionLimit;

            if (_count > 0)
            {
                try {
                    var commit = _solr.Commit();
                    if (commit.Status == 0)
                    {
                        _context.Entity.Inserts += Convert.ToUInt32(_count);
                        _context.Info($"Committed {_count} documents in {TimeSpan.FromMilliseconds(commit.QTime)}");
                    }
                    else
                    {
                        _context.Error($"Failed to commit {_count} documents. SOLR returned status {commit.Status}.");
                    }
                } catch (SolrNetException ex) {
                    _context.Error($"Failed to commit {_count} documents. {ex.Message}");
                }
            }
        }
        public void Delete()
        {
            // delete query will be different for each provider, currently that's handled in extensions on OutputContext
            var sql = _context.SqlDeleteOutputCrossDatabase(_cf, _context.Entity.BatchId);

            using (var cn = _cf.GetConnection(Constants.ApplicationName)) {
                cn.Open();
                try {
                    _context.Entity.Deletes = System.Convert.ToUInt32(cn.Execute(sql));
                } catch (DbException ex) {
                    _context.Error("Unable to perform cross-database delete!");
                    _context.Error(ex.Message);
                }
            }
        }
Beispiel #8
0
        public void Write(IEnumerable <IRow> rows)
        {
            var fullCount = 0;

            foreach (var part in rows.Partition(_context.Entity.InsertSize))
            {
                var batchCount = 0;
                foreach (var row in part)
                {
                    var doc = _documents[batchCount];
                    foreach (var field in _fields)
                    {
                        doc[field.Alias] = row[field];
                    }
                    batchCount++;
                    fullCount++;
                }
                var response = _solr.AddRange(_documents.Take(batchCount));

                if (response.Status == 0)
                {
                    var count = batchCount;
                    _context.Debug(() => $"{count} to output");
                }
                else
                {
                    _context.Error($"Couldn't add range of documents to SOLR.");
                }
            }

            if (fullCount > 0)
            {
                try {
                    var commit = _solr.Commit();
                    if (commit.Status == 0)
                    {
                        _context.Entity.Inserts += System.Convert.ToUInt32(fullCount);
                        _context.Info($"Committed {fullCount} documents in {TimeSpan.FromMilliseconds(commit.QTime)}");
                    }
                    else
                    {
                        _context.Error($"Failed to commit {fullCount} documents.  SOLR returned status {commit.Status}.");
                    }
                } catch (SolrNetException ex) {
                    _context.Error($"Failed to commit {fullCount} documents. {ex.Message}");
                }
            }
        }
Beispiel #9
0
        public void Write(IEnumerable <IRow> rows)
        {
            foreach (var row in rows)
            {
                var message = (string)row[_message] ?? string.Empty;
                switch (row[_level].ToString().ToLower())
                {
                case "warn":
                case "warning":
                    _context.Warn(message);
                    break;

                case "error":
                    _context.Error(message);
                    break;

                case "debug":
                    _context.Debug(() => message);
                    break;

                default:
                    _context.Info(message);
                    break;
                }
            }
        }
Beispiel #10
0
        private void Insert(SqlBulkCopy bulkCopy, DataTable dt, IEnumerable <IRow> inserts)
        {
            var enumerated = inserts.ToArray();

            if (enumerated.Length == 0)
            {
                return;
            }

            // convert to data rows for bulk copy
            var rows = new List <DataRow>();

            foreach (var insert in enumerated)
            {
                var row = dt.NewRow();
                row.ItemArray = insert.ToEnumerable(_output.OutputFields).ToArray();
                rows.Add(row);
            }

            try {
                bulkCopy.WriteToServer(rows.ToArray());
                _output.Entity.Inserts += Convert.ToUInt32(enumerated.Length);
            } catch (Exception ex) {
                _output.Error(ex.Message);
            }
        }
Beispiel #11
0
        public object Detect()
        {
            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            var    version = _context.Entity.GetVersionField();
            string sql;

            switch (_cf.AdoProvider)
            {
            case AdoProvider.PostgreSql:
                sql = $"SELECT {_cf.Enclose(version.Alias)} FROM {_cf.Enclose(_context.Entity.OutputViewName(_context.Process.Name))} WHERE {_cf.Enclose(Constants.TflDeleted)} = false ORDER BY {_cf.Enclose(version.Alias)} DESC LIMIT 1;";
                break;

            default:
                sql = $"SELECT MAX({_cf.Enclose(version.Alias)}) FROM {_cf.Enclose(_context.Entity.OutputViewName(_context.Process.Name))} WHERE {_cf.Enclose(Constants.TflDeleted)} = 0;";
                break;
            }

            _context.Debug(() => $"Loading Output Version: {sql}");

            try {
                using (var cn = _cf.GetConnection()) {
                    cn.Open();
                    return(cn.ExecuteScalar(sql));
                }
            } catch (Exception ex) {
                _context.Error(ex, ex.Message + " " + sql);
                throw;
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            var sql  = _context.SqlUpdateCalculatedFields(_parent, _cf);
            var temp = new List <Field> {
                _context.Entity.TflKey()
            };

            temp.AddRange(_context.Entity.CalculatedFields.Where(f => f.Output));
            var fields = temp.ToArray();

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var trans = cn.BeginTransaction();
                try {
                    foreach (var batch in rows.Partition(_context.Entity.UpdateSize))
                    {
                        var data       = batch.Select(r => r.ToExpandoObject(fields));
                        var batchCount = Convert.ToUInt32(cn.Execute(sql, data, trans, 0, CommandType.Text));
                        _context.Increment(batchCount);
                    }
                    trans.Commit();
                } catch (Exception ex) {
                    _context.Error(ex, ex.Message);
                    trans.Rollback();
                }
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            _output.Entity.UpdateCommand = _output.SqlUpdateOutput(_cf);
            var count = (uint)0;

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                _output.Debug(() => "begin transaction");
                var trans = cn.BeginTransaction();
                try {
                    foreach (var batch in rows.Partition(_output.Entity.UpdateSize))
                    {
                        var batchCount = Convert.ToUInt32(cn.Execute(
                                                              _output.Entity.UpdateCommand,
                                                              batch.Select(r => r.ToExpandoObject(_output.GetUpdateFields().ToArray())),
                                                              trans,
                                                              0,
                                                              CommandType.Text
                                                              ));
                        count += batchCount;
                    }
                    _output.Debug(() => "commit transaction");
                    trans.Commit();
                    _output.Entity.Updates += count;
                    _output.Info("{0} to {1}", count, _output.Connection.Name);
                } catch (Exception ex) {
                    _output.Error(ex.Message);
                    _output.Warn("rollback transaction");
                    trans.Rollback();
                }
            }
        }
Beispiel #14
0
        public void Write(IEnumerable <IRow> rows)
        {
            var sql   = _output.SqlInsertIntoOutput(_cf);
            var count = 0;

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var trans = cn.BeginTransaction();

                try {
                    foreach (var batch in rows.Partition(_output.Entity.InsertSize))
                    {
                        var batchCount = cn.Execute(
                            sql,
                            batch.Select(r => r.ToExpandoObject(_output.OutputFields)),
                            trans,
                            0,
                            CommandType.Text
                            );
                        count += batchCount;
                        _output.Increment(batchCount);
                    }
                    trans.Commit();
                } catch (Exception ex) {
                    _output.Error(ex, ex.Message);
                    trans.Rollback();
                }
                _output.Debug(() => $"{count} to {_output.Connection.Name}");
            }
            _output.Entity.Inserts += count;
        }
Beispiel #15
0
        public void Write(IEnumerable <IRow> rows)
        {
            var fullCount  = 0;
            var batchCount = (uint)0;

            foreach (var part in rows.Partition(_context.Entity.InsertSize))
            {
                var docs = new List <Dictionary <string, object> >();
                foreach (var row in part)
                {
                    batchCount++;
                    fullCount++;
                    docs.Add(_fields.ToDictionary(field => field.Alias.ToLower(), field => row[field]));
                }
                var response = _solr.AddRange(docs);

                _context.Increment(@by: batchCount);
                if (response.Status == 0)
                {
                    _context.Debug(() => $"{batchCount} to output");
                }
                else
                {
                    _context.Error("ah!");
                }
                batchCount = 0;
            }

            _solr.Commit();

            if (fullCount > 0)
            {
                _context.Info($"{fullCount} to output");
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            var sql    = _context.SqlUpdateCalculatedFields(_parent, _cf);
            var fields = _context.GetUpdateCalculatedFields().ToArray();

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var trans = cn.BeginTransaction();
                try {
                    foreach (var batch in rows.Partition(_context.Entity.UpdateSize))
                    {
                        _context.Debug(() => "got a batch!");
                        var data = batch.Select(r => r.ToExpandoObject(fields));
                        _context.Debug(() => "converted to expando object");
                        var batchCount = Convert.ToUInt32(cn.Execute(sql, data, trans, 0, CommandType.Text));
                        _context.Debug(() => $"Updated {batchCount} calculated field records!");
                    }
                    trans.Commit();
                    _context.Debug(() => "Committed updates.");
                } catch (Exception ex) {
                    _context.Error(ex, ex.Message);
                    trans.Rollback();
                }
            }
        }
Beispiel #17
0
        public static bool Save(Microsoft.AnalysisServices.Server server, OutputContext output, IMajorObject obj)
        {
            var builder = new StringBuilder();

            using (var xmlWriter = XmlWriter.Create(builder, new XmlWriterSettings {
                OmitXmlDeclaration = true
            })) {
                Scripter.WriteAlter(xmlWriter, obj, true, true);
                xmlWriter.Flush();
            }

            var command = builder.ToString();

            output.Debug(() => command);
            var results = server.Execute(command);

            if (results.Count > 0)
            {
                foreach (XmlaResult result in results)
                {
                    if (result.Messages.Count > 0)
                    {
                        foreach (XmlaMessage message in result.Messages)
                        {
                            output.Error(message.Description);
                        }
                        return(false);
                    }
                }
            }
            return(true);
        }
Beispiel #18
0
        private void Insert(IEnumerable <IRow> inserts, SqlCeConnection cn, string table)
        {
            var enumerated = inserts.ToArray();

            if (enumerated.Length == 0)
            {
                return;
            }


            try {
                using (var cmd = new SqlCeCommand(table, cn, null)) {
                    cmd.CommandType = CommandType.TableDirect;
                    using (var rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable)) {
                        foreach (var row in enumerated)
                        {
                            var rec = rs.CreateRecord();
                            for (var i = 0; i < _output.OutputFields.Length; i++)
                            {
                                var field = _output.OutputFields[i];
                                rec.SetValue(i, row[field]);
                            }
                            rs.Insert(rec);
                        }
                    }
                }

                _output.Entity.Inserts += Convert.ToUInt32(enumerated.Length);
            } catch (Exception ex) {
                _output.Error(ex.Message);
            }
        }
        public void Update()
        {
            var status = _c.GetEntityStatus();

            if (!status.NeedsUpdate())
            {
                return;
            }

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var select = SelectStatement(status);
                var update = UpdateStatement(status);
                var trans  = cn.BeginTransaction();
                try {
                    var rows = new List <ExpandoObject>();
                    using (var reader = cn.ExecuteReader(select, new { TflBatchId = _c.Entity.BatchId, MasterTflBatchId = _master.BatchId }, trans, 0, CommandType.Text)) {
                        while (reader.Read())
                        {
                            var obj  = new ExpandoObject();
                            var dict = (IDictionary <string, object>)obj;
                            for (var i = 0; i < reader.FieldCount; i++)
                            {
                                dict[reader.GetName(i)] = reader.GetValue(i);
                            }
                            dict[_master.TflBatchId().FieldName()] = _c.Entity.BatchId;
                            rows.Add(obj);
                        }
                    }
                    if (rows.Any())
                    {
                        foreach (var batch in rows.Partition(_master.UpdateSize))
                        {
                            var expanded = batch.ToArray();
                            cn.Execute(update, expanded, trans);
                            _c.Increment(expanded.Length);
                        }
                    }
                    trans.Commit();
                } catch (Exception ex) {
                    _c.Error("error executing: {0} {1}", select, ex.Message);
                    _c.Error(ex, ex.Message);
                    _c.Warn("rolling back");
                    trans.Rollback();
                }
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            var builder    = new StringBuilder();
            var fullCount  = 0;
            var batchCount = (uint)0;

            foreach (var part in rows.Partition(_context.Entity.InsertSize))
            {
                foreach (var row in part)
                {
                    batchCount++;
                    fullCount++;
                    foreach (var field in _context.OutputFields)
                    {
                        switch (field.Type)
                        {
                        case "guid":
                            row[field] = ((Guid)row[field]).ToString();
                            break;

                        case "datetime":
                            row[field] = ((DateTime)row[field]).ToString("o");
                            break;
                        }
                        if (field.SearchType == "geo_point")
                        {
                            row[field] = new Dictionary <string, string> {
                                { "text", row[field].ToString() },
                                { "location", row[field].ToString() }
                            };
                        }
                    }

                    builder.Append(_prefix);
                    foreach (var key in _context.OutputFields.Where(f => f.PrimaryKey))
                    {
                        builder.Append(row[key]);
                    }
                    builder.AppendLine("\"}}");
                    builder.AppendLine(JsonConvert.SerializeObject(_context.OutputFields.ToDictionary(f => f.Alias.ToLower(), f => row[f])));
                }
                var response = _client.Bulk <DynamicResponse>(builder.ToString(), nv => nv
                                                              .AddQueryString("refresh", @"true")
                                                              );
                _context.Increment(@by: batchCount);
                if (response.Success)
                {
                    _context.Debug(() => $"{batchCount} to output");
                }
                else
                {
                    _context.Error(response.OriginalException.Message);
                }
                builder.Clear();
                batchCount = 0;
            }

            _context.Info($"{fullCount} to output");
        }
        public object GetMaxVersion()
        {
            // TODO: Consider tlfdeleted = 0

            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            var version = _context.Entity.GetVersionField();

            _context.Debug(() => $"Detecting Max Output Version: {_context.Connection.Index}.{_context.TypeName()}.{version.Alias.ToLower()}.");

            var body = new {
                aggs = new {
                    version = new {
                        max = new {
                            field = version.Alias.ToLower()
                        }
                    }
                },
                size = 0
            };

            var     result = _client.Search <DynamicResponse>(_context.Connection.Index, _context.TypeName(), new PostData <object>(body));
            dynamic value  = null;

            if (result.Success)
            {
                try {
                    value = result.Body["aggregations"]["version"]["value"].Value;
                } catch (Exception ex) {
                    _context.Error(ex, ex.Message);
                }
            }
            else
            {
                _context.Error(result.ServerError.ToString());
                _context.Debug(() => result.DebugInformation);
            }
            var converted = value ?? null;

            _context.Debug(() => $"Found value: {converted ?? "null"}");
            return(converted);
        }
        public void Write(IEnumerable <IRow> rows)
        {
            var fullCount = 0;

            foreach (var part in rows.Partition(_context.Entity.InsertSize))
            {
                var batchCount = (uint)0;
                var docs       = new List <Dictionary <string, object> >();
                foreach (var row in part)
                {
                    batchCount++;
                    fullCount++;
                    docs.Add(_fields.ToDictionary(field => field.Alias.ToLower(), field => row[field]));
                }
                var response = _solr.AddRange(docs);

                if (response.Status == 0)
                {
                    var count = batchCount;
                    _context.Debug(() => $"{count} to output");
                }
                else
                {
                    _context.Error($"Couldn't add range of {docs.Count} document{docs.Count.Plural()} to SOLR.");
                }
            }

            if (fullCount > 0)
            {
                try {
                    var commit = _solr.Commit();
                    if (commit.Status == 0)
                    {
                        _context.Entity.Inserts += System.Convert.ToUInt32(fullCount);
                        _context.Info($"Committed {fullCount} documents in {TimeSpan.FromMilliseconds(commit.QTime)}");
                    }
                    else
                    {
                        _context.Error($"Failed to commit {fullCount} documents.  SOLR returned status {commit.Status}.");
                    }
                } catch (SolrNetException ex) {
                    _context.Error($"Failed to commit {fullCount} documents. {ex.Message}");
                }
            }
        }
Beispiel #23
0
 public void Write(IEnumerable <IRow> rows)
 {
     _writer.Write(rows);
     try {
         System.IO.File.WriteAllText(_context.Connection.File, _writer.Builder.ToString());
     } catch (System.Exception ex) {
         _context.Error(ex.Message);
     }
 }
        public ActionResponse Execute()
        {
            var updateVariables = string.Join(", ", _model.Fields.Where(f => f.Name != _model.KeyLongName).Select(f => $"{_cf.Enclose(f.Alias)} = @{f.FieldName()}"));

            var builder = new StringBuilder();

            builder.AppendLine($"UPDATE {_output.Process.Flat}");
            builder.AppendLine($"SET {updateVariables}");
            builder.AppendLine($"WHERE {_model.EnclosedKeyLongName} = @{_model.KeyShortName};");

            var command = builder.ToString();

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var count = 0;
                var trans = cn.BeginTransaction();
                try {
                    foreach (var batch in Read(cn, _sql, _model).Partition(_model.MasterEntity.UpdateSize))
                    {
                        var expanded = batch.ToArray();
                        cn.Execute(command, expanded, trans);
                        count += expanded.Length;
                        _output.Increment(Convert.ToUInt32(expanded.Length));
                    }
                    trans.Commit();
                    _output.Info($"{count} record{count.Plural()} updated in flat");
                } catch (Exception ex) {
                    _output.Error(ex.Message);
                    _output.Error(_sql);
                    _output.Warn("rolling back");
                    trans.Rollback();
                    return(new ActionResponse(500, ex.Message)
                    {
                        Action = new Configuration.Action {
                            Type = "internal",
                            Description = "Flatten Action",
                            ErrorMode = "abort"
                        }
                    });
                }
            }

            return(new ActionResponse(200, "Ok"));
        }
Beispiel #25
0
        public ActionResponse Execute()
        {
            using (var cn = _cf.GetConnection()) {
                try {
                    cn.Open();
                } catch (DbException ex) {
                    _context.Error($"Couldn't open {_context.Connection}.");
                    _context.Error(ex.Message);
                    return(new ActionResponse(500, ex.Message)
                    {
                        Action = new Configuration.Action {
                            Type = "internal", ErrorMode = "abort"
                        }
                    });
                }

                Destroy(cn);
                Create(cn);
            }
            return(new ActionResponse());
        }
Beispiel #26
0
        public ActionResponse Execute()
        {
            using (var cn = _cf.GetConnection()) {
                try {
                    cn.Open();
                } catch (DbException ex) {
                    _context.Error(ex.Message);
                    return(new ActionResponse(500, $"Could not open {_context.Connection.Name} connection."));
                }

                Destroy(cn);
                Create(cn);
            }
            return(new ActionResponse());
        }
Beispiel #27
0
        public void Write(IEnumerable <IRow> rows)
        {
            var engine = FileHelpersEngineFactory.Create(_context);
            var file   = Path.Combine(_context.Connection.Folder, _context.Connection.File ?? _context.Entity.OutputTableName(_context.Process.Name));

            _context.Debug(() => $"Writing {file}.");

            using (engine.BeginWriteFile(file)) {
                foreach (var row in rows)
                {
                    var i = 0;
                    foreach (var field in _context.OutputFields)
                    {
                        switch (field.Type)
                        {
                        case "byte[]":
                            engine[i] = Convert.ToBase64String((byte[])row[field]);
                            //engine[i] = Utility.BytesToHexString((byte[]) row[field]);
                            //engine[i] = Encoding.UTF8.GetString((byte[])row[field]);
                            break;

                        case "string":
                            engine[i] = row[field];
                            break;

                        case "datetime":
                            engine[i] = field.Format == string.Empty ? ((DateTime)row[field]).ToString("o") : ((DateTime)row[field]).ToString(field.Format);
                            break;

                        default:
                            engine[i] = row[field].ToString();
                            break;
                        }
                        ++i;
                    }

                    engine.WriteNextValues();
                }
                if (engine.ErrorManager.HasErrors)
                {
                    var errorFile = Path.Combine(Path.GetDirectoryName(_context.Connection.File) ?? string.Empty, Path.GetFileNameWithoutExtension(_context.Connection.File) + ".errors.txt");
                    _context.Error($"File writer had {engine.ErrorManager.ErrorCount} error{engine.ErrorManager.ErrorCount.Plural()}. See {errorFile}.");
                    engine.ErrorManager.SaveErrors(errorFile);
                }
            }
        }
        public ActionResponse Execute()
        {
            var         response = new ActionResponse();
            var         database = _output.Connection.Database;
            var         table    = _output.Entity.Alias;
            var         conn     = _factory.Get();
            DmlResponse res;

            if (!conn.Run(Query.DbList()).Contains(database))
            {
                res = conn.Run(Query.DbCreate(database));
                _output.Info($"Created {database}");
            }

            IDatabaseQuery db = Query.Db(database);

            if (conn.Run(db.TableList()).Contains(table))
            {
                res = conn.Run(db.TableDrop(table));
                _output.Info($"Dropped {table}");
            }

            var keys = _output.Entity.GetPrimaryKey();

            if (keys.Count() > 1)
            {
                _output.Error("You can't create a composite primary key in RethinkDB.");
            }
            else
            {
                if (keys.Any())
                {
                    conn.Run(db.TableCreate(table, primaryKey: keys.First().Alias));
                }
                else
                {
                    conn.Run(db.TableCreate(table));
                }
                _output.Info($"Created {table}");
            }

            return(response);
        }
Beispiel #29
0
        public object GetMaxVersion()
        {
            if (string.IsNullOrEmpty(_context.Entity.Version))
            {
                return(null);
            }

            var    version = _context.Entity.GetVersionField();
            string sql;

            switch (_cf.AdoProvider)
            {
            case AdoProvider.PostgreSql:
                sql = $"SELECT {_cf.Enclose(version.Alias)} FROM {_cf.Enclose(_context.Entity.OutputViewName(_context.Process.Name))} WHERE {_cf.Enclose(Constants.TflDeleted)} = false ORDER BY {_cf.Enclose(version.Alias)} DESC LIMIT 1;";
                break;

            case AdoProvider.SqlCe:
                sql = $"SELECT MAX({_cf.Enclose(version.FieldName())}) FROM {_cf.Enclose(_context.Entity.OutputTableName(_context.Process.Name))} WHERE {_cf.Enclose(_context.Entity.TflDeleted().FieldName())} = 0;";
                break;

            default:
                sql = $"SELECT MAX({_cf.Enclose(version.Alias)}) FROM {_cf.Enclose(_context.Entity.OutputViewName(_context.Process.Name))} WHERE {_cf.Enclose(Constants.TflDeleted)} = 0;";
                break;
            }

            _context.Debug(() => $"Loading Output Version: {sql}");

            try {
                if (_cn.State != ConnectionState.Open)
                {
                    _cn.Open();
                }
                return(_cn.ExecuteScalar(sql, commandTimeout: _context.Connection.RequestTimeout));
            } catch (Exception ex) {
                _context.Error(ex, ex.Message + " " + sql);
                throw;
            }
        }
Beispiel #30
0
            public string ReadLine()
            {
                if (IsTestMode)
                {
                    if (_readCount == _readLimit)
                    {
                        _cout.Error("Read limit exeed!");
                    }

                    string result = null;

                    result = _reader.EndOfStream ? null : _reader.ReadLine();

                    if (_readCount == 0)
                    {
                        _cout.Info("First read from StdIn");
                    }

                    _readCount++;
                    if (_readCount == _readLimit)
                    {
                        _cout.Info("Last read from StdIn");
                    }

                    if (string.IsNullOrEmpty(result))
                    {
                        _cout.Warning("Empty result from StdIn");
                    }

                    return(result);
                }
                else
                {
                    return(Console.ReadLine());
                }
            }