6 ms·
COBOL was the first language to take data format seriously, with "records" and a "data division". To some extent, it still is. Look at the gyrations people go t
by Animats 7y ago
COBOL was the first language to take data format seriously, with "records" and a "data division". To some extent, it still is. Look at the gyrations people go through to get Java or Python to talk to an SQL database. The programming language itself has no clue about database layout.
- ptx 7y agoPython takes care of the records pretty well with named tuples (and since it's dynamically typed, not knowing the fields until runtime is expected). It doesn't have anything for the query part through, like C# does with LINQ - are you saying that COCOL does? What does this knowledge of database layout look like in COBOL?
- skissane 7y ago> It doesn't have anything for the query part through, like C# does with LINQ - are you saying that COCOL does? What does this knowledge of database layout look like in COBOL? COBOL has built-in support for accessing flat file databases with either sequential or indexed organisation. On many mainframe and minicomputer operating systems, the filesystem provides native support for record-oriented files (with fixed or variable width records) and indexed-key files, and COBOL directly integrates with that. COBOL has syntax for defining nested record structures which was often used to define the data schema for these files. You could store the definitions in a "copybook" (conceptually equivalent to an include file in C/C++) and reuse it in many programs which would manipulate the same files. COBOL itself doesn't directly support "querying", except for the strategies of (1) manually iterating through every record in a sequential file to find matching records or (2) looking up a key-indexed file by its key field. There are commonly implemented extensions to COBOL to access relational databases (SQL), hierarchical databases (e.g. IMS), etc. However, those are not part of the core COBOL language, and comparable facilities are defined for many other languages, so COBOL isn't really unique on that point.
- ptx 7y agoThanks. It doesn't seem to me that having built-in syntax for this is necessarily a big advantage, since Python can easily and naturally work with such databases using the built-in dbm module in the standard library (and has no need for defining the schema up front): https://docs.python.org/3/library/dbm.html https://docs.python.org/3/library/dbm.html If you do need to define the database schema in code, there are many third-party libraries that allow you to do this cleanly using classes and fields, e.g. Django. Even Java can do this with Hibernate.
- skissane 7y ago> since Python can easily and naturally work with such databases using the built-in dbm module in the standard library (and has no need for defining the schema up front): https://docs.python.org/3/library/dbm.html https://docs.python.org/3/library/dbm.html Python doesn't support accessing mainframe key-indexed files. There is a port of Python to z/OS [1], but the dbm module can't read/write VSAM KSDS files, and I don't believe there exists any other Python module that can either. (Of course, someone could write a Python C extension using the VSAM C interface, or create a module which calls it using ctypes–z/OS adds some functions to stdio like flocate,fupdate,fdelrec for VSAM access–but I don't think anyone has done that so far.) Now of course, if you are porting your app to another platform, this will not be an issue, but if your data is staying on the mainframe, Python doesn't know how to handle it, COBOL does. > If you do need to define the database schema in code, there are many third-party libraries that allow you to do this cleanly using classes and fields, e.g. Django. Mainframe systems tend to use file formats based on plain text records where fields are assigned to fixed ranges of columns. Is there a Python module to do that? Especially in a declarative way, which is what COBOL provides. [1] https://www.rocketsoftware.com/product-categories/mainframe/python-for-zos https://www.rocketsoftware.com/product-categories/mainframe/...
- ptx 7y agoSorry, I think I was a bit unclear. By "such databases" I meant to refer to your "flat file databases with either sequential or indexed organisation" in general, not the specific format used on mainframes. My question was about the advantages of Cobol's built-in syntax for database access and it seems like, as you said, you could write a Python module for the mainframe database format if desired, and there would then be no particular advantage to Cobol's special syntax. Edit: The J2ME environments used on phones 15 years ago also provided a database (Record Management System) which would be difficult to access from a language other than Java, but that's unrelated to the question of whether the "programming language itself has no clue about database layout".
- jquast 7y agoSince python 3.7, dataclass is probably preferred over namedtuple, which is typed, https://realpython.com/python-data-classes/ https://realpython.com/python-data-classes/
- faizshah 7y agoDataclass is not immutable, for some cases typing.namedtuple should be preferred over dataclass if typing is needed. Deep dive here: https://stackoverflow.com/questions/51671699/data-classes-vs-typing-namedtuple-primary-use-cases https://stackoverflow.com/questions/51671699/data-classes-vs...
- tasubotadas 7y agoThat's not really true - you can set frozen=True and you will get an immutable version of the class.
- EamonnMR 7y agoWe've had great results with Pydantic models. https://pydantic-docs.helpmanual.io/ https://pydantic-docs.helpmanual.io/
- badsectoracula 7y agoThere were many languages which had support for records. Here is an example in Turbo Pascal: program PBook; type EntryRec = record Name: string[32]; Phone: string[30]; end; var Entries: File of EntryRec; Entry: EntryRec; C: Char; I: Longint; begin Assign(Entries, 'entries.dat');Reset(Entries); repeat Write('Phone book, what to do? (Enter, List, Modify, Quit) '); Readln(C); C:=UpCase(C); if C='E' then begin Write('Name: '); Readln(Entry.Name); Write('Phone: '); Readln(Entry.Phone); Seek(Entries, FileSize(Entries)); Write(Entries, Entry); end else if C='L' then begin Seek(Entries, 0); while not Eof(Entries) do begin Read(Entries, Entry); Writeln('Name:', Entry.Name:32, ' Phone:', Entry.Phone:30); end; end else if C='M' then begin Write('Entry number (0..', FileSize(Entries) - 1,'): ');Readln(I); Seek(Entries, I); Read(Entries, Entry); Write('Name (', Entry.Name, '): ');Readln(Entry.Name); Write('Phone (', Entry.Phone, '): ');Readln(Entry.Phone); Seek(Entries, i); Write(Entries, Entry); end; until C='Q'; Close(Entries); end. Many BASIC dialects, like GW-Basic, also had support for record-based files. Though since they often didn't have explicit type support these records were defined in terms of how many fields and characters per field would fit in a record, but still you worked with what was essentially a flat-file database. And of course there were the more high level "4GL" languages that combined a full database and a (mostly) general purpose programming language, like dBase/FoxPro/etc. These also had features like a visual form editor that allowed building data entry and manipulation applications quickly.