mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-12-27 10:25:47 +08:00
- 增加 IDbConnection/IDbTransaction 对象的扩展方法 Select/Insert/Update/Delete 实现 CRUD #267;
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.AdoNetExtensions.MySqlConnectionExtensions {
|
||||
public class Methods {
|
||||
|
||||
string _connectString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=5";
|
||||
|
||||
public Methods() {
|
||||
g.mysql.CodeFirst.SyncStructure<TestConnectionExt>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Insert() {
|
||||
var affrows = 0;
|
||||
using (var conn = new MySqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testinsert" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Update() {
|
||||
var affrows = 0;
|
||||
using (var conn = new MySqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testupdate" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
item = conn.Select<TestConnectionExt>().First();
|
||||
affrows = conn.Update<TestConnectionExt>().SetSource(item).Set(a => a.title, "testupdated").ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Delete() {
|
||||
var affrows = 0;
|
||||
using (var conn = new MySqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testdelete" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
affrows = conn.Delete<TestConnectionExt>().Where(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Select() {
|
||||
var list = new List<TestConnectionExt>();
|
||||
var affrows = 0;
|
||||
using (var conn = new MySqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testselect" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
list = conn.Select<TestConnectionExt>().Where(a => a.id == item.id).ToList();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Single(list);
|
||||
}
|
||||
|
||||
class TestConnectionExt {
|
||||
public Guid id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.AdoNetExtensions.NpgsqlConnectionExtensions {
|
||||
public class Methods {
|
||||
|
||||
string _connectString = "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=5";
|
||||
|
||||
public Methods() {
|
||||
g.pgsql.CodeFirst.SyncStructure<TestConnectionExt>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Insert() {
|
||||
var affrows = 0;
|
||||
using (var conn = new NpgsqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testinsert" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Update() {
|
||||
var affrows = 0;
|
||||
using (var conn = new NpgsqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testupdate" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
item = conn.Select<TestConnectionExt>().First();
|
||||
affrows = conn.Update<TestConnectionExt>().SetSource(item).Set(a => a.title, "testupdated").ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Delete() {
|
||||
var affrows = 0;
|
||||
using (var conn = new NpgsqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testdelete" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
affrows = conn.Delete<TestConnectionExt>().Where(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Select() {
|
||||
var list = new List<TestConnectionExt>();
|
||||
var affrows = 0;
|
||||
using (var conn = new NpgsqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testselect" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
list = conn.Select<TestConnectionExt>().Where(a => a.id == item.id).ToList();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Single(list);
|
||||
}
|
||||
|
||||
class TestConnectionExt {
|
||||
public Guid id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Oracle.ManagedDataAccess.Client;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.AdoNetExtensions.OracleConnectionExtensions {
|
||||
public class Methods {
|
||||
|
||||
string _connectString = "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=5";
|
||||
|
||||
public Methods() {
|
||||
g.oracle.CodeFirst.SyncStructure<TestConnectionExt>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Insert() {
|
||||
var affrows = 0;
|
||||
using (var conn = new OracleConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testinsert" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Update() {
|
||||
var affrows = 0;
|
||||
using (var conn = new OracleConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testupdate" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
item = conn.Select<TestConnectionExt>().First();
|
||||
affrows = conn.Update<TestConnectionExt>().SetSource(item).Set(a => a.title, "testupdated").ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Delete() {
|
||||
var affrows = 0;
|
||||
using (var conn = new OracleConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testdelete" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
affrows = conn.Delete<TestConnectionExt>().Where(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Select() {
|
||||
var list = new List<TestConnectionExt>();
|
||||
var affrows = 0;
|
||||
using (var conn = new OracleConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testselect" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
list = conn.Select<TestConnectionExt>().Where(a => a.id == item.id).ToList();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Single(list);
|
||||
}
|
||||
|
||||
class TestConnectionExt {
|
||||
public Guid id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SQLite;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.AdoNetExtensions.SQLiteConnectionExtensions {
|
||||
public class Methods {
|
||||
|
||||
string _connectString = "Data Source=|DataDirectory|/document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=5";
|
||||
|
||||
public Methods() {
|
||||
g.sqlite.CodeFirst.SyncStructure<TestConnectionExt>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Insert() {
|
||||
var affrows = 0;
|
||||
using (var conn = new SQLiteConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testinsert" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Update() {
|
||||
var affrows = 0;
|
||||
using (var conn = new SQLiteConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testupdate" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
item = conn.Select<TestConnectionExt>().First();
|
||||
affrows = conn.Update<TestConnectionExt>().SetSource(item).Set(a => a.title, "testupdated").ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Delete() {
|
||||
var affrows = 0;
|
||||
using (var conn = new SQLiteConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testdelete" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
affrows = conn.Delete<TestConnectionExt>().Where(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Select() {
|
||||
var list = new List<TestConnectionExt>();
|
||||
var affrows = 0;
|
||||
using (var conn = new SQLiteConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testselect" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
list = conn.Select<TestConnectionExt>().Where(a => a.id == item.id).ToList();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Single(list);
|
||||
}
|
||||
|
||||
class TestConnectionExt {
|
||||
public Guid id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.AdoNetExtensions.SqlConnectionExtensions {
|
||||
public class Methods {
|
||||
|
||||
string _connectString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=5";
|
||||
|
||||
public Methods() {
|
||||
g.sqlserver.CodeFirst.SyncStructure<TestConnectionExt>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Insert() {
|
||||
var affrows = 0;
|
||||
using (var conn = new SqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testinsert" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Update() {
|
||||
var affrows = 0;
|
||||
using (var conn = new SqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testupdate" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
item = conn.Select<TestConnectionExt>().First();
|
||||
affrows = conn.Update<TestConnectionExt>().SetSource(item).Set(a => a.title, "testupdated").ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Delete() {
|
||||
var affrows = 0;
|
||||
using (var conn = new SqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testdelete" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
affrows = conn.Delete<TestConnectionExt>().Where(item).ExecuteAffrows();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Equal(1, affrows);
|
||||
}
|
||||
[Fact]
|
||||
public void Select() {
|
||||
var list = new List<TestConnectionExt>();
|
||||
var affrows = 0;
|
||||
using (var conn = new SqlConnection(_connectString)) {
|
||||
var item = new TestConnectionExt { title = "testselect" };
|
||||
affrows = conn.Insert<TestConnectionExt>().AppendData(item).ExecuteAffrows();
|
||||
Assert.Equal(1, affrows);
|
||||
list = conn.Select<TestConnectionExt>().Where(a => a.id == item.id).ToList();
|
||||
conn.Close();
|
||||
}
|
||||
Assert.Single(list);
|
||||
}
|
||||
|
||||
class TestConnectionExt {
|
||||
public Guid id { get; set; }
|
||||
public string title { get; set; }
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user