初始化
This commit is contained in:
15
Base/ColumnSchema.vb
Normal file
15
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
Base/CommandHelpers.vb
Normal file
297
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
Base/DatabaseData.vb
Normal file
94
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
Base/DatabaseSchema.vb
Normal file
12
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
Base/ForeignKeySchema.vb
Normal file
11
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
Base/IndexSchema.vb
Normal file
16
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
Base/InsertParams.vb
Normal file
7
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
Base/SearchCondition.vb
Normal file
71
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
Base/SearchParams.vb
Normal file
46
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
Base/TableSchema.vb
Normal file
14
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
Base/TriggerBuilder.vb
Normal file
71
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
Base/TriggerSchema.vb
Normal file
20
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
Base/ViewSchema.vb
Normal file
17
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
|
||||
Reference in New Issue
Block a user