MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Archive for July, 2014

Excel EOMONTH Function

without comments

I fielded a question from blog reader on a three year old post about the EOMONTH (End Of Month) function in Excel. The reader want to write a mega-function (a term for putting two or more functions together to solve a problem, according to Mr. Excel). The function would return the first day of any month for the first day of the month and the first day of the next month for any day after the first day of the last month.

I wrote a quick little solution for the user, as shown below:

=IF(A1=EOMONTH(A1,-1)+1,EOMONTH(A1,-1)+1,EOMONTH(A1,0)+1)

The results would look like:

KevinExcelForumula

The user ran into a problem with the example because the date was being calculated and not an integer value. The formula returned the first day of the next month when it should have returned the first day of the current month. That meant the source value in the cell wasn’t an integer. It was a real number. Integer values in Excel are numeric values for 12:00:00 A.M. of any day, and real numbers can be any time of the day.

Unfortunately, the EOMONTH function is a pseudo-overloaded function, and the EOMONTH function takes an int or double (integer or real number) as it’s parameter. That’s because Excel only supports a NUMBER data type, which can be an integer or real number.

The EOMONTH function always returns an integer. The IF function I provided was designed to compare an integer-based date cell value against the result of the EOMONTH function. It wasn’t robust enough to support a comparison of 11/1/13 at 12:01 A.M. against the result of the EOMONTH function (11/1/13 at 12:00 A.M.). That’s why it returned the first of the next month instead of the first of the current month.

The more complete solution requires using the TRUNC function around the source date (B4 or C4 in the following example) to ensure the IF statement compares dates not time-stamps.

=IF(TRUNC(B4)=EOMONTH(B4,-1)+1,EOMONTH(B4,-1)+1,EOMONTH(B4,0)+1)

KevinExcelFormula2

You should note that the TRUNC function effectively rounds down to the integer and removes the fractional portion of the real number before making a comparison. It’s an explicit casting process when we take proactive measures to ensure the value before the IF function compares it.

Written by maclochlainn

July 26th, 2014 at 11:20 pm

PostgreSQL New Database

with one comment

How time flies, last March I explained how to install and configure PostgreSQL on Windows. It was my intent to start posting more content on PostgreSQL but I was distracted by another writing commitment on Oracle Database 12c PL/SQL Advanced Programming Techniques, which should be available in November. It tempted me away from PostgreSQL because I got to write about how to use Java inside Oracle Database 12c, which was fun. Having completed that, I’m back on task. Here’s the second entry on PostgreSQL. It shows you howto create your own database, database administrator role, user, and how to connect with psql CLI (Command Line Interface) as the new user.

  1. Create a user-defined video_db tablespace for your database. This requires that you know where the physical files where created when you installed PostgreSQL. You can discover the directory with the following query:
SELECT   setting AS "Data Location"
FROM     pg_settings
WHERE    name = 'data_directory';
            Data Location
--------------------------------------
 C:/Program Files/PostgreSQL/9.3/data
(1 row)

You create the video_db tablespace with the following syntax:

CREATE TABLESPACE video_db
  OWNER postgres
  LOCATION 'C:\Program Files\PostgreSQL\9.3\data';

You can check the presence of the video_db tablespace after creating it with the following query:

SELECT * FROM pg_tablespace;

It should print:

  spcname   | spcowner | spcacl | spcoptions
------------+----------+--------+------------
 pg_default |       10 |        |
 pg_global  |       10 |        |
 video_db   |       10 |        |
(3 rows)

It’s important to note for those new to PostgreSQL that the pg_global and pg_default tablespaces are creating when initializing the database. The pg_global holds shared tables and the pg_default holds everything else.

  1. Create a database that uses your user-defined video_db tablespace with the following two commands:
CREATE DATABASE videodb
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = video_db
       LC_COLLATE = 'English_United States.1252'
       LC_CTYPE = 'English_United States.1252'
       CONNECTION LIMIT = -1;
 
COMMENT ON DATABASE videodb
  IS 'VideoDB';
  1. Create a database role, grant the super user privileges to the role, and create a user with the role. You can do that with the following three commands:
CREATE ROLE dba
  WITH SUPERUSER;
 
GRANT ALL PRIVILEGES ON DATABASE videodb TO dba;
 
CREATE USER video
  WITH ROLE dba
       PASSWORD 'video';
  1. Connect to the new videodb database with the psql CLI as the video user. You can do that with the following OS command:
psql -d videodb -U video
  1. Once connected as the new video user, you can use a system information function to determine the current database:
SELECT current_database();

It should display:

 current_database
------------------
 videodb
(1 row)

There are many privilege options, and you should choose wisely which ones you use. As always, I hope this answers questions for other users.

Written by maclochlainn

July 24th, 2014 at 3:22 pm

Hiding a Java Source

with one comment

The ability to deploy Java inside the Oracle database led somebody to conclude that the source isn’t visible in the data catalog. Then, that person found that they were wrong because the Java source is visible when you use a DDL command to CREATE, REPLACE, and COMPILE the Java source. This post discloses how to find the Java source and how to prevent it from being stored in the data catalog.

You can verify that the Java class and source files exist with the following query:

1
2
3
4
5
6
7
8
COLUMN object_name FORMAT A20 HEADING "Object Name"
COLUMN object_type FORMAT A12 HEADING "Object Type"
COLUMN status      FORMAT A14 HEADING "Object Status"
SELECT   object_name
,        object_type
,        status
FROM     user_objects
WHERE    object_name = 'ReadFile';

It displays:

Object Name          Object Type  Object Status
-------------------- ------------ --------------
ReadFile             JAVA SOURCE  VALID
ReadFile             JAVA CLASS   VALID
 
2 rows selected.

Then, you can use the following query to discovery a Java library created by a SQL command:

1
2
3
4
5
6
COLUMN line FORMAT 9999 HEADING "Line|#"
COLUMN text FORMAT A66  HEADING "Text"
SELECT   line
,        text
FROM     user_source
WHERE    name = 'ReadFile';

It displays the following:

 Line # Text
------- ------------------------------------------------------------------
    1   // Java library imports.
    2   import java.io.File;
    3   import java.io.BufferedReader;
    4   import java.io.FileNotFoundException;
    5   import java.io.IOException;
    6   import java.io.FileReader;
    7   import java.security.AccessControlException;
    8
    9   // Class definition.
   10   public class ReadFile {
   11     // Define class variables.
   12     private static File file;
   13     private static FileReader inTextFile;
   14     private static BufferedReader inTextReader;
   15     private static StringBuffer output = new StringBuffer();
   16     private static String outLine, outText;
   17
   18     // Define readText() method.
   19     public static String readText(String fromFile)
   20       throws AccessControlException, IOException {
   21       // Read file.
   22       try {
   23         // Initialize File.
   24         file = new File(fromFile);
   25
   26         // Check for valid file.
   27         if (file.exists()) {
   28
   29           // Assign file to a stream.
   30           inTextFile = new FileReader(file);
   31           inTextReader = new BufferedReader(inTextFile);
   32
   33           // Read character-by-character.
   34           while ((outLine = inTextReader.readLine()) != null) {
   35             output.append(outLine + "\n"); }
   36
   37           // Assing the StringBuffer to a String.
   38           outText = Integer.toString(output.toString().length());
   39
   40           // Close File.
   41           inTextFile.close(); }
   42         else {
   43           outText = new String("Empty"); }}
   44       catch (IOException e) {
   45         outText = new String("");
   46         return outText; }
   47     return outText; }}
 
47 rows selected.

You can eliminate the source by compiling the Java library outside the database. Then, you use the loadjava utility to load the only the class file into the data catalog. The syntax would be the following command for an importer user in a video Pluggable Database (PDB):

loadjava -r -f -o -user importer/importer@video ReadFile.class

You should know that this syntax is disallowed by the loadjava utility, notwithstanding it’s found in the Oracle Database 12c documentation:

loadjava -r -f -o -user importer@video/importer ReadFile.class

You can verify that only the Java class file exists with the following query:

1
2
3
4
5
6
7
8
COLUMN object_name FORMAT A20 HEADING "Object Name"
COLUMN object_type FORMAT A12 HEADING "Object Type"
COLUMN status      FORMAT A14 HEADING "Object Status"
SELECT   object_name
,        object_type
,        status
FROM     user_objects
WHERE    object_name = 'ReadFile';

It displays:

Object Name          Object Type  Object Status
-------------------- ------------ --------------
ReadFile             JAVA CLASS   VALID
 
1 row selected.

Hope this helps those who want to hide the Java source files.

Written by maclochlainn

July 22nd, 2014 at 10:23 pm

Visual Studio Freedom

without comments

Some of my students want to learn to write C++ on a Windows OS. At some point, you simply surrender to the fact that many people prefer the Windows OS as a starting point. Traditionally, I’d recommend the Microsoft Visual Studio as their learning vehicle because it’s free to our students because of our relationship with Microsoft. It’s not free to graduates. When a graduate contacts me I recommend Code::Blocks. Just recently, one ask for more than a recommendation. He wanted instructions. These are the instructions.

Download and Install Code::Blocks

Here are the instructions to install and download Code::Blocks 13.12.

CodeBlocks_01

  1. You can download Download and Install Code::Blocks from the web site. Then, you can install Code::Blocks from download folder.

CodeBlocks_02

  1. You launch the codeblocks-12.12mingw-setup.exe file from the download folder.

CodeBlocks_03

  1. This is the first screen of the Code::Blocks wizard. Click the Next button to continue.

CodeBlocks_04

  1. This is the license agreement. Click the I Agree button to continue.

CodeBlocks_05

  1. This screen shows the components to install. The default has all of them checked. I recommend you keep the default. Click the Next button to continue.

CodeBlocks_06

  1. This screen lets you accept the default install location or to chose another install location. I recommend you keep the default install location. Click the Install button to continue.

CodeBlocks_07

  1. This screen shows you the extracting and deployment of files. Click the Next button to continue.

CodeBlocks_08

  1. This indicates that you’ve installed the product, and it now prompts you to run Code::Blocks programscreen shows you the extracting and deployment of files. Click the Yes button to continue.

CodeBlocks_09

  1. This indicates that the installer detects a GNU GCC Compiler. Click the OK button to continue.

CodeBlocks_10

  1. This acknowledges the completion of the installation. Click the OK button to continue.

CodeBlocks_9a

  1. This dialog tells you the installation has completed. Click the Next button to continue.

CodeBlocks_9b

  1. This dialog completes the installation. Click the Finish button to end the installation.

Configure Code::Blocks

CodeBlocks_11

  1. This is the main menu of Code::Blocks application. You need to click the Create a new project link to continue.

CodeBlocks_12

  1. You should choose a Console application for this example. Double click the Console application icon to continue.

CodeBlocks_13

  1. This dialog launches the Console page. Check the Skip this page next time and then click the Next button to continue.

CodeBlocks_14

  1. This dialog lets you choose whether you want to write a C and C++ program. Click the Next button to continue.

CodeBlocks_15

  1. This dialog lets you choose whether you want to write a C and C++ program. Click the Next button to continue.

CodeBlocks_16

  1. This dialog lets you choose the compiler, debug, and release configurations. Click the Finish button to continue.

Create and run a program in Code::Blocks

CodeBlocks_17

  1. This dialog gives you an empty project. Click the Sources item in the list of the Project tab.

CodeBlocks_18

  1. This dialog adds a main.cpp to the list of the Management console. Click the main.cpp item in the list of the Project tab.

CodeBlocks_19

  1. After clicking the main.cpp item in the Project list, the Code::Blocks IDE generates the content of the main.cpp file. Click the Green Arrow to run (compile and execute) the main.cpp file. The endl is a defined in std, and like a \n line terminator.

CodeBlocks_20

  1. A new file hasn’t been compiled (or built), which means you get the following dialog. It wants to know if you want to build the program. Click the Yes button to build the program.

CodeBlocks_21

  1. After making the file, the Build Log tells you whether the code compiles or not. Click the Run green arrow to run the program.

CodeBlocks_22

  1. After running a Console application, you’ll see the output of your program in a console window like the following. You can close it by clicking the console window’s close button.

CodeBlocks_23

  1. After executing the compiled file, the Build Log tells you whether the program compiles successfully or not.

As always, I hope this helps those who read it. Good luck learning to write C/C++ programs.

Written by maclochlainn

July 5th, 2014 at 1:48 am

Posted in C/C++ Programming

Tagged with

Fedora VMWare Upgrade

with 2 comments

When a new update of VMWare comes out, and it is time to upgrade VMWare Tools. Here’s an update on the instructions for upgrading VMWare Tools 6.0.1 through 6.0.4:

  1. Navigate to the VMWare Menu, choose Virtual Machine and in the drop down menu Install VMWare Tools. This will mount a virtual CD in the Oracle Unbreakable Linux virtual machine and it launches the following dialog box:
VMware962

  1. Open a terminal session by right clicking anywhere in the desktop, and then choose Open in Terminal from the context menu. You can then run the VMWare Toolkit by following these instructions as the root user:

The instructions for VMWare 6.0.0 through 6.0.2 are:

cd /media/VMware\ Tools
cp VMwareTools-9.6.2-1688356.tar.gz /tmp
cd /tmp
gunzip VMwareTools-9.6.2-1688356.tar.gz
tar -xvf VMwareTools-9.6.2-1688356.tar
cd vmware-tools-distrib
sudo ./vmware-install.pl

VMWare changed where the VMWare Tools CD are mounted. You can discover it by clicking on the VMware Tools in the left pane (this assumes you log on to Fedora as the student user, and the student user is a sudo-enabled user)

VMWare6.0.4

The instructions for VMWare Tools 6.0.4 forward are listed below. Only the first command changes. You should also note that the VMWare Tools library is the same:

cd /run/media/student/VMware\ Tools
cp VMwareTools-9.6.2-1688356.tar.gz /tmp
cd /tmp
gunzip VMwareTools-9.6.2-1688356.tar.gz
tar -xvf VMwareTools-9.6.2-1688356.tar
cd vmware-tools-distrib
sudo ./vmware-install.pl

The last step requires that you reply to a set of prompts. If you’d like to accept the default at one time, you can use the following command:

sudo ./vmware-install.pl --default

Lastly, you’ll get these instructions form the Perl script that installs the VMWare tools:

The configuration of VMware Tools 9.6.2 build-1688356 for Linux for this 
running kernel completed successfully.
 
You must restart your X session before any mouse or graphics changes take 
effect.
 
You can now run VMware Tools by invoking "/usr/bin/vmware-toolbox-cmd" from the
command line.
 
To enable advanced X features (e.g., guest resolution fit, drag and drop, and 
file and text copy/paste), you will need to do one (or more) of the following:
1. Manually start /usr/bin/vmware-user
2. Log out and log back into your desktop session; and,
3. Restart your X session.
 
Enjoy,
 
--the VMware team

Written by maclochlainn

July 3rd, 2014 at 2:00 am

Posted in Fedora,Linux,VMWare

Tagged with , ,