Postgres, meteor and fibers? Argggh not friends? - postgresql

I'm trying to use postgres with Meteor using Coffeescript. Unfortunately since two days with no success. There is so much confusion in my head right now..
I'm having the error Error: Meteor code must always run within a Fiber. exactly where I do the Meteor.WrapAsync inside runQuery().
I'm using the npm module 'pg' with npmRequire.
So my service code is like this:
dropTables: =>
identifier= 'Drop tables'
Meteor.wrapAsync(runQuery) "DROP TABLE IF EXISTS " + mapping + " ;" , identifier
console.log 'Finished running query'
runQuery = (theQuery, identifier) =>
pg = Meteor.npmRequire 'pg'
console.log 'About to connect to postgres for ', identifier
pg.connect conString, (err, client, done) ->
_logServices.Info 'About to query for ', identifier
myResult = Meteor.wrapAsync((callback) ->
client.query theQuery , (err, result) ->
if err
_logServices.Error 'Error while '+ identifier +' with the query: ', theQuery
else
_logServices.Info "Success " + identifier
_logServices.Info 'closing connection for ', identifier
callback(null, 1)
)()
done()
pg.end()
console.log "still here!"
What I'm exactly trying to achive is to run the postgres things asyncronusly. In order to do one thing per time, but probably I'm missusing the fibers.

Related

psycopg2: "UnboundLocalError","evalue":"local variable 'connection' referenced before assignment"

I have a psycopg2 connection which I am using to connect to postgresql from pyspark. Here is my code -
host = 'IP Address'
port = 'Port'
user = 'postgres'
db = 'postgres'
password = 'password'
def move_records(main_table,stg_table):
try:
connection = psycopg2.connect(host=host,
database=db,
user=user,
password=password,
#driver = driver,
port = port)
cursor = connection.cursor()
move_query = "INSERT INTO " +main_table+ " select * from "+stg_table+" where country ='USA'"
cursor.execute(move_query)
connection.commit()
logger.debug("Record moved successfully")
except (Exception, psycopg2.DatabaseError) as error :
logger.error("%s Error in transction Reverting all other operations of a transaction ", error)
global flag
flag = False
connection.rollback()
finally:
if(connection):
cursor.close()
connection.close()
logger.debug("PostgreSQL connection is closed")
move_records(table_1,table_2)
But I keep getting error below error on line if(connection):
"UnboundLocalError","evalue":"local variable 'connection' referenced before assignment"
Can not figure out what is the issue. Need help.
I am no expert in Python but I have worked on similar thing, connecting from Python to Postgres in in AWS Lambda using psycopg2.
I believe the error lies somewhere in scope of variable. You need to declare all variables(host, port, user, db, password) inside function once again as global or nonlocal and then try to run function.
For your reference, check out this link:-

Different settings for sys_log in database and log file

i want to deactivate error logs in the database completely no error, no exception and no warning in the sys_log table in the database. I want to use a log file instead - all logging should happen only in this log file. (TYPO3 9 LTS)
Is there a possibility to achieve this?
Thanks
Peter
It seems only possible with an own ExceptionHandler and overloading the method writeLogEntries() in TYPO3\CMS\Core\Error\AbstractExceptionHandler:
protected function writeLogEntries(\Throwable $exception, $context) {
// snip
if ($this->logger) {
$this->logger->critical($logTitle . ': ' . $logMessage, [
'TYPO3_MODE' => TYPO3_MODE,
'exception' => $exception
]);
}
// Write error message to sys_log table
$this->writeLog($logTitle . ': ' . $logMessage);
}
There is no configuration possibility, every exception is written to log file and database.

Mongodb '$where' query using javascript regex

I am trying to reproduce the REPLACE function in sql on mongodb.
My collection 'log' has this data in the 'text' field.
[01]ABC0007[0d0a]BB BABLOXC[0d0a]067989 PLPXBNS[0d0a02]BBR OIC002 L5U0/P AMD KAP 041800 T1200AND 2+00[0d0a0b03]
All I'm trying to do is remove the '[..]' using regex (javascript) and use contains('PLPXBNSBBR') like this so that the expression return true per the javadocs in mongo documentation.
This query below successfully works and returns the matching rows.
db.log.find({"$where":"return this.message.replace(new RegExp('0d0a02'),'').contains('PLPXBNS[]BBR') "});
However, I would need to remove the '[..]' and match like this PLPXBNSBBR.
These are the ones I tried unsuccessfully
db.log.find({"$where" : " return this.message.replace( new
RegExp('\[.*?\]', 'g'), '' ).contains('PLPXBNSBBR') " });
db.log.find({"$where" : " return this.message.replace( new
RegExp('/[(.*?)]', 'g'), '' ).contains('PLPXBNSBBR') " });
db.log.find({"$where" : " return this.message.replace( new
RegExp('//[.*//]'), '' ).contains('PLPXBNSBBR') " });
db.log.find({"$where" : " return this.message.replace( new
RegExp('[.*?]'), '' ).contains('PLPXBNSBBR') " });
From the earlier discussion it appears that if I can pass the pattern as /[[^]]+]/g, it should strip the [..] but it is not doing that and not returning the matching rows.
Okay, I was able to use chaining replace successfully to get my desired results.

MYSQLi PHP connect

Is the following a valid mysqli php connect insert statement?
if($mysql_query = $conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')"))
{
?>
<script>alert('successfully registered ');</script>
<?php
}
else
{
?>
<script>alert('error while registering you...');</script>
<?php
}
}
However I am getting error as "error while registering you..."
Thanks
Issue is resolved. There was an issue with the "user_id" column primary key which was being updated duplicate(0 value) with this error "Duplicate entry '0' for key 'PRIMARY'. Upon updating user_id column with auto increment in phpmyadmin, the INSERT query got updated successfully.
Using the below code to check for errors helped.
die(mysqli_error($conn));
A simple example:
<?php
//Create the connection
$con = mysqli_connect("HostName","UserName","password","DBName") or die("Some error occurred during connection " . mysqli_error($con));
// Write query
$strSQL = "SELECT username FROM MyTable";
// Execute the query.
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["username"]."
";
}
// Close the connection
mysqli_close($con);
?>
Can you paste the whole block with connection statement?
Otherwise SQL statement looks fine. Excepct you should escape inputs to MySQL
if($mysql_query = $conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')"))
Why using if($mysql_query = $conn->query ?
better
if($conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')") === TRUE)

OrientDB.RequestError: Cannot find a command executor for the command request: sql.#11.1

I am experimenting with OrientDB (community edition v1.7-rc2) / Oriento (0.4.0)
The function
function linkChildToParent(oChild, oParent) {
return (
oDB.edge.from(oChild).to(oParent)
.create({"#class": 'OrgUnit_isPartOf_OrgUnit'})
.tap(log)
.return(oChild)
);
}
Fails with an exception
C:\Users\Udo\workspace\NodeOrient\node_modules\oriento\node_modules\bluebird\js\main\async.js:93
throw res.e;
^
OrientDB.RequestError: Cannot find a command executor for the command request: sql.#11.1
at Operation.parseError (C:\Users\Udo\workspace\NodeOrient\node_modules\oriento\lib\transport\binary\protocol\operation.js:806:13)
at Operation.consume (C:\Users\Udo\workspace\NodeOrient\node_modules\oriento\lib\transport\binary\protocol\operation.js:396:35)
at Connection.process (C:\Users\Udo\workspace\NodeOrient\node_modules\oriento\lib\transport\binary\connection.js:324:17)
at Connection.handleSocketData (C:\Users\Udo\workspace\NodeOrient\node_modules\oriento\lib\transport\binary\connection.js:250:17)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:764:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:426:10)
at emitReadable (_stream_readable.js:422:5)
at readableAddChunk (_stream_readable.js:165:9)
From previous event:
at Function.Promise$All [as all] (C:\Users\Udo\workspace\NodeOrient\node_modules\bluebird\js\main\promise.js:193:12)
at generateDependents (C:\Users\Udo\workspace\NodeOrient\setupOrgDB.js:202:35)
From previous event:
at Function.Promise$Join [as join] (C:\Users\Udo\workspace\NodeOrient\node_modules\bluebird\js\main\join.js:118:15)
at BinaryTransport.populateDB (C:\Users\Udo\workspace\NodeOrient\setupOrgDB.js:219:20)
So I debugged into the driver till I found
function createEdge (db, config, from, to) {
var command = "CREATE EDGE",
className, attributes;
config = edgeConfig(config);
className = config[0];
attributes = config[1];
command += ' ' + className + ' FROM ' + edgeReference(from) + ' TO ' + edgeReference(to);
if (attributes) {
command += ' CONTENT ' + JSON.stringify(attributes);
}
return db.query(command);
}
The content of command right before return db.query(command); is
CREATE EDGE OrgUnit_isPartOf_OrgUnit FROM (#11.1) TO (#11.0)
I then used the (browser based) console to verify that OrgUnit_isPartOf_OrgUnit actually inherits from Edge. I also verified that it will link OrgUnit with OrgUnit Vertices and that OrgUnit is derived from Vertex. I also double verified that records #11.1 and #11.0 are actually present in the database.
Then I issued
CREATE EDGE OrgUnit_isPartOf_OrgUnit FROM (#11.1) TO (#11.0)
directly in the console and got
com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException: Cannot find a command executor for the command request: sql.#11:1
This is basically the same exception. With Google I found some Javadoc for this exception. However this did not help me at all.
What is wrong and how can I fix it?
The right command should be without parenthesis. Parenthesis executes sub-queries:
CREATE EDGE OrgUnit_isPartOf_OrgUnit FROM #11:1 TO #11:0
For more information look at Create Edge command.