public void OnlyOffsetLimit() { var d = new MsSql2005Dialect(); SqlString str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_ from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), null, new SqlString("10")); Assert.That(str.ToString(), Is.EqualTo("select distinct TOP (10) c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_ from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name")); }
public void SelectAndUpdateStringContainCustomWhere() { const string customWhere = "table_name='second'"; var dialect = new MsSql2005Dialect(); var tg = new TableGenerator(); tg.Configure(NHibernateUtil.Int64, new Dictionary<string, string> {{"where", customWhere}}, dialect); Assert.That(selectSql.GetValue(tg).ToString(), Is.StringContaining(customWhere)); Assert.That(updateSql.GetValue(tg).ToString(), Is.StringContaining(customWhere)); }
public void GetLimitString() { var d = new MsSql2005Dialect(); SqlString str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c.Rating as Rating2_19_0_, c.Last_Name as Last_Name3_19_0, c.First_Name as First_Name4_19_0 from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) Contact1_19_0_, Rating2_19_0_, Last_Name3_19_0, First_Name4_19_0 FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY q_.Rating2_19_0_ DESC, q_.Last_Name3_19_0, q_.First_Name4_19_0) as __hibernate_sort_row FROM (select distinct c.Contact_Id as Contact1_19_0_, c.Rating as Rating2_19_0_, c.Last_Name as Last_Name3_19_0, c.First_Name as First_Name4_19_0 from dbo.Contact c where COALESCE(c.Rating, 0) > 0) as q_) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT fish.id FROM fish"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) id FROM (SELECT fish.id, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT DISTINCT fish_.id FROM fish fish_"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) id FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM (SELECT DISTINCT fish_.id FROM fish fish_) as q_) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT DISTINCT fish_.id as ixx9_ FROM fish fish_"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) ixx9_ FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM (SELECT DISTINCT fish_.id as ixx9_ FROM fish fish_) as q_) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT * FROM fish ORDER BY name"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY name) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT fish.id, fish.name FROM fish ORDER BY name DESC"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) id, name FROM (SELECT fish.id, fish.name, ROW_NUMBER() OVER(ORDER BY fish.name DESC) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT * FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t ORDER BY name DESC"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY name DESC) as __hibernate_sort_row FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT *, (SELECT COUNT(1) FROM fowl WHERE fish_id = fish.id) AS some_count FROM fish"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) *, some_count FROM (SELECT *, (SELECT COUNT(1) FROM fowl WHERE fish_id = fish.id) AS some_count, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT * FROM fish WHERE scales = ", Parameter.Placeholder), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish WHERE scales = ?) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT f.Type, COUNT(DISTINCT f.Name) AS Name FROM Fish f GROUP BY f.Type ORDER BY COUNT(DISTINCT f.Name)"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) Type, Name FROM (SELECT f.Type, COUNT(DISTINCT f.Name) AS Name, ROW_NUMBER() OVER(ORDER BY COUNT(DISTINCT f.Name)) as __hibernate_sort_row FROM Fish f GROUP BY f.Type) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); }
public void NH1187() { MsSql2005Dialect d = new MsSql2005Dialect(); SqlString result = d.GetLimitString(new SqlString("select concat(a.Description,', ', a.Description) as desc from Animal a"), new SqlString("111"), new SqlString("222")); Assert.AreEqual("SELECT TOP (222) desc FROM (select concat(a.Description,', ', a.Description) as desc, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row from Animal a) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", result.ToString()); // The test use the function "cast" because cast need the keyWork "as" too SqlString str = d.GetLimitString(new SqlString("SELECT fish.id, cast('astring, with,comma' as string) as bar FROM fish"), new SqlString("111"), new SqlString("222")); Assert.AreEqual( "SELECT TOP (222) id, bar FROM (SELECT fish.id, cast('astring, with,comma' as string) as bar, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > 111 ORDER BY query.__hibernate_sort_row", str.ToString()); }
public void InquireSchema() { Dialect.MsSql2005Dialect dialect = new MsSql2005Dialect(); DbConnection conn = new SqlConnection(@""); conn.Open(); DatabaseMetadata meta = new DatabaseMetadata(conn, dialect); IDataBaseSchema schema = dialect.GetDataBaseSchema(conn); var dt = schema.GetTables(null, null, null, new string[0]); var cols = schema.GetColumns(null, null, null, null); var keys = schema.GetForeignKeys(null, null, null); foreach (DataRow r in dt.Rows) { var tableMeta = schema.GetTableMetadata(r, true); Console.WriteLine(string.Format("Table {2}:[{0}].[{1}]",tableMeta.Schema,tableMeta.Name,tableMeta.Catalog)); ITableMetadata tm = meta.GetTableMetadata(tableMeta.Name, tableMeta.Schema, tableMeta.Catalog, false); IColumnMetadata col = tm.GetColumnMetadata(cols.Rows[0].ItemArray[2] as string); } }
public void NH2809() { var d = new MsSql2005Dialect(); string t = d.GetTypeName(new BinarySqlType()); Assert.That(t, Is.EqualTo("VARBINARY(MAX)")); t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary - 1, 0, 0); Assert.That(t, Is.EqualTo(String.Format("VARBINARY({0})", SqlClientDriver.MaxSizeForLengthLimitedBinary - 1))); t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary, 0, 0); Assert.That(t, Is.EqualTo(String.Format("VARBINARY({0})", SqlClientDriver.MaxSizeForLengthLimitedBinary))); t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary + 1, 0, 0); Assert.That(t, Is.EqualTo("VARBINARY(MAX)")); }
public void GetLimitStringWithSqlComments() { var d = new MsSql2005Dialect(); Assert.Throws<NotSupportedException>(() => d.GetLimitString(new SqlString(" /* criteria query */ SELECT p from lcdtm"), null, new SqlString("2"))); }
public void GetIfExistsDropConstraintTest_For_Schema_other_than_dbo() { MsSql2005Dialect dialect = new MsSql2005Dialect(); Table foo = new Table("Foo"); foo.Schema = "Other"; string expected = "if exists (select 1 from sys.objects" + " where object_id = OBJECT_ID(N'Other.[Bar]')" + " AND parent_object_id = OBJECT_ID('Other.Foo'))"; string ifExistsDropConstraint = dialect.GetIfExistsDropConstraint(foo, "Bar"); System.Console.WriteLine(ifExistsDropConstraint); Assert.AreEqual(expected, ifExistsDropConstraint); }
public void GetLimitString() { MsSql2005Dialect d = new MsSql2005Dialect(); SqlString str = d.GetLimitString(new SqlString("select distinct c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_ from dbo.Contact c where COALESCE(c.Rating, 0) > 0 order by c.Rating desc , c.Last_Name , c.First_Name"), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) Contact1_19_0_, Rating2_19_0_ FROM (select distinct c.Contact_Id as Contact1_19_0_, c._Rating as Rating2_19_0_, ROW_NUMBER() OVER(ORDER BY c.Rating DESC, c.Last_Name, c.First_Name) as __hibernate_sort_row from dbo.Contact c where COALESCE(c.Rating, 0) > 0) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT fish.id FROM fish"), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) id FROM (SELECT fish.id, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT DISTINCT fish_.id FROM fish fish_"), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) id FROM (SELECT DISTINCT fish_.id, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish fish_) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT DISTINCT fish_.id as ixx9_ FROM fish fish_"), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) ixx9_ FROM (SELECT DISTINCT fish_.id as ixx9_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish fish_) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT * FROM fish ORDER BY name"), 5, 15, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY name) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT fish.id, fish.name FROM fish ORDER BY name DESC"), 7, 28, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) id, name FROM (SELECT fish.id, fish.name, ROW_NUMBER() OVER(ORDER BY fish.name DESC) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString( new SqlString("SELECT * FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t ORDER BY name DESC"), 10, 20, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY name DESC) as __hibernate_sort_row FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT *, (SELECT COUNT(1) FROM fowl WHERE fish_id = fish.id) AS some_count FROM fish"), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) *, some_count FROM (SELECT *, (SELECT COUNT(1) FROM fowl WHERE fish_id = fish.id) AS some_count, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT * FROM fish WHERE scales = ", Parameter.Placeholder), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM fish WHERE scales = ?) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); str = d.GetLimitString(new SqlString("SELECT f.Type, COUNT(DISTINCT f.Name) AS Name FROM Fish f GROUP BY f.Type ORDER BY COUNT(DISTINCT f.Name)"), 1, 10, -1, -2); System.Console.WriteLine(str); Assert.AreEqual( "SELECT TOP (?) Type, Name FROM (SELECT f.Type, COUNT(DISTINCT f.Name) AS Name, ROW_NUMBER() OVER(ORDER BY COUNT(DISTINCT f.Name)) as __hibernate_sort_row FROM Fish f GROUP BY f.Type) as query WHERE query.__hibernate_sort_row > ? ORDER BY query.__hibernate_sort_row", str.ToString()); }
public void CreationScript(string config){ var cp = Container.get<IConfigurationProvider>(); var sfp = Container.get<ISessionFactoryProvider>(); Configuration cfg = null; if (config.noContent()){ cfg = cp.Get(null); } else{ cfg = cp.Get(config); } var con = sfp.Get(config).OpenSession().Connection; using (var con3 = con.GetType().create<IDbConnection>()){ var con2 = con3; if (!(con2 is DbConnection)){ con2 = new StubConnection(con2); } con2.ConnectionString = con.ConnectionString; con2.Open(); Dialect dialect = new MsSql2005Dialect(); if (con.ConnectionString.ToLower().Contains("postgres")){ dialect = new PostgreSQL82Dialect(); } if (con.ConnectionString.ToLower().Contains("oracle")){ dialect = new Oracle9iDialect(); } var commands = cfg.GenerateSchemaUpdateScript( dialect, new DatabaseMetadata((DbConnection) con2, dialect)); SetupScript(commands); } }
private static void VerifyLimitStringForStoredProcedureCalls(string sql) { var d = new MsSql2005Dialect(); var limitSql = d.GetLimitString(new SqlString(sql), null, new SqlString("2")); Assert.That(limitSql, Is.Null, "Limit only: {0}", sql); limitSql = d.GetLimitString(new SqlString(sql), new SqlString("10"), null); Assert.That(limitSql, Is.Null, "Offset only: {0}", sql); limitSql = d.GetLimitString(new SqlString(sql), new SqlString("10"), new SqlString("2")); Assert.That(limitSql, Is.Null, "Limit and Offset: {0}", sql); }
public void GetLimitStringWithSqlCommonTableExpression() { const string SQL = @" WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level) ( -- Anchor member definition SELECT ManagerID, EmployeeID, Title, Deptid, 0 AS Level FROM MyEmployees WHERE ManagerID IS NULL UNION ALL -- Recursive member definition SELECT e.ManagerID, e.EmployeeID, e.Title, e.Deptid, Level + 1 FROM MyEmployees AS e INNER JOIN DirectReports AS ON e.ManagerID = d.EmployeeID ) -- Statement that executes the CTE SELECT ManagerID, EmployeeID, Title, Level FROM DirectReports"; const string EXPECTED_SQL = @" WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level) ( -- Anchor member definition SELECT ManagerID, EmployeeID, Title, Deptid, 0 AS Level FROM MyEmployees WHERE ManagerID IS NULL UNION ALL -- Recursive member definition SELECT e.ManagerID, e.EmployeeID, e.Title, e.Deptid, Level + 1 FROM MyEmployees AS e INNER JOIN DirectReports AS ON e.ManagerID = d.EmployeeID ) -- Statement that executes the CTE SELECT TOP (2) ManagerID, EmployeeID, Title, Level FROM DirectReports"; var d = new MsSql2005Dialect(); var limitSqlQuery = d.GetLimitString(new SqlString(SQL), null, new SqlString("2")); Assert.That(limitSqlQuery, Is.Not.Null); Assert.That(limitSqlQuery.ToString(), Is.EqualTo(EXPECTED_SQL)); }
public void GetLimitStringWithSqlComments() { var d = new MsSql2005Dialect(); var limitSqlQuery = d.GetLimitString(new SqlString(" /* criteria query */ SELECT p from lcdtm"), null, new SqlString("2")); Assert.That(limitSqlQuery, Is.Not.Null); Assert.That(limitSqlQuery.ToString(), Is.EqualTo(" /* criteria query */ SELECT TOP (2) p from lcdtm")); }