public void SimpleSelect() { Net4.MySql.DataBase db = Connect(); var generator = new MySql.SqlGenerator(); db.LoadSchema(); Table historicoVentas = db.Schema.Tables.Where(t => t.Name == "historicoventa").Single(); Select select = new Select(); select.Table = historicoVentas; select.Columns.Add(new SelectColumn(historicoVentas["fecha"])); select.Columns.Add(new SelectColumn(historicoVentas["tipocambio"])); select.Columns.Add(new SelectColumn(historicoVentas["precio"])); Table cliente = db.Schema.Tables.Where(t => t.Name == "cliente").Single(); SelectJoin joinCliente = new SelectJoin(); joinCliente.Table = cliente; joinCliente.Columns.Add(new SelectColumn(cliente["RasonSocial"])); joinCliente.On.Add(new ColumnCompareFilter() { Column = historicoVentas["cliente"], ColumnToCompare = cliente["oid"] }); select.Where.Add(new RangeFilter() { Column = historicoVentas["fecha"], MinValue = DateTime.Now.AddYears(-1), MaxValue = DateTime.Now }); Command command = generator.Select(select); var result = db.GetDataTable(command); }
public void CreateTable() { DataBase db = Connect(); var generator = new OKHOSTING.Sql.Net4.SqlServer.SqlGenerator(); //define table schema Table table = new Table("test1"); table.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, Table = table, IsAutoNumber = true }); table.Columns.Add(new Column() { Name = "TextField", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); table.Columns.Add(new Column() { Name = "NumberField", DbType = DbType.Int32, IsNullable = false, Table = table }); table.Indexes.Add(new Index() { Name = "IX_TextField", Unique = false, Table = table }); table.Indexes[0].Columns.Add(table["TextField"]); //create var sql = generator.Create(table); db.Execute(sql); Assert.IsTrue(db.ExistsTable(table.Name)); //add index sql = generator.Create(table.Indexes[0]); db.Execute(sql); //insert Insert insert = new Insert(); insert.Table = table; //insert.Values.Add(new ColumnValue(table["Id"], 1)); insert.Values.Add(new ColumnValue(table["TextField"], "test11")); insert.Values.Add(new ColumnValue(table["NumberField"], 100)); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert insert = new Insert(); insert.Table = table; //insert.Values.Add(new ColumnValue(table["Id"], 2)); insert.Values.Add(new ColumnValue(table["TextField"], "test15")); insert.Values.Add(new ColumnValue(table["NumberField"], 110)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //select Select select = new Select(); select.Table = table; select.Columns.Add(table["id"]); select.Columns.Add(table["TextField"]); select.Where.Add(new ValueCompareFilter() { Column = table["TextField"], ValueToCompare = "test11", Operator = Data.CompareOperator.Equal }); sql = generator.Select(select); var result = db.GetDataTable(sql); Assert.AreEqual(result.Count, 1); //update Update update = new Update(); update.Table = table; update.Where.Add(new ValueCompareFilter() { Column = table["TextField"], ValueToCompare = "test11" }); //delete Delete delete = new Delete(); delete.Table = table; delete.Where.Add(new ValueCompareFilter() { Column = table["TextField"], ValueToCompare = "test11" }); sql = generator.Delete(delete); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //drop sql = generator.Drop(table); db.Execute(sql); Assert.IsFalse(db.ExistsTable(table.Name)); }
public override Command Select(Select select) { //Validating if the table argument is null if (select == null) throw new ArgumentNullException("select"); if (select is SelectAggregate) { return Select((SelectAggregate)select); } //fix column filters tables names, if they come from a join, we should use the join alias if it exist foreach (ColumnFilter filter in select.Where.Where(f => f is ColumnFilter)) { //this filter is using a join table's column, so we need to the join's table alias if (filter.Column.Table != select.Table) { foreach (SelectJoin join in select.Joins) { //this filter is unsong this join's table, so we assign it the table alias from the join if (filter.Column.Table == join.Table && !string.IsNullOrWhiteSpace(join.Alias) && join.Table.Name != join.Alias) { filter.TableAlias = join.Alias; } } } } //Creating Select sentence Command command = SelectClause(select); if (select.Limit != null && select.Limit.Count > 0) { command.Script = command.Script.Replace("SELECT", "SELECT " + LimitClause(select.Limit).Script); } command.Append(FromClause(select)); command.Append(WhereClause(select.Where, LogicalOperator.And)); command.Append(OrderByClause(select.OrderBy)); command.Script += ScriptSeparator; //Returning the sql sentence return command; }
public void InnerJoinTest() { DataBase db = Connect(); var generator = new OKHOSTING.Sql.Net4.SqlServer.SqlGenerator(); // define table schema Table customer = new Table("customer"); customer.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = customer }); customer.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = customer }); customer.Columns.Add(new Column() { Name = "Country", DbType = DbType.Int32, IsNullable = false, Table = customer }); Table country = new Table("country"); country.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = country }); country.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = country }); ForeignKey countryFK = new ForeignKey(); countryFK.Table = customer; countryFK.RemoteTable = country; countryFK.Name = "FK_customer_country"; countryFK.Columns.Add(new Tuple<Column, Column>(customer["Country"], country["id"])); countryFK.DeleteAction = countryFK.UpdateAction = ConstraintAction.Restrict; var sql = generator.Create(customer); db.Execute(sql); sql = generator.Create(country); db.Execute(sql); sql = generator.Create(countryFK); db.Execute(sql); //insert Insert insert2 = new Insert(); insert2.Table = country; insert2.Values.Add(new ColumnValue(country["Id"], 1)); insert2.Values.Add(new ColumnValue(country["Name"], "Mexico")); sql = generator.Insert(insert2); int affectedRows2 = db.Execute(sql); Assert.AreEqual(affectedRows2, 1); Insert insert = new Insert(); insert.Table = customer; insert.Values.Add(new ColumnValue(customer["Id"], 1)); insert.Values.Add(new ColumnValue(customer["Name"], "Angel")); insert.Values.Add(new ColumnValue(customer["Country"], 1)); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //select Select select = new Select(); select.Table = customer; select.Columns.Add(new SelectColumn(customer["id"])); select.Columns.Add(new SelectColumn(customer["Name"])); SelectJoin join = new SelectJoin(); join.Table = country; join.On.Add(new ColumnCompareFilter() { Column = customer["country"], ColumnToCompare = country["id"], Operator = Data.CompareOperator.Equal }); join.Columns.Add(new SelectColumn(country["name"], "countryName")); join.JoinType = SelectJoinType.Inner; select.Joins.Add(join); sql = generator.Select(select); var result = db.GetDataTable(sql); foreach (IDataRow row in result) { foreach (object obj in row) { Console.Write(obj); } } Assert.AreEqual(result.Count, 1); }
public void InventoryTest() { DataBase db = Connect(); var generator = new OKHOSTING.Sql.Net4.SqlServer.SqlGenerator(); //Create table store Table store = new Table("store"); store.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = store }); store.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 50, IsNullable = false, Table = store }); store.Columns.Add(new Column() { Name = "Inventory", DbType = DbType.AnsiString, Length = 50, IsNullable = false, Table = store }); store.Columns.Add(new Column() { Name = "Employee", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = store }); Command sql = generator.Create(store); db.Execute(sql); //First inserts store Insert insert = new Insert(); insert.Table = store; insert.Values.Add(new ColumnValue(store["Id"], 1)); insert.Values.Add(new ColumnValue(store["Name"], "Abarrotes Torrez")); insert.Values.Add(new ColumnValue(store["Inventory"], "Torreon")); insert.Values.Add(new ColumnValue(store["Employee"], "Juan Rocha Gomez")); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //Second inserts store insert = new Insert(); insert.Table = store; insert.Values.Add(new ColumnValue(store["Id"], 2)); insert.Values.Add(new ColumnValue(store["Name"], "La Furiosa")); insert.Values.Add(new ColumnValue(store["Inventory"], "Zacatecas")); insert.Values.Add(new ColumnValue(store["Employee"], "Martin Torrez")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //third inserts store insert = new Insert(); insert.Table = store; insert.Values.Add(new ColumnValue(store["Id"], 3)); insert.Values.Add(new ColumnValue(store["Name"], "Los dos amigos")); insert.Values.Add(new ColumnValue(store["Inventory"], "Durango")); insert.Values.Add(new ColumnValue(store["Employee"], "Luis Martinez")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //Fourth inserts store insert = new Insert(); insert.Table = store; insert.Values.Add(new ColumnValue(store["Id"], 4)); insert.Values.Add(new ColumnValue(store["Name"], "La Pasada")); insert.Values.Add(new ColumnValue(store["Inventory"], "Nayarit")); insert.Values.Add(new ColumnValue(store["Employee"], "Raul Gomez")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //select whit OrFilter Select select = new Select(); select.Table = store; select.Columns.Add(store["id"]); select.Columns.Add(store["Name"]); select.Columns.Add(store["Employee"]); OrFilter filter = new OrFilter(); filter.InnerFilters.Add(new ValueCompareFilter() { Column = store["id"], ValueToCompare = 1, Operator = Data.CompareOperator.Equal }); filter.InnerFilters.Add(new ValueCompareFilter() { Column = store["id"], ValueToCompare = 2, Operator = Data.CompareOperator.Equal }); select.Where.Add(filter); sql = generator.Select(select); var result = db.GetDataTable(sql); Assert.AreEqual(result.Count, 2); //select whit like select = new Select(); select.Table = store; select.Columns.Add(store["Name"]); select.Columns.Add(store["Employee"]); select.Where.Add(new ValueCompareFilter() { Column = store["Name"], ValueToCompare = '%' + "la" + '%', Operator = Data.CompareOperator.Like }); sql = generator.Select(select); result = db.GetDataTable(sql); Assert.AreEqual(result.Count, 2); Assert.AreEqual(result.Count, 0); //select whit AndFilter select = new Select(); select.Table = store; select.Columns.Add(store["id"]); select.Columns.Add(store["Name"]); select.Columns.Add(store["Inventory"]); AndFilter and = new AndFilter(); and.InnerFilters.Add(new ValueCompareFilter() { Column = store["id"], ValueToCompare = 1, Operator = Data.CompareOperator.Equal }); and.InnerFilters.Add(new ValueCompareFilter() { Column = store["Name"], ValueToCompare = "Abarrotes Torrez", Operator = Data.CompareOperator.Equal }); select.Where.Add(and); sql = generator.Select(select); result = db.GetDataTable(sql); Assert.AreEqual(result.Count, 0); //Drop table sql = generator.Drop(store); db.Execute(sql); Assert.IsFalse(db.ExistsTable(store.Name)); }
/// <summary> /// Creates the From Clause for a Select Sql Sentence /// </summary> /// <param name="table"> /// Table for From Clause creation /// </param> /// <returns> /// From Clause from the Select Sql Sentence /// </returns> protected virtual Command FromClause(Select select) { //Validating if table argument is null if (select == null) throw new ArgumentNullException("select"); //Creating the From Clause Command command = " From " + EncloseName(select.Table.Name); //resolve joins foreach (SelectJoin join in select.Joins) { command.Script += " " + join.JoinType.ToString().ToUpper() + " JOIN " + EncloseName(join.Table.Name) + (string.IsNullOrWhiteSpace(join.Alias)? string.Empty : " " + join.Alias) + " ON "; command.Append(Filter(join.On, LogicalOperator.And)); } //Returning the From sql Clause return command; }
public void TablesTest() { DataBase db = Connect(); var generator = new OKHOSTING.Sql.Net4.SqlServer.SqlGenerator(); //Create table team Table team = new Table("team"); team.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = team }); team.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 50, IsNullable = false, Table = team }); team.Columns.Add(new Column() { Name = "Leage", DbType = DbType.Int32, IsNullable = false, Table = team }); team.Columns.Add(new Column() { Name = "Country", DbType = DbType.Int32, IsNullable = false, Table = team }); //Create table leage Table leage = new Table("leage"); leage.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = leage }); leage.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, IsNullable = false, Table = leage }); //Create table country Table country = new Table("country"); country.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = country }); country.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, IsNullable = false, Table = country }); //Create ForeignKey FK_team_country ForeignKey countryFK = new ForeignKey(); countryFK.Table = team; countryFK.RemoteTable = country; countryFK.Name = "FK_team_country"; countryFK.Columns.Add(new Tuple<Column, Column>(team["Country"], country["Id"])); countryFK.DeleteAction = countryFK.UpdateAction = ConstraintAction.Restrict; //Create ForeignKey FK_team_leage ForeignKey leageFK = new ForeignKey(); leageFK.Table = team; leageFK.RemoteTable = leage; leageFK.Name = "FK_team_leage"; leageFK.Columns.Add(new Tuple<Column, Column>(team["Leage"], leage["Id"])); leageFK.DeleteAction = countryFK.UpdateAction = ConstraintAction.Restrict; Command sql = generator.Create(team); db.Execute(sql); sql = generator.Create(leage); db.Execute(sql); sql = generator.Create(country); db.Execute(sql); //insert Country Insert insert = new Insert(); insert.Table = country; insert.Values.Add(new ColumnValue(country["Id"], 15)); insert.Values.Add(new ColumnValue(country["Name"], "Argentina")); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); insert = new Insert(); insert.Table = country; insert.Values.Add(new ColumnValue(country["Id"], 10)); insert.Values.Add(new ColumnValue(country["Name"], "Brasil")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert leage insert = new Insert(); insert.Table = leage; insert.Values.Add(new ColumnValue(leage["Id"], 100)); insert.Values.Add(new ColumnValue(leage["Name"], "Champions")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); insert = new Insert(); insert.Table = leage; insert.Values.Add(new ColumnValue(leage["Id"], 110)); insert.Values.Add(new ColumnValue(leage["Name"], "Concacaff")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert team insert = new Insert(); insert.Table = team; insert.Values.Add(new ColumnValue(team["Id"], 1)); insert.Values.Add(new ColumnValue(team["Name"], "Barza")); insert.Values.Add(new ColumnValue(team["Leage"], 100)); insert.Values.Add(new ColumnValue(team["Country"], 10)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); insert = new Insert(); insert.Table = team; insert.Values.Add(new ColumnValue(team["Id"], 2)); insert.Values.Add(new ColumnValue(team["Name"], "Pumas")); insert.Values.Add(new ColumnValue(team["Leage"], 110)); insert.Values.Add(new ColumnValue(team["Country"], 15)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //select Select select = new Select(); select.Table = team; select.Columns.Add(new SelectColumn(team["id"])); select.Columns.Add(new SelectColumn(team["Name"])); //Create inner join to country SelectJoin join = new SelectJoin(); join.Table = country; join.On.Add(new ColumnCompareFilter() { Column = team["country"], ColumnToCompare = country["id"], Operator = Data.CompareOperator.Equal }); join.Columns.Add(new SelectColumn(country["name"], "countryName")); join.JoinType = SelectJoinType.Inner; select.Joins.Add(join); //Create inner join to leage SelectJoin join2 = new SelectJoin(); join2.Table = leage; join2.On.Add(new ColumnCompareFilter() { Column = team["leage"], ColumnToCompare = leage["id"], Operator = Data.CompareOperator.Equal }); join2.Columns.Add(new SelectColumn(leage["name"], "leageName")); join2.JoinType = SelectJoinType.Inner; select.Joins.Add(join2); select.Where.Add(new ValueCompareFilter() { Column = team["Name"], ValueToCompare = "Pumas", Operator = Data.CompareOperator.Equal }); sql = generator.Select(select); var result = db.GetDataTable(sql); //delete column id in table leage where id = 110 Delete delete = new Delete(); delete.Table = leage; delete.Where.Add(new ValueCompareFilter() { Column = leage["id"], ValueToCompare = 110, Operator = Data.CompareOperator.Equal }); sql = generator.Delete(delete); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //drop table leage sql = generator.Drop(leage); db.Execute(sql); Assert.IsFalse(db.ExistsTable(leage.Name)); //drop table country sql = generator.Drop(country); db.Execute(sql); Assert.IsFalse(db.ExistsTable(country.Name)); //drop table team sql = generator.Drop(team); db.Execute(sql); Assert.IsFalse(db.ExistsTable(team.Name)); }
/// <summary> /// Creates the Select Clause of a Select Sql Sentence /// </summary> /// <param name="table"> /// Table for Select Clause creation /// </param> /// <returns> /// Select Clause from the Select Sql Sentence /// </returns> protected virtual Command SelectClause(Select select) { //Validating if table argument is null if (select == null) throw new ArgumentNullException("select"); //use all columns if none is defined if (select.Columns.Count == 0) { foreach (Column c in select.Table.Columns) { select.Columns.Add(c); } } //Creating Sql Clause string sql = "SELECT "; //Getting the Table DB table name string tableName = EncloseName(select.Table.Name); //Crossing all the loaded members foreach (var c in select.Columns) { sql += string.Format("{0}.{1}", EncloseName(c.Column.Table.Name), EncloseName(c.Column.Name)); if (!string.IsNullOrWhiteSpace(c.Alias) && c.Alias != c.Column.Name) { sql += string.Format(" AS {0}", EncloseName(c.Alias)); } sql += ", "; } //adding join columns foreach (var join in select.Joins) { foreach (var joinColumn in join.Columns) { string joinTableAlias = string.IsNullOrWhiteSpace(join.Alias) ? join.Table.Name : join.Alias; sql += string.Format("{0}.{1}", EncloseName(joinTableAlias), EncloseName(joinColumn.Column.Name)); if (!string.IsNullOrWhiteSpace(joinColumn.Alias) && joinColumn.Alias != joinColumn.Column.Name) { sql += string.Format(" AS {0}", EncloseName(joinColumn.Alias)); } sql += ", "; } } //Removing last ", " sql = sql.TrimEnd(',', ' '); //Returning the Sql Sentence return sql; }
public void MultiJoinTest() { //Open Connect to DataBase DataBase db = Connect(); var generator = new OKHOSTING.Sql.MySql.SqlGenerator(); // define table Customer Table customer = new Table("customer"); customer.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = customer }); customer.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = customer }); // define table Product Table product = new Table("product"); product.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = product }); product.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = product }); product.Columns.Add(new Column() { Name = "Price", DbType = DbType.Decimal, IsNullable = false, Table = product }); // define table Tax Table tax = new Table("tax"); tax.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = tax }); tax.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = tax }); tax.Columns.Add(new Column() { Name = "Rate", DbType = DbType.Decimal, IsNullable = false, Table = tax }); // define table Sale Table sale = new Table("sale"); sale.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = sale }); sale.Columns.Add(new Column() { Name = "Date", DbType = DbType.Date, Table = sale }); sale.Columns.Add(new Column() { Name = "Customer", DbType = DbType.Int32, IsNullable = false, Table = sale }); sale.Columns.Add(new Column() { Name = "Product", DbType = DbType.Int32, IsNullable = false, Table = sale }); sale.Columns.Add(new Column() { Name = "Tax", DbType = DbType.Int32, IsNullable = false, Table = sale }); //define Foreign Key Sale to Customer ForeignKey customerFK = new ForeignKey(); customerFK.Table = sale; customerFK.RemoteTable = customer; customerFK.Name = "FK_sale_customer"; customerFK.Columns.Add(new Tuple<Column, Column>(sale["Customer"], customer["Id"])); customerFK.DeleteAction = customerFK.UpdateAction = ConstraintAction.Restrict; //define Foreign Key Sale to Product ForeignKey productFK = new ForeignKey(); productFK.Table = sale; productFK.RemoteTable = product; productFK.Name = "FK_sale_product"; productFK.Columns.Add(new Tuple<Column, Column>(sale["Product"], product["Id"])); productFK.DeleteAction = productFK.UpdateAction = ConstraintAction.Restrict; //define Foreign Key Sale to Tax ForeignKey taxFK = new ForeignKey(); taxFK.Table = sale; taxFK.RemoteTable = tax; taxFK.Name = "FK_sale_tax"; taxFK.Columns.Add(new Tuple<Column, Column>(sale["Tax"], tax["Id"])); taxFK.DeleteAction = taxFK.UpdateAction = ConstraintAction.Restrict; //Create table Customer var sql = generator.Create(customer); db.Execute(sql); //Create table Product sql = generator.Create(product); db.Execute(sql); //Create table Tax sql = generator.Create(tax); db.Execute(sql); //Create table Sale sql = generator.Create(sale); db.Execute(sql); //Create foreign Key Sale to Customer sql = generator.Create(customerFK); db.Execute(sql); //Create foreign Key Sale to Product sql = generator.Create(productFK); db.Execute(sql); //Create foreign Key Sale to Tax sql = generator.Create(taxFK); db.Execute(sql); //insert values into customer Insert insert = new Insert(); insert.Table = customer; insert.Values.Add(new ColumnValue(customer["Id"], 1)); insert.Values.Add(new ColumnValue(customer["Name"], "Joyas Loyane SA de CV")); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into customer insert = new Insert(); insert.Table = customer; insert.Values.Add(new ColumnValue(customer["Id"], 2)); insert.Values.Add(new ColumnValue(customer["Name"], "Cartie Joyerias SC de CV")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into product insert = new Insert(); insert.Table = product; insert.Values.Add(new ColumnValue(product["Id"], 1)); insert.Values.Add(new ColumnValue(product["Name"], "Hosting")); insert.Values.Add(new ColumnValue(product["Price"], 125.50)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into product insert = new Insert(); insert.Table = product; insert.Values.Add(new ColumnValue(product["Id"], 2)); insert.Values.Add(new ColumnValue(product["Name"], "Web Page")); insert.Values.Add(new ColumnValue(product["Price"], 1300.75)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into tax insert = new Insert(); insert.Table = tax; insert.Values.Add(new ColumnValue(tax["Id"], 1)); insert.Values.Add(new ColumnValue(tax["Name"], "IVA")); insert.Values.Add(new ColumnValue(tax["Rate"], 16.00)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into sale insert = new Insert(); insert.Table = sale; insert.Values.Add(new ColumnValue(sale["Id"], 1)); insert.Values.Add(new ColumnValue(sale["Date"], DateTime.Today)); insert.Values.Add(new ColumnValue(sale["Customer"], 1)); insert.Values.Add(new ColumnValue(sale["Product"], 1)); insert.Values.Add(new ColumnValue(sale["Tax"], 1)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into sale insert = new Insert(); insert.Table = sale; insert.Values.Add(new ColumnValue(sale["Id"], 2)); insert.Values.Add(new ColumnValue(sale["Date"], DateTime.Today)); insert.Values.Add(new ColumnValue(sale["Customer"], 2)); insert.Values.Add(new ColumnValue(sale["Product"], 2)); insert.Values.Add(new ColumnValue(sale["Tax"], 1)); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //select sale for customer 'Joyas Loyane SA de CV' Select select = new Select(); select.Table = sale; select.Columns.Add(new SelectColumn(sale["id"])); select.Columns.Add(new SelectColumn(sale["Date"])); //join for get name Customer SelectJoin join = new SelectJoin(); join.Table = customer; join.On.Add(new ColumnCompareFilter() { Column = sale["Customer"], ColumnToCompare = customer["Id"], Operator = Data.CompareOperator.Equal }); join.Columns.Add(new SelectColumn(customer["Name"], "customerName")); join.JoinType = SelectJoinType.Inner; //Add Join in select select.Joins.Add(join); //join for get name Product SelectJoin join2 = new SelectJoin(); join2.Table = product; join2.On.Add(new ColumnCompareFilter() { Column = sale["Product"], ColumnToCompare = product["Id"], Operator = Data.CompareOperator.Equal }); join2.Columns.Add(new SelectColumn(product["Name"], "productName")); join2.JoinType = SelectJoinType.Inner; //Add Join in select select.Joins.Add(join2); //join for get name Tax SelectJoin join3 = new SelectJoin(); join3.Table = tax; join3.On.Add(new ColumnCompareFilter() { Column = sale["Tax"], ColumnToCompare = tax["Id"], Operator = Data.CompareOperator.Equal }); join3.Columns.Add(new SelectColumn(tax["Name"], "taxName")); join3.JoinType = SelectJoinType.Inner; //Add Join in select select.Joins.Add(join3); //Where Customer = Joyas Loyane SA de CV. select.Where.Add(new ValueCompareFilter() { Column = customer["Name"], ValueToCompare = "Joyas Loyane SA de CV", Operator = Data.CompareOperator.Equal }); //Execute Select sql = generator.Select(select); var result = db.GetDataTable(sql); //Show result in Command Line foreach (IDataRow row in result) { foreach (object obj in row) { Console.Write(obj); } } Assert.AreEqual(result.Count, 1); //Drop Table Sale (Firts drop the table contains foreign keys) sql = generator.Drop(sale); db.Execute(sql); Assert.IsFalse(db.ExistsTable(sale.Name)); //Drop Table Customer sql = generator.Drop(customer); db.Execute(sql); Assert.IsFalse(db.ExistsTable(sale.Name)); //Drop Table Product sql = generator.Drop(product); db.Execute(sql); Assert.IsFalse(db.ExistsTable(sale.Name)); //Drop Table Tax sql = generator.Drop(tax); db.Execute(sql); Assert.IsFalse(db.ExistsTable(sale.Name)); }
public void selectLike() { //Open Connect to DataBase DataBase db = Connect(); var generator = new OKHOSTING.Sql.MySql.SqlGenerator(); //define table Address Table table = new Table("Address"); table.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = table }); table.Columns.Add(new Column() { Name = "Street", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); table.Columns.Add(new Column() { Name = "Number", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); table.Columns.Add(new Column() { Name = "Suburb", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); table.Columns.Add(new Column() { Name = "State", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); table.Columns.Add(new Column() { Name = "Country", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); //create tanle Address var sql = generator.Create(table); db.Execute(sql); Assert.IsTrue(db.ExistsTable(table.Name)); //insert values into Address Insert insert = new Insert(); insert.Table = table; insert.Values.Add(new ColumnValue(table["Id"], 1)); insert.Values.Add(new ColumnValue(table["Street"], "First Avenue")); insert.Values.Add(new ColumnValue(table["Number"], "12-B")); insert.Values.Add(new ColumnValue(table["Suburb"], "The Roses")); insert.Values.Add(new ColumnValue(table["State"], "California")); insert.Values.Add(new ColumnValue(table["Country"], "United States")); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //insert values into Address insert = new Insert(); insert.Table = table; insert.Values.Add(new ColumnValue(table["Id"], 2)); insert.Values.Add(new ColumnValue(table["Street"], "Second Life")); insert.Values.Add(new ColumnValue(table["Number"], "1120")); insert.Values.Add(new ColumnValue(table["Suburb"], "Park Avenue")); insert.Values.Add(new ColumnValue(table["State"], "New York")); insert.Values.Add(new ColumnValue(table["Country"], "United States")); sql = generator.Insert(insert); affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //SELECT street and suburb FROM address WHERE street LIKE 'Second' Select select = new Select(); select.Table = table; select.Columns.Add(table["Id"]); select.Columns.Add(table["Street"]); select.Columns.Add(table["Suburb"]); select.Where.Add(new ValueCompareFilter() { Column = table["Street"], ValueToCompare = "Second", Operator = Data.CompareOperator.Like }); sql = generator.Select(select); var result = db.GetDataTable(sql); //Show result from query foreach (IDataRow row in result) { foreach (object obj in row) { Console.Write(obj); } } }
public void selectPerson() { //open connect to database DataBase db = Connect(); var generator = new OKHOSTING.Sql.MySql.SqlGenerator(); //define table person Table table = new Table("Person"); table.Columns.Add(new Column() { Name = "Id", DbType = DbType.Int32, IsPrimaryKey = true, IsAutoNumber = true, Table = table }); table.Columns.Add(new Column() { Name = "Name", DbType = DbType.AnsiString, Length = 100, IsNullable = false, Table = table }); table.Columns.Add(new Column() { Name = "Age", DbType = DbType.Int32, IsNullable = false, Table = table }); //create table person var sql = generator.Create(table); db.Execute(sql); Assert.IsTrue(db.ExistsTable(table.Name)); //insert values into person Insert insert = new Insert(); insert.Table = table; insert.Values.Add(new ColumnValue(table["Id"], 1)); insert.Values.Add(new ColumnValue(table["Name"], "Angel")); insert.Values.Add(new ColumnValue(table["Age"], 25)); sql = generator.Insert(insert); int affectedRows = db.Execute(sql); Assert.AreEqual(affectedRows, 1); //SELECT id, name, age FROM person WHERE name='Angel' Select select = new Select(); select.Table = table; select.Columns.Add(table["Id"]); select.Columns.Add(table["Name"]); select.Columns.Add(table["Age"]); select.Where.Add(new ValueCompareFilter() { Column = table["Name"], ValueToCompare = "Angel", Operator = Data.CompareOperator.Equal }); sql = generator.Select(select); var result = db.GetDataTable(sql); //Show result in command line foreach (IDataRow row in result) { foreach (object obj in row) { Console.Write(obj); } } Assert.AreEqual(result.Count, 1); }
/// <summary> /// Returns the From clausule for a Select statement of the /// specified TypeMap<T>, including the respective inner joins /// to the related entities /// </summary> /// <param name="dtype"> /// TypeMap<T> wich you desire the From clausule /// </param> /// <returns> /// From clausule for a Select statement of the /// specified TypeMap<T>, including the respective inner joins /// to the related entities /// </returns> protected override Command FromClause(Select select) { //Validating if dtype argument is null if (select == null) { throw new ArgumentNullException("select"); } //Getting the number of parent DataTypes that have the specified TypeMap<T> int parentDataTypes = select.Joins.Count(); //Initializing the sql sequence Command command = "FROM " + new String('(', parentDataTypes - 1) + EncloseName(select.Table.Name); //Crossing the parent DataTypes of the specified type foreach(SelectJoin join in select.Joins) { //Creating the respective inner join clausule command += " INNER JOIN " + EncloseName(join.Table.Name) + " ON "; //Creating the link conditions of the join (ON clausule body) command += Filter(join.On, LogicalOperator.And); //Removing the last " AND " command.Script = command.Script.Remove(command.Script.Length - 5, 5); //Closing the inner join command += ")"; } //Retrieving sql sentence return command; }