Fork me on GitHub
#beginners
<
2022-08-26
>
quan xing07:08:45

I use mysql8 + next.jdbc the one of the cloumn type is JSON. In mysql client use update command update JSON is OK , but my programm execute update sql show error:

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Invalid JSON text: "Invalid encoding in string." at position 843 in value for column 'bz_unit_certificate_ui.performance'.
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
        at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:371)
        at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)

        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
        at next.jdbc.result_set$stmt__GT_result_set.invokeStatic(result_set.clj:650)
        at next.jdbc.result_set$stmt__GT_result_set.invoke(result_set.clj:645)
        at next.jdbc.result_set$eval9716$fn__9724.invoke(result_set.clj:925)
        at next.jdbc.protocols$eval8798$fn__8799$G__8789__8808.invoke(protocols.clj:33)
        at next.jdbc$execute_BANG_.invokeStatic(jdbc.clj:250)
        at next.jdbc$execute_BANG_.invoke(jdbc.clj:237)
        at clojure.lang.AFn.applyToHelper(AFn.java:156)
        at clojure.lang.AFn.applyTo(AFn.java:144)
        at clojure.spec.test.alpha$spec_checking_fn$fn__3023.doInvoke(alpha.clj:153)
I copy SQL use #dbg from var and run it in mysql is ok. The SQL not have a gramma question.

hiredman07:08:39

Character encoding issue

hiredman07:08:12

Make sure everything is utf8, and your database columns in MySQL are utf8mb4

quan xing07:08:37

How can I encoding it I copy SQL from memory and run it in mysql client is ok

hiredman07:08:57

Because the act of printing it out and copy and pasting it can change the encoding

quan xing07:08:21

Yes I found it in JSON from memory

quan xing10:08:33

I use clojure.string/replace '\\n' to '\\\n' or replace '\\n' to '\n' It's still get a error

(s/replace (first sql) #"\\n" "\\n")
==> Data truncation: Invalid JSON text: "Invalid encoding in string." if I repalce "\\n" to ";" (other chacter) is successed. How can I escape the "\\n"

Jim Newton09:08:14

can someone help me remember the function names to find prefixes and suffixes of a sequence? e.g. given a list of length n, there's a function that given 5 will find the tail/suffix of the list starting at element number 5. (rest^5 some-seq)

Jim Newton09:08:39

no, i want to effectively call rest 5 times, finding the 5th tail. in common-lisp it is called nth-cdr

tomd09:08:13

I think it's just drop

1
jumar09:08:14

drop

✔️ 1
Jim Newton09:08:33

it is amazing how many small things I forget, not using the language for a while. I was searching for nth-rest and rest-n, and nth-suffix, etc...

tomd09:08:43

nthrest works too

tomd09:08:26

but drop is better if you don't need to eagerly eval the dropped items

delaguardo09:08:54

https://clojuredocs.org is amazing for lookup something you forget about. you probably remember rest and for this function on clojuredocs there is See Also section with other functions with similar or connected behaviour.

4
Jim Newton09:08:06

its for a test case so I don't care about laziness. ahh nthrest, not nth-rest. voilà

Jim Newton09:08:50

thanks delaguardo, I did indeed glance quickly through the doc of rest, mostly at the examples, while also searching for prefix, suffix, etc. didn't get all the way to the see-also section.

Jim Newton09:08:41

does the search field in clojuredocs search only function names, or does it search the text as well? its notable that the doc for nthrest does not contain the word "suffix"

Jim Newton09:08:18

neither does the doc for drop.

lispyclouds09:08:25

maybe its due to drop being lazy and suffix could imply eagerness?

Jim Newton09:08:58

naaaa, lazy suffix is quite easy to understand.

delaguardo09:08:43

@U010VP3UY9X search field on clojuredocs is only for function name. but See Also section does contain docstrings for other functions as well. 1. you wanted a function with some extended behaviour for the rest function 2. lookup rest on clojuredocs, that will give you this page - https://clojuredocs.org/clojure.core/rest 3. Inside of See Also right after examples there are links to drop , nthrest , nthnext 4. and for drop the docstring say “Returns a lazy sequence of all but the first n items in coll.” which sounds to me exactly as you describe what you was looking for. Clojure has it own “standard library” which doesn’t have to be similar to anything you previously had to deal with. This is why there is no mention of “suffix” anywhere.

Jim Newton09:08:41

would be nice if the search, also searched the content (or optionally so). That way someone (anyone) could just add a helpful example containing the word suffix, and problem solved. It is nice that clojuredocs already alows users to add helpful examples.

Jim Newton09:08:49

for example, in the reduce doc there's a helpful example which reminds me of the order of the arguments to the client function [acc item] rather than [item acc] . I use that example often actually.

Alex Miller (Clojure team)12:08:06

clojure.repl/find-docs searches docstrings (and can take a regex)

tschady13:08:28

You might try https://clojure.org/api/cheatsheet. Find the right section, in this case “Seq in, Seq out “, then explore Tail-items rest nthrest next fnext nnext drop drop-while take-last for

1
V12:08:13

How would I output an input-stream into a file? I tried this

(defn input-stream->file [input-stream file-path]
  (with-open [o (io/output-stream file-path)]
    (.write o input-stream)))
but receive an error

lispyclouds12:08:14

also the error could be due the input-stream not being open?

sheluchin16:08:32

Given a long string and a set of subs ranges, how can I return a new string with upper-case applied to each of the given ranges? Is there something that can help with this kind of string transformation or just reduce over it with subs?

Daniel Craig17:08:40

(let [long-string "The rain in spain falls mainly on the plains"
        ranges #{[5 8] [5 15] [2 5]}]
    (mapv (comp clojure.string/upper-case (partial apply subs long-string)) ranges))
This could be an approach? perhaps it could be more readable

dpsutton17:08:24

repeated uses of str will murder performance. You’ll really need a StringBuilder

chronno17:08:31

Maybe something like this?:

(let [to-uppercase? (fn [n uppercase-ranges] (some #(some #{n} (apply range %)) uppercase-ranges))]
  (->> "Aliquam erat volutpat. Nunc eleifend leo vitae magna. In id erat non orci commodo lobortis. Proin neque massa, cursus ut, gravida ut, lobortis eget, lacus."
       (map-indexed (fn [idx char]
                      (if (to-uppercase? idx #{[0 7] [13 21]})
                        (Character/toUpperCase char)
                        char)))
       (apply str)))
Although it is linear, so it would be slow for really long strings.

sheluchin17:08:15

The strings I'm dealing with are quite long, so efficiency is definitely a consideration.

dpsutton17:08:48

You might look into a rope datastructure that can handle changes in the string without continually rebuilding the entire string

👀 2
sheluchin17:08:55

I'm a little surprised this isn't part of one of the string manipulation libraries like https://github.com/funcool/cuerdas or https://github.com/expez/superstring.

dpsutton17:08:49

profile it against a reduce. but in general a rope should be better. But who knows with the immutability. Maybe finding a mutable one or perhaps that offers a transient version?

sheluchin17:08:24

Noted.. will do. Thanks everyone for the input.

pppaul19:08:35

any rule of thumb for when a dev should think of this type of issue (performance of strings)