plugins/db/dao.js

714 lines
22 KiB
JavaScript

{{$dao := . -}}
let _{{$dao.DBName}}Dao = {_daoInstances:{}, _daoActions:{}}
function _make{{$dao.DBName}}Dao(conn, tx, rd) {
let dao = {_tableInstances:{}}
let daoActions = _{{$dao.DBName}}Dao._daoActions
// let tableActions = _{{$dao.DBName}}Dao._tableActions
for(let k in _{{$dao.DBName}}Dao) if(typeof _{{$dao.DBName}}Dao[k]==='function') dao[k] = _{{$dao.DBName}}Dao[k]
for(let k in daoActions) if(typeof daoActions[k]==='function') dao[k] = daoActions[k]
{{- range $t := .Tables }}
Object.defineProperty(dao, "{{$t.TableName}}", {get: function(){return dao._getTable("{{$t.TableName}}")}})
{{- end }}
//for(let tableName in _{{$dao.DBName}}Dao._table) Object.defineProperty(dao, tableName, {get: function(){return dao._getTable(tableName)}})
dao._conn = conn
dao._tx = tx
dao._rd = rd
return dao
}
function get{{$dao.FixedDBName}}Dao(connectionName, redisName) {
let cachedDaoName = (connectionName||'')+'_'+(redisName||'')
if(_{{$dao.DBName}}Dao._daoInstances[cachedDaoName]) return _{{$dao.DBName}}Dao._daoInstances[cachedDaoName]
let conn = typeof db==='object' ? (connectionName ? db.fetch(connectionName) : db.fetch()) : null
let rd = typeof redis==='object' ? (redisName ? redis.fetch(redisName) : redis.fetch()) : null
let dao = _make{{$dao.DBName}}Dao(conn, null, rd)
_{{$dao.DBName}}Dao._daoInstances[cachedDaoName] = dao
return dao
}
_{{$dao.DBName}}Dao._getTable = function(tableName){
if(this._tableInstances[tableName]) return this._tableInstances[tableName]
let tb = _{{$dao.DBName}}Dao[tableName]
let t = {}
let daoActions = _{{$dao.DBName}}Dao._daoActions
for(let k in tb) if(typeof tb[k]==='function') t[k] = tb[k]
for(let k in daoActions) if(typeof daoActions[k]==='function') t[k] = daoActions[k]
// for(let k in tableActions) if(typeof tableActions[k]==='function') t[k] = tableActions[k]
t._tableName = tableName
t._conn = this._conn
t._tx = this._tx
t._rd = this._rd
this._tableInstances[tableName] = t
return t
}
_{{$dao.DBName}}Dao.begin = function() {
return _make{{$dao.DBName}}Dao(this._conn, this._conn.begin(), this._rd)
}
_{{$dao.DBName}}Dao._daoActions.query = function(sql, ...args) {
if(this._tx) return this._tx.query(sql, ...args)
if(this._conn) return this._conn.query(sql, ...args)
return null
}
_{{$dao.DBName}}Dao._daoActions.query1 = function(sql, ...args) {
if(this._tx) return this._tx.query1(sql, ...args)
let aa = this._conn.query1(sql, ...args)
if(this._conn) return this._conn.query1(sql, ...args)
return null
}
_{{$dao.DBName}}Dao._daoActions.query11 = function(sql, ...args) {
if(this._tx) return this._tx.query11(sql, ...args)
if(this._conn) return this._conn.query11(sql, ...args)
return null
}
_{{$dao.DBName}}Dao._daoActions.query1a = function(sql, ...args) {
if(this._tx) return this._tx.query1a(sql, ...args)
if(this._conn) return this._conn.query1a(sql, ...args)
return null
}
_{{$dao.DBName}}Dao._daoActions.exec = function(sql, ...args) {
if(this._tx) return this._tx.exec(sql, ...args)
if(this._conn) return this._conn.exec(sql, ...args)
return null
}
_{{$dao.DBName}}Dao._daoActions.insertTo = function(table, data) {
if(this._tx) return this._tx.insert(table, data)
if(this._conn) return this._conn.insert(table, data)
return null
}
_{{$dao.DBName}}Dao._daoActions.replaceTo = function(table, data) {
if(this._tx) return this._tx.replace(table, data)
if(this._conn) return this._conn.replace(table, data)
return null
}
_{{$dao.DBName}}Dao._daoActions.updateTo = function(table, data, sql, ...args) {
if(this._tx) return this._tx.update(table, data, sql, ...args)
if(this._conn) return this._conn.update(table, data, sql, ...args)
return null
}
_{{$dao.DBName}}Dao._daoActions.deleteFrom = function(table, where, ...args) {
if(this._tx) return this._tx.delete(table, where, ...args)
if(this._conn) return this._conn.delete(table, where, ...args)
return null
}
{{- range $t := .Tables }}
_{{$dao.DBName}}Dao.{{$t.TableName}} = {}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item = {}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query = {}
_{{$dao.DBName}}Dao.{{$t.TableName}}QueryList = {}
_{{$dao.DBName}}Dao.{{$t.TableName}}.attach = function(item){
if(!item || typeof item !== 'object') return null
let o = {}
for(let k in item) o[k] = item[k]
let itemSet = _{{$dao.DBName}}Dao.{{$t.TableName}}Item
for(let k in itemSet) if(typeof itemSet[k]==='function') o[k] = itemSet[k]
// item._conn = this._conn
// item._tx = this._tx
// item._rd = this._rd
o._table = this
return o
}
_{{$dao.DBName}}Dao.{{$t.TableName}}.attachList = function(list){
if(!list || typeof list !== 'object' || !(list instanceof Array)) return []
let a = []
for(let i=0; i<list.length; i++) a.push(this.attach(list[i]))
let queryListSet = _{{$dao.DBName}}Dao.{{$t.TableName}}QueryList
for(let k in queryListSet) if(typeof queryListSet[k]==='function') a[k] = queryListSet[k]
return a
}
_{{$dao.DBName}}Dao.{{$t.TableName}}.new = function(data){
let item = {}
{{- range $f := .Fields }}
item.{{$f.Name}} = data && data.{{$f.Name}} !== undefined ? data.{{$f.Name}} : {{$f.Default}}
{{- end }}
{{- if .IsAutoId }}
delete item.{{.AutoIdField}}
{{- end }}
{{- if $t.InvalidSet}}
item.{{$t.ValidField}} = {{$t.ValidFieldConfig.ValidSetValue}}
{{- end }}
return this.attach(item)
}
{{range $k := .UniqueKeys -}}
_{{$dao.DBName}}Dao.{{$t.TableName}}.getBy{{$k.Name}} = function({{$k.Args}}) {
let rr = this.query1("SELECT {{$t.SelectFields}} FROM `{{$t.TableName}}` WHERE {{$k.Where}}{{$t.ValidWhere}}", {{$k.Args}})
if(this._conn) return this.attach(rr)
return null
}
{{- end }}
{{ if $t.PrimaryKey -}}
_{{$dao.DBName}}Dao.{{$t.TableName}}.get = function({{$t.PrimaryKey.Args}}) {
return this.attach( this.query1("SELECT {{$t.SelectFields}} FROM `{{$t.TableName}}` WHERE {{$t.PrimaryKey.Where}}{{$t.ValidWhere}}", {{$t.PrimaryKey.Args}}))
}
_{{$dao.DBName}}Dao.{{$t.TableName}}.getWithFields = function({{$t.PrimaryKey.Args}}, fields) {
return this.attach(this.query1("SELECT "+fields+" FROM `{{$t.TableName}}` WHERE {{$t.PrimaryKey.Where}}{{$t.ValidWhere}}", {{$t.PrimaryKey.Args}}))
}
{{ if .ValidSet -}}
_{{$dao.DBName}}Dao.{{$t.TableName}}.getWithInvalid = function({{$t.PrimaryKey.Args}}) {
return this.attach(this.query1("SELECT {{$t.SelectFields}} FROM `{{$t.TableName}}` WHERE {{$t.PrimaryKey.Where}}", {{$t.PrimaryKey.Args}}))
}
{{- end }}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}.insert = function(data) {
{{- range $index, $field := .AutoGenerated }}
if( data["{{$field}}"] == null ) delete data["{{$field}}"]
{{- end }}
{{- range $index, $field := .AutoGeneratedOnUpdate }}
delete data["{{$field}}"]
{{- end }}
{{- if $t.InvalidSet}}
if(data.{{$t.ValidField}}===undefined) data.{{$t.ValidField}} = {{$t.ValidFieldConfig.ValidSetValue}}
{{- end }}
{{- if .HasVersion }}
let version = this._getVersion()
data["{{$t.VersionField}}"] = version
{{- end }}
let r = this.insertTo("{{$t.TableName}}", data)
{{- if .HasVersion }}
this._commitVersion(version)
r.version = version
{{- end }}
return r
}
_{{$dao.DBName}}Dao.{{$t.TableName}}.replace = function(data) {
{{- range $index, $field := .AutoGenerated }}
if( data["{{$field}}"] == null ) delete data["{{$field}}"]
{{- end }}
{{- range $index, $field := .AutoGeneratedOnUpdate }}
delete data["{{$field}}"]
{{- end }}
{{- if $t.InvalidSet}}
if(data.{{$t.ValidField}}===undefined) data.{{$t.ValidField}} = {{$t.ValidFieldConfig.ValidSetValue}}
{{- end }}
{{- if .HasVersion }}
let version = this._getVersion()
data["{{$t.VersionField}}"] = version
{{- end }}
let r = this.replaceTo("{{$t.TableName}}", data)
{{- if .HasVersion }}
this._commitVersion(version)
r.version = version
{{- end }}
return r
}
{{if .PrimaryKey -}}
_{{$dao.DBName}}Dao.{{$t.TableName}}.update = function(data, {{$t.PrimaryKey.Args}}) {
{{- range $index, $field := .AutoGenerated }}
if( data["{{$field}}"] == null ) delete data["{{$field}}"]
{{- end }}
{{- range $index, $field := .AutoGeneratedOnUpdate }}
delete data["{{$field}}"]
{{- end }}
{{- if .HasVersion }}
let version = this._getVersion()
data["{{$t.VersionField}}"] = version
{{- end }}
let r = this.updateTo("{{$t.TableName}}", data, "{{$t.PrimaryKey.Where}}", {{$t.PrimaryKey.Args}})
{{- if .HasVersion }}
this._commitVersion(version)
r.version = version
{{- end }}
return r
}
{{- if .InvalidSet }}
_{{$dao.DBName}}Dao.{{$t.TableName}}.enable = function({{$t.PrimaryKey.Args}}) {
{{- if .HasVersion }}
let version = this._getVersion()
let r = this.exec("UPDATE `{{$t.TableName}}` set {{$t.ValidSet}}, `{{$t.VersionField}}`=? WHERE {{$t.PrimaryKey.Where}}", version, {{$t.PrimaryKey.Args}})
{{- else }}
let r = this.exec("UPDATE `{{$t.TableName}}` set {{$t.ValidSet}} WHERE {{$t.PrimaryKey.Where}}", {{$t.PrimaryKey.Args}})
{{- end }}
{{- if .HasVersion }}
this._commitVersion(version)
r.version = version
{{- end }}
return r
}
_{{$dao.DBName}}Dao.{{$t.TableName}}.disable = function({{$t.PrimaryKey.Args}}){
{{- if .HasVersion }}
let version = this._getVersion()
let r = this.exec("UPDATE `{{$t.TableName}}` set {{$t.InvalidSet}}, `{{$t.VersionField}}`=? WHERE {{$t.PrimaryKey.Where}}", version, {{$t.PrimaryKey.Args}})
{{- else }}
let r = this.exec("UPDATE `{{$t.TableName}}` set {{$t.InvalidSet}} WHERE {{$t.PrimaryKey.Where}}", {{$t.PrimaryKey.Args}})
{{- end }}
{{- if .HasVersion }}
this._commitVersion(version)
r.version = version
{{- end }}
return r
}
{{- else }}
_{{$dao.DBName}}Dao.{{$t.TableName}}.delete = function({{$t.PrimaryKey.Args}}) {
return this.exec("DELETE FROM `{{$t.TableName}}` WHERE {{$t.PrimaryKey.Where}}", {{$t.PrimaryKey.Args}})
}
_{{$dao.DBName}}Dao.{{$t.TableName}}.deleteBy = function(where, ...args) {
return this.deleteFrom("{{.TableName}}", where, ...args)
}
{{- end }}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}.updateBy = function(data, where, ...args) {
{{- if .HasVersion }}
let version = this._getVersion()
data["{{.VersionField}}"] = version
{{- end }}
let r = this.updateTo("{{.TableName}}", data, where, ...args)
{{- if .HasVersion }}
this._commitVersion(version)
r.version = version
{{- end }}
return r
}
{{ if .HasVersion -}}
_{{$dao.DBName}}Dao.{{$t.TableName}}._getVersion = function() {
let version = 0
if(this._rd){
version = this._rd.incr("_DATA_VERSION_{{.TableName}}")
if(version > 1){
this._rd.setEX("_DATA_VERSION_DOING_{{.TableName}}_"+version, 10, true)
return version
}
this._rd.del("_DATA_VERSION_{{.TableName}}")
version = 0
} else {
logger.warn("use version but not configured redis", {db:"{{.DBName}}", table:"{{.TableName}}"})
}
let maxVersion = parseInt(this.query11("SELECT MAX(`{{.VersionField}}`) FROM `{{.TableName}}`") || '0')
version = maxVersion+1
if(this._rd){
this._rd.mSet("_DATA_VERSION_{{.TableName}}", version, "_DATA_MAX_VERSION_{{.TableName}}", version)
this._rd.setEX("_DATA_VERSION_DOING_{{.TableName}}_"+version, 10, true)
}
return version
}
_{{$dao.DBName}}Dao.{{$t.TableName}}._commitVersion = function(version) {
if(this._rd){
this._rd.del("_DATA_VERSION_DOING_{{.TableName}}_"+version)
let seqVersion = parseInt(this._rd.get("_DATA_VERSION_{{.TableName}}") || '0')
let currentMaxVersion = parseInt(this._rd.get("_DATA_MAX_VERSION_{{.TableName}}") || '0')
for(let i=currentMaxVersion; i<=seqVersion; i++){
if(this._rd.exists("_DATA_VERSION_DOING_{{.TableName}}_"+i)){
break
} else {
this._rd.set("_DATA_MAX_VERSION_{{.TableName}}", i)
}
}
}
}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}.newQuery = function(){
let query = {
_validWhere: "{{.ValidWhere}}",
_sql: "",
_fields: "{{.SelectFields}}",
_where: "",
_extraSql: "",
_extraArgs: [],
_args: [],
_leftJoins: [],
_leftJoinArgs: [],
_result: null,
_lastSql: "",
_lastSqlArgs: [],
}
let querySet = _{{$dao.DBName}}Dao.{{$t.TableName}}Query
for(let k in querySet) if(typeof querySet[k]==='function') query[k] = querySet[k]
query._table = this
// query._conn = this._conn
// query._tx = this._tx
// query._rd = this._rd
return query
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query._parseFields = function(fields, table) {
let fieldArr = fields
if(typeof fields !== 'object' || !(fields instanceof Array)) {
if(typeof fields !== 'string') fields = fields.toString()
if (fields==="" || fields.indexOf('(') !== -1 || fields.indexOf('`') !== -1) return fields
fieldArr = fields.split(",")
}
for(let i=0; i<fieldArr.length; i++){
let field = fieldArr[i]
if(typeof field !== 'string') field = field.toString()
field = field.trim()
let as = ""
if(field.indexOf(' ')!==-1){
let a = field.split(" ")
field = a[0]
if(a[a.length-2].toLowerCase()==="as" && !a[a.length-1].startsWith("`")){
a[a.length-1] = "`" + a[a.length-1] + "`"
}
as = " " + a.slice(1),join(" ")
}
if(table){
fieldArr[i] = "`"+table+"`.`"+field+"`"+as
} else {
fieldArr[i] = "`"+field+"`"+as
}
}
return fieldArr.join(",")
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query._parse = function(tag){
if(this._sql !== ""){
return this._sql, this._args
}
let fields = this._fields
let validWhere = this._validWhere
if(tag==="COUNT"){
fields = "COUNT(*)"
}else if(tag==="COUNT_ALL"){
fields = "COUNT(*)"
validWhere = ""
}else if(tag==="ALL"){
validWhere = ""
}else if(tag==="ALL_VERSION"){
validWhere = ""
//}else if(tag==="VERSION"){
}
let leftJoinsStr = ""
if(this._leftJoins.length > 0){
leftJoinsStr = " " + this._leftJoins.join(" ")
this._args.unshift(...this._leftJoinArgs)
validWhere = validWhere.replace(/ AND /g, " AND `{{.TableName}}`.")
}
if(this._where==="" && validWhere.startsWith(" AND ")) validWhere = validWhere.substring(5)
if(this._extraArgs) this._args.push(...this._extraArgs)
return {sql:"SELECT " + fields + " FROM `{{.TableName}}`" + leftJoinsStr + " WHERE " + this._where + validWhere + this._extraSql, args:this._args}
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.sql = function(sql, ...args){
this._sql = sql
this._args = args
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.fields = function(fields){
this._fields = this._parseFields(fields, "")
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.appendFields = function(fields){
if(this._fields) this._fields += ", "
this._fields += this._parseFields(fields, "")
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.where = function(where, ...args){
this._where = where
this._args = args
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.in = function(field, ...values){
if(field.indexOf("`")===-1) field = "`"+field+"`"
this._where = field+" IN "+this._conn.inKeys(values.length)
this._args = values
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.and = function(where, ...args){
if(this._where) this._where += " AND "
this._where += where
this._args = this._args.push(...args)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.or = function(where, ...args){
if(this._where) this._where += " OR "
this._where += where
this._args = this._args.push(...args)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.andIn = function(field, ...values){
if(field.indexOf("`")===-1) field = "`"+field+"`"
if(this._where) this._where += " AND "
this._where += field + " IN "+this._conn.inKeys(values.length)
this._args = this._args.push(...values)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.orIn = function(field, ...values){
if(field.indexOf("`")===-1) field = "`"+field+"`"
if(this._where) this._where += " OR "
this._where += field + " IN "+this._conn.inKeys(values.length)
this._args = this._args.push(...values)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.orderBy = function(orderBy){
this._extraSql += " ORDER BY " + orderBy
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.groupBy = function(groupBy){
this._extraSql += " GROUP BY " + groupBy
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.limit = function(start, num){
if(num===undefined){
num = start
start = 0
}
this._extraSql += " LIMIT ?,?"
this._extraArgs.push(start, num)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.having = function(where, ...args){
this._extraSql += " HAVING "+where
this._extraArgs = this._extraArgs.push(...args)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.extra = function(sql, ...args){
this._extraSql += sql
this._extraArgs = this._extraArgs.push(...args)
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.leftJoin = function(joinTable, fields, on, ...args){
if(this._fields.indexOf("`{{.TableName}}`.")===-1){
this._fields = "`{{.TableName}}`."+this._fields.replace(/`, `/g, "`, `{{.TableName}}`.`")
}
if(fields) this._fields += ", "+this._parseFields(fields, joinTable)
this._leftJoins = this._leftJoins.push("LEFT JOIN `" + joinTable + "` ON " + on)
this._leftJoinArgs = this._leftJoinArgs.push(...args)
return this
}
{{range $idx := .IndexKeys}}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.by{{$idx.Name}} = function({{$idx.Args}}){
return this.where("{{$idx.Where}}", {{$idx.Args}})
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.and{{$idx.Name}} = function({{$idx.Args}}){
if(this._where) this._where += " AND "
this._where += "{{$idx.Where}}"
this._args = this._args.push({{$idx.Args}})
return this
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.or{{$idx.Name}} = function({{$idx.Args}}){
if(this._where){
this._where += " OR "
}
this._where += "{{$idx.Where}}"
this._args = this._args.push({{$idx.Args}})
return this
}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.query = function(){
let {sql,args} = this._parse("")
this._lastSql = sql
this._lastSqlArgs = args
this._result = this._table.query(sql, ...args).result
return this
}
{{- if .ValidSet }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.queryWithValid = function(){
let {sql,args} = this._parse("ALL")
this._lastSql = sql
this._lastSqlArgs = args
this._result = this._table.query(sql, ...args).result
return this
}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.count = function(){
let {sql,args} = this._parse("COUNT")
this._lastSql = sql
this._lastSqlArgs = args
return parseInt(this._table.query11(sql, ...args) || '0')
}
{{- if .ValidSet }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.countAll = function(){
let {sql,args} = this._parse("COUNT_ALL")
this._lastSql = sql
this._lastSqlArgs = args
return parseInt(this._table.query11(sql, ...args) || '0')
}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.queryByPage = function(start, num){
this.limit(start, num)
return this.query()
}
{{- if .ValidSet }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.queryWithValidByPage = function(start, num){
this.limit(start, num)
return this.queryWithValid()
}
{{- end }}
{{- if .HasVersion }}
{{- if .ValidSet }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.queryByVersion = function(minVersion, maxVersion, limit, withInvalid){
if(minVersion > 0) withInvalid = true
let parseTag = u.StringIf(withInvalid, "ALL_VERSION", "VERSION")
{{- else }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.queryByVersion = function(minVersion, maxVersion, limit){
let parseTag = "VERSION"
{{- end }}
if(maxVersion===0){
if(this._rd){
maxVersion = parseInt(this._rd.GET("_DATA_MAX_VERSION_{{.TableName}}") || '0')
} else {
logger.warn("use version but not configured redis", {db:"{{.DBName}}", table:"{{.TableName}}"})
}
if(maxVersion===0){
maxVersion = parseInt(this._table.query11("SELECT MAX(`{{.VersionField}}`) FROM `{{.TableName}}`") || '0')
this._lastSql = "SELECT MAX(`{{.VersionField}}`) FROM `{{.TableName}}`"
this._lastSqlArgs = []
}
}
this.and("`version` BETWEEN ? AND ?", minVersion+1, maxVersion )
if(limit > 0){
this.orderBy("`{{.VersionField}}`")
this.limit(0, limit)
}
let {sql,args} = this._parse(parseTag)
this._lastSql = sql
this._lastSqlArgs = args
this._result = this._table.query(sql, ...args).result
this.maxVersion = maxVersion
return this
}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.first = function(){
let list = this.list()
if(list.length > 0) return this._table.attach(list[0])
return null
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.list = function(){
if(this._result===null) this.query()
return this._table.attachList(this._result)
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.listBy = function(...fields){
if(this._result===null) this.query()
let list = []
for(let item of this._result){
let o = {}
for(let k in item) if(fields.indexOf(k)!==-1) o[k] = item[k]
list.push(o)
}
return list
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.getSql = function(){
return this._lastSql
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Query.getSqlArgs = function(){
return this._lastSqlArgs
}
_{{$dao.DBName}}Dao.{{$t.TableName}}QueryList.getData = function(){
let list = []
for(let i=0; i<this.length; i++) list.push(list[i].getData())
return list
}
_{{$dao.DBName}}Dao.{{$t.TableName}}QueryList.getJson = function(){
return JSON.stringify(this.getData())
}
{{- if .PrimaryKey }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item.save = function(){
return this._table.replace(this.getData())
}
{{- if .InvalidSet }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item.enable = function(){
return this._table.enable({{.PrimaryKey.ItemArgs}})
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item.disable = function(){
return this._table.disable({{.PrimaryKey.ItemArgs}})
}
{{- else }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item.delete = function(){
return this._table.delete({{.PrimaryKey.ItemArgs}})
}
{{- end }}
{{- end }}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item.getData = function(){
let data = {}
{{- range $f := .Fields }}
if(this.{{$f.Name}} !== undefined) data.{{$f.Name}} = this.{{$f.Name}}
{{- end }}
return data
}
_{{$dao.DBName}}Dao.{{$t.TableName}}Item.getJson = function(){
return JSON.stringify(this.getData())
}
{{- end }}