Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed conflicts in MikeZubers pull request

  • Loading branch information...
commit b4e22ecff822f37299726d0d8816365331654280 2 parents 59a7077 + e21fae5
@6pac 6pac authored
View
95 SubSonic/DataProviders/OracleDataProvider.cs
@@ -113,6 +113,13 @@ private static void AddParams(OracleCommand cmd, QueryCommand qry)
sqlParam.OracleType = GetOracleType(param.DataType);
sqlParam.ParameterName = param.ParameterName;
sqlParam.Value = param.ParameterValue;
+
+ if (param.Mode == ParameterDirection.Output ||
+ param.Mode == ParameterDirection.InputOutput)
+ {
+ sqlParam.Size = param.Size;
+ }
+
cmd.Parameters.Add(sqlParam);
}
}
@@ -156,7 +163,7 @@ public static OracleType GetOracleType(DbType dbType)
case DbType.Int64:
return OracleType.Number;
case DbType.Object:
- return OracleType.Blob;
+ return OracleType.Cursor;
case DbType.SByte:
return OracleType.SByte;
case DbType.Single:
@@ -391,8 +398,7 @@ public override TableSchema.Table GetTableSchema(string tableName, TableType tab
if(!String.IsNullOrEmpty(precision) && precision != "0")
column.NumberPrecision = int.Parse(precision);
- // column.DataType = GetDbType(rdr[OracleSchemaVariable.DATA_TYPE].ToString().ToLower());
- column.DataType = GetDbTypeOracle(rdr[OracleSchemaVariable.DATA_TYPE].ToString().ToLower(), column.NumberScale, column.NumberPrecision);
+ column.DataType = GetDbType(rdr[OracleSchemaVariable.DATA_TYPE].ToString().ToLower());
column.AutoIncrement = false;
int maxLength;
int.TryParse(rdr[OracleSchemaVariable.MAX_LENGTH].ToString(), out maxLength);
@@ -552,7 +558,6 @@ public override TableSchema.Table[] GetPrimaryKeyTables(string tableName)
QueryCommand cmd = new QueryCommand(GET_PRIMARY_KEY_SQL, Name);
cmd.AddParameter(TABLE_NAME_PARAMETER, tableName, DbType.AnsiString);
ArrayList names = new ArrayList();
- //ArrayList list = new ArrayList();
using(IDataReader rdr = GetReader(cmd))
{
@@ -611,43 +616,26 @@ public override string GetForeignKeyTableName(string fkColumnName)
}
/// <summary>
- /// Added to distinguish for ORACLE between float and integer
- /// To be accurate, we should test dataPrecision to determine what kind of int
- /// we deal with 32, 64, while not extactly true cause, for instance 77000 is 5 of dataprecision
- /// and doesn't fit an int16)
- /// 19/03/07 : due to mismatch casting between integer and decimal data type (i.e
- /// what is the real type returned by the OracleClient assembly which is decimal and
- /// returned type from this function, I decided to return always for number type,
- /// decimal). Works better.
+ /// Maps Oracle-specific data types to generic System.Data.DbType types.
/// </summary>
- /// <param name="sqlType">Type of the SQL.</param>
- /// <param name="dataScale">The data scale.</param>
- /// <param name="dataPrecision">The data precision.</param>
- /// <returns></returns>
- public static DbType GetDbTypeOracle(string sqlType, int dataScale, int dataPrecision)
+ /// <param name="sqlType">Oracle data type</param>
+ /// <returns>DbType enum representing the specified Oracle data type.</returns>
+ public override DbType GetDbType(string sqlType)
{
switch(sqlType)
{
- case "varchar2":
- case "varchar":
case "char":
+ case "varchar":
+ case "varchar2":
+ case "clob":
+ return DbType.AnsiString;
case "nchar":
case "nvarchar2":
- case "rowid":
- return DbType.String;
case "nclob":
- case "clob":
- return DbType.AnsiString;
+ case "rowid": //Not sure about ROWID
+ return DbType.String;
case "number":
return DbType.Decimal;
- //if (dataScale > 0)
- //{
- // return DbType.Single;
- //}
- //else
- //{
- // return DbType.Int32;
- //}
case "float":
return DbType.Double;
case "raw":
@@ -655,43 +643,17 @@ public static DbType GetDbTypeOracle(string sqlType, int dataScale, int dataPrec
case "blob":
return DbType.Binary;
case "date":
- case "timestamp":
- return DbType.DateTime;
- default:
- return DbType.String;
- }
- }
-
- /// <summary>
- /// Gets the type of the db.
- /// </summary>
- /// <param name="sqlType">Type of the SQL.</param>
- /// <returns></returns>
- public override DbType GetDbType(string sqlType)
- {
- switch(sqlType)
- {
- case "number":
- return DbType.Single;
- case "float":
- return DbType.Double;
- case "varchar2":
- case "varchar":
- case "char":
- case "nchar":
- case "nvarchar2":
- case "rowid":
- case "nclob":
- case "blob":
- return DbType.String;
- case "raw":
- case "long raw":
- return DbType.Binary;
- case "date":
- case "timestamp":
return DbType.DateTime;
default:
- return DbType.String;
+ //For whatever reason, Oracle9i (+ others?) stores the
+ //precision with certain datatypes. Ex: "timestamp(3)"
+ //So having "timestamp" as a case statement will not work.
+ if(sqlType.StartsWith("timestamp"))
+ return DbType.DateTime;
+ else if (sqlType.StartsWith("interval"))
+ return DbType.String; //No idea how to handle this one
+ else
+ return DbType.String;
}
}
@@ -732,6 +694,7 @@ public override void ExecuteTransaction(QueryCommandCollection commands)
{
cmd = new OracleCommand(qry.CommandSql, (OracleConnection)conn.Connection);
cmd.CommandType = qry.CommandType;
+ cmd.Transaction = trans;
AddParams(cmd, qry);
View
11 SubSonic/Sql Tools/QueryCommand.cs
@@ -370,17 +370,6 @@ public void AddParameter(string parameterName, object parameterValue, DbType dat
}
/// <summary>
- /// Adds the parameter. THIS OVERLOAD DOES NOT ENSURE DATA TYPE SAFETY!
- /// </summary>
- /// <param name="parameterName">Name of the parameter.</param>
- /// <param name="parameterValue">The parameter value.</param>
- [Obsolete("Deprecated: Use AddParameter(string parameterName, object parameterValue, DbType dataType) instead.")]
- public void AddParameter(string parameterName, object parameterValue)
- {
- AddParameter(parameterName, parameterValue, DbType.AnsiString);
- }
-
- /// <summary>
/// Adds the output parameter.
/// </summary>
/// <param name="parameterName">Name of the parameter.</param>
View
1  SubSonic/SqlQuery/SqlGenerators/ANSISqlGenerator.cs
@@ -135,6 +135,7 @@ public virtual string GenerateCommandLine()
{
if (query.IsDistinct)
sb.Append(query.DistinctSpec);
+
//set "TOP"
sb.Append(query.TopSpec);
View
85 SubSonic/SqlQuery/SqlGenerators/OracleGenerator.cs
@@ -22,13 +22,6 @@ namespace SubSonic
/// </summary>
public class OracleGenerator : ANSISqlGenerator
{
- private const string PAGING_SQL =
- @" SELECT *
- FROM(
- {0}
- )
- WHERE row_number BETWEEN {1} AND {2}
- ";
/// <summary>
/// Initializes a new instance of the <see cref="OracleGenerator"/> class.
/// </summary>
@@ -71,55 +64,71 @@ protected override string GenerateAggregateSelect(Aggregate aggregate)
return aggregate.ColumnName;
if(hasAlias)
return String.Format("{0}({1}) AS \"{2}\"", Aggregate.GetFunctionType(aggregate).ToUpper(), aggregate.ColumnName, aggregate.Alias);
+
return string.Format("{0}({1})", Aggregate.GetFunctionType(aggregate).ToUpper(), aggregate.ColumnName);
}
/// <summary>
+ /// Gets the count select.
+ /// </summary>
+ /// <returns></returns>
+ public override string GetCountSelect()
+ {
+ return base.GetCountSelect().Replace('\'', '"');
+ }
+
+ /// <summary>
/// Builds the paged select statement.
/// </summary>
/// <returns></returns>
public override string BuildPagedSelectStatement()
{
- string idColumn = GetSelectColumns()[0];
- string sqlType;
+ int startnum = query.PageSize * query.CurrentPage + 1;
+ int endnum = query.PageSize * query.CurrentPage + query.PageSize;
+ string orderBy = String.Empty;
- TableSchema.TableColumn idCol = FindColumn(idColumn);
- if (idCol != null)
- {
- string pkType = String.Empty;
- if (Utility.IsString(idCol))
- pkType = String.Concat("(", idCol.MaxLength, ")");
- sqlType = Enum.GetName(typeof(SqlDbType), Utility.GetSqlDBType(idCol.DataType));
- sqlType = String.Concat(sqlType, pkType);
- }
- else
- {
- //assume it's an integer
- sqlType = Enum.GetName(typeof(SqlDbType), SqlDbType.Int);
- }
+ if (this.query.OrderBys.Count > 0)
+ orderBy = GenerateOrderBy();
+
+ //The ROW_NUMBER() function in Oracle requires an ORDER BY clause.
+ //In case one is not specified, we need to halt and inform the caller.
+ if(orderBy.Equals(String.Empty))
+ throw new ArgumentException("There is no column specified for the ORDER BY clause", "OrderBys");
+
+ System.Text.StringBuilder sql = new System.Text.StringBuilder();
- string select = GenerateCommandLine();
- string fromLine = GenerateFromList();
- string joins = GenerateJoins();
- string wheres = GenerateWhere();
- string havings = string.Empty;
- string groupby = string.Empty;
+ //Build the command string
+ sql.Append("WITH pagedtable AS (");
+ sql.Append(GenerateCommandLine());
+
+ //Since this class is for Oracle-specific SQL, we can add a hint
+ //which should help pagination queries return rows more quickly.
+ //AFAIK, this is only valid for Oracle 9i or newer.
+ sql.Replace("SELECT", "SELECT /*+ first_rows('" + query.PageSize + "') */");
+
+ sql.Append(", ROW_NUMBER () OVER (");
+ sql.Append(orderBy);
+ sql.Append(") AS rowindex ");
+ sql.Append(Environment.NewLine);
+ sql.Append(GenerateFromList());
+ sql.Append(GenerateJoins());
- string orderby = GenerateOrderBy();
+ sql.Append(GenerateWhere());
if (query.Aggregates.Count > 0)
{
- groupby = GenerateGroupBy();
- havings = GenerateHaving();
+ sql.Append(GenerateGroupBy());
+ sql.Append(Environment.NewLine);
+ sql.Append(GenerateHaving());
}
- int startnum = query.PageSize*query.CurrentPage +1;
- int endnum = query.PageSize*query.CurrentPage + query.PageSize;
- string sql = string.Format(PAGING_SQL
- , String.Concat(select, fromLine.Replace("FROM", ", ROWNUM as row_number FROM"), joins, wheres, groupby, havings)
- , startnum, endnum);
+ sql.Append(") SELECT * FROM pagedtable WHERE rowindex BETWEEN ");
+ sql.Append(startnum);
+ sql.Append(" AND ");
+ sql.Append(endnum);
+ sql.Append(" ORDER BY rowindex");
- return sql;
+ return sql.ToString();
}
#endregion
View
2  SubSonic/SqlQuery/SqlQuery.cs
@@ -84,6 +84,7 @@ public enum QueryType
///
/// </summary>
public List<Aggregate> Aggregates = new List<Aggregate>();
+
/// <summary>
///
/// </summary>
@@ -1952,6 +1953,7 @@ public void CheckLogicalDelete()
/// <returns></returns>
public SqlQuery Distinct()
{
+ DistinctSpec = SqlFragment.DISTINCT;
IsDistinct = true;
return this;
}
Please sign in to comment.
Something went wrong with that request. Please try again.