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: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331:
| unit uMain;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ZConnection, Grids, DBGrids, Menus, DB, ZAbstractRODataset, ZAbstractDataset, ZDataset, ExtCtrls, Buttons, IniFiles, ShlObj, ActiveX;
type TMain = class(TForm) ZConnection1: TZConnection; ZQuery1: TZQuery; DataSource1: TDataSource; DBGrid1: TDBGrid; PopupMenu1: TPopupMenu; ErworbeneProdukteanzeigen1: TMenuItem; N1: TMenuItem; Image1: TImage; SpeedButton1: TSpeedButton; StatusBar1: TStatusBar; SpeedButton2: TSpeedButton; PopupMenu2: TPopupMenu; PopupCustomers: TMenuItem; PopupProducts: TMenuItem; N2: TMenuItem; PopupRelations: TMenuItem; SpeedButton3: TSpeedButton; SpeedButton4: TSpeedButton; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure DBGrid1TitleClick(Column: TColumn); procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton2Click(Sender: TObject); procedure PopupCustomersClick(Sender: TObject); procedure PopupProductsClick(Sender: TObject); procedure PopupRelationsClick(Sender: TObject); procedure SpeedButton4Click(Sender: TObject); private activeTable: String; SrcDir: String; DesktopDir: String; ColumnWidths : record Customers : Array[0..7] of Integer; Products : Array[0..2] of Integer; Relations : Array[0..1] of Integer; end; procedure adjustColumns; procedure saveColumns; procedure loadConfig(filename: String); procedure saveConfig(filename: String); function setActiveTable(tableName: String): Integer; public end;
var Main: TMain; sortOrder: Boolean; sortColumn: String;
implementation
{$R *.dfm}
function GetShellFolder(CSIDL: Integer): String; var pidl : PItemIdList; FolderPath : String; SystemFolder : Integer; Malloc : IMalloc; begin Malloc := nil; FolderPath := ''; SHGetMalloc(Malloc); if Malloc = nil then begin Result := FolderPath; Exit; end; try SystemFolder := CSIDL; if SUCCEEDED(SHGetSpecialFolderLocation(0, SystemFolder, pidl)) then begin SetLength(FolderPath, max_path); if SHGetPathFromIDList(pidl, PChar(FolderPath)) then begin SetLength(FolderPath, length(PChar(FolderPath))); end; end; Result := FolderPath; finally Malloc.Free(pidl); end; end;
function TMain.setActiveTable(tableName: String): Integer; begin activeTable := tableName; if tableName = 'customer' then begin PopupCustomers.Checked := True; PopupProducts.Checked := False; PopupRelations.Checked := False; Speedbutton1.Caption := 'Kunden hinzufügen'; Speedbutton2.Caption := 'Kunden entfernen'; StatusBar1.Panels[0].Text := 'Aktive Tabelle: Kunden'; end; if tableName = 'product' then begin PopupCustomers.Checked := False; PopupProducts.Checked := True; PopupRelations.Checked := False; Speedbutton1.Caption := 'Produkt hinzufügen'; Speedbutton2.Caption := 'Produkt entfernen'; StatusBar1.Panels[0].Text := 'Aktive Tabelle: Produkte'; end; if tableName = 'rel' then begin PopupCustomers.Checked := False; PopupProducts.Checked := False; PopupRelations.Checked := True; Speedbutton1.Caption := 'Relation hinzufügen'; Speedbutton2.Caption := 'Relation entfernen'; StatusBar1.Panels[0].Text := 'Aktive Tabelle: Relation'; end; result := 0; end;
procedure TMain.adjustColumns; var i: Integer; begin if activeTable = 'customer' then for i := 0 to DBGrid1.Columns.Count-1 do DBGrid1.Columns[i].Width := ColumnWidths.Customers[i]; if activeTable = 'products' then for i := 0 to DBGrid1.Columns.Count-1 do DBGrid1.Columns[i].Width := ColumnWidths.Products[i]; if activeTable = 'rel' then for i := 0 to DBGrid1.Columns.Count-1 do DBGrid1.Columns[i].Width := ColumnWidths.Relations[i]; end;
procedure TMain.saveColumns; var i: Integer; begin if activeTable = 'customer' then for i := 0 to DBGrid1.Columns.Count-1 do ColumnWidths.Customers[i] := DBGrid1.Columns[i].Width; if activeTable = 'products' then for i := 0 to DBGrid1.Columns.Count-1 do ColumnWidths.Products[i] := DBGrid1.Columns[i].Width; if activeTable = 'rel' then for i := 0 to DBGrid1.Columns.Count-1 do ColumnWidths.Relations[i] := DBGrid1.Columns[i].Width; end;
procedure TMain.loadConfig(filename: String); var i: Integer; ini: TIniFile; begin ini := TIniFile.Create(filename);
if activeTable = 'customer' then begin for i := 0 to DBGrid1.Columns.Count-1 do DBGrid1.Columns[i].Width := ini.ReadInteger('ColsCustomer',IntToStr(i),50); end; if activeTable = 'products' then begin for i := 0 to DBGrid1.Columns.Count-1 do ColumnWidths.Products[i] := ini.ReadInteger('ColsProduct',IntToStr(i),50); end; if activeTable = 'rel' then begin for i := 0 to DBGrid1.Columns.Count-1 do ColumnWidths.Relations[i] := ini.ReadInteger('ColsRel',IntToStr(i),50); end; adjustColumns;
Main.Left := ini.ReadInteger('Main','Left',0); Main.Top := ini.ReadInteger('Main','Top',0); Main.ClientWidth := ini.ReadInteger('Main','Width',800); Main.ClientHeight := ini.ReadInteger('Main','Height',600);
ini.Free; end;
procedure TMain.saveConfig(filename: String); var i: Integer; ini: TIniFile; begin ini := TIniFile.Create(filename);
for i := 0 to Length(ColumnWidths.Customers) - 1 do ini.WriteInteger('ColsCustomers',IntToStr(i),ColumnWidths.Customers[i]); for i := 0 to Length(ColumnWidths.Products) - 1 do ini.WriteInteger('ColsProducts',IntToStr(i),ColumnWidths.Products[i]); for i := 0 to Length(ColumnWidths.Relations) - 1 do ini.WriteInteger('ColsRelations',IntToStr(i),ColumnWidths.Relations[i]);
ini.WriteInteger('Main','Left',Main.Left); ini.WriteInteger('Main','Top',Main.Top); ini.WriteInteger('Main','Height',Main.ClientHeight); ini.WriteInteger('Main','Width',Main.ClientWidth);
ini.Free; end;
procedure TMain.DBGrid1TitleClick(Column: TColumn); var sortColumn: String; begin sortColumn := DBGrid1.Columns.Items[Column.Index].FieldName; if Column.FieldName <> sortColumn then sortColumn := Column.FieldName else sortOrder := not sortOrder;
DataSource1.DataSet.Close; if sortOrder then ZQuery1.SQL.Text := 'SELECT * FROM ' + activeTable + ' ORDER BY ' + sortColumn + ' ASC' else ZQuery1.SQL.Text := 'SELECT * FROM ' + activeTable + ' ORDER BY ' + sortColumn + ' DESC';
ZQuery1.Open; adjustColumns; end;
procedure TMain.FormClose(Sender: TObject; var Action: TCloseAction); begin ZConnection1.Connected := False; saveConfig(SrcDir+'Config.ini'); end;
procedure TMain.FormCreate(Sender: TObject); begin SrcDir := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName)); DesktopDir := IncludeTrailingPathDelimiter(GetShellFolder(CSIDL_DESKTOP));
setActiveTable('customer'); ZConnection1.Connected := True; ZQuery1.SQL.Text := 'SELECT * FROM customer ORDER BY ID DESC;'; ZQuery1.Open;
loadConfig(SrcDir+'Config.ini'); adjustColumns; end;
procedure TMain.PopupCustomersClick(Sender: TObject); begin setActiveTable('customer'); ZQuery1.SQL.Clear; ZQuery1.SQL.Text := 'SELECT * FROM customer ORDER BY ID DESC;'; ZQuery1.Open; adjustColumns; end;
procedure TMain.PopupRelationsClick(Sender: TObject); begin setActiveTable('rel'); ZQuery1.SQL.Clear; ZQuery1.SQL.Text := 'SELECT * FROM rel;'; ZQuery1.Open; adjustColumns; end;
procedure TMain.PopupProductsClick(Sender: TObject); begin setActiveTable('product'); ZQuery1.SQL.Clear; ZQuery1.SQL.Text := 'SELECT * FROM product ORDER BY ID DESC;'; ZQuery1.Open; adjustColumns; end;
procedure TMain.SpeedButton1Click(Sender: TObject); begin if activeTable = 'customer' then ZQuery1.SQL.Text := 'INSERT INTO `customer` (`Vorname`,`Nachname`,`eMail`,`Straße`,`PLZ`,`Ort`,`Sprache`) VALUES' + '('''', '''', '''', '''', '''', '''', '''');'; if activeTable = 'product' then ZQuery1.SQL.Text := 'INSERT INTO `product` (`Name`,`Version`) VALUES' + '(''Name'', ''Version'');';
if activeTable = 'rel' then ZQuery1.SQL.Text := 'INSERT INTO `rel` (`customer_ID`,`product_ID`) VALUES' + '(''' + inputBox('Eingabe', 'Bitte customer_ID eingeben:', '') + ''',' + ' ''' + inputBox('Eingabe', 'Bitte product_ID eingeben:', '') + ''');';
ZQuery1.ExecSQL; if activeTable = 'rel' then ZQuery1.SQL.Text := 'SELECT * FROM ' + activeTable + ' ORDER BY customer_ID DESC' else ZQuery1.SQL.Text := 'SELECT * FROM ' + activeTable + ' ORDER BY ID DESC'; ZQuery1.Open; adjustColumns; end;
procedure TMain.SpeedButton2Click(Sender: TObject); begin if activeTable = 'customer' then zQuery1.SQL.Text := 'DELETE FROM customer WHERE ID=''' + zQuery1.FieldByName('ID').AsString + ''';'; if activeTable = 'product' then zQuery1.SQL.Text := 'DELETE FROM product WHERE ID=''' + zQuery1.FieldByName('ID').AsString + ''';';
if activeTable = 'rel' then zQuery1.SQL.Text := 'DELETE FROM rel WHERE (customer_ID=''' + zQuery1.FieldByName('customer_ID').AsString + ''' and product_ID=''' + zQuery1.FieldByName('product_ID').AsString + ''');'; ZQuery1.ExecSQL; if activeTable = 'rel' then ZQuery1.SQL.Text := 'SELECT * FROM ' + activeTable + ' ORDER BY customer_ID DESC' else ZQuery1.SQL.Text := 'SELECT * FROM ' + activeTable + ' ORDER BY ID DESC'; ZQuery1.Open; adjustColumns; end;
procedure TMain.SpeedButton4Click(Sender: TObject); begin Main.Close; end;
end. |