-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrust_gen_models.js
351 lines (308 loc) · 8.93 KB
/
rust_gen_models.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env node
const Database = require("better-sqlite3")
const fs = require("fs")
// Rust reserved keywords that need to be escaped
const RUST_KEYWORDS = new Set([
"as",
"break",
"const",
"continue",
"crate",
"else",
"enum",
"extern",
"false",
"fn",
"for",
"if",
"impl",
"in",
"let",
"loop",
"match",
"mod",
"move",
"mut",
"pub",
"ref",
"return",
"self",
"Self",
"static",
"struct",
"super",
"trait",
"true",
"type",
"unsafe",
"use",
"where",
"while",
"abstract",
"async",
"become",
"box",
"do",
"final",
"macro",
"override",
"priv",
"try",
"typeof",
"unsized",
"virtual",
"yield"
])
function escapeRustKeyword(name) {
if (RUST_KEYWORDS.has(name)) {
return `_${name}`
}
return name
}
function sqliteTypeToRust(sqliteType, notNull) {
const type = sqliteType.toUpperCase()
let rustType
if (type.includes("INTEGER")) {
rustType = type.includes("UNSIGNED") ? "u64" : "i64"
} else if (type.includes("TEXT")) {
rustType = "String"
} else if (type.includes("BLOB")) {
rustType = "Vec<u8>"
} else if (type.includes("REAL") || type.includes("FLOAT")) {
rustType = "f64"
} else if (type.includes("BOOLEAN")) {
rustType = "bool"
} else {
rustType = "String" // Default to String for unknown types
}
return notNull ? rustType : `Option<${rustType}>`
}
function snakeToPascalCase(str) {
return str
.split("_")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join("")
}
function camelToSnakeCase(str) {
return str
.replace(/([A-Z])/g, "_$1")
.toLowerCase()
.replace(/^_/, "")
}
function getForeignKeys(db, tableName) {
return db.prepare(`PRAGMA foreign_key_list(${tableName})`).all()
}
function getIndices(db, tableName) {
return db.prepare(`PRAGMA index_list(${tableName})`).all()
}
function getTableInfo(db, tableName) {
const columns = db.prepare(`PRAGMA table_info(${tableName})`).all()
const foreignKeys = getForeignKeys(db, tableName)
const indices = getIndices(db, tableName)
return columns.map((col) => {
const fk = foreignKeys.find((fk) => fk.from === col.name)
const index = indices.find((idx) => {
const indexInfo = db.prepare(`PRAGMA index_info(${idx.name})`).all()
return indexInfo.some((ii) => ii.name === col.name)
})
return {
name: col.name,
type: col.type,
notNull: col.notnull === 1,
primaryKey: col.pk === 1,
hasIndex: !!index,
foreignKey: fk
? {
table: fk.table,
column: fk.to
}
: null
}
})
}
function generateRustStruct(tableName, columns) {
const structName = snakeToPascalCase(tableName)
const lines = ["#[derive(Debug, Clone)]", `pub struct ${structName} {`]
// Regular fields
columns.forEach(
({ name, type, notNull, primaryKey, foreignKey, hasIndex }) => {
let fieldName = camelToSnakeCase(name)
fieldName = escapeRustKeyword(fieldName)
let rustType = sqliteTypeToRust(type, notNull)
// Add field documentation
const docs = []
if (primaryKey) docs.push("Primary key")
if (foreignKey)
docs.push(`Foreign key to ${foreignKey.table}.${foreignKey.column}`)
if (hasIndex) docs.push("Indexed")
if (docs.length > 0) {
lines.push(` /// ${docs.join(". ")}`)
}
lines.push(` pub ${fieldName}: ${rustType},`)
}
)
lines.push("}")
// Add column names as constants
lines.push("")
lines.push(`impl ${structName} {`)
// Add table name constant
lines.push(` pub const TABLE: &'static str = "${tableName}";`)
// Add column constants
columns.forEach(({ name }) => {
const constName = name.toUpperCase()
lines.push(` pub const ${constName}: &'static str = "${name}";`)
})
// Add constructor
lines.push("")
lines.push(" #[allow(clippy::too_many_arguments)]")
lines.push(" pub fn new(")
columns.forEach(({ name, type, notNull }) => {
let fieldName = camelToSnakeCase(name)
fieldName = escapeRustKeyword(fieldName)
let rustType = sqliteTypeToRust(type, notNull)
lines.push(` ${fieldName}: ${rustType},`)
})
lines.push(` ) -> ${structName} {`)
lines.push(` ${structName} {`)
columns.forEach(({ name }) => {
let fieldName = camelToSnakeCase(name)
fieldName = escapeRustKeyword(fieldName)
lines.push(` ${fieldName},`)
})
lines.push(" }")
lines.push(" }")
// Add from_row implementation that uses column names
lines.push("")
lines.push(
" pub fn from_row(row: &rusqlite::Row) -> rusqlite::Result<Self> {"
)
lines.push(" Ok(Self {")
columns.forEach(({ name }) => {
let fieldName = camelToSnakeCase(name)
fieldName = escapeRustKeyword(fieldName)
lines.push(
` ${fieldName}: row.get(Self::${name.toUpperCase()})?,`
)
})
lines.push(" })")
lines.push(" }")
// Add column list method
lines.push("")
lines.push(" pub fn columns() -> &'static [&'static str] {")
lines.push(" &[")
columns.forEach(({ name }) => {
lines.push(` Self::${name.toUpperCase()},`)
})
lines.push(" ]")
lines.push(" }")
// Add get_by_id method using column names
const primaryKey = columns.find((col) => col.primaryKey)
if (primaryKey) {
const pkConst = primaryKey.name.toUpperCase()
const pkType = sqliteTypeToRust(primaryKey.type, primaryKey.notNull)
lines.push("")
lines.push(
` pub fn get_by_id(conn: &rusqlite::Connection, id: ${pkType}) -> rusqlite::Result<Option<Self>> {`
)
lines.push(` let columns = Self::columns().join(", ");`)
lines.push(
` let sql = format!("SELECT {} FROM {} WHERE {} = ?", columns, Self::TABLE, Self::${pkConst});`
)
lines.push(" let mut stmt = conn.prepare(&sql)?;")
lines.push(" let mut rows = stmt.query([id])?;")
lines.push("")
lines.push(" if let Some(row) = rows.next()? {")
lines.push(" Ok(Some(Self::from_row(row)?))")
lines.push(" } else {")
lines.push(" Ok(None)")
lines.push(" }")
lines.push(" }")
}
// Add get_all method using column names
lines.push("")
lines.push(
" pub fn get_all(conn: &rusqlite::Connection) -> rusqlite::Result<Vec<Self>> {"
)
lines.push(` let columns = Self::columns().join(", ");`)
lines.push(
` let sql = format!("SELECT {} FROM {}", columns, Self::TABLE);`
)
lines.push(" let mut stmt = conn.prepare(&sql)?;")
lines.push(" let rows = stmt.query_map([], Self::from_row)?;")
lines.push(" rows.collect()")
lines.push(" }")
// Generate methods for indexed columns
columns.forEach(({ name, type, notNull, hasIndex }) => {
if (hasIndex && !primaryKey) {
const methodName = `get_by_${camelToSnakeCase(name)}`
const paramType = sqliteTypeToRust(type, notNull)
const constName = name.toUpperCase()
lines.push("")
lines.push(
` pub fn ${methodName}(conn: &rusqlite::Connection, value: ${paramType}) -> rusqlite::Result<Vec<Self>> {`
)
lines.push(` let columns = Self::columns().join(", ");`)
lines.push(
` let sql = format!("SELECT {} FROM {} WHERE {} = ?", columns, Self::TABLE, Self::${constName});`
)
lines.push(" let mut stmt = conn.prepare(&sql)?;")
lines.push(" let rows = stmt.query_map([value], Self::from_row)?;")
lines.push(" rows.collect()")
lines.push(" }")
}
})
lines.push("}")
return lines.join("\n")
}
function generateModuleStructure(rustCode) {
return [
"//! Generated Rust types from SQLite schema",
"//! Do not edit manually",
"",
"use rusqlite;",
"",
rustCode
].join("\n")
}
function generateRustTypes(dbPath) {
try {
const db = new Database(dbPath)
// Get all table names
const tables = db
.prepare(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
)
.all()
const rustCode = []
// Generate main structs
tables.forEach(({ name: tableName }) => {
const columns = getTableInfo(db, tableName)
rustCode.push(generateRustStruct(tableName, columns))
rustCode.push("") // Add blank line between structs
})
db.close()
return generateModuleStructure(rustCode.join("\n"))
} catch (error) {
console.error("Error:", error.message)
process.exit(1)
}
}
// CLI handling
if (require.main === module) {
if (process.argv.length !== 3) {
console.log("Usage: node sqlite_to_rust.js <path_to_sqlite_db>")
process.exit(1)
}
const dbPath = process.argv[2]
if (!fs.existsSync(dbPath)) {
console.error(`Error: Database file '${dbPath}' does not exist`)
process.exit(1)
}
const rustCode = generateRustTypes(dbPath)
console.log(rustCode)
}
module.exports = {
generateRustTypes,
sqliteTypeToRust,
generateRustStruct
}