From: Michael D. <mik...@us...> - 2004-07-19 03:20:44
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Driver In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14722/NHibernate/Driver Modified Files: DriverBase.cs IDriver.cs SqlClientDriver.cs Log Message: Add methods for the Driver to be in control of generating the IDbCommand from the SqlString or string. Index: DriverBase.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Driver/DriverBase.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** DriverBase.cs 18 Jul 2004 03:05:08 -0000 1.5 --- DriverBase.cs 19 Jul 2004 03:20:25 -0000 1.6 *************** *** 86,89 **** --- 86,154 ---- } + public virtual IDbCommand GenerateCommand(Dialect.Dialect dialect, SqlCommand.SqlString sqlString) + { + int paramIndex = 0; + IDbCommand cmd = this.CreateCommand(); + + System.Text.StringBuilder builder = new System.Text.StringBuilder(sqlString.SqlParts.Length * 15); + for(int i = 0; i < sqlString.SqlParts.Length; i++) + { + object part = sqlString.SqlParts[i]; + SqlCommand.Parameter parameter = part as SqlCommand.Parameter; + + if(parameter!=null) + { + string paramName = "p" + paramIndex; + builder.Append ( this.FormatNameForSql(paramName) ); + + IDbDataParameter dbParam = GenerateParameter(cmd, paramName, parameter, dialect); + + cmd.Parameters.Add(dbParam); + + paramIndex++; + } + else + { + builder.Append((string)part); + } + } + + cmd.CommandText = builder.ToString(); + + return cmd; + } + + public virtual IDbCommand GenerateCommand(Dialect.Dialect dialect, string sqlString) + { + IDbCommand cmd = this.CreateCommand(); + cmd.CommandText = sqlString; + + return cmd; + } + + /// <summary> + /// Generates an IDbDataParameter for the IDbCommand. It does not add the IDbDataParameter to the IDbCommand's + /// Parameter collection. + /// </summary> + /// <param name="command">The IDbCommand to use to create the IDbDataParameter.</param> + /// <param name="name">The name to set for IDbDataParameter.Name</param> + /// <param name="parameter">The Parameter to convert to an IDbDataParameter.</param> + /// <param name="dialect">The Dialect to use for Default lengths if needed.</param> + /// <returns>An IDbDataParameter ready to be added to an IDbCommand.</returns> + /// <remarks> + /// Drivers that require Size or Precision/Scale to be set before the IDbCommand is prepared should + /// override this method and use the info contained in the Parameter to set those value. By default + /// those values are not set, only the DbType and ParameterName are set. + /// </remarks> + protected virtual IDbDataParameter GenerateParameter(IDbCommand command, string name, SqlCommand.Parameter parameter, Dialect.Dialect dialect) + { + IDbDataParameter dbParam = command.CreateParameter(); + dbParam.DbType = parameter.SqlType.DbType; + + dbParam.ParameterName = this.FormatNameForParameter(name); + + return dbParam; + } + #endregion } Index: SqlClientDriver.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Driver/SqlClientDriver.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SqlClientDriver.cs 18 May 2004 05:02:27 -0000 1.3 --- SqlClientDriver.cs 19 Jul 2004 03:20:25 -0000 1.4 *************** *** 64,67 **** --- 64,133 ---- + /// <summary> + /// Generates an IDbDataParameter that has values for the Size or Precision/Scale Properties set. + /// </summary> + /// <param name="command">The IDbCommand to use to create the IDbDataParameter.</param> + /// <param name="name">The name to set for IDbDataParameter.Name</param> + /// <param name="parameter">The Parameter to convert to an IDbDataParameter.</param> + /// <param name="dialect">The Dialect to use for Default lengths if needed.</param> + /// <returns>An IDbDataParameter ready to be added to an IDbCommand.</returns> + /// <remarks> + /// In order to prepare an IDbCommand against an MsSql database all variable length values need + /// to be set. + /// </remarks> + protected override IDbDataParameter GenerateParameter(IDbCommand command, string name, SqlCommand.Parameter parameter, Dialect.Dialect dialect) + { + IDbDataParameter dbParam = base.GenerateParameter(command, name, parameter, dialect); + + // take a look at the SqlType and determine if this is one that MsSql needs to set Size or + // Precision/Scale for + + // if this parameter needs to have a value set for the Size or Precision/Scale besides the default + // value of the Dialect then one of these will not be null + SqlCommand.ParameterLength pl = null; + SqlCommand.ParameterPrecisionScale pps = null; + + switch( parameter.SqlType.DbType ) + { + case DbType.AnsiString: + case DbType.AnsiStringFixedLength: + pl = parameter as SqlCommand.ParameterLength; + dbParam.Size = dialect.MaxAnsiStringSize; + break; + + case DbType.Binary: + pl = parameter as SqlCommand.ParameterLength; + dbParam.Size = dialect.MaxBinarySize; + break; + + case DbType.String: + case DbType.StringFixedLength: + pl = parameter as SqlCommand.ParameterLength; + dbParam.Size = dialect.MaxStringSize; + break; + case DbType.Decimal: + pps = parameter as SqlCommand.ParameterPrecisionScale; + //TODO: remove this hardcoding... + dbParam.Precision = 19; + dbParam.Scale = 5; + break; + } + + // if one of these is not null then override the default values + // set in the earlier switch statement. + if( pl!=null ) + { + dbParam.Size = pl.Length; + } + else if( pps!=null ) + { + dbParam.Precision = pps.Precision; + dbParam.Scale = pps.Scale; + } + + return dbParam; + + } + } Index: IDriver.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Driver/IDriver.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** IDriver.cs 18 Jul 2004 03:05:08 -0000 1.4 --- IDriver.cs 19 Jul 2004 03:20:25 -0000 1.5 *************** *** 172,175 **** --- 172,196 ---- + /// <summary> + /// Generates an IDbCommand from the SqlString according to the requirements of the DataProvider. + /// </summary> + /// <param name="dialect">The Dialect to help build the IDbCommand</param> + /// <param name="sqlString">The SqlString that contains the sql and parameters.</param> + /// <returns>An IDbCommand with the CommandText and Parameters fully set.</returns> + IDbCommand GenerateCommand(Dialect.Dialect dialect, SqlCommand.SqlString sqlString); + + /// <summary> + /// Generates an IDbCommand from the string containing sql according to the requirements + /// of the DataProvider. + /// </summary> + /// <param name="dialect">The Dialect to help build the IDbCommand</param> + /// <param name="sqlString">The string that contains the sql that has NO parameters.</param> + /// <returns>An IDbCommand with the CommandText fully set.</returns> + /// <remarks>This can not be used to build an IDbCommand that needs to contain Parameters.</remarks> + IDbCommand GenerateCommand(Dialect.Dialect dialect, string sqlString); + + + + } } |