00:51
Manifest0 left
|
|||
thowe | what the idiomatic way to say "$this unless it is false, in that case nil". | 01:35 | |
I want to return NIL if it is an empty string instead of an empty string | 01:36 | ||
SmokeMachine | m: say “” || Nil | 01:47 | |
camelia | Nil | ||
SmokeMachine | m: say “bla” || Nil | ||
camelia | bla | ||
SmokeMachine | But why do you want Nil? | 01:48 | |
thowe | because it is going in to a database column that I don't want empty strings, I want NULL. Nil apparently translates to NULL. | 01:51 | |
Now, what I think I actually want to do is subclass array so that when the element is an Empty Str it returns Nil. That seems cleaner. | 01:55 | ||
SmokeMachine | Why not Str? | 02:05 | |
If using Red (github.com/FCO/Red), you could just not set the value… | 02:08 | ||
ab5tract | thowe: I’m a bit surprised at that behavior to be honest. I would expect a database driver / library to treat undefined objects as NULL on insert | 02:42 | |
thowe | Empty strings don't insert as other data types. They are also less convenient to detect as lack of data. Nil inserting as NULL makes the most sense. | 07:25 | |
SmokeMachine | thowe: I meant the type object Str | 07:29 | |
09:13
dakkar joined
|
|||
monkeyinthejuice | How do I making ARRAY of HASH | 10:26 | |
nahita3882 | hi, [ %(a => 2, b => 3), %(e => 8, a => 77, f => 0) ] is an array of hashes | 10:53 | |
%(...) is one way of making a Hash literal; I'd say it's the most common and unambigous (unlike with {} maybe) | 10:54 | ||
[...] is making an array | |||
and we assign this to my @a = ... for example, and now @a is an Array | 10:55 | ||
@ "sigil" is signaling that the variable is "Positional", i.e., it is a container that you can index it with natural numbers; the default class that does the Positional role is Array, therefore @a is of type Array | 10:57 | ||
its cousin is List -- it also does positional but the main difference is it's immutable (its size shall never change, you cannot really assign to its elements) | 11:00 | ||
and you-cannot-assign-to-elements of Lists is achieved by lack of "scalar containers" on the elements of a List unlike an Array | 11:01 | ||
docs.raku.org/language/containers#...sty_things | 11:02 | ||
one way of making a List is my @b is List = ... | 11:05 | ||
it says it wants a Positional that is a List (not the default Array) | |||
monkeyinthejuice | How do I making ARRAY of ARRAY of hash | 11:20 | |
nahita3882 | if you have Xs (X1, X2, ...) and you do [X1, X2, ...] it's an array of Xs | 11:39 | |
14:31
Manifest0 joined
|
|||
rcmlz | Is shrek a real person or a bot? | 15:32 | |
thowe | SmokeMachine: well... I suppose one needs the ability to store an empty string if required, so silently changing that to NULL is probably not a good default(?) | 15:47 | |
ab5tract | thowe: Str is not representing an empty string. It represents an undefined string | 15:55 | |
I would change the logic that produces the data to generate Str instead of an empty string | 15:56 | ||
I don’t know what db library you are using but I wouldn’t personally use one that doesn’t treat an insert of Str into a string column as a NULL | 15:57 | ||
thowe | I'm getting the empty string by splitting a line of comma separated values. When nothing is between the commas, and using the array element to insert into the database, it is used as an empty string. | 15:58 | |
monkeyinthejuice | Why does person talking begin with <letters> | ||
@Raku bridge why | |||
ab5tract | m: my Str @a; @a[4] = “value”; dd @a | ||
camelia | Str = Array[Str].new(Str, Str, Str, Str, "value") | ||
lizmat | monkeyinthejuice otherwise on the IRC side we wouldn't be able to see who said what | 16:00 | |
ab5tract | m: my @a = “,,,,”.split(“,”).map: { $_ || Str }; dd @a | ||
camelia | [Str, Str, Str, Str, Str] | ||
ab5tract | The point is that the db driver should be smart enough to go Str (or any undefined type object that matches the column type)-> NULL | 16:01 | |
thowe | ab5tract: well, that may be. I'm not sure I know enough about potential issues to have a strong opinion. I think you would have to take that up with the author(s) of DB::Pg. | 16:03 | |
My script is quick and dirty, and I am using it as an excuse to learn a couple random things. A more mature version will abstract that data through validation and an intermediate data construct. in the meantime, I wanted to create a code example for myself of overloading the "[]" postcircumfix operator for an array subclass to spit out Nil instead of an undefined string, I think. | 16:05 | ||
ab5tract | thowe: Are you saying that you have already tried inserting undefined strings (not empty ones) using DB::Pg and the result is not a NULL value in the respective db column? | 16:06 | |
thowe | I've tried whatever ends up in the array. I'm not 100% sure if they are empty or undefined. | 16:07 | |
standby | |||
16:08
MasterDuke joined
|
|||
librasteve | m: enum DBCode <NULL>; sub nullify (@a) { @a.map: {$_ ?? $_ !! NULL} }; ['a','b','',Nil,Str].&nullify.map(*.raku.say); | 16:08 | |
Raku eval | "a" "b" DBCode::NULL DBCode::NULL DBCode::NULL | ||
SmokeMachine | Im almost sure undefined Str will end up as NULL on Pg… I use that on Red, if I’m not completely mistaken… | 16:09 | |
ab5tract | So if you want to override [], you can define an AT-POS method in a class. This will give you control of [] on scalar instances of that class ($foo[]). Storing instances in @foo requires doing the Positional role, which may be a bit more involved (haven’t done this in a while) | 16:11 | |
thowe | m: dd split(',', "this,is,a,line,,,only") | ||
camelia | ("this", "is", "a", "line", "", "", "only").Seq | ||
thowe | I mean, those look like empty strings to me. | 16:12 | |
right? | |||
librasteve | ^^ if what you really want is to make "" be NULL (along with other undefined such as Nil), then testing for Truthiness does that --- personally I would want to store an empty string as "" in the DB which is different from undefined, of course | ||
ab5tract | For a quick and dirty script, my impulse would be to wrap split instead and return Str (or Nil if you really need that) instead of empty strings | ||
thowe: so map the result like I did above and voila? | 16:13 | ||
thowe | the original data is not meant to be an empty Str. It is meant to be "no data". And the empty Str that results is not suitable for most of the DB columns; NULL is. | 16:14 | |
ab5tract | I’ve only been saying that an Str should serve the same purpose as Nil as regards to the database insert. | ||
And yet I have never said an empty string should result in a NULL | 16:15 | ||
librasteve | sorry - that's my misinterpretation of the original question --- I would defer to ab5tract and smokemachine on the best answer | 16:16 | |
ab5tract | m: dd Str === “”; dd Str ~~ “” | ||
camelia | Bool::False Bool::False |
||
thowe | Nor am I saying that. An intentionally inserted empty Str should be an empty string in the DB. The problem is I have empty Str when what I want is Nil. This data should not be empty Str in the database, it should be NULL. Therefor I need to correct the fact that they are empty Strings. | ||
SmokeMachine | I have no Pg here now... but on SQLite type objects seems to be NULL: www.irccloud.com/pastebin/8dGGoj2B/ | 16:17 | |
ab5tract | m: dd split(',', "this,is,a,line,,,only").map: { $_ || Str } | 16:18 | |
camelia | ("this", "is", "a", "line", Str, Str, "only").Seq | ||
thowe | OK. I have no opinion on that. If you think DB::Pg has funny behavior you will have to take that up with someone else. | 16:19 | |
SmokeMachine | empty string is not Str type object... Str type object makes more sense to be inserted on DB than Nil to represent NULL | ||
16:19
MasterDuke left
|
|||
ab5tract | For dealing with the kinds of frustrations that will result from trying to differentiate (‘“”,””’) from (‘,’), I think using Text::CSV is probably the best option | 16:19 | |
thowe | Perhaps, but my goal at the moment was education | 16:20 | |
SmokeMachine | Accepting Str as NULL is not a "funny behaviour", but the "correct" one... | ||
thowe | SmokeMachine: as I said, I have no opinion on that. You will have to talk to the PG::Db people. | 16:21 | |
er DB::Pg | |||
ab5tract | thowe:from what you have written there has been no indication that you have actually tried to insert an Str type object | 16:22 | |
thowe | I'm just working with it. | ||
According to .WHAT, the empty portions are Str | |||
ab5tract | I hope you can see how this might be frustrating to the people here trying to aid you in your education? | ||
SmokeMachine | Have you tried inserting Str instead of “”? | ||
ab5tract | Are you just ignoring every example I have posted? Because that is also frustrating | 16:23 | |
thowe | m: my @line = split(',', "this,is,a,line,,,only"); @line[4].WHAT; | ||
camelia | ( no output ) | ||
SmokeMachine | We are not saying objects of type Str (that is the case of “”), but the Str object type it self… | 16:24 | |
m: say Str # <- this | |||
camelia | (Str) | ||
thowe | well that doesn't help.. in the REPL that says (Str). So yes. a Str that is empty is what it is inserting. And it is landing in the DB as an empty Str. Which is what I would expect. | ||
SmokeMachine | They are 2 different things: “” and Str… | 16:25 | |
m: say “”; say Str | |||
camelia | (Str) |
||
thowe | But I don't want empty strings. I want NULL. Inserting Nil is what works. So I want to convert those empty strings to Nil. I'm not sure what has you upset. When I split it I get "". When I look at that field with WHAT it says it is a Str. So I appear to have an empty Str, not an undefined Str. So what. converting to Nil gets me the behavior I want and the DB columns the data I want in them. | 16:28 | |
SmokeMachine | m: multi to-db(Str:D $str) { $str }; multi to-db(Str:U $str) { “NULL” }; say to-db “”; say to-db Str; # thowe, this | ||
camelia | NULL |
||
ab5tract | m: dd Str === “”; dd Str ~~ “”; dd “” ~~ Str; | ||
camelia | Bool::False Bool::False Bool::True |
||
ab5tract | So just put a Nil instead? You got the answer for how to do that on your first asking it. | 16:29 | |
SmokeMachine | thowe: I’m not upset, I’m trying to explain to you that you are getting it wrong… you are thinking I’m saying to you to sent “” to the database, and that’s NOT what I’m saying… I’m saying to you to send Str (and not “”) to db… | 16:30 | |
thowe | That's what I am doing! I was simply asking for info on the right syntax to achieve that in a couple of ways. Others tried to make it a bigger deal than that, not me. | 16:31 | |
I'm not sure what the difference is between sending "" and Sending Str is. Both appear to be what is in that data, and the result is not what I want. I am changing to Nil and getting what I want. So I'm not sure I much care what the esoteric difference between "" and Str that says it is "" is. | 16:32 | ||
SmokeMachine | docs.raku.org/syntax/Type%20Objects | 16:33 | |
ab5tract | This is because it is not idiomatic to use Nil as the undefined value for a string. The bigger deal part is that multiple people have tried demonstrating in multiple ways what the distinction is. | ||
SmokeMachine | That’s the point… “” is a value, Str is the type itself… | ||
thowe | all I wanted to know was a good idiomatic way to convert it to Nil. The simplest thing seems to be the map previously suggested. | 16:34 | |
Yeah I get that Str is an object. And that "" is a value. The Array elem is a Str with value of "". | |||
ab5tract | m: my Str $f; dd $f; dd $f = “value”; dd $f = Nil | ||
SmokeMachine | A good idiomatic way is to NOT use Nil… | ||
camelia | Str $f = Str Str $f = "value" Str $f = Str |
||
thowe | But Nil does what I want, and in my head, sending Nil when I want the DB column to be NULL makes perfect sense. | 16:35 | |
SmokeMachine | So does Str | ||
thowe | OK, that isn't doing what I want... | ||
I'm not going to "fix" DB::Pg, I'm going to get the behavior I want and move on... | 16:36 | ||
SmokeMachine | Because you are sending “” instead of Str | ||
thowe | I'm sending @splitline[1] | ||
SmokeMachine | That’s not DB::Pg fault… if you send Str it will (probably) work! | ||
thowe | if [1] has a Str with value "" the DB thinks I am inserting an empty string. | 16:37 | |
SmokeMachine | Try sending @splitline[1] || Str | ||
I’m not saying to send Str with any value, but Str | |||
thowe | How does that make more sense than @splitline[1] || Nil ? | ||
when what I want in the DB is NULL. | 16:38 | ||
SmokeMachine | Because Str is an undefined string… | ||
thowe | The DB fields are all different things, Numerics, timestamps, geographic polygons... I often want them to be NULL. Why would I send Str? | 16:39 | |
SmokeMachine | Please try that and see if that works (you can change it back if you don’t like it… I’m just trying to show you that works) | ||
thowe | But I don't understand why I would care? | ||
SmokeMachine | So, it would be better to send Any… | ||
If you don’t want to understand type objects, that’s ok… but type objects are VERY used in raku and that would help you to understand that… but if tou don’t care, I’m ok with that | 16:41 | ||
ab5tract | @splitline[1] || Nil will break for integer values of 0, for example | ||
you will be sending Nil instead of 0, which may not be what you want | |||
thowe | OK, I won't be, but that's a good argument. | 16:42 | |
ab5tract | also, as I tried to demonstrate a moment ago, assigning Nil to a type-constrained container results in setting that container to the type object itself | 16:43 | |
m: dd my Str $s = Nil | |||
camelia | Str $s = Str | ||
SmokeMachine | m: my Int $a is default(42); my $a = 13; say $a; $a = Nil; say $a; # thowe, be careful with this | ||
camelia | Potential difficulties: Redeclaration of symbol '$a'. at <tmp>:1 ------> my Int $a is default(42); my $a<HERE> = 13; say $a; $a = Nil; say $a; # thowe 13 42 |
||
SmokeMachine | 16:43 <SmokeMachine> m: my Int $a is default(42); $a = 13; say $a; $a = Nil; say $a; # thowe, be careful with this | 16:44 | |
m: my Int $a is default(42); $a = 13; say $a; $a = Nil; say $a; # thowe, be careful with this | |||
camelia | 13 42 |
||
SmokeMachine | thowe: ^^ | ||
Nil is not NULL nor undef… | 16:45 | ||
thowe | I didn't think it was, I just knew that inserting it in the DB resulted in a NULL column, which was what I wanted. This is a big table with a lot of data, it will take me some time to get a test going. Right now just testing the || Str | 16:50 | |
at first bluch it might work as my error moved elsewhere to some data that is malformed... | 16:52 | ||
blush | |||
ab5tract | gist.github.com/ab5tract/89c42a1eb...00e6dfceb6 | 17:01 | |
this is probably how I would approach a quick'n'dirty "schema'd CSV parser" | |||
*would start to approach | 17:02 | ||
17:40
dakkar left
|