Feeds:
Posts
Comments

Now that 4Dv11 SQL supports float data type; it would be nice to match that with a C_FLOAT. Using C_REAL throws an error for incompatible assignment at runtime.

I.e. if your database has a field with float data type don’t plan on using it with native 4D code, and don’t expect the complier to warn you. See my feature request with 4D.

C_REAL($vr_test)
` throws error (from GET LAST ERROR STACK): 54 4DRT Argument types are incompatible.
$vr_test:=[Table]float_field

There is an Is Float constant to compare to GET FIELD PROPERTIES data type return longint.

And while we’re at it, why are SQL data type values different from GET FIELD PROPERTIES data type values.

psql via command line does not have an option for password. To run a scheduled task using psql is pointless without full automation.

Warning postgres recommends against doing this, and instead use a password file.

I use this script to kick off the psql command in python. But you can execute psql straight from the batch file, just check the %ERRORLEVEL% batch variable from the calling method.

@echo off

REM scheduled task point to .bat files
REM besides we need to make sure we have system variables in place

REM export a password for use with the system (no quotes)
SET PGHOST=host
SET PGDATABASE=database
SET PGUSER=user
SET PGPASSWORD=user

REM execute psql by file, even though echo is off, errors will still show
psql -X --variable=ON_ERROR_STOP= -1 -w -f filename.sql

This script will monitor a hot folder, take it’s contents and execute the files against a postgres server.

Why post this? This python script in conjunction with a parameter setting batch file can take SQL output and apply it to the postgres database. Originally I was searching how to do this via DOS when I realized I didn’t have all the error catching capability that I wanted.

Developing this came from trying to solve how to systematically apply changes from other systems to one database. This script does not make a distinction between files. So if one system outputs several files that need uploading and separate files target the same record, last loaded is last applied.

I’ve simplified the script a little for ease of posting, this does require system variable PGPASSWORD to run.

import os, glob, shutil

# count
fileCount = 0

# ASSUMES this file is above INCOMING/ BAD/ and ARCHIVE/
filelist = glob.glob("INCOMING/*.sql")

for file in filelist:
    # take the file and thrown it against psql
    # read psql --help for details about options
    # setting ON_ERROR_STOP to nothing tells psql to pass back an error status code
    errorlevel = os.system("psql -X -U some_user -d database --variable=ON_ERROR_STOP= -1 -w -f "+file)
    
    # check for errors (thrown by psql)
    if errorlevel != 0:
        # error was thrown, lets report it and stash the file
        print errorlevel
        shutil.move(file,"BAD/")
    else:
        print file + " processed"
        shutil.move(file,"ARCHIVE/")
        fileCount += 1

print str(fileCount) + " files processed"
Design a site like this with WordPress.com
Get started