Skip to content

Commit

Permalink
修复文档和代码不一致的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
azhai committed Jun 17, 2019
1 parent 3a677ff commit 9afca35
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*.vscode
bin/
obj/
src/Example/appsettings.json
tests/
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ db.Products.Delete(products);
```csharp
using System;

// 分库分表,假设产品按年分库,按月分表
// 当前2019年6月表名为`db_product_y2019`.`t_product_m201906`
int pageNo = 1, pageSize = 10;
DateTime someDay = DateTime.Now.AddMonths(-3);
var referName = products.CurrentName + someDay.ToString("yyyyMM");
var products = db.Products.Where("created_at", ">=", someDay.ToString("yyyy-MM-dd"));
products.DbNameRange = query.GetDbName(true)
products.tableFilter = name => name.CompareTo(referName) >= 0;
// 找出类似db_product_y2019等数据库,并倒序排列数据库和表名
products.DbNameMatch = "db_product_y%";
products.IsTableNameDesc = true;
// 数据表名称限制为 t_products_m201906
products.tableFilter = (table, db) => table.CompareTo(referName) >= 0;
var count = unchecked((int)products.CountSharding());
// Console.WriteLine("Count={0} PageNo={1}", count, pageNo);
var rows = products.PaginateSharding(pageNo, pageSize);
Expand Down
10 changes: 3 additions & 7 deletions src/Dubonnet/DubonArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public partial class DubonQuery<M>
public const int COUMT_IS_EMPTY = -1; // 尚未计数
public const int COUMT_IS_DYNAMIC = -2; // 不缓存计数

public string DbNameRange = "";
public string DbNameMatch = "";
public bool IsTableNameDesc = false;
public ITableCounter tableCounter { get; set; }
public Func<string, string, bool> tableFilter { get; set; }
Expand All @@ -67,7 +67,7 @@ protected List<string> filterShardingNames(bool reload = false)
{
tableCounter = new TableCountDict();
tables = new List<string>();
foreach (var table in ListTable(CurrentName, DbNameRange))
foreach (var table in ListTable(CurrentName, DbNameMatch))
{
if (tableFilter == null || tableFilter(table.TABLE_NAME, table.DbName()))
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public long CountSharding()
/// <summary>
/// Select step by step.
/// </summary>
public List<M> PaginateSharding(int page = 1, int size = 100, int oriention = 0)
public List<M> PaginateSharding(int page = 1, int size = 100)
{
if (page <= 0)
{
Expand All @@ -136,10 +136,6 @@ public List<M> PaginateSharding(int page = 1, int size = 100, int oriention = 0)
{
throw new ArgumentException("Param 'size' should be greater than 0", nameof(size));
}
if (0 != oriention)
{
IsTableNameDesc = oriention < 0;
}

long offset = (page - 1) * size;
var result = new List<M>();
Expand Down
37 changes: 19 additions & 18 deletions src/Dubonnet/DubonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public static readonly ConcurrentDictionary<string, List<TableSchema>>
public static readonly ConcurrentDictionary<Type, List<string>>
paramNameCache = new ConcurrentDictionary<Type, List<string>>();

public static string GetDbCond(string engine, string dbName = "")
public static string GetDbCond(string engine, string dbName)
{
if ("" == dbName)
if ("*" == dbName || "%" == dbName)
{
return "";
}
Expand Down Expand Up @@ -141,61 +141,62 @@ public string GetDbName(bool onlyFunc = false)
return db.Conn.ExecuteScalar<string>(rawSql);
}

public IEnumerable<TableSchema> GetTables(string tableName, string dbName = "", bool isDesc = false)
public IEnumerable<TableSchema> GetTables(string tableName, string dbNameMatch = "", bool isDesc = false)
{
tableName = tableName + "%";
var rawSql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE ? ORDER BY TABLE_NAME";
if ("" == dbNameMatch)
{
dbNameMatch = GetDbName(true);
}
var where = " WHERE" + GetDbCond(db.DriverType, dbNameMatch) + " TABLE_NAME LIKE ?";
var rawSql = "SELECT * FROM INFORMATION_SCHEMA.TABLES" + where + " ORDER BY TABLE_NAME";
switch (db.DriverType) {
case "sqlsrv":
rawSql = "SELECT TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME"
+ " FROM [INFORMATION_SCHEMA].TABLES"
+ " WHERE" + GetDbCond(db.DriverType, dbName)
+ " TABLE_NAME LIKE ? ORDER BY TABLE_NAME";
+ where + " ORDER BY TABLE_NAME";
break;
case "mysql":
tableName = tableName.Replace("_", "\\_");
rawSql = "SELECT TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,TABLE_ROWS,AUTO_INCREMENT"
+ " FROM `information_schema`.TABLES"
+ " WHERE" + GetDbCond(db.DriverType, dbName)
+ " TABLE_NAME LIKE ? ORDER BY TABLE_NAME";
+ where + " ORDER BY TABLE_NAME";
break;
}
if (isDesc)
{
rawSql += " DESC";
}
var (sql, dict) = instance.CompileSql(rawSql, new object[]{tableName});
var (sql, dict) = instance.CompileSql(rawSql, new object[]{tableName + "%"});
return db.Conn.Query<TableSchema>(sql, dict);
}

public IEnumerable<ColumnSchema> GetColumns(string tableName, string dbName = "")
public IEnumerable<ColumnSchema> GetColumns(string tableName)
{
var rawSql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=?";
var where = " WHERE" + GetDbCond(db.DriverType, GetDbName(true)) + " TABLE_NAME = ?";
var rawSql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS" + where + " ORDER BY ORDINAL_POSITION";
switch (db.DriverType) {
case "sqlsrv":
rawSql = "SELECT TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE,"
+ "COLUMN_DEFAULT,IS_NULLABLE,ORDINAL_POSITION"
+ " FROM [INFORMATION_SCHEMA].COLUMNS"
+ " WHERE" + GetDbCond(db.DriverType, dbName)
+ " TABLE_NAME=? ORDER BY ORDINAL_POSITION";
+ where + " ORDER BY ORDINAL_POSITION";
break;
case "mysql":
rawSql = "SELECT TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,DATA_TYPE,"
+ "COLUMN_DEFAULT,IS_NULLABLE,COLUMN_KEY,EXTRA,ORDINAL_POSITION"
+ " FROM `information_schema`.COLUMNS"
+ " WHERE" + GetDbCond(db.DriverType, dbName)
+ " TABLE_NAME=? ORDER BY ORDINAL_POSITION";
+ where + " ORDER BY ORDINAL_POSITION";
break;
}
var (sql, dict) = instance.CompileSql(rawSql, new object[]{tableName});
return db.Conn.Query<ColumnSchema>(sql, dict);
}

public List<TableSchema> ListTable(string name, string dbName = "", bool refresh = false)
public List<TableSchema> ListTable(string name, string dbNameMatch = "", bool refresh = false)
{
if (refresh || !tableNameCache.TryGetValue(name, out List<TableSchema> tables))
{
tables = tableNameCache[name] = GetTables(name, dbName).AsList();
tables = tableNameCache[name] = GetTables(name, dbNameMatch).AsList();
}
return tables;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dubonnet/Dubonnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<ApplicationIcon>dubonnet.ico</ApplicationIcon>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageVersion>2.2.2</PackageVersion>
<PackageVersion>2.2.3</PackageVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
7 changes: 4 additions & 3 deletions src/Example/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ public DubonQuery<Mobile> GetMobileQuery(string start, string stop = "")
{
var query = db.Mobiles.Where("prefix", ">=", start);
var startPre = query.CurrentName + (start+"000").Substring(0, 3);
query.DbNameRange = query.GetDbName() + "%";
// query.DbNameMatch = query.GetDbName(true);
// query.IsTableNameDesc = false;
if (string.IsNullOrEmpty(stop))
{
query.tableFilter = (string table, string db) => table.CompareTo(startPre) >= 0;
query.tableFilter = (table, db) => table.CompareTo(startPre) >= 0;
}
else
{
var stopPre = query.CurrentName + (stop+"999").Substring(0, 3);
query.Where("prefix", "<=", stop + "9999999");
query.tableFilter = (string table, string db) => table.CompareTo(startPre) >= 0
query.tableFilter = (table, db) => table.CompareTo(startPre) >= 0
&& table.CompareTo(stopPre) <= 0;
}
return query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"ConnectionStrings": {
"ConnName": "Mysql",
"SqlServer": "server=192.168.2.107,1433;user id=sa;password=pass;database=Mobile",
"SqlServer": "server=192.168.0.100,1433;user id=sa;password=pass;database=Mobile",
"Mysql": "server=127.0.0.1;user id=dba;password=pass;port=3306;database=db_mobile;SslMode=none",
"Redis": "127.0.0.1:6379,defaultDatabase=0"
},
Expand Down

0 comments on commit 9afca35

Please sign in to comment.