Archive for the ‘Curiosità’ Category

Errori banali in SQL

Venerdì, Marzo 13th, 2009

Voglio parlare di due errori molto banali che possono essere farti in SQL, soprattutto dai principianti. La particolarità di questi due errori, che sicuramente non saranno gli unici nel loro genere, è che sembrano avere un significato e invece ne hanno un altro. Di conseguenza il server (almeno MySQL, usato per provare le sintassi) eseguirà ciò che gli si chiede di fare, che forse non è ciò che si intendeva realmente.

Creiamo la tabella che useremo per il test:

USE test

CREATE TABLE `t1` (
`c1` INT ,
`c2` INT
)

Inseriamo un record che ci servirà:

INSERT INTO t1 VALUES (1, 2)

Fin qui dovrebbe essere andato tutto bene e lo possiamo verificare così:

SELECT * FROM t1

Uso errato di OR

Questo errore non riguarda solo SQL ma un po’ tutti i linguaggi. Esempio:

SELECT * FROM t1 WHERE c1=100 OR 200

Questa query restituirà il record che abbiamo inserito, nonostante c1 non valga 100 nè 200. Perchè? Perchè l’espressione viene valutata così:

SELECT * FROM t1 WHERE (c1=100) OR 200

Qualsiasi valore intero diverso da 0, se convertito in booleano, risulta TRUE. Di conseguenza il risultato di (c1=100) OR 200 non può che essere TRUE.

La sintassi corretta è:

SELECT * FROM t1 WHERE c1=100 OR c1=200

Oppure:

SELECT * FROM t1 WHERE c1 IN (100, 200)

Errore nella UPDATE

Mi è capitato di vedere una istruzione UPDATE scritta in modo sbagliato e un programmatore alle prime armi disperato:

UPDATE t1 SET c1=100 AND c2=200

Questa istruzione cambia il valore del campo c1 di tutti i record in 0, perchè viene valutata così:

UPDATE t1 SET c1=(100 AND c2=200)

100 è sempre TRUE, di conseguenza l’espressione restituisce TRUE (1) se c2=200, altrimenti FALSE (0).

Chiaramente la sintassi esatta è la seguente:

UPDATE t1 SET c1=100, c2=200

Conclusione

Nei casi (rari e non certo “puliti”) in cui il programmatore voglia scrivere le query che qui ho presentato come errori, ottenendo esattamente ciò che il server effettivamente fa, dovrebbe comunque usare le parentesi. Altrimenti l’istruzione risulterà chiarissima per un computer ma un po’ meno per un essere umano.

1234567890 e altri strani timestamp

Sabato, Febbraio 14th, 2009

La maggior parte dei visitatori di questo blog saprà bene cos’è il timestamp di UNIX. Per gli altri: si tratta di un modo per rappresentare la data e l’ora con un unico numero, usato in ambiente UNIX (quindi anche GNU/Linux) e in programmazione. E’ il numero di secondi trascorsi dal 1 gennaio 1970, momento che viene spesso chiamato UNIX Epoch.

Ho appena letto sul blog di DeadhLord che è da poco scoccata l’ora UNIX 1234567890. Era venerdì 13 febbraio 2009 alle ore 23:31:30.

Allora, come se non fosse ora di andare a letto, ho pensato bene di passarvi la voce e di informarvi di alcune scadenze UNIX passate e future.

Il passato

  • 1000000000 - dom set  9 01:46:40 UTC 2001
  • 1010101010 - gio gen  3 23:36:50 UTC 2002
  • 1111111111 - ven mar 18 01:58:31 UTC 2005

Il futuro

  • 1500000000 - ven lug 14 02:40:00 UTC 2017
  • 2000000000 - mer mag 18 03:33:20 UTC 2033
  • 222222222 - dom gen 16 00:23:42 UTC 1977

La fine del mondo

L’ultima data conosciuta dal terminale di GNU/Linux è 2147483647, che corrisponde a martedì 19 gennaio 2038 alle ore 3:14:07. Dopo… il nulla. Se eseguite il comando:

date -ud@2147483648

Vi risponderà un inquietante “data non valida”.

E allora ricorriamo al comando ddate. Questo programmino converte una data dal nostro calendario al Calendario della Discordia. Chiediamogli come chiama il 19 gennaio 2038, e lui ci risponderà:

Prickle-Prickle, Chaos 19, 3204 YOLD

La savana e il branco di gnu

Venerdì, Febbraio 13th, 2009

Dopo il post dove ho spiegato cos’è uno gnu, completiamo l’argomento.

GNU ha un sito che ospita progetti software liberi: una specie di SourceForge rgorosamente libero. Trattandosi dell’ambiente naturale per gli gnu, non poteva che chiamarsi… Savannah.

Il kernel ufficiale di GNU non è Linux, ma un progetto che non ha ancora raggiunto la maturità: HURD. Si tratta di un doppio anagramma ricorsivo. HURD sta per: Hird of Unix-Replacing Daemons, cioè Hird di demoni che sostituiscono Unix. HIRD sta per Hurd of Interfaces Representing Depth, cioè Hurd di interfacce che rappresentano la profondità. Sia HURD sia HIRD non significano nulla, però si pronunciano presumibilmente come herd, che significa branco. Quindi GNU/Hurd potrebbe essere un “branco di gnu”.

Cos’è uno gnu?

Venerdì, Febbraio 13th, 2009

Tutti sanno… anzi no, tutti dovrebbero sapere che Linux è un kernel, cioè solo una parte (importante) di un sistema operativo completo chiamato GNU. Che poi sarebbe l’acronimo ricorsivo (che chiama sè stesso) di Gnu’s Not Unix. Si, vabbè, ma acronimi a parte: che diavolo è uno gnu???

E’ un animale erbivoro originario dell’Africa, appartenente alla famiglia dei bovidi. Le sue corna sono molto caratteristiche e può essere alto fino a 1 metro e 40 cm. Il logo del progetto GNU è una stilizzazione divertente ma tutto sommato quasi fedele. E’ molto azzeccata perchè coglie un aspetto di questo animale: ha un aspetto molto saggio.

Il nome gli è stato appioppato dai Khoi, che lo pronunciano con la g dura (come del resto i nordamericani).

Ogni anno, a maggio, un milione e mezzo di gnu si spostano dalle savane alle foreste. Tornano in novembre quando le piogge estive hanno rinverdito le pianure. Il che prova che lo gnu è molto più intelligente dell’italiano medio, che non sa assolutamente in quale stagione si raccolgano le zucche.

Purtroppo questi animali hanno ben poco dello spirito di condivisione e solidarietà che anima (animava?) il software libero. Infatti i maschi dominanti marcano il territorio con le feci e con i ferormoni emessi da ghiandole che stanno negli zoccoli (non impressionatevi, lo fa anche il vostro gatto quando struscia le guance sul divano), per spassarsela con le femmine. I maschi sottomessi, invece, se ne vanno dal branco e si riuniscono in gruppi di profughi.

Lo gnu è molto importante per la vegetazione della savana. Infatti le loro feci servono da concime, il loro camminare fa bene alla terra e mangiando potano le piante. Questi animali però a volte diventano un piatto abbondante sulla tavola dei leoni e delle iene.

Pirati contro Micro$oft, 10 a 0

Lunedì, Gennaio 26th, 2009

Ho fatto un whois su microsoft.com, perchè ho bisogno di analizzare i possibili output di questo comando. Il risultato è stato molto divertente. Non sapevo, ad esempio, che alla Micro$oft facessero l’amore con i maiali. Copio l’output per intero. In grassetto le cose interessanti.

$ whois microsoft.com

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.

Server Name: MICROSOFT.COM.ZZZZZZ.MORE.DETAILS.AT.WWW.BEYONDWHOIS.COM
IP Address: 203.36.226.2
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.ZZZZZ.GET.LAID.AT.WWW.SWINGINGCOMMUNITY.COM
IP Address: 69.41.185.194
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.ZZZZZ.DOWNLOAD.MOVIE.ONLINE.ZML2.COM
IP Address: 64.28.187.63
Registrar: DIRECTI INTERNET SOLUTIONS PVT. LTD. D/B/A PUBLICDOMAINREGISTRY.COM
Whois Server: whois.PublicDomainRegistry.com
Referral URL: http://www.PublicDomainRegistry.com

Server Name: MICROSOFT.COM.ZZZOMBIED.AND.HACKED.BY.WWW.WEB-HACK.COM
IP Address: 217.107.217.167
Registrar: ONLINENIC, INC.
Whois Server: whois.onlinenic.com
Referral URL: http://www.OnlineNIC.com

Server Name: MICROSOFT.COM.ZZZ.IS.0WNED.AND.HAX0RED.BY.SUB7.NET
IP Address: 207.44.240.96
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.WILL.LIVE.FOREVER.BECOUSE.UNIXSUCKS.COM
IP Address: 185.3.4.7
Registrar: MELBOURNE IT, LTD. D/B/A INTERNET NAMES WORLDWIDE
Whois Server: whois.melbourneit.com
Referral URL: http://www.melbourneit.com

Server Name: MICROSOFT.COM.WILL.BE.SLAPPED.IN.THE.FACE.BY.MY.BLUE.VEINED.SPANNER.NET
IP Address: 216.127.80.46
Registrar: COMPUTER SERVICES LANGENBACH GMBH DBA JOKER.COM
Whois Server: whois.joker.com
Referral URL: http://www.joker.com

Server Name: MICROSOFT.COM.WILL.BE.BEATEN.WITH.MY.SPANNER.NET
IP Address: 216.127.80.46
Registrar: COMPUTER SERVICES LANGENBACH GMBH DBA JOKER.COM
Whois Server: whois.joker.com
Referral URL: http://www.joker.com

Server Name: MICROSOFT.COM.WAREZ.AT.TOPLIST.GULLI.COM
IP Address: 80.190.192.33
Registrar: EPAG DOMAINSERVICES GMBH
Whois Server: whois.enterprice.net
Referral URL: http://www.enterprice.net

Server Name: MICROSOFT.COM.TOTALLY.SUCKS.S3U.NET
IP Address: 207.208.13.22
Registrar: ENOM, INC.
Whois Server: whois.enom.com
Referral URL: http://www.enom.com

Server Name: MICROSOFT.COM.SOFTWARE.IS.NOT.USED.AT.REG.RU
Registrar: MELBOURNE IT, LTD. D/B/A INTERNET NAMES WORLDWIDE
Whois Server: whois.melbourneit.com
Referral URL: http://www.melbourneit.com

Server Name: MICROSOFT.COM.SHOULD.GIVE.UP.BECAUSE.LINUXISGOD.COM
IP Address: 65.160.248.13
Registrar: GKG.NET, INC.
Whois Server: whois.gkg.net
Referral URL: http://www.gkg.net

Server Name: MICROSOFT.COM.RAWKZ.MUH.WERLD.MENTALFLOSS.CA
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.OHMYGODITBURNS.COM
IP Address: 216.158.63.6
Registrar: DOTSTER, INC.
Whois Server: whois.dotster.com
Referral URL: http://www.dotster.com

Server Name: MICROSOFT.COM.MORE.INFO.AT.WWW.BEYONDWHOIS.COM
IP Address: 203.36.226.2
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.LOVES.ME.KOSMAL.NET
IP Address: 65.75.198.123
Registrar: GODADDY.COM, INC.
Whois Server: whois.godaddy.com
Referral URL: http://registrar.godaddy.com

Server Name: MICROSOFT.COM.LIVES.AT.SHAUNEWING.COM
IP Address: 216.40.250.172
Registrar: ENOM, INC.
Whois Server: whois.enom.com
Referral URL: http://www.enom.com

Server Name: MICROSOFT.COM.IS.NOT.YEPPA.ORG
Registrar: OVH
Whois Server: whois.ovh.com
Referral URL: http://www.ovh.com

Server Name: MICROSOFT.COM.IS.NOT.HOSTED.BY.ACTIVEDOMAINDNS.NET
IP Address: 217.148.161.5
Registrar: ENOM, INC.
Whois Server: whois.enom.com
Referral URL: http://www.enom.com

Server Name: MICROSOFT.COM.IS.IN.BED.WITH.CURTYV.COM
IP Address: 216.55.187.193
Registrar: ABACUS AMERICA, INC. DBA NAMES4EVER
Whois Server: whois.names4ever.com
Referral URL: http://www.names4ever.com

Server Name: MICROSOFT.COM.IS.HOSTED.ON.PROFITHOSTING.NET
IP Address: 66.49.213.213
Registrar: NAME.COM LLC
Whois Server: whois.name.com
Referral URL: http://www.name.com

Server Name: MICROSOFT.COM.IS.GOD.BECOUSE.UNIXSUCKS.COM
IP Address: 161.16.56.24
Registrar: MELBOURNE IT, LTD. D/B/A INTERNET NAMES WORLDWIDE
Whois Server: whois.melbourneit.com
Referral URL: http://www.melbourneit.com

Server Name: MICROSOFT.COM.IS.A.STEAMING.HEAP.OF.FUCKING-BULLSHIT.NET
IP Address: 63.99.165.11
Registrar: ADVANCED INTERNET TECHNOLOGIES, INC.
Whois Server: whois.aitdomains.com
Referral URL: http://www.aitdomains.com

Server Name: MICROSOFT.COM.IS.A.MESS.TIMPORTER.CO.UK
Registrar: MELBOURNE IT, LTD. D/B/A INTERNET NAMES WORLDWIDE
Whois Server: whois.melbourneit.com
Referral URL: http://www.melbourneit.com

Server Name: MICROSOFT.COM.HAS.ITS.OWN.CRACKLAB.COM
IP Address: 209.26.95.44
Registrar: DOTSTER, INC.
Whois Server: whois.dotster.com
Referral URL: http://www.dotster.com

Server Name: MICROSOFT.COM.HAS.A.PRESENT.COMING.FROM.HUGHESMISSILES.COM
IP Address: 66.154.11.27
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.FILLS.ME.WITH.BELLIGERENCE.NET
IP Address: 130.58.82.232
Registrar: CRONON AG BERLIN, NIEDERLASSUNG REGENSBURG
Whois Server: whois.tmagnic.net
Referral URL: http://nsi-robo.tmag.de

Server Name: MICROSOFT.COM.CAN.GO.FUCK.ITSELF.AT.SECZY.COM
IP Address: 209.187.114.147
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Server Name: MICROSOFT.COM.ARE.GODDAMN.PIGFUCKERS.NET.NS-NOT-IN-SERVICE.COM
IP Address: 216.127.80.46
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net

Domain Name: MICROSOFT.COM
Registrar: TUCOWS INC.
Whois Server: whois.tucows.com
Referral URL: http://domainhelp.opensrs.net
Name Server: NS1.MSFT.NET
Name Server: NS2.MSFT.NET
Name Server: NS3.MSFT.NET
Name Server: NS4.MSFT.NET
Name Server: NS5.MSFT.NET
Status: clientDeleteProhibited
Status: clientTransferProhibited
Status: clientUpdateProhibited
Updated Date: 10-oct-2006
Creation Date: 02-may-1991
Expiration Date: 03-may-2014

>>> Last update of whois database: Mon, 26 Jan 2009 02:50:09 EST <<<

NOTICE: The expiration date displayed in this record is the date the
registrar’s sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant’s agreement with the sponsoring
registrar.  Users may consult the sponsoring registrar’s Whois database to
view the registrar’s reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services’ (”VeriSign”) Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability.  VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.

The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.Registrant:
Microsoft Corporation
One Microsoft Way
Redmond, WA 98052
US

Domain name: MICROSOFT.COM

Administrative Contact:
Administrator, Domain  domains@microsoft.com
One Microsoft Way
Redmond, WA 98052
US
+1.4258828080
Technical Contact:
Hostmaster, MSN  msnhst@microsoft.com
One Microsoft Way
Redmond, WA 98052
US
+1.4258828080

Registration Service Provider:
DBMS VeriSign, dbms-support@verisign.com
800-579-2848 x4
Please contact DBMS VeriSign for domain updates, DNS/Nameserver
changes, and general domain support questions.

Registrar of Record: TUCOWS, INC.
Record last updated on 15-Nov-2007.
Record expires on 03-May-2014.
Record created on 02-May-1991.

Registrar Domain Name Help Center:
http://domainhelp.tucows.com

Domain servers in listed order:
NS2.MSFT.NET
NS4.MSFT.NET
NS1.MSFT.NET
NS5.MSFT.NET
NS3.MSFT.NET

Domain status: clientDeleteProhibited
clientTransferProhibited
clientUpdateProhibited

The Data in the Tucows Registrar WHOIS database is provided to you by Tucows
for information purposes only, and may be used to assist you in obtaining
information about or related to a domain name’s registration record.

Tucows makes this information available “as is,” and does not guarantee its
accuracy.

By submitting a WHOIS query, you agree that you will use this data only for
lawful purposes and that, under no circumstances will you use this data to:
a) allow, enable, or otherwise support the transmission by e-mail,
telephone, or facsimile of mass, unsolicited, commercial advertising or
solicitations to entities other than the data recipient’s own existing
customers; or (b) enable high volume, automated, electronic processes that
send queries or data to the systems of any Registry Operator or
ICANN-Accredited registrar, except as reasonably necessary to register
domain names or modify existing registrations.

The compilation, repackaging, dissemination or other use of this Data is
expressly prohibited without the prior written consent of Tucows.

Tucows reserves the right to terminate your access to the Tucows WHOIS
database in its sole discretion, including without limitation, for excessive
querying of the WHOIS database or for failure to otherwise abide by this
policy.

Tucows reserves the right to modify these terms at any time.

By submitting this query, you agree to abide by these terms.

NOTE: THE WHOIS DATABASE IS A CONTACT DATABASE ONLY.  LACK OF A DOMAIN
RECORD DOES NOT SIGNIFY DOMAIN AVAILABILITY.

Aptitude non è la Super Mucca

Giovedì, Gennaio 22nd, 2009

Visualizziamo l’help di aptitude lanciandolo con il parametro –help. Il programma ci darà una serie di informazioni, e alla fine ci dirà di non possedere i poteri della Super Mucca. Questa cosa ci insospettisce, allora proviamo a digitare:

aptitude moo

aptitude a questo punto negherà di avere un easter egg (funzionalità nascosta all’interno di un programma, di solito divertente). Noi, un po’ scettici, digiteremo:

aptitude moo -v

aptitude continuerà a negare l’evidenza. Aggiungete una v. Probabilmente lui continuerà a negare, ma voi continuate aggiungendo una v ogni volta: alla fine crollerà e farà contenti gli estimatori del Piccolo Principe.

Inoltre, per chi non l’avesse notato, esplorando l’interfaccia di aptitude troviamo il Campo Minato.

Stranezze pseudo-booleane

Domenica, Dicembre 21st, 2008

Come già accennato, MySQL non ha un vero tipo booleano. Il tipo BOOL (o BOOLEAN) è in realtà un sinonimo di TINYINY(1). Gli operatori di confronto e le funzioni pseudobooleane restituiscono 0 invece di FALSE e 1 invece di TRUE. Ma veniamo alla stranezza in questione. Provate a eseguire le query seguenti:

SELECT 2 = 1 = 0
SELECT 3 < 2 < 1
SELECT 'a' = 'b' = 'c'
SELECT 0 = 0 != 0

Queste query restituiscono 1. Che cosa succede internamente? Analizziamo solo la prima: una volta capito cosa accade, diverrà chiaro anche per le altre due. Prima MySQL valuta l’espressione più a sinistra, cioè 2=1, la quale ovviamente è falsa, quindi restituisce 0. Dopodichè valuta l’espressione rimanente, cioè 0=0, che essendo vera restituisce 1.
Non si tratta quindi di un vero e proprio bug
, ma il risultato è difficile da interpretare per un essere umano. Stando a quanto si può leggere su Launchpad, Drizzle in qualche modo cercherà di evitare situazioni simili.

Per curiosità ho provato la stessa espressione con Php, che si comporta allo stesso modo ma obbliga a utilizzare le parentesi per evitare espressioni poco chiare. Ad esempio la riga seguente è errata:

echo 2==1==0;

Mentre la riga seguente è corretta e restituisce 1:

echo (2==1)==0;

Il coleottero non è un fisico

Sabato, Ottobre 11th, 2008

“Considerando l’apertura alare e la frequenza del battito delle ali rapportate al peso, è scientificamente provato che un coleottero non può volare. Vola perchè non lo sa.”.

Secondo me lo sa benissimo ma se ne frega. Comunque questa frase è scritta all’ingresso della facoltà di ingegneria aeronautica dell’università di Cambrige.

Una curiosità

Sabato, Agosto 9th, 2008

Ho scoperto un’istruzione non documentata:

SHOW COLUMN TYPES

Se provate a eseguirla (almeno con la 5.0.51, cioè la mia) vedrete che in realtà mostra solo due tipi di dati. Probabilmente non è ancora documentata proprio perchè l’implementazione è ancora incompleta ed è stata inserita per errore. O forse non verrà mai completata: non sono poi così sicuro che sia utile.

Chi scopre altre funzioni non documentate, anche se incomplete, me lo scriva nei commenti o sul forum!