public void Drop(AbstractConnection connection, Entity entity) {
            if (!EntityExists.Exists(connection, entity))
                return;

            var sql = string.Format(FORMAT, connection.Enclose(entity.OutputName()));

            using (var cn = connection.GetConnection()) {
                cn.Open();
                cn.Execute(sql);
                connection.Logger.EntityDebug(entity.Name, "Dropped Output {0}", entity.OutputName());
            }
        }
        public override void Create(AbstractConnection connection, Process process, Entity entity) {

            if (EntityExists != null && EntityExists.Exists(connection, entity)) {
                process.Logger.EntityWarn(entity.Name,"Trying to create entity that already exists! {0}", entity.Name);
                return;
            }

            var keyType = entity.IsMaster() ? FieldType.MasterKey : FieldType.PrimaryKey;

            var writer = process.StarEnabled && keyType == FieldType.MasterKey ?
                new FieldSqlWriter(entity.Fields, entity.CalculatedFields, process.CalculatedFields, GetRelationshipFields(process.Relationships, entity)) :
                new FieldSqlWriter(entity.Fields, entity.CalculatedFields);

            var primaryKey = writer.FieldType(keyType).Alias(connection.L, connection.R).Asc().Values();
            var defs = new List<string>();
            defs.AddRange(writer
                .Reload()
                .AddBatchId(entity.Index)
                .AddDeleted(entity)
                .AddSurrogateKey(entity.Index)
                .Output()
                .Alias(connection.L, connection.R)
                .DataType(new SqlServerDataTypeService())
                .AppendIf(" NOT NULL", keyType)
                .Values());

            var rowVersion = entity.Fields.WithSimpleType("rowversion").WithoutInput().WithoutOutput();
            if (rowVersion.Any()) {
                var alias = rowVersion.First().Alias;
                defs.Add(connection.Enclose(alias) + " [ROWVERSION] NOT NULL");
            }

            var createSql = connection.TableQueryWriter.CreateTable(entity.OutputName(), defs);
            _logger.EntityDebug(entity.Name, createSql);

            var indexSql = connection.TableQueryWriter.AddUniqueClusteredIndex(entity.OutputName());
            _logger.EntityDebug(entity.Name, indexSql);

            var keySql = connection.TableQueryWriter.AddPrimaryKey(entity.OutputName(), primaryKey);
            _logger.EntityDebug(entity.Name, keySql);

            using (var cn = connection.GetConnection()) {
                cn.Open();
                cn.Execute(createSql);
                cn.Execute(indexSql);
                cn.Execute(keySql);
                _logger.EntityInfo(entity.Name, "Initialized {0} in {1} on {2}.", entity.OutputName(), connection.Database, connection.Server);
            }
        }
 public SqlEntityDelete(AbstractConnection connection, Entity entity)
     : base(connection) {
     _name = Connection.Enclose(entity.OutputName());
     _isMaster = entity.IsMaster();
     BatchSize = connection.BatchSize;
     UseTransaction = true;
 }
 public bool Exists(AbstractConnection connection, Entity entity) {
     using (var cn = connection.GetConnection()) {
         cn.Open();
         var table = cn.Query<string>(SQL, new { name = entity.OutputName() }).DefaultIfEmpty(string.Empty).FirstOrDefault();
         return !string.IsNullOrEmpty(table);
     }
 }
        public SqlServerBulkLoadOperation(AbstractConnection connection, Entity entity)
            : base(connection, connection.Enclose(entity.OutputName())) {
            _entity = entity;
            _batchSize = connection.BatchSize;
            UseTransaction = false;

            TurnOptionOn(SqlBulkCopyOptions.TableLock);
            TurnOptionOn(SqlBulkCopyOptions.UseInternalTransaction);
            TurnOptionOff(SqlBulkCopyOptions.CheckConstraints);
            TurnOptionOff(SqlBulkCopyOptions.FireTriggers);
            TurnOptionOn(SqlBulkCopyOptions.KeepNulls);
        }
 public int Count(AbstractConnection connection, Entity entity, bool useAlias) {
     if (_connectionChecker == null || _connectionChecker.Check(connection)) {
         if (_entityExists.Exists(connection, entity)) {
             using (var cn = connection.GetConnection()) {
                 cn.Open();
                 var sql = string.Format("SELECT COUNT(*) FROM [{0}].[{1}] WITH (NOLOCK);", entity.Schema, entity.OutputName());
                 var cmd = cn.CreateCommand();
                 cmd.CommandText = sql;
                 return (int)cmd.ExecuteScalar();
             }
         }
     }
     return 0;
 }
        public bool RecordsExist(AbstractConnection connection, Entity entity) {

            if (EntityExists.Exists(connection, entity)) {

                using (var cn = connection.GetConnection()) {
                    cn.Open();
                    var sql = [email protected]"SELECT TOP(1) [{entity.PrimaryKey.First().Alias}] FROM [{entity.OutputName()}];";
                    var cmd = cn.CreateCommand();
                    cmd.CommandText = sql;
                    using (var reader = cmd.ExecuteReader()) {
                        return reader.Read();
                    }
                }
            }
            return false;
        }
        public bool RecordsExist(AbstractConnection connection, Entity entity) {

            if (!EntityExists.Exists(connection, entity))
                return false;

            using (var cn = connection.GetConnection()) {
                cn.Open();
                var exists = cn.Query<bool>(string.Format(@"
                    SELECT EXISTS(
                        SELECT ""{0}"" 
                        FROM ""{1}""
                        LIMIT 1
                    );
                ", entity.PrimaryKey.First().Alias, entity.OutputName())).DefaultIfEmpty(false).First();
                return exists;
            }
        }
        public bool RecordsExist(AbstractConnection connection, Entity entity) {
            
            if (!EntityExists.Exists(connection, entity))
                return false;

            using (var cn = connection.GetConnection()) {
                cn.Open();
                var records = cn.Query<string>(string.Format(@"
                    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
                    SELECT `{0}`
                    FROM `{1}`
                    LIMIT 1;
                    COMMIT;
                ", entity.PrimaryKey.First().Alias, entity.OutputName()));
                return records != null && records.Any();
            }
        }
 public bool Exists(AbstractConnection connection, Entity entity) {
     return new SqlServerTableExists(connection).OutputExists(entity.OutputName());
 }
 public static string Path(AbstractConnection connection, Entity entity) {
     return connection.Folder.TrimEnd(new[] { '\\' }) + "\\" + entity.OutputName().TrimStart(new[] { '\\' });
 }
 public bool Exists(AbstractConnection connection, Entity entity) {
     using (var cn = connection.GetConnection()) {
         cn.Open();
         return cn.Query<bool>(SQL, new { name = entity.OutputName() }).DefaultIfEmpty(false).FirstOrDefault();
     }
 }