Consolidated ASP Classic MVC framework from best components
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

344 строки
12KB

  1. '==============================================================
  2. ' GenerateRepo.vbs (AxonASP CLI + G3DB edition)
  3. '
  4. ' Usage (runs under the AxonASP CLI in the container):
  5. ' scripts\gen.cmd repo /table:Products /pk:Id
  6. '
  7. ' Reads the connection string from public\web.config (<appSettings> ConnectionString = "<driver>:<dsn>"),
  8. ' discovers the table schema, and generates into app/models/:
  9. ' POBO_<Table>.asp (class with typed Property Let/Get)
  10. ' <Table>Repository.asp (parameterized CRUD + filter + paging + search, via DAL())
  11. '
  12. ' Schema discovery currently supports SQLite (PRAGMA table_info). Templates live in scripts/templates/.
  13. '==============================================================
  14. Option Explicit
  15. ' AxonASP CLI: provide the WScript object (see _cli.vbs)
  16. Dim shimFso
  17. Set shimFso = CreateObject("Scripting.FileSystemObject")
  18. ExecuteGlobal shimFso.OpenTextFile(Server.MapPath("/scripts/_cli.vbs"), 1).ReadAll
  19. '---------------- Args & paths ----------------
  20. Dim fso, args, i, arg, parts
  21. Dim tableName, primaryKey, csKey, configPath, outDir
  22. Set fso = CreateObject("Scripting.FileSystemObject")
  23. Set args = WScript.Arguments
  24. csKey = "ConnectionString"
  25. configPath = Server.MapPath("/public/web.config")
  26. outDir = Server.MapPath("/app/models")
  27. For i = 0 To args.Count - 1
  28. arg = args(i)
  29. If InStr(arg, ":") > 0 Then
  30. parts = Split(arg, ":", 2)
  31. Select Case LCase(Replace(parts(0), "/", ""))
  32. Case "table": tableName = parts(1)
  33. Case "pk": primaryKey = parts(1)
  34. Case "cs": csKey = parts(1)
  35. Case "config": configPath = parts(1)
  36. End Select
  37. End If
  38. Next
  39. If Len(tableName) = 0 Or Len(primaryKey) = 0 Then
  40. WScript.Echo "Usage: scripts\gen.cmd repo /table:<TableName> /pk:<PrimaryKey>"
  41. WScript.Quit 1
  42. End If
  43. If Not IsValidIdentifier(tableName) Then
  44. WScript.Echo "Error: Invalid table name format. Only letters, numbers, and underscores are allowed."
  45. WScript.Quit 1
  46. End If
  47. If Not IsValidIdentifier(primaryKey) Then
  48. WScript.Echo "Error: Invalid primary key format. Only letters, numbers, and underscores are allowed."
  49. WScript.Quit 1
  50. End If
  51. '---------------- Config & connection ----------------
  52. Dim connStr, p, driver, dsn
  53. connStr = GetAppSetting(csKey, configPath)
  54. If connStr = "nothing" Then
  55. WScript.Echo "Error: Key '" & csKey & "' not found in " & configPath
  56. WScript.Quit 1
  57. End If
  58. p = InStr(connStr, ":")
  59. If p < 2 Then
  60. WScript.Echo "Error: ConnectionString must look like '<driver>:<dsn>'. Got: " & connStr
  61. WScript.Quit 1
  62. End If
  63. driver = LCase(Left(connStr, p - 1))
  64. dsn = Mid(connStr, p + 1)
  65. If driver <> "sqlite" And driver <> "sqlite3" Then
  66. WScript.Echo "Error: schema discovery is implemented for SQLite only (driver is '" & driver & "')."
  67. WScript.Quit 1
  68. End If
  69. Dim db
  70. Set db = Server.CreateObject("G3DB")
  71. If Not db.Open("sqlite", dsn) Then
  72. WScript.Echo "DB connection failed: " & db.LastError
  73. WScript.Quit 1
  74. End If
  75. '---------------- Discover schema ----------------
  76. Dim cols(), kinds(), idx, rs
  77. idx = -1
  78. Set rs = db.Query("PRAGMA table_info(" & tableName & ")")
  79. If IsEmpty(rs) Then
  80. WScript.Echo "Error reading table [" & tableName & "]: " & db.LastError
  81. WScript.Quit 1
  82. End If
  83. Do Until rs.EOF
  84. idx = idx + 1
  85. ReDim Preserve cols(idx), kinds(idx)
  86. cols(idx) = CStr(rs(1)) ' name
  87. kinds(idx) = TypeKind(rs(2)) ' declared type -> string/number/bool/date
  88. rs.MoveNext
  89. Loop
  90. rs.Close
  91. db.Close
  92. If idx < 0 Then
  93. WScript.Echo "No columns found for table [" & tableName & "]. Run the migrations first."
  94. WScript.Quit 1
  95. End If
  96. If Not InArrayInsensitive(cols, primaryKey) Then
  97. WScript.Echo "Primary key '" & primaryKey & "' not found in table [" & tableName & "]."
  98. WScript.Quit 1
  99. End If
  100. '---------------- Generate files from templates ----------------
  101. If Not fso.FolderExists(outDir) Then fso.CreateFolder outDir
  102. Dim poboPath, repoPath
  103. poboPath = outDir & "/POBO_" & tableName & ".asp"
  104. repoPath = outDir & "/" & tableName & "Repository.asp"
  105. WriteFile poboPath, BuildPOBO(cols, kinds, tableName, primaryKey)
  106. WScript.Echo "POBO class written to: " & poboPath
  107. WriteFile repoPath, BuildRepository(cols, tableName, primaryKey)
  108. WScript.Echo "Repository written to: " & repoPath
  109. WScript.Echo ""
  110. WScript.Echo "Next steps:"
  111. WScript.Echo "1. Include both files from app/models/autoload_models.asp:"
  112. WScript.Echo " <!--#include file=""POBO_" & tableName & ".asp""-->"
  113. WScript.Echo " <!--#include file=""" & tableName & "Repository.asp""-->"
  114. Set fso = Nothing
  115. '==============================================================
  116. ' Generators
  117. '==============================================================
  118. Function ReadTemplate(name)
  119. Dim ts
  120. Set ts = fso.OpenTextFile(Server.MapPath("/scripts/templates/" & name), 1)
  121. ReadTemplate = ts.ReadAll
  122. ts.Close
  123. End Function
  124. Sub WriteFile(path, text)
  125. Dim tf
  126. Set tf = fso.CreateTextFile(path, True)
  127. tf.Write text
  128. tf.Close
  129. End Sub
  130. Function BuildPOBO(colsArr, kindsArr, tName, pk)
  131. Dim fields, defaults, propList, props, k, name, kind
  132. fields = "" : defaults = "" : propList = "" : props = ""
  133. For k = 0 To UBound(colsArr)
  134. name = colsArr(k)
  135. kind = kindsArr(k)
  136. fields = fields & " Private p" & name & vbCrLf
  137. Select Case kind
  138. Case "string": defaults = defaults & " p" & name & " = """"" & vbCrLf
  139. Case "number": defaults = defaults & " p" & name & " = 0" & vbCrLf
  140. Case "bool": defaults = defaults & " p" & name & " = False" & vbCrLf
  141. Case Else: defaults = defaults & " p" & name & " = Null" & vbCrLf
  142. End Select
  143. If k > 0 Then propList = propList & ","
  144. propList = propList & """" & name & """"
  145. props = props & " Public Property Get " & name & "()" & vbCrLf
  146. props = props & " " & name & " = p" & name & vbCrLf
  147. props = props & " End Property" & vbCrLf & vbCrLf
  148. props = props & " Public Property Let " & name & "(val)" & vbCrLf
  149. props = props & " On Error Resume Next" & vbCrLf
  150. Select Case kind
  151. Case "string"
  152. props = props & " p" & name & " = CStr(val)" & vbCrLf
  153. Case "bool"
  154. props = props & " p" & name & " = CBool(val)" & vbCrLf
  155. Case "date"
  156. props = props & " p" & name & " = CDate(val)" & vbCrLf
  157. Case Else
  158. props = props & " If IsNumeric(val) Then" & vbCrLf
  159. props = props & " p" & name & " = CDbl(val)" & vbCrLf
  160. props = props & " Else" & vbCrLf
  161. props = props & " p" & name & " = val" & vbCrLf
  162. props = props & " End If" & vbCrLf
  163. End Select
  164. props = props & " If Err.Number <> 0 Then" & vbCrLf
  165. props = props & " Err.Raise Err.Number, ""POBO_" & tName & "." & name & """, ""Invalid value for " & name & ": "" & Err.Description" & vbCrLf
  166. props = props & " End If" & vbCrLf
  167. props = props & " On Error GoTo 0" & vbCrLf
  168. props = props & " End Property" & vbCrLf & vbCrLf
  169. Next
  170. Dim t
  171. t = ReadTemplate("POBO.tpl.asp")
  172. t = Replace(t, "{{TABLE}}", tName)
  173. t = Replace(t, "{{PK}}", pk)
  174. t = Replace(t, "{{DATE}}", CStr(Now()))
  175. t = Replace(t, "{{FIELDS}}", fields)
  176. t = Replace(t, "{{DEFAULTS}}", defaults)
  177. t = Replace(t, "{{PROPLIST}}", propList)
  178. t = Replace(t, "{{PROPERTIES}}", props)
  179. BuildPOBO = t
  180. End Function
  181. Function BuildRepository(colsArr, tName, pk)
  182. Dim selectList, colList, mapLines, insCols, insPh, insParams, updSet, updParams, k, c
  183. selectList = "" : colList = "" : mapLines = "" : insCols = "" : insPh = "" : insParams = "" : updSet = "" : updParams = ""
  184. For k = 0 To UBound(colsArr)
  185. c = colsArr(k)
  186. If k > 0 Then
  187. selectList = selectList & ", "
  188. colList = colList & "|"
  189. End If
  190. selectList = selectList & QI(c)
  191. colList = colList & c
  192. mapLines = mapLines & " If Not IsNull(rs(" & k & ")) Then m." & c & " = rs(" & k & ")" & vbCrLf
  193. If Not StrEqualCI(c, pk) Then
  194. If Len(insCols) > 0 Then
  195. insCols = insCols & ", "
  196. insPh = insPh & ", "
  197. insParams = insParams & ", "
  198. updSet = updSet & ", "
  199. updParams = updParams & ", "
  200. End If
  201. insCols = insCols & QI(c)
  202. insPh = insPh & "?"
  203. insParams = insParams & "model." & c
  204. updSet = updSet & QI(c) & " = ?"
  205. updParams = updParams & "model." & c
  206. End If
  207. Next
  208. Dim t
  209. t = ReadTemplate("Repository.tpl.asp")
  210. t = Replace(t, "{{TABLE}}", tName)
  211. t = Replace(t, "{{PK}}", pk)
  212. t = Replace(t, "{{QTABLE}}", QI(tName))
  213. t = Replace(t, "{{QPK}}", QI(pk))
  214. t = Replace(t, "{{DATE}}", CStr(Now()))
  215. t = Replace(t, "{{COLUMN_LIST}}", colList)
  216. t = Replace(t, "{{SELECT_LIST}}", selectList)
  217. t = Replace(t, "{{MAP_LINES}}", mapLines)
  218. t = Replace(t, "{{INSERT_COLS}}", insCols)
  219. t = Replace(t, "{{INSERT_PLACEHOLDERS}}", insPh)
  220. t = Replace(t, "{{INSERT_PARAMS}}", "Array(" & insParams & ")")
  221. t = Replace(t, "{{UPDATE_SET}}", updSet)
  222. t = Replace(t, "{{UPDATE_PARAMS}}", "Array(" & updParams & ", model." & pk & ")")
  223. BuildRepository = t
  224. End Function
  225. '==============================================================
  226. ' Helpers (generator side)
  227. '==============================================================
  228. ' Quoted identifier for use inside the generated VBScript string literals: "Col" -> ""Col""
  229. Function QI(name)
  230. QI = """""" & Replace(CStr(name), """", """""""""") & """"""
  231. End Function
  232. ' SQLite declared type -> simple kind (follows SQLite's affinity rules, roughly)
  233. Function TypeKind(declType)
  234. Dim t
  235. t = UCase("" & declType)
  236. If InStr(t, "BOOL") > 0 Then
  237. TypeKind = "bool"
  238. ElseIf InStr(t, "DATE") > 0 Or InStr(t, "TIME") > 0 Then
  239. TypeKind = "date"
  240. ElseIf InStr(t, "CHAR") > 0 Or InStr(t, "CLOB") > 0 Or InStr(t, "TEXT") > 0 Then
  241. TypeKind = "string"
  242. ElseIf InStr(t, "INT") > 0 Or InStr(t, "REAL") > 0 Or InStr(t, "FLOA") > 0 Or InStr(t, "DOUB") > 0 Or InStr(t, "NUM") > 0 Or InStr(t, "DEC") > 0 Then
  243. TypeKind = "number"
  244. ElseIf Len(t) = 0 Then
  245. TypeKind = "string"
  246. Else
  247. TypeKind = "string"
  248. End If
  249. End Function
  250. Function GetAppSetting(key, configFilePath)
  251. Dim xml, nodes, node, j
  252. If Not fso.FileExists(configFilePath) Then
  253. GetAppSetting = "nothing"
  254. Exit Function
  255. End If
  256. Set xml = CreateObject("Microsoft.XMLDOM")
  257. xml.Async = False
  258. xml.Load(configFilePath)
  259. Set nodes = xml.selectNodes("//appSettings/add")
  260. For j = 0 To nodes.Length - 1
  261. Set node = nodes.Item(j)
  262. If node.getAttribute("key") = key Then
  263. GetAppSetting = node.getAttribute("value")
  264. Exit Function
  265. End If
  266. Next
  267. GetAppSetting = "nothing"
  268. End Function
  269. Function InArrayInsensitive(a, val)
  270. Dim z
  271. InArrayInsensitive = False
  272. For z = 0 To UBound(a)
  273. If StrEqualCI(a(z), val) Then
  274. InArrayInsensitive = True
  275. Exit Function
  276. End If
  277. Next
  278. End Function
  279. Function StrEqualCI(a, b)
  280. StrEqualCI = (LCase(CStr(a)) = LCase(CStr(b)))
  281. End Function
  282. ' Validate identifier (table name, column name, etc.)
  283. Function IsValidIdentifier(name)
  284. Dim n, ch
  285. If IsEmpty(name) Then
  286. IsValidIdentifier = False
  287. Exit Function
  288. End If
  289. If Len(name) = 0 Then
  290. IsValidIdentifier = False
  291. Exit Function
  292. End If
  293. For n = 1 To Len(name)
  294. ch = Mid(name, n, 1)
  295. If Not ((ch >= "a" And ch <= "z") Or (ch >= "A" And ch <= "Z") Or (ch >= "0" And ch <= "9") Or ch = "_") Then
  296. IsValidIdentifier = False
  297. Exit Function
  298. End If
  299. Next
  300. IsValidIdentifier = True
  301. End Function

Powered by TurnKey Linux.