Files
hotime/vendor/gitee.com/chunanyong/dm/zp.go
T
hoteas b43f968b6c feat(db): 添加对达梦数据库的支持
- 在应用程序中新增对达梦数据库(DM)的配置和连接支持
- 实现 SetDmDB 函数以配置达梦数据库连接
- 更新数据库操作逻辑,支持达梦特有的 SQL 语法和功能
- 在相关文件中添加达梦数据库的处理逻辑,包括表创建、数据插入和查询
- 更新 go.mod 和 go.sum 文件以引入达梦数据库驱动
- 增强文档,详细说明达梦数据库的配置和使用方法
2026-03-20 10:46:51 +08:00

125 lines
2.5 KiB
Go

/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"gitee.com/chunanyong/dm/util"
)
const (
MAX_FILE_SIZE = 100 * 1024 * 1024
FLUSH_SIZE = 32 * 1024
)
type goRun interface {
doRun()
}
type logWriter struct {
flushQueue chan []byte
date string
logFile *os.File
flushFreq int
filePath string
filePrefix string
buffer *Dm_build_0
}
func (lw *logWriter) doRun() {
defer func() {
lw.beforeExit()
lw.closeCurrentFile()
}()
i := 0
for {
var ibytes []byte
select {
case ibytes = <-lw.flushQueue:
if LogLevel != LOG_OFF {
if i == LogFlushQueueSize {
lw.doFlush(lw.buffer)
i = 0
} else {
lw.buffer.Dm_build_26(ibytes, 0, len(ibytes))
i++
}
}
case <-time.After(time.Duration(LogFlushFreq) * time.Millisecond):
if LogLevel != LOG_OFF && lw.buffer.Dm_build_5() > 0 {
lw.doFlush(lw.buffer)
i = 0
}
}
}
}
func (lw *logWriter) doFlush(buffer *Dm_build_0) {
if lw.needCreateNewFile() {
lw.closeCurrentFile()
lw.logFile = lw.createNewFile()
}
if lw.logFile != nil {
buffer.Dm_build_20(lw.logFile, buffer.Dm_build_5())
}
}
func (lw *logWriter) closeCurrentFile() {
if lw.logFile != nil {
lw.logFile.Close()
lw.logFile = nil
}
}
func (lw *logWriter) createNewFile() *os.File {
lw.date = time.Now().Format("2006-01-02")
fileName := lw.filePrefix + "_" + lw.date + "_" + strconv.Itoa(time.Now().Nanosecond()) + ".log"
lw.filePath = LogDir
if len(lw.filePath) > 0 {
if _, err := os.Stat(lw.filePath); err != nil {
os.MkdirAll(lw.filePath, 0755)
}
if _, err := os.Stat(lw.filePath + fileName); err != nil {
logFile, err := os.Create(lw.filePath + fileName)
if err != nil {
fmt.Println(err)
return nil
}
return logFile
}
}
return nil
}
func (lw *logWriter) needCreateNewFile() bool {
now := time.Now().Format("2006-01-02")
fileInfo, err := lw.logFile.Stat()
return now != lw.date || err != nil || lw.logFile == nil || fileInfo.Size() > int64(MAX_FILE_SIZE)
}
func (lw *logWriter) beforeExit() {
close(lw.flushQueue)
var ibytes []byte
for ibytes = <-lw.flushQueue; ibytes != nil; ibytes = <-lw.flushQueue {
lw.buffer.Dm_build_26(ibytes, 0, len(ibytes))
if lw.buffer.Dm_build_5() >= LogBufferSize {
lw.doFlush(lw.buffer)
}
}
if lw.buffer.Dm_build_5() > 0 {
lw.doFlush(lw.buffer)
}
}
func (lw *logWriter) WriteLine(msg string) {
var b = []byte(strings.TrimSpace(msg) + util.LINE_SEPARATOR)
lw.flushQueue <- b
}