{{$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 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 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