admin
2025-01-22 7e31442f0e9b07764e9c6a9680d3d4aeba5fe1de
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
Quintiq file version 2.0
#parent: #root
Method GetOrCreate (
  String table,
  String keyNameTuple,
  String nonKeyNameTuple,
  String keyValueTuple,
  String nonKeyValueTuple
) as Number
{
  Description: 'Maintain by R&D'
  TextBody:
  [*
    // jasperb Feb-21-2012 (created)
    // danr June-6-2012 (changed for WriteToFile in particular)
    
    /*
    Input arguments:
     * table: e.g. benchmark or setup
     * nametuple: comma-seperated column titles
     * keyValueTuple: comma-separated values, these *are* used for matching (avoiding duplicates)
     * nonkeyValueTuple: : comma-separated values, not used for matching
    
    If keyValueTuple = "" then unique entries will always be created
    
    Writing to file makes CSV's (using Benchmark::Delimiter (currently '$') as separator), one for each table.
    First column of "table" is always the id.
    Following columns are always the key columns, after that the non-key ones.
    */
    
    result := -1;
    
    nameTuple := '';
    valueTuple := '';
    
    if( keyValueTuple <> "" and nonKeyValueTuple <> "" )
    {
      nameTuple := keyNameTuple + "," + nonKeyNameTuple;
      valueTuple := keyValueTuple + "," + nonKeyValueTuple;
    }
    else
    {
      nameTuple := keyNameTuple + nonKeyNameTuple;
      valueTuple := keyValueTuple + nonKeyValueTuple
    }
    
    if( this.WriteToFile() and this.WriteToDB() )
    {
      error( 'both WriteToFile() = true and WriteToDB() = true; cant deal with that' );
    }
    
    if( this.WriteToFile() )
    {
      OS::CreateDirectory( this.Directory() );
    
      filename := this.Directory() + '/' + table + '.csv'
    
      file := OSFile::Construct();
      file.Open( filename, 'ReadWrite', true );
    
      searchFor :=  BenchmarkUtilities::FormatCSV( keyValueTuple );
      matchID := 0; maxID := 0; count := 0;
      lineID := 0; lineRest := '';
    
      while( not file.IsEOF() and matchID = 0 )
      {
        line := file.ReadTextLine();
        count++;
    
        if( count > 1 )
        {
          // We skip the header line, if it exists
          BenchmarkUtilities::SplitCSVLine( line, lineID, lineRest );
          // Keep track of max. ID that occurs
          maxID := maxvalue( lineID, maxID );
          // See if this is the line we're looking for
          if( keyValueTuple <> '' and lineRest.FindString(searchFor, 0 ) = 0 )
          {
            matchID := lineID;
          }
        }
      }
    
    
      if( count = 0 )
      {
        // file is empty
        file.WriteTextLine( BenchmarkUtilities::FormatCSV( table + 'id,' + nameTuple ) );
      }
    
      result := -1;
      if( matchID = 0 )
      {
        // No match, write new one
        newID := maxID + 1;
        file.WriteTextLine( BenchmarkUtilities::FormatCSV( [String] newID + ',' + valueTuple ) );
        result := newID;
      }
      else
      {
        // match
        result := matchID;
      }
      file.Close();
    }
    
    if( this.WriteToDB() )
    {
      idname := table + "id";
    
      selectquery := "SELECT " + idname + " FROM " + table + " WHERE (" + keyNameTuple + ")=(" + keyValueTuple + ") ORDER BY " + idname;
      queryresult := this.FlatQuery( selectquery );
    
      if( queryresult.NrRows() = 0 )
      {
        insertquery := "INSERT INTO " + table + " (" + nameTuple + ") VALUES (" + valueTuple + ")";
        queryresult := this.FlatQuery( insertquery );
    
        infoquery := "SELECT LAST_INSERT_ID()";
        queryresult := this.FlatQuery( infoquery );
    
        if( queryresult.NrRows() = 0 )
        {
          error('SELECT LAST_INSERT_ID() returns nothing');
        }
      }
      else if( queryresult.NrRows() > 1 )
      {
        info('WHOA! More than one matching result found while updating table ' + table);
      }
    
      result := queryresult.Row( 0 ).Field( 0 ).AsNumber();
    }
    
    if( not this.WriteToFile() and not this.WriteToDB() )
    {
      result := 0;
    }
    
    return result;
  *]
}