dbfunc.pas
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /// <author>cxg 2020-9-29</author> unit dbfunc; interface uses Classes, uunidacpool, serial, uunidac, ulog, SysUtils; function query( const params : RawByteString): RawByteString; function save( const params : RawByteString): RawByteString; function execsql( const params : RawByteString): RawByteString; function execsp( const params : RawByteString): RawByteString; implementation function query( const params : RawByteString): RawByteString; var pool: tunidacpool; dm: Tunidac; requ, resp: TSerial; accountno, sql: string ; tablenum, i: integer; begin dm := nil; pool := nil; requ := TSerial.Create; resp := TSerial.Create; try try requ.loadFromRawByteString( params ); accountno := requ.force( 'accountno' ).AsString; tablenum := requ.force( 'tablenum' ).AsInteger; pool := GetDBPool(accountno); dm := pool.Lock; dm.dsp.DataSet := dm.qry; for i := 1 to tablenum do begin sql := requ.force( 'sql' +inttostr(i)).AsString ; dm.qry.Close; dm.qry.sql.Clear; dm.qry.sql.Add(sql); resp.force( 'dataset' + inttostr(i)).AsVariant := dm.dsp.Data; end; dm.dsp.DataSet := nil; resp.force( 'return' ).AsBoolean := True; Result := resp.saveToRawByteString; except on e: Exception do begin resp.force( 'return' ).AsBoolean := False; resp.force( 'err' ).AsString := e.Message; Result := resp.saveToRawByteString; Log.WriteLog( 'dbfunc.query ' + e.Message); end; end; finally pool.unlock(dm); requ.Free; resp.Free; end; end; function save( const params : RawByteString): RawByteString; var pool: tunidacpool; dm: Tunidac; requ, resp: TSerial; accountno, tablename, nosavefields: string ; tablenum, i, err: integer; delta: OleVariant; begin pool := nil; dm := nil; requ := TSerial.Create; resp := TSerial.Create; try try requ.loadFromRawByteString( params ); tablenum := requ.force( 'tablenum' ).AsInteger; accountno := requ.force( 'accountno' ).AsString; pool := GetDBPool(accountno); dm := pool.Lock; dm.dsp.DataSet := dm.qry; if not dm.conn.InTransaction then dm.conn.StartTransaction; // 开启事务 for i := 1 to tablenum do begin tablename := requ.force( 'tablename' + IntToStr(i)).AsString; nosavefields := requ.force( 'nosavefields' + IntToStr(i)).AsString; //不要提交的字段 例:字段一,字段二 dm.qry.Close; dm.qry.sql.Clear; dm.qry.sql.Add( 'select * from ' + tablename + ' where 1=2' ); delta := requ.force( 'delta' + IntToStr(i)).AsVariant; dm.dsp.ApplyUpdates(delta, 0, err); if err > 0 then begin dm.conn.Rollback; // 回滚事务 resp.force( 'return' ).AsBoolean := False; resp.saveToRawByteString; Exit; //有一个表提交失败,就要中止 end; end; dm.conn.Commit; // 提交事务 dm.dsp.DataSet := nil; resp.force( 'return' ).AsBoolean := True; resp.saveToRawByteString; except on e: Exception do begin resp.force( 'return' ).AsBoolean := False; resp.force( 'err' ).AsString := e.Message; resp.saveToRawByteString; Log.WriteLog( 'dbfunc.save ' + e.Message); end; end; finally requ.Free; resp.Free; pool.Unlock(dm); end; end; function execsql( const params : RawByteString): RawByteString; var requ: TSerial; pool: tunidacpool; dm: Tunidac; accountno, sql: string ; begin pool := nil; dm := nil; requ := TSerial.Create; try try requ.loadFromRawByteString( params ); accountno := requ.force( 'accountno' ).AsString; sql := requ.force( 'sql' ).AsString; pool := GetDBPool(accountno); dm := pool.Lock; if not dm.conn.InTransaction then dm.conn.StartTransaction; //开启事务 dm.qry.Close; dm.qry.sql.Clear; dm.qry.sql.Add(sql); dm.qry.Execute; dm.conn.Commit; //提交事务 requ.clear; requ.force( 'return' ).AsBoolean := True; requ.saveToRawByteString; except on e: Exception do begin dm.conn.Rollback; //回滚事务 requ.clear; requ.force( 'return' ).AsBoolean := False; requ.saveToRawByteString; Log.WriteLog( 'dbfunc.execsql ' + e.Message); end; end; finally requ.Free; pool.Unlock(dm); end; end; function execsp( const params : RawByteString): RawByteString; var requ: TSerial; pool: tunidacpool; dm: Tunidac; accountno, spname, lparams: string ; list: tstringlist; i: Integer; begin pool := nil; dm := nil; requ := TSerial.Create; list := TStringList.Create; try try requ.loadFromRawByteString( params ); accountno := requ.force( 'accountno' ).AsString; spname := requ.force( 'spname' ).AsString; lparams := requ.force( 'params' ).AsString; pool := GetDBPool(accountno); dm := pool.Lock; dm.dsp.DataSet := dm.sp; dm.sp.Close; dm.sp.Params.Clear; dm.sp.StoredProcName := spname; dm.sp.Prepare; if lparams > '' then // 不是所有的存储过程都有参数 begin list.Delimiter := ';' ; list.DelimitedText := params ; for i := 0 to list.count - 1 do dm.sp.FindParam(list.Names[i]).AsString := list.Values[list.Names[i]]; end; dm.dsp.DataSet := nil; requ.clear; requ.force( 'return' ).AsBoolean := true ; requ.force( 'dataset' ).AsVariant := dm.dsp.Data; requ.saveToRawByteString; except on e: Exception do begin requ.force( 'return' ).AsBoolean := False; requ.force( 'err' ).AsString := e.Message; requ.saveToRawByteString; Log.WriteLog( 'dbfunc.execsp ' + e.Message); end; end; finally requ.Free; list.Free; pool.Unlock(dm); end; end; end. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/13750114.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-09-29 雪花ID实现新增数据同步
2018-09-29 delphi win64 DEBUG不能进预设断点的问题
2017-09-29 NETSH.EXE操作SSL
2017-09-29 OPENSSL生成SSL自签证书