Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 107 lines (88 sloc) 2.31 KB
#!/usr/local/bin/stsh
#-sqlite:<ref>dbref
framework:FMDB load.
class ColumnInfo {
var name.
var type.
-description {
"Column: {var:self/name} type: {var:self/type}".
}
}
class TableInfo {
var name.
var columns.
-description {
cd := self columns description.
"Table {var:self/name} columns: {cd}".
}
}
class SQLiteScheme : MPWScheme {
var db.
-initWithPath: dbPath {
self setDb:(FMDatabase databaseWithPath:dbPath).
self db open.
self.
}
-dictionariesForResultSet:resultSet
{
results := NSMutableArray array.
{ resultSet next } whileTrue: { results addObject:resultSet resultDictionary. }.
results.
}
-dictionariesForQuery:query {
self dictionariesForResultSet:(self db executeQuery:query).
}
/. {
|= {
resultSet := self dictionariesForQuery: 'select name from sqlite_master where [type] = "table" '.
resultSet collect at:'name'.
}
}
/schema/:table {
|= {
resultSet := self dictionariesForQuery: "PRAGMA table_info({table})".
columns := resultSet collect: { :colDict |
#ColumnInfo{
#'name': (colDict at:'name') ,
#'type': (colDict at:'type')
}.
}.
#TableInfo{ #'name': table, #'columns': columns }.
}
}
/:table/count {
|= { self dictionariesForQuery: "select count(*) from {table}" | firstObject | at:'count(*)'. }
}
/:table/:index {
|= { self dictionariesForQuery: "select * from {table}" | at: index. }
}
/:table {
|= { self dictionariesForQuery: "select * from {table}". }
}
/:table/:column/:index {
|= { self dictionariesForQuery: "select * from {table}" | at: index. }
}
/:table/where/:column/:value {
|= { self dictionariesForQuery: "select * from {table} where {column} = {value}". }
}
/:table/column/:column {
|= { self dictionariesForQuery: "select {column} from {table}"| collect | at:column. }
}
-tables {
db:. collect: { :table| db:schema/{table}. }.
}
-<void>logTables {
stdout do println: scheme:db tables each.
}
}
extension NSObject {
-initWithDictionary:aDict {
aDict allKeys do:{ :key |
self setValue: (aDict at:key) forKey:key.
}.
self.
}
}
scheme:db := SQLiteScheme alloc initWithPath: dbref path.
stdout println: db:schema/artists
shell runInteractiveLoop.