Skip to content

Commit 96ffbe5

Browse files
committed
sql errors and exceptions handling
1 parent 0b92376 commit 96ffbe5

9 files changed

+79
-23
lines changed

Connection.vb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ Public Class Connection
2323
AddHandler Me._provider.InfoMessage, AddressOf Connection.errorHandler
2424
End Sub
2525

26-
Public Overrides Function CreateAndBeginTransaction(Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Databasic.Transaction
27-
Return New Transaction() With {
26+
Protected Shared Sub errorHandler(sender As Object, args As MySqlInfoMessageEventArgs)
27+
Dim sqlErrors As Databasic.SqlErrorsCollection = New SqlErrorsCollection()
28+
For index = 0 To args.errors.Length - 1
29+
sqlErrors.Add(New Databasic.MySql.SqlError(args.errors(index)))
30+
Next
31+
Databasic.Events.RaiseError(sqlErrors)
32+
End Sub
33+
34+
Protected Overrides Function createAndBeginTransaction(Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Databasic.Transaction
35+
Me.OpenedTransaction = New Transaction() With {
2836
.ConnectionWrapper = Me,
2937
.Instance = Me._provider.BeginTransaction(isolationLevel)
3038
}
39+
Return Me.OpenedTransaction
3140
End Function
3241

3342
End Class

Databasic.MySql.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<DependentUpon>Settings.settings</DependentUpon>
7979
<DesignTimeSharedInput>True</DesignTimeSharedInput>
8080
</Compile>
81+
<Compile Include="SqlError.vb" />
8182
<Compile Include="ProviderResource.vb" />
8283
<Compile Include="Statement.vb" />
8384
<Compile Include="Transaction.vb" />

ProviderResource.vb

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,51 @@
3131
Return Databasic.Statement.Prepare("SELECT LAST_INSERT_ID()", transaction).FetchOne().ToInstance(Of Object)()
3232
End Function
3333

34-
'Public Overrides Function GetAll(
35-
' connection As Databasic.Connection,
36-
' columns As String,
37-
' table As String,
38-
' Optional offset As Int64? = Nothing,
39-
' Optional limit As Int64? = Nothing,
40-
' Optional orderByStatement As String = ""
41-
' ) As Databasic.Statement
42-
' Dim sql = $"SELECT {columns} FROM {table}"
43-
' offset = If(offset, 0)
44-
' limit = If(limit, 0)
45-
' If limit > 0 Then
46-
' sql += If(orderByStatement.Length > 0, " ORDER BY " + orderByStatement, "") +
47-
' $" LIMIT {If(limit = 0, "18446744073709551615", limit.ToString())} OFFSET {offset}"
48-
' End If
49-
' Return Databasic.Statement.Prepare(sql, connection).FetchAll()
50-
'End Function
34+
'Public Overrides Function GetAll(
35+
' connection As Databasic.Connection,
36+
' columns As String,
37+
' table As String,
38+
' Optional offset As Int64? = Nothing,
39+
' Optional limit As Int64? = Nothing,
40+
' Optional orderByStatement As String = ""
41+
' ) As Databasic.Statement
42+
' Dim sql = $"SELECT {columns} FROM {table}"
43+
' offset = If(offset, 0)
44+
' limit = If(limit, 0)
45+
' If limit > 0 Then
46+
' sql += If(orderByStatement.Length > 0, " ORDER BY " + orderByStatement, "") +
47+
' $" LIMIT {If(limit = 0, "18446744073709551615", limit.ToString())} OFFSET {offset}"
48+
' End If
49+
' Return Databasic.Statement.Prepare(sql, connection).FetchAll()
50+
'End Function
51+
52+
Public Overridable Function GetList(
53+
conditionSqlStatement As String,
54+
conditionParams As Object,
55+
orderBySqlStatement As String,
56+
offset As Int64?,
57+
limit As Int64?,
58+
connectionOrTransaction As Object,
59+
ByRef classMetaDescription As MetaDescription
60+
) As Databasic.Statement
61+
Dim columns As String = String.Join(",", Databasic.ProviderResource.ColumnsArray(classMetaDescription.ClassType, 0))
62+
Dim sql As String = $"SELECT {columns} FROM {ActiveRecord.Resource.Table(classMetaDescription)}"
63+
Dim params As Object = Nothing
64+
If Not String.IsNullOrEmpty(conditionSqlStatement) Then
65+
sql += " WHERE " + conditionSqlStatement
66+
params = conditionParams
67+
End If
68+
If Not String.IsNullOrEmpty(orderBySqlStatement) Then
69+
sql += " ORDER BY " + orderBySqlStatement
70+
End If
71+
If offset.HasValue Then
72+
' offset a number, but could be 0, limit is unknown
73+
sql += $" LIMIT {If(Not limit.HasValue, "18446744073709551615", limit.ToString())} OFFSET {offset}"
74+
ElseIf limit.HasValue And limit > 0 Then
75+
' offset is null, limit is a number, but could be 0
76+
sql += $" LIMIT {limit.ToString()}"
77+
End If
78+
Return Databasic.Statement.Prepare(sql, connectionOrTransaction).FetchAll(params)
79+
End Function
5180

5281
End Class

SqlError.vb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Imports MySql.Data.MySqlClient
2+
3+
Public Class SqlError
4+
Inherits Databasic.SqlError
5+
6+
Public Property Level As String
7+
8+
Public Sub New(mySqlError As Global.MySql.Data.MySqlClient.MySqlError)
9+
Me.Message = mySqlError.Message
10+
Me.Code = mySqlError.Code
11+
12+
Me.Level = mySqlError.Level
13+
End Sub
14+
15+
End Class

Transaction.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Imports MySql.Data.MySqlClient
33

44
Public Class Transaction
55
Inherits Databasic.Transaction
6+
67
Public Overrides Property Instance As DbTransaction
78
Get
89
Return Me._instance
@@ -12,4 +13,5 @@ Public Class Transaction
1213
End Set
1314
End Property
1415
Private _instance As MySqlTransaction
16+
1517
End Class

content/App.config.install.xdt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<clear xdt:Transform="InsertIfMissing" />
55
<add
66
name="Databasic.Example.MySql"
7-
connectionString="server=127.0.0.1;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
7+
connectionString="server=localhost;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
88
providerName="MySql.Data.MySqlClient"
99
xdt:Transform="InsertIfMissing"
1010
xdt:Locator="Match(name)" />

content/App.config.uninstall.xdt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<connectionStrings>
44
<add
55
name="Databasic.Example.MySql"
6-
connectionString="server=127.0.0.1;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
6+
connectionString="server=localhost;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
77
providerName="MySql.Data.MySqlClient"
88
xdt:Transform="Remove"
99
xdt:Locator="Match(name)" />

content/Web.config.install.xdt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<clear xdt:Transform="InsertIfMissing" />
55
<add
66
name="Databasic.Example.MySql"
7-
connectionString="server=127.0.0.1;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
7+
connectionString="server=localhost;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
88
providerName="MySql.Data.MySqlClient"
99
xdt:Transform="InsertIfMissing"
1010
xdt:Locator="Match(name)" />

content/Web.config.uninstall.xdt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<connectionStrings>
44
<add
55
name="Databasic.Example.MySql"
6-
connectionString="server=127.0.0.1;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
6+
connectionString="server=localhost;port=3306;uid=USER_NAME;pwd=PASSWORD;database=DATABASE;"
77
providerName="MySql.Data.MySqlClient"
88
xdt:Transform="Remove"
99
xdt:Locator="Match(name)" />

0 commit comments

Comments
 (0)