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