mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2024-12-22 00:36:41 +02:00
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package schema
|
|
|
|
import (
|
|
"database/sql"
|
|
)
|
|
|
|
// TODO(js) Can we see tables in an attached database? How are their names handled? See https://sqlite.org/lang_naming.html
|
|
|
|
const sqliteAllColumns = `SELECT * FROM %s LIMIT 0`
|
|
|
|
const sqliteTableNamesWithSchema = `
|
|
SELECT
|
|
"" AS schema,
|
|
name
|
|
FROM
|
|
sqlite_master
|
|
WHERE
|
|
type = 'table'
|
|
ORDER BY
|
|
name
|
|
`
|
|
|
|
const sqliteViewNamesWithSchema = `
|
|
SELECT
|
|
"" AS schema,
|
|
name
|
|
FROM
|
|
sqlite_master
|
|
WHERE
|
|
type = 'view'
|
|
ORDER BY
|
|
name
|
|
`
|
|
|
|
const sqlitePrimaryKey = `
|
|
SELECT
|
|
name
|
|
FROM
|
|
pragma_table_info(?)
|
|
WHERE
|
|
pk > 0
|
|
ORDER BY
|
|
pk
|
|
`
|
|
|
|
type sqliteDialect struct{}
|
|
|
|
func (sqliteDialect) escapeIdent(ident string) string {
|
|
// "tablename"
|
|
return escapeWithDoubleQuotes(ident)
|
|
}
|
|
|
|
func (d sqliteDialect) ColumnTypes(db *sql.DB, schema, name string) ([]*sql.ColumnType, error) {
|
|
return fetchColumnTypes(db, sqliteAllColumns, schema, name, d.escapeIdent)
|
|
}
|
|
|
|
func (sqliteDialect) PrimaryKey(db *sql.DB, schema, name string) ([]string, error) {
|
|
// if schema == "" {
|
|
// return fetchNames(db, sqlitePrimaryKey, "", name)
|
|
// }
|
|
return fetchNames(db, sqlitePrimaryKey, "", name)
|
|
}
|
|
|
|
func (sqliteDialect) TableNames(db *sql.DB) ([][2]string, error) {
|
|
return fetchObjectNames(db, sqliteTableNamesWithSchema)
|
|
}
|
|
|
|
func (sqliteDialect) ViewNames(db *sql.DB) ([][2]string, error) {
|
|
return fetchObjectNames(db, sqliteViewNamesWithSchema)
|
|
}
|