private static string ReplaceAttributeName(IDMLIdentifiers dmlIdentifiers, Match m)
        {
            string result = m.Value.Substring(1, m.Value.Length - 2);

            // Escape the attribute name
            return(dmlIdentifiers.EscapeIdentifier(result));
        }
Beispiel #2
0
        private static byte[] GetDatabaseBinaryAttribute(string id, DbBinaryConfig dbBinaryConfig)
        {
            IDMLIdentifiers         identifiers = null;
            IDatabaseAccessProvider dbAccessProvider;

            if (string.IsNullOrEmpty(dbBinaryConfig.DBConnection))
            {
                dbAccessProvider = DatabaseAccess.ForCurrentDatabase;
                identifiers      = DatabaseAccess.ForCurrentDatabase.DatabaseServices.DMLService.Identifiers;
            }
            else
            {
                dbAccessProvider = DatabaseAccess.ForDBConnection(dbBinaryConfig.DBConnection);
                identifiers      = DatabaseAccess.ForDBConnection(dbBinaryConfig.DBConnection).DatabaseServices.DMLService.Identifiers;
            }

            string selectSQL = "SELECT " + identifiers.EscapeIdentifier(dbBinaryConfig.Attribute.ToUpper()) + " FROM " +
                               dbBinaryConfig.EntityGetter(null, BuiltInFunction.GetCurrentLocale()) +
                               " WHERE " + identifiers.EscapeIdentifier(dbBinaryConfig.Id.ToUpper()) + " = ";

            if (dbBinaryConfig.IsAlphaId)
            {
                selectSQL += "'" + BuiltInFunction.EncodeSql(id) + "'";
            }
            else
            {
                selectSQL += BuiltInFunction.EncodeSql(id);
            }

            return(GetBinaryFromDb(selectSQL, dbBinaryConfig.Attribute, dbAccessProvider));
        }
Beispiel #3
0
        protected HashSet <string> GetAutoGeneratedColumns(TableSourceInfo tableSource)
        {
            HashSet <string> columns = new HashSet <string>();

            IDMLIdentifiers identifiers     = DatabaseServices.DMLService.Identifiers;
            string          paramPrefix     = DatabaseServices.ExecutionService.ParameterPrefix;
            string          tableOwnerParam = paramPrefix + "table_owner";
            string          tableNameParam  = paramPrefix + "table_name";

            string sql = string.Format(sqlFormatAutoGeneratedColumns,
                                       (tableSource.Database.IsLinkedServer ? DMLIdentifiers.EscapeIdentifierInner(tableSource.Database.LinkedServer) + "." : "")
                                       + DMLIdentifiers.EscapeIdentifierInner(tableSource.Database.Catalog), tableOwnerParam, tableNameParam);

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                using (IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, sql)) {
                    DatabaseServices.ExecutionService.CreateParameter(cmd, tableOwnerParam, DbType.String, tableSource.Schema);
                    DatabaseServices.ExecutionService.CreateParameter(cmd, tableNameParam, DbType.String, tableSource.Name);
                    cmd.CommandTimeout = QueryTimeout;
                    using (IDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            columns.Add(Convert.ToString(reader["COLUMN_NAME"]));
                        }
                        return(columns);
                    }
                }
            }
        }
        public static string ReplaceEntityReferencesAndAttributesInOrderBy(IDMLIdentifiers dmlIdentifiers, string paramValue, string[] entityNames, string[] entityAliases, IDictionary <string, string>[] entityNamesToAttributes)
        {
            string          ret;
            MatchCollection mc;
            Match           m;

            paramValue = paramValue.Trim();

            mc = NotAllowedTableNamesRegex.Matches(paramValue);
            if (mc.Count > 0)
            {
                m = mc[0];
                throw new DataBaseException("'" + m.Groups[2] + m.Groups[3] + "' found in 'Order By' parameter is a reserved prefix");
            }

            if (paramValue != "")
            {
                mc = DynOrderByValidateRegex.Matches(paramValue);
                if (mc.Count != 1 || mc[0].Value != paramValue)
                {
                    throw new DataBaseException("Invalid syntax in 'Order By' parameter.");
                }
            }

            MatchEvaluator meReplaceEntityName          = match => ReplaceEntityNameInOrderBy(match, entityNames, entityAliases);
            MatchEvaluator meReplaceEntityAttributeName = match => ReplaceEntityAttributesName(dmlIdentifiers, match, entityNames, entityAliases, entityNamesToAttributes);
            MatchEvaluator meReplaceAttributeName       = match => ReplaceAttributeName(dmlIdentifiers, match);


            ret = EntitiesInOrderByRegex.Replace(paramValue, meReplaceEntityName);
            ret = AliasAttributesRegex.Replace(ret, meReplaceEntityAttributeName);
            ret = AttributesRegex.Replace(ret, meReplaceAttributeName); // replace delimiters in attributes that doesn't belong to any entity
            return(ret);
        }
        protected string GetQualifiedTableName(DatabaseInfo databaseInfo, string tableName)
        {
            IDMLIdentifiers identifiers = DatabaseServices.DMLService.Identifiers;

            return(string.Format("{0}.{1}{2}",
                                 identifiers.EscapeIdentifier(databaseInfo.Schema),
                                 identifiers.EscapeIdentifier(tableName),
                                 databaseInfo.IsDatabaseLink ? "@" + identifiers.EscapeIdentifier(databaseInfo.DatabaseLink) : ""));
        }
 public static string ReplaceEntityIdentifiers(IDMLIdentifiers dmlIdentifiers, string inParam, bool enableFixLiteralsMisinterpretedAsIdentifiers)
 {
     if (enableFixLiteralsMisinterpretedAsIdentifiers)
     {
         return(new EscapeIdentifier(match => ReplaceAttributeName(dmlIdentifiers, match))
                .ParseSQL(inParam, true /* allowcommentHints */));
     }
     return(inParam);
 }
 public DMLService(IDatabaseServices databaseServices) : base(databaseServices)
 {
     queries                    = new DMLQueries(this);
     identifiers                = new DMLIdentifiers(this);
     operators                  = new DMLOperators(this);
     functions                  = new DMLFunctions(this);
     aggregateFunctions         = new DMLAggregateFunctions(this);
     defaultValues              = new DMLDefaultValues(this);
     syntaxHighlightDefinitions = new DMLSyntaxHighlightDefinitions(this);
 }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DMLService"/> class.
 /// </summary>
 /// <param name="databaseServices">The database services.</param>
 public DMLService(IDatabaseServices databaseServices)
     : base(databaseServices)
 {
     _queries            = new DMLQueries(this);
     _identifiers        = new DMLIdentifiers(this);
     _operators          = new DMLOperators(this);
     _functions          = new DMLFunctions(this);
     _aggregateFunctions = new DMLAggregateFunctions(this);
     _defaultValues      = new DMLDefaultValues(this);
 }
Beispiel #9
0
        public void TestEscapeIdentifier(DatabaseProviderTestCase tc)
        {
            var             databaseServices = tc.Services;
            var             sqlExecutor      = new SQLExecutor(databaseServices);
            IDMLIdentifiers dmlIdentifiers   = databaseServices.DMLService.Identifiers;

            string sql   = "SELECT " + dmlIdentifiers.EscapeIdentifier("DUMMY" + MachineName) + "." + dmlIdentifiers.EscapeIdentifier("SELECT") + " FROM " + dmlIdentifiers.EscapeIdentifier("DUMMY" + MachineName);
            int    value = sqlExecutor.ExecuteScalar(sql).RuntimeValue <int>();

            AssertEqual(123, value, "Escape function didn't work as expected. SQL: " + sql);
        }
Beispiel #10
0
        public void TestGetValidIdentifier(DatabaseProviderTestCase tc)
        {
            var             databaseServices = tc.Services;
            var             sqlExecutor      = new SQLExecutor(databaseServices);
            IDMLIdentifiers dmlIdentifiers   = databaseServices.DMLService.Identifiers;
            string          paramPrefix      = databaseServices.ExecutionService.ParameterPrefix;
            string          param            = "";

            for (int i = 0; i < dmlIdentifiers.MaxLength - 1; i++)
            {
                param += "m";
            }
            string paramName       = dmlIdentifiers.GetValidIdentifier("inparam" + param, true);
            string parameterForSQL = paramPrefix + paramName;
            string sql             = string.Format("SELECT {0} FROM DUMMY" + MachineName, parameterForSQL);

            int value = sqlExecutor.ExecuteScalar(sql, (i, t) => paramPrefix + paramName, 1).RuntimeValue <int>();

            AssertEqual(1, value, "GetValidIdentifier didn't work as expected. SQL: " + sql);
        }
Beispiel #11
0
        public static string ReplaceEntityReferencesInParameter(IDMLIdentifiers dmlIdentifiers, string inParam)
        {
            string          ret;
            MatchCollection mc;
            Match           m;

            mc = NotAllowedTableNamesRegex.Matches(inParam);
            if (mc.Count > 0)
            {
                m = mc[0];
                throw new DataBaseException("'" + m.Groups[2] + m.Groups[3] + "' found in 'Expand Inline' parameter is a reserved prefix");
            }

            ret = inParam;
            MatchEvaluator meReplaceAttributeName = match => ReplaceAttributeName(dmlIdentifiers, match);

            ret = AttributesRegex.Replace(ret, meReplaceAttributeName);

            return(ret);
        }
        public static string ReplaceEntityReferencesInParameter(IDMLIdentifiers dmlIdentifiers, string inParam, bool enableFixLiteralsMisinterpretedAsIdentifiers)
        {
            var mc = NotAllowedTableNamesRegex.Matches(inParam);

            if (mc.Count > 0)
            {
                var m = mc[0];
                throw new DataBaseException("'" + m.Groups[2] + m.Groups[3] + "' found in 'Expand Inline' parameter is a reserved prefix");
            }

            var ret = inParam;

            if (!enableFixLiteralsMisinterpretedAsIdentifiers)
            {
                MatchEvaluator meReplaceAttributeName = match => ReplaceAttributeName(dmlIdentifiers, match);
                ret = AttributesRegex.Replace(ret, meReplaceAttributeName);
            }

            return(ret);
        }
Beispiel #13
0
        public int GetDatabaseObjectsDefinitionHash(string objectNameFilter)
        {
            int          hashResult = -1;
            DatabaseInfo dbInfo     = DatabaseServices.ObjectFactory.CreateLocalDatabaseInfo() as DatabaseInfo;

            if (dbInfo == null)
            {
                return(hashResult);
            }

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                IDMLIdentifiers identifiers = DatabaseServices.DMLService.Identifiers;
                string          sql         = string.Format(@"SELECT BINARY_CHECKSUM(max(modify_date) + count(name))
                                             FROM {0}.sys.all_objects
                                             WHERE name LIKE '%{1}%'", identifiers.EscapeIdentifier(dbInfo.Catalog), objectNameFilter);
                using (IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, sql)) {
                    cmd.CommandTimeout = QueryTimeout;
                    hashResult         = Convert.ToInt32(DatabaseServices.ExecutionService.ExecuteScalar(cmd));
                }
            }
            return(hashResult);
        }
Beispiel #14
0
        private static byte[] GetDatabaseBinaryAttribute(string id, DbBinaryConfig dbBinaryConfig)
        {
            IDMLIdentifiers         identifiers = null;
            IDatabaseAccessProvider dbAccessProvider;

            if (string.IsNullOrEmpty(dbBinaryConfig.DBConnection))
            {
                dbAccessProvider = DatabaseAccess.ForCurrentDatabase;
                identifiers      = dbAccessProvider.DatabaseServices.DMLService.Identifiers;
            }
            else
            {
                dbAccessProvider = DatabaseAccess.ForDBConnection(dbBinaryConfig.DBConnection);
                identifiers      = dbAccessProvider.DatabaseServices.DMLService.Identifiers;
            }

            using (Transaction trans = dbAccessProvider.GetReadOnlyTransaction()) {
                try {
                    using (Command cmd = trans.CreateCommand(
                               "SELECT " + identifiers.EscapeIdentifier(dbBinaryConfig.Attribute.ToUpper()) + " FROM " +
                               dbBinaryConfig.EntityGetter(null, BuiltInFunction.GetCurrentLocale()) +
                               " WHERE " + identifiers.EscapeIdentifier(dbBinaryConfig.Id.ToUpper()) + " = @ID")) {
                        cmd.CreateParameter("@ID", (DbType)dbBinaryConfig.IdDbType, id);

                        using (IDataReader reader = cmd.ExecuteReader()) {
                            if ((reader.IsClosed) || (!reader.Read()))
                            {
                                return(null);
                            }
                            return(ReadBytes(reader, dbBinaryConfig.Attribute));
                        }
                    }
                } catch (Exception) {
                    return(null);
                }
            }
        }
        private static string ReplaceEntityAttributesName(IDMLIdentifiers dmlIdentifiers, Match m, string[] entityNames, string[] entityAliases, IDictionary <string, string>[] entityAttributesNamesToDatabaseNames)
        {
            string[] result          = m.Value.Split('.');
            string   entityAlias     = result[0];
            string   entityAttribute = GetEntityAttributeName(result[1]).ToLower(); // remove the attribute identifier delimiters
            string   entityPhysicalAttributeName;

            // Find the entity attributes name mapper
            IDictionary <string, string> attributesDatabaseNamesMapper = null;

            for (int i = 0; i <= entityAliases.Length; i++)
            {
                if (entityAliases[i] == entityAlias)
                {
                    attributesDatabaseNamesMapper = entityAttributesNamesToDatabaseNames[i];
                    break;
                }
            }

            entityPhysicalAttributeName = attributesDatabaseNamesMapper != null ? attributesDatabaseNamesMapper[entityAttribute] : entityAttribute;

            // Escape the attribute name
            return(entityAlias + "." + dmlIdentifiers.EscapeIdentifier(entityPhysicalAttributeName));
        }
 public static string ReplaceEntityReferencesInParameter(IDMLIdentifiers dmlIdentifiers, string inParam)
 {
     return(ReplaceEntityReferencesInParameter(dmlIdentifiers, inParam, false));
 }
        public static string ReplaceEntityReferencesInOrderBy(IDMLIdentifiers dmlIdentifiers, string paramValue, string[] entityNames, string[] entityAliases)
        {
            var attributes = new IDictionary <string, string> [entityNames.Length];

            return(ReplaceEntityReferencesAndAttributesInOrderBy(dmlIdentifiers, paramValue, entityNames, entityAliases, attributes));
        }