Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Sep 7, 2017
1 parent 1bdb3a0 commit 274b5e4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
5 changes: 1 addition & 4 deletions src/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ export type SchemaDisposeFunction<T> =

export interface ShapeMatcher {
mainTable: lf.schema.Table
pk: {
name: string,
queried: boolean
}
pk: string
definition: Object
}

Expand Down
32 changes: 12 additions & 20 deletions src/storage/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class Database {
}

return this.database$
.concatMap<any, any>(db => {
.concatMap(db => {
const entity = clone(raw)
const [ table ] = Database.getTables(db, tableName)
const columnMapper = schema!.mapper
Expand Down Expand Up @@ -290,7 +290,7 @@ export class Database {
}

const queries: lf.query.Builder[] = []
const removedIds: any = []
const removedIds: string[] = []
queries.push(predicatableQuery(db, table, predicate!, StatementType.Delete))

const prefetch = predicatableQuery(db, table, predicate!, StatementType.Select)
Expand Down Expand Up @@ -433,7 +433,6 @@ export class Database {
const containFields = !!clause.fields
let predicate: Predicate<T>

const containKey = containFields ? contains(pk, clause.fields!) : true
const [ additionJoinInfo, err ] = !clause.where ? [ null, null ] :
tryCatch(() => {
predicate = parsePredicate(clause.where!)
Expand All @@ -454,14 +453,14 @@ export class Database {
const tablesStruct: TablesStruct = Object.create(null)

const { table, columns, joinInfo, definition, contextName } =
this.traverseQueryFields(db, tableName, fieldsSet, containKey, !containFields, [], {}, tablesStruct, mode)
this.traverseQueryFields(db, tableName, fieldsSet, !containFields, [], {}, tablesStruct, mode)
const query =
predicatableQuery(db, table!, null, StatementType.Select, ...columns)

joinInfo.forEach((info: JoinInfo) => {
const pred = info.predicate
if (pred) {
query.leftOuterJoin(info.table, pred)
const joinPredicate = info.predicate
if (joinPredicate) {
query.leftOuterJoin(info.table, joinPredicate)
}
})

Expand All @@ -475,10 +474,7 @@ export class Database {
})

const matcher = {
pk: {
name: pk,
queried: containKey
},
pk,
definition,
mainTable: table!
}
Expand Down Expand Up @@ -533,7 +529,6 @@ export class Database {
db: lf.Database,
tableName: string,
fieldsValue: Set<Field>,
hasKey: boolean,
glob: boolean,
path: string[] = [],
context: Record = {},
Expand Down Expand Up @@ -564,12 +559,9 @@ export class Database {
.every(key => contains(key, navigators))
assert(!onlyNavigator, Exception.InvalidQuery())

if (!hasKey) {
// 保证主键一定比关联字段更早的被遍历到
const fields = Array.from(fieldsValue)
fields.unshift(schema.pk)
fieldsValue = new Set(fields)
}
// 保证主键一定比关联字段更早的被遍历到
fieldsValue.delete(schema.pk)
fieldsValue.add(schema.pk)

const suffix = (context[tableName] || 0) + 1
context[tableName] = suffix
Expand Down Expand Up @@ -648,9 +640,9 @@ export class Database {
handleAdvanced({ columns: [column], advanced: true }, ctx.key, columnDef)
break
case LeafType.navigator:
const { containKey, fields, assocaiation } = ctx.leaf as NavigatorLeaf
const { fields, assocaiation } = ctx.leaf as NavigatorLeaf
const ret =
this.traverseQueryFields(db, assocaiation.name, new Set(fields), containKey, glob, path.slice(0), context, tablesStruct, mode)
this.traverseQueryFields(db, assocaiation.name, new Set(fields), glob, path.slice(0), context, tablesStruct, mode)
handleAdvanced(ret, ctx.key, assocaiation)
ctx.skip()
break
Expand Down
12 changes: 6 additions & 6 deletions src/storage/modules/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Selector <T> {
}
const { pk, mainTable } = this.shape

const column = mainTable[pk.name]
const column = mainTable[pk]
const rangeQuery = predicatableQuery(this.db, mainTable, predicate, StatementType.Select, column)

if (this.orderDescriptions && this.orderDescriptions.length) {
Expand Down Expand Up @@ -187,7 +187,7 @@ export class Selector <T> {
values(): Observable<T[]> | never {
if (typeof this.limit !== 'undefined' || typeof this.skip !== 'undefined') {
const p = this.rangeQuery.exec()
.then(r => r.map(v => v[this.shape.pk.name]))
.then(r => r.map(v => v[this.shape.pk]))
.then(pks => this.getValue(this.getQuery(this.inPKs(pks))))
return this.mapFn(Observable.fromPromise(p))
} else {
Expand Down Expand Up @@ -229,15 +229,15 @@ export class Selector <T> {

private inPKs(pks: (string | number)[]): lf.Predicate {
const { pk, mainTable } = this.shape
return mainTable[pk.name].in(pks)
return mainTable[pk].in(pks)
}

private getValue(query: lf.query.Select) {
return query.exec()
.then((rows: any[]) => {
const result = graph<T>(rows, this.shape.definition)
const col = this.shape.pk.name
return !this.shape.pk.queried ? this.removeKey(result, col) : result
const col = this.shape.pk
return !this.shape.pk ? this.removeKey(result, col) : result
})
}

Expand Down Expand Up @@ -276,7 +276,7 @@ export class Selector <T> {
const listener = () => {
return rangeQuery.exec()
.then((r) => {
observer.next(r.map(v => v[this.shape.pk.name]))
observer.next(r.map(v => v[this.shape.pk]))
})
.catch(e => observer.error(e))
}
Expand Down

0 comments on commit 274b5e4

Please sign in to comment.