<%@//Script头、过程和函数定义 program procode; %> <%!//声明变量 function GetDataType(F: string): string; begin Result := GetDeliBack(F, '='); Result := GetDeliPri(Result, '@0#'); end; function GetAllow_NotRep(F: string): boolean; begin F := GetDeliBack(F, '@2#'); F := GetDeliPri(F, '@3#'); Result := '1' = F; end; function GetCField(F: string): string; begin Result := GetDeliBack(F, '@0#'); Result := GetDeliPri(Result, '@1#'); end; function IsToMd5(F: string): boolean; begin F := GetDeliBack(F, '@6#'); F := GetDeliPri(F, '@7#'); Result := '1' = F; end; function IsToEncrypt(F: string): boolean; begin F := GetDeliBack(F, '@9#'); F := GetDeliPri(F, '@10#'); Result := '1' = F; end; function GetCreateType(F: string): string; begin F := GetDeliBack(F, '@7#'); F := GetDeliPri(F, '@8#'); Result := F; end; function GetMustCreate(F: string): boolean; begin F := GetDeliBack(F, '@8#'); F := GetDeliPri(F, '@9#'); Result := trim(F)='1'; end; function GetRField(F: string): string; begin Result := GetDeliPri(F, '='); Result := GetDeliBack(Result, '*'); end; function IsIntFloat(DataType: string): boolean; begin DataType := GetDataType(DataType); Result := (lowercase(datatype) = 'ftinteger') or (lowercase(datatype) = 'ftcurrency') or (lowercase(datatype) = 'ftfmtbcd') or (lowercase(datatype) = 'ftsmallint') or (lowercase(datatype) = 'ftshortint') or (lowercase(datatype) = 'ftlargeint') or (lowercase(datatype) = 'ftbcd') or (lowercase(datatype) = 'ftfloat'); end; function IsDatetime(DataType: string): boolean; begin DataType := GetDataType(DataType); Result := (LowerCase(DataType) = 'ftDate') or (LowerCase(DataType) = 'ftTime') or (LowerCase(DataType) = 'ftDateTime'); end; function IsNumfromSL(SLType: TStringlist; FieldName: string): boolean; var lp: integer; IsNum: boolean; F: string; begin isNum := false; for lp := 0 to SLType.Count - 1 do begin F := LowerCase(trim(GetRField(SLType[lp]))); if(F=LowerCase(FieldName)) then begin isNum := IsIntFloat(SLType[lp]); break; end; end; Result := IsNum; end; function GetValue(ds: THjhMemoryDataSet; Conn: TminiConnection; F, DataType: string;IsInsert: boolean;IsUpdate: boolean;FieldName: string; onlyset: boolean): string; var At: string; begin if ds = nil then Result := Request.V(F) else Result := ds.V(F); At := GetCreateType(DataType); if (trim(At) > '0') and ((trim(Result) = '') or GetMustCreate(DataType)) then begin if At = '1' then Result := GetClientUniqueCode else if At = '2' then Result := formatdatetime('yyyy-mm-dd', Now) else if At = '3' then Result := formatdatetime('hh:nn:ss', Now) else if At = '4' then Result := formatdatetime('yyyy-mm-dd hh:nn:ss', Now) else if At = '5' then Result := formatdatetime('yyyy-mm-dd hh:nn:ss.zzz', Now) else if At = '6' then Result := GetSnowID() else if At = '7' then Result := Request.Session.Values['Loginedusername'] else if At = '8' then begin Result := Request.Session.Values['Loginedusername']; if Conn <> nil then if IsUpdate then begin if (LowerCase(Conn.DbType) = 'mssql') or (LowerCase(Conn.DbType) = 'db2') then Result := 'isnull(' + FieldName + ','''')' + '+' + QuotedStr(',') + '+' + QuotedStr(Request.Session.Values['Loginedusername']) else if (LowerCase(Conn.DbType) = 'mysql') or (LowerCase(Conn.DbType) = 'sqlite') then Result := 'concat(IFNULL(' + FieldName + ','''')' + ',concat(' + QuotedStr(',')+',' + QuotedStr(Request.Session.Values['Loginedusername']) + '))' else Result := FieldName + '+' + QuotedStr(',') + '+' + QuotedStr(Request.Session.Values['Loginedusername']); exit; end; end else if At = '9' then begin Result := '1'; if Conn <> nil then if IsUpdate then begin if (LowerCase(Conn.DbType) = 'mssql') or (LowerCase(Conn.DbType) = 'db2') then Result := 'isnull(' + FieldName + ',0)' + '+1' else if (LowerCase(Conn.DbType) = 'mysql') or (LowerCase(Conn.DbType) = 'sqlite') then Result := 'IFNULL(' + FieldName + ',0)' + '+1' else Result := FieldName + '+1'; exit; end; end; end; if IsIntFloat(DataType) then begin if onlyset then exit; if Result = '' then Result := 'null' else StrToFloat(Result); end else if IsDatetime(DataType) then begin if onlyset then exit; if Result = '' then Result := 'null' else if Conn <> nil then begin if (LowerCase(Conn.DbType) = 'mssql') or (LowerCase(Conn.DbType) = 'db2') then Result := 'convert(datetime,' + QuotedStr(Result) + ', 20)' else begin if length(Result) < 15 then Result := 'str_to_date(' + QuotedStr(Result) + ',''%Y-%m-%d'')' else Result := 'str_to_date(' + QuotedStr(Result) + ',''%Y-%m-%d %H:%i:%s'')'; end; end; end else begin if (IsToMd5(DataType)) and (length(Result) <> 32) then Result := GetMd5(Result, false); if (IsToEncrypt(DataType)) then Result := SetPWD(Result, '#'); if onlyset then exit; if Conn <> nil then if (LowerCase(Conn.DbType) = 'mysql') or (LowerCase(Conn.DbType) = 'sqlite') then Result := StringReplace(Result, '\', '\\', [rfReplaceAll]); Result := QuotedStr(Result); if (LowerCase(Conn.DbType) = 'mssql') and (IsInsert or IsUpdate) then Result := 'N' + Result; end; end; function GetWhere(ds: THjhMemoryDataSet; Conn: TminiConnection; Sys_DB_Fields: string; SL: TStringlist; IsRepCall: boolean): string; var lp, I: integer; SLTmp, SL2: TStringlist; Dt, Value, One: string; begin SLTmp := TStringlist.Create; SL2 := TStringlist.Create; try SLTmp.CommaText := Sys_DB_Fields; SL2.CommaText := SL.Values['KEYWORD']; Result := ' where '; for lp := 0 to SL2.Count - 1 do begin Dt := ''; for I := 0 to SLTmp.Count - 1 do begin if SL2[lp] = GetRField(SLTmp[I]) then begin Dt := SLTmp[I]; break; end; end; Value := GetValue(ds, Conn, SL2[lp], Dt, false, false, '', false); if (Value = 'null') and (IsIntFloat(dt) or IsDatetime(dt)) then begin if IsRepCall then One := SL2[lp] + ' is not null ' else One := SL2[lp] + ' is null '; end else begin if IsRepCall then One := SL2[lp] + '<>' + Value else One := SL2[lp] + '=' + Value; end; if trim(Result) = 'where' then Result := Result + One else Result := Result + ' and ' + One; end; finally SL2.Free; SLTmp.Free; end; end; function CheckRep(IsMain: boolean; ds: THjhMemoryDataSet; Conn: TminiConnection; Query: TminiQuery; SL, SL2: TStringlist;MyName: string): boolean; var lp: integer; TmpStr, TmpStr2, TmpStr3, fnew_data, Sys_DB_SQL, ErrStr, CF, V: string; Flag, F2: boolean; begin Result := false; TmpStr := ''; ErrStr := ''; Flag := false; F2 := true; //检查重复 TmpStr2 := GetWhere(ds, Conn, SL2.CommaText, SL, true); ///result := true; print(TmpStr2); exit ; for lp := 0 to SL2.Count - 1 do begin if not GetAllow_NotRep(SL2[lp]) then Continue; CF := GetCField(SL2[lp]); TmpStr3 := GetRField(SL2[lp]); V := GetValue(ds, Conn, TmpStr3, SL2[lp],false, false, '', false); if (ds <> nil) then fnew_data := ds.V('fnew_data') else fnew_data := Request.V('fnew_data'); if trim(fnew_data) = 'true' then TmpStr3 := ' where ' + TmpStr3 + '=' + V else TmpStr3 := TmpStr2 + ' and ' + TmpStr3 + '=' + V; Sys_DB_SQL := ' select count(*) as cc from ' + SL.Values['WRITE_TABLE'] + ' ' + TmpStr3; if not Query.O(Sys_DB_SQL, ErrStr) then begin Print(MyName+'[%@clear@%]'); print('检查[' + CF + ']重复时出错:' + ErrStr + Sys_DB_SQL + ''); Print(''); exit; end; if Query.I('cc') > 0 then begin F2 := false; if not Flag then begin Flag := true; Print('[%@clear@%]'); end; if (ds <> nil)and(not IsMain) then print(MyName+'第[' + IntToStr(ds.CurRecordPos + 1) + ']行[' + CF + ']值[' + AnsiDequotedStr(V, '''') + ']重复,不能保存!
') else print(MyName+'[' + CF + ']值[' + AnsiDequotedStr(V, '''') + ']重复,不能保存!
'); if ds = nil then Print(MyName+''); end; end; if not F2 then exit; Result := true; end; function UpdateData(IsMain: boolean; ds: THjhMemoryDataSet; var NeedExe: string; Conn: TminiConnection; Query: TminiQuery; SL, SL2: TStringlist): boolean; var lp: integer; fnew_data, TmpStr, TmpStr2, TmpStr3, Sys_DB_SQL, ErrStr: string; begin Result := false; NeedExe := ''; //检查重复 if ds = nil then if not CheckRep(true, ds, Conn, Query, SL, SL2,'') then exit; if ds <> nil then fnew_data := ds.V('fnew_data') else fnew_data := Request.V('fnew_data'); if (IsMain) and (trim(fnew_data) = 'true') then begin Print('[%@clear@%]'); print('当前是新增状态,但已存在该记录,不能保存!'); exit; end; TmpStr := ''; for lp := 0 to SL2.Count - 1 do begin if (trim(SL2[lp]) = '') or (Pos('*', GetDeliPri(SL2[lp], '=')) > 0) then Continue; TmpStr3 := GetRField(SL2[lp]); TmpStr3 := TmpStr3 + '=' + GetValue(ds, Conn, TmpStr3, SL2[lp], false, true, TmpStr3, false); if TmpStr = '' then TmpStr := TmpStr3 else TmpStr := TmpStr + ',' + TmpStr3; end; TmpStr2 := GetWhere(ds, Conn, SL2.CommaText, SL, false); Sys_DB_SQL := ' update ' + SL.Values['WRITE_TABLE'] + ' Set ' + TmpStr + TmpStr2; if not Query.E(Sys_DB_SQL, ErrStr) then begin NeedExe := '1'; Print('[%@clear@%]'); print('保存数据出错:' + ErrStr + Sys_DB_SQL + ''); Print('[%@clear@%]'); print('保存数据出错:' + ErrStr + '' + Sys_DB_SQL); Print(''); exit; end; Result := true; end; function InsertData(IsMain: boolean; ds: THjhMemoryDataSet; var NeedExe: string;Conn: TminiConnection; Query: TminiQuery; SL, SL2: TStringlist): boolean; var lp: integer; fnew_data, Fields, OneValue, Values, Sys_DB_SQL, ErrStr, One: string; begin Result := false; NeedExe := ''; //检查重复 if ds = nil then if not CheckRep(true, ds, Conn, Query, SL, SL2,'') then exit; if ds <> nil then fnew_data := ds.V('fnew_data') else fnew_data := Request.V('fnew_data'); if (IsMain) and (trim(fnew_data) <> 'true') then begin Print('[%@clear@%]'); print('当前不是新增状态,但找不到该记录,不能保存!请先找出来再修改!'); exit; end; Fields := ''; Values := ''; for lp := 0 to SL2.Count - 1 do begin if (trim(SL2[lp]) = '') or (Pos('*', trim(SL2[lp])) > 0) then Continue; One := GetRField(SL2[lp]); OneValue := GetValue(ds, Conn, One, SL2[lp], true, false, One, false); if Fields = '' then begin Fields := One; Values := OneValue; end else begin Fields := Fields + ',' + One; Values := Values + ',' + OneValue; end; end; Sys_DB_SQL := ' insert into ' + SL.Values['WRITE_TABLE'] + '(' + Fields + ') values(' + Values + ') '; if not Query.E(Sys_DB_SQL, ErrStr) then begin NeedExe := '1'; Print('[%@clear@%]'); print('插入数据出错:' + ErrStr + Sys_DB_SQL + ''); Print('[%@clear@%]'); print('增加数据出错:' + ErrStr + '' + Sys_DB_SQL); Print(''); exit; end; Result := true; end; function SaveGridData_DeleteNo(AllDs, mds, mds2: THjhMemoryDataSet; IsMain: boolean; SL, SL2: TStringlist; Name, Fields, Data: string; Conn: TminiConnection; Query: TminiQuery): boolean; var SLTmp, SLtmp2: TStringlist; mx,main,v, WhereSQL, One, TmpStr, F: string; lp, Index, I: integer; begin Result := false; //print('nded ismx ' + mds.V('mxmainkeys') + '---' + SL.Values['KEYWORD']); WhereSQL := ''; SLTmp := TStringlist.Create; SLtmp2 := TStringlist.Create; try TmpStr := mds.V('mxmainkeys'); TmpStr:=StringReplace(TmpStr,';',#13#10, [rfReplaceAll]); TmpStr:=StringReplace(TmpStr,';',#13#10, [rfReplaceAll]); TmpStr:=StringReplace(TmpStr,',',#13#10, [rfReplaceAll]); TmpStr:=StringReplace(TmpStr,',',#13#10, [rfReplaceAll]); SLTmp.Text := TmpStr; for lp := 0 to SLTmp.Count - 1 do begin mx := GetDeliPri(SLTmp[lp], '='); main := GetDeliBack(SLTmp[lp], '='); v := mds2.V(main); if IsNumfromSL(SL2, mx) then begin if trim(v) = '' then One := mx + ' is null ' else One := mx + '=' + v end else One := mx + '=' + QuotedStr(v); if WhereSQL = '' then WhereSQL := One else WhereSQL := WhereSQL + ' and ' + One; end; //} //读取旧数据 WhereSqL := ' SELECT ' + SL.Values['KEYWORD'] + ' FROM ' + SL.Values['WRITE_TABLE'] + ' WHERE ' + WhereSqL; if not Query.O(WhereSqL, TmpStr) then begin Print('[%@clear@%]'); print('读取数据出错:' + TmpStr + ''); exit; end; TmpStr := SL.Values['KEYWORD']; TmpStr:=StringReplace(TmpStr,';',#13#10, [rfReplaceAll]); TmpStr:=StringReplace(TmpStr,';',#13#10, [rfReplaceAll]); TmpStr:=StringReplace(TmpStr,',',#13#10, [rfReplaceAll]); TmpStr:=StringReplace(TmpStr,',',#13#10, [rfReplaceAll]); SLTmp.Text := TmpStr; SLtmp2.Clear; Query.First; while not Query.Eof do begin //{ TmpStr := ''; for lp := 0 to SlTmp.Count - 1 do begin if TmpStr = '' then TmpStr := Query.V(SLTmp[lp]) else TmpStr := TmpStr + '[@#]' + Query.V(SLTmp[lp]); end; SLTmp2.Add(TmpStr); //} Query.Next; end; //找出待删除的记录 SLTmp2.Sorted := true; AllDs.First; while not AllDs.Eof do begin TmpStr := ''; for lp := 0 to SlTmp.Count - 1 do begin if TmpStr = '' then TmpStr := AllDs.V(SLTmp[lp]) else TmpStr := TmpStr + '[@#]' + AllDs.V(SLTmp[lp]); end; Index := SLTmp2.IndexOf(TmpStr); if Index > -1 then SLTmp2.Delete(Index); AllDs.Next; end; //开始删除记录 for I := 0 to SLTmp2.Count - 1 do begin WhereSQL := ''; TmpStr := SLTmp2[I]; for lp := 0 to SlTmp.Count - 1 do begin F := SLTmp[lp]; v := GetDeliPri(TmpStr, '[@#]'); TmpStr := GetDeliBack(TmpStr, '[@#]'); if IsNumfromSL(SL2, F) then begin if trim(v) = '' then One := F + ' is null ' else One := F + '=' + v end else One := F + '=' + QuotedStr(v); if WhereSQL = '' then WhereSQL := One else WhereSQL := WhereSQL + ' and ' + One; end; WhereSqL := ' DELETE FROM ' + SL.Values['WRITE_TABLE'] + ' WHERE ' + WhereSqL; if not Query.E(WhereSqL, TmpStr) then begin Print('[%@clear@%]'); print('保存数据出错:' + TmpStr + WhereSqL + ''); exit; end; end; finally SLtmp2.Free; SLTmp.Free; end; Result := true; end; function ProMemoryData(ds: THjhMemoryDataSet; SL, SL2: TStringlist): boolean; var lp: integer; At, Value, One, OneValue, DataType: string; Flag: boolean; begin ds.First; while not ds.eof do begin for lp := 0 to SL2.Count - 1 do begin if (trim(SL2[lp]) = '') or (Pos('*', trim(SL2[lp])) > 0) then Continue; Flag := false; One := GetRField(SL2[lp]); OneValue := ds.V(One); DataType := SL2[lp]; At := GetCreateType(DataType); //if trim(At) > '0' then if (trim(At) > '0') and ((trim(OneValue) = '') or GetMustCreate(DataType)) then Flag := true; if IsIntFloat(DataType) then begin end else if IsDatetime(DataType) then begin end else begin if (IsToMd5(DataType)) and (length(OneValue) > 0) then Flag := true; if (IsToEncrypt(DataType)) then Flag := true; end; if Flag then begin OneValue := GetValue(ds, nil, One, SL2[lp], true, false, One, true); ds.S(One, OneValue); end; end; ds.Next; end; end; function SaveGridData(mds, mds2: THjhMemoryDataSet; IsMain: boolean; SL, SL2: TStringlist; Name, Fields, Data: string; Conn: TminiConnection; Query: TminiQuery): boolean; var lp: integer; TmpStr, TmpStr2, TmpStr3, Sys_DB_SQL, ErrStr: string; AllDs: THjhMemoryDataSet; begin Result := false; AllDs := THjhMemoryDataSet.Create(nil); try AllDs.CommaText := Data; AllDs.First; while not AllDs.Eof do begin if not CheckRep(IsMain, AllDs, Conn, Query, SL, SL2, Name) then exit; TmpStr2 := GetWhere(AllDs, Conn, Fields, SL, false); Sys_DB_SQL := 'select count(*) as cc from ' + SL.Values['WRITE_TABLE'] + TmpStr2; if not Query.O(Sys_DB_SQL, ErrStr) then begin Print('[%@clear@%]'); print('读取数据出错:' + ErrStr + ''); exit; end; if Query.I('cc') > 0 then begin //更新 SL.Values['WRITE_TABLE'] if not UpdateData(IsMain, AllDs, TmpStr3, Conn, Query, SL, SL2) then begin exit; end; end else begin //插入 if not InsertData(IsMain, AllDs, TmpStr3, Conn, Query, SL, SL2) then begin exit; end; end; AllDs.Next; end; if (mds.RecordCount>1) and (mds.V('ismx') = '1') and (mds.V('mxmainkeys') <> '') then begin if not SaveGridData_DeleteNo(AllDs, mds, mds2, IsMain, SL, SL2, Name, Fields, Data, Conn, Query) then exit; end; finally AllDs.Free; end; Result := true; end; var Sys_DB_Info, Sys_DB_Source, Sys_DB_SQL: string; Query: TminiQuery; ErrStr, TmpStr, TmpStr2, TmpStr3, TmpStr4, TmpStr5: string; Pooler: TminiPooler; lp, i, Sys_DB_RecordCount, Sys_DB_PageCount: integer; SL, SL2, SL3: TStringlist; mds, mds2: THjhMemoryDataSet; Sys_DB_yyyyy12: string; Sys_DB_Fields_yyyyy12: string; Sys_DB_Form_ID: string; flag: boolean; %> <% if trim(Request.V('savetype')) = '1' then begin SL := TStringlist.Create; //WRITE_TABLE SL2 := TStringlist.Create; mds := THjhMemoryDataSet.Create(nil); mds2 := THjhMemoryDataSet.Create(nil); try Pooler := nil; mds.CommaText := trim(Request.PostedData); mds.First; TmpStr4 := ''; while not mds.Eof do begin TmpStr := mds.V('CName'); if trim(TmpStr) = '' then TmpStr := mds.V('Name'); TmpStr3 := mds.V('db_form_info'); TmpStr3 := GetPWD(trim(TmpStr3), '@*@'); SL.CommaText := TmpStr3; Sys_DB_Source := SL.Values['SYS_DB_SOURCE']; if trim(TmpStr4) = '' then TmpStr4 := Sys_DB_Source; if ('1'='1') and (Pooler <> nil) then if Pooler.Conn.InTransaction and (TmpStr4 <> Sys_DB_Source) then begin if (LowerCase(Pooler.Conn.DbType) = 'mysql') and ('1'='1') then if not Pooler.Conn.StartTrans(ErrStr) then begin print(ErrStr); exit; end; if not Pooler.Conn.Commit(ErrStr) then begin print(ErrStr); exit; end; Pooler.HadUsed := false; //注意: 一定要有这行,把实例还回连接池 end; SL2.Sorted := true; SL2.CommaText := mds.V('Fields'); if (Pooler = nil) or (TmpStr4 <> Sys_DB_Source) then if not Pub.O(Sys_DB_Source, Pooler, Query, ErrStr) then begin Print('[%@clear@%]'); print('数据库连接出错:' + ErrStr + ''); Print(''); exit; end; if Pooler.DBType = 'memorytable' then begin mds2 := Pub.Getds; mds2.CommaText := mds.V('Data'); ProMemoryData(mds2, SL, SL2); DBMemory_MxSave(mds2, Pooler.DBName, SL2.CommaText); end else begin if not Pooler.Conn.Connected then if not Pooler.Conn.RepOpen(ErrStr) then exit; if ('1'='1') and (not Pooler.Conn.InTransaction) then if not Pooler.Conn.StartTrans(ErrStr) then begin print(ErrStr); exit; end; try if mds.V('AType') = '1' then mds2.CommaText := mds.V('Data'); if not SaveGridData(mds, mds2, mds.V('AType') = '1', SL, SL2, TmpStr, mds.V('Fields'), mds.V('Data'), Pooler.Conn, Query) then begin if ('1'='1') then if not Pooler.Conn.Rollback(ErrStr) then begin print(ErrStr); exit; end; exit; end; finally if ('1'='1') and (TmpStr4 <> Sys_DB_Source) or ('1'<>'1') then Pooler.HadUsed := false; //注意: 一定要有这行,把实例还回连接池 end; TmpStr4 := Sys_DB_Source; end; mds.Next; end; Print(''); if Pooler.Conn <> nil then if (LowerCase(Pooler.Conn.DbType) = 'mysql') and ('1'='1') then Pooler.Conn.StartTrans(ErrStr); if '1'='1' then if not Pooler.Conn.Commit(ErrStr) then begin print(ErrStr); exit; end; finally if Pooler <> nil then Pooler.HadUsed := false; //注意: 一定要有这行,把实例还回连接池 mds.Free; SL2.Free; SL.Free; end; end; %>