|
1 | | -Imports Databasic.ActiveRecord |
| 1 | +Imports System.Text.RegularExpressions |
| 2 | +Imports Databasic.ActiveRecord |
2 | 3 |
|
3 | 4 | Public Class ProviderResource |
4 | | - Inherits Databasic.ProviderResource |
| 5 | + Inherits Databasic.ProviderResource |
5 | 6 |
|
6 | | - Public Overrides Function GetTableColumns(table As String, connection As Databasic.Connection) As Dictionary(Of String, Boolean) |
7 | | - Dim result As New Dictionary(Of String, Boolean) |
8 | | - Dim createSql As String = Databasic.Statement.Prepare(" |
| 7 | + Public Overrides Function GetTableColumns(table As String, connection As Databasic.Connection) As Dictionary(Of String, Boolean) |
| 8 | + Dim createSql = Me._getCreateTableStatement(table, connection) |
| 9 | + createSql = Me._prepareCreateTableStatement(createSql) |
| 10 | + Return Me._parseColumnsFromCreateTable(createSql) |
| 11 | + End Function |
| 12 | + |
| 13 | + Private Function _getCreateTableStatement(table As String, connection As Databasic.Connection) As String |
| 14 | + Return Databasic.Statement.Prepare(" |
9 | 15 | SELECT sql |
10 | 16 | FROM sqlite_master t |
11 | 17 | WHERE |
12 | 18 | t.type = 'table' AND |
13 | 19 | t.name = @table |
14 | | - ", |
15 | | - connection |
| 20 | + ", connection |
16 | 21 | ).FetchAll(New With { |
17 | 22 | .table = table |
18 | 23 | }).ToInstance(Of String)() |
19 | | - Dim pos = createSql.IndexOf("(") |
20 | | - If pos = -1 Then Return result |
| 24 | + End Function |
| 25 | + |
| 26 | + Private Function _prepareCreateTableStatement(createSql As String) As String |
| 27 | + '' createSql example: |
| 28 | + 'CREATE TABLE persons ( |
| 29 | + ' id_person INT NOT NULL, |
| 30 | + ' id_parent INT NULL, |
| 31 | + ' id_department INT NOT NULL, |
| 32 | + ' name VARCHAR(100) NOT NULL, |
| 33 | + ' surname VARCHAR(100) NULL, |
| 34 | + ' salary DECIMAL(9, 2) NOT NULL DEFAULT 0, |
| 35 | + ' gender CHAR(1) NOT NULL DEFAULT 'O' |
| 36 | + ') |
| 37 | + Dim result As String = "", |
| 38 | + indexPos = 0, |
| 39 | + m As Match, |
| 40 | + pos = createSql.IndexOf("(") |
21 | 41 | createSql = createSql.Substring(pos + 1) |
22 | 42 | pos = createSql.LastIndexOf(")") |
23 | | - If pos = -1 Then Return result |
24 | 43 | createSql = createSql.Substring(0, pos) |
25 | | - Dim columnsSql As String() = createSql.Split(",") |
26 | | - Dim columnSql As String |
27 | | - Dim columnName As String |
28 | | - Dim columnCouldBeNull As Boolean |
| 44 | + ' replace temporarily all comma chars in value type definition places |
| 45 | + ' to be able to split all columns by comma char later |
| 46 | + For Each m In Regex.Matches(createSql, "\(\d+(,)\s*\d+\)") |
| 47 | + result &= createSql.Substring(indexPos, m.Index - indexPos) _ |
| 48 | + & m.Value.Replace(",", "__DATABASIC_COMMA_CHAR__") |
| 49 | + indexPos = m.Index + m.Length |
| 50 | + Next |
| 51 | + result &= createSql.Substring(m.Index + m.Length) |
| 52 | + ' comma replacing end |
| 53 | + result = result.Replace("\t", " ").Replace("\r", " ").Replace("\n", " ") |
| 54 | + Return result |
| 55 | + End Function |
| 56 | + |
| 57 | + Private Function _parseColumnsFromCreateTable(createSql As String) As Object |
| 58 | + Dim result As New Dictionary(Of String, Boolean), |
| 59 | + columnsSql As String() = createSql.Split(","), |
| 60 | + columnSql As String, |
| 61 | + columnName As String, |
| 62 | + columnCouldBeNull As Boolean, |
| 63 | + pos As Int32 |
29 | 64 | For index = 0 To columnsSql.Length - 1 |
30 | 65 | columnSql = columnsSql(index).Trim(" "c, "\t", "\r", "\n") |
31 | 66 | pos = columnSql.IndexOf(" ") |
32 | 67 | If (pos = -1) Then Continue For |
| 68 | + ' columnSql example: |
| 69 | + ' salary DECIMAL(9__DATABASIC_COMMA_CHAR__ 2) NOT NULL DEFAULT 0 |
| 70 | + columnSql = columnSql.Replace("__DATABASIC_COMMA_CHAR__", ",") |
| 71 | + ' columnSql example: |
| 72 | + ' salary DECIMAL(9, 2) NOT NULL DEFAULT 0 |
33 | 73 | columnName = columnSql.Substring(0, pos) |
34 | 74 | columnCouldBeNull = columnSql.ToLower().IndexOf("not null") = -1 |
35 | 75 | result.Add(columnName, columnCouldBeNull) |
36 | 76 | Next |
37 | 77 | Return result |
38 | 78 | End Function |
39 | 79 |
|
40 | | - Public Overrides Function GetLastInsertedId(ByRef transaction As Databasic.Transaction, Optional ByRef classMetaDescription As MetaDescription = Nothing) As Object |
41 | | - Return Databasic.Statement.Prepare("SELECT LAST_INSERT_ID()", transaction).FetchOne().ToInstance(Of Object)() |
| 80 | + Public Overrides Function GetLastInsertedId( |
| 81 | + ByRef transaction As Databasic.Transaction, |
| 82 | + Optional ByRef classMetaDescription As MetaDescription = Nothing |
| 83 | + ) As Object |
| 84 | + Return Databasic.Statement.Prepare( |
| 85 | + "SELECT LAST_INSERT_ROWID()", transaction |
| 86 | + ).FetchOne().ToInstance(Of Object)() |
42 | 87 | End Function |
43 | 88 |
|
44 | 89 | 'Public Overrides Function GetAll( |
|
0 commit comments