Ejemplo n.º 1
0
        public static string[] GetListaAlterCmd(this TipoDatabase tipo, Type type)
        {
            var tabela       = type.GetTabela();
            var listaDeCampo = type.GetCampos();
            var column       = new List <string>();

            var listaDeAlterCmd = new List <string>();

            foreach (var campo in listaDeCampo)
            {
                var dataType = tipo.GetDataType(campo);
                if (dataType == null)
                {
                    continue;
                }

                column.Clear();
                column.Add(campo.Nome);
                column.Add(dataType);
                if (campo.Tipo != CampoTipo.Nul)
                {
                    column.Add("Not Null");
                }

                listaDeAlterCmd.Add(tipo.GetAlterTable(tabela.Nome, column.ToArray()));
            }

            return(listaDeAlterCmd.ToArray());
        }
Ejemplo n.º 2
0
        public static string GetSelectLim(this TipoDatabase tipo, string sql, int qtde, int pagina = 0)
        {
            var regini = pagina * qtde;
            var regfin = regini + qtde;

            switch (tipo)
            {
            case TipoDatabase.Firebird:
                return($"select firts {qtde} skip {regini} * from ({sql})");

            case TipoDatabase.MySql:
            case TipoDatabase.PostgreSql:
                return($"select * from ({sql}) limit {regini}, {qtde}");

            default:
            case TipoDatabase.Oracle:
                return
                    ($"select * from ( " +
                     $"  select a.*, ROWNUM rnum from ({sql}) a " +
                     $"  where ROWNUM <= {regfin} " +
                     $") where rnum >= {regini} ");

            case TipoDatabase.SqLite:
                return($"select * from ({sql}) limit {qtde} offset {regini}");

            case TipoDatabase.SqlServer:
                return($"select * from ({sql}) offset {regini} rows fetch next {qtde} rows only ");
            }
        }
Ejemplo n.º 3
0
        public static string GetDataType(this TipoDatabase tipo, CampoAttribute campo)
        {
            var types    = tipo.GetTypes();
            var tipoDado = campo.DataType.GetTipoDadoModel();
            var str      = types.ContainsKey(tipoDado.Dado) ? types[tipoDado.Dado] : null;

            if (str == null)
            {
                return(null);
            }

            var tam = string.Empty;

            if (campo.Tamanho > 0)
            {
                tam += "(" + campo.Tamanho;
                if (campo.Precisao > 0)
                {
                    tam += "," + campo.Precisao;
                }
                tam += ")";
            }

            return(str.Replace("{tam}", tam));
        }
Ejemplo n.º 4
0
        public static string GetCreateCmd(this TipoDatabase tipo, Type type)
        {
            var tabela       = type.GetTabela();
            var listaDeCampo = type.GetCampos();
            var campos       = new List <string>();
            var keys         = new List <string>();
            var column       = new List <string>();

            foreach (var campo in listaDeCampo)
            {
                var dataType = tipo.GetDataType(campo);
                if (dataType == null)
                {
                    continue;
                }

                column.Clear();
                column.Add(campo.Nome);
                column.Add(dataType);
                if (campo.Tipo != CampoTipo.Nul)
                {
                    column.Add("Not Null");
                }

                campos.Add(string.Join(" ", column));

                if (campo.Tipo == CampoTipo.Key)
                {
                    keys.Add(campo.Nome);
                }
            }

            return
                (tipo.GetCreateTable(tabela.Nome, campos.ToArray(), keys.ToArray()));
        }
Ejemplo n.º 5
0
        public static string[] GetListaDeForeignCmd(this TipoDatabase tipo, Type type)
        {
            var tabela         = type.GetTabela();
            var campos         = type.GetCampos();
            var listaDeRelacao = type.GetRelacoesGet();
            var foreigns       = new List <string>();

            foreach (var relacao in listaDeRelacao)
            {
                var camposProp = relacao.OwnerProp.PropertyType.GetCampos();
                var camposRel  = RelacaoCampos.GetRelacaoCampos(relacao.Campos);

                string   table  = tabela.Nome;
                string[] fields = camposRel
                                  .ConvertAll(x => campos.FirstOrDefault(c => c.Atributo == x.Atributo).Nome).ToArray();

                string   tableRel  = relacao.OwnerProp.PropertyType.GetTabela().Nome;
                string[] fieldsRel = camposRel
                                     .ConvertAll(x => camposProp.FirstOrDefault(c => c.Atributo == x.AtributoRel).Nome).ToArray();

                var foreign = tipo.GetForeignKey(table, fields, tableRel, fieldsRel);
                if (!string.IsNullOrEmpty(foreign))
                {
                    foreigns.Add(foreign);
                }
            }

            return(foreigns.ToArray());
        }
Ejemplo n.º 6
0
        //-- lista de comando

        public static string[] GetListaDeComando(this TipoDatabase tipo)
        {
            switch (tipo)
            {
            case TipoDatabase.Oracle:
                return(new[]
                {
                    "ALTER SESSION SET NLS_LANGUAGE            = 'BRAZILIAN PORTUGUESE'",
                    "ALTER SESSION SET NLS_TERRITORY           = 'BRAZIL'",
                    "ALTER SESSION SET NLS_CURRENCY            = 'R$'",
                    "ALTER SESSION SET NLS_ISO_CURRENCY        = 'BRAZIL'",
                    "ALTER SESSION SET NLS_NUMERIC_CHARACTERS  = '.,'",
                    "ALTER SESSION SET NLS_CALENDAR            = 'GREGORIAN'",
                    "ALTER SESSION SET NLS_DATE_FORMAT         = 'DD/MM/RR HH24:MI:SS'",
                    "ALTER SESSION SET NLS_DATE_LANGUAGE       = 'BRAZILIAN PORTUGUESE'",
                    "ALTER SESSION SET NLS_TIME_FORMAT         = 'HH24:MI:SSXFF'",
                    "ALTER SESSION SET NLS_TIMESTAMP_FORMAT    = 'DD/MM/RR HH24:MI:SSXFF'",
                    "ALTER SESSION SET NLS_TIME_TZ_FORMAT      = 'HH24:MI:SSXFF TZR'",
                    "ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'DD/MM/RR HH24:MI:SSXFF TZR'",
                    "ALTER SESSION SET NLS_DUAL_CURRENCY       = 'Cr$'",
                    "ALTER SESSION SET NLS_COMP                = 'BINARY'",
                    "ALTER SESSION SET NLS_LENGTH_SEMANTICS    = 'BYTE'",
                    "ALTER SESSION SET NLS_NCHAR_CONV_EXCP     = 'FALSE'",
                });
            }

            return(null);
        }
Ejemplo n.º 7
0
 public ConnectionConfig(TipoDatabase tipoDatabase, string nomeAssembly, string nomeType, string connectionString)
 {
     TipoDatabase     = tipoDatabase;
     NomeAssembly     = nomeAssembly ?? throw new ArgumentNullException(nameof(nomeAssembly));
     NomeType         = nomeType ?? throw new ArgumentNullException(nameof(nomeType));
     ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
 }
Ejemplo n.º 8
0
        public static string GetValueStr(this TipoDatabase tipoDatabase, object value)
        {
            var tipoDado = value?.GetType().GetTipoDadoModel();

            return(value == null ? "null" :
                   value is DateTime?tipoDatabase.GetValueData((DateTime)value) :
                       tipoDado != null?tipoDado.ConvertStr.Invoke(value) : value.ToString());
        }
Ejemplo n.º 9
0
        //-- foreign

        public static string GetDropForeignKey(this TipoDatabase tipo, string table, string tableRel)
        {
            switch (tipo)
            {
            case TipoDatabase.SqLite:
                return(string.Empty);

            default:
                return($"alter table {table} drop constraint {table}_{tableRel}");
            }
        }
Ejemplo n.º 10
0
        public static ConnectionConfig GetConnectionConfig(this TipoDatabase tipoDatabase)
        {
            var listaConnectionConfig =
                ConnectionConfigExtensions.GetListaConnectionConfig();

            return
                (listaConnectionConfig.FirstOrDefault(x => x.TipoDatabase == tipoDatabase)
                 ??
                 new ConnectionConfig(tipoDatabase, string.Empty, string.Empty, string.Empty)
                );
        }
Ejemplo n.º 11
0
        public static string GetAlterTable(this TipoDatabase tipo, string table, string[] column)
        {
            switch (tipo)
            {
            case TipoDatabase.SqLite:
                return($"alter table {table} add column {string.Join(" ", column)}");

            default:
                return($"alter table {table} add {string.Join(" ", column)}");
            }
        }
Ejemplo n.º 12
0
        //-- primary

        public static string GetPrimaryKey(this TipoDatabase tipo, string table, string[] keys)
        {
            switch (tipo)
            {
            case TipoDatabase.SqLite:
                return($"primary key ({string.Join(", ", keys)})");

            default:
                return($"constraint pk_{table} primary key on ({string.Join(", ", keys)})");
            }
        }
Ejemplo n.º 13
0
        //-- parameter

        public static string GetNameParameter(this TipoDatabase tipo, string atributo)
        {
            switch (tipo)
            {
            case TipoDatabase.Oracle:
                return($":{atributo}");

            default:
                return($"@{atributo}");
            }
        }
Ejemplo n.º 14
0
        public static string GetSqlTableExiste(this TipoDatabase tipo, string tablename)
        {
            switch (tipo)
            {
            case TipoDatabase.Firebird:
                return($"select count(*) from RDB$RELATIONS where RDB$RELATION_NAME = '{tablename}'");

            default:
            case TipoDatabase.Oracle:
                return($"select count(*) from USER_TABLES where TABLE_NAME = '{tablename}'");
            }
        }
Ejemplo n.º 15
0
        /*
         * SELECT * FROM (
         * SELECT a.*, ROWNUM rnum FROM (
         *  SELECT * FROM tabela_enorme ORDER BY campo_indexado
         * ) a WHERE ROWNUM <= 61200
         * ) WHERE rnum >= 61000;
         */

        //-- table

        public static string GetCreateTable(this TipoDatabase tipo, string table, string[] fields, string[] keys)
        {
            var campos = fields.ToList();

            if (keys.Any())
            {
                campos.Add(tipo.GetPrimaryKey(table, keys));
            }

            return
                ($"create table {table} ({string.Join(", ", campos)})");
        }
Ejemplo n.º 16
0
        public static string GetSequenceGen(this TipoDatabase tipo, string tablename)
        {
            switch (tipo)
            {
            case TipoDatabase.Firebird:
                return($"select GEN_ID(SQ_{tablename}, 0) from RDB$DATABASE");

            default:
            case TipoDatabase.Oracle:
                return($"select SQ_{tablename}.NEXTVAL from DUAL");
            }
        }
Ejemplo n.º 17
0
 public Ambiente(string codigo,
                 TipoDatabase tipoDatabase, string providerName,
                 string database, string username, string password, string hostname)
 {
     Codigo       = codigo;
     TipoDatabase = tipoDatabase;
     ProviderName = providerName;
     Database     = database;
     Username     = username;
     Password     = password;
     Hostname     = hostname;
 }
Ejemplo n.º 18
0
        public static string GetForeignKey(this TipoDatabase tipo, string table, string[] fields,
                                           string tableRel, string[] fieldsRel)
        {
            switch (tipo)
            {
            case TipoDatabase.SqLite:
                return(string.Empty);

            default:
                return($"alter table {table} add constraint {table}_{tableRel} foreign key ({string.Join(", ", fields)})" +
                       $" references {tableRel} ({string.Join(", ", fieldsRel)})");
            }
        }
Ejemplo n.º 19
0
        //-- value

        public static string GetValueData(this TipoDatabase tipoDatabase, DateTime value)
        {
            switch (tipoDatabase)
            {
            case TipoDatabase.Firebird:
                return("'" + value.ToString("dd.MM.yyyy HH:mm:ss") + "'");

            case TipoDatabase.Oracle:
                return("to_date('" + value.ToString("dd/MM/yyyy HH:mm:ss") + "', 'DD/MM/YYYY HH24:MI:SS')");

            case TipoDatabase.MySql:
            case TipoDatabase.PostgreSql:
                return("'" + value.ToString("yyyy/MM/dd HH:mm:ss") + "'");

            case TipoDatabase.SqLite:
                return("'" + value.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            }

            return(null);
        }
Ejemplo n.º 20
0
        public static string[] GetListaDeDropForeignCmd(this TipoDatabase tipo, Type type)
        {
            var tabela         = type.GetTabela();
            var listaDeRelacao = type.GetRelacoesGet();
            var foreigns       = new List <string>();

            foreach (var relacao in listaDeRelacao)
            {
                var camposRel = RelacaoCampos.GetRelacaoCampos(relacao.Campos);

                string table    = tabela.Nome;
                string tableRel = relacao.OwnerProp.PropertyType.GetTabela().Nome;

                var foreign = tipo.GetDropForeignKey(table, tableRel);
                if (!string.IsNullOrEmpty(foreign))
                {
                    foreigns.Add(foreign);
                }
            }

            return(foreigns.ToArray());
        }
Ejemplo n.º 21
0
        private static Dictionary <TipoDado, string> GetTypes(this TipoDatabase tipo)
        {
            switch (tipo)
            {
            default:
            case TipoDatabase.Firebird:
            case TipoDatabase.MySql:
            case TipoDatabase.PostgreSql:
                return(new Dictionary <TipoDado, string>
                {
                    { TipoDado.Bool, "char(1)" },
                    { TipoDado.Date, "timestamp" },
                    { TipoDado.Real, "numeric{tam}" },
                    { TipoDado.Int, "integer" },
                    { TipoDado.Str, "varchar{tam}" },
                });

            case TipoDatabase.Oracle:
                return(new Dictionary <TipoDado, string>
                {
                    { TipoDado.Bool, "char(1)" },
                    { TipoDado.Date, "date" },
                    { TipoDado.Real, "number{tam}" },
                    { TipoDado.Int, "integer" },
                    { TipoDado.Str, "varchar{tam}" },
                });

            case TipoDatabase.SqLite:
                return(new Dictionary <TipoDado, string>
                {
                    { TipoDado.Bool, "text" },
                    { TipoDado.Date, "text" },
                    { TipoDado.Real, "real" },
                    { TipoDado.Int, "integer" },
                    { TipoDado.Str, "text" },
                });
            }
        }
Ejemplo n.º 22
0
        public QueryableValue()
        {
            var tipoDatabaseStr = ConfigurationManager.AppSettings["tipoDatabase"] ?? string.Empty;

            _tipoDatabase = tipoDatabaseStr.GetTipoDatabase();
        }
Ejemplo n.º 23
0
 public Clausula(TipoDatabase tipoDatabase)
 {
     TipoDatabase = tipoDatabase;
 }
Ejemplo n.º 24
0
        //-- get relacao

        public static void GetRelacaoLista(this IAbstractDataContext context, object obj, bool inRelacao, TipoDatabase tipoDatabase)
        {
            var relacoes = obj.GetType().GetRelacoesGet();

            foreach (var relacao in relacoes)
            {
                relacao.OwnerObj = obj;
                var val = relacao.OwnerProp.GetValue(obj);
                if (val != null)
                {
                    var ret = context.GetRelacao(val.GetType(), relacao, inRelacao, tipoDatabase);
                    relacao.OwnerProp.SetValue(obj, ret);
                }
            }
        }
Ejemplo n.º 25
0
        //-- limits

        public static string GetSelectLim <TObject>(this TipoDatabase tipoDatabase, string sql, int qtde, int pagina = 0)
        {
            return(tipoDatabase.GetSelectLim(sql, qtde, pagina));
        }
Ejemplo n.º 26
0
        private static object GetRelacao(this IAbstractDataContext context, Type type, RelacaoAttribute relacao, bool inRelacao, TipoDatabase tipoDatabase)
        {
            if (relacao == null)
            {
                return(null);
            }

            var campos = RelacaoCampos.GetRelacaoCampos(relacao.Campos);
            var wheres = new List <string>();

            foreach (var campo in campos)
            {
                var value = relacao.OwnerObj.GetInstancePropOrField(campo.AtributoRel);
                wheres.Add($"{campo.Atributo} = {tipoDatabase.GetValueStr(value)}");
            }

            if (type is IList)
            {
                return(context.GetLista(type, string.Join(" and ", wheres), inRelacao));
            }
            else if (type is object)
            {
                return(context.GetObjeto(type, string.Join(" and ", wheres)));
            }

            return(null);
        }
Ejemplo n.º 27
0
 public Comando(TipoDatabase tipoDatabase)
 {
     _tipoDatabase = tipoDatabase;
 }
Ejemplo n.º 28
0
        public static void AddFiltro <TObject>(this List <string> where, string name, TipoDatabase tipoDatabase,
                                               List <TObject> lista, TObject inicial, TObject final)
        {
            if (lista != null && lista.Any())
            {
                where.Add($"{name} in ({string.Join(",", lista.ConvertAll(x => tipoDatabase.GetValueStr(x)))})");
            }

            if (inicial != null)
            {
                var valueInicial = tipoDatabase.GetValueStr(inicial);
                if (!valueInicial.Equals("null"))
                {
                    where.Add($"{name} >= {valueInicial}");
                }
            }

            if (final != null)
            {
                var valueFinal = tipoDatabase.GetValueStr(final);
                if (!valueFinal.Equals("null"))
                {
                    where.Add($"{name} <= {valueFinal}");
                }
            }
        }
Ejemplo n.º 29
0
 public IComando ComTipoDatabase(TipoDatabase tipoDatabase)
 {
     _tipoDatabase = tipoDatabase;
     return(this);
 }
Ejemplo n.º 30
0
 public static string GetSelectMax(this TipoDatabase tipo, string sql, string column)
 {
     return($"select coalesce(max({column}),0) + 1 as {column} from ({sql})");
 }