Laravel DB::raw COPY - database

im using Laravel 5.5 and I have a very large file that i need to insert its content into the postgres database.
In my research i saw the COPY function from Postgresql documentation. Copy function documentation.
I tested the command below in the PgAdmin and works fine.
COPY requisitions FROM '/srv/www/billing_log' DELIMITER ',';
So i saw that in Laravel we can use something like DB::raw() to execute this commands in postgres. I try using the below code but nothing happens and its show no errors.
public function insertFile()
{
DB::raw("COPY requisitions FROM '/srv/www/billing_log' DELIMITER ','");
return 'OK';
}
The 'OK' is returned and my DB config is working fine on the others methods.
Could somebody help me?

DB::raw doesn't execute anything, it just returns an expression to use in the query builder.
You can use DB::statement() to execute a raw statement.
https://laravel.com/docs/5.6/database#running-queries

Related

Error using DoCmd.TransferDataBase in PowerShell - 'The Table cannot be exported'

I'm trying to export a MS Access table from a .accdb file for distribution and upload to tables on other db files across a network.
My method call looks like this:
$appPath = C:\Users\Me\Desktop\SourceAccessFile.accdb"
$reportpath = "C:\Users\Me\Desktop\DesiredFileName.mdb"
$Access.DoCmd.TransferDatabase([acDataTransferType]::acExport, "Microsoft Access", $appPath, [acObjectType]::acTable, "TPnPDispatchedExport-WC", $reportPath)
With the following $Error value:
'' cannot be imported, exported, or copied to Access database files.
I can't see what's wrong with either of my paths, both echoed and checked. The format of my arguments seems fine but I'm clearly missing something. Does anyone with fresh eyes see what I might be doing wrong here? Glad to share more if it can be useful.

Spark Streaming display (streaming) not working

I follow this example to simulate streaming in Spark from a source file. At the end of the example, a function named display is used which is supported only in databricks. I run my code in Jupyter notebook. What is the alternative in Jupyter to get the same output obtained from display function?
screenshoot_of_the_Example
Update_1:
The code:
# Source
sourceStream=spark.readStream.format("csv").\
option("header",True).\
schema(schema).option("ignoreLeadingWhiteSpace",True).\
option("mode","dropMalformed").\
option("maxFilesPerTrigger",1).load("D:/PHD Project/Paper_3/Tutorials/HeartTest_1/").\
withColumnRenamed("output","label")
#stream test data to the ML model
streamingHeart=pModel.transform(sourceStream).select('label')
I do the following:
streamingHeart.writeStream.outputMode("append").\
format("csv").option("path", "D:/PHD \
Project/Paper_3/Tutorials/sa1/").option("checkpointLocation",\
"checkpoint/filesink_checkpoint").start()\
The problem is that the generated files (output files) are empty. What might be the reason behind that?
I solved the problem by changing the checkpoint, as follow.
Project/Paper_3/Tutorials/sa1/").option("checkpointLocation",\
"checkpoint/filesink_checkpoint_1")

CakePhp 4.x basic Authentication

I am following the CakePHP 4.x tutorial to the letter (as far as I can see) until chapter "CMS Tutorial - Authentication".
Half way through "Now, on every request, the AuthenticationMiddleware will inspect the request session to look for an authenticated user. If we are loading the /users/login page, it will also inspect the posted form data (if any) to extract the credentials."
When I try to access articles or users I get an error:
( ! ) Fatal error: Interface
'Authentication\AuthenticationServiceProviderInterface' not found in
C:\wamp64\www\cake\src\Application.php on line 41
I have tried to figure out why this would be, but I cannot find it. I have tried looking up the same problem on the internet, no dice. Not even a mention that this could be security related (I found a mention about strict brower settings earlier but it was related to another problem).
I have uploaded my code on Github here: https://github.com/plafeber/cakephp-tutorial
I would greatly appreciate any feedback. I was under the assumption that if I create the full code set from the tutorial, given of course I run CakePHP 4.1.5 and follow the related Cake 4.x manual, that it would work. However, I already found out that I have to change the line about the use of DefaultPasswordHasher compared to what was in the code. So I can imagine the Tutorial page is not exactly as it should be.
This would be hte correct line about the use of the DefaultPasswordHasher in User.php;
//the use line
use Cake\Auth\DefaultPasswordHasher as AuthDefaultPasswordHasher;
//and the function
protected function _setPassword(string $password) : ?string
{
if (strlen($password) > 0) {
$hasher = new AuthDefaultPasswordHasher();
return $hasher->hash($password);
}
}
The solution to this was to navigate to the Cake install dir (containing the src and config folder and so on), then running the Composer call again. This apparently placed the filed in the right directories and then the error no longer appeared.

How to verify size and content of the collection in JSON?

I am new to this and have a JSON String returned with an array in it like the following in a Java SpringBoot application.
{... "downlineLevels": ["01","02","03","04","05","06","07"] }
Only the following JUnit tests are successful.
1) .andExpect(jsonPath("$.downlineLevels", hasSize(1)))
I was expecting hasSize(7)
2) .andExpect(jsonPath("$.downlineLevels.[0]", is("01")))
I cannot do $.downlineLevels.[1]. So how can I check all the members.
I found out the problem. The JUnit test was pointing to a different database using another properties file. Sorry for the confusion.

CakePHP Cakemenu plugin fails after global error due to incorrect string encoding

I am using CakePHP 2.1.2 with PHP 5.3.5 and a plugin called 'Cakemenu' which normally works fine. The plugin stores menus in a db table with the menu link stored as text like
array('plugin'=>null,'controller'=>'assets','action'=>'index');
The helper in the plugin gets those values, then executes this code to convert that text to an array:
//Try to evaluate the link (if starts with array)
if (eregi('^array', $value['Menu']['link'])) {
$code = "\$parse = " . $value['Menu']['link'] . ";";
$result = eval($code);
if (is_array($parse)) {
$value['Menu']['link'] = $parse;
}
}
Everything works fine unless CakePHP is handling an error. For example if I mistype the name of a controller in the browser I should get a menu and then the missing controller message. Instead I get a page full "Parse error: syntax error, unexpected $end in..." messages pointing to the line with the eval statement. If I printout the variable that is getting eval'ed I see that it has been (incorrectly) encoded with Html entities when it normally does not.
Good string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'assets','action'=>'index');
Bad string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'Parts','action'=>'add');
To temporarily fix the problem I added two statements to just replace the offending characters
$value['Menu']['link'] = str_replace( ''','\'',$value['Menu']['link']);
$value['Menu']['link'] = str_replace( '>','>',$value['Menu']['link']);
and everything works great again. Some other pieces of information that might be helpful is that the array of data used to generate the menu is read during the beforeFilter of the app and saved in a view variable and then the menu is generated as an element in the view.
I'm thinking that the error causes CakePHP (or PHP) to skip some loading or configuration process and that causes the string to be mishandled. Any help would be appreciated, thanks
Your beforeFilter() method won't be executed on error pages. You'll have to handle your errors yourself and manually call beforeFilter(). I wrote a blog post on how to use custom error pages - pay close attention to the Controller Callbacks section.

Resources