初始化
This commit is contained in:
15
Database/Base/ColumnSchema.vb
Normal file
15
Database/Base/ColumnSchema.vb
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
Namespace Database.Base
|
||||
''' <summary>
|
||||
''' Contains the schema of a single DB column.
|
||||
''' </summary>
|
||||
Public Class ColumnSchema
|
||||
Public ColumnName As String
|
||||
Public ColumnType As String
|
||||
Public Length As Integer
|
||||
Public IsNullable As Boolean
|
||||
Public DefaultValue As String
|
||||
Public IsIdentity As Boolean
|
||||
Public IsCaseSensitivity As Boolean? = Nothing
|
||||
End Class
|
||||
End NameSpace
|
||||
297
Database/Base/CommandHelpers.vb
Normal file
297
Database/Base/CommandHelpers.vb
Normal file
@@ -0,0 +1,297 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Base
|
||||
|
||||
Public MustInherit Class CommandHelpers
|
||||
Public Overridable Function Search(param As SearchParams) As String
|
||||
Dim searchString As New StringBuilder
|
||||
|
||||
'基础查询
|
||||
searchString.Append("Select")
|
||||
searchString.Append(" ")
|
||||
searchString.Append($"{String.Join(",", param.SearchColNames)}")
|
||||
searchString.Append(" ")
|
||||
searchString.Append("From")
|
||||
searchString.Append(" ")
|
||||
searchString.Append($"`{param.TableName}`")
|
||||
|
||||
'筛选条件
|
||||
If param.SearchCondition IsNot Nothing Then
|
||||
If param.SearchCondition.Count > 0 Then
|
||||
searchString.Append(" ")
|
||||
searchString.Append("Where")
|
||||
For i As Integer = 0 To param.SearchCondition.Count - 1
|
||||
If i > 0 Then
|
||||
searchString.Append(" ")
|
||||
searchString.Append(param.SearchCondition(i).LogicPrevious.ToString())
|
||||
End If
|
||||
searchString.Append(param.SearchCondition(i).ToString())
|
||||
Next
|
||||
End If
|
||||
End If
|
||||
|
||||
'排序与排序方式
|
||||
If param.OrderType <> SearchParams.OrderTypeEnum.None Then
|
||||
searchString.Append($" Order By {param.OrderColName} {param.OrderType}")
|
||||
End If
|
||||
|
||||
'返回结果行数
|
||||
If param.Limit > -1 Then
|
||||
searchString.Append($" Limit {param.Limit}")
|
||||
End If
|
||||
|
||||
searchString.Append(";")
|
||||
Return searchString.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function SearchAll(tableName As String) As String
|
||||
Return $"Select * FROM `{tableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchAll(tableName As String, condition As String) As String
|
||||
Return $"Select * FROM `{tableName}` WHERE {condition};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Search(columnName As List(Of String), tableName As String) As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"`{columnName(i)}`")
|
||||
Else
|
||||
colNameString.Append($",`{columnName(i)}`")
|
||||
End If
|
||||
Next
|
||||
|
||||
Return $"Select {colNameString} FROM `{tableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Search(columnName As String, tableName As String) As String
|
||||
Return $"Select {columnName} FROM `{tableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Search(columnName As String, tableName As String, condition As String) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` WHERE {condition};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Search(columnName As String, tableName As String, condition As String, limit As Integer) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` WHERE {condition} Limit {limit};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchDistinct(columnName As String, tableName As String) As String
|
||||
Return $"Select Distinct {columnName} FROM `{tableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchDistinct(columnName As String, tableName As String, condition As String) As String
|
||||
Return $"Select Distinct {columnName} FROM `{tableName}` WHERE {condition};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchDescOrder(columnName As String, tableName As String, orderCol As String) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` Order By {orderCol} Desc;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchDescOrder(columnName As String, tableName As String, orderCol As String, limit As Integer) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` Order By {orderCol} Desc Limit {limit};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchDescOrder(columnName As String, ByVal tableName As String, ByVal condition As String, ByVal orderCol As String) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` WHERE {condition} Order By `{orderCol}` Desc;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchDescOrder(columnName As String, ByVal tableName As String, ByVal condition As String, ByVal orderCol As String, limit As Integer) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` WHERE {condition} Order By `{orderCol}` Desc Limit {limit};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchAscOrder(ByVal columnName As String, ByVal tableName As String, ByVal orderCol As String) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` Order By {orderCol} Asc;"
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function SearchAscOrder(ByVal columnName As String, ByVal tableName As String, ByVal condition As String, ByVal orderCol As String) As String
|
||||
Return $"Select {columnName} FROM `{tableName}` WHERE {condition} Order By {orderCol} Asc;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchNullTable(tableName As String) As String
|
||||
Return $"Select * FROM `{tableName}` Where Limit 0;"
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function Insert(ByVal tableName As String, ByVal values As String) As String
|
||||
Return $"Insert into `{tableName}` Values ( {values} );"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Insert(ByVal tableName As String, ByVal colNames As String, ByVal values As String) As String
|
||||
Return $"Insert into `{tableName}` ({colNames}) Values ( {values} );"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Insert(tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"`{keyValuePair.Key}`")
|
||||
values.Append($"'{keyValuePair.Value}'")
|
||||
Else
|
||||
colNames.Append($",`{keyValuePair.Key}`")
|
||||
values.Append($",'{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
Public Overridable Function InsertByParameters(tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"`{keyValuePair.Key}`")
|
||||
values.Append($"{keyValuePair.Value}")
|
||||
Else
|
||||
colNames.Append($",`{keyValuePair.Key}`")
|
||||
values.Append($",{keyValuePair.Value}")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function AddCol(ByVal tableName As String, ByVal colName As String, ByVal colType As String, Optional isNull As Boolean = True) As String
|
||||
Return $"Alter Table `{tableName}` Add `{colName}` {colType} {IIf(isNull, "Default Null", "Not Null")};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function AddCol(ByVal tableName As String, colParam As DatabaseData) As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"Alter Table `{tableName}` ")
|
||||
sb.Append("Add ")
|
||||
sb.Append(colParam.ToAddColString())
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function AddCols(tableName As String, colList As List(Of DatabaseData)) As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"Alter Table `{tableName}` ")
|
||||
sb.Append("Add ")
|
||||
sb.Append("( ")
|
||||
|
||||
sb.Append(colList(0).ToAddColString())
|
||||
For i As Integer = 1 To colList.Count - 1
|
||||
sb.Append($",{colList(i).ToAddColString()}")
|
||||
Next
|
||||
|
||||
sb.Append(");")
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function Update(ByVal tableName As String, ByVal destStr As String, ByVal condition As String) As String
|
||||
Return $"Update `{tableName}` Set {destStr} Where {condition};"
|
||||
End Function
|
||||
|
||||
Public Overridable Function Update(ByVal tableName As String, dicNameValues As Dictionary(Of String, String), ByVal condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"`{keyValuePair.Key}` = '{keyValuePair.Value}'")
|
||||
Else
|
||||
destStr.Append($",`{keyValuePair.Key}` = '{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return Update(tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
Public Overridable Function Update(ByVal tableName As String, names() As String, values() As String, condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
If names.Length <> values.Length Then
|
||||
Throw New Exception("DBHelpers_Update:names.Length <> values.Length")
|
||||
End If
|
||||
|
||||
For i As Integer = 0 To names.Length - 1
|
||||
If i = 0 Then
|
||||
destStr.Append($"{names(i)} = '{values(i)}'")
|
||||
Else
|
||||
destStr.Append($",{names(i)} = '{values(i)}'")
|
||||
End If
|
||||
Next
|
||||
|
||||
Return Update(tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
Public Overridable Function DeleteRows(ByVal tableName As String, ByVal condition As String) As String
|
||||
Return $"Delete From `{tableName}` Where {condition};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DeleteTable(ByVal tableName As String) As String
|
||||
Return $"Delete From `{tableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function DropCol(ByVal tableName As String, ByVal colName As String) As String
|
||||
Return $"Alter Table `{tableName}` Drop Column `{colName}`;"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 删除数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropTable(ByVal tableName As String) As String
|
||||
Return $"Drop Table `{tableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function CreateTable(ByVal tableName As String, ByVal createStr As String) As String
|
||||
Return $"Create Table `{tableName}` ( {createStr} );"
|
||||
End Function
|
||||
|
||||
Public Overridable Function CreateTableWhenNotExists(tableName As String, createStr As String) As String
|
||||
Return $"Create Table if not exists `{tableName}` ( {createStr} );"
|
||||
End Function
|
||||
|
||||
|
||||
Public Overridable Function CreateLikeTable(tableName As String, baseTableName As String) As String
|
||||
Return $"create table `{tableName}` like `{baseTableName}`;"
|
||||
End Function
|
||||
|
||||
Public Overridable Function CreateLikeTableNotExists(tableName As String, baseTableName As String) As String
|
||||
Return $"create table if not exists `{tableName}` like `{baseTableName}`;"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 创建表,同时复制基础表数据(不包含原表索引与主键)
|
||||
''' 若想复制表结构加数据,则先复制表结构创建表,再拷贝数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="baseTableName">基础表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function CreateCopyTable(tableName As String, baseTableName As String) As String
|
||||
Return $"create table `{tableName}` as select * from `{baseTableName}`;"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 不存在表时即创建表,同时复制基础表数据(不包含原表索引与主键)
|
||||
''' 若想复制表结构加数据,则先复制表结构创建表,再拷贝数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="baseTableName">基础表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function CreateCopyTableNotExists(tableName As String, baseTableName As String) As String
|
||||
Return $"create table if not exists `{tableName}` as select * from `{baseTableName}`;"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 复制基础表数据到新表中
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="baseTableName">基础表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function InsertCopyTable(tableName As String, baseTableName As String) As String
|
||||
Return $"insert into `{tableName}` select * from `{baseTableName}`;"
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
94
Database/Base/DatabaseData.vb
Normal file
94
Database/Base/DatabaseData.vb
Normal file
@@ -0,0 +1,94 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Base
|
||||
Public Class DatabaseData
|
||||
Enum TypeEnum
|
||||
[Bit]
|
||||
[Char]
|
||||
[Date]
|
||||
[DateTime]
|
||||
[Double]
|
||||
[Enum]
|
||||
[Float]
|
||||
[Int]
|
||||
[IntUnsigned]
|
||||
[Json]
|
||||
[Text]
|
||||
[Time]
|
||||
Varchar
|
||||
[Year]
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As TypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case TypeEnum.Char, TypeEnum.Varchar
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case TypeEnum.Int
|
||||
sb.Append($" {DataType}")
|
||||
If IsAutoIncrement Then sb.Append($" AUTO_INCREMENT")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
12
Database/Base/DatabaseSchema.vb
Normal file
12
Database/Base/DatabaseSchema.vb
Normal file
@@ -0,0 +1,12 @@
|
||||
Imports System.Collections.Generic
|
||||
|
||||
Namespace Database.Base
|
||||
|
||||
''' <summary>
|
||||
''' Contains the entire database schema
|
||||
''' </summary>
|
||||
Public Class DatabaseSchema
|
||||
Public Tables As List(Of TableSchema) = New List(Of TableSchema)()
|
||||
Public Views As List(Of ViewSchema) = New List(Of ViewSchema)()
|
||||
End Class
|
||||
End NameSpace
|
||||
11
Database/Base/ForeignKeySchema.vb
Normal file
11
Database/Base/ForeignKeySchema.vb
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
Namespace Database.Base
|
||||
Public Class ForeignKeySchema
|
||||
Public TableName As String
|
||||
Public ColumnName As String
|
||||
Public ForeignTableName As String
|
||||
Public ForeignColumnName As String
|
||||
Public CascadeOnDelete As Boolean
|
||||
Public IsNullable As Boolean
|
||||
End Class
|
||||
End NameSpace
|
||||
16
Database/Base/IndexSchema.vb
Normal file
16
Database/Base/IndexSchema.vb
Normal file
@@ -0,0 +1,16 @@
|
||||
Imports System.Collections.Generic
|
||||
|
||||
Namespace Database.Base
|
||||
|
||||
|
||||
Public Class IndexSchema
|
||||
Public IndexName As String
|
||||
Public IsUnique As Boolean
|
||||
Public Columns As List(Of IndexColumn)
|
||||
End Class
|
||||
|
||||
Public Class IndexColumn
|
||||
Public ColumnName As String
|
||||
Public IsAscending As Boolean
|
||||
End Class
|
||||
End NameSpace
|
||||
7
Database/Base/InsertParams.vb
Normal file
7
Database/Base/InsertParams.vb
Normal file
@@ -0,0 +1,7 @@
|
||||
Namespace Database.Base
|
||||
Public Class InsertParams
|
||||
Public Property TableName() As String
|
||||
|
||||
Public Property InsertKeyValue As Dictionary(Of String, String)
|
||||
End Class
|
||||
End Namespace
|
||||
71
Database/Base/SearchCondition.vb
Normal file
71
Database/Base/SearchCondition.vb
Normal file
@@ -0,0 +1,71 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Base
|
||||
Public Class SearchCondition
|
||||
Enum ConditionType
|
||||
LessThan
|
||||
GreaterThen
|
||||
EqualTo
|
||||
LessThanOrEqualTo
|
||||
GreaterThenOrEqualTo
|
||||
End Enum
|
||||
|
||||
Enum LogicType
|
||||
[And]
|
||||
[Or]
|
||||
[Not]
|
||||
End Enum
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 判断列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 判断条件
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Condition() As ConditionType = ConditionType.EqualTo
|
||||
|
||||
''' <summary>
|
||||
''' 判断值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前条件与上一个条件的逻辑关系
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property LogicPrevious() As LogicType = LogicType.And
|
||||
|
||||
''' <summary>
|
||||
''' 将当前条件转换为字符串,不支持将条件逻辑关系同时转换
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Overrides Function ToString() As String
|
||||
Dim stringBuilder As New StringBuilder
|
||||
stringBuilder.Append(" ")
|
||||
stringBuilder.Append(ColName)
|
||||
|
||||
Select Case Condition
|
||||
Case ConditionType.EqualTo
|
||||
stringBuilder.Append("=")
|
||||
Case ConditionType.LessThan
|
||||
stringBuilder.Append("<")
|
||||
Case ConditionType.LessThanOrEqualTo
|
||||
stringBuilder.Append("<=")
|
||||
Case ConditionType.GreaterThen
|
||||
stringBuilder.Append(">")
|
||||
Case ConditionType.GreaterThenOrEqualTo
|
||||
stringBuilder.Append(">=")
|
||||
End Select
|
||||
|
||||
stringBuilder.Append($"'{ColValue}'")
|
||||
|
||||
Return stringBuilder.ToString()
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
46
Database/Base/SearchParams.vb
Normal file
46
Database/Base/SearchParams.vb
Normal file
@@ -0,0 +1,46 @@
|
||||
Namespace Database.Base
|
||||
Public Class SearchParams
|
||||
Enum OrderTypeEnum
|
||||
None
|
||||
Desc
|
||||
Asc
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 查询条件的表名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property TableName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当IsSearchAllCols = False时,查询返回列名集合
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property SearchColNames() As String()
|
||||
|
||||
''' <summary>
|
||||
''' 查询的条件
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property SearchCondition() As List(Of SearchCondition)
|
||||
|
||||
''' <summary>
|
||||
''' 排序方式
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property OrderType As OrderTypeEnum = OrderTypeEnum.None
|
||||
|
||||
''' <summary>
|
||||
''' 但需要排序时排序列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property OrderColName() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 从返回结果提取指定行的内容
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Limit() As Integer = 0
|
||||
End Class
|
||||
End Namespace
|
||||
14
Database/Base/TableSchema.vb
Normal file
14
Database/Base/TableSchema.vb
Normal file
@@ -0,0 +1,14 @@
|
||||
Imports System.Collections.Generic
|
||||
|
||||
Namespace Database.Base
|
||||
|
||||
|
||||
Public Class TableSchema
|
||||
Public TableName As String
|
||||
Public TableSchemaName As String
|
||||
Public Columns As List(Of ColumnSchema)
|
||||
Public PrimaryKey As List(Of String)
|
||||
Public ForeignKeys As List(Of ForeignKeySchema)
|
||||
Public Indexes As List(Of IndexSchema)
|
||||
End Class
|
||||
End NameSpace
|
||||
71
Database/Base/TriggerBuilder.vb
Normal file
71
Database/Base/TriggerBuilder.vb
Normal file
@@ -0,0 +1,71 @@
|
||||
Imports System.Collections.Generic
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Base
|
||||
|
||||
Public Module TriggerBuilder
|
||||
Public Function GetForeignKeyTriggers(ByVal dt As TableSchema) As IList(Of TriggerSchema)
|
||||
Dim result As IList(Of TriggerSchema) = New List(Of TriggerSchema)()
|
||||
For Each fks As ForeignKeySchema In dt.ForeignKeys
|
||||
result.Add(GenerateInsertTrigger(fks))
|
||||
result.Add(GenerateUpdateTrigger(fks))
|
||||
result.Add(GenerateDeleteTrigger(fks))
|
||||
Next
|
||||
Return result
|
||||
End Function
|
||||
|
||||
Private Function MakeTriggerName(ByVal fks As ForeignKeySchema, ByVal prefix As String) As String
|
||||
Return prefix & "_" & fks.TableName & "_" & fks.ColumnName & "_" & fks.ForeignTableName & "_" & fks.ForeignColumnName
|
||||
End Function
|
||||
|
||||
Public Function GenerateInsertTrigger(ByVal fks As ForeignKeySchema) As TriggerSchema
|
||||
Dim trigger As TriggerSchema = New TriggerSchema()
|
||||
trigger.Name = MakeTriggerName(fks, "fki")
|
||||
trigger.Type = TriggerType.Before
|
||||
trigger.Event = TriggerEvent.Insert
|
||||
trigger.Table = fks.TableName
|
||||
Dim nullString As String = ""
|
||||
|
||||
If fks.IsNullable Then
|
||||
nullString = " NEW." & fks.ColumnName & " IS NOT NULL AND"
|
||||
End If
|
||||
|
||||
trigger.Body = "SELECT RAISE(ROLLBACK, 'insert on table " & fks.TableName & " violates foreign key constraint " & trigger.Name & "')" & " WHERE" & nullString & " (SELECT " & fks.ForeignColumnName & " FROM " & fks.ForeignTableName & " WHERE " & fks.ForeignColumnName & " = NEW." & fks.ColumnName & ") IS NULL; "
|
||||
Return trigger
|
||||
End Function
|
||||
|
||||
Public Function GenerateUpdateTrigger(ByVal fks As ForeignKeySchema) As TriggerSchema
|
||||
Dim trigger As TriggerSchema = New TriggerSchema()
|
||||
trigger.Name = MakeTriggerName(fks, "fku")
|
||||
trigger.Type = TriggerType.Before
|
||||
trigger.Event = TriggerEvent.Update
|
||||
trigger.Table = fks.TableName
|
||||
Dim triggerName As String = trigger.Name
|
||||
Dim nullString As String = ""
|
||||
|
||||
If fks.IsNullable Then
|
||||
nullString = " NEW." & fks.ColumnName & " IS NOT NULL AND"
|
||||
End If
|
||||
|
||||
trigger.Body = "SELECT RAISE(ROLLBACK, 'update on table " & fks.TableName & " violates foreign key constraint " & triggerName & "')" & " WHERE" & nullString & " (SELECT " & fks.ForeignColumnName & " FROM " & fks.ForeignTableName & " WHERE " & fks.ForeignColumnName & " = NEW." & fks.ColumnName & ") IS NULL; "
|
||||
Return trigger
|
||||
End Function
|
||||
|
||||
Public Function GenerateDeleteTrigger(ByVal fks As ForeignKeySchema) As TriggerSchema
|
||||
Dim trigger As TriggerSchema = New TriggerSchema()
|
||||
trigger.Name = MakeTriggerName(fks, "fkd")
|
||||
trigger.Type = TriggerType.Before
|
||||
trigger.Event = TriggerEvent.Delete
|
||||
trigger.Table = fks.ForeignTableName
|
||||
Dim triggerName as String = trigger.Name
|
||||
|
||||
If Not fks.CascadeOnDelete Then
|
||||
trigger.Body = "SELECT RAISE(ROLLBACK, 'delete on table " & fks.ForeignTableName & " violates foreign key constraint " & triggerName & "')" & " WHERE (SELECT " & fks.ColumnName & " FROM " & fks.TableName & " WHERE " & fks.ColumnName & " = OLD." & fks.ForeignColumnName & ") IS NOT NULL; "
|
||||
Else
|
||||
trigger.Body = "DELETE FROM [" & fks.TableName & "] WHERE " & fks.ColumnName & " = OLD." & fks.ForeignColumnName & "; "
|
||||
End If
|
||||
|
||||
Return trigger
|
||||
End Function
|
||||
End Module
|
||||
End NameSpace
|
||||
20
Database/Base/TriggerSchema.vb
Normal file
20
Database/Base/TriggerSchema.vb
Normal file
@@ -0,0 +1,20 @@
|
||||
Namespace Database.Base
|
||||
Public Enum TriggerEvent
|
||||
Delete
|
||||
Update
|
||||
Insert
|
||||
End Enum
|
||||
|
||||
Public Enum TriggerType
|
||||
After
|
||||
Before
|
||||
End Enum
|
||||
|
||||
Public Class TriggerSchema
|
||||
Public Name As String
|
||||
Public [Event] As TriggerEvent
|
||||
Public Type As TriggerType
|
||||
Public Body As String
|
||||
Public Table As String
|
||||
End Class
|
||||
End Namespace
|
||||
17
Database/Base/ViewSchema.vb
Normal file
17
Database/Base/ViewSchema.vb
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
Namespace Database.Base
|
||||
''' <summary>
|
||||
''' Describes a single view schema
|
||||
''' </summary>
|
||||
Public Class ViewSchema
|
||||
''' <summary>
|
||||
''' Contains the view name
|
||||
''' </summary>
|
||||
Public ViewName As String
|
||||
|
||||
''' <summary>
|
||||
''' Contains the view SQL statement
|
||||
''' </summary>
|
||||
Public ViewSql As String
|
||||
End Class
|
||||
End NameSpace
|
||||
748
Database/DbCmdHelper.vb
Normal file
748
Database/DbCmdHelper.vb
Normal file
@@ -0,0 +1,748 @@
|
||||
Imports System.Text
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 数据库语句助手
|
||||
''' 时间:2020-12-21
|
||||
''' 作者:ML
|
||||
''' 版本:1.0
|
||||
'''
|
||||
''' 注意:添加一条数据库帮助语句时,需要考虑Mysql/Sqlite/Mssql等数据库是否支持命令,不支持则需要在对应帮助类中重写该帮助语句
|
||||
''' 注意:Sqlite数据库与大多数据库不相同,DB开头数据库语句大多不适用
|
||||
'''
|
||||
''' </summary>
|
||||
Public MustInherit Class DbCmdHelper
|
||||
Protected FiledSuffix As Char
|
||||
Protected FiledPrefix As Char
|
||||
|
||||
Public Shared Function CreateCmdHelper(type As DbExecutor.DbTypeEnum) As DbCmdHelper
|
||||
Select Case type
|
||||
Case DbExecutor.DbTypeEnum.Mysql
|
||||
Return New MysqlCmdHelper()
|
||||
Case DbExecutor.DbTypeEnum.Mssql
|
||||
Return New MssqlCmdHelper()
|
||||
Case DbExecutor.DbTypeEnum.Sqlite
|
||||
Return New SqliteCmdHelper()
|
||||
Case Else
|
||||
Throw New Exception($"CreateCmdHelper :Unknown Type {type}")
|
||||
End Select
|
||||
End Function
|
||||
|
||||
|
||||
#Region "访问单数据库连接"
|
||||
''' <summary>
|
||||
''' 查询指定数据表符合条件的所有数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="condition">查询条件,</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchAll(tableName As String, Optional condition As String = "") As String
|
||||
If String.IsNullOrWhiteSpace(condition) Then
|
||||
Return $"Select * FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select * FROM {FiledSuffix}{tableName}{FiledPrefix} WHERE {condition};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="columnName">列名集合,需要返回多列时用','符号分隔列名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Search(columnName As String, tableName As String, Optional condition As String = "") As String
|
||||
If String.IsNullOrWhiteSpace(condition) Then
|
||||
Return $"Select {columnName} FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select {columnName} FROM {FiledSuffix}{tableName}{FiledPrefix} WHERE {condition};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="columnName">表名</param>
|
||||
''' <param name="tableName">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Search(columnName As List(Of String), tableName As String, Optional condition As String = "") As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
End If
|
||||
Next
|
||||
|
||||
If String.IsNullOrWhiteSpace(condition) Then
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix} Where {condition};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchOrder(columnName As List(Of String), tableName As String, Optional orderString As String = "") As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
End If
|
||||
Next
|
||||
|
||||
If String.IsNullOrWhiteSpace(orderString) Then
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix} {orderString};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定表包含的内容行数
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <param name="condition">查询条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchCount(tableName As String, Optional condition As String = "") As String
|
||||
Return Search("count(*)", tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchTableInfo(tableName As String) As String
|
||||
Return $"Select * from information_schema.tables where table_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表是否存在的信息,返回查询当前表在数据库中存在的数量
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchTableExists(tableName As String) As String
|
||||
Return $"Select count(*) from information_schema.tables where table_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colNames">列名字符串</param>
|
||||
''' <param name="values">列值字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Insert(tableName As String, colNames As String, values As String) As String
|
||||
Dim strresult As String = $"Insert into {FiledSuffix}{tableName}{FiledPrefix} ({colNames}) Values ( {values} );"
|
||||
Return strresult
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="dicNameValues">列名与列值键值对</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Insert(tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"'{keyValuePair.Value}'")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",'{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
Public Overridable Function Insert2(tableName As String, dicNameValues As Dictionary(Of String, Object )) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, Object) In dicNameValues
|
||||
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"'{keyValuePair.Value}'")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",'{keyValuePair.Value}'")
|
||||
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行,通过命令参数方式执行时使用
|
||||
''' </summary>
|
||||
''' <param name="tableName"></param>
|
||||
''' <param name="dicNameValues"></param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function InsertParam(tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"{keyValuePair.Value}")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",{keyValuePair.Value}")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行,通过命令参数方式执行时使用,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colNames">字段列表</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function InsertParam(tableName As String, colNames As List(Of String)) As String
|
||||
Dim colNameString As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If colNameString.Length = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{colName}{FiledPrefix}")
|
||||
values.Append($"@{colName}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{colName}{FiledPrefix}")
|
||||
values.Append($",@{colName}")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNameString.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定表数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="destStr">更新字符串</param>
|
||||
''' <param name="condition"></param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Update(tableName As String, destStr As String, condition As String) As String
|
||||
Return $"Update {FiledSuffix}{tableName}{FiledPrefix} Set {destStr} Where {condition};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定表数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="dicNameValues">更新列名与列值键值对</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Update(tableName As String, dicNameValues As Dictionary(Of String, String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix} = '{keyValuePair.Value}'")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix} = '{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return Update(tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="colNames">更新列名的集合</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function UpdateParam(tableName As String, colNames As List(Of String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
End If
|
||||
Next
|
||||
Return Update(tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据表增加一列数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">列名</param>
|
||||
''' <param name="colType">列类型</param>
|
||||
''' <param name="isNull">是否允许为空</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function AddCol(tableName As String, colName As String, colType As String, Optional isNull As Boolean = True) As String
|
||||
Return $"Alter Table {FiledSuffix}{tableName}{FiledPrefix} Add {FiledSuffix}{colName}{FiledPrefix} {colType} {IIf(isNull, "Default Null", "Not Null")};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 数据表删除一列数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">需要删除的列名,仅一列</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropCol(tableName As String, colName As String) As String
|
||||
Return $"Alter Table {FiledSuffix}{tableName}{FiledPrefix} Drop Column {FiledSuffix}{colName}{FiledPrefix};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 删除指定表多行数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DeleteRows(tableName As String, condition As String) As String
|
||||
Return $"Delete From {FiledSuffix}{tableName}{FiledPrefix} Where {condition};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 创建数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function CreateTable(tableName As String, createStr As String) As String
|
||||
Return $"Create Table {FiledSuffix}{tableName}{FiledPrefix} ( {createStr} );"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 创建数据表,如果存在则不创建
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function CreateTableWhenNotExists(tableName As String, createStr As String) As String
|
||||
Return $"Create Table if not exists {FiledSuffix}{tableName}{FiledPrefix} ( {createStr} );"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空数据表,表依旧存在
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DeleteTable(tableName As String) As String
|
||||
Return $"Delete From {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 删除数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropTable(tableName As String) As String
|
||||
Return $"Drop Table {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 删除数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropTableWhenExists(tableName As String) As String
|
||||
Return $"Drop Table If Exists {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
End Function
|
||||
#End Region
|
||||
|
||||
|
||||
#Region "访问多数据库连接"
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="ColsName">列名集合,需要返回多列时用','符号分隔列名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearch(dbName As String, colsName As String, tableName As String, Optional condition As String = "") As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Select {colsName} From ")
|
||||
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
|
||||
If String.IsNullOrWhiteSpace(condition) = False Then
|
||||
cmdText.Append($" WHERE {condition}")
|
||||
End If
|
||||
|
||||
cmdText.Append($";")
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有指定列的去重数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="ColsName">列名集合,需要返回多列时用','符号分隔列名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDistinctSearch(dbName As String, colsName As String, tableName As String, Optional condition As String = "") As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Select Distinct {colsName} From ")
|
||||
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
|
||||
If String.IsNullOrWhiteSpace(condition) = False Then
|
||||
cmdText.Append($" WHERE {condition}")
|
||||
End If
|
||||
|
||||
cmdText.Append($";")
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="columnName">表名</param>
|
||||
''' <param name="tableName">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearch(dbName As String, columnName As List(Of String), tableName As String, Optional condition As String = "") As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
End If
|
||||
Next
|
||||
|
||||
Return DbSearch(dbName, colNameString.ToString(), tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定表包含的内容行数
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <param name="condition">查询条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchCount(dbName As String, tableName As String, Optional condition As String = "") As String
|
||||
Return DbSearch(dbName, "count(*)", tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有数据
|
||||
'''
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <param name="condition">查询条件(可选)</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchAll(dbName As String, tableName As String, Optional condition As String = "") As String
|
||||
Return DbSearch(dbName, "*", tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchTableInfo(dbName As String, tableName As String) As String
|
||||
Return DbSearch("", "*", "information_schema.tables", "table_schema = '{dbName}' and table_name = '{tableName}'")
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表是否存在的信息,返回查询当前表在指定数据库中存在的数量
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchTableExists(dbName As String, tableName As String) As String
|
||||
Return DbSearch("", "count(*)", "information_schema.tables", "table_schema = '{dbName}' and table_name = '{tableName}'")
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colNames">列名字符串</param>
|
||||
''' <param name="values">列值字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbInsert(dbName As String, tableName As String, colNames As String, values As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Insert into ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" ({colNames}) Values ( {values} );")
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="dicNameValues">列名与列值键值对</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbInsert(dbName As String, tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"'{keyValuePair.Value}'")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",'{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
|
||||
Return DbInsert(dbName, tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表插入一行,通过命令参数方式执行时使用,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName"></param>
|
||||
''' <param name="colNames">需要插入列名的集合</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbInsertParam(dbName As String, tableName As String, colNames As List(Of String)) As String
|
||||
Dim colNameBuilder As New StringBuilder
|
||||
Dim valueBuilder As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If colNameBuilder.Length = 0 Then
|
||||
colNameBuilder.Append($"{FiledSuffix}{colName}{FiledPrefix}")
|
||||
valueBuilder.Append($"@{colName}")
|
||||
Else
|
||||
colNameBuilder.Append($",{FiledSuffix}{colName}{FiledPrefix}")
|
||||
valueBuilder.Append($",@{colName}")
|
||||
End If
|
||||
Next
|
||||
'insert into dbName.tablename (1,2,3) value (@1,@2,@3)
|
||||
Return DbInsert(dbName, tableName, colNameBuilder.ToString(), valueBuilder.ToString())
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="destStr">更新字符串</param>
|
||||
''' <param name="condition"></param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbUpdate(dbName As String, tableName As String, destStr As String, condition As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
Dim tmpStrCmdText As String = ""
|
||||
|
||||
cmdText.Append($"Update ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Set {destStr} Where {condition};")
|
||||
tmpStrCmdText = cmdText.ToString()
|
||||
Console.WriteLine("SQL_CMD = " & tmpStrCmdText)
|
||||
|
||||
Return tmpStrCmdText
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="colNames">更新列名的集合</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbUpdateParam(dbName As String, tableName As String, colNames As List(Of String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
End If
|
||||
Next
|
||||
Return DbUpdate(dbName, tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="filedDictionary">更新列名与列值键值对</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbUpdate(dbName As String, tableName As String, filedDictionary As Dictionary(Of String, String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each filed As KeyValuePair(Of String, String) In filedDictionary
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{filed.Key}{FiledPrefix} = '{filed.Value}'")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{filed.Key}{FiledPrefix} = '{filed.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return DbUpdate(dbName, tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中指定数据表增加一列数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">列名</param>
|
||||
''' <param name="colType">列类型</param>
|
||||
''' <param name="isNull">是否允许为空</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbAddCol(dbName As String, tableName As String, colName As String, colType As String, Optional isNull As Boolean = True) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Alter Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Add {FiledSuffix}{colName}{FiledPrefix} {colType} {IIf(isNull, "Default Null", "Not Null")};")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表删除一列数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">需要删除的列名,仅一列</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDropCol(dbName As String, tableName As String, colName As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Alter Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Drop Column {FiledSuffix}{colName}{FiledPrefix};")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中指定表删除多行数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDeleteRows(dbName As String, tableName As String, condition As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Delete From ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Where {condition};")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中创建数据表
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbCreateTable(dbName As String, tableName As String, createStr As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Create Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" ( {createStr} );")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中创建数据表,如果存在则不创建
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbCreateTableWhenNotExists(dbName As String, tableName As String, createStr As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Create Table if not exists ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" ( {createStr} );")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空指定数据库中数据表,表依旧存在
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDeleteTable(dbName As String, tableName As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Delete From ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($";")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 删除指定数据库中数据表
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDropTable(dbName As String, tableName As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Drop Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($";")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
#End Region
|
||||
End Class
|
||||
377
Database/DbExecutor.vb
Normal file
377
Database/DbExecutor.vb
Normal file
@@ -0,0 +1,377 @@
|
||||
Imports System.Data.Common
|
||||
Imports System.Data.SqlClient
|
||||
Imports System.Data.SQLite
|
||||
Imports MySql.Data.MySqlClient
|
||||
|
||||
''' <summary>
|
||||
''' 数据库通用命令执行器
|
||||
''' 时间:2020-12-21
|
||||
''' 作者:ML
|
||||
''' 版本:1.0
|
||||
''' </summary>
|
||||
Public Class DbExecutor
|
||||
Implements IDisposable
|
||||
''' <summary>
|
||||
''' 数据库类型,目前支持Mysql与Sqlite
|
||||
''' </summary>
|
||||
Enum DbTypeEnum
|
||||
Mysql
|
||||
Sqlite
|
||||
Mssql
|
||||
End Enum
|
||||
|
||||
|
||||
Private ReadOnly _connectionString As String '数据库连接语句
|
||||
|
||||
Private ReadOnly _dbType As DbTypeEnum '数据库类型
|
||||
|
||||
Private _connection As DbConnection '数据库连接句柄
|
||||
|
||||
Private _command As DbCommand '数据库命令句柄
|
||||
|
||||
Private _dataAdapter As DbDataAdapter '数据库查询填充器句柄
|
||||
|
||||
Private _transaction As DbTransaction '数据库事务句柄
|
||||
|
||||
Private _commandHelper As DbCmdHelper '数据库语句填充助手
|
||||
|
||||
Sub New(type As DbTypeEnum, connectionString As String)
|
||||
_dbType = type
|
||||
_connectionString = connectionString
|
||||
InitByDbType(_dbType)
|
||||
End Sub
|
||||
|
||||
Private Sub InitByDbType(type As DbTypeEnum)
|
||||
Select Case type
|
||||
Case DbTypeEnum.Mysql
|
||||
_connection = New MySqlConnection()
|
||||
_command = _connection.CreateCommand()
|
||||
_dataAdapter = New MySqlDataAdapter() With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
|
||||
|
||||
_commandHelper = New MysqlCmdHelper()
|
||||
Case DbTypeEnum.Sqlite
|
||||
_connection = New SQLiteConnection()
|
||||
_command = _connection.CreateCommand()
|
||||
_dataAdapter = New SQLiteDataAdapter() With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
|
||||
|
||||
_commandHelper = New SqliteCmdHelper()
|
||||
Case DbTypeEnum.Mssql
|
||||
_connection = New SqlConnection()
|
||||
_command = _connection.CreateCommand()
|
||||
_dataAdapter = New SqlDataAdapter() With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
|
||||
|
||||
_commandHelper = New MssqlCmdHelper()
|
||||
End Select
|
||||
End Sub
|
||||
|
||||
|
||||
Public ReadOnly Property DatabaseType() As DbTypeEnum
|
||||
Get
|
||||
Return _dbType
|
||||
End Get
|
||||
'Set(value As DbTypeEnum)
|
||||
' _dbType = value
|
||||
' '执行上一个数据库的关闭操作
|
||||
' InitByDbType(_dbType)
|
||||
'End Set
|
||||
End Property
|
||||
|
||||
Public ReadOnly Property Connection() As DbConnection
|
||||
Get
|
||||
Return _connection
|
||||
End Get
|
||||
End Property
|
||||
|
||||
|
||||
Public ReadOnly Property Command() As DbCommand
|
||||
Get
|
||||
Return _command
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public ReadOnly Property CmdHelper() As DbCmdHelper
|
||||
Get
|
||||
Return _commandHelper
|
||||
End Get
|
||||
End Property
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 打开数据库连接
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Function Open() As Boolean
|
||||
If _connection Is Nothing Then Return False
|
||||
If String.IsNullOrWhiteSpace(_connectionString) Then Return False
|
||||
Try
|
||||
_connection.ConnectionString = _connectionString
|
||||
_connection.Open()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return True
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 关闭数据库连接
|
||||
''' </summary>
|
||||
Public Sub Close()
|
||||
If _connection Is Nothing Then Return
|
||||
If _connection.State = ConnectionState.Closed Then Return
|
||||
_connection.Close()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 创建当前连接的命令执行句柄
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Function CreateCommand() As DbCommand
|
||||
Return _connection.CreateCommand()
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 运行非查询语句,返回执行该语句受到影响的行数
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteNonQuery(commandText As String) As Integer
|
||||
Dim result As Integer
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
result = _command.ExecuteNonQuery()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 使用命令参数模式执行非查询语句,返回执行该语句受到影响的行数
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteNonQuery(commandText As String, commandParams As DbParameterCollection) As Integer
|
||||
Dim result As Integer
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
result = _command.ExecuteNonQuery()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回数据库读取流的句柄
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteReader(commandText As String) As DbDataReader
|
||||
Dim result As DbDataReader
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
result = _command.ExecuteReader()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 使用命令参数模式执行数据库语句,返回数据库读取流的句柄
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteReader(commandText As String, commandParams As DbParameterCollection) As DbDataReader
|
||||
Dim result As DbDataReader
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
result = _command.ExecuteReader()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回查询结果的第一行第一列的内容
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteScalar(commandText As String) As Object
|
||||
Dim result As Object
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
result = _command.ExecuteScalar()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 使用命令参数模式执行数据库语句,返回查询结果的第一行第一列的内容
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteScalar(commandText As String, commandParams As DbParameterCollection) As Object
|
||||
Dim result As Object
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
result = _command.ExecuteScalar()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回执行结果返回的数据表,常用于查询命令
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteDataTable(commandText As String, Optional withKey As Boolean = True) As DataTable
|
||||
Dim dataTable As New DataTable
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
If withKey Then
|
||||
_dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
|
||||
Else
|
||||
_dataAdapter.MissingSchemaAction = MissingSchemaAction.Add
|
||||
End If
|
||||
_dataAdapter.SelectCommand = _command
|
||||
_dataAdapter.Fill(dataTable)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
Return dataTable
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回执行结果返回的数据表,常用于查询命令
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteDataTable(commandText As String, commandParams As DbParameterCollection) As DataTable
|
||||
Dim dataTable As New DataTable
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
_dataAdapter.SelectCommand = _command
|
||||
_dataAdapter.Fill(dataTable)
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return dataTable
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 开启事务
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Function BeginTransaction() As DbTransaction
|
||||
_transaction = _connection.BeginTransaction()
|
||||
Return _transaction
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 提交事务
|
||||
''' </summary>
|
||||
Public Sub CommitTransaction()
|
||||
Try
|
||||
_transaction.Commit()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 回滚事务
|
||||
''' </summary>
|
||||
Public Sub RollbackTransaction()
|
||||
Try
|
||||
_transaction.Rollback()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 创建数据参数
|
||||
''' </summary>
|
||||
''' <param name="type">参数数据类型</param>
|
||||
''' <param name="ParameterName">参数名称</param>
|
||||
''' <param name="value">参数值</param>
|
||||
''' <returns></returns>
|
||||
Public Function CreateDbParameter(type As DbType, parameterName As String, value As Object) As DbParameter
|
||||
Dim dbParam As DbParameter = _command.CreateParameter()
|
||||
dbParam.DbType = type
|
||||
dbParam.ParameterName = parameterName
|
||||
dbParam.Value = value
|
||||
Return dbParam
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 添加数据参数
|
||||
''' </summary>
|
||||
''' <param name="type"></param>
|
||||
''' <param name="parameterName"></param>
|
||||
''' <param name="value"></param>
|
||||
''' <returns></returns>
|
||||
Public Function AddDbParameter(type As DbType, parameterName As String, value As Object) As DbParameter
|
||||
Dim dbParam As DbParameter = _command.CreateParameter()
|
||||
dbParam.DbType = type
|
||||
dbParam.ParameterName = parameterName
|
||||
dbParam.Value = value
|
||||
_command.Parameters.Add(dbParam)
|
||||
Return dbParam
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空数据
|
||||
''' </summary>
|
||||
Public Sub ClearDbParameter()
|
||||
_command.Parameters.Clear()
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 回收资源
|
||||
''' </summary>
|
||||
Public Sub Dispose() Implements IDisposable.Dispose
|
||||
If _connection IsNot Nothing Then
|
||||
If _connection.State = ConnectionState.Open Then
|
||||
_connection.Close()
|
||||
End If
|
||||
_connection.Dispose()
|
||||
End If
|
||||
|
||||
If _command IsNot Nothing Then _command.Dispose()
|
||||
If _dataAdapter IsNot Nothing Then _dataAdapter.Dispose()
|
||||
|
||||
GC.Collect() '对所有缓存垃圾进行回收
|
||||
End Sub
|
||||
End Class
|
||||
10
Database/Mssql/MssqlCmdHelper.vb
Normal file
10
Database/Mssql/MssqlCmdHelper.vb
Normal file
@@ -0,0 +1,10 @@
|
||||
Namespace Database.Mssql
|
||||
Public Class MssqlCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "["c
|
||||
FiledPrefix = "]"c
|
||||
End Sub
|
||||
End Class
|
||||
End Namespace
|
||||
8
Database/MssqlCmdHelper.vb
Normal file
8
Database/MssqlCmdHelper.vb
Normal file
@@ -0,0 +1,8 @@
|
||||
Public Class MssqlCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "["c
|
||||
FiledPrefix = "]"c
|
||||
End Sub
|
||||
End Class
|
||||
222
Database/Mysql/DataParam.vb
Normal file
222
Database/Mysql/DataParam.vb
Normal file
@@ -0,0 +1,222 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Mysql
|
||||
Public Class DataParam
|
||||
Enum DataTypeEnum
|
||||
'###############################数值类型#############################
|
||||
''' <summary>
|
||||
''' 1 byte,小整数值
|
||||
''' </summary>
|
||||
Tinyint
|
||||
|
||||
''' <summary>
|
||||
''' 2 bytes,大整数值
|
||||
''' </summary>
|
||||
Smallint
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,大整数值
|
||||
''' </summary>
|
||||
Mediumint
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,大整数值
|
||||
''' </summary>
|
||||
Int
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,大整数值
|
||||
''' </summary>
|
||||
[Integer]
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,极大整数值
|
||||
''' </summary>
|
||||
Bigint
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,单精度浮点数值
|
||||
''' </summary>
|
||||
Float
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,双精度浮点数值
|
||||
''' </summary>
|
||||
[Double]
|
||||
|
||||
|
||||
|
||||
'####################日期类型###############################
|
||||
|
||||
''' <summary>
|
||||
''' 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2.小数值
|
||||
''' </summary>
|
||||
[Decimal]
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,日期值,YYYY-MM-DD
|
||||
''' </summary>
|
||||
[Date]
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,时间值或持续时间,HH:MM:SS
|
||||
''' </summary>
|
||||
Time
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 1 bytes,年份值,YYYY
|
||||
''' </summary>
|
||||
Year
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,混合日期和时间值,YYYY-MM-DD HH:MM:SS
|
||||
''' </summary>
|
||||
Datetime
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,混合日期和时间值,时间戳,YYYYMMDD HHMMSS
|
||||
''' </summary>
|
||||
Timestamp
|
||||
|
||||
|
||||
'####################字符类型###############################
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,定长字符串
|
||||
''' </summary>
|
||||
[Char]
|
||||
|
||||
''' <summary>
|
||||
''' 0-65535 bytes,变长字符串
|
||||
''' </summary>
|
||||
Varchar
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,不超过 255 个字符的二进制字符串
|
||||
''' </summary>
|
||||
Tinyblob
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,短文本字符串
|
||||
''' </summary>
|
||||
Tinytext
|
||||
|
||||
''' <summary>
|
||||
''' 0-65 535 bytes,二进制形式的长文本数据
|
||||
''' </summary>
|
||||
Blob
|
||||
|
||||
''' <summary>
|
||||
''' 0-65 535 bytes,长文本数据
|
||||
''' </summary>
|
||||
Text
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 0-16 777 215 bytes,二进制形式的中等长度文本数据
|
||||
''' </summary>
|
||||
Mediumblob
|
||||
|
||||
''' <summary>
|
||||
''' 0-16 777 215 bytes,中等长度文本数据
|
||||
''' </summary>
|
||||
Mediumtext
|
||||
|
||||
''' <summary>
|
||||
''' 0-4 294 967 295 bytes,二进制形式的极大文本数据
|
||||
''' </summary>
|
||||
Longblob
|
||||
|
||||
''' <summary>
|
||||
''' 0-4 294 967 295 bytes,极大文本数据
|
||||
''' </summary>
|
||||
Longtext
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As DataTypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型是否带符号
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsUnsigned() As Boolean
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case DataTypeEnum.Varchar,DataTypeEnum.[Char]
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case DataTypeEnum.Int
|
||||
sb.Append($" {DataType}")
|
||||
if IsUnsigned Then sb.Append($" Unsigned")
|
||||
If IsAutoIncrement Then sb.Append($" AUTO_INCREMENT")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
13
Database/Mysql/MysqlCmdHelper.vb
Normal file
13
Database/Mysql/MysqlCmdHelper.vb
Normal file
@@ -0,0 +1,13 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Mysql
|
||||
Public Class MysqlCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New
|
||||
FiledSuffix = "`"c
|
||||
FiledPrefix = "`"c
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
9
Database/MysqlCmdHelper.vb
Normal file
9
Database/MysqlCmdHelper.vb
Normal file
@@ -0,0 +1,9 @@
|
||||
Public Class MysqlCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "`"c
|
||||
FiledPrefix = "`"c
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
220
Database/MysqlDataParam.vb
Normal file
220
Database/MysqlDataParam.vb
Normal file
@@ -0,0 +1,220 @@
|
||||
Imports System.Text
|
||||
|
||||
Public Class MysqlDataParam
|
||||
Enum DataTypeEnum
|
||||
'###############################数值类型#############################
|
||||
''' <summary>
|
||||
''' 1 byte,小整数值
|
||||
''' </summary>
|
||||
Tinyint
|
||||
|
||||
''' <summary>
|
||||
''' 2 bytes,大整数值
|
||||
''' </summary>
|
||||
Smallint
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,大整数值
|
||||
''' </summary>
|
||||
Mediumint
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,大整数值
|
||||
''' </summary>
|
||||
Int
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,大整数值
|
||||
''' </summary>
|
||||
[Integer]
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,极大整数值
|
||||
''' </summary>
|
||||
Bigint
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,单精度浮点数值
|
||||
''' </summary>
|
||||
Float
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,双精度浮点数值
|
||||
''' </summary>
|
||||
[Double]
|
||||
|
||||
|
||||
|
||||
'####################日期类型###############################
|
||||
|
||||
''' <summary>
|
||||
''' 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2.小数值
|
||||
''' </summary>
|
||||
[Decimal]
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,日期值,YYYY-MM-DD
|
||||
''' </summary>
|
||||
[Date]
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,时间值或持续时间,HH:MM:SS
|
||||
''' </summary>
|
||||
Time
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 1 bytes,年份值,YYYY
|
||||
''' </summary>
|
||||
Year
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,混合日期和时间值,YYYY-MM-DD HH:MM:SS
|
||||
''' </summary>
|
||||
Datetime
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,混合日期和时间值,时间戳,YYYYMMDD HHMMSS
|
||||
''' </summary>
|
||||
Timestamp
|
||||
|
||||
|
||||
'####################字符类型###############################
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,定长字符串
|
||||
''' </summary>
|
||||
[Char]
|
||||
|
||||
''' <summary>
|
||||
''' 0-65535 bytes,变长字符串
|
||||
''' </summary>
|
||||
Varchar
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,不超过 255 个字符的二进制字符串
|
||||
''' </summary>
|
||||
Tinyblob
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,短文本字符串
|
||||
''' </summary>
|
||||
Tinytext
|
||||
|
||||
''' <summary>
|
||||
''' 0-65 535 bytes,二进制形式的长文本数据
|
||||
''' </summary>
|
||||
Blob
|
||||
|
||||
''' <summary>
|
||||
''' 0-65 535 bytes,长文本数据
|
||||
''' </summary>
|
||||
Text
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 0-16 777 215 bytes,二进制形式的中等长度文本数据
|
||||
''' </summary>
|
||||
Mediumblob
|
||||
|
||||
''' <summary>
|
||||
''' 0-16 777 215 bytes,中等长度文本数据
|
||||
''' </summary>
|
||||
Mediumtext
|
||||
|
||||
''' <summary>
|
||||
''' 0-4 294 967 295 bytes,二进制形式的极大文本数据
|
||||
''' </summary>
|
||||
Longblob
|
||||
|
||||
''' <summary>
|
||||
''' 0-4 294 967 295 bytes,极大文本数据
|
||||
''' </summary>
|
||||
Longtext
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As DataTypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型是否带符号
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsUnsigned() As Boolean
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case DataTypeEnum.Varchar, DataTypeEnum.[Char]
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case DataTypeEnum.Int
|
||||
sb.Append($" {DataType}")
|
||||
If IsUnsigned Then sb.Append($" Unsigned")
|
||||
If IsAutoIncrement Then sb.Append($" AUTO_INCREMENT")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
106
Database/Sqlite/DataParam.vb
Normal file
106
Database/Sqlite/DataParam.vb
Normal file
@@ -0,0 +1,106 @@
|
||||
Imports System.Text
|
||||
|
||||
Namespace Database.Sqlite
|
||||
Public Class DataParam
|
||||
Enum DataTypeEnum
|
||||
Varchar
|
||||
Nchar
|
||||
Blob
|
||||
Bit
|
||||
Datetime
|
||||
[Decimal]
|
||||
Real
|
||||
UniqueIdentifier
|
||||
Int
|
||||
[Integer]
|
||||
TinyInt
|
||||
[Single]
|
||||
Nvarchar
|
||||
SmallInt
|
||||
SmallUint
|
||||
Uint
|
||||
UnsignedInteger
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As DataTypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为唯一值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsUnique() As Boolean
|
||||
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case DataTypeEnum.Varchar, DataTypeEnum.Nchar, DataTypeEnum.Nvarchar
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case DataTypeEnum.Int,DataTypeEnum.Integer
|
||||
sb.Append($" {DataType}")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
If IsAutoIncrement Then sb.Append($" AUTOINCREMENT")
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
20
Database/Sqlite/SqliteCmdHelper.vb
Normal file
20
Database/Sqlite/SqliteCmdHelper.vb
Normal file
@@ -0,0 +1,20 @@
|
||||
Namespace Database.Sqlite
|
||||
Public Class SqliteCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "["c
|
||||
FiledPrefix = "]"c
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="tableName"></param>
|
||||
''' <returns></returns>
|
||||
Public Overrides Function SearchTableInfo(tableName As String) As String
|
||||
Return $"select * from sqlite_master where tbl_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
19
Database/SqliteCmdHelper.vb
Normal file
19
Database/SqliteCmdHelper.vb
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
Public Class SqliteCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "["c
|
||||
FiledPrefix = "]"c
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="tableName"></param>
|
||||
''' <returns></returns>
|
||||
Public Overrides Function SearchTableInfo(tableName As String) As String
|
||||
Return $"select * from sqlite_master where tbl_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
End Class
|
||||
105
Database/SqliteDataParam.vb
Normal file
105
Database/SqliteDataParam.vb
Normal file
@@ -0,0 +1,105 @@
|
||||
Imports System.Text
|
||||
|
||||
|
||||
Public Class SqliteDataParam
|
||||
Enum DataTypeEnum
|
||||
Varchar
|
||||
Nchar
|
||||
Blob
|
||||
Bit
|
||||
Datetime
|
||||
[Decimal]
|
||||
Real
|
||||
UniqueIdentifier
|
||||
Int
|
||||
[Integer]
|
||||
TinyInt
|
||||
[Single]
|
||||
Nvarchar
|
||||
SmallInt
|
||||
SmallUint
|
||||
Uint
|
||||
UnsignedInteger
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As DataTypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为唯一值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsUnique() As Boolean
|
||||
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case DataTypeEnum.Varchar, DataTypeEnum.Nchar, DataTypeEnum.Nvarchar
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case DataTypeEnum.Int, DataTypeEnum.Integer
|
||||
sb.Append($" {DataType}")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
If IsAutoIncrement Then sb.Append($" AUTOINCREMENT")
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user