forked from alsonkemp/turbinado
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alsonk
committed
Nov 25, 2008
1 parent
5d7e217
commit 421e3c0
Showing
3 changed files
with
180 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
module Turbinado.Database.ORM.Generator where | ||
|
||
|
||
import qualified Data.Map as M | ||
|
||
import Config.Master | ||
|
||
type ConnectionString = String | ||
type TableName = String | ||
type ColumnName = String | ||
type Column = (SqlColDesc, DependentKeys, Boolean) -- Boolean == isPrimaryKey | ||
type DependentKeys = [(TableName, ColumnName)] -- all columns which are targets of foreign keys | ||
type DependentKeys = [((ColumnName, (TableName, ColumnName))] -- all columns which are targets of foreign keys | ||
|
||
type TableColumn = (TableName, ColumnName) | ||
type TableColumns = M.Map TableColumn Column | ||
|
||
generateModels :: FilePath -> IO () | ||
generateModels cs fp = do conn <- openDBConnection | ||
generateModels cs fp = do conn <- databaseConnection | ||
ts <- Database.HDBC.getTables conn | ||
ds <- zip ts $ mapM (describeTable conn) ts | ||
let tcs = combineTablesColumns ts ds | ||
pks <- getPrimaryKeys conn t | ||
let tcs' = combinePrimaryKeys tcs pks | ||
-- TODO: Pull in indices | ||
pks <- getPrimaryKeys conn ts | ||
let tcs' = combinePrimaryKeys pks tcs | ||
fks <- getForeignKeys t | ||
let tcs'' = foldl | ||
let tcs'' = combineForeignKeys fks tcs' | ||
|
||
|
||
combineTablesColumns :: [TableName] -> [(ColumnName, SqlColDesc)] -> TableColumn | ||
combineTablesColumns :: [TableName] -> [(ColumnName, SqlColDesc)] -> TableColumns | ||
combineTablesColumsn ts cs = | ||
M.fromList $ zipWith (\t (c, d) -> ((t,c), (d, [], False)) ) ts cs | ||
|
||
combinePrimaryKeys :: [(TableName, [ColumnName])] -> TableColumns -> TableColumns | ||
combinePrimaryKeys pks tcs = | ||
foldl (\tcs (t, cs) -> foldl (\c -> M.adjust (\(d,k,_) -> (d, k, True)) (t, c) ) tcs cs) tcs pks | ||
|
||
addDependentKey :: (TableColumn, TableColumn) -> TableColumns -> TableColumns | ||
addDependentKey (parTable, parColumn), ((depTable, depColumn)) t = | ||
let c@(d, k, i) = M.lookup (parTable, parColumn) t in | ||
M.insert (parTable, parColumn) (d, k `union` (depTable, depColumn), i) | ||
combineForeignKeys :: [(TableColumn, TableColumn)] -> TableColumns -> TableColumns | ||
combineForeignKeys fks tcs = foldl (\tcs' fk -> combineForeignKey fk tcs') tcs fks | ||
|
||
combineForeignKey :: (TableColumn, TableColumn) -> TableColumns -> TableColumns | ||
combineForeignKey (parTable, parColumn), ((depTable, depColumn)) tcs = | ||
let c@(d, k, i) = M.lookup (parTable, parColumn) tcs in | ||
M.insert (parTable, parColumn) (d, k `union` (parColumn, (depTable, depColumn)), i) t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters