Pages

Tuesday, June 28, 2022

Restrict on Drop/Delete table - IBM i

Database files are key for any application. One can never imagine the impact of a database file being accidentally deleted. 

There are ways to recover the data if the file is journaled. But, this would take lot of effort and business disruption. 

With the introduction of RESTRICT ON DROP attribute in IBM i 7.5, we can restrict a file from being deleted accidentally. 

Add Restriction on Drop

To prevent a file/table from being deleted, ADD RESTRICTION ON DROP can be used with ALTER TABLE. 

By adding this restriction a file cannot be deleted both by using DLTF (Delete File) from command line/CL program or by using DROP TABLE from SQL. 

ALTER TABLE <Library>/<File> ADD RESTRICT ON DROP 

E.g.: 

RESTRICT ON DROP



This would add a restriction on REDDYP1/TEST and this file cannot be deleted. Once added, the file cannot be deleted. 

Below error would appear if we try to delete the file from command line. Same would happen with DROP TABLE from SQL. 

RESTRICT ON DROP








Delete file with Restrict drop












Drop Table









DSPFD (Display File Description) command can be used to verify if a file has restriction on drop. DSPFD would contain the below if the restriction is present. 

Restrict on drop  . . . . . . . . . . . . . :            NO

Remove Restriction on Drop 

If the file can/need to be deleted, the restriction need to be removed using DROP RESTRICT ON DROP with ALTER TABLE. 

ALTER TABLE <Library>/<File> DROP RESTRICT ON DROP 

E.g.: 

Drop Restriction on Drop




*This attribute is only available since IBM i 7.5. 
 
If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Introduction to Java Programming

Java

JAVA is one of the most popular languages and widely used. Let's start the journey of learning Java programming with a bit of introduction about Java. 
  • History of Java
  • Uses of Java
  • Features of Java

History of Java

First public version of Java (1.0) was released in 1996 by Sun Microsystems. History of Java goes beyond 1996. James Gosling, Mike Sheridan and Patrick Naughton initiated the Java language project in June 1991. It was initially called as Oak then renamed to Green and finally Java (derived from Java coffee, A type of coffee in Indonesia). Java was designed with C/C++ like syntax so this can be found familiar by the developers. 

On November 13, 2006 Sun released much of it's Java Virtual Machine (JVM) as free and open source software. This process was finished on May 8, 2007. 

Below were the five primary goals behind the creation of Java. 
  • It must be Simple, Object oriented and familiar. 
  • It must be Robust and Secure. 
  • It must be architecture neutral and portable. 
  • It must execute with high performance. 
  • It must be interpreted, threaded and dynamic. 

Uses of Java

Java is widely used for developing.
  • Desktop Applications
  • Web Applications
  • Web Servers 
  • Application Servers
  • Mobile Applications 
  • Games 
This isn't the full list and there are much more applications where Java can be used. 

Features of Java

Platform Independent

Java is compiled and interpreted. Compiler converts the Java program to Bytecode and JVM executes the Bytecode generated by the compiler. Bytecode compiled can be run on any platform making Java platform independent (E.g.: Java code compiled on windows can be run on Mac OS or Linux, Vice versa). 

Object Oriented Programming Language

Object oriented programming is a way that the program is written as a collection of objects. An instance of a class is referred as an Object. 

Below are the primary concepts of Object oriented programming. 
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Simple

Java is easy to learn and use. It does not contain the complex features like pointers, Explicit memory allocation, operator overloading and multiple inheritances making it easy to learn and maintain. 

Java contains the structure similar to C/C++ making it familiar to the developers. 

Robust

Java is Robust as it is designed to catch the errors as early as possible. Java is capable of handling run time errors, supports automatic garbage collection (automatically deallocates the memory blocks) and exception handling. 

Secure

Java programs run in a separate environment independent of the OS, this makes Java more secure. Also, Java does not use the pointers, this makes the Java program secure from secure from flaws like stack corruption or buffer overflow.

Distributed

Java programs can be easily distributed to multiple systems that are connected through internet. Remote Method Invocation and Enterprise Java Beans are used to create distributed applications in Java.

Multithreading

Java supports multithreading, that allows concurrent execution of multiple parts of the program (two or more) for maximum CPU utilization.

Portable

Java is platform independent, it's bytecode can be taken to any platform for execution. 

High Performance

Java architecture is defined to reduce the overhead during the runtime. Java uses JIT (Just In Time) compiler which compiles code on-demand basics, it only compiles the methods that are called in the program. This makes the applications to execute faster. 

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Sunday, June 19, 2022

Opening and Closing a file in Python

 

Open a file

Python has a built-in function open() to open a file. Opening a file can be for different purposes like reading and/or writing data. Open function returns a file object which can be used for reading, writing and/or updating data in a file. 

Syntax

fileobject = open(file, mode, buffering, encoding, errors, newline, closefd, opener)

Among all the parameters mentioned in the syntax, only file is the mandatory parameter. Open function accepts file name and returns a corresponding file object. By using this file object data from the can be read or data can be written to the file (based on the mode specified). 

file

Before we see the importance of all the parameters, let's have a look at an example using just the mandatory parameter 'file'. 

E.g.: 

1

test_file = open("test_file.txt")


In the above example, we are trying to open the text file 'test_file.txt' and read the contents of the file. 

Couple of things to note here is, 
  • We have not mentioned where the file is located. 
  • We have not mentioned the mode (i.e., read, write or update) in which the file is opened. 
If we do not mention the file location, by default Python will look for the file in the same folder where the Python program is located. 

Let's consider if the file doesn't exist on the same folder, mentioning the file name with out the location will throw 'FileNotFoundError'. 

To avoid this error, full path of the file can be specified in the open function. 

E.g.:

1

test_file = open("C:/Users/Admin/Desktop/test_file.txt")


Let's now look at the usage of the optional parameter. 

mode

This parameter is used to specify the mode in which the file is to be opened. Default value for this parameter is 'r' (read). So, if we don't mention the mode Python opens the file in read mode by default. In the above example, test_file.txt gets opened in read mode (same as below example). 

E.g.:

1

test_file = open("test_file.txt", mode = 'r')


One thing to remember here is, value for the mode is case sensitive. Mentioning 'R' instead or 'r' would result in value error (ValueError: invalid mode: 'R').

Below are the valid values for the mode parameter. 
  • 'r' - File is open for reading (default). 
  • 'w' - File is open for writing, truncates the file first. Any data that is already present in the file would be lost as soon as it is open with write mode. Creates the file if not already present. 
  • 'x' - File is open for exclusive creation, failing if the file already exists. If the purpose is to create a new file and write data, 'x' is safer option compared to 'w'.
  • 'a' - open for writing, appending to the end of file if it exists. With 'w', if the file is already present, data is cleared first. With 'a', data in the file is not cleared and any data written is added to the end of file. 
These values can be used as is for the purpose specified. There are few other valid values which can be used in combination with the above values. 

  • 'b' - binary mode, used for binary files. This is used in combination with any of the modes mentioned above. 
    • E.g.: mode = 'rb' for reading binary files. 
    • mode = 'wb' for writing binary files. 
  • 't' - text mode (default), In case of binary files, 'b' has to be explicitly mentioned. 't' is the default value and doesn't need to be mentioned if reading/writing text (i.e., String). 
    • E.g.: mode = 'r', mode = 'rt' would both work for the same purpose of reading text data.
  • '+' - open for updating (reading and writing). With the use of mode 'r', we are only able to read the data from a file and not write vice versa with 'w', 'x' and 'a'. Adding '+' would give the flexibility to do so. 
    • E.g.: mode = 'r+' would allow both read and writing data into the file. 
    • mode = 'w+' would allow both write and read data. 

buffering

Buffering is another optional parameter and determines the buffering policy while reading a file. Buffering policy is different for binary and text files. 

Default value for buffering is '-1' and works as below. 
  • For text files - Uses line buffering. 
  • For binary files - Uses buffer size in fixed chunks based on the default buffer size (can be found using io.DEFAULT_BUFFER_SIZE).
Other valid values are, 
  • '0' - To turn off the buffering. '0' is only valid in case of binary files. 
  • '1' - To select line buffering. '1' is only  valid in case of text files. 
  • Any other other integer > 1 indicates the number of bytes of a fixed size chunk buffer. 4

encoding

Encoding refers to the encoding used to encode/decode the file. This is only applicable for text files. 

Default value for encoding parameter is 'None' i.e., no encoding is ussed. 

'codecs' module has a list of supported encoding methods. Default encoding method is platform dependent (can be found using locale.getpreferredencoding()). 

errors

Errors defines how encoding and decoding errors are handled. This can only be used with text files. 

There are many standard error handlers are available (can be found under Error Handlers). Apart from these any error handling name that is generated with codecs.register_error() are also valid.

Some of the valid values are, 
  • 'strict' -  Raise a ValueError exception if there is an encoding error.
  • 'ignore' - To ignores errors. Ignoring errors could result in a data loss.
  • 'replace' - To replace the malformed data with a replacement marker (such as '?').

newline

Newline determines how a end of line is identified (Universal newlines mode). This can only be used with text files. Valid values are None, '', '\n', '\r', '\n\r'. 
  • '\n' - Line Feed (LF), used as a new line character in Unix/Mac. 
  • '\r' - Carriage Return (CR), used as a new line character in Unix.
  • '\r\n' - Carriage Return (CR) Line Feed (LF), used as a new line character in Windows. 
While reading the file if new line is specified as, 
  • None Universal newlines mode is enabled. Lines in the input can end in '\n''\r', or '\r\n', and these are translated into '\n' before being returned to the caller
  • ' ' - Universal newlines mode is enabled, but line endings are not translated before being returned to the caller.
  •  If any other valid values (i.e., '\n', '\r' or '\r\n'), input lines are only terminated by the given string, and the line endings are not translated before being returned. 
While writing the file if new line is specified as, 
  • None - Any '\n' specified will be converted to the default line separator (default can be found is os.linesep) while writing.  
  • ' ' or '\n' - No translation takes place while writing. 
  • For all other values '\n' is converted to the string specified.

 closefd

Closefd determines if the file description is to be closed or not when the file is closed. 
  • Default value for this parameter is True, File descriptor is also closed when a file is closed. 
  • False - Only file is closed and file descriptor is kept open when file is closed. 

opener

Opener parameter can be used to pass the custom opener instead of the system opener (i.e., os.open). 

Default value is None and is same as passing os.open.

Custom opener must return an open file descriptor. 

Close a file

An opened file must be closed once the file operations (i.e., read/write) are completed. 

close() method can be used to close the file. 

Syntax

fileobject.close()

close method needs to be used with the file object created from the file open(). 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Saturday, June 11, 2022

Working with Save Files - IBM i

Save files (SAVF)

Save file is a type of file in IBM  which allows saving objects (files, programs, save files etc...) and libraries. 

Creating a save file

CRTSAVF (Create Save File) is the command to create a save file. 

CRTSAVF FILE(<library name>/<save file name>) TEXT(<description>)

E.g.:

Create Save File (CRTSAVF)

Saving objects/library into a save file

SAVOBJ (Save Object) is the command to save objects into a save file. 

SAVOBJ OBJ(<objects to save>) LIB(<library where objects are present>) DEV(*SAVF) SAVF(<save file name>)

E.g.: 

Save objects into a save file (SAVOBJ)

In the above command, 
  • OBJ (Objects) parameter is used to specify the objects that need to be saved to a save file. 
  • LIB (Library) parameter is used to specify the library from which objects need to be saved. 
  • DEV (Device) parameter is used to specify the device type (*SAVF for saving into save file).
  • OBJTYPE (Object types) parameter is used to specify what object types are to be considered for saving into save file. 
    • This is very useful when we have different objects with same name and different type (*FILE, *PGM etc...) in the same library. 
    • By default *ALL is considered and all objects with the name specified against OBJ parameter are saved. 
  • SAVF (Save file) parameter is used to specify the save file name and library to which objects are saved.
These are the mandatory parameters need to enter while saving an object. There are few other additional parameters, below are some of them. 
  • TGTRLS (Target Release) parameter is used to specify the release of the operating system on which objects are to be restored to and use the objects. 
    • By default *CURRENT is considered and objects are saved with the current system's OS release. And, objects cannot be restored to any other system which is lower OS release than current system. 
  • SELECT (Select) parameter is used to Include or Omit the objects from the list of objects specified against OBJ parameter. 
  • DTACPR (Data Compression) is used to specify whether data compression is used or not.
Above command is used to save objects into a save file. There is a different command SAVLIB (Save Library), Most of the parameters of this command are same except that SAVOBJ accepts object names and object types and SAVLIB accepts library name and saves the whole library instead. 

SAVLIB LIB(<library to save>) DEV(*SAVF) SAVF(<save file name>)

E.g.:

Save library (SAVLIB) into save file

We often notice a message like 'Save file TESTSAVF in REDDYP1 already contains data. (C G)
when we run SAVOBJ or SAVLIB. This is because the save file already has the data (possibly from the SAVOBJ or SAVLIB run earlier). 

Save library into save file

Valid reply values are - 
  • C - Cancel processing, it will cancel the processing of SAVOBJ or SAVLIB command that has been run. 
  • G - Clear file and continue processing, it will clear the data already present in the save file and continue saving objects/library to the save file. 

Display Save File (DSPSAVF)

In the above example, In order to take option 'C' or 'G', we should know what's already in the save file so we can decide if it can be cleared or not. 

DSPSAVF (Display Save File) can be used to display what objects/library is saved into the save file. 

DSPSAVF FILE(<library name>/<save file name>)

E.g.: 

Display Save file (DSPSAVF)

We have one other parameter 'OUTPUT' which is '*' by default. Using '*' would display the contents of the save file on the screen. Other valid option is '*PRINT' which prints the contents of the save file into spool file. 

Restore Objects/Library from Save file

RSTOBJ (Restore Object) is the command to restore the objects saved in a save file.

RSTOBJ OBJ(<Objects to restore>) SAVLIB(<Library objects were saved from>) DEV(*SAVF) SAVF(<Save file name>) RSTLIB(<Library objects to be restored into>)

E.g.:

Rest Objects from Save file (RSTOBJ) - IBM i

In the above command, 
  • Objects (OBJ) parameter is used to specify the objects need to be restored from the save file. *ALL can be specified to restore all the objects saved, generic* (E.g.: TEST* would consider all objects starting with TEST) to restore objects starting specific text or list of object names can be specified. 
  • Saved Library (SAVLIB) parameter is used to specify the library name from which objects were saved into save file. If not sure, this can be found by using DSPSAVF command. 
  • Object Types (OBJTYPE) parameter is used to specify the objects of specific types that are to be restored from save file. *ALL is the default and restores all object types matching with OBJ.
  • Save File (SAVF) parameter is used to specify the save file name and library. 
One thing to note here is, by using the above command objects are restored into the same library from which objects were saved. This is because of the RSTLIB (Restore to library).

Default value for RSTLIB parameter is *SAVLIB which indicates to restore the objects into the same library it was saved from. For various reasons, we may need to restore the objects to a different library. In this case, we can specify the corresponding library name against RSTLIB parameter.

RSTLIB parameter in RSTOBJ

Above command (RSTOBJ) is used to restore objects saved into a save file. 

RSTLIB (Restore Library) command is used if a library was saved into a save file using SAVLIB command.

RSTLIB SAVLIB(<Library Saved>) DEV(*SAVF) SAVF(<Save file name>) RSTLIB(<Restore to library>)

E.g.:

Restore Library (RSTLIB) - IBM i

Similar to RSTOBJ, by default RSTLIB restores the library with the same name as the library saved. RSTLIB parameter to be specified with different library name if objects are to be restored into different library.

Transfer Save file from IBM i to PC

Often it is essential to transfer save file from one IBM i to another IBM i or IBM i to PC. Save file can be transferred from IBM i to PC by using FTP. 

Below is the FTP script required to transfer the save file from IBM i to PC. 

1

2

3

4

5

6

FTP <IBM i System Name>

User

Password

BIN

GET <Library>/<Save file> <Save file name>

QUIT


Open the command line/terminal in order to run the FTP connection with IBM i. 

In the above script,
  • Line - 1: FTP command to be issued followed by IBM i system name or IP address. This establishes a connection with IBM i and prompts for credentials.
  • Lines 2 & 3: IBM i credentials (User & Password) are to be entered. 
  • Line - 4: BIN command to be issued to set the representation type is Binary IMAGE. 
  • Line - 5: GET command to be issued to get the save file from IBM i library and place into PC. 
    • <Library>/<Save file> - IBM i Library name and Save file name
    • <Save file name> - Name save file to be saved with on PC. 
  • Line - 6: QUIT command to be issued to quit the FTP session.

Transfer Save file from PC to IBM i

Below is the FTP script required to transfer save file from PC to IBM i.

1

2

3

4

5

6

FTP <IBM i System Name>

User

Password

BIN

PUT <Save file name> <Library>/<Save file> 

QUIT


Open the command line/terminal in order to run the FTP connection with IBM i. 

In the above script,
  • Line - 1: FTP command to be issued followed by IBM i system name or IP address. This establishes a connection with IBM i and prompts for credentials.
  • Lines 2 & 3: IBM i credentials (User & Password) are to be entered. 
  • Line - 4: BIN command to be issued to set the representation type is Binary IMAGE. 
  • Line - 5: PUT command to be issued to put the save file from PC and place into IBM i Library. 
    • <Save file name> - Name of the save file on PC. 
    • <Library>/<Save file> - IBM i Library name and Save file name.
  • Line - 6: QUIT command to be issued to quit the FTP session.
One thing to note is if the objects were saved into save file for the higher version of the OS, Objects cannot be restored into a system which is on lower OS release (E.g.: If objects were saved for Target Release V7R4, Objects cannot be restored into V7R3 or lower).


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

OOPs.. Is Java a pure Object Oriented Language?

Object Oriented Programming Object Oriented Programming or OOPs is a programming model which is based on the Objects.  We probably have to t...