コード例 #1
0
            // Creates a new parameter for a value in this expression translator
            internal IngresParameter CreateParameter(object value, DbType dbType)
            {
                string parameterName = string.Concat("@p", parameterNameCount.ToString(CultureInfo.InvariantCulture));

                parameterNameCount++;
                IngresParameter parameter = new IngresParameter(parameterName, value);

                parameter.DbType = dbType;
                _parameters.Add(parameter);
                return(parameter);
            }
コード例 #2
0
        /// <summary>
        /// Creates a SqlParameter given a name, type, and direction
        /// </summary>
        internal static DbParameter CreateDbParameter(string name, TypeUsage type, ParameterMode mode, object value)
        {
            var result = new IngresParameter(name, value)
            {
                Direction  = MetadataHelpers.ParameterModeToParameterDirection(mode),
                IngresType = GetIngresType(type),
                IsNullable = MetadataHelpers.IsNullable(type)
            };

            result.Size = GetParameterSize(type, mode != ParameterMode.In) ?? result.Size;
            return(result);
        }
コード例 #3
0
 public override void Visit(DbConstantExpression expression)
 {
     if (_createParameters)
     {
         IngresParameter parameter =
             CreateParameter(expression.Value, expression.ResultType);
         _commandText.Append(parameter.ParameterName);
     }
     else
     {
         _commandText.Append(
             _sqlGenerator.WriteSql(expression.Accept(_sqlGenerator)));
     }
 }
コード例 #4
0
        /// <summary>
        /// Allows the Ingres Data Provider implementation of the
        /// DbCommandBuilder class to handle additional parameter properties.
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="row"></param>
        /// <param name="statementType"></param>
        /// <param name="whereClause"></param>
        protected override void ApplyParameterInfo(
            DbParameter parm,
            DataRow row,
            StatementType statementType,
            bool whereClause)
        {
            IngresParameter ingresParm = (IngresParameter)parm;

            ingresParm.IngresType = (IngresType)row["ProviderType"];

            Object objPrecision = row["NumericPrecision"];

            if (objPrecision != DBNull.Value)
            {
                ingresParm.Precision = Convert.ToByte(objPrecision);
            }

            Object objScale = row["NumericScale"];

            if (objScale != DBNull.Value)
            {
                ingresParm.Scale = Convert.ToByte(objScale);
            }
        }          // ApplyParameterInfo
コード例 #5
0
        }          // ApplyParameterInfo

        /// <summary>
        /// Populate the IngresCommand.Parameters collection with data type
        /// metadata about the parameters of the specified database procedure.
        /// </summary>
        /// <param name="command"></param>
        public static void DeriveParameters(IngresCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(
                          "DeriveParameters parameter 'command' must not be null.");
            }

            IngresConnection conn = command.Connection;

            if (conn == null || conn.State != ConnectionState.Open)
            {
                throw new InvalidOperationException(
                          "The IngresCommand.Connection must be specified and open.");
            }

            if (command.CommandText == null ||
                command.CommandText.Length == 0)
            {
                throw new InvalidOperationException(
                          "The IngresCommand.CommandText must be specify a procedure name.");
            }

            if (command.CommandType != CommandType.StoredProcedure)
            {
                throw new InvalidOperationException(
                          "Only CommandType.StoredProcedure is supported.");
            }

            ArrayList tokens = Ingres.ProviderInternals.MetaData.ScanSqlStatement(
                command.CommandText);

            String[] restriction = new String[3];
            if (tokens.Count == 1)                         // procname
            {
                restriction[2] = UnquoteIdent((String)tokens[0], conn, "\"", "\"");
            }
            else
            if (tokens.Count == 3 &&                       // schemaname.procname
                (String)tokens[1] == ".")
            {
                restriction[1] = UnquoteIdent((String)tokens[0], conn, "\"", "\"");
                restriction[2] = UnquoteIdent((String)tokens[2], conn, "\"", "\"");
            }
            else
            if (tokens.Count == 5 &&                       // catalogname.schemaname.procname
                (String)tokens[1] == "." &&
                (String)tokens[3] == ".")
            {
                restriction[0] = UnquoteIdent((String)tokens[0], conn, "\"", "\"");
                restriction[1] = UnquoteIdent((String)tokens[2], conn, "\"", "\"");
                restriction[2] = UnquoteIdent((String)tokens[4], conn, "\"", "\"");
            }
            else
            {
                throw new InvalidOperationException(
                          "Invalid procedure name.");
            }


            DataTable datatable = conn.GetSchema("ProcedureParameters", restriction);

            command.Parameters.Clear();

            foreach (DataRow row in datatable.Rows)
            {
                string          name       = (String)row["COLUMN_NAME"];
                IngresType      ingresType = (IngresType)row["INGRESTYPE"];
                IngresParameter parm       = new IngresParameter(name, ingresType);
                command.Parameters.Add(parm);
            }              // end foreach (DataRow row in datatable)
        }
コード例 #6
0
        /// <summary>
        /// Creates a IngresParameter given a name, type, and direction
        /// </summary>
        internal static IngresParameter CreateSqlParameter(IngresProviderManifest manifest, string name, TypeUsage type, ParameterMode mode, object value)
        {
            int?size;

            //
            // NOTE: Adjust the parameter type so that it will work with textual
            //       GUIDs.  (Commented out because Ingres does not support GUID.)
            //
            //if ((manifest != null) && !manifest._binaryGuid &&
            //    (MetadataHelpers.GetPrimitiveTypeKind(type) == PrimitiveTypeKind.Guid))
            //{
            //    type = TypeUsage.CreateStringTypeUsage(
            //        PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String),
            //        false, true);
            //}

            IngresParameter result = new IngresParameter(name, value);

            // .Direction
            ParameterDirection direction = MetadataHelpers.ParameterModeToParameterDirection(mode);

            if (result.Direction != direction)
            {
                result.Direction = direction;
            }

            // .Size and .DbType
            // output parameters are handled differently (we need to ensure there is space for return
            // values where the user has not given a specific Size/MaxLength)
            bool   isOutParam = mode != ParameterMode.In;
            DbType sqlDbType  = GetSqlDbType(type, isOutParam, out size);

            if (result.DbType != sqlDbType)
            {
                PrimitiveTypeKind kind = MetadataHelpers.GetPrimitiveTypeKind(type);
                if (kind == PrimitiveTypeKind.Time ||           // get time subtype
                    kind == PrimitiveTypeKind.DateTimeOffset || // dateTimeOffset not supported
                    kind == PrimitiveTypeKind.Guid)
                {
                    result.IngresType = manifest.GetIngresType(kind);
                }
                else
                {
                    result.DbType = sqlDbType;
                }
            }

            // Note that we overwrite 'facet' parameters where either the value is different or
            // there is an output parameter.
            if (size.HasValue && (isOutParam || result.Size != size.Value))
            {
                result.Size = size.Value;
            }

            // .IsNullable
            bool isNullable = MetadataHelpers.IsNullable(type);

            if (isOutParam || isNullable != result.IsNullable)
            {
                result.IsNullable = isNullable;
            }

            return(result);
        }
コード例 #7
0
        /// <summary>
        /// Takes, as input, a list of user names and a list of role names and removes the
        /// specified users from the specified roles.
        /// </summary>
        /// <remarks>
        /// This is the main implementation for the <c>RemoveUsersFromRoles</c> method of the provider. Please
        /// see the corresponding method in the Facade, which calls this method, for full documentaion.
        /// </remarks>
        /// <param name="usernames">The array of usernames.</param>
        /// <param name="roleNames">The array of roles.</param>
        internal void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            // Validate the input parameters
            ValidationUtil.CheckArrayParameterIsOk(ref roleNames, true, true, true, 256, "roleNames");
            ValidationUtil.CheckArrayParameterIsOk(ref usernames, true, true, true, 256, "usernames");

            // Instantiate lists to hold the calculated user and role Ids.
            List <string> userIdsList = new List <string>();
            List <string> roleIdsList = new List <string>();

            // Ensure that all of the roleNames are valid
            foreach (string username in usernames)
            {
                string userId = this.GetUserIdByName(username);

                if (userId == null)
                {
                    throw new ProviderException(string.Format(Messages.UserWasNotFound, username));
                }

                userIdsList.Add(userId);
            }

            // Ensure that all of the roleNames are valid
            foreach (string rolename in roleNames)
            {
                string roleId = this.GetRoleIdByName(rolename);

                if (roleId == null)
                {
                    throw new ProviderException(string.Format(Messages.RoleNotFound, rolename));
                }

                roleIdsList.Add(roleId);
            }

            // Ensure that the users are actually the the roles to begin with!
            foreach (string username in usernames)
            {
                foreach (string rolename in roleNames)
                {
                    if (!this.IsUserInRole(username, rolename))
                    {
                        throw new ProviderException(string.Format(Messages.UserAlreadyNotInRole, username, rolename));
                    }
                }
            }

            // Build up the SQL string
            string sql = @"
                            DELETE FROM aspnet_UsersInRoles 
                            WHERE 
                                UserId = ? 
                            AND RoleId = ? 
                           ";

            // Create a new command and enrol in the current transaction.
            IngresCommand cmd = new IngresCommand(sql, this.conn);

            cmd.Transaction    = this.tran;
            cmd.CommandTimeout = this.config.CommandTimeout;

            // Add the required parameters
            IngresParameter userParm = cmd.Parameters.Add("UserId", DbType.String);
            IngresParameter roleParm = cmd.Parameters.Add("RoleId", DbType.String);

            // For each user
            foreach (string userId in userIdsList)
            {
                // For each role
                foreach (string roleId in roleIdsList)
                {
                    userParm.Value = userId;
                    roleParm.Value = roleId;

                    // Remove the user from the role
                    cmd.ExecuteNonQuery();
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Takes, as input, a list of user names and a list of role names and adds the specified
        /// users to the specified roles.
        /// </summary>
        /// <remarks>
        /// This is the main implementation for the <c>AddUsersToRoles</c> method of the provider. Please
        /// see the corresponding method in the Facade, which calls this method, for full documentaion.
        /// </remarks>
        /// <param name="usernames">A list of user names.</param>
        /// <param name="roleNames">A list of roles.</param>
        internal void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            // Note: Most of this validation is also done by the .NET framework - however I will
            // explicitly do it here also.
            ValidationUtil.CheckArrayParameterIsOk(ref roleNames, true, true, true, 256, "roleNames");
            ValidationUtil.CheckArrayParameterIsOk(ref usernames, true, true, true, 256, "usernames");

            // Ensure that all of the roleNames are valid.
            foreach (string rolename in roleNames)
            {
                if (!this.RoleExists(rolename))
                {
                    throw new ProviderException(string.Format(Messages.RoleNotFound, rolename));
                }
            }

            // Ensure that all of the usernames are valid.
            foreach (string username in usernames)
            {
                if (!this.UserExists(username))
                {
                    throw new ProviderException(string.Format(Messages.UserWasNotFound, username));
                }
            }

            // Ensure that all of the users actually are in the roles.
            foreach (string username in usernames)
            {
                foreach (string rolename in roleNames)
                {
                    if (this.IsUserInRole(username, rolename))
                    {
                        throw new ProviderException(string.Format(Messages.UserAlreadyInRole, username, rolename));
                    }
                }
            }

            // Instantiate a string to hold the required SQL
            string sql = @"
                            INSERT INTO aspnet_UsersInRoles
                               (UserId,
                                RoleId)
                            VALUES
                               (?, 
                                ?)
                           ";

            // Create a new command and enrol in the current transaction
            IngresCommand cmd = new IngresCommand(sql, this.conn);

            cmd.Transaction    = this.tran;
            cmd.CommandTimeout = this.config.CommandTimeout;

            // Add the required parameters
            IngresParameter userParam = cmd.Parameters.Add("UserId", DbType.String);
            IngresParameter roleParam = cmd.Parameters.Add("RoleId", DbType.String);

            // Note: this if a bit "row at a time" style rather than "chunk at a time"
            // processing - however it makes for easier to read and understand code and
            // allows for a static parameterized SQL string. If performance is poor then
            // this could be changed.

            // for each user
            foreach (string username in usernames)
            {
                // obtain the user id
                string userId = this.GetUserIdByName(username);

                // for each rolename
                foreach (string rolename in roleNames)
                {
                    // obtain role id
                    string roleId = this.GetRoleIdByName(rolename);

                    userParam.Value = userId;
                    roleParam.Value = roleId;

                    // try to add user to role
                    // The number of rows affected
                    int rows = cmd.ExecuteNonQuery();

                    // One row should be affected
                    if (rows != 1)
                    {
                        throw new ProviderException(string.Format(Messages.UnknownError));
                    }
                }
            }
        }